Code

Merging from trunk
[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(sp_shape_marker_get_transform_at_start(path_it->front()));
185             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
186                 tr = Geom::Scale(style->stroke_width.computed) * tr;
187             }
189             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
191             Geom::Matrix old_tr = marker_item->transform;
192             marker_item->transform = tr;
193             renderer->renderItem (ctx, marker_item);
194             marker_item->transform = old_tr;
195         }
197         if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
198             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
199             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
200             while (curve_it2 != path_it->end_default())
201             {
202                 /* Put marker between curve_it1 and curve_it2.
203                  * Loop to end_default (so including closing segment), because when a path is closed,
204                  * there should be a midpoint marker between last segment and closing straight line segment */
206                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
207                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
209                 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
211                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
212                     tr = Geom::Scale(style->stroke_width.computed) * tr;
213                 }
215                 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
217                 Geom::Matrix old_tr = marker_item->transform;
218                 marker_item->transform = tr;
219                 renderer->renderItem (ctx, marker_item);
220                 marker_item->transform = old_tr;
222                 ++curve_it1;
223                 ++curve_it2;
224             }
225         }
227         if ( shape->marker[SP_MARKER_LOC_END] ) {
228             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
229             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
231             /* Get reference to last curve in the path.
232              * For moveto-only path, this returns the "closing line segment". */
233             unsigned int index = path_it->size_default();
234             if (index > 0) {
235                 index--;
236             }
237             Geom::Curve const &lastcurve = (*path_it)[index];
239             Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
241             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
242                 tr = Geom::Scale(style->stroke_width.computed) * tr;
243             }
245             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
247             Geom::Matrix old_tr = marker_item->transform;
248             marker_item->transform = tr;
249             renderer->renderItem (ctx, marker_item);
250             marker_item->transform = old_tr;
251         }
252     }
255 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
257     SPGroup *group = SP_GROUP(item);
258     CairoRenderer *renderer = ctx->getRenderer();
259     TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
261     GSList *l = g_slist_reverse(group->childList(false));
262     while (l) {
263         SPObject *o = SP_OBJECT (l->data);
264         if (SP_IS_ITEM(o)) {
265             renderer->renderItem (ctx, SP_ITEM (o));
266         }
267         l = g_slist_remove (l, o);
268     }
271 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
273     bool translated = false;
274     SPUse *use = SP_USE(item);
275     CairoRenderer *renderer = ctx->getRenderer();
277     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
278         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
279         ctx->pushState();
280         ctx->transform(&tp);
281         translated = true;
282     }
284     if (use->child && SP_IS_ITEM(use->child)) {
285         renderer->renderItem(ctx, SP_ITEM(use->child));
286     }
288     if (translated) {
289         ctx->popState();
290     }
293 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
295     SPText *group = SP_TEXT (item);
296     group->layout.showGlyphs(ctx);
299 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
301     SPFlowtext *group = SP_FLOWTEXT(item);
302     group->layout.showGlyphs(ctx);
305 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
307     SPImage *image;
308     guchar *px;
309     int w, h, rs;
311     image = SP_IMAGE (item);
313     if (!image->pixbuf) return;
314     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
316     px = gdk_pixbuf_get_pixels (image->pixbuf);
317     w = gdk_pixbuf_get_width (image->pixbuf);
318     h = gdk_pixbuf_get_height (image->pixbuf);
319     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
321     double x = image->x.computed;
322     double y = image->y.computed;
323     double width = image->width.computed;
324     double height = image->height.computed;
326     if (image->aspect_align != SP_ASPECT_NONE) {
327         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
328                                                      &x, &y, &width, &height);
329     }
331     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
332         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
333     }
335     Geom::Translate tp(x, y);
336     Geom::Scale s(width / (double)w, height / (double)h);
337     Geom::Matrix t(s * tp);
339     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
342 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
344     SPSymbol *symbol = SP_SYMBOL(item);
345     if (!SP_OBJECT_IS_CLONED (symbol))
346         return;
348     /* Cloned <symbol> is actually renderable */
349     ctx->pushState();
350     ctx->transform(&symbol->c2p);
352     // apply viewbox if set
353     if (0 /*symbol->viewBox_set*/) {
354         Geom::Matrix vb2user;
355         double x, y, width, height;
356         double view_width, view_height;
357         x = 0.0;
358         y = 0.0;
359         width = 1.0;
360         height = 1.0;
362         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
363         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
365         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
366                                      &x, &y,&width, &height);
368         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
369         vb2user = Geom::identity();
370         vb2user[0] = width / view_width;
371         vb2user[3] = height / view_height;
372         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
373         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
375         ctx->transform(&vb2user);
376     }
378     sp_group_render(item, ctx);
379     ctx->popState();
382 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
384     SPRoot *root = SP_ROOT(item);
385     CairoRenderer *renderer = ctx->getRenderer();
387     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
388         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
390     ctx->pushState();
391     renderer->setStateForItem(ctx, item);
392     Geom::Matrix tempmat (root->c2p);
393     ctx->transform(&tempmat);
394     sp_group_render(item, ctx);
395     ctx->popState();
398 /**
399     This function converts the item to a raster image and includes the image into the cairo renderer.
400     It is only used for filters and then only when rendering filters as bitmaps is requested.
401 */
402 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
405     // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
407     // Calculate resolution
408     double res;
409     /** @TODO reimplement the resolution stuff   (WHY?)
410     */
411     res = ctx->getBitmapResolution();
412     if(res == 0) {
413         res = PX_PER_IN;
414     }
415     TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
417     // Get the bounding box of the selection in document coordinates.
418     NRRect bbox(item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX ));
420     // The width and height of the bitmap in pixels
421     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * (res / PX_PER_IN));
422     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * (res / PX_PER_IN));
423     
424     // Scale to exactly fit integer bitmap inside bounding box
425     double scale_x = (bbox.x1 - bbox.x0) / width;
426     double scale_y = (bbox.y1 - bbox.y0) / height;
428     // Location of bounding box in document coordinates.
429     double shift_x = bbox.x0;
430     double shift_y = bbox.y1;
432     // For default 90 dpi, snap bitmap to pixel grid
433     if (res == PX_PER_IN) { 
434         shift_x = round (shift_x);
435         shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
436                                      // Remove the negations when the inversion is gone.
437     }
439     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
441     // Matix to put bitmap in correct place on document
442     Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
443                                  (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
445     // ctx matrix already includes item transformation. We must substract.
446     Geom::Matrix t_item =  sp_item_i2d_affine (item);
447     Geom::Matrix t = t_on_document * t_item.inverse();
449     // Do the export
450     SPDocument *document = SP_OBJECT(item)->document;
451     GSList *items = NULL;
452     items = g_slist_append(items, item);
454     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
455         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
457     if (pb) {
458         TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
459         unsigned char *px = gdk_pixbuf_get_pixels (pb);
460         unsigned int w = gdk_pixbuf_get_width(pb);
461         unsigned int h = gdk_pixbuf_get_height(pb);
462         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
463         ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
464         gdk_pixbuf_unref (pb);
465     }
466     g_slist_free (items);
470 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
472     // Check item's visibility
473     if (item->isHidden()) {
474         return;
475     }
477     SPStyle* style = SP_OBJECT_STYLE (item);
478     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
479         return sp_asbitmap_render(item, ctx);
480     }
482     if (SP_IS_ROOT(item)) {
483         TRACE(("root\n"));
484         return sp_root_render(item, ctx);
485     } else if (SP_IS_GROUP(item)) {
486         TRACE(("group\n"));
487         return sp_group_render(item, ctx);
488     } else if (SP_IS_SHAPE(item)) {
489         TRACE(("shape\n"));
490         return sp_shape_render(item, ctx);
491     } else if (SP_IS_USE(item)) {
492         TRACE(("use begin---\n"));
493         sp_use_render(item, ctx);
494         TRACE(("---use end\n"));
495     } else if (SP_IS_SYMBOL(item)) {
496         TRACE(("symbol\n"));
497         return sp_symbol_render(item, ctx);
498     } else if (SP_IS_TEXT(item)) {
499         TRACE(("text\n"));
500         return sp_text_render(item, ctx);
501     } else if (SP_IS_FLOWTEXT(item)) {
502         TRACE(("flowtext\n"));
503         return sp_flowtext_render(item, ctx);
504     } else if (SP_IS_IMAGE(item)) {
505         TRACE(("image\n"));
506         return sp_image_render(item, ctx);
507     }
510 void
511 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
513     SPStyle const *style = SP_OBJECT_STYLE(item);
514     ctx->setStateForStyle(style);
516     CairoRenderState *state = ctx->getCurrentState();
517     state->clip_path = item->clip_ref->getObject();
518     state->mask = item->mask_ref->getObject();
519     state->item_transform = Geom::Matrix (item->transform);
521     // If parent_has_userspace is true the parent state's transform
522     // has to be used for the mask's/clippath's context.
523     // This is so because we use the image's/(flow)text's transform for positioning
524     // instead of explicitly specifying it and letting the renderer do the
525     // transformation before rendering the item.
526     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
527         state->parent_has_userspace = TRUE;
528     TRACE(("setStateForItem opacity: %f\n", state->opacity));
531 void
532 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
534     ctx->pushState();
535     setStateForItem(ctx, item);
537     CairoRenderState *state = ctx->getCurrentState();
538     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
540     // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
541     if (state->need_layer) {
542         state->merge_opacity = FALSE;
543         ctx->pushLayer();
544     }
545     Geom::Matrix tempmat (item->transform);
546     ctx->transform(&tempmat);
547     sp_item_invoke_render(item, ctx);
549     if (state->need_layer)
550         ctx->popLayer();
552     ctx->popState();
555 bool
556 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
558     g_assert( ctx != NULL );
560     if (ctx->_vector_based_target) {
561         // width and height in pt
562         ctx->_width = sp_document_width(doc) * PT_PER_PX;
563         ctx->_height = sp_document_height(doc) * PT_PER_PX;
564     } else {
565         ctx->_width = sp_document_width(doc);
566         ctx->_height = sp_document_height(doc);
567     }
569     NRRect d;
570     if (pageBoundingBox || !base) {
571         d.x0 = d.y0 = 0;
572         d.x1 = ceil(ctx->_width);
573         d.y1 = ceil(ctx->_height);
574     } else {
575         sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE);
576         if (ctx->_vector_based_target) {
577             // convert from px to pt
578             d.x0 *= PT_PER_PX;
579             d.x1 *= PT_PER_PX;
580             d.y0 *= PT_PER_PX;
581             d.y1 *= PT_PER_PX;
582         }
583     }
584     TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
586     bool ret = ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
588     if (ret && !pageBoundingBox && base)
589     {
590         Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
591                                     (d.y1 - ctx->_height) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
592         ctx->transform(&tp);
594         ctx->_width  = d.x1 - d.x0;
595         ctx->_height = d.y1 - d.y0;
596     }
597     
598     return ret;
601 #include "macros.h" // SP_PRINT_*
603 // Apply an SVG clip path
604 void
605 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
607     g_assert( ctx != NULL && ctx->_is_valid );
609     if (cp == NULL)
610         return;
612     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
613     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
615     Geom::Matrix saved_ctm;
616     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
617         //SP_PRINT_DRECT("clipd", cp->display->bbox);
618         NRRect clip_bbox(cp->display->bbox);
619         Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
620         t[4] = clip_bbox.x0;
621         t[5] = clip_bbox.y0;
622         t *= ctx->getCurrentState()->transform;
623         ctx->getTransform(&saved_ctm);
624         ctx->setTransform(&t);
625     }
627     TRACE(("BEGIN clip\n"));
628     SPObject *co = SP_OBJECT(cp);
629     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
630         if (SP_IS_ITEM(child)) {
631             SPItem *item = SP_ITEM(child);
633             // combine transform of the item in clippath and the item using clippath:
634             Geom::Matrix tempmat (item->transform);
635             tempmat = tempmat * (ctx->getCurrentState()->item_transform);
637             // render this item in clippath
638             ctx->pushState();
639             ctx->transform(&tempmat);
640             setStateForItem(ctx, item);
641             sp_item_invoke_render(item, ctx);
642             ctx->popState();
643         }
644     }
645     TRACE(("END clip\n"));
647     // do clipping only if this was the first call to applyClipPath
648     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
649         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
650         cairo_clip(ctx->_cr);
652     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
653         ctx->setTransform(&saved_ctm);
655     ctx->setRenderMode(saved_mode);
658 // Apply an SVG mask
659 void
660 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
662     g_assert( ctx != NULL && ctx->_is_valid );
664     if (mask == NULL)
665         return;
667     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
668     NRRect mask_bbox(mask->display->bbox);
669     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
670     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
671         Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
672         t[4] = mask_bbox.x0;
673         t[5] = mask_bbox.y0;
674         t *= ctx->getCurrentState()->transform;
675         ctx->setTransform(&t);
676     }
678     // Clip mask contents... but...
679     // The mask's bounding box is the "geometric bounding box" which doesn't allow for
680     // filters which extend outside the bounding box. So don't clip.
681     // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
683     ctx->pushState();
685     TRACE(("BEGIN mask\n"));
686     SPObject *co = SP_OBJECT(mask);
687     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
688         if (SP_IS_ITEM(child)) {
689             SPItem *item = SP_ITEM(child);
690             renderItem(ctx, item);
691         }
692     }
693     TRACE(("END mask\n"));
695     ctx->popState();
698 void
699 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
700                              double *x, double *y, double *width, double *height)
702     if (aspect_align == SP_ASPECT_NONE)
703         return;
705     double scalex, scaley, scale;
706     double new_width, new_height;
707     scalex = *width / vp_width;
708     scaley = *height / vp_height;
709     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
710     new_width = vp_width * scale;
711     new_height = vp_height * scale;
712     /* Now place viewbox to requested position */
713     switch (aspect_align) {
714         case SP_ASPECT_XMIN_YMIN:
715             break;
716         case SP_ASPECT_XMID_YMIN:
717             *x -= 0.5 * (new_width - *width);
718             break;
719         case SP_ASPECT_XMAX_YMIN:
720             *x -= 1.0 * (new_width - *width);
721             break;
722         case SP_ASPECT_XMIN_YMID:
723             *y -= 0.5 * (new_height - *height);
724             break;
725         case SP_ASPECT_XMID_YMID:
726             *x -= 0.5 * (new_width - *width);
727             *y -= 0.5 * (new_height - *height);
728             break;
729         case SP_ASPECT_XMAX_YMID:
730             *x -= 1.0 * (new_width - *width);
731             *y -= 0.5 * (new_height - *height);
732             break;
733         case SP_ASPECT_XMIN_YMAX:
734             *y -= 1.0 * (new_height - *height);
735             break;
736         case SP_ASPECT_XMID_YMAX:
737             *x -= 0.5 * (new_width - *width);
738             *y -= 1.0 * (new_height - *height);
739             break;
740         case SP_ASPECT_XMAX_YMAX:
741             *x -= 1.0 * (new_width - *width);
742             *y -= 1.0 * (new_height - *height);
743             break;
744         default:
745             break;
746     }
747     *width = new_width;
748     *height = new_height;
751 #include "clear-n_.h"
753 }  /* namespace Internal */
754 }  /* namespace Extension */
755 }  /* namespace Inkscape */
757 #undef TRACE
759 /* End of GNU GPL code */
761 /*
762   Local Variables:
763   mode:c++
764   c-file-style:"stroustrup"
765   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
766   indent-tabs-mode:nil
767   fill-column:99
768   End:
769 */
770 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :