Code

67f9354d8cac97085e57fc87b6f18d90cbc29df8
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
1 /** \file
2  * Rendering with Cairo.
3  */
4 /*
5  * Author:
6  *   Miklos Erdelyi <erdelyim@gmail.com>
7  *   Jon A. Cruz <jon@joncruz.org>
8  *   Abhishek Sharma
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 "libnrtype/Layout-TNG.h"
33 #include <2geom/transforms.h>
34 #include <2geom/pathvector.h>
36 #include <glib/gmem.h>
38 #include <glibmm/i18n.h>
39 #include "display/nr-arena.h"
40 #include "display/nr-arena-item.h"
41 #include "display/nr-arena-group.h"
42 #include "display/curve.h"
43 #include "display/canvas-bpath.h"
44 #include "sp-item.h"
45 #include "sp-item-group.h"
46 #include "style.h"
47 #include "marker.h"
48 #include "sp-linear-gradient.h"
49 #include "sp-radial-gradient.h"
50 #include "sp-root.h"
51 #include "sp-shape.h"
52 #include "sp-use.h"
53 #include "sp-text.h"
54 #include "sp-flowtext.h"
55 #include "sp-image.h"
56 #include "sp-symbol.h"
57 #include "sp-pattern.h"
58 #include "sp-mask.h"
59 #include "sp-clippath.h"
61 #include <unit-constants.h>
62 #include "helper/png-write.h"
63 #include "helper/pixbuf-ops.h"
65 #include "cairo-renderer.h"
66 #include "cairo-render-context.h"
67 #include "extension/system.h"
69 #include "io/sys.h"
71 #include <cairo.h>
73 // include support for only the compiled-in surface types
74 #ifdef CAIRO_HAS_PDF_SURFACE
75 #include <cairo-pdf.h>
76 #endif
77 #ifdef CAIRO_HAS_PS_SURFACE
78 #include <cairo-ps.h>
79 #endif
81 //#define TRACE(_args) g_printf _args
82 #define TRACE(_args)
83 //#define TEST(_args) _args
84 #define TEST(_args)
86 // FIXME: expose these from sp-clippath/mask.cpp
87 struct SPClipPathView {
88     SPClipPathView *next;
89     unsigned int key;
90     NRArenaItem *arenaitem;
91     NRRect bbox;
92 };
94 struct SPMaskView {
95     SPMaskView *next;
96     unsigned int key;
97     NRArenaItem *arenaitem;
98     NRRect bbox;
99 };
101 namespace Inkscape {
102 namespace Extension {
103 namespace Internal {
105 CairoRenderer::CairoRenderer(void)
106   : _omitText(false)
107 {}
109 CairoRenderer::~CairoRenderer(void)
111     /* restore default signal handling for SIGPIPE */
112 #if !defined(_WIN32) && !defined(__WIN32__)
113     (void) signal(SIGPIPE, SIG_DFL);
114 #endif
116     return;
119 CairoRenderContext*
120 CairoRenderer::createContext(void)
122     CairoRenderContext *new_context = new CairoRenderContext(this);
123     g_assert( new_context != NULL );
125     new_context->_state_stack = NULL;
126     new_context->_state = NULL;
128     // create initial render state
129     CairoRenderState *state = new_context->_createState();
130     state->transform = Geom::identity();
131     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
132     new_context->_state = state;
134     return new_context;
137 void
138 CairoRenderer::destroyContext(CairoRenderContext *ctx)
140     delete ctx;
143 /*
145 Here comes the rendering part which could be put into the 'render' methods of SPItems'
147 */
149 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
150 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
157 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
162     bool render = true;
163     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
164         if (style->stroke_width.computed > 1e-9) {
165             tr = Geom::Scale(style->stroke_width.computed) * tr;
166         } else {
167             render = false; // stroke width zero and marker is thus scaled down to zero, skip
168         }
169     }
171     if (render) {
172         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
173         tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
174         Geom::Matrix old_tr = marker_item->transform;
175         marker_item->transform = tr;
176         ctx->getRenderer()->renderItem (ctx, marker_item);
177         marker_item->transform = old_tr;
178     }
181 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
183     NRRect pbox;
185     SPShape *shape = SP_SHAPE(item);
187     if (!shape->curve) return;
189     item->invoke_bbox( &pbox, Geom::identity(), TRUE);
191     SPStyle* style = SP_OBJECT_STYLE (item);
193     Geom::PathVector const & pathv = shape->curve->get_pathvector();
194     if (pathv.empty()) return;
196     ctx->renderPathVector(pathv, style, &pbox);
198     // START marker
199     for (int i = 0; i < 2; i++) {  // SP_MARKER_LOC and SP_MARKER_LOC_START
200         if ( shape->marker[i] ) {
201             SPMarker* marker = SP_MARKER (shape->marker[i]);
202             Geom::Matrix tr;
203             if (marker->orient_auto) {
204                 tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
205             } else {
206                 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
207             }
208             sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
209         }
210     }
211     // MID marker
212     for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
213         if ( !shape->marker[i] ) continue;
214         SPMarker* marker = SP_MARKER (shape->marker[i]);
215         for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
216             // START position
217             if ( path_it != pathv.begin() 
218                  && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
219             {
220                 Geom::Matrix tr;
221                 if (marker->orient_auto) {
222                     tr = sp_shape_marker_get_transform_at_start(path_it->front());
223                 } else {
224                     tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
225                 }
226                 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
227             }
228             // MID position
229             if (path_it->size_default() > 1) {
230                 Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
231                 Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
232                 while (curve_it2 != path_it->end_default())
233                 {
234                     /* Put marker between curve_it1 and curve_it2.
235                      * Loop to end_default (so including closing segment), because when a path is closed,
236                      * there should be a midpoint marker between last segment and closing straight line segment */
237                     Geom::Matrix tr;
238                     if (marker->orient_auto) {
239                         tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
240                     } else {
241                         tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
242                     }
244                     sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
246                     ++curve_it1;
247                     ++curve_it2;
248                 }
249             }
250             // END position
251             if ( path_it != (pathv.end()-1) && !path_it->empty()) {
252                 Geom::Curve const &lastcurve = path_it->back_default();
253                 Geom::Matrix tr;
254                 if (marker->orient_auto) {
255                     tr = sp_shape_marker_get_transform_at_end(lastcurve);
256                 } else {
257                     tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
258                 }
259                 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
260             }
261         }
262     }
263     // END marker
264     for (int i = 0; i < 4; i += 3) {  // SP_MARKER_LOC and SP_MARKER_LOC_END
265         if ( shape->marker[i] ) {
266             SPMarker* marker = SP_MARKER (shape->marker[i]);
268             /* Get reference to last curve in the path.
269              * For moveto-only path, this returns the "closing line segment". */
270             Geom::Path const &path_last = pathv.back();
271             unsigned int index = path_last.size_default();
272             if (index > 0) {
273                 index--;
274             }
275             Geom::Curve const &lastcurve = path_last[index];
277             Geom::Matrix tr;
278             if (marker->orient_auto) {
279                 tr = sp_shape_marker_get_transform_at_end(lastcurve);
280             } else {
281                 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
282             }
284             sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
285         }
286     }
289 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
291     SPGroup *group = SP_GROUP(item);
292     CairoRenderer *renderer = ctx->getRenderer();
293     TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
295     GSList *l = g_slist_reverse(group->childList(false));
296     while (l) {
297         SPObject *o = SP_OBJECT (l->data);
298         if (SP_IS_ITEM(o)) {
299             renderer->renderItem (ctx, SP_ITEM (o));
300         }
301         l = g_slist_remove (l, o);
302     }
305 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
307     bool translated = false;
308     SPUse *use = SP_USE(item);
309     CairoRenderer *renderer = ctx->getRenderer();
311     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
312         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
313         ctx->pushState();
314         ctx->transform(&tp);
315         translated = true;
316     }
318     if (use->child && SP_IS_ITEM(use->child)) {
319         renderer->renderItem(ctx, SP_ITEM(use->child));
320     }
322     if (translated) {
323         ctx->popState();
324     }
327 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
329     SPText *group = SP_TEXT (item);
330     group->layout.showGlyphs(ctx);
333 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
335     SPFlowtext *group = SP_FLOWTEXT(item);
336     group->layout.showGlyphs(ctx);
339 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
341     SPImage *image;
342     guchar *px;
343     int w, h, rs;
345     image = SP_IMAGE (item);
347     if (!image->pixbuf) return;
348     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
350     px = gdk_pixbuf_get_pixels (image->pixbuf);
351     w = gdk_pixbuf_get_width (image->pixbuf);
352     h = gdk_pixbuf_get_height (image->pixbuf);
353     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
355     double x = image->x.computed;
356     double y = image->y.computed;
357     double width = image->width.computed;
358     double height = image->height.computed;
360     if (image->aspect_align != SP_ASPECT_NONE) {
361         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
362                                                      &x, &y, &width, &height);
363     }
365     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
366         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
367     }
369     Geom::Translate tp(x, y);
370     Geom::Scale s(width / (double)w, height / (double)h);
371     Geom::Matrix t(s * tp);
373     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
376 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
378     SPSymbol *symbol = SP_SYMBOL(item);
379     if (!SP_OBJECT_IS_CLONED (symbol))
380         return;
382     /* Cloned <symbol> is actually renderable */
383     ctx->pushState();
384     ctx->transform(&symbol->c2p);
386     // apply viewbox if set
387     if (0 /*symbol->viewBox_set*/) {
388         Geom::Matrix vb2user;
389         double x, y, width, height;
390         double view_width, view_height;
391         x = 0.0;
392         y = 0.0;
393         width = 1.0;
394         height = 1.0;
396         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
397         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
399         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
400                                      &x, &y,&width, &height);
402         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
403         vb2user = Geom::identity();
404         vb2user[0] = width / view_width;
405         vb2user[3] = height / view_height;
406         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
407         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
409         ctx->transform(&vb2user);
410     }
412     sp_group_render(item, ctx);
413     ctx->popState();
416 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
418     SPRoot *root = SP_ROOT(item);
419     CairoRenderer *renderer = ctx->getRenderer();
421     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
422         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
424     ctx->pushState();
425     renderer->setStateForItem(ctx, item);
426     Geom::Matrix tempmat (root->c2p);
427     ctx->transform(&tempmat);
428     sp_group_render(item, ctx);
429     ctx->popState();
432 /**
433     This function converts the item to a raster image and includes the image into the cairo renderer.
434     It is only used for filters and then only when rendering filters as bitmaps is requested.
435 */
436 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
439     // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
441     // Calculate resolution
442     double res;
443     /** @TODO reimplement the resolution stuff   (WHY?)
444     */
445     res = ctx->getBitmapResolution();
446     if(res == 0) {
447         res = PX_PER_IN;
448     }
449     TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
451     // Get the bounding box of the selection in document coordinates.
452     Geom::OptRect bbox = 
453            item->getBounds(item->i2d_affine(), SPItem::RENDERING_BBOX);
455     if (!bbox) // no bbox, e.g. empty group
456         return;
458     // The width and height of the bitmap in pixels
459     unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
460     unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
461     
462     // Scale to exactly fit integer bitmap inside bounding box
463     double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
464     double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
466     // Location of bounding box in document coordinates.
467     double shift_x = bbox->min()[Geom::X];
468     double shift_y = bbox->max()[Geom::Y];
470     // For default 90 dpi, snap bitmap to pixel grid
471     if (res == PX_PER_IN) { 
472         shift_x = round (shift_x);
473         shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
474                                      // Remove the negations when the inversion is gone.
475     }
477     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
479     // Matix to put bitmap in correct place on document
480     Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
481                                  (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
483     // ctx matrix already includes item transformation. We must substract.
484     Geom::Matrix t_item =  item->i2d_affine ();
485     Geom::Matrix t = t_on_document * t_item.inverse();
487     // Do the export
488     SPDocument *document = SP_OBJECT(item)->document;
489     GSList *items = NULL;
490     items = g_slist_append(items, item);
492     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
493         bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y], 
494         width, height, res, res, (guint32) 0xffffff00, items );
496     if (pb) {
497         TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
498         unsigned char *px = gdk_pixbuf_get_pixels (pb);
499         unsigned int w = gdk_pixbuf_get_width(pb);
500         unsigned int h = gdk_pixbuf_get_height(pb);
501         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
502         ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
503         gdk_pixbuf_unref (pb);
504     }
505     g_slist_free (items);
509 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
511     // Check item's visibility
512     if (item->isHidden()) {
513         return;
514     }
516     SPStyle* style = SP_OBJECT_STYLE (item);
517     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
518         return sp_asbitmap_render(item, ctx);
519     }
521     if (SP_IS_ROOT(item)) {
522         TRACE(("root\n"));
523         return sp_root_render(item, ctx);
524     } else if (SP_IS_GROUP(item)) {
525         TRACE(("group\n"));
526         return sp_group_render(item, ctx);
527     } else if (SP_IS_SHAPE(item)) {
528         TRACE(("shape\n"));
529         return sp_shape_render(item, ctx);
530     } else if (SP_IS_USE(item)) {
531         TRACE(("use begin---\n"));
532         sp_use_render(item, ctx);
533         TRACE(("---use end\n"));
534     } else if (SP_IS_SYMBOL(item)) {
535         TRACE(("symbol\n"));
536         return sp_symbol_render(item, ctx);
537     } else if (SP_IS_TEXT(item)) {
538         TRACE(("text\n"));
539         return sp_text_render(item, ctx);
540     } else if (SP_IS_FLOWTEXT(item)) {
541         TRACE(("flowtext\n"));
542         return sp_flowtext_render(item, ctx);
543     } else if (SP_IS_IMAGE(item)) {
544         TRACE(("image\n"));
545         return sp_image_render(item, ctx);
546     }
549 void
550 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
552     SPStyle const *style = SP_OBJECT_STYLE(item);
553     ctx->setStateForStyle(style);
555     CairoRenderState *state = ctx->getCurrentState();
556     state->clip_path = item->clip_ref->getObject();
557     state->mask = item->mask_ref->getObject();
558     state->item_transform = Geom::Matrix (item->transform);
560     // If parent_has_userspace is true the parent state's transform
561     // has to be used for the mask's/clippath's context.
562     // This is so because we use the image's/(flow)text's transform for positioning
563     // instead of explicitly specifying it and letting the renderer do the
564     // transformation before rendering the item.
565     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
566         state->parent_has_userspace = TRUE;
567     TRACE(("setStateForItem opacity: %f\n", state->opacity));
570 void
571 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
573     if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
574         // skip text if _omitText is true
575         return;
576     }
578     ctx->pushState();
579     setStateForItem(ctx, item);
581     CairoRenderState *state = ctx->getCurrentState();
582     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
584     // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
585     if (state->need_layer) {
586         state->merge_opacity = FALSE;
587         ctx->pushLayer();
588     }
589     Geom::Matrix tempmat (item->transform);
590     ctx->transform(&tempmat);
591     sp_item_invoke_render(item, ctx);
593     if (state->need_layer)
594         ctx->popLayer();
596     ctx->popState();
599 bool
600 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
602 // PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
604     g_assert( ctx != NULL );
606     if (!base) {
607         base = SP_ITEM(doc->getRoot());
608     }
610     NRRect d;
611     if (pageBoundingBox) {
612         d.x0 = d.y0 = 0;
613         d.x1 = doc->getWidth();
614         d.y1 = doc->getHeight();
615     } else {
616         base->invoke_bbox( &d, base->i2d_affine(), TRUE, SPItem::RENDERING_BBOX);
617     }
619     if (ctx->_vector_based_target) {
620         // convert from px to pt
621         d.x0 *= PT_PER_PX;
622         d.x1 *= PT_PER_PX;
623         d.y0 *= PT_PER_PX;
624         d.y1 *= PT_PER_PX;
625     }
627     ctx->_width = d.x1-d.x0;
628     ctx->_height = d.y1-d.y0;
630     TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
632     bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
634     if (ret && !pageBoundingBox)
635     {
636         double high = doc->getHeight();
637         if (ctx->_vector_based_target)
638             high *= PT_PER_PX;
640         Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
641                                     (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
642         ctx->transform(&tp);
643     }
644     
645     return ret;
648 #include "macros.h" // SP_PRINT_*
650 // Apply an SVG clip path
651 void
652 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
654     g_assert( ctx != NULL && ctx->_is_valid );
656     if (cp == NULL)
657         return;
659     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
660     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
662     Geom::Matrix saved_ctm;
663     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
664         //SP_PRINT_DRECT("clipd", cp->display->bbox);
665         NRRect clip_bbox(cp->display->bbox);
666         Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
667         t[4] = clip_bbox.x0;
668         t[5] = clip_bbox.y0;
669         t *= ctx->getCurrentState()->transform;
670         ctx->getTransform(&saved_ctm);
671         ctx->setTransform(&t);
672     }
674     TRACE(("BEGIN clip\n"));
675     SPObject *co = SP_OBJECT(cp);
676     for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
677         if (SP_IS_ITEM(child)) {
678             SPItem *item = SP_ITEM(child);
680             // combine transform of the item in clippath and the item using clippath:
681             Geom::Matrix tempmat (item->transform);
682             tempmat = tempmat * (ctx->getCurrentState()->item_transform);
684             // render this item in clippath
685             ctx->pushState();
686             ctx->transform(&tempmat);
687             setStateForItem(ctx, item);
688             sp_item_invoke_render(item, ctx);
689             ctx->popState();
690         }
691     }
692     TRACE(("END clip\n"));
694     // do clipping only if this was the first call to applyClipPath
695     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
696         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
697         cairo_clip(ctx->_cr);
699     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
700         ctx->setTransform(&saved_ctm);
702     ctx->setRenderMode(saved_mode);
705 // Apply an SVG mask
706 void
707 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
709     g_assert( ctx != NULL && ctx->_is_valid );
711     if (mask == NULL)
712         return;
714     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
715     NRRect mask_bbox(mask->display->bbox);
716     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
717     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
718         Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
719         t[4] = mask_bbox.x0;
720         t[5] = mask_bbox.y0;
721         t *= ctx->getCurrentState()->transform;
722         ctx->setTransform(&t);
723     }
725     // Clip mask contents... but...
726     // The mask's bounding box is the "geometric bounding box" which doesn't allow for
727     // filters which extend outside the bounding box. So don't clip.
728     // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
730     ctx->pushState();
732     TRACE(("BEGIN mask\n"));
733     SPObject *co = SP_OBJECT(mask);
734     for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
735         if (SP_IS_ITEM(child)) {
736             SPItem *item = SP_ITEM(child);
737             renderItem(ctx, item);
738         }
739     }
740     TRACE(("END mask\n"));
742     ctx->popState();
745 void
746 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
747                              double *x, double *y, double *width, double *height)
749     if (aspect_align == SP_ASPECT_NONE)
750         return;
752     double scalex, scaley, scale;
753     double new_width, new_height;
754     scalex = *width / vp_width;
755     scaley = *height / vp_height;
756     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
757     new_width = vp_width * scale;
758     new_height = vp_height * scale;
759     /* Now place viewbox to requested position */
760     switch (aspect_align) {
761         case SP_ASPECT_XMIN_YMIN:
762             break;
763         case SP_ASPECT_XMID_YMIN:
764             *x -= 0.5 * (new_width - *width);
765             break;
766         case SP_ASPECT_XMAX_YMIN:
767             *x -= 1.0 * (new_width - *width);
768             break;
769         case SP_ASPECT_XMIN_YMID:
770             *y -= 0.5 * (new_height - *height);
771             break;
772         case SP_ASPECT_XMID_YMID:
773             *x -= 0.5 * (new_width - *width);
774             *y -= 0.5 * (new_height - *height);
775             break;
776         case SP_ASPECT_XMAX_YMID:
777             *x -= 1.0 * (new_width - *width);
778             *y -= 0.5 * (new_height - *height);
779             break;
780         case SP_ASPECT_XMIN_YMAX:
781             *y -= 1.0 * (new_height - *height);
782             break;
783         case SP_ASPECT_XMID_YMAX:
784             *x -= 0.5 * (new_width - *width);
785             *y -= 1.0 * (new_height - *height);
786             break;
787         case SP_ASPECT_XMAX_YMAX:
788             *x -= 1.0 * (new_width - *width);
789             *y -= 1.0 * (new_height - *height);
790             break;
791         default:
792             break;
793     }
794     *width = new_width;
795     *height = new_height;
798 #include "clear-n_.h"
800 }  /* namespace Internal */
801 }  /* namespace Extension */
802 }  /* namespace Inkscape */
804 #undef TRACE
806 /* End of GNU GPL code */
808 /*
809   Local Variables:
810   mode:c++
811   c-file-style:"stroustrup"
812   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
813   indent-tabs-mode:nil
814   fill-column:99
815   End:
816 */
817 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :