Code

eb19c3cbd67ab1da98564c5e77a93e8860fb27cc
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
1 #define __SP_CAIRO_RENDERER_C__
3 /** \file
4  * Rendering with Cairo.
5  */
6 /*
7  * Author:
8  *   Miklos Erdelyi <erdelyim@gmail.com>
9  *
10  * Copyright (C) 2006 Miklos Erdelyi
11  *
12  * Licensed under GNU GPL
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #ifndef PANGO_ENABLE_BACKEND
20 #define PANGO_ENABLE_BACKEND
21 #endif
23 #ifndef PANGO_ENABLE_ENGINE
24 #define PANGO_ENABLE_ENGINE
25 #endif
28 #include <signal.h>
29 #include <errno.h>
31 #include <libnr/n-art-bpath.h>
32 #include <libnr/nr-matrix-ops.h>
33 #include <libnr/nr-matrix-fns.h>
34 #include <libnr/nr-matrix-translate-ops.h>
35 #include <libnr/nr-scale-matrix-ops.h>
37 #include "libnr/nr-matrix-rotate-ops.h"
38 #include "libnr/nr-matrix-translate-ops.h"
39 #include "libnr/nr-rotate-fns.h"
40 #include "libnr/nr-scale-ops.h"
41 #include "libnr/nr-scale-translate-ops.h"
42 #include "libnr/nr-translate-matrix-ops.h"
43 #include "libnr/nr-translate-scale-ops.h"
44 #include "libnr/nr-convert2geom.h"
45 #include <2geom/transforms.h>
46 #include <2geom/pathvector.h>
48 #include <glib/gmem.h>
50 #include <glibmm/i18n.h>
51 #include "display/nr-arena.h"
52 #include "display/nr-arena-item.h"
53 #include "display/nr-arena-group.h"
54 #include "display/curve.h"
55 #include "display/canvas-bpath.h"
56 #include "sp-item.h"
57 #include "sp-item-group.h"
58 #include "style.h"
59 #include "marker.h"
60 #include "sp-linear-gradient.h"
61 #include "sp-radial-gradient.h"
62 #include "sp-root.h"
63 #include "sp-shape.h"
64 #include "sp-use.h"
65 #include "sp-text.h"
66 #include "sp-flowtext.h"
67 #include "sp-image.h"
68 #include "sp-symbol.h"
69 #include "sp-pattern.h"
70 #include "sp-mask.h"
71 #include "sp-clippath.h"
73 #include <unit-constants.h>
74 #include "helper/png-write.h"
75 #include "helper/pixbuf-ops.h"
77 #include "cairo-renderer.h"
78 #include "cairo-render-context.h"
79 #include "extension/system.h"
81 #include "io/sys.h"
83 #include <cairo.h>
85 // include support for only the compiled-in surface types
86 #ifdef CAIRO_HAS_PDF_SURFACE
87 #include <cairo-pdf.h>
88 #endif
89 #ifdef CAIRO_HAS_PS_SURFACE
90 #include <cairo-ps.h>
91 #endif
93 //#define TRACE(_args) g_printf _args
94 #define TRACE(_args)
96 // FIXME: expose these from sp-clippath/mask.cpp
97 struct SPClipPathView {
98     SPClipPathView *next;
99     unsigned int key;
100     NRArenaItem *arenaitem;
101     NRRect bbox;
102 };
104 struct SPMaskView {
105     SPMaskView *next;
106     unsigned int key;
107     NRArenaItem *arenaitem;
108     NRRect bbox;
109 };
111 namespace Inkscape {
112 namespace Extension {
113 namespace Internal {
115 CairoRenderer::CairoRenderer(void)
116 {}
118 CairoRenderer::~CairoRenderer(void)
120     /* restore default signal handling for SIGPIPE */
121 #if !defined(_WIN32) && !defined(__WIN32__)
122     (void) signal(SIGPIPE, SIG_DFL);
123 #endif
125     return;
128 CairoRenderContext*
129 CairoRenderer::createContext(void)
131     CairoRenderContext *new_context = new CairoRenderContext(this);
132     g_assert( new_context != NULL );
134     new_context->_state_stack = NULL;
135     new_context->_state = NULL;
137     // create initial render state
138     CairoRenderState *state = new_context->_createState();
139     state->transform.set_identity();
140     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
141     new_context->_state = state;
143     return new_context;
146 void
147 CairoRenderer::destroyContext(CairoRenderContext *ctx)
149     delete ctx;
152 /*
154 Here comes the rendering part which could be put into the 'render' methods of SPItems'
156 */
158 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
159 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
167 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
169 /* TODO FIXME: this does not render painting-marker-01-f.svg of SVG1.1 Test suite correctly. (orientation of one of the markers middle left ) */
170 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
172     NRRect pbox;
174     SPShape *shape = SP_SHAPE(item);
176     if (!shape->curve) return;
178     /* fixme: Think (Lauris) */
179     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
181     SPStyle* style = SP_OBJECT_STYLE (item);
182     CairoRenderer *renderer = ctx->getRenderer();
184     const_NRBPath bp;
185     bp.path = SP_CURVE_BPATH(shape->curve);
187     ctx->renderPath(&bp, style, &pbox);
189     Geom::PathVector const & pathv = shape->curve->get_pathvector();
190     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
191         if ( shape->marker[SP_MARKER_LOC_START] ) {
192             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
193             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
195             Geom::Point p = path_it->front().pointAt(0);
196             Geom::Point tang = path_it->front().unitTangentAt(0);
197             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
199             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
200                 tr = NR::scale(style->stroke_width.computed) * tr;
201             }
203             tr = marker_item->transform * marker->c2p * tr;
205             NR::Matrix old_tr = marker_item->transform;
206             marker_item->transform = tr;
207             renderer->renderItem (ctx, marker_item);
208             marker_item->transform = old_tr;
209         }
211         if ( shape->marker[SP_MARKER_LOC_MID] ) {
212             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
213             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
214             while (curve_it2 != path_it->end_default())
215             {
216                 /* Put marker between curve_it1 and curve_it2.
217                  * Loop to end_default (so including closing segment), because when a path is closed,
218                  * there should be a midpoint marker between last segment and closing straight line segment */
220                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
221                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
223                 Geom::Point p = curve_it1->pointAt(1);
224                 Geom::Point tang1 = curve_it1->unitTangentAt(1);
225                 Geom::Point tang2 = curve_it2->unitTangentAt(0);
226                 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang1, tang2)));
228                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
229                     tr = NR::scale(style->stroke_width.computed) * tr;
230                 }
232                 tr = marker_item->transform * marker->c2p * tr;
234                 NR::Matrix old_tr = marker_item->transform;
235                 marker_item->transform = tr;
236                 renderer->renderItem (ctx, marker_item);
237                 marker_item->transform = old_tr;
239                 ++curve_it1;
240                 ++curve_it2;
241             }
242         }
244         if ( shape->marker[SP_MARKER_LOC_END] ) {
245             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
246             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
248             Geom::Point p = path_it->back_default().pointAt(1);
249             Geom::Point tang = path_it->back_default().unitTangentAt(1);
250             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
252             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
253                 tr = NR::scale(style->stroke_width.computed) * tr;
254             }
256             tr = marker_item->transform * marker->c2p * tr;
258             NR::Matrix old_tr = marker_item->transform;
259             marker_item->transform = tr;
260             renderer->renderItem (ctx, marker_item);
261             marker_item->transform = old_tr;
262         }
263     }
266 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
268     SPGroup *group = SP_GROUP(item);
269     CairoRenderer *renderer = ctx->getRenderer();
270     TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
272     GSList *l = g_slist_reverse(group->childList(false));
273     while (l) {
274         SPObject *o = SP_OBJECT (l->data);
275         if (SP_IS_ITEM(o)) {
276             renderer->renderItem (ctx, SP_ITEM (o));
277         }
278         l = g_slist_remove (l, o);
279     }
282 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
284     bool translated = false;
285     SPUse *use = SP_USE(item);
286     CairoRenderer *renderer = ctx->getRenderer();
288     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
289         NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
290         ctx->pushState();
291         ctx->transform(&tp);
292         translated = true;
293     }
295     if (use->child && SP_IS_ITEM(use->child)) {
296         renderer->renderItem(ctx, SP_ITEM(use->child));
297     }
299     if (translated) {
300         ctx->popState();
301     }
304 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
306     SPText *group = SP_TEXT (item);
307     group->layout.showGlyphs(ctx);
310 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
312     SPFlowtext *group = SP_FLOWTEXT(item);
313     group->layout.showGlyphs(ctx);
316 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
318     SPImage *image;
319     guchar *px;
320     int w, h, rs;
322     image = SP_IMAGE (item);
324     if (!image->pixbuf) return;
325     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
327     px = gdk_pixbuf_get_pixels (image->pixbuf);
328     w = gdk_pixbuf_get_width (image->pixbuf);
329     h = gdk_pixbuf_get_height (image->pixbuf);
330     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
332     double x = image->x.computed;
333     double y = image->y.computed;
334     double width = image->width.computed;
335     double height = image->height.computed;
337     if (image->aspect_align != SP_ASPECT_NONE) {
338         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
339                                                      &x, &y, &width, &height);
340     }
342     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
343         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
344     }
346     NR::translate tp(x, y);
347     NR::scale s(width / (double)w, height / (double)h);
348     NR::Matrix t(s * tp);
350     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
353 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
355     SPSymbol *symbol = SP_SYMBOL(item);
356     if (!SP_OBJECT_IS_CLONED (symbol))
357         return;
359     /* Cloned <symbol> is actually renderable */
360     ctx->pushState();
361     ctx->transform(&symbol->c2p);
363     // apply viewbox if set
364     if (0 /*symbol->viewBox_set*/) {
365         NR::Matrix vb2user;
366         double x, y, width, height;
367         double view_width, view_height;
368         x = 0.0;
369         y = 0.0;
370         width = 1.0;
371         height = 1.0;
373         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
374         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
376         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
377                                      &x, &y,&width, &height);
379         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
380         vb2user.set_identity();
381         vb2user[0] = width / view_width;
382         vb2user[3] = height / view_height;
383         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
384         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
386         ctx->transform(&vb2user);
387     }
389     sp_group_render(item, ctx);
390     ctx->popState();
393 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
395     SPRoot *root = SP_ROOT(item);
396     CairoRenderer *renderer = ctx->getRenderer();
398     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
399         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
401     ctx->pushState();
402     renderer->setStateForItem(ctx, item);
403     ctx->transform(&root->c2p);
404     sp_group_render(item, ctx);
405     ctx->popState();
408 /**
409     this function convert the item to a raster image and include the raster into the cairo renderer
410 */
411 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
413     g_warning("render as bitmap");
415     //the code now was copied from sp_selection_create_bitmap_copy
417     SPDocument *document = SP_OBJECT(item)->document;
418     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
420     // Get the bounding box of the selection
421     //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
422     // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
423     NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
426     // List of the items to show; all others will be hidden
427     GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
428     items = g_slist_append(items, item);
430     // Remember parent and z-order of the topmost one
431     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
432     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
433     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
435     // Calculate resolution
436     double res;
437     /** @TODO reimplement the resolution stuff
438     */
439     res = ctx->getBitmapResolution();
440     if(res == 0) {
441         res = PX_PER_IN;
442     }
445     // The width and height of the bitmap in pixels
446     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
447     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
449     // Find out if we have to run a filter
450     gchar const *run = NULL;
451     gchar const *filter = NULL;
452     /** @TODO reimplement the filter stuff
453     //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
454     if (filter) {
455         // filter command is given;
456         // see if we have a parameter to pass to it
457         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
458         if (param1) {
459             if (param1[strlen(param1) - 1] == '%') {
460                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
461                 gchar p1[256];
462                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
463                 // the first param is always the image filename, the second is param1
464                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
465             } else {
466                 // otherwise pass the param1 unchanged
467                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
468             }
469         } else {
470             // run without extra parameter
471             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
472         }
473     }
474     */
475     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
476     NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
477     NR::Matrix t;
479     double shift_x = bbox.x0;
480     double shift_y = bbox.y1;
481     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
482         shift_x = round (shift_x);
483         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
484     }
485     t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
487     //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
489     // Do the export
490     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
491         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
493     // Run filter, if any
494     /*
495     if (run) {
496         g_print ("Running external filter: %s\n", run);
497         system (run);
498     }
499     */
500     if (pb) {
501         unsigned char *px = gdk_pixbuf_get_pixels (pb);
502         unsigned int w = gdk_pixbuf_get_width(pb);
503         unsigned int h = gdk_pixbuf_get_height(pb);
504         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
505         NR::Matrix matrix;
506         matrix = t;
507         //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
508         //matrix.set_identity();
510         ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
511     /*
512         // Create the repr for the image
513         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
514         repr->setAttribute("xlink:href", filename);
515         repr->setAttribute("sodipodi:absref", filepath);
516         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
517             sp_repr_set_svg_double(repr, "width", width);
518             sp_repr_set_svg_double(repr, "height", height);
519         } else {
520             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
521             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
522         }
524         // Write transform
525         gchar *c=sp_svg_transform_write(t);
526         repr->setAttribute("transform", c);
527         g_free(c);
529         // add the new repr to the parent
530         parent->appendChild(repr);
532         // move to the saved position
533         repr->setPosition(pos > 0 ? pos + 1 : 1);
534     */
535         gdk_pixbuf_unref (pb);
536     }
537     g_slist_free (items);
540 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
542     // Check item's visibility
543     if (item->isHidden()) {
544         return;
545     }
547     SPStyle* style = SP_OBJECT_STYLE (item);
548     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
549         return sp_asbitmap_render(item, ctx);
550     }
552     if (SP_IS_ROOT(item)) {
553         TRACE(("root\n"));
554         return sp_root_render(item, ctx);
555     } else if (SP_IS_GROUP(item)) {
556         TRACE(("group\n"));
557         return sp_group_render(item, ctx);
558     } else if (SP_IS_SHAPE(item)) {
559         TRACE(("shape\n"));
560         return sp_shape_render(item, ctx);
561     } else if (SP_IS_USE(item)) {
562         TRACE(("use begin---\n"));
563         sp_use_render(item, ctx);
564         TRACE(("---use end\n"));
565     } else if (SP_IS_SYMBOL(item)) {
566         TRACE(("symbol\n"));
567         return sp_symbol_render(item, ctx);
568     } else if (SP_IS_TEXT(item)) {
569         TRACE(("text\n"));
570         return sp_text_render(item, ctx);
571     } else if (SP_IS_FLOWTEXT(item)) {
572         TRACE(("flowtext\n"));
573         return sp_flowtext_render(item, ctx);
574     } else if (SP_IS_IMAGE(item)) {
575         TRACE(("image\n"));
576         return sp_image_render(item, ctx);
577     }
580 void
581 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
583     SPStyle const *style = SP_OBJECT_STYLE(item);
584     ctx->setStateForStyle(style);
586     CairoRenderState *state = ctx->getCurrentState();
587     state->clip_path = item->clip_ref->getObject();
588     state->mask = item->mask_ref->getObject();
590     // If parent_has_userspace is true the parent state's transform
591     // has to be used for the mask's/clippath's context.
592     // This is so because we use the image's/(flow)text's transform for positioning
593     // instead of explicitly specifying it and letting the renderer do the
594     // transformation before rendering the item.
595     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
596         state->parent_has_userspace = TRUE;
597     TRACE(("set op: %f\n", state->opacity));
600 void
601 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
603     ctx->pushState();
604     setStateForItem(ctx, item);
606     CairoRenderState *state = ctx->getCurrentState();
607     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
609     if (state->need_layer) {
610         state->merge_opacity = FALSE;
611         ctx->pushLayer();
612     }
613     ctx->transform(&item->transform);
614     sp_item_invoke_render(item, ctx);
616     if (state->need_layer)
617         ctx->popLayer();
619     ctx->popState();
622 bool
623 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
625     g_assert( ctx != NULL );
627     if (ctx->_vector_based_target) {
628         // width and height in pt
629         ctx->_width = sp_document_width(doc) * PT_PER_PX;
630         ctx->_height = sp_document_height(doc) * PT_PER_PX;
631     } else {
632         ctx->_width = sp_document_width(doc);
633         ctx->_height = sp_document_height(doc);
634     }
636     NRRect d;
637     bool pageBoundingBox = true;
638     if (pageBoundingBox) {
639         d.x0 = d.y0 = 0;
640         d.x1 = ceil(ctx->_width);
641         d.y1 = ceil(ctx->_height);
642     } else {
643         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
644         sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
645         if (ctx->_vector_based_target) {
646             // convert from px to pt
647             d.x0 *= PT_PER_PX;
648             d.x1 *= PT_PER_PX;
649             d.y0 *= PT_PER_PX;
650             d.y1 *= PT_PER_PX;
651         }
652     }
653     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
655     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
658 #include "macros.h" // SP_PRINT_*
660 void
661 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
663     g_assert( ctx != NULL && ctx->_is_valid );
665     if (cp == NULL)
666         return;
668     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
669     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
671     NR::Matrix saved_ctm;
672     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
673         //SP_PRINT_DRECT("clipd", cp->display->bbox);
674         NRRect clip_bbox(cp->display->bbox);
675         NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
676         t[4] = clip_bbox.x0;
677         t[5] = clip_bbox.y0;
678         t *= ctx->getCurrentState()->transform;
679         ctx->getTransform(&saved_ctm);
680         ctx->setTransform(&t);
681     }
683     TRACE(("BEGIN clip\n"));
684     SPObject *co = SP_OBJECT(cp);
685     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
686         if (SP_IS_ITEM(child)) {
687             SPItem *item = SP_ITEM(child);
688             renderItem(ctx, item);
689         }
690     }
691     TRACE(("END clip\n"));
693     // do clipping only if this was the first call to applyClipPath
694     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
695         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
696         cairo_clip(ctx->_cr);
698     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
699         ctx->setTransform(&saved_ctm);
701     ctx->setRenderMode(saved_mode);
704 void
705 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
707     g_assert( ctx != NULL && ctx->_is_valid );
709     if (mask == NULL)
710         return;
712     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
713     NRRect mask_bbox(mask->display->bbox);
714     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
715     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
716         NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
717         t[4] = mask_bbox.x0;
718         t[5] = mask_bbox.y0;
719         t *= ctx->getCurrentState()->transform;
720         ctx->setTransform(&t);
721     }
723     // clip mask contents
724     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
726     ctx->pushState();
728     TRACE(("BEGIN mask\n"));
729     SPObject *co = SP_OBJECT(mask);
730     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
731         if (SP_IS_ITEM(child)) {
732             SPItem *item = SP_ITEM(child);
733             renderItem(ctx, item);
734         }
735     }
736     TRACE(("END mask\n"));
738     ctx->popState();
741 void
742 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
743                              double *x, double *y, double *width, double *height)
745     if (aspect_align == SP_ASPECT_NONE)
746         return;
748     double scalex, scaley, scale;
749     double new_width, new_height;
750     scalex = *width / vp_width;
751     scaley = *height / vp_height;
752     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
753     new_width = vp_width * scale;
754     new_height = vp_height * scale;
755     /* Now place viewbox to requested position */
756     switch (aspect_align) {
757         case SP_ASPECT_XMIN_YMIN:
758             break;
759         case SP_ASPECT_XMID_YMIN:
760             *x -= 0.5 * (new_width - *width);
761             break;
762         case SP_ASPECT_XMAX_YMIN:
763             *x -= 1.0 * (new_width - *width);
764             break;
765         case SP_ASPECT_XMIN_YMID:
766             *y -= 0.5 * (new_height - *height);
767             break;
768         case SP_ASPECT_XMID_YMID:
769             *x -= 0.5 * (new_width - *width);
770             *y -= 0.5 * (new_height - *height);
771             break;
772         case SP_ASPECT_XMAX_YMID:
773             *x -= 1.0 * (new_width - *width);
774             *y -= 0.5 * (new_height - *height);
775             break;
776         case SP_ASPECT_XMIN_YMAX:
777             *y -= 1.0 * (new_height - *height);
778             break;
779         case SP_ASPECT_XMID_YMAX:
780             *x -= 0.5 * (new_width - *width);
781             *y -= 1.0 * (new_height - *height);
782             break;
783         case SP_ASPECT_XMAX_YMAX:
784             *x -= 1.0 * (new_width - *width);
785             *y -= 1.0 * (new_height - *height);
786             break;
787         default:
788             break;
789     }
790     *width = new_width;
791     *height = new_height;
794 #include "clear-n_.h"
796 }  /* namespace Internal */
797 }  /* namespace Extension */
798 }  /* namespace Inkscape */
800 #undef TRACE
802 /* End of GNU GPL code */
804 /*
805   Local Variables:
806   mode:c++
807   c-file-style:"stroustrup"
808   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
809   indent-tabs-mode:nil
810   fill-column:99
811   End:
812 */
813 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :