Code

use CairoRenderContext::renderPathVector
[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     Geom::PathVector const & pathv = shape->curve->get_pathvector();
186     ctx->renderPathVector(pathv, style, &pbox);
188     /* TODO: make code prettier: lots of variables can be taken out of the loop! */
189     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
190         if ( shape->marker[SP_MARKER_LOC_START] ) {
191             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
192             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
194             Geom::Point p = path_it->front().pointAt(0);
195             Geom::Point tang = path_it->front().unitTangentAt(0);
196             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
198             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
199                 tr = NR::scale(style->stroke_width.computed) * tr;
200             }
202             tr = marker_item->transform * marker->c2p * tr;
204             NR::Matrix old_tr = marker_item->transform;
205             marker_item->transform = tr;
206             renderer->renderItem (ctx, marker_item);
207             marker_item->transform = old_tr;
208         }
210         if ( shape->marker[SP_MARKER_LOC_MID] ) {
211             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
212             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
213             while (curve_it2 != path_it->end_default())
214             {
215                 /* Put marker between curve_it1 and curve_it2.
216                  * Loop to end_default (so including closing segment), because when a path is closed,
217                  * there should be a midpoint marker between last segment and closing straight line segment */
219                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
220                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
222                 Geom::Point p = curve_it1->pointAt(1);
223                 Geom::Point tang1 = curve_it1->unitTangentAt(1);
224                 Geom::Point tang2 = curve_it2->unitTangentAt(0);
225                 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang1, tang2)));
227                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
228                     tr = NR::scale(style->stroke_width.computed) * tr;
229                 }
231                 tr = marker_item->transform * marker->c2p * tr;
233                 NR::Matrix old_tr = marker_item->transform;
234                 marker_item->transform = tr;
235                 renderer->renderItem (ctx, marker_item);
236                 marker_item->transform = old_tr;
238                 ++curve_it1;
239                 ++curve_it2;
240             }
241         }
243         if ( shape->marker[SP_MARKER_LOC_END] ) {
244             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
245             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
247             Geom::Point p = path_it->back_default().pointAt(1);
248             Geom::Point tang = path_it->back_default().unitTangentAt(1);
249             NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
251             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
252                 tr = NR::scale(style->stroke_width.computed) * tr;
253             }
255             tr = marker_item->transform * marker->c2p * tr;
257             NR::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         NR::Matrix tp(NR::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     NR::translate tp(x, y);
346     NR::scale s(width / (double)w, height / (double)h);
347     NR::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         NR::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.set_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     ctx->transform(&root->c2p);
403     sp_group_render(item, ctx);
404     ctx->popState();
407 /**
408     this function convert the item to a raster image and include the raster into the cairo renderer
409 */
410 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
412     g_warning("render as bitmap");
414     //the code now was copied from sp_selection_create_bitmap_copy
416     SPDocument *document = SP_OBJECT(item)->document;
417     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
419     // Get the bounding box of the selection
420     //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
421     // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
422     NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
425     // List of the items to show; all others will be hidden
426     GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
427     items = g_slist_append(items, item);
429     // Remember parent and z-order of the topmost one
430     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
431     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
432     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
434     // Calculate resolution
435     double res;
436     /** @TODO reimplement the resolution stuff
437     */
438     res = ctx->getBitmapResolution();
439     if(res == 0) {
440         res = PX_PER_IN;
441     }
444     // The width and height of the bitmap in pixels
445     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
446     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
448     // Find out if we have to run a filter
449     gchar const *run = NULL;
450     gchar const *filter = NULL;
451     /** @TODO reimplement the filter stuff
452     //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
453     if (filter) {
454         // filter command is given;
455         // see if we have a parameter to pass to it
456         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
457         if (param1) {
458             if (param1[strlen(param1) - 1] == '%') {
459                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
460                 gchar p1[256];
461                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
462                 // the first param is always the image filename, the second is param1
463                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
464             } else {
465                 // otherwise pass the param1 unchanged
466                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
467             }
468         } else {
469             // run without extra parameter
470             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
471         }
472     }
473     */
474     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
475     NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
476     NR::Matrix t;
478     double shift_x = bbox.x0;
479     double shift_y = bbox.y1;
480     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
481         shift_x = round (shift_x);
482         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
483     }
484     t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
486     //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
488     // Do the export
489     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
490         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
492     // Run filter, if any
493     /*
494     if (run) {
495         g_print ("Running external filter: %s\n", run);
496         system (run);
497     }
498     */
499     if (pb) {
500         unsigned char *px = gdk_pixbuf_get_pixels (pb);
501         unsigned int w = gdk_pixbuf_get_width(pb);
502         unsigned int h = gdk_pixbuf_get_height(pb);
503         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
504         NR::Matrix matrix;
505         matrix = t;
506         //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
507         //matrix.set_identity();
509         ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
510     /*
511         // Create the repr for the image
512         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
513         repr->setAttribute("xlink:href", filename);
514         repr->setAttribute("sodipodi:absref", filepath);
515         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
516             sp_repr_set_svg_double(repr, "width", width);
517             sp_repr_set_svg_double(repr, "height", height);
518         } else {
519             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
520             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
521         }
523         // Write transform
524         gchar *c=sp_svg_transform_write(t);
525         repr->setAttribute("transform", c);
526         g_free(c);
528         // add the new repr to the parent
529         parent->appendChild(repr);
531         // move to the saved position
532         repr->setPosition(pos > 0 ? pos + 1 : 1);
533     */
534         gdk_pixbuf_unref (pb);
535     }
536     g_slist_free (items);
539 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
541     // Check item's visibility
542     if (item->isHidden()) {
543         return;
544     }
546     SPStyle* style = SP_OBJECT_STYLE (item);
547     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
548         return sp_asbitmap_render(item, ctx);
549     }
551     if (SP_IS_ROOT(item)) {
552         TRACE(("root\n"));
553         return sp_root_render(item, ctx);
554     } else if (SP_IS_GROUP(item)) {
555         TRACE(("group\n"));
556         return sp_group_render(item, ctx);
557     } else if (SP_IS_SHAPE(item)) {
558         TRACE(("shape\n"));
559         return sp_shape_render(item, ctx);
560     } else if (SP_IS_USE(item)) {
561         TRACE(("use begin---\n"));
562         sp_use_render(item, ctx);
563         TRACE(("---use end\n"));
564     } else if (SP_IS_SYMBOL(item)) {
565         TRACE(("symbol\n"));
566         return sp_symbol_render(item, ctx);
567     } else if (SP_IS_TEXT(item)) {
568         TRACE(("text\n"));
569         return sp_text_render(item, ctx);
570     } else if (SP_IS_FLOWTEXT(item)) {
571         TRACE(("flowtext\n"));
572         return sp_flowtext_render(item, ctx);
573     } else if (SP_IS_IMAGE(item)) {
574         TRACE(("image\n"));
575         return sp_image_render(item, ctx);
576     }
579 void
580 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
582     SPStyle const *style = SP_OBJECT_STYLE(item);
583     ctx->setStateForStyle(style);
585     CairoRenderState *state = ctx->getCurrentState();
586     state->clip_path = item->clip_ref->getObject();
587     state->mask = item->mask_ref->getObject();
589     // If parent_has_userspace is true the parent state's transform
590     // has to be used for the mask's/clippath's context.
591     // This is so because we use the image's/(flow)text's transform for positioning
592     // instead of explicitly specifying it and letting the renderer do the
593     // transformation before rendering the item.
594     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
595         state->parent_has_userspace = TRUE;
596     TRACE(("set op: %f\n", state->opacity));
599 void
600 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
602     ctx->pushState();
603     setStateForItem(ctx, item);
605     CairoRenderState *state = ctx->getCurrentState();
606     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
608     if (state->need_layer) {
609         state->merge_opacity = FALSE;
610         ctx->pushLayer();
611     }
612     ctx->transform(&item->transform);
613     sp_item_invoke_render(item, ctx);
615     if (state->need_layer)
616         ctx->popLayer();
618     ctx->popState();
621 bool
622 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
624     g_assert( ctx != NULL );
626     if (ctx->_vector_based_target) {
627         // width and height in pt
628         ctx->_width = sp_document_width(doc) * PT_PER_PX;
629         ctx->_height = sp_document_height(doc) * PT_PER_PX;
630     } else {
631         ctx->_width = sp_document_width(doc);
632         ctx->_height = sp_document_height(doc);
633     }
635     NRRect d;
636     bool pageBoundingBox = true;
637     if (pageBoundingBox) {
638         d.x0 = d.y0 = 0;
639         d.x1 = ceil(ctx->_width);
640         d.y1 = ceil(ctx->_height);
641     } else {
642         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
643         sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
644         if (ctx->_vector_based_target) {
645             // convert from px to pt
646             d.x0 *= PT_PER_PX;
647             d.x1 *= PT_PER_PX;
648             d.y0 *= PT_PER_PX;
649             d.y1 *= PT_PER_PX;
650         }
651     }
652     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
654     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
657 #include "macros.h" // SP_PRINT_*
659 void
660 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
662     g_assert( ctx != NULL && ctx->_is_valid );
664     if (cp == NULL)
665         return;
667     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
668     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
670     NR::Matrix saved_ctm;
671     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
672         //SP_PRINT_DRECT("clipd", cp->display->bbox);
673         NRRect clip_bbox(cp->display->bbox);
674         NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
675         t[4] = clip_bbox.x0;
676         t[5] = clip_bbox.y0;
677         t *= ctx->getCurrentState()->transform;
678         ctx->getTransform(&saved_ctm);
679         ctx->setTransform(&t);
680     }
682     TRACE(("BEGIN clip\n"));
683     SPObject *co = SP_OBJECT(cp);
684     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
685         if (SP_IS_ITEM(child)) {
686             SPItem *item = SP_ITEM(child);
687             renderItem(ctx, item);
688         }
689     }
690     TRACE(("END clip\n"));
692     // do clipping only if this was the first call to applyClipPath
693     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
694         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
695         cairo_clip(ctx->_cr);
697     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
698         ctx->setTransform(&saved_ctm);
700     ctx->setRenderMode(saved_mode);
703 void
704 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
706     g_assert( ctx != NULL && ctx->_is_valid );
708     if (mask == NULL)
709         return;
711     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
712     NRRect mask_bbox(mask->display->bbox);
713     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
714     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
715         NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
716         t[4] = mask_bbox.x0;
717         t[5] = mask_bbox.y0;
718         t *= ctx->getCurrentState()->transform;
719         ctx->setTransform(&t);
720     }
722     // clip mask contents
723     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
725     ctx->pushState();
727     TRACE(("BEGIN mask\n"));
728     SPObject *co = SP_OBJECT(mask);
729     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
730         if (SP_IS_ITEM(child)) {
731             SPItem *item = SP_ITEM(child);
732             renderItem(ctx, item);
733         }
734     }
735     TRACE(("END mask\n"));
737     ctx->popState();
740 void
741 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
742                              double *x, double *y, double *width, double *height)
744     if (aspect_align == SP_ASPECT_NONE)
745         return;
747     double scalex, scaley, scale;
748     double new_width, new_height;
749     scalex = *width / vp_width;
750     scaley = *height / vp_height;
751     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
752     new_width = vp_width * scale;
753     new_height = vp_height * scale;
754     /* Now place viewbox to requested position */
755     switch (aspect_align) {
756         case SP_ASPECT_XMIN_YMIN:
757             break;
758         case SP_ASPECT_XMID_YMIN:
759             *x -= 0.5 * (new_width - *width);
760             break;
761         case SP_ASPECT_XMAX_YMIN:
762             *x -= 1.0 * (new_width - *width);
763             break;
764         case SP_ASPECT_XMIN_YMID:
765             *y -= 0.5 * (new_height - *height);
766             break;
767         case SP_ASPECT_XMID_YMID:
768             *x -= 0.5 * (new_width - *width);
769             *y -= 0.5 * (new_height - *height);
770             break;
771         case SP_ASPECT_XMAX_YMID:
772             *x -= 1.0 * (new_width - *width);
773             *y -= 0.5 * (new_height - *height);
774             break;
775         case SP_ASPECT_XMIN_YMAX:
776             *y -= 1.0 * (new_height - *height);
777             break;
778         case SP_ASPECT_XMID_YMAX:
779             *x -= 0.5 * (new_width - *width);
780             *y -= 1.0 * (new_height - *height);
781             break;
782         case SP_ASPECT_XMAX_YMAX:
783             *x -= 1.0 * (new_width - *width);
784             *y -= 1.0 * (new_height - *height);
785             break;
786         default:
787             break;
788     }
789     *width = new_width;
790     *height = new_height;
793 #include "clear-n_.h"
795 }  /* namespace Internal */
796 }  /* namespace Extension */
797 }  /* namespace Inkscape */
799 #undef TRACE
801 /* End of GNU GPL code */
803 /*
804   Local Variables:
805   mode:c++
806   c-file-style:"stroustrup"
807   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
808   indent-tabs-mode:nil
809   fill-column:99
810   End:
811 */
812 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :