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 {}
108 CairoRenderer::~CairoRenderer(void)
109 {
110 /* restore default signal handling for SIGPIPE */
111 #if !defined(_WIN32) && !defined(__WIN32__)
112 (void) signal(SIGPIPE, SIG_DFL);
113 #endif
115 return;
116 }
118 CairoRenderContext*
119 CairoRenderer::createContext(void)
120 {
121 CairoRenderContext *new_context = new CairoRenderContext(this);
122 g_assert( new_context != NULL );
124 new_context->_state_stack = NULL;
125 new_context->_state = NULL;
127 // create initial render state
128 CairoRenderState *state = new_context->_createState();
129 state->transform = Geom::identity();
130 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
131 new_context->_state = state;
133 return new_context;
134 }
136 void
137 CairoRenderer::destroyContext(CairoRenderContext *ctx)
138 {
139 delete ctx;
140 }
142 /*
144 Here comes the rendering part which could be put into the 'render' methods of SPItems'
146 */
148 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
149 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
150 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
157 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
159 static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
160 {
161 bool render = true;
162 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
163 if (style->stroke_width.computed > 1e-9) {
164 tr = Geom::Scale(style->stroke_width.computed) * tr;
165 } else {
166 render = false; // stroke width zero and marker is thus scaled down to zero, skip
167 }
168 }
170 if (render) {
171 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
172 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
173 Geom::Matrix old_tr = marker_item->transform;
174 marker_item->transform = tr;
175 ctx->getRenderer()->renderItem (ctx, marker_item);
176 marker_item->transform = old_tr;
177 }
178 }
180 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
181 {
182 NRRect pbox;
184 SPShape *shape = SP_SHAPE(item);
186 if (!shape->curve) return;
188 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
190 SPStyle* style = SP_OBJECT_STYLE (item);
192 Geom::PathVector const & pathv = shape->curve->get_pathvector();
194 ctx->renderPathVector(pathv, style, &pbox);
196 // START marker
197 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
198 if ( shape->marker[i] ) {
199 SPMarker* marker = SP_MARKER (shape->marker[i]);
200 Geom::Matrix tr;
201 if (marker->orient_auto) {
202 tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
203 } else {
204 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
205 }
206 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
207 }
208 }
209 // MID marker
210 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
211 if ( !shape->marker[i] ) continue;
212 SPMarker* marker = SP_MARKER (shape->marker[i]);
213 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
214 // START position
215 if ( path_it != pathv.begin()
216 && ! ((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
217 {
218 Geom::Matrix tr;
219 if (marker->orient_auto) {
220 tr = sp_shape_marker_get_transform_at_start(path_it->front());
221 } else {
222 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
223 }
224 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
225 }
226 // MID position
227 if (path_it->size_default() > 1) {
228 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
229 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
230 while (curve_it2 != path_it->end_default())
231 {
232 /* Put marker between curve_it1 and curve_it2.
233 * Loop to end_default (so including closing segment), because when a path is closed,
234 * there should be a midpoint marker between last segment and closing straight line segment */
235 Geom::Matrix tr;
236 if (marker->orient_auto) {
237 tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
238 } else {
239 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
240 }
242 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
244 ++curve_it1;
245 ++curve_it2;
246 }
247 }
248 // END position
249 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
250 Geom::Curve const &lastcurve = path_it->back_default();
251 Geom::Matrix tr;
252 if (marker->orient_auto) {
253 tr = sp_shape_marker_get_transform_at_end(lastcurve);
254 } else {
255 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
256 }
257 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
258 }
259 }
260 }
261 // END marker
262 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
263 if ( shape->marker[i] ) {
264 SPMarker* marker = SP_MARKER (shape->marker[i]);
266 /* Get reference to last curve in the path.
267 * For moveto-only path, this returns the "closing line segment". */
268 Geom::Path const &path_last = pathv.back();
269 unsigned int index = path_last.size_default();
270 if (index > 0) {
271 index--;
272 }
273 Geom::Curve const &lastcurve = path_last[index];
275 Geom::Matrix tr;
276 if (marker->orient_auto) {
277 tr = sp_shape_marker_get_transform_at_end(lastcurve);
278 } else {
279 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
280 }
282 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
283 }
284 }
285 }
287 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
288 {
289 SPGroup *group = SP_GROUP(item);
290 CairoRenderer *renderer = ctx->getRenderer();
291 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
293 GSList *l = g_slist_reverse(group->childList(false));
294 while (l) {
295 SPObject *o = SP_OBJECT (l->data);
296 if (SP_IS_ITEM(o)) {
297 renderer->renderItem (ctx, SP_ITEM (o));
298 }
299 l = g_slist_remove (l, o);
300 }
301 }
303 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
304 {
305 bool translated = false;
306 SPUse *use = SP_USE(item);
307 CairoRenderer *renderer = ctx->getRenderer();
309 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
310 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
311 ctx->pushState();
312 ctx->transform(&tp);
313 translated = true;
314 }
316 if (use->child && SP_IS_ITEM(use->child)) {
317 renderer->renderItem(ctx, SP_ITEM(use->child));
318 }
320 if (translated) {
321 ctx->popState();
322 }
323 }
325 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
326 {
327 SPText *group = SP_TEXT (item);
328 group->layout.showGlyphs(ctx);
329 }
331 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
332 {
333 SPFlowtext *group = SP_FLOWTEXT(item);
334 group->layout.showGlyphs(ctx);
335 }
337 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
338 {
339 SPImage *image;
340 guchar *px;
341 int w, h, rs;
343 image = SP_IMAGE (item);
345 if (!image->pixbuf) return;
346 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
348 px = gdk_pixbuf_get_pixels (image->pixbuf);
349 w = gdk_pixbuf_get_width (image->pixbuf);
350 h = gdk_pixbuf_get_height (image->pixbuf);
351 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
353 double x = image->x.computed;
354 double y = image->y.computed;
355 double width = image->width.computed;
356 double height = image->height.computed;
358 if (image->aspect_align != SP_ASPECT_NONE) {
359 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
360 &x, &y, &width, &height);
361 }
363 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
364 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
365 }
367 Geom::Translate tp(x, y);
368 Geom::Scale s(width / (double)w, height / (double)h);
369 Geom::Matrix t(s * tp);
371 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
372 }
374 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
375 {
376 SPSymbol *symbol = SP_SYMBOL(item);
377 if (!SP_OBJECT_IS_CLONED (symbol))
378 return;
380 /* Cloned <symbol> is actually renderable */
381 ctx->pushState();
382 ctx->transform(&symbol->c2p);
384 // apply viewbox if set
385 if (0 /*symbol->viewBox_set*/) {
386 Geom::Matrix vb2user;
387 double x, y, width, height;
388 double view_width, view_height;
389 x = 0.0;
390 y = 0.0;
391 width = 1.0;
392 height = 1.0;
394 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
395 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
397 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
398 &x, &y,&width, &height);
400 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
401 vb2user = Geom::identity();
402 vb2user[0] = width / view_width;
403 vb2user[3] = height / view_height;
404 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
405 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
407 ctx->transform(&vb2user);
408 }
410 sp_group_render(item, ctx);
411 ctx->popState();
412 }
414 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
415 {
416 SPRoot *root = SP_ROOT(item);
417 CairoRenderer *renderer = ctx->getRenderer();
419 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
420 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
422 ctx->pushState();
423 renderer->setStateForItem(ctx, item);
424 Geom::Matrix tempmat (root->c2p);
425 ctx->transform(&tempmat);
426 sp_group_render(item, ctx);
427 ctx->popState();
428 }
430 /**
431 This function converts the item to a raster image and includes the image into the cairo renderer.
432 It is only used for filters and then only when rendering filters as bitmaps is requested.
433 */
434 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
435 {
437 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
439 // Calculate resolution
440 double res;
441 /** @TODO reimplement the resolution stuff (WHY?)
442 */
443 res = ctx->getBitmapResolution();
444 if(res == 0) {
445 res = PX_PER_IN;
446 }
447 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
449 // Get the bounding box of the selection in document coordinates.
450 Geom::OptRect bbox =
451 item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
453 if (!bbox) // no bbox, e.g. empty group
454 return;
456 // The width and height of the bitmap in pixels
457 unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
458 unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
460 // Scale to exactly fit integer bitmap inside bounding box
461 double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
462 double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
464 // Location of bounding box in document coordinates.
465 double shift_x = bbox->min()[Geom::X];
466 double shift_y = bbox->max()[Geom::Y];
468 // For default 90 dpi, snap bitmap to pixel grid
469 if (res == PX_PER_IN) {
470 shift_x = round (shift_x);
471 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
472 // Remove the negations when the inversion is gone.
473 }
475 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
477 // Matix to put bitmap in correct place on document
478 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
479 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
481 // ctx matrix already includes item transformation. We must substract.
482 Geom::Matrix t_item = sp_item_i2d_affine (item);
483 Geom::Matrix t = t_on_document * t_item.inverse();
485 // Do the export
486 SPDocument *document = SP_OBJECT(item)->document;
487 GSList *items = NULL;
488 items = g_slist_append(items, item);
490 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
491 bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
492 width, height, res, res, (guint32) 0xffffff00, items );
494 if (pb) {
495 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
496 unsigned char *px = gdk_pixbuf_get_pixels (pb);
497 unsigned int w = gdk_pixbuf_get_width(pb);
498 unsigned int h = gdk_pixbuf_get_height(pb);
499 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
500 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
501 gdk_pixbuf_unref (pb);
502 }
503 g_slist_free (items);
504 }
507 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
508 {
509 // Check item's visibility
510 if (item->isHidden()) {
511 return;
512 }
514 SPStyle* style = SP_OBJECT_STYLE (item);
515 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
516 return sp_asbitmap_render(item, ctx);
517 }
519 if (SP_IS_ROOT(item)) {
520 TRACE(("root\n"));
521 return sp_root_render(item, ctx);
522 } else if (SP_IS_GROUP(item)) {
523 TRACE(("group\n"));
524 return sp_group_render(item, ctx);
525 } else if (SP_IS_SHAPE(item)) {
526 TRACE(("shape\n"));
527 return sp_shape_render(item, ctx);
528 } else if (SP_IS_USE(item)) {
529 TRACE(("use begin---\n"));
530 sp_use_render(item, ctx);
531 TRACE(("---use end\n"));
532 } else if (SP_IS_SYMBOL(item)) {
533 TRACE(("symbol\n"));
534 return sp_symbol_render(item, ctx);
535 } else if (SP_IS_TEXT(item)) {
536 TRACE(("text\n"));
537 return sp_text_render(item, ctx);
538 } else if (SP_IS_FLOWTEXT(item)) {
539 TRACE(("flowtext\n"));
540 return sp_flowtext_render(item, ctx);
541 } else if (SP_IS_IMAGE(item)) {
542 TRACE(("image\n"));
543 return sp_image_render(item, ctx);
544 }
545 }
547 void
548 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
549 {
550 SPStyle const *style = SP_OBJECT_STYLE(item);
551 ctx->setStateForStyle(style);
553 CairoRenderState *state = ctx->getCurrentState();
554 state->clip_path = item->clip_ref->getObject();
555 state->mask = item->mask_ref->getObject();
556 state->item_transform = Geom::Matrix (item->transform);
558 // If parent_has_userspace is true the parent state's transform
559 // has to be used for the mask's/clippath's context.
560 // This is so because we use the image's/(flow)text's transform for positioning
561 // instead of explicitly specifying it and letting the renderer do the
562 // transformation before rendering the item.
563 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
564 state->parent_has_userspace = TRUE;
565 TRACE(("setStateForItem opacity: %f\n", state->opacity));
566 }
568 void
569 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
570 {
571 ctx->pushState();
572 setStateForItem(ctx, item);
574 CairoRenderState *state = ctx->getCurrentState();
575 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
577 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
578 if (state->need_layer) {
579 state->merge_opacity = FALSE;
580 ctx->pushLayer();
581 }
582 Geom::Matrix tempmat (item->transform);
583 ctx->transform(&tempmat);
584 sp_item_invoke_render(item, ctx);
586 if (state->need_layer)
587 ctx->popLayer();
589 ctx->popState();
590 }
592 bool
593 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
594 {
595 g_assert( ctx != NULL );
597 if (!base)
598 base = SP_ITEM(sp_document_root(doc));
600 NRRect d;
601 if (pageBoundingBox) {
602 d.x0 = d.y0 = 0;
603 d.x1 = ceil(sp_document_width(doc));
604 d.y1 = ceil(sp_document_height(doc));
605 } else {
606 sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
607 }
609 if (ctx->_vector_based_target) {
610 // convert from px to pt
611 d.x0 *= PT_PER_PX;
612 d.x1 *= PT_PER_PX;
613 d.y0 *= PT_PER_PX;
614 d.y1 *= PT_PER_PX;
615 }
617 ctx->_width = d.x1-d.x0;
618 ctx->_height = d.y1-d.y0;
620 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
622 bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
624 if (ret && !pageBoundingBox)
625 {
626 double high = sp_document_height(doc);
627 if (ctx->_vector_based_target)
628 high *= PT_PER_PX;
630 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
631 (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
632 ctx->transform(&tp);
633 }
635 return ret;
636 }
638 #include "macros.h" // SP_PRINT_*
640 // Apply an SVG clip path
641 void
642 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
643 {
644 g_assert( ctx != NULL && ctx->_is_valid );
646 if (cp == NULL)
647 return;
649 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
650 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
652 Geom::Matrix saved_ctm;
653 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
654 //SP_PRINT_DRECT("clipd", cp->display->bbox);
655 NRRect clip_bbox(cp->display->bbox);
656 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
657 t[4] = clip_bbox.x0;
658 t[5] = clip_bbox.y0;
659 t *= ctx->getCurrentState()->transform;
660 ctx->getTransform(&saved_ctm);
661 ctx->setTransform(&t);
662 }
664 TRACE(("BEGIN clip\n"));
665 SPObject *co = SP_OBJECT(cp);
666 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
667 if (SP_IS_ITEM(child)) {
668 SPItem *item = SP_ITEM(child);
670 // combine transform of the item in clippath and the item using clippath:
671 Geom::Matrix tempmat (item->transform);
672 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
674 // render this item in clippath
675 ctx->pushState();
676 ctx->transform(&tempmat);
677 setStateForItem(ctx, item);
678 sp_item_invoke_render(item, ctx);
679 ctx->popState();
680 }
681 }
682 TRACE(("END clip\n"));
684 // do clipping only if this was the first call to applyClipPath
685 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
686 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
687 cairo_clip(ctx->_cr);
689 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
690 ctx->setTransform(&saved_ctm);
692 ctx->setRenderMode(saved_mode);
693 }
695 // Apply an SVG mask
696 void
697 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
698 {
699 g_assert( ctx != NULL && ctx->_is_valid );
701 if (mask == NULL)
702 return;
704 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
705 NRRect mask_bbox(mask->display->bbox);
706 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
707 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
708 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
709 t[4] = mask_bbox.x0;
710 t[5] = mask_bbox.y0;
711 t *= ctx->getCurrentState()->transform;
712 ctx->setTransform(&t);
713 }
715 // Clip mask contents... but...
716 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
717 // filters which extend outside the bounding box. So don't clip.
718 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
720 ctx->pushState();
722 TRACE(("BEGIN mask\n"));
723 SPObject *co = SP_OBJECT(mask);
724 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
725 if (SP_IS_ITEM(child)) {
726 SPItem *item = SP_ITEM(child);
727 renderItem(ctx, item);
728 }
729 }
730 TRACE(("END mask\n"));
732 ctx->popState();
733 }
735 void
736 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
737 double *x, double *y, double *width, double *height)
738 {
739 if (aspect_align == SP_ASPECT_NONE)
740 return;
742 double scalex, scaley, scale;
743 double new_width, new_height;
744 scalex = *width / vp_width;
745 scaley = *height / vp_height;
746 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
747 new_width = vp_width * scale;
748 new_height = vp_height * scale;
749 /* Now place viewbox to requested position */
750 switch (aspect_align) {
751 case SP_ASPECT_XMIN_YMIN:
752 break;
753 case SP_ASPECT_XMID_YMIN:
754 *x -= 0.5 * (new_width - *width);
755 break;
756 case SP_ASPECT_XMAX_YMIN:
757 *x -= 1.0 * (new_width - *width);
758 break;
759 case SP_ASPECT_XMIN_YMID:
760 *y -= 0.5 * (new_height - *height);
761 break;
762 case SP_ASPECT_XMID_YMID:
763 *x -= 0.5 * (new_width - *width);
764 *y -= 0.5 * (new_height - *height);
765 break;
766 case SP_ASPECT_XMAX_YMID:
767 *x -= 1.0 * (new_width - *width);
768 *y -= 0.5 * (new_height - *height);
769 break;
770 case SP_ASPECT_XMIN_YMAX:
771 *y -= 1.0 * (new_height - *height);
772 break;
773 case SP_ASPECT_XMID_YMAX:
774 *x -= 0.5 * (new_width - *width);
775 *y -= 1.0 * (new_height - *height);
776 break;
777 case SP_ASPECT_XMAX_YMAX:
778 *x -= 1.0 * (new_width - *width);
779 *y -= 1.0 * (new_height - *height);
780 break;
781 default:
782 break;
783 }
784 *width = new_width;
785 *height = new_height;
786 }
788 #include "clear-n_.h"
790 } /* namespace Internal */
791 } /* namespace Extension */
792 } /* namespace Inkscape */
794 #undef TRACE
796 /* End of GNU GPL code */
798 /*
799 Local Variables:
800 mode:c++
801 c-file-style:"stroustrup"
802 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
803 indent-tabs-mode:nil
804 fill-column:99
805 End:
806 */
807 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :