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 "libnrtype/Layout-TNG.h"
33 #include <2geom/transforms.h>
34 #include <2geom/pathvector.h>
36 #include <glib/gmem.h>
38 #include <glibmm/i18n.h>
39 #include "display/nr-arena.h"
40 #include "display/nr-arena-item.h"
41 #include "display/nr-arena-group.h"
42 #include "display/curve.h"
43 #include "display/canvas-bpath.h"
44 #include "sp-item.h"
45 #include "sp-item-group.h"
46 #include "style.h"
47 #include "marker.h"
48 #include "sp-linear-gradient.h"
49 #include "sp-radial-gradient.h"
50 #include "sp-root.h"
51 #include "sp-shape.h"
52 #include "sp-use.h"
53 #include "sp-text.h"
54 #include "sp-flowtext.h"
55 #include "sp-image.h"
56 #include "sp-symbol.h"
57 #include "sp-pattern.h"
58 #include "sp-mask.h"
59 #include "sp-clippath.h"
61 #include <unit-constants.h>
62 #include "helper/png-write.h"
63 #include "helper/pixbuf-ops.h"
65 #include "cairo-renderer.h"
66 #include "cairo-render-context.h"
67 #include "extension/system.h"
69 #include "io/sys.h"
71 #include <cairo.h>
73 // include support for only the compiled-in surface types
74 #ifdef CAIRO_HAS_PDF_SURFACE
75 #include <cairo-pdf.h>
76 #endif
77 #ifdef CAIRO_HAS_PS_SURFACE
78 #include <cairo-ps.h>
79 #endif
81 //#define TRACE(_args) g_printf _args
82 #define TRACE(_args)
83 //#define TEST(_args) _args
84 #define TEST(_args)
86 // FIXME: expose these from sp-clippath/mask.cpp
87 struct SPClipPathView {
88 SPClipPathView *next;
89 unsigned int key;
90 NRArenaItem *arenaitem;
91 NRRect bbox;
92 };
94 struct SPMaskView {
95 SPMaskView *next;
96 unsigned int key;
97 NRArenaItem *arenaitem;
98 NRRect bbox;
99 };
101 namespace Inkscape {
102 namespace Extension {
103 namespace Internal {
105 CairoRenderer::CairoRenderer(void)
106 : _omitText(false)
107 {}
109 CairoRenderer::~CairoRenderer(void)
110 {
111 /* restore default signal handling for SIGPIPE */
112 #if !defined(_WIN32) && !defined(__WIN32__)
113 (void) signal(SIGPIPE, SIG_DFL);
114 #endif
116 return;
117 }
119 CairoRenderContext*
120 CairoRenderer::createContext(void)
121 {
122 CairoRenderContext *new_context = new CairoRenderContext(this);
123 g_assert( new_context != NULL );
125 new_context->_state_stack = NULL;
126 new_context->_state = NULL;
128 // create initial render state
129 CairoRenderState *state = new_context->_createState();
130 state->transform = Geom::identity();
131 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
132 new_context->_state = state;
134 return new_context;
135 }
137 void
138 CairoRenderer::destroyContext(CairoRenderContext *ctx)
139 {
140 delete ctx;
141 }
143 /*
145 Here comes the rendering part which could be put into the 'render' methods of SPItems'
147 */
149 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
150 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
157 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
161 {
162 bool render = true;
163 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
164 if (style->stroke_width.computed > 1e-9) {
165 tr = Geom::Scale(style->stroke_width.computed) * tr;
166 } else {
167 render = false; // stroke width zero and marker is thus scaled down to zero, skip
168 }
169 }
171 if (render) {
172 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
173 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
174 Geom::Matrix old_tr = marker_item->transform;
175 marker_item->transform = tr;
176 ctx->getRenderer()->renderItem (ctx, marker_item);
177 marker_item->transform = old_tr;
178 }
179 }
181 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
182 {
183 NRRect pbox;
185 SPShape *shape = SP_SHAPE(item);
187 if (!shape->curve) return;
189 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
191 SPStyle* style = SP_OBJECT_STYLE (item);
193 Geom::PathVector const & pathv = shape->curve->get_pathvector();
194 if (pathv.empty()) return;
196 ctx->renderPathVector(pathv, style, &pbox);
198 // START marker
199 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
200 if ( shape->marker[i] ) {
201 SPMarker* marker = SP_MARKER (shape->marker[i]);
202 Geom::Matrix tr;
203 if (marker->orient_auto) {
204 tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
205 } else {
206 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
207 }
208 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
209 }
210 }
211 // MID marker
212 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
213 if ( !shape->marker[i] ) continue;
214 SPMarker* marker = SP_MARKER (shape->marker[i]);
215 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
216 // START position
217 if ( path_it != pathv.begin()
218 && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
219 {
220 Geom::Matrix tr;
221 if (marker->orient_auto) {
222 tr = sp_shape_marker_get_transform_at_start(path_it->front());
223 } else {
224 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
225 }
226 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
227 }
228 // MID position
229 if (path_it->size_default() > 1) {
230 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
231 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
232 while (curve_it2 != path_it->end_default())
233 {
234 /* Put marker between curve_it1 and curve_it2.
235 * Loop to end_default (so including closing segment), because when a path is closed,
236 * there should be a midpoint marker between last segment and closing straight line segment */
237 Geom::Matrix tr;
238 if (marker->orient_auto) {
239 tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
240 } else {
241 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
242 }
244 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
246 ++curve_it1;
247 ++curve_it2;
248 }
249 }
250 // END position
251 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
252 Geom::Curve const &lastcurve = path_it->back_default();
253 Geom::Matrix tr;
254 if (marker->orient_auto) {
255 tr = sp_shape_marker_get_transform_at_end(lastcurve);
256 } else {
257 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
258 }
259 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
260 }
261 }
262 }
263 // END marker
264 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
265 if ( shape->marker[i] ) {
266 SPMarker* marker = SP_MARKER (shape->marker[i]);
268 /* Get reference to last curve in the path.
269 * For moveto-only path, this returns the "closing line segment". */
270 Geom::Path const &path_last = pathv.back();
271 unsigned int index = path_last.size_default();
272 if (index > 0) {
273 index--;
274 }
275 Geom::Curve const &lastcurve = path_last[index];
277 Geom::Matrix tr;
278 if (marker->orient_auto) {
279 tr = sp_shape_marker_get_transform_at_end(lastcurve);
280 } else {
281 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
282 }
284 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
285 }
286 }
287 }
289 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
290 {
291 SPGroup *group = SP_GROUP(item);
292 CairoRenderer *renderer = ctx->getRenderer();
293 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
295 GSList *l = g_slist_reverse(group->childList(false));
296 while (l) {
297 SPObject *o = SP_OBJECT (l->data);
298 if (SP_IS_ITEM(o)) {
299 renderer->renderItem (ctx, SP_ITEM (o));
300 }
301 l = g_slist_remove (l, o);
302 }
303 }
305 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
306 {
307 bool translated = false;
308 SPUse *use = SP_USE(item);
309 CairoRenderer *renderer = ctx->getRenderer();
311 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
312 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
313 ctx->pushState();
314 ctx->transform(&tp);
315 translated = true;
316 }
318 if (use->child && SP_IS_ITEM(use->child)) {
319 renderer->renderItem(ctx, SP_ITEM(use->child));
320 }
322 if (translated) {
323 ctx->popState();
324 }
325 }
327 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
328 {
329 SPText *group = SP_TEXT (item);
330 group->layout.showGlyphs(ctx);
331 }
333 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
334 {
335 SPFlowtext *group = SP_FLOWTEXT(item);
336 group->layout.showGlyphs(ctx);
337 }
339 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
340 {
341 SPImage *image;
342 guchar *px;
343 int w, h, rs;
345 image = SP_IMAGE (item);
347 if (!image->pixbuf) return;
348 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
350 px = gdk_pixbuf_get_pixels (image->pixbuf);
351 w = gdk_pixbuf_get_width (image->pixbuf);
352 h = gdk_pixbuf_get_height (image->pixbuf);
353 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
355 double x = image->x.computed;
356 double y = image->y.computed;
357 double width = image->width.computed;
358 double height = image->height.computed;
360 if (image->aspect_align != SP_ASPECT_NONE) {
361 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
362 &x, &y, &width, &height);
363 }
365 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
366 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
367 }
369 Geom::Translate tp(x, y);
370 Geom::Scale s(width / (double)w, height / (double)h);
371 Geom::Matrix t(s * tp);
373 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
374 }
376 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
377 {
378 SPSymbol *symbol = SP_SYMBOL(item);
379 if (!SP_OBJECT_IS_CLONED (symbol))
380 return;
382 /* Cloned <symbol> is actually renderable */
383 ctx->pushState();
384 ctx->transform(&symbol->c2p);
386 // apply viewbox if set
387 if (0 /*symbol->viewBox_set*/) {
388 Geom::Matrix vb2user;
389 double x, y, width, height;
390 double view_width, view_height;
391 x = 0.0;
392 y = 0.0;
393 width = 1.0;
394 height = 1.0;
396 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
397 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
399 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
400 &x, &y,&width, &height);
402 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
403 vb2user = Geom::identity();
404 vb2user[0] = width / view_width;
405 vb2user[3] = height / view_height;
406 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
407 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
409 ctx->transform(&vb2user);
410 }
412 sp_group_render(item, ctx);
413 ctx->popState();
414 }
416 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
417 {
418 SPRoot *root = SP_ROOT(item);
419 CairoRenderer *renderer = ctx->getRenderer();
421 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
422 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
424 ctx->pushState();
425 renderer->setStateForItem(ctx, item);
426 Geom::Matrix tempmat (root->c2p);
427 ctx->transform(&tempmat);
428 sp_group_render(item, ctx);
429 ctx->popState();
430 }
432 /**
433 This function converts the item to a raster image and includes the image into the cairo renderer.
434 It is only used for filters and then only when rendering filters as bitmaps is requested.
435 */
436 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
437 {
439 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
441 // Calculate resolution
442 double res;
443 /** @TODO reimplement the resolution stuff (WHY?)
444 */
445 res = ctx->getBitmapResolution();
446 if(res == 0) {
447 res = PX_PER_IN;
448 }
449 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
451 // Get the bounding box of the selection in document coordinates.
452 Geom::OptRect bbox =
453 item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
455 if (!bbox) // no bbox, e.g. empty group
456 return;
458 // The width and height of the bitmap in pixels
459 unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
460 unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
462 // Scale to exactly fit integer bitmap inside bounding box
463 double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
464 double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
466 // Location of bounding box in document coordinates.
467 double shift_x = bbox->min()[Geom::X];
468 double shift_y = bbox->max()[Geom::Y];
470 // For default 90 dpi, snap bitmap to pixel grid
471 if (res == PX_PER_IN) {
472 shift_x = round (shift_x);
473 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
474 // Remove the negations when the inversion is gone.
475 }
477 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
479 // Matix to put bitmap in correct place on document
480 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
481 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
483 // ctx matrix already includes item transformation. We must substract.
484 Geom::Matrix t_item = sp_item_i2d_affine (item);
485 Geom::Matrix t = t_on_document * t_item.inverse();
487 // Do the export
488 SPDocument *document = SP_OBJECT(item)->document;
489 GSList *items = NULL;
490 items = g_slist_append(items, item);
492 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
493 bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
494 width, height, res, res, (guint32) 0xffffff00, items );
496 if (pb) {
497 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
498 unsigned char *px = gdk_pixbuf_get_pixels (pb);
499 unsigned int w = gdk_pixbuf_get_width(pb);
500 unsigned int h = gdk_pixbuf_get_height(pb);
501 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
502 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
503 gdk_pixbuf_unref (pb);
504 }
505 g_slist_free (items);
506 }
509 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
510 {
511 // Check item's visibility
512 if (item->isHidden()) {
513 return;
514 }
516 SPStyle* style = SP_OBJECT_STYLE (item);
517 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
518 return sp_asbitmap_render(item, ctx);
519 }
521 if (SP_IS_ROOT(item)) {
522 TRACE(("root\n"));
523 return sp_root_render(item, ctx);
524 } else if (SP_IS_GROUP(item)) {
525 TRACE(("group\n"));
526 return sp_group_render(item, ctx);
527 } else if (SP_IS_SHAPE(item)) {
528 TRACE(("shape\n"));
529 return sp_shape_render(item, ctx);
530 } else if (SP_IS_USE(item)) {
531 TRACE(("use begin---\n"));
532 sp_use_render(item, ctx);
533 TRACE(("---use end\n"));
534 } else if (SP_IS_SYMBOL(item)) {
535 TRACE(("symbol\n"));
536 return sp_symbol_render(item, ctx);
537 } else if (SP_IS_TEXT(item)) {
538 TRACE(("text\n"));
539 return sp_text_render(item, ctx);
540 } else if (SP_IS_FLOWTEXT(item)) {
541 TRACE(("flowtext\n"));
542 return sp_flowtext_render(item, ctx);
543 } else if (SP_IS_IMAGE(item)) {
544 TRACE(("image\n"));
545 return sp_image_render(item, ctx);
546 }
547 }
549 void
550 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
551 {
552 SPStyle const *style = SP_OBJECT_STYLE(item);
553 ctx->setStateForStyle(style);
555 CairoRenderState *state = ctx->getCurrentState();
556 state->clip_path = item->clip_ref->getObject();
557 state->mask = item->mask_ref->getObject();
558 state->item_transform = Geom::Matrix (item->transform);
560 // If parent_has_userspace is true the parent state's transform
561 // has to be used for the mask's/clippath's context.
562 // This is so because we use the image's/(flow)text's transform for positioning
563 // instead of explicitly specifying it and letting the renderer do the
564 // transformation before rendering the item.
565 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
566 state->parent_has_userspace = TRUE;
567 TRACE(("setStateForItem opacity: %f\n", state->opacity));
568 }
570 void
571 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
572 {
573 if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
574 // skip text if _omitText is true
575 return;
576 }
578 ctx->pushState();
579 setStateForItem(ctx, item);
581 CairoRenderState *state = ctx->getCurrentState();
582 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
584 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
585 if (state->need_layer) {
586 state->merge_opacity = FALSE;
587 ctx->pushLayer();
588 }
589 Geom::Matrix tempmat (item->transform);
590 ctx->transform(&tempmat);
591 sp_item_invoke_render(item, ctx);
593 if (state->need_layer)
594 ctx->popLayer();
596 ctx->popState();
597 }
599 bool
600 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
601 {
602 // PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
604 g_assert( ctx != NULL );
606 if (!base)
607 base = SP_ITEM(sp_document_root(doc));
609 NRRect d;
610 if (pageBoundingBox) {
611 d.x0 = d.y0 = 0;
612 d.x1 = sp_document_width(doc);
613 d.y1 = sp_document_height(doc);
614 } else {
615 sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
616 }
618 if (ctx->_vector_based_target) {
619 // convert from px to pt
620 d.x0 *= PT_PER_PX;
621 d.x1 *= PT_PER_PX;
622 d.y0 *= PT_PER_PX;
623 d.y1 *= PT_PER_PX;
624 }
626 ctx->_width = d.x1-d.x0;
627 ctx->_height = d.y1-d.y0;
629 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
631 bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
633 if (ret && !pageBoundingBox)
634 {
635 double high = sp_document_height(doc);
636 if (ctx->_vector_based_target)
637 high *= PT_PER_PX;
639 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
640 (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
641 ctx->transform(&tp);
642 }
644 return ret;
645 }
647 #include "macros.h" // SP_PRINT_*
649 // Apply an SVG clip path
650 void
651 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
652 {
653 g_assert( ctx != NULL && ctx->_is_valid );
655 if (cp == NULL)
656 return;
658 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
659 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
661 Geom::Matrix saved_ctm;
662 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
663 //SP_PRINT_DRECT("clipd", cp->display->bbox);
664 NRRect clip_bbox(cp->display->bbox);
665 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
666 t[4] = clip_bbox.x0;
667 t[5] = clip_bbox.y0;
668 t *= ctx->getCurrentState()->transform;
669 ctx->getTransform(&saved_ctm);
670 ctx->setTransform(&t);
671 }
673 TRACE(("BEGIN clip\n"));
674 SPObject *co = SP_OBJECT(cp);
675 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
676 if (SP_IS_ITEM(child)) {
677 SPItem *item = SP_ITEM(child);
679 // combine transform of the item in clippath and the item using clippath:
680 Geom::Matrix tempmat (item->transform);
681 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
683 // render this item in clippath
684 ctx->pushState();
685 ctx->transform(&tempmat);
686 setStateForItem(ctx, item);
687 sp_item_invoke_render(item, ctx);
688 ctx->popState();
689 }
690 }
691 TRACE(("END clip\n"));
693 // do clipping only if this was the first call to applyClipPath
694 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
695 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
696 cairo_clip(ctx->_cr);
698 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
699 ctx->setTransform(&saved_ctm);
701 ctx->setRenderMode(saved_mode);
702 }
704 // Apply an SVG mask
705 void
706 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
707 {
708 g_assert( ctx != NULL && ctx->_is_valid );
710 if (mask == NULL)
711 return;
713 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
714 NRRect mask_bbox(mask->display->bbox);
715 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
716 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
717 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
718 t[4] = mask_bbox.x0;
719 t[5] = mask_bbox.y0;
720 t *= ctx->getCurrentState()->transform;
721 ctx->setTransform(&t);
722 }
724 // Clip mask contents... but...
725 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
726 // filters which extend outside the bounding box. So don't clip.
727 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
729 ctx->pushState();
731 TRACE(("BEGIN mask\n"));
732 SPObject *co = SP_OBJECT(mask);
733 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
734 if (SP_IS_ITEM(child)) {
735 SPItem *item = SP_ITEM(child);
736 renderItem(ctx, item);
737 }
738 }
739 TRACE(("END mask\n"));
741 ctx->popState();
742 }
744 void
745 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
746 double *x, double *y, double *width, double *height)
747 {
748 if (aspect_align == SP_ASPECT_NONE)
749 return;
751 double scalex, scaley, scale;
752 double new_width, new_height;
753 scalex = *width / vp_width;
754 scaley = *height / vp_height;
755 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
756 new_width = vp_width * scale;
757 new_height = vp_height * scale;
758 /* Now place viewbox to requested position */
759 switch (aspect_align) {
760 case SP_ASPECT_XMIN_YMIN:
761 break;
762 case SP_ASPECT_XMID_YMIN:
763 *x -= 0.5 * (new_width - *width);
764 break;
765 case SP_ASPECT_XMAX_YMIN:
766 *x -= 1.0 * (new_width - *width);
767 break;
768 case SP_ASPECT_XMIN_YMID:
769 *y -= 0.5 * (new_height - *height);
770 break;
771 case SP_ASPECT_XMID_YMID:
772 *x -= 0.5 * (new_width - *width);
773 *y -= 0.5 * (new_height - *height);
774 break;
775 case SP_ASPECT_XMAX_YMID:
776 *x -= 1.0 * (new_width - *width);
777 *y -= 0.5 * (new_height - *height);
778 break;
779 case SP_ASPECT_XMIN_YMAX:
780 *y -= 1.0 * (new_height - *height);
781 break;
782 case SP_ASPECT_XMID_YMAX:
783 *x -= 0.5 * (new_width - *width);
784 *y -= 1.0 * (new_height - *height);
785 break;
786 case SP_ASPECT_XMAX_YMAX:
787 *x -= 1.0 * (new_width - *width);
788 *y -= 1.0 * (new_height - *height);
789 break;
790 default:
791 break;
792 }
793 *width = new_width;
794 *height = new_height;
795 }
797 #include "clear-n_.h"
799 } /* namespace Internal */
800 } /* namespace Extension */
801 } /* namespace Inkscape */
803 #undef TRACE
805 /* End of GNU GPL code */
807 /*
808 Local Variables:
809 mode:c++
810 c-file-style:"stroustrup"
811 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
812 indent-tabs-mode:nil
813 fill-column:99
814 End:
815 */
816 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :