e7620f3195c17bd1fc6a1dfef61c23c9a07df2cc
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)
95 // FIXME: expose these from sp-clippath/mask.cpp
96 struct SPClipPathView {
97 SPClipPathView *next;
98 unsigned int key;
99 NRArenaItem *arenaitem;
100 NRRect bbox;
101 };
103 struct SPMaskView {
104 SPMaskView *next;
105 unsigned int key;
106 NRArenaItem *arenaitem;
107 NRRect bbox;
108 };
110 namespace Inkscape {
111 namespace Extension {
112 namespace Internal {
114 CairoRenderer::CairoRenderer(void)
115 {}
117 CairoRenderer::~CairoRenderer(void)
118 {
119 /* restore default signal handling for SIGPIPE */
120 #if !defined(_WIN32) && !defined(__WIN32__)
121 (void) signal(SIGPIPE, SIG_DFL);
122 #endif
124 return;
125 }
127 CairoRenderContext*
128 CairoRenderer::createContext(void)
129 {
130 CairoRenderContext *new_context = new CairoRenderContext(this);
131 g_assert( new_context != NULL );
133 new_context->_state_stack = NULL;
134 new_context->_state = NULL;
136 // create initial render state
137 CairoRenderState *state = new_context->_createState();
138 state->transform.set_identity();
139 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
140 new_context->_state = state;
142 return new_context;
143 }
145 void
146 CairoRenderer::destroyContext(CairoRenderContext *ctx)
147 {
148 delete ctx;
149 }
151 /*
153 Here comes the rendering part which could be put into the 'render' methods of SPItems'
155 */
157 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
158 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
159 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
168 /* 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 ) */
169 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
170 {
171 NRRect pbox;
173 SPShape *shape = SP_SHAPE(item);
175 if (!shape->curve) return;
177 /* fixme: Think (Lauris) */
178 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
180 SPStyle* style = SP_OBJECT_STYLE (item);
181 CairoRenderer *renderer = ctx->getRenderer();
183 Geom::PathVector const & pathv = shape->curve->get_pathvector();
185 ctx->renderPathVector(pathv, style, &pbox);
187 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
188 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
189 if ( shape->marker[SP_MARKER_LOC_START] ) {
190 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
191 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
193 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_start(path_it->front())));
195 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
196 tr = NR::scale(style->stroke_width.computed) * tr;
197 }
199 tr = marker_item->transform * marker->c2p * tr;
201 NR::Matrix old_tr = marker_item->transform;
202 marker_item->transform = tr;
203 renderer->renderItem (ctx, marker_item);
204 marker_item->transform = old_tr;
205 }
207 if ( shape->marker[SP_MARKER_LOC_MID] ) {
208 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
209 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
210 while (curve_it2 != path_it->end_default())
211 {
212 /* Put marker between curve_it1 and curve_it2.
213 * Loop to end_default (so including closing segment), because when a path is closed,
214 * there should be a midpoint marker between last segment and closing straight line segment */
216 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
217 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
219 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(*curve_it1, *curve_it2)));
221 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
222 tr = NR::scale(style->stroke_width.computed) * tr;
223 }
225 tr = marker_item->transform * marker->c2p * tr;
227 NR::Matrix old_tr = marker_item->transform;
228 marker_item->transform = tr;
229 renderer->renderItem (ctx, marker_item);
230 marker_item->transform = old_tr;
232 ++curve_it1;
233 ++curve_it2;
234 }
235 }
237 if ( shape->marker[SP_MARKER_LOC_END] ) {
238 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
239 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
241 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_end(path_it->back_default())));
243 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
244 tr = NR::scale(style->stroke_width.computed) * tr;
245 }
247 tr = marker_item->transform * marker->c2p * tr;
249 NR::Matrix old_tr = marker_item->transform;
250 marker_item->transform = tr;
251 renderer->renderItem (ctx, marker_item);
252 marker_item->transform = old_tr;
253 }
254 }
255 }
257 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
258 {
259 SPGroup *group = SP_GROUP(item);
260 CairoRenderer *renderer = ctx->getRenderer();
261 TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
263 GSList *l = g_slist_reverse(group->childList(false));
264 while (l) {
265 SPObject *o = SP_OBJECT (l->data);
266 if (SP_IS_ITEM(o)) {
267 renderer->renderItem (ctx, SP_ITEM (o));
268 }
269 l = g_slist_remove (l, o);
270 }
271 }
273 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
274 {
275 bool translated = false;
276 SPUse *use = SP_USE(item);
277 CairoRenderer *renderer = ctx->getRenderer();
279 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
280 NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
281 ctx->pushState();
282 ctx->transform(&tp);
283 translated = true;
284 }
286 if (use->child && SP_IS_ITEM(use->child)) {
287 renderer->renderItem(ctx, SP_ITEM(use->child));
288 }
290 if (translated) {
291 ctx->popState();
292 }
293 }
295 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
296 {
297 SPText *group = SP_TEXT (item);
298 group->layout.showGlyphs(ctx);
299 }
301 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
302 {
303 SPFlowtext *group = SP_FLOWTEXT(item);
304 group->layout.showGlyphs(ctx);
305 }
307 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
308 {
309 SPImage *image;
310 guchar *px;
311 int w, h, rs;
313 image = SP_IMAGE (item);
315 if (!image->pixbuf) return;
316 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
318 px = gdk_pixbuf_get_pixels (image->pixbuf);
319 w = gdk_pixbuf_get_width (image->pixbuf);
320 h = gdk_pixbuf_get_height (image->pixbuf);
321 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
323 double x = image->x.computed;
324 double y = image->y.computed;
325 double width = image->width.computed;
326 double height = image->height.computed;
328 if (image->aspect_align != SP_ASPECT_NONE) {
329 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
330 &x, &y, &width, &height);
331 }
333 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
334 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
335 }
337 NR::translate tp(x, y);
338 NR::scale s(width / (double)w, height / (double)h);
339 NR::Matrix t(s * tp);
341 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
342 }
344 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
345 {
346 SPSymbol *symbol = SP_SYMBOL(item);
347 if (!SP_OBJECT_IS_CLONED (symbol))
348 return;
350 /* Cloned <symbol> is actually renderable */
351 ctx->pushState();
352 ctx->transform(&symbol->c2p);
354 // apply viewbox if set
355 if (0 /*symbol->viewBox_set*/) {
356 NR::Matrix vb2user;
357 double x, y, width, height;
358 double view_width, view_height;
359 x = 0.0;
360 y = 0.0;
361 width = 1.0;
362 height = 1.0;
364 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
365 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
367 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
368 &x, &y,&width, &height);
370 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
371 vb2user.set_identity();
372 vb2user[0] = width / view_width;
373 vb2user[3] = height / view_height;
374 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
375 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
377 ctx->transform(&vb2user);
378 }
380 sp_group_render(item, ctx);
381 ctx->popState();
382 }
384 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
385 {
386 SPRoot *root = SP_ROOT(item);
387 CairoRenderer *renderer = ctx->getRenderer();
389 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
390 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
392 ctx->pushState();
393 renderer->setStateForItem(ctx, item);
394 ctx->transform(&root->c2p);
395 sp_group_render(item, ctx);
396 ctx->popState();
397 }
399 /**
400 this function convert the item to a raster image and include the raster into the cairo renderer
401 */
402 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
403 {
404 g_warning("render as bitmap");
406 //the code now was copied from sp_selection_create_bitmap_copy
408 SPDocument *document = SP_OBJECT(item)->document;
409 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
411 // Get the bounding box of the selection
412 //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
413 // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
414 NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
417 // List of the items to show; all others will be hidden
418 GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
419 items = g_slist_append(items, item);
421 // Remember parent and z-order of the topmost one
422 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
423 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
424 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
426 // Calculate resolution
427 double res;
428 /** @TODO reimplement the resolution stuff
429 */
430 res = ctx->getBitmapResolution();
431 if(res == 0) {
432 res = PX_PER_IN;
433 }
436 // The width and height of the bitmap in pixels
437 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
438 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
440 // Find out if we have to run a filter
441 gchar const *run = NULL;
442 gchar const *filter = NULL;
443 /** @TODO reimplement the filter stuff
444 //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
445 if (filter) {
446 // filter command is given;
447 // see if we have a parameter to pass to it
448 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
449 if (param1) {
450 if (param1[strlen(param1) - 1] == '%') {
451 // if the param string ends with %, interpret it as a percentage of the image's max dimension
452 gchar p1[256];
453 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
454 // the first param is always the image filename, the second is param1
455 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
456 } else {
457 // otherwise pass the param1 unchanged
458 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
459 }
460 } else {
461 // run without extra parameter
462 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
463 }
464 }
465 */
466 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
467 NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
468 NR::Matrix t;
470 double shift_x = bbox.x0;
471 double shift_y = bbox.y1;
472 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
473 shift_x = round (shift_x);
474 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
475 }
476 t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
478 //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
480 // Do the export
481 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
482 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
484 // Run filter, if any
485 /*
486 if (run) {
487 g_print ("Running external filter: %s\n", run);
488 system (run);
489 }
490 */
491 if (pb) {
492 unsigned char *px = gdk_pixbuf_get_pixels (pb);
493 unsigned int w = gdk_pixbuf_get_width(pb);
494 unsigned int h = gdk_pixbuf_get_height(pb);
495 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
496 NR::Matrix matrix;
497 matrix = t;
498 //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
499 //matrix.set_identity();
501 ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
502 /*
503 // Create the repr for the image
504 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
505 repr->setAttribute("xlink:href", filename);
506 repr->setAttribute("sodipodi:absref", filepath);
507 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
508 sp_repr_set_svg_double(repr, "width", width);
509 sp_repr_set_svg_double(repr, "height", height);
510 } else {
511 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
512 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
513 }
515 // Write transform
516 gchar *c=sp_svg_transform_write(t);
517 repr->setAttribute("transform", c);
518 g_free(c);
520 // add the new repr to the parent
521 parent->appendChild(repr);
523 // move to the saved position
524 repr->setPosition(pos > 0 ? pos + 1 : 1);
525 */
526 gdk_pixbuf_unref (pb);
527 }
528 g_slist_free (items);
529 }
531 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
532 {
533 // Check item's visibility
534 if (item->isHidden()) {
535 return;
536 }
538 SPStyle* style = SP_OBJECT_STYLE (item);
539 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
540 return sp_asbitmap_render(item, ctx);
541 }
543 if (SP_IS_ROOT(item)) {
544 TRACE(("root\n"));
545 return sp_root_render(item, ctx);
546 } else if (SP_IS_GROUP(item)) {
547 TRACE(("group\n"));
548 return sp_group_render(item, ctx);
549 } else if (SP_IS_SHAPE(item)) {
550 TRACE(("shape\n"));
551 return sp_shape_render(item, ctx);
552 } else if (SP_IS_USE(item)) {
553 TRACE(("use begin---\n"));
554 sp_use_render(item, ctx);
555 TRACE(("---use end\n"));
556 } else if (SP_IS_SYMBOL(item)) {
557 TRACE(("symbol\n"));
558 return sp_symbol_render(item, ctx);
559 } else if (SP_IS_TEXT(item)) {
560 TRACE(("text\n"));
561 return sp_text_render(item, ctx);
562 } else if (SP_IS_FLOWTEXT(item)) {
563 TRACE(("flowtext\n"));
564 return sp_flowtext_render(item, ctx);
565 } else if (SP_IS_IMAGE(item)) {
566 TRACE(("image\n"));
567 return sp_image_render(item, ctx);
568 }
569 }
571 void
572 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
573 {
574 SPStyle const *style = SP_OBJECT_STYLE(item);
575 ctx->setStateForStyle(style);
577 CairoRenderState *state = ctx->getCurrentState();
578 state->clip_path = item->clip_ref->getObject();
579 state->mask = item->mask_ref->getObject();
581 // If parent_has_userspace is true the parent state's transform
582 // has to be used for the mask's/clippath's context.
583 // This is so because we use the image's/(flow)text's transform for positioning
584 // instead of explicitly specifying it and letting the renderer do the
585 // transformation before rendering the item.
586 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
587 state->parent_has_userspace = TRUE;
588 TRACE(("set op: %f\n", state->opacity));
589 }
591 void
592 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
593 {
594 ctx->pushState();
595 setStateForItem(ctx, item);
597 CairoRenderState *state = ctx->getCurrentState();
598 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
600 if (state->need_layer) {
601 state->merge_opacity = FALSE;
602 ctx->pushLayer();
603 }
604 ctx->transform(&item->transform);
605 sp_item_invoke_render(item, ctx);
607 if (state->need_layer)
608 ctx->popLayer();
610 ctx->popState();
611 }
613 bool
614 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
615 {
616 g_assert( ctx != NULL );
618 if (ctx->_vector_based_target) {
619 // width and height in pt
620 ctx->_width = sp_document_width(doc) * PT_PER_PX;
621 ctx->_height = sp_document_height(doc) * PT_PER_PX;
622 } else {
623 ctx->_width = sp_document_width(doc);
624 ctx->_height = sp_document_height(doc);
625 }
627 NRRect d;
628 bool pageBoundingBox = true;
629 if (pageBoundingBox) {
630 d.x0 = d.y0 = 0;
631 d.x1 = ceil(ctx->_width);
632 d.y1 = ceil(ctx->_height);
633 } else {
634 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
635 sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
636 if (ctx->_vector_based_target) {
637 // convert from px to pt
638 d.x0 *= PT_PER_PX;
639 d.x1 *= PT_PER_PX;
640 d.y0 *= PT_PER_PX;
641 d.y1 *= PT_PER_PX;
642 }
643 }
644 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
646 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
647 }
649 #include "macros.h" // SP_PRINT_*
651 void
652 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
653 {
654 g_assert( ctx != NULL && ctx->_is_valid );
656 if (cp == NULL)
657 return;
659 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
660 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
662 NR::Matrix saved_ctm;
663 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
664 //SP_PRINT_DRECT("clipd", cp->display->bbox);
665 NRRect clip_bbox(cp->display->bbox);
666 NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
667 t[4] = clip_bbox.x0;
668 t[5] = clip_bbox.y0;
669 t *= ctx->getCurrentState()->transform;
670 ctx->getTransform(&saved_ctm);
671 ctx->setTransform(&t);
672 }
674 TRACE(("BEGIN clip\n"));
675 SPObject *co = SP_OBJECT(cp);
676 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
677 if (SP_IS_ITEM(child)) {
678 SPItem *item = SP_ITEM(child);
679 renderItem(ctx, item);
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 void
696 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
697 {
698 g_assert( ctx != NULL && ctx->_is_valid );
700 if (mask == NULL)
701 return;
703 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
704 NRRect mask_bbox(mask->display->bbox);
705 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
706 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
707 NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
708 t[4] = mask_bbox.x0;
709 t[5] = mask_bbox.y0;
710 t *= ctx->getCurrentState()->transform;
711 ctx->setTransform(&t);
712 }
714 // clip mask contents
715 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
717 ctx->pushState();
719 TRACE(("BEGIN mask\n"));
720 SPObject *co = SP_OBJECT(mask);
721 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
722 if (SP_IS_ITEM(child)) {
723 SPItem *item = SP_ITEM(child);
724 renderItem(ctx, item);
725 }
726 }
727 TRACE(("END mask\n"));
729 ctx->popState();
730 }
732 void
733 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
734 double *x, double *y, double *width, double *height)
735 {
736 if (aspect_align == SP_ASPECT_NONE)
737 return;
739 double scalex, scaley, scale;
740 double new_width, new_height;
741 scalex = *width / vp_width;
742 scaley = *height / vp_height;
743 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
744 new_width = vp_width * scale;
745 new_height = vp_height * scale;
746 /* Now place viewbox to requested position */
747 switch (aspect_align) {
748 case SP_ASPECT_XMIN_YMIN:
749 break;
750 case SP_ASPECT_XMID_YMIN:
751 *x -= 0.5 * (new_width - *width);
752 break;
753 case SP_ASPECT_XMAX_YMIN:
754 *x -= 1.0 * (new_width - *width);
755 break;
756 case SP_ASPECT_XMIN_YMID:
757 *y -= 0.5 * (new_height - *height);
758 break;
759 case SP_ASPECT_XMID_YMID:
760 *x -= 0.5 * (new_width - *width);
761 *y -= 0.5 * (new_height - *height);
762 break;
763 case SP_ASPECT_XMAX_YMID:
764 *x -= 1.0 * (new_width - *width);
765 *y -= 0.5 * (new_height - *height);
766 break;
767 case SP_ASPECT_XMIN_YMAX:
768 *y -= 1.0 * (new_height - *height);
769 break;
770 case SP_ASPECT_XMID_YMAX:
771 *x -= 0.5 * (new_width - *width);
772 *y -= 1.0 * (new_height - *height);
773 break;
774 case SP_ASPECT_XMAX_YMAX:
775 *x -= 1.0 * (new_width - *width);
776 *y -= 1.0 * (new_height - *height);
777 break;
778 default:
779 break;
780 }
781 *width = new_width;
782 *height = new_height;
783 }
785 #include "clear-n_.h"
787 } /* namespace Internal */
788 } /* namespace Extension */
789 } /* namespace Inkscape */
791 #undef TRACE
793 /* End of GNU GPL code */
795 /*
796 Local Variables:
797 mode:c++
798 c-file-style:"stroustrup"
799 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
800 indent-tabs-mode:nil
801 fill-column:99
802 End:
803 */
804 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :