Code

1d103fed27cadb980ebbdd3c54c41b8a066c153b
[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"
45 #include <glib/gmem.h>
47 #include <glibmm/i18n.h>
48 #include "display/nr-arena.h"
49 #include "display/nr-arena-item.h"
50 #include "display/nr-arena-group.h"
51 #include "display/curve.h"
52 #include "display/canvas-bpath.h"
53 #include "sp-item.h"
54 #include "sp-item-group.h"
55 #include "style.h"
56 #include "marker.h"
57 #include "sp-linear-gradient.h"
58 #include "sp-radial-gradient.h"
59 #include "sp-root.h"
60 #include "sp-shape.h"
61 #include "sp-use.h"
62 #include "sp-text.h"
63 #include "sp-flowtext.h"
64 #include "sp-image.h"
65 #include "sp-symbol.h"
66 #include "sp-pattern.h"
67 #include "sp-mask.h"
68 #include "sp-clippath.h"
70 #include <unit-constants.h>
71 #include "helper/png-write.h"
72 #include "helper/pixbuf-ops.h"
74 #include "cairo-renderer.h"
75 #include "cairo-render-context.h"
76 #include "extension/system.h"
78 #include "io/sys.h"
80 #include <cairo.h>
82 // include support for only the compiled-in surface types
83 #ifdef CAIRO_HAS_PDF_SURFACE
84 #include <cairo-pdf.h>
85 #endif
86 #ifdef CAIRO_HAS_PS_SURFACE
87 #include <cairo-ps.h>
88 #endif
90 //#define TRACE(_args) g_printf _args
91 #define TRACE(_args)
93 // FIXME: expose these from sp-clippath/mask.cpp
94 struct SPClipPathView {
95     SPClipPathView *next;
96     unsigned int key;
97     NRArenaItem *arenaitem;
98     NRRect bbox;
99 };
101 struct SPMaskView {
102     SPMaskView *next;
103     unsigned int key;
104     NRArenaItem *arenaitem;
105     NRRect bbox;
106 };
108 namespace Inkscape {
109 namespace Extension {
110 namespace Internal {
112 CairoRenderer::CairoRenderer(void)
113 {}
115 CairoRenderer::~CairoRenderer(void)
117     /* restore default signal handling for SIGPIPE */
118 #if !defined(_WIN32) && !defined(__WIN32__)
119     (void) signal(SIGPIPE, SIG_DFL);
120 #endif
122     return;
125 CairoRenderContext*
126 CairoRenderer::createContext(void)
128     CairoRenderContext *new_context = new CairoRenderContext(this);
129     g_assert( new_context != NULL );
131     new_context->_state_stack = NULL;
132     new_context->_state = NULL;
134     // create initial render state
135     CairoRenderState *state = new_context->_createState();
136     nr_matrix_set_identity(&state->transform);
137     new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
138     new_context->_state = state;
140     return new_context;
143 void
144 CairoRenderer::destroyContext(CairoRenderContext *ctx)
146     delete ctx;
149 /*
151 Here comes the rendering part which could be put into the 'render' methods of SPItems'
153 */
155 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
156 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
157 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
159 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
168     NRRect pbox;
170     SPShape *shape = SP_SHAPE(item);
172     if (!shape->curve) return;
174     /* fixme: Think (Lauris) */
175     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
176     NR::Matrix const i2d = sp_item_i2d_affine(item);
178     SPStyle* style = SP_OBJECT_STYLE (item);
179     CairoRenderer *renderer = ctx->getRenderer();
181     NRBPath bp;
182     bp.path = SP_CURVE_BPATH(shape->curve);
184     ctx->renderPath(&bp, style, &pbox);
186     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
187         for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
188             if (sp_shape_marker_required (shape, m, bp)) {
190                 SPMarker* marker = SP_MARKER (shape->marker[m]);
191                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
193                 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
195                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
196                     tr = NR::scale(style->stroke_width.computed) * tr;
197                 }
199                 tr = marker_item->transform * marker->c2p * tr;
201                 NR::Matrix old_tr = marker_item->transform;
202                 marker_item->transform = tr;
203                 renderer->renderItem (ctx, marker_item);
204                 marker_item->transform = old_tr;
205             }
206         }
207     }
210 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
212     SPGroup *group = SP_GROUP(item);
213     CairoRenderer *renderer = ctx->getRenderer();
214     TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
216     GSList *l = g_slist_reverse(group->childList(false));
217     while (l) {
218         SPObject *o = SP_OBJECT (l->data);
219         if (SP_IS_ITEM(o)) {
220             renderer->renderItem (ctx, SP_ITEM (o));
221         }
222         l = g_slist_remove (l, o);
223     }
226 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
228     bool translated = false;
229     NRMatrix tp;
230     SPUse *use = SP_USE(item);
231     CairoRenderer *renderer = ctx->getRenderer();
233     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
234         nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
235         ctx->pushState();
236         ctx->transform(&tp);
237         translated = true;
238     }
240     if (use->child && SP_IS_ITEM(use->child)) {
241         renderer->renderItem(ctx, SP_ITEM(use->child));
242     }
244     if (translated) {
245         ctx->popState();
246     }
249 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
251     SPText *group = SP_TEXT (item);
252     group->layout.showGlyphs(ctx);
255 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
257     SPFlowtext *group = SP_FLOWTEXT(item);
258     group->layout.showGlyphs(ctx);
261 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
263     SPImage *image;
264     NRMatrix tp, s, t;
265     guchar *px;
266     int w, h, rs;
268     image = SP_IMAGE (item);
270     if (!image->pixbuf) return;
271     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
273     px = gdk_pixbuf_get_pixels (image->pixbuf);
274     w = gdk_pixbuf_get_width (image->pixbuf);
275     h = gdk_pixbuf_get_height (image->pixbuf);
276     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
278     double x = image->x.computed;
279     double y = image->y.computed;
280     double width = image->width.computed;
281     double height = image->height.computed;
283     if (image->aspect_align != SP_ASPECT_NONE) {
284         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
285                                                      &x, &y, &width, &height);
286     }
288     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
289         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
290     }
292     nr_matrix_set_translate (&tp, x, y);
293     nr_matrix_set_scale (&s, width / (double)w, height / (double)h);
294     nr_matrix_multiply (&t, &s, &tp);
296     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
299 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
301     SPSymbol *symbol = SP_SYMBOL(item);
302     if (!SP_OBJECT_IS_CLONED (symbol))
303         return;
305     /* Cloned <symbol> is actually renderable */
306     ctx->pushState();
307     ctx->transform(&symbol->c2p);
309     // apply viewbox if set
310     if (0 /*symbol->viewBox_set*/) {
311         NRMatrix vb2user;
312         double x, y, width, height;
313         double view_width, view_height;
314         x = 0.0;
315         y = 0.0;
316         width = 1.0;
317         height = 1.0;
319         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
320         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
322         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
323                                      &x, &y,&width, &height);
325         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
326         nr_matrix_set_identity(&vb2user);
327         vb2user[0] = width / view_width;
328         vb2user[3] = height / view_height;
329         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
330         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
332         ctx->transform(&vb2user);
333     }
335     sp_group_render(item, ctx);
336     ctx->popState();
339 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
341     SPRoot *root = SP_ROOT(item);
342     CairoRenderer *renderer = ctx->getRenderer();
344     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
345         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
347     ctx->pushState();
348     renderer->setStateForItem(ctx, item);
349     ctx->transform(root->c2p);
350     sp_group_render(item, ctx);
351     ctx->popState();
354 /**
355     this function convert the item to a raster image and include the raster into the cairo renderer
356 */
357 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
359     g_warning("render as bitmap");
361     //the code now was copied from sp_selection_create_bitmap_copy
363     SPDocument *document = SP_OBJECT(item)->document;
364     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
366     // Get the bounding box of the selection
367     //NR::Maybe<NR::Rect> _bbox = item->getBounds(sp_item_i2d_affine(item));
368     // NRRect bbox = item->getBounds(sp_item_i2d_affine(item));
369     NRRect bbox(item->getBounds(sp_item_i2d_affine(item)));
372     // List of the items to show; all others will be hidden
373     GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
374     items = g_slist_append(items, item);
376     // Remember parent and z-order of the topmost one
377     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
378     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
379     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
381     // Calculate resolution
382     double res;
383     /** @TODO reimplement the resolution stuff
384     */
385     res = ctx->getBitmapResolution();
386     if(res == 0) {
387         res = PX_PER_IN;
388     }
391     // The width and height of the bitmap in pixels
392     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
393     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
395     // Find out if we have to run a filter
396     gchar const *run = NULL;
397     gchar const *filter = NULL;
398     /** @TODO reimplement the filter stuff
399     //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
400     if (filter) {
401         // filter command is given;
402         // see if we have a parameter to pass to it
403         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
404         if (param1) {
405             if (param1[strlen(param1) - 1] == '%') {
406                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
407                 gchar p1[256];
408                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
409                 // the first param is always the image filename, the second is param1
410                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
411             } else {
412                 // otherwise pass the param1 unchanged
413                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
414             }
415         } else {
416             // run without extra parameter
417             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
418         }
419     }
420     */
421     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
422     NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
423     NR::Matrix t;
425     double shift_x = bbox.x0;
426     double shift_y = bbox.y1;
427     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
428         shift_x = round (shift_x);
429         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
430     }
431     t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
433     //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
435     // Do the export
436     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
437         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
439     // Run filter, if any
440     /*
441     if (run) {
442         g_print ("Running external filter: %s\n", run);
443         system (run);
444     }
445     */
446     if (pb) {
447         unsigned char *px = gdk_pixbuf_get_pixels (pb);
448         unsigned int w = gdk_pixbuf_get_width(pb);
449         unsigned int h = gdk_pixbuf_get_height(pb);
450         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
451         NRMatrix matrix;
452         matrix = t;
453         //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
454         //nr_matrix_set_identity(&matrix);
456         ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
457     /*
458         // Create the repr for the image
459         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
460         repr->setAttribute("xlink:href", filename);
461         repr->setAttribute("sodipodi:absref", filepath);
462         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
463             sp_repr_set_svg_double(repr, "width", width);
464             sp_repr_set_svg_double(repr, "height", height);
465         } else {
466             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
467             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
468         }
470         // Write transform
471         gchar *c=sp_svg_transform_write(t);
472         repr->setAttribute("transform", c);
473         g_free(c);
475         // add the new repr to the parent
476         parent->appendChild(repr);
478         // move to the saved position
479         repr->setPosition(pos > 0 ? pos + 1 : 1);
480     */
481         gdk_pixbuf_unref (pb);
482     }
483     g_slist_free (items);
486 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
488     // Check item's visibility
489     if (item->isHidden()) {
490         return;
491     }
493     SPStyle* style = SP_OBJECT_STYLE (item);
494     if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
495         return sp_asbitmap_render(item, ctx);
496     }
498     if (SP_IS_ROOT(item)) {
499         TRACE(("root\n"));
500         return sp_root_render(item, ctx);
501     } else if (SP_IS_GROUP(item)) {
502         TRACE(("group\n"));
503         return sp_group_render(item, ctx);
504     } else if (SP_IS_SHAPE(item)) {
505         TRACE(("shape\n"));
506         return sp_shape_render(item, ctx);
507     } else if (SP_IS_USE(item)) {
508         TRACE(("use begin---\n"));
509         sp_use_render(item, ctx);
510         TRACE(("---use end\n"));
511     } else if (SP_IS_SYMBOL(item)) {
512         TRACE(("symbol\n"));
513         return sp_symbol_render(item, ctx);
514     } else if (SP_IS_TEXT(item)) {
515         TRACE(("text\n"));
516         return sp_text_render(item, ctx);
517     } else if (SP_IS_FLOWTEXT(item)) {
518         TRACE(("flowtext\n"));
519         return sp_flowtext_render(item, ctx);
520     } else if (SP_IS_IMAGE(item)) {
521         TRACE(("image\n"));
522         return sp_image_render(item, ctx);
523     }
526 void
527 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
529     SPStyle const *style = SP_OBJECT_STYLE(item);
530     ctx->setStateForStyle(style);
532     CairoRenderState *state = ctx->getCurrentState();
533     state->clip_path = item->clip_ref->getObject();
534     state->mask = item->mask_ref->getObject();
536     // If parent_has_userspace is true the parent state's transform
537     // has to be used for the mask's/clippath's context.
538     // This is so because we use the image's/(flow)text's transform for positioning
539     // instead of explicitly specifying it and letting the renderer do the
540     // transformation before rendering the item.
541     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
542         state->parent_has_userspace = TRUE;
543     TRACE(("set op: %f\n", state->opacity));
546 void
547 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
549     ctx->pushState();
550     setStateForItem(ctx, item);
552     CairoRenderState *state = ctx->getCurrentState();
553     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
555     if (state->need_layer) {
556         state->merge_opacity = FALSE;
557         ctx->pushLayer();
558     }
559     ctx->transform(item->transform);
560     sp_item_invoke_render(item, ctx);
562     if (state->need_layer)
563         ctx->popLayer();
565     ctx->popState();
568 bool
569 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
571     g_assert( ctx != NULL );
573     if (ctx->_vector_based_target) {
574         // width and height in pt
575         ctx->_width = sp_document_width(doc) * PT_PER_PX;
576         ctx->_height = sp_document_height(doc) * PT_PER_PX;
577     } else {
578         ctx->_width = sp_document_width(doc);
579         ctx->_height = sp_document_height(doc);
580     }
582     NRRect d;
583     bool pageBoundingBox = true;
584     if (pageBoundingBox) {
585         d.x0 = d.y0 = 0;
586         d.x1 = ceil(ctx->_width);
587         d.y1 = ceil(ctx->_height);
588     } else {
589         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
590         sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
591         if (ctx->_vector_based_target) {
592             // convert from px to pt
593             d.x0 *= PT_PER_PX;
594             d.x1 *= PT_PER_PX;
595             d.y0 *= PT_PER_PX;
596             d.y1 *= PT_PER_PX;
597         }
598     }
599     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
601     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
604 #include "macros.h" // SP_PRINT_*
606 void
607 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
609     g_assert( ctx != NULL && ctx->_is_valid );
611     if (cp == NULL)
612         return;
614     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
615     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
617     NRMatrix saved_ctm;
618     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
619         NRMatrix t;
620         //SP_PRINT_DRECT("clipd", cp->display->bbox);
621         NRRect clip_bbox(cp->display->bbox);
622         nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
623         t.c[4] = clip_bbox.x0;
624         t.c[5] = clip_bbox.y0;
625         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
626         ctx->getTransform(&saved_ctm);
627         ctx->setTransform(&t);
628     }
630     TRACE(("BEGIN clip\n"));
631     SPObject *co = SP_OBJECT(cp);
632     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
633         if (SP_IS_ITEM(child)) {
634             SPItem *item = SP_ITEM(child);
635             renderItem(ctx, item);
636         }
637     }
638     TRACE(("END clip\n"));
640     // do clipping only if this was the first call to applyClipPath
641     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
642         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
643         cairo_clip(ctx->_cr);
645     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
646         ctx->setTransform(&saved_ctm);
648     ctx->setRenderMode(saved_mode);
651 void
652 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
654     g_assert( ctx != NULL && ctx->_is_valid );
656     if (mask == NULL)
657         return;
659     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
660     NRRect mask_bbox(mask->display->bbox);
661     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
662     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
663         NRMatrix t;
664         nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
665         t.c[4] = mask_bbox.x0;
666         t.c[5] = mask_bbox.y0;
667         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
668         ctx->setTransform(&t);
669     }
671     // clip mask contents
672     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
674     ctx->pushState();
676     TRACE(("BEGIN mask\n"));
677     SPObject *co = SP_OBJECT(mask);
678     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
679         if (SP_IS_ITEM(child)) {
680             SPItem *item = SP_ITEM(child);
681             renderItem(ctx, item);
682         }
683     }
684     TRACE(("END mask\n"));
686     ctx->popState();
689 void
690 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
691                              double *x, double *y, double *width, double *height)
693     if (aspect_align == SP_ASPECT_NONE)
694         return;
696     double scalex, scaley, scale;
697     double new_width, new_height;
698     scalex = *width / vp_width;
699     scaley = *height / vp_height;
700     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
701     new_width = vp_width * scale;
702     new_height = vp_height * scale;
703     /* Now place viewbox to requested position */
704     switch (aspect_align) {
705         case SP_ASPECT_XMIN_YMIN:
706             break;
707         case SP_ASPECT_XMID_YMIN:
708             *x -= 0.5 * (new_width - *width);
709             break;
710         case SP_ASPECT_XMAX_YMIN:
711             *x -= 1.0 * (new_width - *width);
712             break;
713         case SP_ASPECT_XMIN_YMID:
714             *y -= 0.5 * (new_height - *height);
715             break;
716         case SP_ASPECT_XMID_YMID:
717             *x -= 0.5 * (new_width - *width);
718             *y -= 0.5 * (new_height - *height);
719             break;
720         case SP_ASPECT_XMAX_YMID:
721             *x -= 1.0 * (new_width - *width);
722             *y -= 0.5 * (new_height - *height);
723             break;
724         case SP_ASPECT_XMIN_YMAX:
725             *y -= 1.0 * (new_height - *height);
726             break;
727         case SP_ASPECT_XMID_YMAX:
728             *x -= 0.5 * (new_width - *width);
729             *y -= 1.0 * (new_height - *height);
730             break;
731         case SP_ASPECT_XMAX_YMAX:
732             *x -= 1.0 * (new_width - *width);
733             *y -= 1.0 * (new_height - *height);
734             break;
735         default:
736             break;
737     }
738     *width = new_width;
739     *height = new_height;
742 #include "clear-n_.h"
744 }  /* namespace Internal */
745 }  /* namespace Extension */
746 }  /* namespace Inkscape */
748 #undef TRACE
750 /* End of GNU GPL code */
752 /*
753   Local Variables:
754   mode:c++
755   c-file-style:"stroustrup"
756   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
757   indent-tabs-mode:nil
758   fill-column:99
759   End:
760 */
761 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :