Code

Added missing (and very important) file.
[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 static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
160     bool render = true;
161     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
162         if (style->stroke_width.computed > 1e-9) {
163             tr = Geom::Scale(style->stroke_width.computed) * tr;
164         } else {
165             render = false; // stroke width zero and marker is thus scaled down to zero, skip
166         }
167     }
169     if (render) {
170         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
171         tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
172         Geom::Matrix old_tr = marker_item->transform;
173         marker_item->transform = tr;
174         ctx->getRenderer()->renderItem (ctx, marker_item);
175         marker_item->transform = old_tr;
176     }
179 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
181     NRRect pbox;
183     SPShape *shape = SP_SHAPE(item);
185     if (!shape->curve) return;
187     sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
189     SPStyle* style = SP_OBJECT_STYLE (item);
191     Geom::PathVector const & pathv = shape->curve->get_pathvector();
193     ctx->renderPathVector(pathv, style, &pbox);
195     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
196       // START position
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(path_it->front());
203                 } else {
204                     tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
205                 }
206                 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
207             }
208         }
210       // MID position
211         for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
212             if ( shape->marker[i] && (path_it->size_default() > 1) ) {
213                 Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
214                 Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
215                 while (curve_it2 != path_it->end_default())
216                 {
217                     /* Put marker between curve_it1 and curve_it2.
218                      * Loop to end_default (so including closing segment), because when a path is closed,
219                      * there should be a midpoint marker between last segment and closing straight line segment */
221                     SPMarker* marker = SP_MARKER (shape->marker[i]);
223                     Geom::Matrix tr;
224                     if (marker->orient_auto) {
225                         tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
226                     } else {
227                         tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
228                     }
230                     sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
232                     ++curve_it1;
233                     ++curve_it2;
234                 }
235             }
236         }
238       // END position
239         for (int i = 0; i < 4; i += 3) {  // SP_MARKER_LOC and SP_MARKER_LOC_END
240             if ( shape->marker[i] ) {
241                 SPMarker* marker = SP_MARKER (shape->marker[i]);
243                 /* Get reference to last curve in the path.
244                  * For moveto-only path, this returns the "closing line segment". */
245                 unsigned int index = path_it->size_default();
246                 if (index > 0) {
247                     index--;
248                 }
249                 Geom::Curve const &lastcurve = (*path_it)[index];
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                 }
258                 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
259             }
260         }
261     }
264 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
266     SPGroup *group = SP_GROUP(item);
267     CairoRenderer *renderer = ctx->getRenderer();
268     TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
270     GSList *l = g_slist_reverse(group->childList(false));
271     while (l) {
272         SPObject *o = SP_OBJECT (l->data);
273         if (SP_IS_ITEM(o)) {
274             renderer->renderItem (ctx, SP_ITEM (o));
275         }
276         l = g_slist_remove (l, o);
277     }
280 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
282     bool translated = false;
283     SPUse *use = SP_USE(item);
284     CairoRenderer *renderer = ctx->getRenderer();
286     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
287         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
288         ctx->pushState();
289         ctx->transform(&tp);
290         translated = true;
291     }
293     if (use->child && SP_IS_ITEM(use->child)) {
294         renderer->renderItem(ctx, SP_ITEM(use->child));
295     }
297     if (translated) {
298         ctx->popState();
299     }
302 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
304     SPText *group = SP_TEXT (item);
305     group->layout.showGlyphs(ctx);
308 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
310     SPFlowtext *group = SP_FLOWTEXT(item);
311     group->layout.showGlyphs(ctx);
314 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
316     SPImage *image;
317     guchar *px;
318     int w, h, rs;
320     image = SP_IMAGE (item);
322     if (!image->pixbuf) return;
323     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
325     px = gdk_pixbuf_get_pixels (image->pixbuf);
326     w = gdk_pixbuf_get_width (image->pixbuf);
327     h = gdk_pixbuf_get_height (image->pixbuf);
328     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
330     double x = image->x.computed;
331     double y = image->y.computed;
332     double width = image->width.computed;
333     double height = image->height.computed;
335     if (image->aspect_align != SP_ASPECT_NONE) {
336         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
337                                                      &x, &y, &width, &height);
338     }
340     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
341         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
342     }
344     Geom::Translate tp(x, y);
345     Geom::Scale s(width / (double)w, height / (double)h);
346     Geom::Matrix t(s * tp);
348     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
351 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
353     SPSymbol *symbol = SP_SYMBOL(item);
354     if (!SP_OBJECT_IS_CLONED (symbol))
355         return;
357     /* Cloned <symbol> is actually renderable */
358     ctx->pushState();
359     ctx->transform(&symbol->c2p);
361     // apply viewbox if set
362     if (0 /*symbol->viewBox_set*/) {
363         Geom::Matrix vb2user;
364         double x, y, width, height;
365         double view_width, view_height;
366         x = 0.0;
367         y = 0.0;
368         width = 1.0;
369         height = 1.0;
371         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
372         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
374         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
375                                      &x, &y,&width, &height);
377         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
378         vb2user = Geom::identity();
379         vb2user[0] = width / view_width;
380         vb2user[3] = height / view_height;
381         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
382         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
384         ctx->transform(&vb2user);
385     }
387     sp_group_render(item, ctx);
388     ctx->popState();
391 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
393     SPRoot *root = SP_ROOT(item);
394     CairoRenderer *renderer = ctx->getRenderer();
396     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
397         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
399     ctx->pushState();
400     renderer->setStateForItem(ctx, item);
401     Geom::Matrix tempmat (root->c2p);
402     ctx->transform(&tempmat);
403     sp_group_render(item, ctx);
404     ctx->popState();
407 /**
408     This function converts the item to a raster image and includes the image into the cairo renderer.
409     It is only used for filters and then only when rendering filters as bitmaps is requested.
410 */
411 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
414     // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
416     // Calculate resolution
417     double res;
418     /** @TODO reimplement the resolution stuff   (WHY?)
419     */
420     res = ctx->getBitmapResolution();
421     if(res == 0) {
422         res = PX_PER_IN;
423     }
424     TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
426     // Get the bounding box of the selection in document coordinates.
427     Geom::OptRect bbox = 
428            item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
430     if (!bbox) // no bbox, e.g. empty group
431         return;
433     // The width and height of the bitmap in pixels
434     unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
435     unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
436     
437     // Scale to exactly fit integer bitmap inside bounding box
438     double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
439     double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
441     // Location of bounding box in document coordinates.
442     double shift_x = bbox->min()[Geom::X];
443     double shift_y = bbox->max()[Geom::Y];
445     // For default 90 dpi, snap bitmap to pixel grid
446     if (res == PX_PER_IN) { 
447         shift_x = round (shift_x);
448         shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
449                                      // Remove the negations when the inversion is gone.
450     }
452     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
454     // Matix to put bitmap in correct place on document
455     Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
456                                  (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
458     // ctx matrix already includes item transformation. We must substract.
459     Geom::Matrix t_item =  sp_item_i2d_affine (item);
460     Geom::Matrix t = t_on_document * t_item.inverse();
462     // Do the export
463     SPDocument *document = SP_OBJECT(item)->document;
464     GSList *items = NULL;
465     items = g_slist_append(items, item);
467     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
468         bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y], 
469         width, height, res, res, (guint32) 0xffffff00, items );
471     if (pb) {
472         TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
473         unsigned char *px = gdk_pixbuf_get_pixels (pb);
474         unsigned int w = gdk_pixbuf_get_width(pb);
475         unsigned int h = gdk_pixbuf_get_height(pb);
476         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
477         ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
478         gdk_pixbuf_unref (pb);
479     }
480     g_slist_free (items);
484 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
486     // Check item's visibility
487     if (item->isHidden()) {
488         return;
489     }
491     SPStyle* style = SP_OBJECT_STYLE (item);
492     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
493         return sp_asbitmap_render(item, ctx);
494     }
496     if (SP_IS_ROOT(item)) {
497         TRACE(("root\n"));
498         return sp_root_render(item, ctx);
499     } else if (SP_IS_GROUP(item)) {
500         TRACE(("group\n"));
501         return sp_group_render(item, ctx);
502     } else if (SP_IS_SHAPE(item)) {
503         TRACE(("shape\n"));
504         return sp_shape_render(item, ctx);
505     } else if (SP_IS_USE(item)) {
506         TRACE(("use begin---\n"));
507         sp_use_render(item, ctx);
508         TRACE(("---use end\n"));
509     } else if (SP_IS_SYMBOL(item)) {
510         TRACE(("symbol\n"));
511         return sp_symbol_render(item, ctx);
512     } else if (SP_IS_TEXT(item)) {
513         TRACE(("text\n"));
514         return sp_text_render(item, ctx);
515     } else if (SP_IS_FLOWTEXT(item)) {
516         TRACE(("flowtext\n"));
517         return sp_flowtext_render(item, ctx);
518     } else if (SP_IS_IMAGE(item)) {
519         TRACE(("image\n"));
520         return sp_image_render(item, ctx);
521     }
524 void
525 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
527     SPStyle const *style = SP_OBJECT_STYLE(item);
528     ctx->setStateForStyle(style);
530     CairoRenderState *state = ctx->getCurrentState();
531     state->clip_path = item->clip_ref->getObject();
532     state->mask = item->mask_ref->getObject();
533     state->item_transform = Geom::Matrix (item->transform);
535     // If parent_has_userspace is true the parent state's transform
536     // has to be used for the mask's/clippath's context.
537     // This is so because we use the image's/(flow)text's transform for positioning
538     // instead of explicitly specifying it and letting the renderer do the
539     // transformation before rendering the item.
540     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
541         state->parent_has_userspace = TRUE;
542     TRACE(("setStateForItem opacity: %f\n", state->opacity));
545 void
546 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
548     ctx->pushState();
549     setStateForItem(ctx, item);
551     CairoRenderState *state = ctx->getCurrentState();
552     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
554     // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
555     if (state->need_layer) {
556         state->merge_opacity = FALSE;
557         ctx->pushLayer();
558     }
559     Geom::Matrix tempmat (item->transform);
560     ctx->transform(&tempmat);
561     sp_item_invoke_render(item, ctx);
563     if (state->need_layer)
564         ctx->popLayer();
566     ctx->popState();
569 bool
570 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
572     g_assert( ctx != NULL );
574     if (!base)
575         base = SP_ITEM(sp_document_root(doc));
577     NRRect d;
578     if (pageBoundingBox) {
579         d.x0 = d.y0 = 0;
580         d.x1 = ceil(sp_document_width(doc));
581         d.y1 = ceil(sp_document_height(doc));
582     } else {
583         sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
584     }
586     if (ctx->_vector_based_target) {
587         // convert from px to pt
588         d.x0 *= PT_PER_PX;
589         d.x1 *= PT_PER_PX;
590         d.y0 *= PT_PER_PX;
591         d.y1 *= PT_PER_PX;
592     }
594     ctx->_width = d.x1-d.x0;
595     ctx->_height = d.y1-d.y0;
597     TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
599     bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
601     if (ret && !pageBoundingBox)
602     {
603         double high = sp_document_height(doc);
604         if (ctx->_vector_based_target)
605             high *= PT_PER_PX;
607         Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
608                                     (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
609         ctx->transform(&tp);
610     }
611     
612     return ret;
615 #include "macros.h" // SP_PRINT_*
617 // Apply an SVG clip path
618 void
619 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
621     g_assert( ctx != NULL && ctx->_is_valid );
623     if (cp == NULL)
624         return;
626     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
627     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
629     Geom::Matrix saved_ctm;
630     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
631         //SP_PRINT_DRECT("clipd", cp->display->bbox);
632         NRRect clip_bbox(cp->display->bbox);
633         Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
634         t[4] = clip_bbox.x0;
635         t[5] = clip_bbox.y0;
636         t *= ctx->getCurrentState()->transform;
637         ctx->getTransform(&saved_ctm);
638         ctx->setTransform(&t);
639     }
641     TRACE(("BEGIN clip\n"));
642     SPObject *co = SP_OBJECT(cp);
643     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
644         if (SP_IS_ITEM(child)) {
645             SPItem *item = SP_ITEM(child);
647             // combine transform of the item in clippath and the item using clippath:
648             Geom::Matrix tempmat (item->transform);
649             tempmat = tempmat * (ctx->getCurrentState()->item_transform);
651             // render this item in clippath
652             ctx->pushState();
653             ctx->transform(&tempmat);
654             setStateForItem(ctx, item);
655             sp_item_invoke_render(item, ctx);
656             ctx->popState();
657         }
658     }
659     TRACE(("END clip\n"));
661     // do clipping only if this was the first call to applyClipPath
662     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
663         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
664         cairo_clip(ctx->_cr);
666     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
667         ctx->setTransform(&saved_ctm);
669     ctx->setRenderMode(saved_mode);
672 // Apply an SVG mask
673 void
674 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
676     g_assert( ctx != NULL && ctx->_is_valid );
678     if (mask == NULL)
679         return;
681     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
682     NRRect mask_bbox(mask->display->bbox);
683     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
684     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
685         Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
686         t[4] = mask_bbox.x0;
687         t[5] = mask_bbox.y0;
688         t *= ctx->getCurrentState()->transform;
689         ctx->setTransform(&t);
690     }
692     // Clip mask contents... but...
693     // The mask's bounding box is the "geometric bounding box" which doesn't allow for
694     // filters which extend outside the bounding box. So don't clip.
695     // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
697     ctx->pushState();
699     TRACE(("BEGIN mask\n"));
700     SPObject *co = SP_OBJECT(mask);
701     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
702         if (SP_IS_ITEM(child)) {
703             SPItem *item = SP_ITEM(child);
704             renderItem(ctx, item);
705         }
706     }
707     TRACE(("END mask\n"));
709     ctx->popState();
712 void
713 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
714                              double *x, double *y, double *width, double *height)
716     if (aspect_align == SP_ASPECT_NONE)
717         return;
719     double scalex, scaley, scale;
720     double new_width, new_height;
721     scalex = *width / vp_width;
722     scaley = *height / vp_height;
723     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
724     new_width = vp_width * scale;
725     new_height = vp_height * scale;
726     /* Now place viewbox to requested position */
727     switch (aspect_align) {
728         case SP_ASPECT_XMIN_YMIN:
729             break;
730         case SP_ASPECT_XMID_YMIN:
731             *x -= 0.5 * (new_width - *width);
732             break;
733         case SP_ASPECT_XMAX_YMIN:
734             *x -= 1.0 * (new_width - *width);
735             break;
736         case SP_ASPECT_XMIN_YMID:
737             *y -= 0.5 * (new_height - *height);
738             break;
739         case SP_ASPECT_XMID_YMID:
740             *x -= 0.5 * (new_width - *width);
741             *y -= 0.5 * (new_height - *height);
742             break;
743         case SP_ASPECT_XMAX_YMID:
744             *x -= 1.0 * (new_width - *width);
745             *y -= 0.5 * (new_height - *height);
746             break;
747         case SP_ASPECT_XMIN_YMAX:
748             *y -= 1.0 * (new_height - *height);
749             break;
750         case SP_ASPECT_XMID_YMAX:
751             *x -= 0.5 * (new_width - *width);
752             *y -= 1.0 * (new_height - *height);
753             break;
754         case SP_ASPECT_XMAX_YMAX:
755             *x -= 1.0 * (new_width - *width);
756             *y -= 1.0 * (new_height - *height);
757             break;
758         default:
759             break;
760     }
761     *width = new_width;
762     *height = new_height;
765 #include "clear-n_.h"
767 }  /* namespace Internal */
768 }  /* namespace Extension */
769 }  /* namespace Inkscape */
771 #undef TRACE
773 /* End of GNU GPL code */
775 /*
776   Local Variables:
777   mode:c++
778   c-file-style:"stroustrup"
779   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
780   indent-tabs-mode:nil
781   fill-column:99
782   End:
783 */
784 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :