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 state->transform.set_identity();
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 const_NRBPath bp;
182 bp.path = SP_CURVE_BPATH(shape->curve);
184 ctx->renderPath(&bp, style, &pbox);
186 for (NArtBpath const* 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 SPUse *use = SP_USE(item);
230 CairoRenderer *renderer = ctx->getRenderer();
232 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
233 NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
234 ctx->pushState();
235 ctx->transform(&tp);
236 translated = true;
237 }
239 if (use->child && SP_IS_ITEM(use->child)) {
240 renderer->renderItem(ctx, SP_ITEM(use->child));
241 }
243 if (translated) {
244 ctx->popState();
245 }
246 }
248 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
249 {
250 SPText *group = SP_TEXT (item);
251 group->layout.showGlyphs(ctx);
252 }
254 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
255 {
256 SPFlowtext *group = SP_FLOWTEXT(item);
257 group->layout.showGlyphs(ctx);
258 }
260 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
261 {
262 SPImage *image;
263 guchar *px;
264 int w, h, rs;
266 image = SP_IMAGE (item);
268 if (!image->pixbuf) return;
269 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
271 px = gdk_pixbuf_get_pixels (image->pixbuf);
272 w = gdk_pixbuf_get_width (image->pixbuf);
273 h = gdk_pixbuf_get_height (image->pixbuf);
274 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
276 double x = image->x.computed;
277 double y = image->y.computed;
278 double width = image->width.computed;
279 double height = image->height.computed;
281 if (image->aspect_align != SP_ASPECT_NONE) {
282 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
283 &x, &y, &width, &height);
284 }
286 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
287 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
288 }
290 NR::translate tp(x, y);
291 NR::scale s(width / (double)w, height / (double)h);
292 NR::Matrix t(s * tp);
294 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
295 }
297 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
298 {
299 SPSymbol *symbol = SP_SYMBOL(item);
300 if (!SP_OBJECT_IS_CLONED (symbol))
301 return;
303 /* Cloned <symbol> is actually renderable */
304 ctx->pushState();
305 ctx->transform(&symbol->c2p);
307 // apply viewbox if set
308 if (0 /*symbol->viewBox_set*/) {
309 NR::Matrix vb2user;
310 double x, y, width, height;
311 double view_width, view_height;
312 x = 0.0;
313 y = 0.0;
314 width = 1.0;
315 height = 1.0;
317 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
318 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
320 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
321 &x, &y,&width, &height);
323 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
324 vb2user.set_identity();
325 vb2user[0] = width / view_width;
326 vb2user[3] = height / view_height;
327 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
328 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
330 ctx->transform(&vb2user);
331 }
333 sp_group_render(item, ctx);
334 ctx->popState();
335 }
337 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
338 {
339 SPRoot *root = SP_ROOT(item);
340 CairoRenderer *renderer = ctx->getRenderer();
342 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
343 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
345 ctx->pushState();
346 renderer->setStateForItem(ctx, item);
347 ctx->transform(&root->c2p);
348 sp_group_render(item, ctx);
349 ctx->popState();
350 }
352 /**
353 this function convert the item to a raster image and include the raster into the cairo renderer
354 */
355 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
356 {
357 g_warning("render as bitmap");
359 //the code now was copied from sp_selection_create_bitmap_copy
361 SPDocument *document = SP_OBJECT(item)->document;
362 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
364 // Get the bounding box of the selection
365 //NR::Maybe<NR::Rect> _bbox = item->getBounds(sp_item_i2d_affine(item));
366 // NRRect bbox = item->getBounds(sp_item_i2d_affine(item));
367 NRRect bbox(item->getBounds(sp_item_i2d_affine(item)));
370 // List of the items to show; all others will be hidden
371 GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
372 items = g_slist_append(items, item);
374 // Remember parent and z-order of the topmost one
375 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
376 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
377 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
379 // Calculate resolution
380 double res;
381 /** @TODO reimplement the resolution stuff
382 */
383 res = ctx->getBitmapResolution();
384 if(res == 0) {
385 res = PX_PER_IN;
386 }
389 // The width and height of the bitmap in pixels
390 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
391 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
393 // Find out if we have to run a filter
394 gchar const *run = NULL;
395 gchar const *filter = NULL;
396 /** @TODO reimplement the filter stuff
397 //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
398 if (filter) {
399 // filter command is given;
400 // see if we have a parameter to pass to it
401 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
402 if (param1) {
403 if (param1[strlen(param1) - 1] == '%') {
404 // if the param string ends with %, interpret it as a percentage of the image's max dimension
405 gchar p1[256];
406 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
407 // the first param is always the image filename, the second is param1
408 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
409 } else {
410 // otherwise pass the param1 unchanged
411 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
412 }
413 } else {
414 // run without extra parameter
415 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
416 }
417 }
418 */
419 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
420 NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
421 NR::Matrix t;
423 double shift_x = bbox.x0;
424 double shift_y = bbox.y1;
425 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
426 shift_x = round (shift_x);
427 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
428 }
429 t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
431 //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
433 // Do the export
434 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
435 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
437 // Run filter, if any
438 /*
439 if (run) {
440 g_print ("Running external filter: %s\n", run);
441 system (run);
442 }
443 */
444 if (pb) {
445 unsigned char *px = gdk_pixbuf_get_pixels (pb);
446 unsigned int w = gdk_pixbuf_get_width(pb);
447 unsigned int h = gdk_pixbuf_get_height(pb);
448 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
449 NR::Matrix matrix;
450 matrix = t;
451 //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
452 //matrix.set_identity();
454 ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
455 /*
456 // Create the repr for the image
457 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
458 repr->setAttribute("xlink:href", filename);
459 repr->setAttribute("sodipodi:absref", filepath);
460 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
461 sp_repr_set_svg_double(repr, "width", width);
462 sp_repr_set_svg_double(repr, "height", height);
463 } else {
464 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
465 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
466 }
468 // Write transform
469 gchar *c=sp_svg_transform_write(t);
470 repr->setAttribute("transform", c);
471 g_free(c);
473 // add the new repr to the parent
474 parent->appendChild(repr);
476 // move to the saved position
477 repr->setPosition(pos > 0 ? pos + 1 : 1);
478 */
479 gdk_pixbuf_unref (pb);
480 }
481 g_slist_free (items);
482 }
484 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
485 {
486 // Check item's visibility
487 if (item->isHidden()) {
488 return;
489 }
491 SPStyle* style = SP_OBJECT_STYLE (item);
492 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
493 return sp_asbitmap_render(item, ctx);
494 }
496 if (SP_IS_ROOT(item)) {
497 TRACE(("root\n"));
498 return sp_root_render(item, ctx);
499 } else if (SP_IS_GROUP(item)) {
500 TRACE(("group\n"));
501 return sp_group_render(item, ctx);
502 } else if (SP_IS_SHAPE(item)) {
503 TRACE(("shape\n"));
504 return sp_shape_render(item, ctx);
505 } else if (SP_IS_USE(item)) {
506 TRACE(("use begin---\n"));
507 sp_use_render(item, ctx);
508 TRACE(("---use end\n"));
509 } else if (SP_IS_SYMBOL(item)) {
510 TRACE(("symbol\n"));
511 return sp_symbol_render(item, ctx);
512 } else if (SP_IS_TEXT(item)) {
513 TRACE(("text\n"));
514 return sp_text_render(item, ctx);
515 } else if (SP_IS_FLOWTEXT(item)) {
516 TRACE(("flowtext\n"));
517 return sp_flowtext_render(item, ctx);
518 } else if (SP_IS_IMAGE(item)) {
519 TRACE(("image\n"));
520 return sp_image_render(item, ctx);
521 }
522 }
524 void
525 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
526 {
527 SPStyle const *style = SP_OBJECT_STYLE(item);
528 ctx->setStateForStyle(style);
530 CairoRenderState *state = ctx->getCurrentState();
531 state->clip_path = item->clip_ref->getObject();
532 state->mask = item->mask_ref->getObject();
534 // If parent_has_userspace is true the parent state's transform
535 // has to be used for the mask's/clippath's context.
536 // This is so because we use the image's/(flow)text's transform for positioning
537 // instead of explicitly specifying it and letting the renderer do the
538 // transformation before rendering the item.
539 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
540 state->parent_has_userspace = TRUE;
541 TRACE(("set op: %f\n", state->opacity));
542 }
544 void
545 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
546 {
547 ctx->pushState();
548 setStateForItem(ctx, item);
550 CairoRenderState *state = ctx->getCurrentState();
551 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
553 if (state->need_layer) {
554 state->merge_opacity = FALSE;
555 ctx->pushLayer();
556 }
557 ctx->transform(&item->transform);
558 sp_item_invoke_render(item, ctx);
560 if (state->need_layer)
561 ctx->popLayer();
563 ctx->popState();
564 }
566 bool
567 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
568 {
569 g_assert( ctx != NULL );
571 if (ctx->_vector_based_target) {
572 // width and height in pt
573 ctx->_width = sp_document_width(doc) * PT_PER_PX;
574 ctx->_height = sp_document_height(doc) * PT_PER_PX;
575 } else {
576 ctx->_width = sp_document_width(doc);
577 ctx->_height = sp_document_height(doc);
578 }
580 NRRect d;
581 bool pageBoundingBox = true;
582 if (pageBoundingBox) {
583 d.x0 = d.y0 = 0;
584 d.x1 = ceil(ctx->_width);
585 d.y1 = ceil(ctx->_height);
586 } else {
587 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
588 sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
589 if (ctx->_vector_based_target) {
590 // convert from px to pt
591 d.x0 *= PT_PER_PX;
592 d.x1 *= PT_PER_PX;
593 d.y0 *= PT_PER_PX;
594 d.y1 *= PT_PER_PX;
595 }
596 }
597 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
599 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
600 }
602 #include "macros.h" // SP_PRINT_*
604 void
605 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
606 {
607 g_assert( ctx != NULL && ctx->_is_valid );
609 if (cp == NULL)
610 return;
612 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
613 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
615 NR::Matrix saved_ctm;
616 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
617 //SP_PRINT_DRECT("clipd", cp->display->bbox);
618 NRRect clip_bbox(cp->display->bbox);
619 NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
620 t[4] = clip_bbox.x0;
621 t[5] = clip_bbox.y0;
622 t *= ctx->getCurrentState()->transform;
623 ctx->getTransform(&saved_ctm);
624 ctx->setTransform(&t);
625 }
627 TRACE(("BEGIN clip\n"));
628 SPObject *co = SP_OBJECT(cp);
629 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
630 if (SP_IS_ITEM(child)) {
631 SPItem *item = SP_ITEM(child);
632 renderItem(ctx, item);
633 }
634 }
635 TRACE(("END clip\n"));
637 // do clipping only if this was the first call to applyClipPath
638 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
639 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
640 cairo_clip(ctx->_cr);
642 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
643 ctx->setTransform(&saved_ctm);
645 ctx->setRenderMode(saved_mode);
646 }
648 void
649 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
650 {
651 g_assert( ctx != NULL && ctx->_is_valid );
653 if (mask == NULL)
654 return;
656 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
657 NRRect mask_bbox(mask->display->bbox);
658 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
659 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
660 NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
661 t[4] = mask_bbox.x0;
662 t[5] = mask_bbox.y0;
663 t *= ctx->getCurrentState()->transform;
664 ctx->setTransform(&t);
665 }
667 // clip mask contents
668 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
670 ctx->pushState();
672 TRACE(("BEGIN mask\n"));
673 SPObject *co = SP_OBJECT(mask);
674 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
675 if (SP_IS_ITEM(child)) {
676 SPItem *item = SP_ITEM(child);
677 renderItem(ctx, item);
678 }
679 }
680 TRACE(("END mask\n"));
682 ctx->popState();
683 }
685 void
686 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
687 double *x, double *y, double *width, double *height)
688 {
689 if (aspect_align == SP_ASPECT_NONE)
690 return;
692 double scalex, scaley, scale;
693 double new_width, new_height;
694 scalex = *width / vp_width;
695 scaley = *height / vp_height;
696 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
697 new_width = vp_width * scale;
698 new_height = vp_height * scale;
699 /* Now place viewbox to requested position */
700 switch (aspect_align) {
701 case SP_ASPECT_XMIN_YMIN:
702 break;
703 case SP_ASPECT_XMID_YMIN:
704 *x -= 0.5 * (new_width - *width);
705 break;
706 case SP_ASPECT_XMAX_YMIN:
707 *x -= 1.0 * (new_width - *width);
708 break;
709 case SP_ASPECT_XMIN_YMID:
710 *y -= 0.5 * (new_height - *height);
711 break;
712 case SP_ASPECT_XMID_YMID:
713 *x -= 0.5 * (new_width - *width);
714 *y -= 0.5 * (new_height - *height);
715 break;
716 case SP_ASPECT_XMAX_YMID:
717 *x -= 1.0 * (new_width - *width);
718 *y -= 0.5 * (new_height - *height);
719 break;
720 case SP_ASPECT_XMIN_YMAX:
721 *y -= 1.0 * (new_height - *height);
722 break;
723 case SP_ASPECT_XMID_YMAX:
724 *x -= 0.5 * (new_width - *width);
725 *y -= 1.0 * (new_height - *height);
726 break;
727 case SP_ASPECT_XMAX_YMAX:
728 *x -= 1.0 * (new_width - *width);
729 *y -= 1.0 * (new_height - *height);
730 break;
731 default:
732 break;
733 }
734 *width = new_width;
735 *height = new_height;
736 }
738 #include "clear-n_.h"
740 } /* namespace Internal */
741 } /* namespace Extension */
742 } /* namespace Inkscape */
744 #undef TRACE
746 /* End of GNU GPL code */
748 /*
749 Local Variables:
750 mode:c++
751 c-file-style:"stroustrup"
752 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
753 indent-tabs-mode:nil
754 fill-column:99
755 End:
756 */
757 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :