Code

early version export to PS using blur to bitmap
[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     if(style->filter.set != 0) {
180         sp_asbitmap_render(item, ctx);
181         return;
182     }
183     CairoRenderer *renderer = ctx->getRenderer();
185     NRBPath bp;
186     bp.path = SP_CURVE_BPATH(shape->curve);
188     ctx->renderPath(&bp, style, &pbox);
190     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
191         for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
192             if (sp_shape_marker_required (shape, m, bp)) {
194                 SPMarker* marker = SP_MARKER (shape->marker[m]);
195                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
197                 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
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             }
210         }
211     }
214 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
216     SPGroup *group = SP_GROUP(item);
217     CairoRenderer *renderer = ctx->getRenderer();
218     TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
220     GSList *l = g_slist_reverse(group->childList(false));
221     while (l) {
222         SPObject *o = SP_OBJECT (l->data);
223         if (SP_IS_ITEM(o)) {
224             renderer->renderItem (ctx, SP_ITEM (o));
225         }
226         l = g_slist_remove (l, o);
227     }
230 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
232     bool translated = false;
233     NRMatrix tp;
234     SPUse *use = SP_USE(item);
235     CairoRenderer *renderer = ctx->getRenderer();
237     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
238         nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
239         ctx->pushState();
240         ctx->transform(&tp);
241         translated = true;
242     }
244     if (use->child && SP_IS_ITEM(use->child)) {
245         renderer->renderItem(ctx, SP_ITEM(use->child));
246     }
248     if (translated) {
249         ctx->popState();
250     }
253 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
255     SPText *group = SP_TEXT (item);
256     group->layout.showGlyphs(ctx);
259 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
261     SPFlowtext *group = SP_FLOWTEXT(item);
262     group->layout.showGlyphs(ctx);
265 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
267     SPImage *image;
268     NRMatrix tp, s, t;
269     guchar *px;
270     int w, h, rs;
272     image = SP_IMAGE (item);
274     if (!image->pixbuf) return;
275     if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
277     px = gdk_pixbuf_get_pixels (image->pixbuf);
278     w = gdk_pixbuf_get_width (image->pixbuf);
279     h = gdk_pixbuf_get_height (image->pixbuf);
280     rs = gdk_pixbuf_get_rowstride (image->pixbuf);
282     double x = image->x.computed;
283     double y = image->y.computed;
284     double width = image->width.computed;
285     double height = image->height.computed;
287     if (image->aspect_align != SP_ASPECT_NONE) {
288         calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
289                                                      &x, &y, &width, &height);
290     }
292     if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
293         ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
294     }
296     nr_matrix_set_translate (&tp, x, y);
297     nr_matrix_set_scale (&s, width / (double)w, height / (double)h);
298     nr_matrix_multiply (&t, &s, &tp);
300     ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
303 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
305     SPSymbol *symbol = SP_SYMBOL(item);
306     if (!SP_OBJECT_IS_CLONED (symbol))
307         return;
309     /* Cloned <symbol> is actually renderable */
310     ctx->pushState();
311     ctx->transform(&symbol->c2p);
313     // apply viewbox if set
314     if (0 /*symbol->viewBox_set*/) {
315         NRMatrix vb2user;
316         double x, y, width, height;
317         double view_width, view_height;
318         x = 0.0;
319         y = 0.0;
320         width = 1.0;
321         height = 1.0;
323         view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
324         view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
326         calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
327                                      &x, &y,&width, &height);
329         // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
330         nr_matrix_set_identity(&vb2user);
331         vb2user[0] = width / view_width;
332         vb2user[3] = height / view_height;
333         vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
334         vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
336         ctx->transform(&vb2user);
337     }
339     sp_group_render(item, ctx);
340     ctx->popState();
343 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
345     SPRoot *root = SP_ROOT(item);
346     CairoRenderer *renderer = ctx->getRenderer();
348     if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
349         ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
351     ctx->pushState();
352     renderer->setStateForItem(ctx, item);
353     ctx->transform(root->c2p);
354     sp_group_render(item, ctx);
355     ctx->popState();
358 /**
359     this function convert the item to a raster image and include the raster into the cairo renderer
360 */
361 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
363     g_warning("render as bitmap");
365     //the code now was copied from sp_selection_create_bitmap_copy
367     SPDocument *document = SP_OBJECT(item)->document;
368     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
370     // Get the bounding box of the selection
371     //NR::Maybe<NR::Rect> _bbox = item->getBounds(sp_item_i2d_affine(item));
372     // NRRect bbox = item->getBounds(sp_item_i2d_affine(item));
373     NRRect bbox(item->getBounds(sp_item_i2d_affine(item)));
376     // List of the items to show; all others will be hidden
377     GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
378     items = g_slist_append(items, item);
380     // Generate a random value from the current time (you may create bitmap from the same object(s)
381     // multiple times, and this is done so that they don't clash)
382     GTimeVal cu;
383     g_get_current_time (&cu);
384     guint current = (int) (cu.tv_sec * 1000000 + cu.tv_usec) % 1024;
386     // Create the filename
387     gchar *filename = g_strdup_printf ("%s-%s-%u.png", document->name, SP_OBJECT_REPR(items->data)->attribute("id"), current);
388     // Imagemagick is known not to handle spaces in filenames, so we replace anything but letters,
389     // digits, and a few other chars, with "_"
390     filename = g_strcanon (filename, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.=+~$#@^&!?", '_');
391     // Build the complete path by adding document->base if set
392     gchar *filepath = g_build_filename (document->base?document->base:"", filename, NULL);
394     //g_print ("%s\n", filepath);
396     // Remember parent and z-order of the topmost one
397     gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
398     SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
399     Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
401     // Calculate resolution
402     double res;
403     /** @TODO reimplement the resolution stuff
404     */
405     res = PX_PER_IN;
407     // The width and height of the bitmap in pixels
408     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
409     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
411     // Find out if we have to run a filter
412     gchar const *run = NULL;
413     gchar const *filter = NULL;
414     /** @TODO reimplement the filter stuff
415     //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
416     if (filter) {
417         // filter command is given;
418         // see if we have a parameter to pass to it
419         gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
420         if (param1) {
421             if (param1[strlen(param1) - 1] == '%') {
422                 // if the param string ends with %, interpret it as a percentage of the image's max dimension
423                 gchar p1[256];
424                 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
425                 // the first param is always the image filename, the second is param1
426                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
427             } else {
428                 // otherwise pass the param1 unchanged
429                 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
430             }
431         } else {
432             // run without extra parameter
433             run = g_strdup_printf ("%s \"%s\"", filter, filepath);
434         }
435     }
436     */
437     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
438     NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
439     NR::Matrix t;
441     double shift_x = bbox.x0;
442     double shift_y = bbox.y1;
443     if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
444         shift_x = round (shift_x);
445         shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
446     }
447     t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y) * eek.inverse()));
449     // Do the export
450     GdkPixbuf *pb = sp_generate_internal_bitmap(document, filepath,
451         bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
453     /*sp_export_png_file(document, filepath,
454                    bbox.x0, bbox.y0, bbox.x1, bbox.y1,
455                    width, height, res, res,
456                    (guint32) 0xffffff00,
457                    NULL, NULL,
458                    true,  //bool force_overwrite,
459                    items);
460     */
461     // Run filter, if any
462     /*
463     if (run) {
464         g_print ("Running external filter: %s\n", run);
465         system (run);
466     }
467     */
468     // Import the image back
469     //GdkPixbuf *pb = gdk_pixbuf_new_from_file (filepath, NULL);
470     if (pb) {
471         unsigned char *px = gdk_pixbuf_get_pixels (pb);
472         unsigned int w = gdk_pixbuf_get_width(pb);
473         unsigned int h = gdk_pixbuf_get_height(pb);
474         unsigned int rs = gdk_pixbuf_get_rowstride(pb);
475         NRMatrix matrix = t;
476         //nr_matrix_set_identity(&matrix);
478         ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
479     /*
480         // Create the repr for the image
481         Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
482         repr->setAttribute("xlink:href", filename);
483         repr->setAttribute("sodipodi:absref", filepath);
484         if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
485             sp_repr_set_svg_double(repr, "width", width);
486             sp_repr_set_svg_double(repr, "height", height);
487         } else {
488             sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
489             sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
490         }
492         // Write transform
493         gchar *c=sp_svg_transform_write(t);
494         repr->setAttribute("transform", c);
495         g_free(c);
497         // add the new repr to the parent
498         parent->appendChild(repr);
500         // move to the saved position
501         repr->setPosition(pos > 0 ? pos + 1 : 1);
503         // Clean up
504         Inkscape::GC::release(repr);
505     */
506         gdk_pixbuf_unref (pb);
508         // Complete undoable transaction
509         // sp_document_done (document, SP_VERB_SELECTION_CREATE_BITMAP, _("Create bitmap"));
510     }
511     g_slist_free (items);
513     g_free (filename);
514     g_free (filepath);
517 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
519     /*
520     if(ctx->_state->has_filtereffect)
521         printf("\nhas filtereffects");
522     */
524     if (SP_IS_ROOT(item)) {
525         TRACE(("root\n"));
526         return sp_root_render(item, ctx);
527     } else if (SP_IS_GROUP(item)) {
528         TRACE(("group\n"));
529         return sp_group_render(item, ctx);
530     } else if (SP_IS_SHAPE(item)) {
531         TRACE(("shape\n"));
532         return sp_shape_render(item, ctx);
533     } else if (SP_IS_USE(item)) {
534         TRACE(("use begin---\n"));
535         sp_use_render(item, ctx);
536         TRACE(("---use end\n"));
537     } else if (SP_IS_SYMBOL(item)) {
538         TRACE(("symbol\n"));
539         return sp_symbol_render(item, ctx);
540     } else if (SP_IS_TEXT(item)) {
541         TRACE(("text\n"));
542         return sp_text_render(item, ctx);
543     } else if (SP_IS_FLOWTEXT(item)) {
544         TRACE(("flowtext\n"));
545         return sp_flowtext_render(item, ctx);
546     } else if (SP_IS_IMAGE(item)) {
547         TRACE(("image\n"));
548         return sp_image_render(item, ctx);
549     }
552 void
553 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
555     SPStyle const *style = SP_OBJECT_STYLE(item);
556     ctx->setStateForStyle(style);
558     CairoRenderState *state = ctx->getCurrentState();
559     state->clip_path = item->clip_ref->getObject();
560     state->mask = item->mask_ref->getObject();
562     // If parent_has_userspace is true the parent state's transform
563     // has to be used for the mask's/clippath's context.
564     // This is so because we use the image's/(flow)text's transform for positioning
565     // instead of explicitly specifying it and letting the renderer do the
566     // transformation before rendering the item.
567     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
568         state->parent_has_userspace = TRUE;
569     TRACE(("set op: %f\n", state->opacity));
572 void
573 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
575     ctx->pushState();
576     setStateForItem(ctx, item);
578     CairoRenderState *state = ctx->getCurrentState();
579     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
581     if (state->need_layer) {
582         state->merge_opacity = FALSE;
583         ctx->pushLayer();
584     }
585     ctx->transform(item->transform);
586     sp_item_invoke_render(item, ctx);
588     if (state->need_layer)
589         ctx->popLayer();
591     ctx->popState();
594 bool
595 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
597     g_assert( ctx != NULL );
599     if (ctx->_vector_based_target) {
600         // width and height in pt
601         ctx->_width = sp_document_width(doc) * PT_PER_PX;
602         ctx->_height = sp_document_height(doc) * PT_PER_PX;
603     } else {
604         ctx->_width = sp_document_width(doc);
605         ctx->_height = sp_document_height(doc);
606     }
608     NRRect d;
609     bool pageBoundingBox = true;
610     if (pageBoundingBox) {
611         d.x0 = d.y0 = 0;
612         d.x1 = ceil(ctx->_width);
613         d.y1 = ceil(ctx->_height);
614     } else {
615         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
616         sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
617         if (ctx->_vector_based_target) {
618             // convert from px to pt
619             d.x0 *= PT_PER_PX;
620             d.x1 *= PT_PER_PX;
621             d.y0 *= PT_PER_PX;
622             d.y1 *= PT_PER_PX;
623         }
624     }
625     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
627     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
630 #include "macros.h" // SP_PRINT_*
632 void
633 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
635     g_assert( ctx != NULL && ctx->_is_valid );
637     if (cp == NULL)
638         return;
640     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
641     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
643     NRMatrix saved_ctm;
644     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
645         NRMatrix t;
646         //SP_PRINT_DRECT("clipd", cp->display->bbox);
647         NRRect clip_bbox(cp->display->bbox);
648         nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
649         t.c[4] = clip_bbox.x0;
650         t.c[5] = clip_bbox.y0;
651         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
652         ctx->getTransform(&saved_ctm);
653         ctx->setTransform(&t);
654     }
656     TRACE(("BEGIN clip\n"));
657     SPObject *co = SP_OBJECT(cp);
658     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
659         if (SP_IS_ITEM(child)) {
660             SPItem *item = SP_ITEM(child);
661             renderItem(ctx, item);
662         }
663     }
664     TRACE(("END clip\n"));
666     // do clipping only if this was the first call to applyClipPath
667     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
668         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
669         cairo_clip(ctx->_cr);
671     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
672         ctx->setTransform(&saved_ctm);
674     ctx->setRenderMode(saved_mode);
677 void
678 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
680     g_assert( ctx != NULL && ctx->_is_valid );
682     if (mask == NULL)
683         return;
685     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
686     NRRect mask_bbox(mask->display->bbox);
687     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
688     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
689         NRMatrix t;
690         nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
691         t.c[4] = mask_bbox.x0;
692         t.c[5] = mask_bbox.y0;
693         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
694         ctx->setTransform(&t);
695     }
697     // clip mask contents
698     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
700     ctx->pushState();
702     TRACE(("BEGIN mask\n"));
703     SPObject *co = SP_OBJECT(mask);
704     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
705         if (SP_IS_ITEM(child)) {
706             SPItem *item = SP_ITEM(child);
707             renderItem(ctx, item);
708         }
709     }
710     TRACE(("END mask\n"));
712     ctx->popState();
715 void
716 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
717                              double *x, double *y, double *width, double *height)
719     if (aspect_align == SP_ASPECT_NONE)
720         return;
722     double scalex, scaley, scale;
723     double new_width, new_height;
724     scalex = *width / vp_width;
725     scaley = *height / vp_height;
726     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
727     new_width = vp_width * scale;
728     new_height = vp_height * scale;
729     /* Now place viewbox to requested position */
730     switch (aspect_align) {
731         case SP_ASPECT_XMIN_YMIN:
732             break;
733         case SP_ASPECT_XMID_YMIN:
734             *x -= 0.5 * (new_width - *width);
735             break;
736         case SP_ASPECT_XMAX_YMIN:
737             *x -= 1.0 * (new_width - *width);
738             break;
739         case SP_ASPECT_XMIN_YMID:
740             *y -= 0.5 * (new_height - *height);
741             break;
742         case SP_ASPECT_XMID_YMID:
743             *x -= 0.5 * (new_width - *width);
744             *y -= 0.5 * (new_height - *height);
745             break;
746         case SP_ASPECT_XMAX_YMID:
747             *x -= 1.0 * (new_width - *width);
748             *y -= 0.5 * (new_height - *height);
749             break;
750         case SP_ASPECT_XMIN_YMAX:
751             *y -= 1.0 * (new_height - *height);
752             break;
753         case SP_ASPECT_XMID_YMAX:
754             *x -= 0.5 * (new_width - *width);
755             *y -= 1.0 * (new_height - *height);
756             break;
757         case SP_ASPECT_XMAX_YMAX:
758             *x -= 1.0 * (new_width - *width);
759             *y -= 1.0 * (new_height - *height);
760             break;
761         default:
762             break;
763     }
764     *width = new_width;
765     *height = new_height;
768 #include "clear-n_.h"
770 }  /* namespace Internal */
771 }  /* namespace Extension */
772 }  /* namespace Inkscape */
774 #undef TRACE
776 /* End of GNU GPL code */
778 /*
779   Local Variables:
780   mode:c++
781   c-file-style:"stroustrup"
782   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
783   indent-tabs-mode:nil
784   fill-column:99
785   End:
786 */
787 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :