Code

NR:: to Geom:: for most of src/extension/
[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-matrix-ops.h>
32 #include <libnr/nr-matrix-fns.h>
33 #include <libnr/nr-matrix-translate-ops.h>
34 #include <libnr/nr-scale-matrix-ops.h>
36 #include "libnr/nr-matrix-rotate-ops.h"
37 #include "libnr/nr-matrix-translate-ops.h"
38 #include "libnr/nr-rotate-fns.h"
39 #include "libnr/nr-scale-ops.h"
40 #include "libnr/nr-scale-translate-ops.h"
41 #include "libnr/nr-translate-matrix-ops.h"
42 #include "libnr/nr-translate-scale-ops.h"
43 #include "libnr/nr-convert2geom.h"
44 #include <2geom/transforms.h>
45 #include <2geom/pathvector.h>
47 #include <glib/gmem.h>
49 #include <glibmm/i18n.h>
50 #include "display/nr-arena.h"
51 #include "display/nr-arena-item.h"
52 #include "display/nr-arena-group.h"
53 #include "display/curve.h"
54 #include "display/canvas-bpath.h"
55 #include "sp-item.h"
56 #include "sp-item-group.h"
57 #include "style.h"
58 #include "marker.h"
59 #include "sp-linear-gradient.h"
60 #include "sp-radial-gradient.h"
61 #include "sp-root.h"
62 #include "sp-shape.h"
63 #include "sp-use.h"
64 #include "sp-text.h"
65 #include "sp-flowtext.h"
66 #include "sp-image.h"
67 #include "sp-symbol.h"
68 #include "sp-pattern.h"
69 #include "sp-mask.h"
70 #include "sp-clippath.h"
72 #include <unit-constants.h>
73 #include "helper/png-write.h"
74 #include "helper/pixbuf-ops.h"
76 #include "cairo-renderer.h"
77 #include "cairo-render-context.h"
78 #include "extension/system.h"
80 #include "io/sys.h"
82 #include <cairo.h>
84 // include support for only the compiled-in surface types
85 #ifdef CAIRO_HAS_PDF_SURFACE
86 #include <cairo-pdf.h>
87 #endif
88 #ifdef CAIRO_HAS_PS_SURFACE
89 #include <cairo-ps.h>
90 #endif
92 //#define TRACE(_args) g_printf _args
93 #define TRACE(_args)
95 // FIXME: expose these from sp-clippath/mask.cpp
96 struct SPClipPathView {
97     SPClipPathView *next;
98     unsigned int key;
99     NRArenaItem *arenaitem;
100     NRRect bbox;
101 };
103 struct SPMaskView {
104     SPMaskView *next;
105     unsigned int key;
106     NRArenaItem *arenaitem;
107     NRRect bbox;
108 };
110 namespace Inkscape {
111 namespace Extension {
112 namespace Internal {
114 CairoRenderer::CairoRenderer(void)
115 {}
117 CairoRenderer::~CairoRenderer(void)
119     /* restore default signal handling for SIGPIPE */
120 #if !defined(_WIN32) && !defined(__WIN32__)
121     (void) signal(SIGPIPE, SIG_DFL);
122 #endif
124     return;
127 CairoRenderContext*
128 CairoRenderer::createContext(void)
130     CairoRenderContext *new_context = new CairoRenderContext(this);
131     g_assert( new_context != NULL );
133     new_context->_state_stack = NULL;
134     new_context->_state = NULL;
136     // create initial render state
137     CairoRenderState *state = new_context->_createState();
138     state->transform = Geom::identity();
139     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
140     new_context->_state = state;
142     return new_context;
145 void
146 CairoRenderer::destroyContext(CairoRenderContext *ctx)
148     delete ctx;
151 /*
153 Here comes the rendering part which could be put into the 'render' methods of SPItems'
155 */
157 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
158 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
159 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
168 /* 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 ) */
169 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
171     NRRect pbox;
173     SPShape *shape = SP_SHAPE(item);
175     if (!shape->curve) return;
177     /* fixme: Think (Lauris) */
178     sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
180     SPStyle* style = SP_OBJECT_STYLE (item);
181     CairoRenderer *renderer = ctx->getRenderer();
183     Geom::PathVector const & pathv = shape->curve->get_pathvector();
185     ctx->renderPathVector(pathv, style, &pbox);
187     /* TODO: make code prettier: lots of variables can be taken out of the loop! */
188     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
189         if ( shape->marker[SP_MARKER_LOC_START] ) {
190             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
191             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
193             Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
195             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
196                 tr = Geom::Scale(style->stroke_width.computed) * tr;
197             }
199             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
201             Geom::Matrix old_tr = marker_item->transform;
202             marker_item->transform = tr;
203             renderer->renderItem (ctx, marker_item);
204             marker_item->transform = old_tr;
205         }
207         if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
208             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
209             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
210             while (curve_it2 != path_it->end_default())
211             {
212                 /* Put marker between curve_it1 and curve_it2.
213                  * Loop to end_default (so including closing segment), because when a path is closed,
214                  * there should be a midpoint marker between last segment and closing straight line segment */
216                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
217                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
219                 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
221                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
222                     tr = Geom::Scale(style->stroke_width.computed) * tr;
223                 }
225                 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
227                 Geom::Matrix old_tr = marker_item->transform;
228                 marker_item->transform = tr;
229                 renderer->renderItem (ctx, marker_item);
230                 marker_item->transform = old_tr;
232                 ++curve_it1;
233                 ++curve_it2;
234             }
235         }
237         if ( shape->marker[SP_MARKER_LOC_END] ) {
238             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
239             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
241             /* Get reference to last curve in the path.
242              * For moveto-only path, this returns the "closing line segment". */
243             unsigned int index = path_it->size_default();
244             if (index > 0) {
245                 index--;
246             }
247             Geom::Curve const &lastcurve = (*path_it)[index];
249             Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
251             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
252                 tr = Geom::Scale(style->stroke_width.computed) * tr;
253             }
255             tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
257             Geom::Matrix old_tr = marker_item->transform;
258             marker_item->transform = tr;
259             renderer->renderItem (ctx, marker_item);
260             marker_item->transform = old_tr;
261         }
262     }
265 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
267     SPGroup *group = SP_GROUP(item);
268     CairoRenderer *renderer = ctx->getRenderer();
269     TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
271     GSList *l = g_slist_reverse(group->childList(false));
272     while (l) {
273         SPObject *o = SP_OBJECT (l->data);
274         if (SP_IS_ITEM(o)) {
275             renderer->renderItem (ctx, SP_ITEM (o));
276         }
277         l = g_slist_remove (l, o);
278     }
281 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
283     bool translated = false;
284     SPUse *use = SP_USE(item);
285     CairoRenderer *renderer = ctx->getRenderer();
287     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
288         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
289         ctx->pushState();
290         ctx->transform(&tp);
291         translated = true;
292     }
294     if (use->child && SP_IS_ITEM(use->child)) {
295         renderer->renderItem(ctx, SP_ITEM(use->child));
296     }
298     if (translated) {
299         ctx->popState();
300     }
303 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
305     SPText *group = SP_TEXT (item);
306     group->layout.showGlyphs(ctx);
309 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
311     SPFlowtext *group = SP_FLOWTEXT(item);
312     group->layout.showGlyphs(ctx);
315 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
317     SPImage *image;
318     guchar *px;
319     int w, h, rs;
321     image = SP_IMAGE (item);
323     if (!image->pixbuf) return;
324     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
326     px = gdk_pixbuf_get_pixels (image->pixbuf);
327     w = gdk_pixbuf_get_width (image->pixbuf);
328     h = gdk_pixbuf_get_height (image->pixbuf);
329     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
331     double x = image->x.computed;
332     double y = image->y.computed;
333     double width = image->width.computed;
334     double height = image->height.computed;
336     if (image->aspect_align != SP_ASPECT_NONE) {
337         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
338                                                      &x, &y, &width, &height);
339     }
341     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
342         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
343     }
345     Geom::Translate tp(x, y);
346     Geom::Scale s(width / (double)w, height / (double)h);
347     Geom::Matrix t(s * tp);
349     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
352 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
354     SPSymbol *symbol = SP_SYMBOL(item);
355     if (!SP_OBJECT_IS_CLONED (symbol))
356         return;
358     /* Cloned <symbol> is actually renderable */
359     ctx->pushState();
360     ctx->transform(&symbol->c2p);
362     // apply viewbox if set
363     if (0 /*symbol->viewBox_set*/) {
364         Geom::Matrix vb2user;
365         double x, y, width, height;
366         double view_width, view_height;
367         x = 0.0;
368         y = 0.0;
369         width = 1.0;
370         height = 1.0;
372         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
373         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
375         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
376                                      &x, &y,&width, &height);
378         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
379         vb2user = Geom::identity();
380         vb2user[0] = width / view_width;
381         vb2user[3] = height / view_height;
382         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
383         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
385         ctx->transform(&vb2user);
386     }
388     sp_group_render(item, ctx);
389     ctx->popState();
392 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
394     SPRoot *root = SP_ROOT(item);
395     CairoRenderer *renderer = ctx->getRenderer();
397     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
398         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
400     ctx->pushState();
401     renderer->setStateForItem(ctx, item);
402     Geom::Matrix tempmat (root->c2p);
403     ctx->transform(&tempmat);
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     //boost::optional<Geom::Rect> _bbox = item->getBounds(sp_item_i2d_affine(item));
422     // NRRect bbox = item->getBounds(sp_item_i2d_affine(item));
423     NRRect bbox(item->getBounds(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     Geom::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
477     Geom::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 = (Geom::Matrix)(Geom::Scale(1, -1) * (Geom::Matrix)(Geom::Translate (shift_x, shift_y)* eek.inverse()));
487     //t = t * ((Geom::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         Geom::Matrix matrix;
506         matrix = t;
507         //matrix = ((Geom::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     Geom::Matrix tempmat (item->transform);
614     ctx->transform(&tempmat);
615     sp_item_invoke_render(item, ctx);
617     if (state->need_layer)
618         ctx->popLayer();
620     ctx->popState();
623 bool
624 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
626     g_assert( ctx != NULL );
628     if (ctx->_vector_based_target) {
629         // width and height in pt
630         ctx->_width = sp_document_width(doc) * PT_PER_PX;
631         ctx->_height = sp_document_height(doc) * PT_PER_PX;
632     } else {
633         ctx->_width = sp_document_width(doc);
634         ctx->_height = sp_document_height(doc);
635     }
637     NRRect d;
638     bool pageBoundingBox = true;
639     if (pageBoundingBox) {
640         d.x0 = d.y0 = 0;
641         d.x1 = ceil(ctx->_width);
642         d.y1 = ceil(ctx->_height);
643     } else {
644         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
645         sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
646         if (ctx->_vector_based_target) {
647             // convert from px to pt
648             d.x0 *= PT_PER_PX;
649             d.x1 *= PT_PER_PX;
650             d.y0 *= PT_PER_PX;
651             d.y1 *= PT_PER_PX;
652         }
653     }
654     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
656     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
659 #include "macros.h" // SP_PRINT_*
661 void
662 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
664     g_assert( ctx != NULL && ctx->_is_valid );
666     if (cp == NULL)
667         return;
669     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
670     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
672     Geom::Matrix saved_ctm;
673     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
674         //SP_PRINT_DRECT("clipd", cp->display->bbox);
675         NRRect clip_bbox(cp->display->bbox);
676         Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
677         t[4] = clip_bbox.x0;
678         t[5] = clip_bbox.y0;
679         t *= ctx->getCurrentState()->transform;
680         ctx->getTransform(&saved_ctm);
681         ctx->setTransform(&t);
682     }
684     TRACE(("BEGIN clip\n"));
685     SPObject *co = SP_OBJECT(cp);
686     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
687         if (SP_IS_ITEM(child)) {
688             SPItem *item = SP_ITEM(child);
689             renderItem(ctx, item);
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 void
706 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
708     g_assert( ctx != NULL && ctx->_is_valid );
710     if (mask == NULL)
711         return;
713     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
714     NRRect mask_bbox(mask->display->bbox);
715     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
716     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
717         Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
718         t[4] = mask_bbox.x0;
719         t[5] = mask_bbox.y0;
720         t *= ctx->getCurrentState()->transform;
721         ctx->setTransform(&t);
722     }
724     // clip mask contents
725     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
727     ctx->pushState();
729     TRACE(("BEGIN mask\n"));
730     SPObject *co = SP_OBJECT(mask);
731     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
732         if (SP_IS_ITEM(child)) {
733             SPItem *item = SP_ITEM(child);
734             renderItem(ctx, item);
735         }
736     }
737     TRACE(("END mask\n"));
739     ctx->popState();
742 void
743 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
744                              double *x, double *y, double *width, double *height)
746     if (aspect_align == SP_ASPECT_NONE)
747         return;
749     double scalex, scaley, scale;
750     double new_width, new_height;
751     scalex = *width / vp_width;
752     scaley = *height / vp_height;
753     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
754     new_width = vp_width * scale;
755     new_height = vp_height * scale;
756     /* Now place viewbox to requested position */
757     switch (aspect_align) {
758         case SP_ASPECT_XMIN_YMIN:
759             break;
760         case SP_ASPECT_XMID_YMIN:
761             *x -= 0.5 * (new_width - *width);
762             break;
763         case SP_ASPECT_XMAX_YMIN:
764             *x -= 1.0 * (new_width - *width);
765             break;
766         case SP_ASPECT_XMIN_YMID:
767             *y -= 0.5 * (new_height - *height);
768             break;
769         case SP_ASPECT_XMID_YMID:
770             *x -= 0.5 * (new_width - *width);
771             *y -= 0.5 * (new_height - *height);
772             break;
773         case SP_ASPECT_XMAX_YMID:
774             *x -= 1.0 * (new_width - *width);
775             *y -= 0.5 * (new_height - *height);
776             break;
777         case SP_ASPECT_XMIN_YMAX:
778             *y -= 1.0 * (new_height - *height);
779             break;
780         case SP_ASPECT_XMID_YMAX:
781             *x -= 0.5 * (new_width - *width);
782             *y -= 1.0 * (new_height - *height);
783             break;
784         case SP_ASPECT_XMAX_YMAX:
785             *x -= 1.0 * (new_width - *width);
786             *y -= 1.0 * (new_height - *height);
787             break;
788         default:
789             break;
790     }
791     *width = new_width;
792     *height = new_height;
795 #include "clear-n_.h"
797 }  /* namespace Internal */
798 }  /* namespace Extension */
799 }  /* namespace Inkscape */
801 #undef TRACE
803 /* End of GNU GPL code */
805 /*
806   Local Variables:
807   mode:c++
808   c-file-style:"stroustrup"
809   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
810   indent-tabs-mode:nil
811   fill-column:99
812   End:
813 */
814 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :