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);
177 SPStyle* style = SP_OBJECT_STYLE (item);
178 CairoRenderer *renderer = ctx->getRenderer();
180 const_NRBPath bp;
181 bp.path = SP_CURVE_BPATH(shape->curve);
183 ctx->renderPath(&bp, style, &pbox);
185 for (NArtBpath const* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
186 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
187 if (sp_shape_marker_required (shape, m, bp)) {
189 SPMarker* marker = SP_MARKER (shape->marker[m]);
190 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
192 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
194 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
195 tr = NR::scale(style->stroke_width.computed) * tr;
196 }
198 tr = marker_item->transform * marker->c2p * tr;
200 NR::Matrix old_tr = marker_item->transform;
201 marker_item->transform = tr;
202 renderer->renderItem (ctx, marker_item);
203 marker_item->transform = old_tr;
204 }
205 }
206 }
207 }
209 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
210 {
211 SPGroup *group = SP_GROUP(item);
212 CairoRenderer *renderer = ctx->getRenderer();
213 TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
215 GSList *l = g_slist_reverse(group->childList(false));
216 while (l) {
217 SPObject *o = SP_OBJECT (l->data);
218 if (SP_IS_ITEM(o)) {
219 renderer->renderItem (ctx, SP_ITEM (o));
220 }
221 l = g_slist_remove (l, o);
222 }
223 }
225 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
226 {
227 bool translated = false;
228 SPUse *use = SP_USE(item);
229 CairoRenderer *renderer = ctx->getRenderer();
231 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
232 NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
233 ctx->pushState();
234 ctx->transform(&tp);
235 translated = true;
236 }
238 if (use->child && SP_IS_ITEM(use->child)) {
239 renderer->renderItem(ctx, SP_ITEM(use->child));
240 }
242 if (translated) {
243 ctx->popState();
244 }
245 }
247 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
248 {
249 SPText *group = SP_TEXT (item);
250 group->layout.showGlyphs(ctx);
251 }
253 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
254 {
255 SPFlowtext *group = SP_FLOWTEXT(item);
256 group->layout.showGlyphs(ctx);
257 }
259 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
260 {
261 SPImage *image;
262 guchar *px;
263 int w, h, rs;
265 image = SP_IMAGE (item);
267 if (!image->pixbuf) return;
268 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
270 px = gdk_pixbuf_get_pixels (image->pixbuf);
271 w = gdk_pixbuf_get_width (image->pixbuf);
272 h = gdk_pixbuf_get_height (image->pixbuf);
273 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
275 double x = image->x.computed;
276 double y = image->y.computed;
277 double width = image->width.computed;
278 double height = image->height.computed;
280 if (image->aspect_align != SP_ASPECT_NONE) {
281 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
282 &x, &y, &width, &height);
283 }
285 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
286 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
287 }
289 NR::translate tp(x, y);
290 NR::scale s(width / (double)w, height / (double)h);
291 NR::Matrix t(s * tp);
293 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
294 }
296 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
297 {
298 SPSymbol *symbol = SP_SYMBOL(item);
299 if (!SP_OBJECT_IS_CLONED (symbol))
300 return;
302 /* Cloned <symbol> is actually renderable */
303 ctx->pushState();
304 ctx->transform(&symbol->c2p);
306 // apply viewbox if set
307 if (0 /*symbol->viewBox_set*/) {
308 NR::Matrix vb2user;
309 double x, y, width, height;
310 double view_width, view_height;
311 x = 0.0;
312 y = 0.0;
313 width = 1.0;
314 height = 1.0;
316 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
317 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
319 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
320 &x, &y,&width, &height);
322 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
323 vb2user.set_identity();
324 vb2user[0] = width / view_width;
325 vb2user[3] = height / view_height;
326 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
327 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
329 ctx->transform(&vb2user);
330 }
332 sp_group_render(item, ctx);
333 ctx->popState();
334 }
336 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
337 {
338 SPRoot *root = SP_ROOT(item);
339 CairoRenderer *renderer = ctx->getRenderer();
341 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
342 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
344 ctx->pushState();
345 renderer->setStateForItem(ctx, item);
346 ctx->transform(&root->c2p);
347 sp_group_render(item, ctx);
348 ctx->popState();
349 }
351 /**
352 this function convert the item to a raster image and include the raster into the cairo renderer
353 */
354 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
355 {
356 g_warning("render as bitmap");
358 //the code now was copied from sp_selection_create_bitmap_copy
360 SPDocument *document = SP_OBJECT(item)->document;
361 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
363 // Get the bounding box of the selection
364 //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
365 // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
366 NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
369 // List of the items to show; all others will be hidden
370 GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
371 items = g_slist_append(items, item);
373 // Remember parent and z-order of the topmost one
374 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
375 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
376 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
378 // Calculate resolution
379 double res;
380 /** @TODO reimplement the resolution stuff
381 */
382 res = ctx->getBitmapResolution();
383 if(res == 0) {
384 res = PX_PER_IN;
385 }
388 // The width and height of the bitmap in pixels
389 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
390 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
392 // Find out if we have to run a filter
393 gchar const *run = NULL;
394 gchar const *filter = NULL;
395 /** @TODO reimplement the filter stuff
396 //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
397 if (filter) {
398 // filter command is given;
399 // see if we have a parameter to pass to it
400 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
401 if (param1) {
402 if (param1[strlen(param1) - 1] == '%') {
403 // if the param string ends with %, interpret it as a percentage of the image's max dimension
404 gchar p1[256];
405 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
406 // the first param is always the image filename, the second is param1
407 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
408 } else {
409 // otherwise pass the param1 unchanged
410 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
411 }
412 } else {
413 // run without extra parameter
414 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
415 }
416 }
417 */
418 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
419 NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
420 NR::Matrix t;
422 double shift_x = bbox.x0;
423 double shift_y = bbox.y1;
424 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
425 shift_x = round (shift_x);
426 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
427 }
428 t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
430 //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
432 // Do the export
433 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
434 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
436 // Run filter, if any
437 /*
438 if (run) {
439 g_print ("Running external filter: %s\n", run);
440 system (run);
441 }
442 */
443 if (pb) {
444 unsigned char *px = gdk_pixbuf_get_pixels (pb);
445 unsigned int w = gdk_pixbuf_get_width(pb);
446 unsigned int h = gdk_pixbuf_get_height(pb);
447 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
448 NR::Matrix matrix;
449 matrix = t;
450 //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
451 //matrix.set_identity();
453 ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
454 /*
455 // Create the repr for the image
456 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
457 repr->setAttribute("xlink:href", filename);
458 repr->setAttribute("sodipodi:absref", filepath);
459 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
460 sp_repr_set_svg_double(repr, "width", width);
461 sp_repr_set_svg_double(repr, "height", height);
462 } else {
463 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
464 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
465 }
467 // Write transform
468 gchar *c=sp_svg_transform_write(t);
469 repr->setAttribute("transform", c);
470 g_free(c);
472 // add the new repr to the parent
473 parent->appendChild(repr);
475 // move to the saved position
476 repr->setPosition(pos > 0 ? pos + 1 : 1);
477 */
478 gdk_pixbuf_unref (pb);
479 }
480 g_slist_free (items);
481 }
483 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
484 {
485 // Check item's visibility
486 if (item->isHidden()) {
487 return;
488 }
490 SPStyle* style = SP_OBJECT_STYLE (item);
491 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
492 return sp_asbitmap_render(item, ctx);
493 }
495 if (SP_IS_ROOT(item)) {
496 TRACE(("root\n"));
497 return sp_root_render(item, ctx);
498 } else if (SP_IS_GROUP(item)) {
499 TRACE(("group\n"));
500 return sp_group_render(item, ctx);
501 } else if (SP_IS_SHAPE(item)) {
502 TRACE(("shape\n"));
503 return sp_shape_render(item, ctx);
504 } else if (SP_IS_USE(item)) {
505 TRACE(("use begin---\n"));
506 sp_use_render(item, ctx);
507 TRACE(("---use end\n"));
508 } else if (SP_IS_SYMBOL(item)) {
509 TRACE(("symbol\n"));
510 return sp_symbol_render(item, ctx);
511 } else if (SP_IS_TEXT(item)) {
512 TRACE(("text\n"));
513 return sp_text_render(item, ctx);
514 } else if (SP_IS_FLOWTEXT(item)) {
515 TRACE(("flowtext\n"));
516 return sp_flowtext_render(item, ctx);
517 } else if (SP_IS_IMAGE(item)) {
518 TRACE(("image\n"));
519 return sp_image_render(item, ctx);
520 }
521 }
523 void
524 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
525 {
526 SPStyle const *style = SP_OBJECT_STYLE(item);
527 ctx->setStateForStyle(style);
529 CairoRenderState *state = ctx->getCurrentState();
530 state->clip_path = item->clip_ref->getObject();
531 state->mask = item->mask_ref->getObject();
533 // If parent_has_userspace is true the parent state's transform
534 // has to be used for the mask's/clippath's context.
535 // This is so because we use the image's/(flow)text's transform for positioning
536 // instead of explicitly specifying it and letting the renderer do the
537 // transformation before rendering the item.
538 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
539 state->parent_has_userspace = TRUE;
540 TRACE(("set op: %f\n", state->opacity));
541 }
543 void
544 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
545 {
546 ctx->pushState();
547 setStateForItem(ctx, item);
549 CairoRenderState *state = ctx->getCurrentState();
550 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
552 if (state->need_layer) {
553 state->merge_opacity = FALSE;
554 ctx->pushLayer();
555 }
556 ctx->transform(&item->transform);
557 sp_item_invoke_render(item, ctx);
559 if (state->need_layer)
560 ctx->popLayer();
562 ctx->popState();
563 }
565 bool
566 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
567 {
568 g_assert( ctx != NULL );
570 if (ctx->_vector_based_target) {
571 // width and height in pt
572 ctx->_width = sp_document_width(doc) * PT_PER_PX;
573 ctx->_height = sp_document_height(doc) * PT_PER_PX;
574 } else {
575 ctx->_width = sp_document_width(doc);
576 ctx->_height = sp_document_height(doc);
577 }
579 NRRect d;
580 bool pageBoundingBox = true;
581 if (pageBoundingBox) {
582 d.x0 = d.y0 = 0;
583 d.x1 = ceil(ctx->_width);
584 d.y1 = ceil(ctx->_height);
585 } else {
586 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
587 sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
588 if (ctx->_vector_based_target) {
589 // convert from px to pt
590 d.x0 *= PT_PER_PX;
591 d.x1 *= PT_PER_PX;
592 d.y0 *= PT_PER_PX;
593 d.y1 *= PT_PER_PX;
594 }
595 }
596 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
598 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
599 }
601 #include "macros.h" // SP_PRINT_*
603 void
604 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
605 {
606 g_assert( ctx != NULL && ctx->_is_valid );
608 if (cp == NULL)
609 return;
611 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
612 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
614 NR::Matrix saved_ctm;
615 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
616 //SP_PRINT_DRECT("clipd", cp->display->bbox);
617 NRRect clip_bbox(cp->display->bbox);
618 NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
619 t[4] = clip_bbox.x0;
620 t[5] = clip_bbox.y0;
621 t *= ctx->getCurrentState()->transform;
622 ctx->getTransform(&saved_ctm);
623 ctx->setTransform(&t);
624 }
626 TRACE(("BEGIN clip\n"));
627 SPObject *co = SP_OBJECT(cp);
628 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
629 if (SP_IS_ITEM(child)) {
630 SPItem *item = SP_ITEM(child);
631 renderItem(ctx, item);
632 }
633 }
634 TRACE(("END clip\n"));
636 // do clipping only if this was the first call to applyClipPath
637 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
638 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
639 cairo_clip(ctx->_cr);
641 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
642 ctx->setTransform(&saved_ctm);
644 ctx->setRenderMode(saved_mode);
645 }
647 void
648 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
649 {
650 g_assert( ctx != NULL && ctx->_is_valid );
652 if (mask == NULL)
653 return;
655 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
656 NRRect mask_bbox(mask->display->bbox);
657 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
658 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
659 NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
660 t[4] = mask_bbox.x0;
661 t[5] = mask_bbox.y0;
662 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 :