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-matrix-ops.h>
32 #include <libnr/nr-matrix-fns.h>
33 #include <libnr/nr-matrix-translate-ops.h>
34 #include <libnr/nr-scale-matrix-ops.h>
36 #include "libnr/nr-matrix-rotate-ops.h"
37 #include "libnr/nr-matrix-translate-ops.h"
38 #include "libnr/nr-rotate-fns.h"
39 #include "libnr/nr-scale-ops.h"
40 #include "libnr/nr-scale-translate-ops.h"
41 #include "libnr/nr-translate-matrix-ops.h"
42 #include "libnr/nr-translate-scale-ops.h"
43 #include "libnr/nr-convert2geom.h"
44 #include <2geom/transforms.h>
45 #include <2geom/pathvector.h>
47 #include <glib/gmem.h>
49 #include <glibmm/i18n.h>
50 #include "display/nr-arena.h"
51 #include "display/nr-arena-item.h"
52 #include "display/nr-arena-group.h"
53 #include "display/curve.h"
54 #include "display/canvas-bpath.h"
55 #include "sp-item.h"
56 #include "sp-item-group.h"
57 #include "style.h"
58 #include "marker.h"
59 #include "sp-linear-gradient.h"
60 #include "sp-radial-gradient.h"
61 #include "sp-root.h"
62 #include "sp-shape.h"
63 #include "sp-use.h"
64 #include "sp-text.h"
65 #include "sp-flowtext.h"
66 #include "sp-image.h"
67 #include "sp-symbol.h"
68 #include "sp-pattern.h"
69 #include "sp-mask.h"
70 #include "sp-clippath.h"
72 #include <unit-constants.h>
73 #include "helper/png-write.h"
74 #include "helper/pixbuf-ops.h"
76 #include "cairo-renderer.h"
77 #include "cairo-render-context.h"
78 #include "extension/system.h"
80 #include "io/sys.h"
82 #include <cairo.h>
84 // include support for only the compiled-in surface types
85 #ifdef CAIRO_HAS_PDF_SURFACE
86 #include <cairo-pdf.h>
87 #endif
88 #ifdef CAIRO_HAS_PS_SURFACE
89 #include <cairo-ps.h>
90 #endif
92 //#define TRACE(_args) g_printf _args
93 #define TRACE(_args)
94 //#define TEST(_args) _args
95 #define TEST(_args)
97 // FIXME: expose these from sp-clippath/mask.cpp
98 struct SPClipPathView {
99 SPClipPathView *next;
100 unsigned int key;
101 NRArenaItem *arenaitem;
102 NRRect bbox;
103 };
105 struct SPMaskView {
106 SPMaskView *next;
107 unsigned int key;
108 NRArenaItem *arenaitem;
109 NRRect bbox;
110 };
112 namespace Inkscape {
113 namespace Extension {
114 namespace Internal {
116 CairoRenderer::CairoRenderer(void)
117 {}
119 CairoRenderer::~CairoRenderer(void)
120 {
121 /* restore default signal handling for SIGPIPE */
122 #if !defined(_WIN32) && !defined(__WIN32__)
123 (void) signal(SIGPIPE, SIG_DFL);
124 #endif
126 return;
127 }
129 CairoRenderContext*
130 CairoRenderer::createContext(void)
131 {
132 CairoRenderContext *new_context = new CairoRenderContext(this);
133 g_assert( new_context != NULL );
135 new_context->_state_stack = NULL;
136 new_context->_state = NULL;
138 // create initial render state
139 CairoRenderState *state = new_context->_createState();
140 state->transform = Geom::identity();
141 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
142 new_context->_state = state;
144 return new_context;
145 }
147 void
148 CairoRenderer::destroyContext(CairoRenderContext *ctx)
149 {
150 delete ctx;
151 }
153 /*
155 Here comes the rendering part which could be put into the 'render' methods of SPItems'
157 */
159 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
160 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
167 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
168 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
170 /* TODO FIXME: this does not render painting-marker-01-f.svg of SVG1.1 Test suite correctly. (orientation of one of the markers middle left ) */
171 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
172 {
173 NRRect pbox;
175 SPShape *shape = SP_SHAPE(item);
177 if (!shape->curve) return;
179 /* fixme: Think (Lauris) */
180 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
182 SPStyle* style = SP_OBJECT_STYLE (item);
183 CairoRenderer *renderer = ctx->getRenderer();
185 Geom::PathVector const & pathv = shape->curve->get_pathvector();
187 ctx->renderPathVector(pathv, style, &pbox);
189 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
190 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
191 if ( shape->marker[SP_MARKER_LOC_START] ) {
192 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
193 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
195 Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
197 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
198 tr = Geom::Scale(style->stroke_width.computed) * tr;
199 }
201 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
203 Geom::Matrix old_tr = marker_item->transform;
204 marker_item->transform = tr;
205 renderer->renderItem (ctx, marker_item);
206 marker_item->transform = old_tr;
207 }
209 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
210 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
211 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
212 while (curve_it2 != path_it->end_default())
213 {
214 /* Put marker between curve_it1 and curve_it2.
215 * Loop to end_default (so including closing segment), because when a path is closed,
216 * there should be a midpoint marker between last segment and closing straight line segment */
218 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
219 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
221 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
223 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
224 tr = Geom::Scale(style->stroke_width.computed) * tr;
225 }
227 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
229 Geom::Matrix old_tr = marker_item->transform;
230 marker_item->transform = tr;
231 renderer->renderItem (ctx, marker_item);
232 marker_item->transform = old_tr;
234 ++curve_it1;
235 ++curve_it2;
236 }
237 }
239 if ( shape->marker[SP_MARKER_LOC_END] ) {
240 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
241 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
243 /* Get reference to last curve in the path.
244 * For moveto-only path, this returns the "closing line segment". */
245 unsigned int index = path_it->size_default();
246 if (index > 0) {
247 index--;
248 }
249 Geom::Curve const &lastcurve = (*path_it)[index];
251 Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
253 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
254 tr = Geom::Scale(style->stroke_width.computed) * tr;
255 }
257 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
259 Geom::Matrix old_tr = marker_item->transform;
260 marker_item->transform = tr;
261 renderer->renderItem (ctx, marker_item);
262 marker_item->transform = old_tr;
263 }
264 }
265 }
267 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
268 {
269 SPGroup *group = SP_GROUP(item);
270 CairoRenderer *renderer = ctx->getRenderer();
271 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
273 GSList *l = g_slist_reverse(group->childList(false));
274 while (l) {
275 SPObject *o = SP_OBJECT (l->data);
276 if (SP_IS_ITEM(o)) {
277 renderer->renderItem (ctx, SP_ITEM (o));
278 }
279 l = g_slist_remove (l, o);
280 }
281 }
283 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
284 {
285 bool translated = false;
286 SPUse *use = SP_USE(item);
287 CairoRenderer *renderer = ctx->getRenderer();
289 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
290 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
291 ctx->pushState();
292 ctx->transform(&tp);
293 translated = true;
294 }
296 if (use->child && SP_IS_ITEM(use->child)) {
297 renderer->renderItem(ctx, SP_ITEM(use->child));
298 }
300 if (translated) {
301 ctx->popState();
302 }
303 }
305 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
306 {
307 SPText *group = SP_TEXT (item);
308 group->layout.showGlyphs(ctx);
309 }
311 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
312 {
313 SPFlowtext *group = SP_FLOWTEXT(item);
314 group->layout.showGlyphs(ctx);
315 }
317 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
318 {
319 SPImage *image;
320 guchar *px;
321 int w, h, rs;
323 image = SP_IMAGE (item);
325 if (!image->pixbuf) return;
326 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
328 px = gdk_pixbuf_get_pixels (image->pixbuf);
329 w = gdk_pixbuf_get_width (image->pixbuf);
330 h = gdk_pixbuf_get_height (image->pixbuf);
331 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
333 double x = image->x.computed;
334 double y = image->y.computed;
335 double width = image->width.computed;
336 double height = image->height.computed;
338 if (image->aspect_align != SP_ASPECT_NONE) {
339 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
340 &x, &y, &width, &height);
341 }
343 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
344 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
345 }
347 Geom::Translate tp(x, y);
348 Geom::Scale s(width / (double)w, height / (double)h);
349 Geom::Matrix t(s * tp);
351 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
352 }
354 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
355 {
356 SPSymbol *symbol = SP_SYMBOL(item);
357 if (!SP_OBJECT_IS_CLONED (symbol))
358 return;
360 /* Cloned <symbol> is actually renderable */
361 ctx->pushState();
362 ctx->transform(&symbol->c2p);
364 // apply viewbox if set
365 if (0 /*symbol->viewBox_set*/) {
366 Geom::Matrix vb2user;
367 double x, y, width, height;
368 double view_width, view_height;
369 x = 0.0;
370 y = 0.0;
371 width = 1.0;
372 height = 1.0;
374 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
375 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
377 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
378 &x, &y,&width, &height);
380 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
381 vb2user = Geom::identity();
382 vb2user[0] = width / view_width;
383 vb2user[3] = height / view_height;
384 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
385 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
387 ctx->transform(&vb2user);
388 }
390 sp_group_render(item, ctx);
391 ctx->popState();
392 }
394 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
395 {
396 SPRoot *root = SP_ROOT(item);
397 CairoRenderer *renderer = ctx->getRenderer();
399 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
400 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
402 ctx->pushState();
403 renderer->setStateForItem(ctx, item);
404 Geom::Matrix tempmat (root->c2p);
405 ctx->transform(&tempmat);
406 sp_group_render(item, ctx);
407 ctx->popState();
408 }
410 /**
411 This function converts the item to a raster image and includes the image into the cairo renderer.
412 It is only used for filters and then only when rendering filters as bitmaps is requested.
413 */
414 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
415 {
417 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
419 // Calculate resolution
420 double res;
421 /** @TODO reimplement the resolution stuff (WHY?)
422 */
423 res = ctx->getBitmapResolution();
424 if(res == 0) {
425 res = PX_PER_IN;
426 }
427 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
429 // Get the bounding box of the selection in document coordinates.
430 NRRect bbox(item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX ));
432 // The width and height of the bitmap in pixels
433 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * (res / PX_PER_IN));
434 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * (res / PX_PER_IN));
436 // Scale to exactly fit integer bitmap inside bounding box
437 double scale_x = (bbox.x1 - bbox.x0) / width;
438 double scale_y = (bbox.y1 - bbox.y0) / height;
440 // Location of bounding box in document coordinates.
441 double shift_x = bbox.x0;
442 double shift_y = bbox.y1;
444 // For default 90 dpi, snap bitmap to pixel grid
445 if (res == PX_PER_IN) {
446 shift_x = round (shift_x);
447 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
448 // Remove the negations when the inversion is gone.
449 }
451 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
453 // Matix to put bitmap in correct place on document
454 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
455 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
457 // ctx matrix already includes item transformation. We must substract.
458 Geom::Matrix t_item = sp_item_i2d_affine (item);
459 Geom::Matrix t = t_on_document * t_item.inverse();
461 // Do the export
462 SPDocument *document = SP_OBJECT(item)->document;
463 GSList *items = NULL;
464 items = g_slist_append(items, item);
466 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
467 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
469 if (pb) {
470 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
471 unsigned char *px = gdk_pixbuf_get_pixels (pb);
472 unsigned int w = gdk_pixbuf_get_width(pb);
473 unsigned int h = gdk_pixbuf_get_height(pb);
474 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
475 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
476 gdk_pixbuf_unref (pb);
477 }
478 g_slist_free (items);
479 }
482 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
483 {
484 // Check item's visibility
485 if (item->isHidden()) {
486 return;
487 }
489 SPStyle* style = SP_OBJECT_STYLE (item);
490 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
491 return sp_asbitmap_render(item, ctx);
492 }
494 if (SP_IS_ROOT(item)) {
495 TRACE(("root\n"));
496 return sp_root_render(item, ctx);
497 } else if (SP_IS_GROUP(item)) {
498 TRACE(("group\n"));
499 return sp_group_render(item, ctx);
500 } else if (SP_IS_SHAPE(item)) {
501 TRACE(("shape\n"));
502 return sp_shape_render(item, ctx);
503 } else if (SP_IS_USE(item)) {
504 TRACE(("use begin---\n"));
505 sp_use_render(item, ctx);
506 TRACE(("---use end\n"));
507 } else if (SP_IS_SYMBOL(item)) {
508 TRACE(("symbol\n"));
509 return sp_symbol_render(item, ctx);
510 } else if (SP_IS_TEXT(item)) {
511 TRACE(("text\n"));
512 return sp_text_render(item, ctx);
513 } else if (SP_IS_FLOWTEXT(item)) {
514 TRACE(("flowtext\n"));
515 return sp_flowtext_render(item, ctx);
516 } else if (SP_IS_IMAGE(item)) {
517 TRACE(("image\n"));
518 return sp_image_render(item, ctx);
519 }
520 }
522 void
523 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
524 {
525 SPStyle const *style = SP_OBJECT_STYLE(item);
526 ctx->setStateForStyle(style);
528 CairoRenderState *state = ctx->getCurrentState();
529 state->clip_path = item->clip_ref->getObject();
530 state->mask = item->mask_ref->getObject();
531 state->item_transform = Geom::Matrix (item->transform);
533 // If parent_has_userspace is true the parent state's transform
534 // has to be used for the mask's/clippath's context.
535 // This is so because we use the image's/(flow)text's transform for positioning
536 // instead of explicitly specifying it and letting the renderer do the
537 // transformation before rendering the item.
538 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
539 state->parent_has_userspace = TRUE;
540 TRACE(("setStateForItem opacity: %f\n", state->opacity));
541 }
543 void
544 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
545 {
546 ctx->pushState();
547 setStateForItem(ctx, item);
549 CairoRenderState *state = ctx->getCurrentState();
550 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
552 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
553 if (state->need_layer) {
554 state->merge_opacity = FALSE;
555 ctx->pushLayer();
556 }
557 Geom::Matrix tempmat (item->transform);
558 ctx->transform(&tempmat);
559 sp_item_invoke_render(item, ctx);
561 if (state->need_layer)
562 ctx->popLayer();
564 ctx->popState();
565 }
567 bool
568 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
569 {
570 g_assert( ctx != NULL );
572 if (ctx->_vector_based_target) {
573 // width and height in pt
574 ctx->_width = sp_document_width(doc) * PT_PER_PX;
575 ctx->_height = sp_document_height(doc) * PT_PER_PX;
576 } else {
577 ctx->_width = sp_document_width(doc);
578 ctx->_height = sp_document_height(doc);
579 }
581 NRRect d;
582 if (pageBoundingBox || !base) {
583 d.x0 = d.y0 = 0;
584 d.x1 = ceil(ctx->_width);
585 d.y1 = ceil(ctx->_height);
586 } else {
587 sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE);
588 if (ctx->_vector_based_target) {
589 // convert from px to pt
590 d.x0 *= PT_PER_PX;
591 d.x1 *= PT_PER_PX;
592 d.y0 *= PT_PER_PX;
593 d.y1 *= PT_PER_PX;
594 }
595 }
596 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
598 bool ret = ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
600 if (ret && !pageBoundingBox && base)
601 {
602 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
603 (d.y1 - ctx->_height) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
604 ctx->transform(&tp);
606 ctx->_width = d.x1 - d.x0;
607 ctx->_height = d.y1 - d.y0;
608 }
610 return ret;
611 }
613 #include "macros.h" // SP_PRINT_*
615 // Apply an SVG clip path
616 void
617 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
618 {
619 g_assert( ctx != NULL && ctx->_is_valid );
621 if (cp == NULL)
622 return;
624 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
625 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
627 Geom::Matrix saved_ctm;
628 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
629 //SP_PRINT_DRECT("clipd", cp->display->bbox);
630 NRRect clip_bbox(cp->display->bbox);
631 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
632 t[4] = clip_bbox.x0;
633 t[5] = clip_bbox.y0;
634 t *= ctx->getCurrentState()->transform;
635 ctx->getTransform(&saved_ctm);
636 ctx->setTransform(&t);
637 }
639 TRACE(("BEGIN clip\n"));
640 SPObject *co = SP_OBJECT(cp);
641 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
642 if (SP_IS_ITEM(child)) {
643 SPItem *item = SP_ITEM(child);
645 // combine transform of the item in clippath and the item using clippath:
646 Geom::Matrix tempmat (item->transform);
647 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
649 // render this item in clippath
650 ctx->pushState();
651 ctx->transform(&tempmat);
652 setStateForItem(ctx, item);
653 sp_item_invoke_render(item, ctx);
654 ctx->popState();
655 }
656 }
657 TRACE(("END clip\n"));
659 // do clipping only if this was the first call to applyClipPath
660 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
661 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
662 cairo_clip(ctx->_cr);
664 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
665 ctx->setTransform(&saved_ctm);
667 ctx->setRenderMode(saved_mode);
668 }
670 // Apply an SVG mask
671 void
672 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
673 {
674 g_assert( ctx != NULL && ctx->_is_valid );
676 if (mask == NULL)
677 return;
679 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
680 NRRect mask_bbox(mask->display->bbox);
681 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
682 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
683 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
684 t[4] = mask_bbox.x0;
685 t[5] = mask_bbox.y0;
686 t *= ctx->getCurrentState()->transform;
687 ctx->setTransform(&t);
688 }
690 // Clip mask contents... but...
691 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
692 // filters which extend outside the bounding box. So don't clip.
693 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
695 ctx->pushState();
697 TRACE(("BEGIN mask\n"));
698 SPObject *co = SP_OBJECT(mask);
699 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
700 if (SP_IS_ITEM(child)) {
701 SPItem *item = SP_ITEM(child);
702 renderItem(ctx, item);
703 }
704 }
705 TRACE(("END mask\n"));
707 ctx->popState();
708 }
710 void
711 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
712 double *x, double *y, double *width, double *height)
713 {
714 if (aspect_align == SP_ASPECT_NONE)
715 return;
717 double scalex, scaley, scale;
718 double new_width, new_height;
719 scalex = *width / vp_width;
720 scaley = *height / vp_height;
721 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
722 new_width = vp_width * scale;
723 new_height = vp_height * scale;
724 /* Now place viewbox to requested position */
725 switch (aspect_align) {
726 case SP_ASPECT_XMIN_YMIN:
727 break;
728 case SP_ASPECT_XMID_YMIN:
729 *x -= 0.5 * (new_width - *width);
730 break;
731 case SP_ASPECT_XMAX_YMIN:
732 *x -= 1.0 * (new_width - *width);
733 break;
734 case SP_ASPECT_XMIN_YMID:
735 *y -= 0.5 * (new_height - *height);
736 break;
737 case SP_ASPECT_XMID_YMID:
738 *x -= 0.5 * (new_width - *width);
739 *y -= 0.5 * (new_height - *height);
740 break;
741 case SP_ASPECT_XMAX_YMID:
742 *x -= 1.0 * (new_width - *width);
743 *y -= 0.5 * (new_height - *height);
744 break;
745 case SP_ASPECT_XMIN_YMAX:
746 *y -= 1.0 * (new_height - *height);
747 break;
748 case SP_ASPECT_XMID_YMAX:
749 *x -= 0.5 * (new_width - *width);
750 *y -= 1.0 * (new_height - *height);
751 break;
752 case SP_ASPECT_XMAX_YMAX:
753 *x -= 1.0 * (new_width - *width);
754 *y -= 1.0 * (new_height - *height);
755 break;
756 default:
757 break;
758 }
759 *width = new_width;
760 *height = new_height;
761 }
763 #include "clear-n_.h"
765 } /* namespace Internal */
766 } /* namespace Extension */
767 } /* namespace Inkscape */
769 #undef TRACE
771 /* End of GNU GPL code */
773 /*
774 Local Variables:
775 mode:c++
776 c-file-style:"stroustrup"
777 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
778 indent-tabs-mode:nil
779 fill-column:99
780 End:
781 */
782 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :