1 #define __SP_CAIRO_RENDERER_C__
3 /** \file
4 * Rendering with Cairo.
5 */
6 /*
7 * Author:
8 * Miklos Erdelyi <erdelyim@gmail.com>
9 *
10 * Copyright (C) 2006 Miklos Erdelyi
11 *
12 * Licensed under GNU GPL
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #ifndef PANGO_ENABLE_BACKEND
20 #define PANGO_ENABLE_BACKEND
21 #endif
23 #ifndef PANGO_ENABLE_ENGINE
24 #define PANGO_ENABLE_ENGINE
25 #endif
28 #include <signal.h>
29 #include <errno.h>
31 #include "libnr/nr-rect.h"
32 #include <2geom/transforms.h>
33 #include <2geom/pathvector.h>
35 #include <glib/gmem.h>
37 #include <glibmm/i18n.h>
38 #include "display/nr-arena.h"
39 #include "display/nr-arena-item.h"
40 #include "display/nr-arena-group.h"
41 #include "display/curve.h"
42 #include "display/canvas-bpath.h"
43 #include "sp-item.h"
44 #include "sp-item-group.h"
45 #include "style.h"
46 #include "marker.h"
47 #include "sp-linear-gradient.h"
48 #include "sp-radial-gradient.h"
49 #include "sp-root.h"
50 #include "sp-shape.h"
51 #include "sp-use.h"
52 #include "sp-text.h"
53 #include "sp-flowtext.h"
54 #include "sp-image.h"
55 #include "sp-symbol.h"
56 #include "sp-pattern.h"
57 #include "sp-mask.h"
58 #include "sp-clippath.h"
60 #include <unit-constants.h>
61 #include "helper/png-write.h"
62 #include "helper/pixbuf-ops.h"
64 #include "cairo-renderer.h"
65 #include "cairo-render-context.h"
66 #include "extension/system.h"
68 #include "io/sys.h"
70 #include <cairo.h>
72 // include support for only the compiled-in surface types
73 #ifdef CAIRO_HAS_PDF_SURFACE
74 #include <cairo-pdf.h>
75 #endif
76 #ifdef CAIRO_HAS_PS_SURFACE
77 #include <cairo-ps.h>
78 #endif
80 //#define TRACE(_args) g_printf _args
81 #define TRACE(_args)
82 //#define TEST(_args) _args
83 #define TEST(_args)
85 // FIXME: expose these from sp-clippath/mask.cpp
86 struct SPClipPathView {
87 SPClipPathView *next;
88 unsigned int key;
89 NRArenaItem *arenaitem;
90 NRRect bbox;
91 };
93 struct SPMaskView {
94 SPMaskView *next;
95 unsigned int key;
96 NRArenaItem *arenaitem;
97 NRRect bbox;
98 };
100 namespace Inkscape {
101 namespace Extension {
102 namespace Internal {
104 CairoRenderer::CairoRenderer(void)
105 {}
107 CairoRenderer::~CairoRenderer(void)
108 {
109 /* restore default signal handling for SIGPIPE */
110 #if !defined(_WIN32) && !defined(__WIN32__)
111 (void) signal(SIGPIPE, SIG_DFL);
112 #endif
114 return;
115 }
117 CairoRenderContext*
118 CairoRenderer::createContext(void)
119 {
120 CairoRenderContext *new_context = new CairoRenderContext(this);
121 g_assert( new_context != NULL );
123 new_context->_state_stack = NULL;
124 new_context->_state = NULL;
126 // create initial render state
127 CairoRenderState *state = new_context->_createState();
128 state->transform = Geom::identity();
129 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
130 new_context->_state = state;
132 return new_context;
133 }
135 void
136 CairoRenderer::destroyContext(CairoRenderContext *ctx)
137 {
138 delete ctx;
139 }
141 /*
143 Here comes the rendering part which could be put into the 'render' methods of SPItems'
145 */
147 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
148 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
149 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
150 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
159 {
160 NRRect pbox;
162 SPShape *shape = SP_SHAPE(item);
164 if (!shape->curve) return;
166 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
168 SPStyle* style = SP_OBJECT_STYLE (item);
169 CairoRenderer *renderer = ctx->getRenderer();
171 Geom::PathVector const & pathv = shape->curve->get_pathvector();
173 ctx->renderPathVector(pathv, style, &pbox);
175 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
176 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
177 if ( shape->marker[SP_MARKER_LOC_START] ) {
178 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
179 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
181 Geom::Matrix tr;
182 if (marker->orient_auto) {
183 tr = sp_shape_marker_get_transform_at_start(path_it->front());
184 } else {
185 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
186 }
188 bool render = true;
189 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
190 if (style->stroke_width.computed > 1e-9) {
191 tr = Geom::Scale(style->stroke_width.computed) * tr;
192 } else {
193 render = false; // stroke width zero and marker is thus scaled down to zero, skip
194 }
195 }
197 if (render) {
198 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
199 Geom::Matrix old_tr = marker_item->transform;
200 marker_item->transform = tr;
201 renderer->renderItem (ctx, marker_item);
202 marker_item->transform = old_tr;
203 }
204 }
206 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
207 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
208 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
209 while (curve_it2 != path_it->end_default())
210 {
211 /* Put marker between curve_it1 and curve_it2.
212 * Loop to end_default (so including closing segment), because when a path is closed,
213 * there should be a midpoint marker between last segment and closing straight line segment */
215 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
216 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
218 Geom::Matrix tr;
219 if (marker->orient_auto) {
220 tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
221 } else {
222 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
223 }
225 bool render = true;
226 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
227 if (style->stroke_width.computed > 1e-9) {
228 tr = Geom::Scale(style->stroke_width.computed) * tr;
229 } else {
230 render = false; // stroke width zero and marker is thus scaled down to zero, skip
231 }
232 }
234 if (render) {
235 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
236 Geom::Matrix old_tr = marker_item->transform;
237 marker_item->transform = tr;
238 renderer->renderItem (ctx, marker_item);
239 marker_item->transform = old_tr;
240 }
242 ++curve_it1;
243 ++curve_it2;
244 }
245 }
247 if ( shape->marker[SP_MARKER_LOC_END] ) {
248 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
249 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
251 /* Get reference to last curve in the path.
252 * For moveto-only path, this returns the "closing line segment". */
253 unsigned int index = path_it->size_default();
254 if (index > 0) {
255 index--;
256 }
257 Geom::Curve const &lastcurve = (*path_it)[index];
259 Geom::Matrix tr;
260 if (marker->orient_auto) {
261 tr = sp_shape_marker_get_transform_at_end(lastcurve);
262 } else {
263 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
264 }
266 bool render = true;
267 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
268 if (style->stroke_width.computed > 1e-9) {
269 tr = Geom::Scale(style->stroke_width.computed) * tr;
270 } else {
271 render = false; // stroke width zero and marker is thus scaled down to zero, skip
272 }
273 }
275 if (render) {
276 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
277 Geom::Matrix old_tr = marker_item->transform;
278 marker_item->transform = tr;
279 renderer->renderItem (ctx, marker_item);
280 marker_item->transform = old_tr;
281 }
282 }
283 }
284 }
286 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
287 {
288 SPGroup *group = SP_GROUP(item);
289 CairoRenderer *renderer = ctx->getRenderer();
290 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
292 GSList *l = g_slist_reverse(group->childList(false));
293 while (l) {
294 SPObject *o = SP_OBJECT (l->data);
295 if (SP_IS_ITEM(o)) {
296 renderer->renderItem (ctx, SP_ITEM (o));
297 }
298 l = g_slist_remove (l, o);
299 }
300 }
302 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
303 {
304 bool translated = false;
305 SPUse *use = SP_USE(item);
306 CairoRenderer *renderer = ctx->getRenderer();
308 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
309 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
310 ctx->pushState();
311 ctx->transform(&tp);
312 translated = true;
313 }
315 if (use->child && SP_IS_ITEM(use->child)) {
316 renderer->renderItem(ctx, SP_ITEM(use->child));
317 }
319 if (translated) {
320 ctx->popState();
321 }
322 }
324 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
325 {
326 SPText *group = SP_TEXT (item);
327 group->layout.showGlyphs(ctx);
328 }
330 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
331 {
332 SPFlowtext *group = SP_FLOWTEXT(item);
333 group->layout.showGlyphs(ctx);
334 }
336 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
337 {
338 SPImage *image;
339 guchar *px;
340 int w, h, rs;
342 image = SP_IMAGE (item);
344 if (!image->pixbuf) return;
345 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
347 px = gdk_pixbuf_get_pixels (image->pixbuf);
348 w = gdk_pixbuf_get_width (image->pixbuf);
349 h = gdk_pixbuf_get_height (image->pixbuf);
350 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
352 double x = image->x.computed;
353 double y = image->y.computed;
354 double width = image->width.computed;
355 double height = image->height.computed;
357 if (image->aspect_align != SP_ASPECT_NONE) {
358 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
359 &x, &y, &width, &height);
360 }
362 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
363 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
364 }
366 Geom::Translate tp(x, y);
367 Geom::Scale s(width / (double)w, height / (double)h);
368 Geom::Matrix t(s * tp);
370 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
371 }
373 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
374 {
375 SPSymbol *symbol = SP_SYMBOL(item);
376 if (!SP_OBJECT_IS_CLONED (symbol))
377 return;
379 /* Cloned <symbol> is actually renderable */
380 ctx->pushState();
381 ctx->transform(&symbol->c2p);
383 // apply viewbox if set
384 if (0 /*symbol->viewBox_set*/) {
385 Geom::Matrix vb2user;
386 double x, y, width, height;
387 double view_width, view_height;
388 x = 0.0;
389 y = 0.0;
390 width = 1.0;
391 height = 1.0;
393 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
394 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
396 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
397 &x, &y,&width, &height);
399 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
400 vb2user = Geom::identity();
401 vb2user[0] = width / view_width;
402 vb2user[3] = height / view_height;
403 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
404 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
406 ctx->transform(&vb2user);
407 }
409 sp_group_render(item, ctx);
410 ctx->popState();
411 }
413 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
414 {
415 SPRoot *root = SP_ROOT(item);
416 CairoRenderer *renderer = ctx->getRenderer();
418 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
419 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
421 ctx->pushState();
422 renderer->setStateForItem(ctx, item);
423 Geom::Matrix tempmat (root->c2p);
424 ctx->transform(&tempmat);
425 sp_group_render(item, ctx);
426 ctx->popState();
427 }
429 /**
430 This function converts the item to a raster image and includes the image into the cairo renderer.
431 It is only used for filters and then only when rendering filters as bitmaps is requested.
432 */
433 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
434 {
436 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
438 // Calculate resolution
439 double res;
440 /** @TODO reimplement the resolution stuff (WHY?)
441 */
442 res = ctx->getBitmapResolution();
443 if(res == 0) {
444 res = PX_PER_IN;
445 }
446 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
448 // Get the bounding box of the selection in document coordinates.
449 Geom::OptRect bbox =
450 item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
452 if (!bbox) // no bbox, e.g. empty group
453 return;
455 // The width and height of the bitmap in pixels
456 unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
457 unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
459 // Scale to exactly fit integer bitmap inside bounding box
460 double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
461 double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
463 // Location of bounding box in document coordinates.
464 double shift_x = bbox->min()[Geom::X];
465 double shift_y = bbox->max()[Geom::Y];
467 // For default 90 dpi, snap bitmap to pixel grid
468 if (res == PX_PER_IN) {
469 shift_x = round (shift_x);
470 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
471 // Remove the negations when the inversion is gone.
472 }
474 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
476 // Matix to put bitmap in correct place on document
477 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
478 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
480 // ctx matrix already includes item transformation. We must substract.
481 Geom::Matrix t_item = sp_item_i2d_affine (item);
482 Geom::Matrix t = t_on_document * t_item.inverse();
484 // Do the export
485 SPDocument *document = SP_OBJECT(item)->document;
486 GSList *items = NULL;
487 items = g_slist_append(items, item);
489 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
490 bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
491 width, height, res, res, (guint32) 0xffffff00, items );
493 if (pb) {
494 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
495 unsigned char *px = gdk_pixbuf_get_pixels (pb);
496 unsigned int w = gdk_pixbuf_get_width(pb);
497 unsigned int h = gdk_pixbuf_get_height(pb);
498 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
499 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
500 gdk_pixbuf_unref (pb);
501 }
502 g_slist_free (items);
503 }
506 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
507 {
508 // Check item's visibility
509 if (item->isHidden()) {
510 return;
511 }
513 SPStyle* style = SP_OBJECT_STYLE (item);
514 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
515 return sp_asbitmap_render(item, ctx);
516 }
518 if (SP_IS_ROOT(item)) {
519 TRACE(("root\n"));
520 return sp_root_render(item, ctx);
521 } else if (SP_IS_GROUP(item)) {
522 TRACE(("group\n"));
523 return sp_group_render(item, ctx);
524 } else if (SP_IS_SHAPE(item)) {
525 TRACE(("shape\n"));
526 return sp_shape_render(item, ctx);
527 } else if (SP_IS_USE(item)) {
528 TRACE(("use begin---\n"));
529 sp_use_render(item, ctx);
530 TRACE(("---use end\n"));
531 } else if (SP_IS_SYMBOL(item)) {
532 TRACE(("symbol\n"));
533 return sp_symbol_render(item, ctx);
534 } else if (SP_IS_TEXT(item)) {
535 TRACE(("text\n"));
536 return sp_text_render(item, ctx);
537 } else if (SP_IS_FLOWTEXT(item)) {
538 TRACE(("flowtext\n"));
539 return sp_flowtext_render(item, ctx);
540 } else if (SP_IS_IMAGE(item)) {
541 TRACE(("image\n"));
542 return sp_image_render(item, ctx);
543 }
544 }
546 void
547 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
548 {
549 SPStyle const *style = SP_OBJECT_STYLE(item);
550 ctx->setStateForStyle(style);
552 CairoRenderState *state = ctx->getCurrentState();
553 state->clip_path = item->clip_ref->getObject();
554 state->mask = item->mask_ref->getObject();
555 state->item_transform = Geom::Matrix (item->transform);
557 // If parent_has_userspace is true the parent state's transform
558 // has to be used for the mask's/clippath's context.
559 // This is so because we use the image's/(flow)text's transform for positioning
560 // instead of explicitly specifying it and letting the renderer do the
561 // transformation before rendering the item.
562 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
563 state->parent_has_userspace = TRUE;
564 TRACE(("setStateForItem opacity: %f\n", state->opacity));
565 }
567 void
568 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
569 {
570 ctx->pushState();
571 setStateForItem(ctx, item);
573 CairoRenderState *state = ctx->getCurrentState();
574 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
576 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
577 if (state->need_layer) {
578 state->merge_opacity = FALSE;
579 ctx->pushLayer();
580 }
581 Geom::Matrix tempmat (item->transform);
582 ctx->transform(&tempmat);
583 sp_item_invoke_render(item, ctx);
585 if (state->need_layer)
586 ctx->popLayer();
588 ctx->popState();
589 }
591 bool
592 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
593 {
594 g_assert( ctx != NULL );
596 if (!base)
597 base = SP_ITEM(sp_document_root(doc));
599 NRRect d;
600 if (pageBoundingBox) {
601 d.x0 = d.y0 = 0;
602 d.x1 = ceil(sp_document_width(doc));
603 d.y1 = ceil(sp_document_height(doc));
604 } else {
605 sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
606 }
608 if (ctx->_vector_based_target) {
609 // convert from px to pt
610 d.x0 *= PT_PER_PX;
611 d.x1 *= PT_PER_PX;
612 d.y0 *= PT_PER_PX;
613 d.y1 *= PT_PER_PX;
614 }
616 ctx->_width = d.x1-d.x0;
617 ctx->_height = d.y1-d.y0;
619 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
621 bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
623 if (ret && !pageBoundingBox)
624 {
625 double high = sp_document_height(doc);
626 if (ctx->_vector_based_target)
627 high *= PT_PER_PX;
629 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
630 (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
631 ctx->transform(&tp);
632 }
634 return ret;
635 }
637 #include "macros.h" // SP_PRINT_*
639 // Apply an SVG clip path
640 void
641 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
642 {
643 g_assert( ctx != NULL && ctx->_is_valid );
645 if (cp == NULL)
646 return;
648 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
649 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
651 Geom::Matrix saved_ctm;
652 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
653 //SP_PRINT_DRECT("clipd", cp->display->bbox);
654 NRRect clip_bbox(cp->display->bbox);
655 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
656 t[4] = clip_bbox.x0;
657 t[5] = clip_bbox.y0;
658 t *= ctx->getCurrentState()->transform;
659 ctx->getTransform(&saved_ctm);
660 ctx->setTransform(&t);
661 }
663 TRACE(("BEGIN clip\n"));
664 SPObject *co = SP_OBJECT(cp);
665 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
666 if (SP_IS_ITEM(child)) {
667 SPItem *item = SP_ITEM(child);
669 // combine transform of the item in clippath and the item using clippath:
670 Geom::Matrix tempmat (item->transform);
671 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
673 // render this item in clippath
674 ctx->pushState();
675 ctx->transform(&tempmat);
676 setStateForItem(ctx, item);
677 sp_item_invoke_render(item, ctx);
678 ctx->popState();
679 }
680 }
681 TRACE(("END clip\n"));
683 // do clipping only if this was the first call to applyClipPath
684 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
685 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
686 cairo_clip(ctx->_cr);
688 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
689 ctx->setTransform(&saved_ctm);
691 ctx->setRenderMode(saved_mode);
692 }
694 // Apply an SVG mask
695 void
696 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
697 {
698 g_assert( ctx != NULL && ctx->_is_valid );
700 if (mask == NULL)
701 return;
703 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
704 NRRect mask_bbox(mask->display->bbox);
705 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
706 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
707 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
708 t[4] = mask_bbox.x0;
709 t[5] = mask_bbox.y0;
710 t *= ctx->getCurrentState()->transform;
711 ctx->setTransform(&t);
712 }
714 // Clip mask contents... but...
715 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
716 // filters which extend outside the bounding box. So don't clip.
717 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
719 ctx->pushState();
721 TRACE(("BEGIN mask\n"));
722 SPObject *co = SP_OBJECT(mask);
723 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
724 if (SP_IS_ITEM(child)) {
725 SPItem *item = SP_ITEM(child);
726 renderItem(ctx, item);
727 }
728 }
729 TRACE(("END mask\n"));
731 ctx->popState();
732 }
734 void
735 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
736 double *x, double *y, double *width, double *height)
737 {
738 if (aspect_align == SP_ASPECT_NONE)
739 return;
741 double scalex, scaley, scale;
742 double new_width, new_height;
743 scalex = *width / vp_width;
744 scaley = *height / vp_height;
745 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
746 new_width = vp_width * scale;
747 new_height = vp_height * scale;
748 /* Now place viewbox to requested position */
749 switch (aspect_align) {
750 case SP_ASPECT_XMIN_YMIN:
751 break;
752 case SP_ASPECT_XMID_YMIN:
753 *x -= 0.5 * (new_width - *width);
754 break;
755 case SP_ASPECT_XMAX_YMIN:
756 *x -= 1.0 * (new_width - *width);
757 break;
758 case SP_ASPECT_XMIN_YMID:
759 *y -= 0.5 * (new_height - *height);
760 break;
761 case SP_ASPECT_XMID_YMID:
762 *x -= 0.5 * (new_width - *width);
763 *y -= 0.5 * (new_height - *height);
764 break;
765 case SP_ASPECT_XMAX_YMID:
766 *x -= 1.0 * (new_width - *width);
767 *y -= 0.5 * (new_height - *height);
768 break;
769 case SP_ASPECT_XMIN_YMAX:
770 *y -= 1.0 * (new_height - *height);
771 break;
772 case SP_ASPECT_XMID_YMAX:
773 *x -= 0.5 * (new_width - *width);
774 *y -= 1.0 * (new_height - *height);
775 break;
776 case SP_ASPECT_XMAX_YMAX:
777 *x -= 1.0 * (new_width - *width);
778 *y -= 1.0 * (new_height - *height);
779 break;
780 default:
781 break;
782 }
783 *width = new_width;
784 *height = new_height;
785 }
787 #include "clear-n_.h"
789 } /* namespace Internal */
790 } /* namespace Extension */
791 } /* namespace Inkscape */
793 #undef TRACE
795 /* End of GNU GPL code */
797 /*
798 Local Variables:
799 mode:c++
800 c-file-style:"stroustrup"
801 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
802 indent-tabs-mode:nil
803 fill-column:99
804 End:
805 */
806 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :