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/nr-matrix-ops.h>
32 #include <libnr/nr-matrix-fns.h>
33 #include <libnr/nr-matrix-translate-ops.h>
34 #include <libnr/nr-scale-matrix-ops.h>
36 #include "libnr/nr-matrix-rotate-ops.h"
37 #include "libnr/nr-matrix-translate-ops.h"
38 #include "libnr/nr-rotate-fns.h"
39 #include "libnr/nr-scale-ops.h"
40 #include "libnr/nr-scale-translate-ops.h"
41 #include "libnr/nr-translate-matrix-ops.h"
42 #include "libnr/nr-translate-scale-ops.h"
43 #include "libnr/nr-convert2geom.h"
44 #include <2geom/transforms.h>
45 #include <2geom/pathvector.h>
47 #include <glib/gmem.h>
49 #include <glibmm/i18n.h>
50 #include "display/nr-arena.h"
51 #include "display/nr-arena-item.h"
52 #include "display/nr-arena-group.h"
53 #include "display/curve.h"
54 #include "display/canvas-bpath.h"
55 #include "sp-item.h"
56 #include "sp-item-group.h"
57 #include "style.h"
58 #include "marker.h"
59 #include "sp-linear-gradient.h"
60 #include "sp-radial-gradient.h"
61 #include "sp-root.h"
62 #include "sp-shape.h"
63 #include "sp-use.h"
64 #include "sp-text.h"
65 #include "sp-flowtext.h"
66 #include "sp-image.h"
67 #include "sp-symbol.h"
68 #include "sp-pattern.h"
69 #include "sp-mask.h"
70 #include "sp-clippath.h"
72 #include <unit-constants.h>
73 #include "helper/png-write.h"
74 #include "helper/pixbuf-ops.h"
76 #include "cairo-renderer.h"
77 #include "cairo-render-context.h"
78 #include "extension/system.h"
80 #include "io/sys.h"
82 #include <cairo.h>
84 // include support for only the compiled-in surface types
85 #ifdef CAIRO_HAS_PDF_SURFACE
86 #include <cairo-pdf.h>
87 #endif
88 #ifdef CAIRO_HAS_PS_SURFACE
89 #include <cairo-ps.h>
90 #endif
92 //#define TRACE(_args) g_printf _args
93 #define TRACE(_args)
95 // FIXME: expose these from sp-clippath/mask.cpp
96 struct SPClipPathView {
97 SPClipPathView *next;
98 unsigned int key;
99 NRArenaItem *arenaitem;
100 NRRect bbox;
101 };
103 struct SPMaskView {
104 SPMaskView *next;
105 unsigned int key;
106 NRArenaItem *arenaitem;
107 NRRect bbox;
108 };
110 namespace Inkscape {
111 namespace Extension {
112 namespace Internal {
114 CairoRenderer::CairoRenderer(void)
115 {}
117 CairoRenderer::~CairoRenderer(void)
118 {
119 /* restore default signal handling for SIGPIPE */
120 #if !defined(_WIN32) && !defined(__WIN32__)
121 (void) signal(SIGPIPE, SIG_DFL);
122 #endif
124 return;
125 }
127 CairoRenderContext*
128 CairoRenderer::createContext(void)
129 {
130 CairoRenderContext *new_context = new CairoRenderContext(this);
131 g_assert( new_context != NULL );
133 new_context->_state_stack = NULL;
134 new_context->_state = NULL;
136 // create initial render state
137 CairoRenderState *state = new_context->_createState();
138 state->transform = Geom::identity();
139 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
140 new_context->_state = state;
142 return new_context;
143 }
145 void
146 CairoRenderer::destroyContext(CairoRenderContext *ctx)
147 {
148 delete ctx;
149 }
151 /*
153 Here comes the rendering part which could be put into the 'render' methods of SPItems'
155 */
157 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
158 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
159 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
168 /* TODO FIXME: this does not render painting-marker-01-f.svg of SVG1.1 Test suite correctly. (orientation of one of the markers middle left ) */
169 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
170 {
171 NRRect pbox;
173 SPShape *shape = SP_SHAPE(item);
175 if (!shape->curve) return;
177 /* fixme: Think (Lauris) */
178 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
180 SPStyle* style = SP_OBJECT_STYLE (item);
181 CairoRenderer *renderer = ctx->getRenderer();
183 Geom::PathVector const & pathv = shape->curve->get_pathvector();
185 ctx->renderPathVector(pathv, style, &pbox);
187 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
188 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
189 if ( shape->marker[SP_MARKER_LOC_START] ) {
190 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
191 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
193 Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
195 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
196 tr = Geom::Scale(style->stroke_width.computed) * tr;
197 }
199 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
201 Geom::Matrix old_tr = marker_item->transform;
202 marker_item->transform = tr;
203 renderer->renderItem (ctx, marker_item);
204 marker_item->transform = old_tr;
205 }
207 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
208 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
209 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
210 while (curve_it2 != path_it->end_default())
211 {
212 /* Put marker between curve_it1 and curve_it2.
213 * Loop to end_default (so including closing segment), because when a path is closed,
214 * there should be a midpoint marker between last segment and closing straight line segment */
216 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
217 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
219 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
221 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
222 tr = Geom::Scale(style->stroke_width.computed) * tr;
223 }
225 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
227 Geom::Matrix old_tr = marker_item->transform;
228 marker_item->transform = tr;
229 renderer->renderItem (ctx, marker_item);
230 marker_item->transform = old_tr;
232 ++curve_it1;
233 ++curve_it2;
234 }
235 }
237 if ( shape->marker[SP_MARKER_LOC_END] ) {
238 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
239 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
241 /* Get reference to last curve in the path.
242 * For moveto-only path, this returns the "closing line segment". */
243 unsigned int index = path_it->size_default();
244 if (index > 0) {
245 index--;
246 }
247 Geom::Curve const &lastcurve = (*path_it)[index];
249 Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
251 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
252 tr = Geom::Scale(style->stroke_width.computed) * tr;
253 }
255 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
257 Geom::Matrix old_tr = marker_item->transform;
258 marker_item->transform = tr;
259 renderer->renderItem (ctx, marker_item);
260 marker_item->transform = old_tr;
261 }
262 }
263 }
265 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
266 {
267 SPGroup *group = SP_GROUP(item);
268 CairoRenderer *renderer = ctx->getRenderer();
269 TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
271 GSList *l = g_slist_reverse(group->childList(false));
272 while (l) {
273 SPObject *o = SP_OBJECT (l->data);
274 if (SP_IS_ITEM(o)) {
275 renderer->renderItem (ctx, SP_ITEM (o));
276 }
277 l = g_slist_remove (l, o);
278 }
279 }
281 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
282 {
283 bool translated = false;
284 SPUse *use = SP_USE(item);
285 CairoRenderer *renderer = ctx->getRenderer();
287 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
288 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
289 ctx->pushState();
290 ctx->transform(&tp);
291 translated = true;
292 }
294 if (use->child && SP_IS_ITEM(use->child)) {
295 renderer->renderItem(ctx, SP_ITEM(use->child));
296 }
298 if (translated) {
299 ctx->popState();
300 }
301 }
303 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
304 {
305 SPText *group = SP_TEXT (item);
306 group->layout.showGlyphs(ctx);
307 }
309 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
310 {
311 SPFlowtext *group = SP_FLOWTEXT(item);
312 group->layout.showGlyphs(ctx);
313 }
315 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
316 {
317 SPImage *image;
318 guchar *px;
319 int w, h, rs;
321 image = SP_IMAGE (item);
323 if (!image->pixbuf) return;
324 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
326 px = gdk_pixbuf_get_pixels (image->pixbuf);
327 w = gdk_pixbuf_get_width (image->pixbuf);
328 h = gdk_pixbuf_get_height (image->pixbuf);
329 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
331 double x = image->x.computed;
332 double y = image->y.computed;
333 double width = image->width.computed;
334 double height = image->height.computed;
336 if (image->aspect_align != SP_ASPECT_NONE) {
337 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
338 &x, &y, &width, &height);
339 }
341 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
342 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
343 }
345 Geom::Translate tp(x, y);
346 Geom::Scale s(width / (double)w, height / (double)h);
347 Geom::Matrix t(s * tp);
349 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
350 }
352 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
353 {
354 SPSymbol *symbol = SP_SYMBOL(item);
355 if (!SP_OBJECT_IS_CLONED (symbol))
356 return;
358 /* Cloned <symbol> is actually renderable */
359 ctx->pushState();
360 ctx->transform(&symbol->c2p);
362 // apply viewbox if set
363 if (0 /*symbol->viewBox_set*/) {
364 Geom::Matrix vb2user;
365 double x, y, width, height;
366 double view_width, view_height;
367 x = 0.0;
368 y = 0.0;
369 width = 1.0;
370 height = 1.0;
372 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
373 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
375 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
376 &x, &y,&width, &height);
378 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
379 vb2user = Geom::identity();
380 vb2user[0] = width / view_width;
381 vb2user[3] = height / view_height;
382 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
383 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
385 ctx->transform(&vb2user);
386 }
388 sp_group_render(item, ctx);
389 ctx->popState();
390 }
392 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
393 {
394 SPRoot *root = SP_ROOT(item);
395 CairoRenderer *renderer = ctx->getRenderer();
397 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
398 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
400 ctx->pushState();
401 renderer->setStateForItem(ctx, item);
402 Geom::Matrix tempmat (root->c2p);
403 ctx->transform(&tempmat);
404 sp_group_render(item, ctx);
405 ctx->popState();
406 }
408 /**
409 this function convert the item to a raster image and include the raster into the cairo renderer
410 */
411 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
412 {
413 //the code now was copied from sp_selection_create_bitmap_copy
415 SPDocument *document = SP_OBJECT(item)->document;
416 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
418 // Get the bounding box of the selection
419 //boost::optional<Geom::Rect> _bbox = item->getBounds(sp_item_i2d_affine(item));
420 // NRRect bbox = item->getBounds(sp_item_i2d_affine(item));
421 NRRect bbox(item->getBounds(sp_item_i2d_affine(item)));
424 // List of the items to show; all others will be hidden
425 GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
426 items = g_slist_append(items, item);
428 // Remember parent and z-order of the topmost one
429 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
430 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
431 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
433 // Calculate resolution
434 double res;
435 /** @TODO reimplement the resolution stuff
436 */
437 res = ctx->getBitmapResolution();
438 if(res == 0) {
439 res = PX_PER_IN;
440 }
443 // The width and height of the bitmap in pixels
444 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * (res / PX_PER_IN));
445 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * (res / PX_PER_IN));
447 double scale_x = (bbox.x1 - bbox.x0) / width;
448 double scale_y = (bbox.y1 - bbox.y0) / height;
451 // Find out if we have to run a filter
452 gchar const *run = NULL;
453 gchar const *filter = NULL;
454 /** @TODO reimplement the filter stuff
455 //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
456 if (filter) {
457 // filter command is given;
458 // see if we have a parameter to pass to it
459 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
460 if (param1) {
461 if (param1[strlen(param1) - 1] == '%') {
462 // if the param string ends with %, interpret it as a percentage of the image's max dimension
463 gchar p1[256];
464 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
465 // the first param is always the image filename, the second is param1
466 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
467 } else {
468 // otherwise pass the param1 unchanged
469 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
470 }
471 } else {
472 // run without extra parameter
473 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
474 }
475 }
476 */
477 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
478 Geom::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
479 Geom::Matrix t;
481 double shift_x = bbox.x0;
482 double shift_y = bbox.y1;
483 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
484 shift_x = round (shift_x);
485 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
486 }
487 t = (Geom::Matrix)(Geom::Scale(scale_x, -scale_y) * (Geom::Matrix)(Geom::Translate (shift_x, shift_y)* eek.inverse()));
489 //t = t * ((Geom::Matrix)ctx->getCurrentState()->transform).inverse();
491 // Do the export
492 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
493 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
495 // Run filter, if any
496 /*
497 if (run) {
498 g_print ("Running external filter: %s\n", run);
499 system (run);
500 }
501 */
502 if (pb) {
503 unsigned char *px = gdk_pixbuf_get_pixels (pb);
504 unsigned int w = gdk_pixbuf_get_width(pb);
505 unsigned int h = gdk_pixbuf_get_height(pb);
506 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
507 Geom::Matrix matrix;
508 matrix = t;
509 //matrix = ((Geom::Matrix)ctx->getCurrentState()->transform).inverse();
510 //matrix.set_identity();
512 ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
513 /*
514 // Create the repr for the image
515 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
516 repr->setAttribute("xlink:href", filename);
517 repr->setAttribute("sodipodi:absref", filepath);
518 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
519 sp_repr_set_svg_double(repr, "width", width);
520 sp_repr_set_svg_double(repr, "height", height);
521 } else {
522 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
523 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
524 }
526 // Write transform
527 gchar *c=sp_svg_transform_write(t);
528 repr->setAttribute("transform", c);
529 g_free(c);
531 // add the new repr to the parent
532 parent->appendChild(repr);
534 // move to the saved position
535 repr->setPosition(pos > 0 ? pos + 1 : 1);
536 */
537 gdk_pixbuf_unref (pb);
538 }
539 g_slist_free (items);
540 }
542 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
543 {
544 // Check item's visibility
545 if (item->isHidden()) {
546 return;
547 }
549 SPStyle* style = SP_OBJECT_STYLE (item);
550 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
551 return sp_asbitmap_render(item, ctx);
552 }
554 if (SP_IS_ROOT(item)) {
555 TRACE(("root\n"));
556 return sp_root_render(item, ctx);
557 } else if (SP_IS_GROUP(item)) {
558 TRACE(("group\n"));
559 return sp_group_render(item, ctx);
560 } else if (SP_IS_SHAPE(item)) {
561 TRACE(("shape\n"));
562 return sp_shape_render(item, ctx);
563 } else if (SP_IS_USE(item)) {
564 TRACE(("use begin---\n"));
565 sp_use_render(item, ctx);
566 TRACE(("---use end\n"));
567 } else if (SP_IS_SYMBOL(item)) {
568 TRACE(("symbol\n"));
569 return sp_symbol_render(item, ctx);
570 } else if (SP_IS_TEXT(item)) {
571 TRACE(("text\n"));
572 return sp_text_render(item, ctx);
573 } else if (SP_IS_FLOWTEXT(item)) {
574 TRACE(("flowtext\n"));
575 return sp_flowtext_render(item, ctx);
576 } else if (SP_IS_IMAGE(item)) {
577 TRACE(("image\n"));
578 return sp_image_render(item, ctx);
579 }
580 }
582 void
583 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
584 {
585 SPStyle const *style = SP_OBJECT_STYLE(item);
586 ctx->setStateForStyle(style);
588 CairoRenderState *state = ctx->getCurrentState();
589 state->clip_path = item->clip_ref->getObject();
590 state->mask = item->mask_ref->getObject();
591 state->item_transform = Geom::Matrix (item->transform);
593 // If parent_has_userspace is true the parent state's transform
594 // has to be used for the mask's/clippath's context.
595 // This is so because we use the image's/(flow)text's transform for positioning
596 // instead of explicitly specifying it and letting the renderer do the
597 // transformation before rendering the item.
598 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
599 state->parent_has_userspace = TRUE;
600 TRACE(("set op: %f\n", state->opacity));
601 }
603 void
604 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
605 {
606 ctx->pushState();
607 setStateForItem(ctx, item);
609 CairoRenderState *state = ctx->getCurrentState();
610 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
612 if (state->need_layer) {
613 state->merge_opacity = FALSE;
614 ctx->pushLayer();
615 }
616 Geom::Matrix tempmat (item->transform);
617 ctx->transform(&tempmat);
618 sp_item_invoke_render(item, ctx);
620 if (state->need_layer)
621 ctx->popLayer();
623 ctx->popState();
624 }
626 bool
627 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
628 {
629 g_assert( ctx != NULL );
631 if (ctx->_vector_based_target) {
632 // width and height in pt
633 ctx->_width = sp_document_width(doc) * PT_PER_PX;
634 ctx->_height = sp_document_height(doc) * PT_PER_PX;
635 } else {
636 ctx->_width = sp_document_width(doc);
637 ctx->_height = sp_document_height(doc);
638 }
640 NRRect d;
641 if (pageBoundingBox || !base) {
642 d.x0 = d.y0 = 0;
643 d.x1 = ceil(ctx->_width);
644 d.y1 = ceil(ctx->_height);
645 } else {
646 sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE);
647 if (ctx->_vector_based_target) {
648 // convert from px to pt
649 d.x0 *= PT_PER_PX;
650 d.x1 *= PT_PER_PX;
651 d.y0 *= PT_PER_PX;
652 d.y1 *= PT_PER_PX;
653 }
654 }
655 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
657 bool ret = ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
659 if (ret && !pageBoundingBox && base)
660 {
661 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
662 (d.y1 - ctx->_height) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
663 ctx->transform(&tp);
665 ctx->_width = d.x1 - d.x0;
666 ctx->_height = d.y1 - d.y0;
667 }
669 return ret;
670 }
672 #include "macros.h" // SP_PRINT_*
674 void
675 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
676 {
677 g_assert( ctx != NULL && ctx->_is_valid );
679 if (cp == NULL)
680 return;
682 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
683 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
685 Geom::Matrix saved_ctm;
686 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
687 //SP_PRINT_DRECT("clipd", cp->display->bbox);
688 NRRect clip_bbox(cp->display->bbox);
689 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
690 t[4] = clip_bbox.x0;
691 t[5] = clip_bbox.y0;
692 t *= ctx->getCurrentState()->transform;
693 ctx->getTransform(&saved_ctm);
694 ctx->setTransform(&t);
695 }
697 TRACE(("BEGIN clip\n"));
698 SPObject *co = SP_OBJECT(cp);
699 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
700 if (SP_IS_ITEM(child)) {
701 SPItem *item = SP_ITEM(child);
703 // combine transform of the item in clippath and the item using clippath:
704 Geom::Matrix tempmat (item->transform);
705 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
707 // render this item in clippath
708 ctx->pushState();
709 ctx->transform(&tempmat);
710 setStateForItem(ctx, item);
711 sp_item_invoke_render(item, ctx);
712 ctx->popState();
713 }
714 }
715 TRACE(("END clip\n"));
717 // do clipping only if this was the first call to applyClipPath
718 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
719 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
720 cairo_clip(ctx->_cr);
722 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
723 ctx->setTransform(&saved_ctm);
725 ctx->setRenderMode(saved_mode);
726 }
728 void
729 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
730 {
731 g_assert( ctx != NULL && ctx->_is_valid );
733 if (mask == NULL)
734 return;
736 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
737 NRRect mask_bbox(mask->display->bbox);
738 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
739 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
740 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
741 t[4] = mask_bbox.x0;
742 t[5] = mask_bbox.y0;
743 t *= ctx->getCurrentState()->transform;
744 ctx->setTransform(&t);
745 }
747 // clip mask contents
748 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
750 ctx->pushState();
752 TRACE(("BEGIN mask\n"));
753 SPObject *co = SP_OBJECT(mask);
754 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
755 if (SP_IS_ITEM(child)) {
756 SPItem *item = SP_ITEM(child);
757 renderItem(ctx, item);
758 }
759 }
760 TRACE(("END mask\n"));
762 ctx->popState();
763 }
765 void
766 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
767 double *x, double *y, double *width, double *height)
768 {
769 if (aspect_align == SP_ASPECT_NONE)
770 return;
772 double scalex, scaley, scale;
773 double new_width, new_height;
774 scalex = *width / vp_width;
775 scaley = *height / vp_height;
776 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
777 new_width = vp_width * scale;
778 new_height = vp_height * scale;
779 /* Now place viewbox to requested position */
780 switch (aspect_align) {
781 case SP_ASPECT_XMIN_YMIN:
782 break;
783 case SP_ASPECT_XMID_YMIN:
784 *x -= 0.5 * (new_width - *width);
785 break;
786 case SP_ASPECT_XMAX_YMIN:
787 *x -= 1.0 * (new_width - *width);
788 break;
789 case SP_ASPECT_XMIN_YMID:
790 *y -= 0.5 * (new_height - *height);
791 break;
792 case SP_ASPECT_XMID_YMID:
793 *x -= 0.5 * (new_width - *width);
794 *y -= 0.5 * (new_height - *height);
795 break;
796 case SP_ASPECT_XMAX_YMID:
797 *x -= 1.0 * (new_width - *width);
798 *y -= 0.5 * (new_height - *height);
799 break;
800 case SP_ASPECT_XMIN_YMAX:
801 *y -= 1.0 * (new_height - *height);
802 break;
803 case SP_ASPECT_XMID_YMAX:
804 *x -= 0.5 * (new_width - *width);
805 *y -= 1.0 * (new_height - *height);
806 break;
807 case SP_ASPECT_XMAX_YMAX:
808 *x -= 1.0 * (new_width - *width);
809 *y -= 1.0 * (new_height - *height);
810 break;
811 default:
812 break;
813 }
814 *width = new_width;
815 *height = new_height;
816 }
818 #include "clear-n_.h"
820 } /* namespace Internal */
821 } /* namespace Extension */
822 } /* namespace Inkscape */
824 #undef TRACE
826 /* End of GNU GPL code */
828 /*
829 Local Variables:
830 mode:c++
831 c-file-style:"stroustrup"
832 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
833 indent-tabs-mode:nil
834 fill-column:99
835 End:
836 */
837 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :