Code

86e04fb548e2904feb7676102bde39f7427b208f
[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/nr-rect.h"
32 #include <2geom/transforms.h>
33 #include <2geom/pathvector.h>
35 #include <glib/gmem.h>
37 #include <glibmm/i18n.h>
38 #include "display/nr-arena.h"
39 #include "display/nr-arena-item.h"
40 #include "display/nr-arena-group.h"
41 #include "display/curve.h"
42 #include "display/canvas-bpath.h"
43 #include "sp-item.h"
44 #include "sp-item-group.h"
45 #include "style.h"
46 #include "marker.h"
47 #include "sp-linear-gradient.h"
48 #include "sp-radial-gradient.h"
49 #include "sp-root.h"
50 #include "sp-shape.h"
51 #include "sp-use.h"
52 #include "sp-text.h"
53 #include "sp-flowtext.h"
54 #include "sp-image.h"
55 #include "sp-symbol.h"
56 #include "sp-pattern.h"
57 #include "sp-mask.h"
58 #include "sp-clippath.h"
60 #include <unit-constants.h>
61 #include "helper/png-write.h"
62 #include "helper/pixbuf-ops.h"
64 #include "cairo-renderer.h"
65 #include "cairo-render-context.h"
66 #include "extension/system.h"
68 #include "io/sys.h"
70 #include <cairo.h>
72 // include support for only the compiled-in surface types
73 #ifdef CAIRO_HAS_PDF_SURFACE
74 #include <cairo-pdf.h>
75 #endif
76 #ifdef CAIRO_HAS_PS_SURFACE
77 #include <cairo-ps.h>
78 #endif
80 //#define TRACE(_args) g_printf _args
81 #define TRACE(_args)
82 //#define TEST(_args) _args
83 #define TEST(_args)
85 // FIXME: expose these from sp-clippath/mask.cpp
86 struct SPClipPathView {
87     SPClipPathView *next;
88     unsigned int key;
89     NRArenaItem *arenaitem;
90     NRRect bbox;
91 };
93 struct SPMaskView {
94     SPMaskView *next;
95     unsigned int key;
96     NRArenaItem *arenaitem;
97     NRRect bbox;
98 };
100 namespace Inkscape {
101 namespace Extension {
102 namespace Internal {
104 CairoRenderer::CairoRenderer(void)
105 {}
107 CairoRenderer::~CairoRenderer(void)
109     /* restore default signal handling for SIGPIPE */
110 #if !defined(_WIN32) && !defined(__WIN32__)
111     (void) signal(SIGPIPE, SIG_DFL);
112 #endif
114     return;
117 CairoRenderContext*
118 CairoRenderer::createContext(void)
120     CairoRenderContext *new_context = new CairoRenderContext(this);
121     g_assert( new_context != NULL );
123     new_context->_state_stack = NULL;
124     new_context->_state = NULL;
126     // create initial render state
127     CairoRenderState *state = new_context->_createState();
128     state->transform = Geom::identity();
129     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
130     new_context->_state = state;
132     return new_context;
135 void
136 CairoRenderer::destroyContext(CairoRenderContext *ctx)
138     delete ctx;
141 /*
143 Here comes the rendering part which could be put into the 'render' methods of SPItems'
145 */
147 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
148 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
149 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
150 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
158 /* 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 ) */
159 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
161     NRRect pbox;
163     SPShape *shape = SP_SHAPE(item);
165     if (!shape->curve) return;
167     /* fixme: Think (Lauris) */
168     sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
170     SPStyle* style = SP_OBJECT_STYLE (item);
171     CairoRenderer *renderer = ctx->getRenderer();
173     Geom::PathVector const & pathv = shape->curve->get_pathvector();
175     ctx->renderPathVector(pathv, style, &pbox);
177     /* TODO: make code prettier: lots of variables can be taken out of the loop! */
178     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
179         if ( shape->marker[SP_MARKER_LOC_START] ) {
180             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
181             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
183             Geom::Matrix tr;
184             if (marker->orient_auto) {
185                 tr = sp_shape_marker_get_transform_at_start(path_it->front());
186             } else {
187                 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
188             }
190             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
191                 tr = Geom::Scale(style->stroke_width.computed) * tr;
192             }
194             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
196             Geom::Matrix old_tr = marker_item->transform;
197             marker_item->transform = tr;
198             renderer->renderItem (ctx, marker_item);
199             marker_item->transform = old_tr;
200         }
202         if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
203             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
204             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
205             while (curve_it2 != path_it->end_default())
206             {
207                 /* Put marker between curve_it1 and curve_it2.
208                  * Loop to end_default (so including closing segment), because when a path is closed,
209                  * there should be a midpoint marker between last segment and closing straight line segment */
211                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
212                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
214                 Geom::Matrix tr;
215                 if (marker->orient_auto) {
216                     tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
217                 } else {
218                     tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
219                 }
221                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
222                     tr = Geom::Scale(style->stroke_width.computed) * tr;
223                 }
225                 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
227                 Geom::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             /* Get reference to last curve in the path.
242              * For moveto-only path, this returns the "closing line segment". */
243             unsigned int index = path_it->size_default();
244             if (index > 0) {
245                 index--;
246             }
247             Geom::Curve const &lastcurve = (*path_it)[index];
249             Geom::Matrix tr;
250             if (marker->orient_auto) {
251                 tr = sp_shape_marker_get_transform_at_end(lastcurve);
252             } else {
253                 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
254             }
256             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
257                 tr = Geom::Scale(style->stroke_width.computed) * tr;
258             }
260             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
262             Geom::Matrix old_tr = marker_item->transform;
263             marker_item->transform = tr;
264             renderer->renderItem (ctx, marker_item);
265             marker_item->transform = old_tr;
266         }
267     }
270 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
272     SPGroup *group = SP_GROUP(item);
273     CairoRenderer *renderer = ctx->getRenderer();
274     TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
276     GSList *l = g_slist_reverse(group->childList(false));
277     while (l) {
278         SPObject *o = SP_OBJECT (l->data);
279         if (SP_IS_ITEM(o)) {
280             renderer->renderItem (ctx, SP_ITEM (o));
281         }
282         l = g_slist_remove (l, o);
283     }
286 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
288     bool translated = false;
289     SPUse *use = SP_USE(item);
290     CairoRenderer *renderer = ctx->getRenderer();
292     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
293         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
294         ctx->pushState();
295         ctx->transform(&tp);
296         translated = true;
297     }
299     if (use->child && SP_IS_ITEM(use->child)) {
300         renderer->renderItem(ctx, SP_ITEM(use->child));
301     }
303     if (translated) {
304         ctx->popState();
305     }
308 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
310     SPText *group = SP_TEXT (item);
311     group->layout.showGlyphs(ctx);
314 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
316     SPFlowtext *group = SP_FLOWTEXT(item);
317     group->layout.showGlyphs(ctx);
320 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
322     SPImage *image;
323     guchar *px;
324     int w, h, rs;
326     image = SP_IMAGE (item);
328     if (!image->pixbuf) return;
329     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
331     px = gdk_pixbuf_get_pixels (image->pixbuf);
332     w = gdk_pixbuf_get_width (image->pixbuf);
333     h = gdk_pixbuf_get_height (image->pixbuf);
334     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
336     double x = image->x.computed;
337     double y = image->y.computed;
338     double width = image->width.computed;
339     double height = image->height.computed;
341     if (image->aspect_align != SP_ASPECT_NONE) {
342         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
343                                                      &x, &y, &width, &height);
344     }
346     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
347         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
348     }
350     Geom::Translate tp(x, y);
351     Geom::Scale s(width / (double)w, height / (double)h);
352     Geom::Matrix t(s * tp);
354     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
357 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
359     SPSymbol *symbol = SP_SYMBOL(item);
360     if (!SP_OBJECT_IS_CLONED (symbol))
361         return;
363     /* Cloned <symbol> is actually renderable */
364     ctx->pushState();
365     ctx->transform(&symbol->c2p);
367     // apply viewbox if set
368     if (0 /*symbol->viewBox_set*/) {
369         Geom::Matrix vb2user;
370         double x, y, width, height;
371         double view_width, view_height;
372         x = 0.0;
373         y = 0.0;
374         width = 1.0;
375         height = 1.0;
377         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
378         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
380         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
381                                      &x, &y,&width, &height);
383         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
384         vb2user = Geom::identity();
385         vb2user[0] = width / view_width;
386         vb2user[3] = height / view_height;
387         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
388         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
390         ctx->transform(&vb2user);
391     }
393     sp_group_render(item, ctx);
394     ctx->popState();
397 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
399     SPRoot *root = SP_ROOT(item);
400     CairoRenderer *renderer = ctx->getRenderer();
402     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
403         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
405     ctx->pushState();
406     renderer->setStateForItem(ctx, item);
407     Geom::Matrix tempmat (root->c2p);
408     ctx->transform(&tempmat);
409     sp_group_render(item, ctx);
410     ctx->popState();
413 /**
414     This function converts the item to a raster image and includes the image into the cairo renderer.
415     It is only used for filters and then only when rendering filters as bitmaps is requested.
416 */
417 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
420     // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
422     // Calculate resolution
423     double res;
424     /** @TODO reimplement the resolution stuff   (WHY?)
425     */
426     res = ctx->getBitmapResolution();
427     if(res == 0) {
428         res = PX_PER_IN;
429     }
430     TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
432     // Get the bounding box of the selection in document coordinates.
433     NRRect bbox(item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX ));
435     // The width and height of the bitmap in pixels
436     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * (res / PX_PER_IN));
437     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * (res / PX_PER_IN));
438     
439     // Scale to exactly fit integer bitmap inside bounding box
440     double scale_x = (bbox.x1 - bbox.x0) / width;
441     double scale_y = (bbox.y1 - bbox.y0) / height;
443     // Location of bounding box in document coordinates.
444     double shift_x = bbox.x0;
445     double shift_y = bbox.y1;
447     // For default 90 dpi, snap bitmap to pixel grid
448     if (res == PX_PER_IN) { 
449         shift_x = round (shift_x);
450         shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
451                                      // Remove the negations when the inversion is gone.
452     }
454     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
456     // Matix to put bitmap in correct place on document
457     Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
458                                  (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
460     // ctx matrix already includes item transformation. We must substract.
461     Geom::Matrix t_item =  sp_item_i2d_affine (item);
462     Geom::Matrix t = t_on_document * t_item.inverse();
464     // Do the export
465     SPDocument *document = SP_OBJECT(item)->document;
466     GSList *items = NULL;
467     items = g_slist_append(items, item);
469     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
470         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
472     if (pb) {
473         TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
474         unsigned char *px = gdk_pixbuf_get_pixels (pb);
475         unsigned int w = gdk_pixbuf_get_width(pb);
476         unsigned int h = gdk_pixbuf_get_height(pb);
477         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
478         ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
479         gdk_pixbuf_unref (pb);
480     }
481     g_slist_free (items);
485 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
487     // Check item's visibility
488     if (item->isHidden()) {
489         return;
490     }
492     SPStyle* style = SP_OBJECT_STYLE (item);
493     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
494         return sp_asbitmap_render(item, ctx);
495     }
497     if (SP_IS_ROOT(item)) {
498         TRACE(("root\n"));
499         return sp_root_render(item, ctx);
500     } else if (SP_IS_GROUP(item)) {
501         TRACE(("group\n"));
502         return sp_group_render(item, ctx);
503     } else if (SP_IS_SHAPE(item)) {
504         TRACE(("shape\n"));
505         return sp_shape_render(item, ctx);
506     } else if (SP_IS_USE(item)) {
507         TRACE(("use begin---\n"));
508         sp_use_render(item, ctx);
509         TRACE(("---use end\n"));
510     } else if (SP_IS_SYMBOL(item)) {
511         TRACE(("symbol\n"));
512         return sp_symbol_render(item, ctx);
513     } else if (SP_IS_TEXT(item)) {
514         TRACE(("text\n"));
515         return sp_text_render(item, ctx);
516     } else if (SP_IS_FLOWTEXT(item)) {
517         TRACE(("flowtext\n"));
518         return sp_flowtext_render(item, ctx);
519     } else if (SP_IS_IMAGE(item)) {
520         TRACE(("image\n"));
521         return sp_image_render(item, ctx);
522     }
525 void
526 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
528     SPStyle const *style = SP_OBJECT_STYLE(item);
529     ctx->setStateForStyle(style);
531     CairoRenderState *state = ctx->getCurrentState();
532     state->clip_path = item->clip_ref->getObject();
533     state->mask = item->mask_ref->getObject();
534     state->item_transform = Geom::Matrix (item->transform);
536     // If parent_has_userspace is true the parent state's transform
537     // has to be used for the mask's/clippath's context.
538     // This is so because we use the image's/(flow)text's transform for positioning
539     // instead of explicitly specifying it and letting the renderer do the
540     // transformation before rendering the item.
541     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
542         state->parent_has_userspace = TRUE;
543     TRACE(("setStateForItem opacity: %f\n", state->opacity));
546 void
547 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
549     ctx->pushState();
550     setStateForItem(ctx, item);
552     CairoRenderState *state = ctx->getCurrentState();
553     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
555     // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
556     if (state->need_layer) {
557         state->merge_opacity = FALSE;
558         ctx->pushLayer();
559     }
560     Geom::Matrix tempmat (item->transform);
561     ctx->transform(&tempmat);
562     sp_item_invoke_render(item, ctx);
564     if (state->need_layer)
565         ctx->popLayer();
567     ctx->popState();
570 bool
571 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
573     g_assert( ctx != NULL );
575     if (!base)
576         base = SP_ITEM(sp_document_root(doc));
578     NRRect d;
579     if (pageBoundingBox) {
580         d.x0 = d.y0 = 0;
581         d.x1 = ceil(sp_document_width(doc));
582         d.y1 = ceil(sp_document_height(doc));
583     } else {
584         sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE, SPItem::RENDERING_BBOX);
585     }
587     if (ctx->_vector_based_target) {
588         // convert from px to pt
589         d.x0 *= PT_PER_PX;
590         d.x1 *= PT_PER_PX;
591         d.y0 *= PT_PER_PX;
592         d.y1 *= PT_PER_PX;
593     }
595     ctx->_width = d.x1-d.x0;
596     ctx->_height = d.y1-d.y0;
598     TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
600     bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
602     if (ret && !pageBoundingBox)
603     {
604         double high = sp_document_height(doc);
605         if (ctx->_vector_based_target)
606             high *= PT_PER_PX;
608         Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
609                                     (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
610         ctx->transform(&tp);
611     }
612     
613     return ret;
616 #include "macros.h" // SP_PRINT_*
618 // Apply an SVG clip path
619 void
620 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
622     g_assert( ctx != NULL && ctx->_is_valid );
624     if (cp == NULL)
625         return;
627     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
628     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
630     Geom::Matrix saved_ctm;
631     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
632         //SP_PRINT_DRECT("clipd", cp->display->bbox);
633         NRRect clip_bbox(cp->display->bbox);
634         Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
635         t[4] = clip_bbox.x0;
636         t[5] = clip_bbox.y0;
637         t *= ctx->getCurrentState()->transform;
638         ctx->getTransform(&saved_ctm);
639         ctx->setTransform(&t);
640     }
642     TRACE(("BEGIN clip\n"));
643     SPObject *co = SP_OBJECT(cp);
644     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
645         if (SP_IS_ITEM(child)) {
646             SPItem *item = SP_ITEM(child);
648             // combine transform of the item in clippath and the item using clippath:
649             Geom::Matrix tempmat (item->transform);
650             tempmat = tempmat * (ctx->getCurrentState()->item_transform);
652             // render this item in clippath
653             ctx->pushState();
654             ctx->transform(&tempmat);
655             setStateForItem(ctx, item);
656             sp_item_invoke_render(item, ctx);
657             ctx->popState();
658         }
659     }
660     TRACE(("END clip\n"));
662     // do clipping only if this was the first call to applyClipPath
663     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
664         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
665         cairo_clip(ctx->_cr);
667     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
668         ctx->setTransform(&saved_ctm);
670     ctx->setRenderMode(saved_mode);
673 // Apply an SVG mask
674 void
675 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
677     g_assert( ctx != NULL && ctx->_is_valid );
679     if (mask == NULL)
680         return;
682     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
683     NRRect mask_bbox(mask->display->bbox);
684     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
685     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
686         Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
687         t[4] = mask_bbox.x0;
688         t[5] = mask_bbox.y0;
689         t *= ctx->getCurrentState()->transform;
690         ctx->setTransform(&t);
691     }
693     // Clip mask contents... but...
694     // The mask's bounding box is the "geometric bounding box" which doesn't allow for
695     // filters which extend outside the bounding box. So don't clip.
696     // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
698     ctx->pushState();
700     TRACE(("BEGIN mask\n"));
701     SPObject *co = SP_OBJECT(mask);
702     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
703         if (SP_IS_ITEM(child)) {
704             SPItem *item = SP_ITEM(child);
705             renderItem(ctx, item);
706         }
707     }
708     TRACE(("END mask\n"));
710     ctx->popState();
713 void
714 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
715                              double *x, double *y, double *width, double *height)
717     if (aspect_align == SP_ASPECT_NONE)
718         return;
720     double scalex, scaley, scale;
721     double new_width, new_height;
722     scalex = *width / vp_width;
723     scaley = *height / vp_height;
724     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
725     new_width = vp_width * scale;
726     new_height = vp_height * scale;
727     /* Now place viewbox to requested position */
728     switch (aspect_align) {
729         case SP_ASPECT_XMIN_YMIN:
730             break;
731         case SP_ASPECT_XMID_YMIN:
732             *x -= 0.5 * (new_width - *width);
733             break;
734         case SP_ASPECT_XMAX_YMIN:
735             *x -= 1.0 * (new_width - *width);
736             break;
737         case SP_ASPECT_XMIN_YMID:
738             *y -= 0.5 * (new_height - *height);
739             break;
740         case SP_ASPECT_XMID_YMID:
741             *x -= 0.5 * (new_width - *width);
742             *y -= 0.5 * (new_height - *height);
743             break;
744         case SP_ASPECT_XMAX_YMID:
745             *x -= 1.0 * (new_width - *width);
746             *y -= 0.5 * (new_height - *height);
747             break;
748         case SP_ASPECT_XMIN_YMAX:
749             *y -= 1.0 * (new_height - *height);
750             break;
751         case SP_ASPECT_XMID_YMAX:
752             *x -= 0.5 * (new_width - *width);
753             *y -= 1.0 * (new_height - *height);
754             break;
755         case SP_ASPECT_XMAX_YMAX:
756             *x -= 1.0 * (new_width - *width);
757             *y -= 1.0 * (new_height - *height);
758             break;
759         default:
760             break;
761     }
762     *width = new_width;
763     *height = new_height;
766 #include "clear-n_.h"
768 }  /* namespace Internal */
769 }  /* namespace Extension */
770 }  /* namespace Inkscape */
772 #undef TRACE
774 /* End of GNU GPL code */
776 /*
777   Local Variables:
778   mode:c++
779   c-file-style:"stroustrup"
780   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
781   indent-tabs-mode:nil
782   fill-column:99
783   End:
784 */
785 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :