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)
116 {
117 /* restore default signal handling for SIGPIPE */
118 #if !defined(_WIN32) && !defined(__WIN32__)
119 (void) signal(SIGPIPE, SIG_DFL);
120 #endif
122 return;
123 }
125 CairoRenderContext*
126 CairoRenderer::createContext(void)
127 {
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;
141 }
143 void
144 CairoRenderer::destroyContext(CairoRenderContext *ctx)
145 {
146 delete ctx;
147 }
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)
167 {
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 }
208 }
210 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
211 {
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 }
224 }
226 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
227 {
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 }
247 }
249 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
250 {
251 SPText *group = SP_TEXT (item);
252 group->layout.showGlyphs(ctx);
253 }
255 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
256 {
257 SPFlowtext *group = SP_FLOWTEXT(item);
258 group->layout.showGlyphs(ctx);
259 }
261 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
262 {
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));
297 }
299 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
300 {
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();
337 }
339 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
340 {
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();
352 }
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)
358 {
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);
484 }
486 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
487 {
488 SPStyle* style = SP_OBJECT_STYLE (item);
489 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
490 return sp_asbitmap_render(item, ctx);
491 }
493 if (SP_IS_ROOT(item)) {
494 TRACE(("root\n"));
495 return sp_root_render(item, ctx);
496 } else if (SP_IS_GROUP(item)) {
497 TRACE(("group\n"));
498 return sp_group_render(item, ctx);
499 } else if (SP_IS_SHAPE(item)) {
500 TRACE(("shape\n"));
501 return sp_shape_render(item, ctx);
502 } else if (SP_IS_USE(item)) {
503 TRACE(("use begin---\n"));
504 sp_use_render(item, ctx);
505 TRACE(("---use end\n"));
506 } else if (SP_IS_SYMBOL(item)) {
507 TRACE(("symbol\n"));
508 return sp_symbol_render(item, ctx);
509 } else if (SP_IS_TEXT(item)) {
510 TRACE(("text\n"));
511 return sp_text_render(item, ctx);
512 } else if (SP_IS_FLOWTEXT(item)) {
513 TRACE(("flowtext\n"));
514 return sp_flowtext_render(item, ctx);
515 } else if (SP_IS_IMAGE(item)) {
516 TRACE(("image\n"));
517 return sp_image_render(item, ctx);
518 }
519 }
521 void
522 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
523 {
524 SPStyle const *style = SP_OBJECT_STYLE(item);
525 ctx->setStateForStyle(style);
527 CairoRenderState *state = ctx->getCurrentState();
528 state->clip_path = item->clip_ref->getObject();
529 state->mask = item->mask_ref->getObject();
531 // If parent_has_userspace is true the parent state's transform
532 // has to be used for the mask's/clippath's context.
533 // This is so because we use the image's/(flow)text's transform for positioning
534 // instead of explicitly specifying it and letting the renderer do the
535 // transformation before rendering the item.
536 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
537 state->parent_has_userspace = TRUE;
538 TRACE(("set op: %f\n", state->opacity));
539 }
541 void
542 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
543 {
544 ctx->pushState();
545 setStateForItem(ctx, item);
547 CairoRenderState *state = ctx->getCurrentState();
548 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
550 if (state->need_layer) {
551 state->merge_opacity = FALSE;
552 ctx->pushLayer();
553 }
554 ctx->transform(item->transform);
555 sp_item_invoke_render(item, ctx);
557 if (state->need_layer)
558 ctx->popLayer();
560 ctx->popState();
561 }
563 bool
564 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
565 {
566 g_assert( ctx != NULL );
568 if (ctx->_vector_based_target) {
569 // width and height in pt
570 ctx->_width = sp_document_width(doc) * PT_PER_PX;
571 ctx->_height = sp_document_height(doc) * PT_PER_PX;
572 } else {
573 ctx->_width = sp_document_width(doc);
574 ctx->_height = sp_document_height(doc);
575 }
577 NRRect d;
578 bool pageBoundingBox = true;
579 if (pageBoundingBox) {
580 d.x0 = d.y0 = 0;
581 d.x1 = ceil(ctx->_width);
582 d.y1 = ceil(ctx->_height);
583 } else {
584 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
585 sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
586 if (ctx->_vector_based_target) {
587 // convert from px to pt
588 d.x0 *= PT_PER_PX;
589 d.x1 *= PT_PER_PX;
590 d.y0 *= PT_PER_PX;
591 d.y1 *= PT_PER_PX;
592 }
593 }
594 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
596 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
597 }
599 #include "macros.h" // SP_PRINT_*
601 void
602 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
603 {
604 g_assert( ctx != NULL && ctx->_is_valid );
606 if (cp == NULL)
607 return;
609 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
610 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
612 NRMatrix saved_ctm;
613 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
614 NRMatrix t;
615 //SP_PRINT_DRECT("clipd", cp->display->bbox);
616 NRRect clip_bbox(cp->display->bbox);
617 nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
618 t.c[4] = clip_bbox.x0;
619 t.c[5] = clip_bbox.y0;
620 nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
621 ctx->getTransform(&saved_ctm);
622 ctx->setTransform(&t);
623 }
625 TRACE(("BEGIN clip\n"));
626 SPObject *co = SP_OBJECT(cp);
627 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
628 if (SP_IS_ITEM(child)) {
629 SPItem *item = SP_ITEM(child);
630 renderItem(ctx, item);
631 }
632 }
633 TRACE(("END clip\n"));
635 // do clipping only if this was the first call to applyClipPath
636 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
637 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
638 cairo_clip(ctx->_cr);
640 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
641 ctx->setTransform(&saved_ctm);
643 ctx->setRenderMode(saved_mode);
644 }
646 void
647 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
648 {
649 g_assert( ctx != NULL && ctx->_is_valid );
651 if (mask == NULL)
652 return;
654 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
655 NRRect mask_bbox(mask->display->bbox);
656 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
657 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
658 NRMatrix t;
659 nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
660 t.c[4] = mask_bbox.x0;
661 t.c[5] = mask_bbox.y0;
662 nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
663 ctx->setTransform(&t);
664 }
666 // clip mask contents
667 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
669 ctx->pushState();
671 TRACE(("BEGIN mask\n"));
672 SPObject *co = SP_OBJECT(mask);
673 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
674 if (SP_IS_ITEM(child)) {
675 SPItem *item = SP_ITEM(child);
676 renderItem(ctx, item);
677 }
678 }
679 TRACE(("END mask\n"));
681 ctx->popState();
682 }
684 void
685 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
686 double *x, double *y, double *width, double *height)
687 {
688 if (aspect_align == SP_ASPECT_NONE)
689 return;
691 double scalex, scaley, scale;
692 double new_width, new_height;
693 scalex = *width / vp_width;
694 scaley = *height / vp_height;
695 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
696 new_width = vp_width * scale;
697 new_height = vp_height * scale;
698 /* Now place viewbox to requested position */
699 switch (aspect_align) {
700 case SP_ASPECT_XMIN_YMIN:
701 break;
702 case SP_ASPECT_XMID_YMIN:
703 *x -= 0.5 * (new_width - *width);
704 break;
705 case SP_ASPECT_XMAX_YMIN:
706 *x -= 1.0 * (new_width - *width);
707 break;
708 case SP_ASPECT_XMIN_YMID:
709 *y -= 0.5 * (new_height - *height);
710 break;
711 case SP_ASPECT_XMID_YMID:
712 *x -= 0.5 * (new_width - *width);
713 *y -= 0.5 * (new_height - *height);
714 break;
715 case SP_ASPECT_XMAX_YMID:
716 *x -= 1.0 * (new_width - *width);
717 *y -= 0.5 * (new_height - *height);
718 break;
719 case SP_ASPECT_XMIN_YMAX:
720 *y -= 1.0 * (new_height - *height);
721 break;
722 case SP_ASPECT_XMID_YMAX:
723 *x -= 0.5 * (new_width - *width);
724 *y -= 1.0 * (new_height - *height);
725 break;
726 case SP_ASPECT_XMAX_YMAX:
727 *x -= 1.0 * (new_width - *width);
728 *y -= 1.0 * (new_height - *height);
729 break;
730 default:
731 break;
732 }
733 *width = new_width;
734 *height = new_height;
735 }
737 #include "clear-n_.h"
739 } /* namespace Internal */
740 } /* namespace Extension */
741 } /* namespace Inkscape */
743 #undef TRACE
745 /* End of GNU GPL code */
747 /*
748 Local Variables:
749 mode:c++
750 c-file-style:"stroustrup"
751 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
752 indent-tabs-mode:nil
753 fill-column:99
754 End:
755 */
756 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :