Code

fix orientation of markers
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
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/n-art-bpath.h>
32 #include <libnr/nr-matrix-ops.h>
33 #include <libnr/nr-matrix-fns.h>
34 #include <libnr/nr-matrix-translate-ops.h>
35 #include <libnr/nr-scale-matrix-ops.h>
37 #include "libnr/nr-matrix-rotate-ops.h"
38 #include "libnr/nr-matrix-translate-ops.h"
39 #include "libnr/nr-rotate-fns.h"
40 #include "libnr/nr-scale-ops.h"
41 #include "libnr/nr-scale-translate-ops.h"
42 #include "libnr/nr-translate-matrix-ops.h"
43 #include "libnr/nr-translate-scale-ops.h"
44 #include "libnr/nr-convert2geom.h"
45 #include <2geom/transforms.h>
46 #include <2geom/pathvector.h>
48 #include <glib/gmem.h>
50 #include <glibmm/i18n.h>
51 #include "display/nr-arena.h"
52 #include "display/nr-arena-item.h"
53 #include "display/nr-arena-group.h"
54 #include "display/curve.h"
55 #include "display/canvas-bpath.h"
56 #include "sp-item.h"
57 #include "sp-item-group.h"
58 #include "style.h"
59 #include "marker.h"
60 #include "sp-linear-gradient.h"
61 #include "sp-radial-gradient.h"
62 #include "sp-root.h"
63 #include "sp-shape.h"
64 #include "sp-use.h"
65 #include "sp-text.h"
66 #include "sp-flowtext.h"
67 #include "sp-image.h"
68 #include "sp-symbol.h"
69 #include "sp-pattern.h"
70 #include "sp-mask.h"
71 #include "sp-clippath.h"
73 #include <unit-constants.h>
74 #include "helper/png-write.h"
75 #include "helper/pixbuf-ops.h"
77 #include "cairo-renderer.h"
78 #include "cairo-render-context.h"
79 #include "extension/system.h"
81 #include "io/sys.h"
83 #include <cairo.h>
85 // include support for only the compiled-in surface types
86 #ifdef CAIRO_HAS_PDF_SURFACE
87 #include <cairo-pdf.h>
88 #endif
89 #ifdef CAIRO_HAS_PS_SURFACE
90 #include <cairo-ps.h>
91 #endif
93 //#define TRACE(_args) g_printf _args
94 #define TRACE(_args)
96 // FIXME: expose these from sp-clippath/mask.cpp
97 struct SPClipPathView {
98     SPClipPathView *next;
99     unsigned int key;
100     NRArenaItem *arenaitem;
101     NRRect bbox;
102 };
104 struct SPMaskView {
105     SPMaskView *next;
106     unsigned int key;
107     NRArenaItem *arenaitem;
108     NRRect bbox;
109 };
111 namespace Inkscape {
112 namespace Extension {
113 namespace Internal {
115 CairoRenderer::CairoRenderer(void)
116 {}
118 CairoRenderer::~CairoRenderer(void)
120     /* restore default signal handling for SIGPIPE */
121 #if !defined(_WIN32) && !defined(__WIN32__)
122     (void) signal(SIGPIPE, SIG_DFL);
123 #endif
125     return;
128 CairoRenderContext*
129 CairoRenderer::createContext(void)
131     CairoRenderContext *new_context = new CairoRenderContext(this);
132     g_assert( new_context != NULL );
134     new_context->_state_stack = NULL;
135     new_context->_state = NULL;
137     // create initial render state
138     CairoRenderState *state = new_context->_createState();
139     state->transform.set_identity();
140     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
141     new_context->_state = state;
143     return new_context;
146 void
147 CairoRenderer::destroyContext(CairoRenderContext *ctx)
149     delete ctx;
152 /*
154 Here comes the rendering part which could be put into the 'render' methods of SPItems'
156 */
158 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
159 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
167 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
169 /* 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 ) */
170 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
172     NRRect pbox;
174     SPShape *shape = SP_SHAPE(item);
176     if (!shape->curve) return;
178     /* fixme: Think (Lauris) */
179     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
181     SPStyle* style = SP_OBJECT_STYLE (item);
182     CairoRenderer *renderer = ctx->getRenderer();
184     Geom::PathVector const & pathv = shape->curve->get_pathvector();
186     ctx->renderPathVector(pathv, style, &pbox);
188     /* TODO: make code prettier: lots of variables can be taken out of the loop! */
189     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
190         if ( shape->marker[SP_MARKER_LOC_START] ) {
191             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
192             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
194             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_start(path_it->front())));
196             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
197                 tr = NR::scale(style->stroke_width.computed) * tr;
198             }
200             tr = marker_item->transform * marker->c2p * tr;
202             NR::Matrix old_tr = marker_item->transform;
203             marker_item->transform = tr;
204             renderer->renderItem (ctx, marker_item);
205             marker_item->transform = old_tr;
206         }
208         if ( shape->marker[SP_MARKER_LOC_MID] ) {
209             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
210             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
211             while (curve_it2 != path_it->end_default())
212             {
213                 /* Put marker between curve_it1 and curve_it2.
214                  * Loop to end_default (so including closing segment), because when a path is closed,
215                  * there should be a midpoint marker between last segment and closing straight line segment */
217                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
218                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
220                 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(*curve_it1, *curve_it2)));
222                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
223                     tr = NR::scale(style->stroke_width.computed) * tr;
224                 }
226                 tr = marker_item->transform * marker->c2p * tr;
228                 NR::Matrix old_tr = marker_item->transform;
229                 marker_item->transform = tr;
230                 renderer->renderItem (ctx, marker_item);
231                 marker_item->transform = old_tr;
233                 ++curve_it1;
234                 ++curve_it2;
235             }
236         }
238         if ( shape->marker[SP_MARKER_LOC_END] ) {
239             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
240             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
242             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_end(path_it->back_default())));
244             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
245                 tr = NR::scale(style->stroke_width.computed) * tr;
246             }
248             tr = marker_item->transform * marker->c2p * tr;
250             NR::Matrix old_tr = marker_item->transform;
251             marker_item->transform = tr;
252             renderer->renderItem (ctx, marker_item);
253             marker_item->transform = old_tr;
254         }
255     }
258 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
260     SPGroup *group = SP_GROUP(item);
261     CairoRenderer *renderer = ctx->getRenderer();
262     TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
264     GSList *l = g_slist_reverse(group->childList(false));
265     while (l) {
266         SPObject *o = SP_OBJECT (l->data);
267         if (SP_IS_ITEM(o)) {
268             renderer->renderItem (ctx, SP_ITEM (o));
269         }
270         l = g_slist_remove (l, o);
271     }
274 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
276     bool translated = false;
277     SPUse *use = SP_USE(item);
278     CairoRenderer *renderer = ctx->getRenderer();
280     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
281         NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
282         ctx->pushState();
283         ctx->transform(&tp);
284         translated = true;
285     }
287     if (use->child && SP_IS_ITEM(use->child)) {
288         renderer->renderItem(ctx, SP_ITEM(use->child));
289     }
291     if (translated) {
292         ctx->popState();
293     }
296 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
298     SPText *group = SP_TEXT (item);
299     group->layout.showGlyphs(ctx);
302 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
304     SPFlowtext *group = SP_FLOWTEXT(item);
305     group->layout.showGlyphs(ctx);
308 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
310     SPImage *image;
311     guchar *px;
312     int w, h, rs;
314     image = SP_IMAGE (item);
316     if (!image->pixbuf) return;
317     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
319     px = gdk_pixbuf_get_pixels (image->pixbuf);
320     w = gdk_pixbuf_get_width (image->pixbuf);
321     h = gdk_pixbuf_get_height (image->pixbuf);
322     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
324     double x = image->x.computed;
325     double y = image->y.computed;
326     double width = image->width.computed;
327     double height = image->height.computed;
329     if (image->aspect_align != SP_ASPECT_NONE) {
330         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
331                                                      &x, &y, &width, &height);
332     }
334     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
335         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
336     }
338     NR::translate tp(x, y);
339     NR::scale s(width / (double)w, height / (double)h);
340     NR::Matrix t(s * tp);
342     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
345 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
347     SPSymbol *symbol = SP_SYMBOL(item);
348     if (!SP_OBJECT_IS_CLONED (symbol))
349         return;
351     /* Cloned <symbol> is actually renderable */
352     ctx->pushState();
353     ctx->transform(&symbol->c2p);
355     // apply viewbox if set
356     if (0 /*symbol->viewBox_set*/) {
357         NR::Matrix vb2user;
358         double x, y, width, height;
359         double view_width, view_height;
360         x = 0.0;
361         y = 0.0;
362         width = 1.0;
363         height = 1.0;
365         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
366         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
368         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
369                                      &x, &y,&width, &height);
371         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
372         vb2user.set_identity();
373         vb2user[0] = width / view_width;
374         vb2user[3] = height / view_height;
375         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
376         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
378         ctx->transform(&vb2user);
379     }
381     sp_group_render(item, ctx);
382     ctx->popState();
385 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
387     SPRoot *root = SP_ROOT(item);
388     CairoRenderer *renderer = ctx->getRenderer();
390     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
391         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
393     ctx->pushState();
394     renderer->setStateForItem(ctx, item);
395     ctx->transform(&root->c2p);
396     sp_group_render(item, ctx);
397     ctx->popState();
400 /**
401     this function convert the item to a raster image and include the raster into the cairo renderer
402 */
403 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
405     g_warning("render as bitmap");
407     //the code now was copied from sp_selection_create_bitmap_copy
409     SPDocument *document = SP_OBJECT(item)->document;
410     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
412     // Get the bounding box of the selection
413     //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
414     // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
415     NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
418     // List of the items to show; all others will be hidden
419     GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
420     items = g_slist_append(items, item);
422     // Remember parent and z-order of the topmost one
423     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
424     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
425     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
427     // Calculate resolution
428     double res;
429     /** @TODO reimplement the resolution stuff
430     */
431     res = ctx->getBitmapResolution();
432     if(res == 0) {
433         res = PX_PER_IN;
434     }
437     // The width and height of the bitmap in pixels
438     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
439     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
441     // Find out if we have to run a filter
442     gchar const *run = NULL;
443     gchar const *filter = NULL;
444     /** @TODO reimplement the filter stuff
445     //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
446     if (filter) {
447         // filter command is given;
448         // see if we have a parameter to pass to it
449         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
450         if (param1) {
451             if (param1[strlen(param1) - 1] == '%') {
452                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
453                 gchar p1[256];
454                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
455                 // the first param is always the image filename, the second is param1
456                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
457             } else {
458                 // otherwise pass the param1 unchanged
459                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
460             }
461         } else {
462             // run without extra parameter
463             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
464         }
465     }
466     */
467     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
468     NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
469     NR::Matrix t;
471     double shift_x = bbox.x0;
472     double shift_y = bbox.y1;
473     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
474         shift_x = round (shift_x);
475         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
476     }
477     t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
479     //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
481     // Do the export
482     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
483         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
485     // Run filter, if any
486     /*
487     if (run) {
488         g_print ("Running external filter: %s\n", run);
489         system (run);
490     }
491     */
492     if (pb) {
493         unsigned char *px = gdk_pixbuf_get_pixels (pb);
494         unsigned int w = gdk_pixbuf_get_width(pb);
495         unsigned int h = gdk_pixbuf_get_height(pb);
496         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
497         NR::Matrix matrix;
498         matrix = t;
499         //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
500         //matrix.set_identity();
502         ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
503     /*
504         // Create the repr for the image
505         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
506         repr->setAttribute("xlink:href", filename);
507         repr->setAttribute("sodipodi:absref", filepath);
508         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
509             sp_repr_set_svg_double(repr, "width", width);
510             sp_repr_set_svg_double(repr, "height", height);
511         } else {
512             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
513             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
514         }
516         // Write transform
517         gchar *c=sp_svg_transform_write(t);
518         repr->setAttribute("transform", c);
519         g_free(c);
521         // add the new repr to the parent
522         parent->appendChild(repr);
524         // move to the saved position
525         repr->setPosition(pos > 0 ? pos + 1 : 1);
526     */
527         gdk_pixbuf_unref (pb);
528     }
529     g_slist_free (items);
532 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
534     // Check item's visibility
535     if (item->isHidden()) {
536         return;
537     }
539     SPStyle* style = SP_OBJECT_STYLE (item);
540     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
541         return sp_asbitmap_render(item, ctx);
542     }
544     if (SP_IS_ROOT(item)) {
545         TRACE(("root\n"));
546         return sp_root_render(item, ctx);
547     } else if (SP_IS_GROUP(item)) {
548         TRACE(("group\n"));
549         return sp_group_render(item, ctx);
550     } else if (SP_IS_SHAPE(item)) {
551         TRACE(("shape\n"));
552         return sp_shape_render(item, ctx);
553     } else if (SP_IS_USE(item)) {
554         TRACE(("use begin---\n"));
555         sp_use_render(item, ctx);
556         TRACE(("---use end\n"));
557     } else if (SP_IS_SYMBOL(item)) {
558         TRACE(("symbol\n"));
559         return sp_symbol_render(item, ctx);
560     } else if (SP_IS_TEXT(item)) {
561         TRACE(("text\n"));
562         return sp_text_render(item, ctx);
563     } else if (SP_IS_FLOWTEXT(item)) {
564         TRACE(("flowtext\n"));
565         return sp_flowtext_render(item, ctx);
566     } else if (SP_IS_IMAGE(item)) {
567         TRACE(("image\n"));
568         return sp_image_render(item, ctx);
569     }
572 void
573 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
575     SPStyle const *style = SP_OBJECT_STYLE(item);
576     ctx->setStateForStyle(style);
578     CairoRenderState *state = ctx->getCurrentState();
579     state->clip_path = item->clip_ref->getObject();
580     state->mask = item->mask_ref->getObject();
582     // If parent_has_userspace is true the parent state's transform
583     // has to be used for the mask's/clippath's context.
584     // This is so because we use the image's/(flow)text's transform for positioning
585     // instead of explicitly specifying it and letting the renderer do the
586     // transformation before rendering the item.
587     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
588         state->parent_has_userspace = TRUE;
589     TRACE(("set op: %f\n", state->opacity));
592 void
593 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
595     ctx->pushState();
596     setStateForItem(ctx, item);
598     CairoRenderState *state = ctx->getCurrentState();
599     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
601     if (state->need_layer) {
602         state->merge_opacity = FALSE;
603         ctx->pushLayer();
604     }
605     ctx->transform(&item->transform);
606     sp_item_invoke_render(item, ctx);
608     if (state->need_layer)
609         ctx->popLayer();
611     ctx->popState();
614 bool
615 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
617     g_assert( ctx != NULL );
619     if (ctx->_vector_based_target) {
620         // width and height in pt
621         ctx->_width = sp_document_width(doc) * PT_PER_PX;
622         ctx->_height = sp_document_height(doc) * PT_PER_PX;
623     } else {
624         ctx->_width = sp_document_width(doc);
625         ctx->_height = sp_document_height(doc);
626     }
628     NRRect d;
629     bool pageBoundingBox = true;
630     if (pageBoundingBox) {
631         d.x0 = d.y0 = 0;
632         d.x1 = ceil(ctx->_width);
633         d.y1 = ceil(ctx->_height);
634     } else {
635         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
636         sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
637         if (ctx->_vector_based_target) {
638             // convert from px to pt
639             d.x0 *= PT_PER_PX;
640             d.x1 *= PT_PER_PX;
641             d.y0 *= PT_PER_PX;
642             d.y1 *= PT_PER_PX;
643         }
644     }
645     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
647     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
650 #include "macros.h" // SP_PRINT_*
652 void
653 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
655     g_assert( ctx != NULL && ctx->_is_valid );
657     if (cp == NULL)
658         return;
660     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
661     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
663     NR::Matrix saved_ctm;
664     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
665         //SP_PRINT_DRECT("clipd", cp->display->bbox);
666         NRRect clip_bbox(cp->display->bbox);
667         NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
668         t[4] = clip_bbox.x0;
669         t[5] = clip_bbox.y0;
670         t *= ctx->getCurrentState()->transform;
671         ctx->getTransform(&saved_ctm);
672         ctx->setTransform(&t);
673     }
675     TRACE(("BEGIN clip\n"));
676     SPObject *co = SP_OBJECT(cp);
677     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
678         if (SP_IS_ITEM(child)) {
679             SPItem *item = SP_ITEM(child);
680             renderItem(ctx, item);
681         }
682     }
683     TRACE(("END clip\n"));
685     // do clipping only if this was the first call to applyClipPath
686     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
687         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
688         cairo_clip(ctx->_cr);
690     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
691         ctx->setTransform(&saved_ctm);
693     ctx->setRenderMode(saved_mode);
696 void
697 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
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         NR::Matrix t(NR::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
716     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
718     ctx->pushState();
720     TRACE(("BEGIN mask\n"));
721     SPObject *co = SP_OBJECT(mask);
722     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
723         if (SP_IS_ITEM(child)) {
724             SPItem *item = SP_ITEM(child);
725             renderItem(ctx, item);
726         }
727     }
728     TRACE(("END mask\n"));
730     ctx->popState();
733 void
734 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
735                              double *x, double *y, double *width, double *height)
737     if (aspect_align == SP_ASPECT_NONE)
738         return;
740     double scalex, scaley, scale;
741     double new_width, new_height;
742     scalex = *width / vp_width;
743     scaley = *height / vp_height;
744     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
745     new_width = vp_width * scale;
746     new_height = vp_height * scale;
747     /* Now place viewbox to requested position */
748     switch (aspect_align) {
749         case SP_ASPECT_XMIN_YMIN:
750             break;
751         case SP_ASPECT_XMID_YMIN:
752             *x -= 0.5 * (new_width - *width);
753             break;
754         case SP_ASPECT_XMAX_YMIN:
755             *x -= 1.0 * (new_width - *width);
756             break;
757         case SP_ASPECT_XMIN_YMID:
758             *y -= 0.5 * (new_height - *height);
759             break;
760         case SP_ASPECT_XMID_YMID:
761             *x -= 0.5 * (new_width - *width);
762             *y -= 0.5 * (new_height - *height);
763             break;
764         case SP_ASPECT_XMAX_YMID:
765             *x -= 1.0 * (new_width - *width);
766             *y -= 0.5 * (new_height - *height);
767             break;
768         case SP_ASPECT_XMIN_YMAX:
769             *y -= 1.0 * (new_height - *height);
770             break;
771         case SP_ASPECT_XMID_YMAX:
772             *x -= 0.5 * (new_width - *width);
773             *y -= 1.0 * (new_height - *height);
774             break;
775         case SP_ASPECT_XMAX_YMAX:
776             *x -= 1.0 * (new_width - *width);
777             *y -= 1.0 * (new_height - *height);
778             break;
779         default:
780             break;
781     }
782     *width = new_width;
783     *height = new_height;
786 #include "clear-n_.h"
788 }  /* namespace Internal */
789 }  /* namespace Extension */
790 }  /* namespace Inkscape */
792 #undef TRACE
794 /* End of GNU GPL code */
796 /*
797   Local Variables:
798   mode:c++
799   c-file-style:"stroustrup"
800   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
801   indent-tabs-mode:nil
802   fill-column:99
803   End:
804 */
805 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :