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();
195 ctx->renderPathVector(pathv, style, &pbox);
197 // START marker
198 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
199 if ( shape->marker[i] ) {
200 SPMarker* marker = SP_MARKER (shape->marker[i]);
201 Geom::Matrix tr;
202 if (marker->orient_auto) {
203 tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
204 } else {
205 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
206 }
207 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
208 }
209 }
210 // MID marker
211 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
212 if ( !shape->marker[i] ) continue;
213 SPMarker* marker = SP_MARKER (shape->marker[i]);
214 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
215 // START position
216 if ( path_it != pathv.begin()
217 && ! ((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
218 {
219 Geom::Matrix tr;
220 if (marker->orient_auto) {
221 tr = sp_shape_marker_get_transform_at_start(path_it->front());
222 } else {
223 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
224 }
225 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
226 }
227 // MID position
228 if (path_it->size_default() > 1) {
229 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
230 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
231 while (curve_it2 != path_it->end_default())
232 {
233 /* Put marker between curve_it1 and curve_it2.
234 * Loop to end_default (so including closing segment), because when a path is closed,
235 * there should be a midpoint marker between last segment and closing straight line segment */
236 Geom::Matrix tr;
237 if (marker->orient_auto) {
238 tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
239 } else {
240 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
241 }
243 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
245 ++curve_it1;
246 ++curve_it2;
247 }
248 }
249 // END position
250 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
251 Geom::Curve const &lastcurve = path_it->back_default();
252 Geom::Matrix tr;
253 if (marker->orient_auto) {
254 tr = sp_shape_marker_get_transform_at_end(lastcurve);
255 } else {
256 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
257 }
258 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
259 }
260 }
261 }
262 // END marker
263 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
264 if ( shape->marker[i] ) {
265 SPMarker* marker = SP_MARKER (shape->marker[i]);
267 /* Get reference to last curve in the path.
268 * For moveto-only path, this returns the "closing line segment". */
269 Geom::Path const &path_last = pathv.back();
270 unsigned int index = path_last.size_default();
271 if (index > 0) {
272 index--;
273 }
274 Geom::Curve const &lastcurve = path_last[index];
276 Geom::Matrix tr;
277 if (marker->orient_auto) {
278 tr = sp_shape_marker_get_transform_at_end(lastcurve);
279 } else {
280 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
281 }
283 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
284 }
285 }
286 }
288 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
289 {
290 SPGroup *group = SP_GROUP(item);
291 CairoRenderer *renderer = ctx->getRenderer();
292 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
294 GSList *l = g_slist_reverse(group->childList(false));
295 while (l) {
296 SPObject *o = SP_OBJECT (l->data);
297 if (SP_IS_ITEM(o)) {
298 renderer->renderItem (ctx, SP_ITEM (o));
299 }
300 l = g_slist_remove (l, o);
301 }
302 }
304 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
305 {
306 bool translated = false;
307 SPUse *use = SP_USE(item);
308 CairoRenderer *renderer = ctx->getRenderer();
310 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
311 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
312 ctx->pushState();
313 ctx->transform(&tp);
314 translated = true;
315 }
317 if (use->child && SP_IS_ITEM(use->child)) {
318 renderer->renderItem(ctx, SP_ITEM(use->child));
319 }
321 if (translated) {
322 ctx->popState();
323 }
324 }
326 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
327 {
328 SPText *group = SP_TEXT (item);
329 group->layout.showGlyphs(ctx);
330 }
332 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
333 {
334 SPFlowtext *group = SP_FLOWTEXT(item);
335 group->layout.showGlyphs(ctx);
336 }
338 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
339 {
340 SPImage *image;
341 guchar *px;
342 int w, h, rs;
344 image = SP_IMAGE (item);
346 if (!image->pixbuf) return;
347 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
349 px = gdk_pixbuf_get_pixels (image->pixbuf);
350 w = gdk_pixbuf_get_width (image->pixbuf);
351 h = gdk_pixbuf_get_height (image->pixbuf);
352 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
354 double x = image->x.computed;
355 double y = image->y.computed;
356 double width = image->width.computed;
357 double height = image->height.computed;
359 if (image->aspect_align != SP_ASPECT_NONE) {
360 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
361 &x, &y, &width, &height);
362 }
364 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
365 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
366 }
368 Geom::Translate tp(x, y);
369 Geom::Scale s(width / (double)w, height / (double)h);
370 Geom::Matrix t(s * tp);
372 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
373 }
375 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
376 {
377 SPSymbol *symbol = SP_SYMBOL(item);
378 if (!SP_OBJECT_IS_CLONED (symbol))
379 return;
381 /* Cloned <symbol> is actually renderable */
382 ctx->pushState();
383 ctx->transform(&symbol->c2p);
385 // apply viewbox if set
386 if (0 /*symbol->viewBox_set*/) {
387 Geom::Matrix vb2user;
388 double x, y, width, height;
389 double view_width, view_height;
390 x = 0.0;
391 y = 0.0;
392 width = 1.0;
393 height = 1.0;
395 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
396 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
398 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
399 &x, &y,&width, &height);
401 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
402 vb2user = Geom::identity();
403 vb2user[0] = width / view_width;
404 vb2user[3] = height / view_height;
405 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
406 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
408 ctx->transform(&vb2user);
409 }
411 sp_group_render(item, ctx);
412 ctx->popState();
413 }
415 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
416 {
417 SPRoot *root = SP_ROOT(item);
418 CairoRenderer *renderer = ctx->getRenderer();
420 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
421 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
423 ctx->pushState();
424 renderer->setStateForItem(ctx, item);
425 Geom::Matrix tempmat (root->c2p);
426 ctx->transform(&tempmat);
427 sp_group_render(item, ctx);
428 ctx->popState();
429 }
431 /**
432 This function converts the item to a raster image and includes the image into the cairo renderer.
433 It is only used for filters and then only when rendering filters as bitmaps is requested.
434 */
435 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
436 {
438 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
440 // Calculate resolution
441 double res;
442 /** @TODO reimplement the resolution stuff (WHY?)
443 */
444 res = ctx->getBitmapResolution();
445 if(res == 0) {
446 res = PX_PER_IN;
447 }
448 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
450 // Get the bounding box of the selection in document coordinates.
451 Geom::OptRect bbox =
452 item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
454 if (!bbox) // no bbox, e.g. empty group
455 return;
457 // The width and height of the bitmap in pixels
458 unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
459 unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
461 // Scale to exactly fit integer bitmap inside bounding box
462 double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
463 double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
465 // Location of bounding box in document coordinates.
466 double shift_x = bbox->min()[Geom::X];
467 double shift_y = bbox->max()[Geom::Y];
469 // For default 90 dpi, snap bitmap to pixel grid
470 if (res == PX_PER_IN) {
471 shift_x = round (shift_x);
472 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
473 // Remove the negations when the inversion is gone.
474 }
476 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
478 // Matix to put bitmap in correct place on document
479 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
480 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
482 // ctx matrix already includes item transformation. We must substract.
483 Geom::Matrix t_item = sp_item_i2d_affine (item);
484 Geom::Matrix t = t_on_document * t_item.inverse();
486 // Do the export
487 SPDocument *document = SP_OBJECT(item)->document;
488 GSList *items = NULL;
489 items = g_slist_append(items, item);
491 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
492 bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
493 width, height, res, res, (guint32) 0xffffff00, items );
495 if (pb) {
496 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
497 unsigned char *px = gdk_pixbuf_get_pixels (pb);
498 unsigned int w = gdk_pixbuf_get_width(pb);
499 unsigned int h = gdk_pixbuf_get_height(pb);
500 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
501 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
502 gdk_pixbuf_unref (pb);
503 }
504 g_slist_free (items);
505 }
508 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
509 {
510 // Check item's visibility
511 if (item->isHidden()) {
512 return;
513 }
515 SPStyle* style = SP_OBJECT_STYLE (item);
516 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
517 return sp_asbitmap_render(item, ctx);
518 }
520 if (SP_IS_ROOT(item)) {
521 TRACE(("root\n"));
522 return sp_root_render(item, ctx);
523 } else if (SP_IS_GROUP(item)) {
524 TRACE(("group\n"));
525 return sp_group_render(item, ctx);
526 } else if (SP_IS_SHAPE(item)) {
527 TRACE(("shape\n"));
528 return sp_shape_render(item, ctx);
529 } else if (SP_IS_USE(item)) {
530 TRACE(("use begin---\n"));
531 sp_use_render(item, ctx);
532 TRACE(("---use end\n"));
533 } else if (SP_IS_SYMBOL(item)) {
534 TRACE(("symbol\n"));
535 return sp_symbol_render(item, ctx);
536 } else if (SP_IS_TEXT(item)) {
537 TRACE(("text\n"));
538 return sp_text_render(item, ctx);
539 } else if (SP_IS_FLOWTEXT(item)) {
540 TRACE(("flowtext\n"));
541 return sp_flowtext_render(item, ctx);
542 } else if (SP_IS_IMAGE(item)) {
543 TRACE(("image\n"));
544 return sp_image_render(item, ctx);
545 }
546 }
548 void
549 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
550 {
551 SPStyle const *style = SP_OBJECT_STYLE(item);
552 ctx->setStateForStyle(style);
554 CairoRenderState *state = ctx->getCurrentState();
555 state->clip_path = item->clip_ref->getObject();
556 state->mask = item->mask_ref->getObject();
557 state->item_transform = Geom::Matrix (item->transform);
559 // If parent_has_userspace is true the parent state's transform
560 // has to be used for the mask's/clippath's context.
561 // This is so because we use the image's/(flow)text's transform for positioning
562 // instead of explicitly specifying it and letting the renderer do the
563 // transformation before rendering the item.
564 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
565 state->parent_has_userspace = TRUE;
566 TRACE(("setStateForItem opacity: %f\n", state->opacity));
567 }
569 void
570 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
571 {
572 if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
573 // skip text if _omitText is true
574 return;
575 }
577 ctx->pushState();
578 setStateForItem(ctx, item);
580 CairoRenderState *state = ctx->getCurrentState();
581 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
583 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
584 if (state->need_layer) {
585 state->merge_opacity = FALSE;
586 ctx->pushLayer();
587 }
588 Geom::Matrix tempmat (item->transform);
589 ctx->transform(&tempmat);
590 sp_item_invoke_render(item, ctx);
592 if (state->need_layer)
593 ctx->popLayer();
595 ctx->popState();
596 }
598 bool
599 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
600 {
601 // PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
603 g_assert( ctx != NULL );
605 if (!base)
606 base = SP_ITEM(sp_document_root(doc));
608 NRRect d;
609 if (pageBoundingBox) {
610 d.x0 = d.y0 = 0;
611 d.x1 = ceil(sp_document_width(doc));
612 d.y1 = ceil(sp_document_height(doc));
613 } else {
614 sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
615 }
617 if (ctx->_vector_based_target) {
618 // convert from px to pt
619 d.x0 *= PT_PER_PX;
620 d.x1 *= PT_PER_PX;
621 d.y0 *= PT_PER_PX;
622 d.y1 *= PT_PER_PX;
623 }
625 ctx->_width = d.x1-d.x0;
626 ctx->_height = d.y1-d.y0;
628 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
630 bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
632 if (ret && !pageBoundingBox)
633 {
634 double high = sp_document_height(doc);
635 if (ctx->_vector_based_target)
636 high *= PT_PER_PX;
638 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
639 (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
640 ctx->transform(&tp);
641 }
643 return ret;
644 }
646 #include "macros.h" // SP_PRINT_*
648 // Apply an SVG clip path
649 void
650 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
651 {
652 g_assert( ctx != NULL && ctx->_is_valid );
654 if (cp == NULL)
655 return;
657 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
658 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
660 Geom::Matrix saved_ctm;
661 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
662 //SP_PRINT_DRECT("clipd", cp->display->bbox);
663 NRRect clip_bbox(cp->display->bbox);
664 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
665 t[4] = clip_bbox.x0;
666 t[5] = clip_bbox.y0;
667 t *= ctx->getCurrentState()->transform;
668 ctx->getTransform(&saved_ctm);
669 ctx->setTransform(&t);
670 }
672 TRACE(("BEGIN clip\n"));
673 SPObject *co = SP_OBJECT(cp);
674 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
675 if (SP_IS_ITEM(child)) {
676 SPItem *item = SP_ITEM(child);
678 // combine transform of the item in clippath and the item using clippath:
679 Geom::Matrix tempmat (item->transform);
680 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
682 // render this item in clippath
683 ctx->pushState();
684 ctx->transform(&tempmat);
685 setStateForItem(ctx, item);
686 sp_item_invoke_render(item, ctx);
687 ctx->popState();
688 }
689 }
690 TRACE(("END clip\n"));
692 // do clipping only if this was the first call to applyClipPath
693 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
694 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
695 cairo_clip(ctx->_cr);
697 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
698 ctx->setTransform(&saved_ctm);
700 ctx->setRenderMode(saved_mode);
701 }
703 // Apply an SVG mask
704 void
705 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
706 {
707 g_assert( ctx != NULL && ctx->_is_valid );
709 if (mask == NULL)
710 return;
712 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
713 NRRect mask_bbox(mask->display->bbox);
714 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
715 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
716 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
717 t[4] = mask_bbox.x0;
718 t[5] = mask_bbox.y0;
719 t *= ctx->getCurrentState()->transform;
720 ctx->setTransform(&t);
721 }
723 // Clip mask contents... but...
724 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
725 // filters which extend outside the bounding box. So don't clip.
726 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
728 ctx->pushState();
730 TRACE(("BEGIN mask\n"));
731 SPObject *co = SP_OBJECT(mask);
732 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
733 if (SP_IS_ITEM(child)) {
734 SPItem *item = SP_ITEM(child);
735 renderItem(ctx, item);
736 }
737 }
738 TRACE(("END mask\n"));
740 ctx->popState();
741 }
743 void
744 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
745 double *x, double *y, double *width, double *height)
746 {
747 if (aspect_align == SP_ASPECT_NONE)
748 return;
750 double scalex, scaley, scale;
751 double new_width, new_height;
752 scalex = *width / vp_width;
753 scaley = *height / vp_height;
754 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
755 new_width = vp_width * scale;
756 new_height = vp_height * scale;
757 /* Now place viewbox to requested position */
758 switch (aspect_align) {
759 case SP_ASPECT_XMIN_YMIN:
760 break;
761 case SP_ASPECT_XMID_YMIN:
762 *x -= 0.5 * (new_width - *width);
763 break;
764 case SP_ASPECT_XMAX_YMIN:
765 *x -= 1.0 * (new_width - *width);
766 break;
767 case SP_ASPECT_XMIN_YMID:
768 *y -= 0.5 * (new_height - *height);
769 break;
770 case SP_ASPECT_XMID_YMID:
771 *x -= 0.5 * (new_width - *width);
772 *y -= 0.5 * (new_height - *height);
773 break;
774 case SP_ASPECT_XMAX_YMID:
775 *x -= 1.0 * (new_width - *width);
776 *y -= 0.5 * (new_height - *height);
777 break;
778 case SP_ASPECT_XMIN_YMAX:
779 *y -= 1.0 * (new_height - *height);
780 break;
781 case SP_ASPECT_XMID_YMAX:
782 *x -= 0.5 * (new_width - *width);
783 *y -= 1.0 * (new_height - *height);
784 break;
785 case SP_ASPECT_XMAX_YMAX:
786 *x -= 1.0 * (new_width - *width);
787 *y -= 1.0 * (new_height - *height);
788 break;
789 default:
790 break;
791 }
792 *width = new_width;
793 *height = new_height;
794 }
796 #include "clear-n_.h"
798 } /* namespace Internal */
799 } /* namespace Extension */
800 } /* namespace Inkscape */
802 #undef TRACE
804 /* End of GNU GPL code */
806 /*
807 Local Variables:
808 mode:c++
809 c-file-style:"stroustrup"
810 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
811 indent-tabs-mode:nil
812 fill-column:99
813 End:
814 */
815 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :