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"
44 #include "libnr/nr-convert2geom.h"
45 #include <2geom/transforms.h>
46 #include <2geom/pathvector.h>
48 #include <glib/gmem.h>
50 #include <glibmm/i18n.h>
51 #include "display/nr-arena.h"
52 #include "display/nr-arena-item.h"
53 #include "display/nr-arena-group.h"
54 #include "display/curve.h"
55 #include "display/canvas-bpath.h"
56 #include "sp-item.h"
57 #include "sp-item-group.h"
58 #include "style.h"
59 #include "marker.h"
60 #include "sp-linear-gradient.h"
61 #include "sp-radial-gradient.h"
62 #include "sp-root.h"
63 #include "sp-shape.h"
64 #include "sp-use.h"
65 #include "sp-text.h"
66 #include "sp-flowtext.h"
67 #include "sp-image.h"
68 #include "sp-symbol.h"
69 #include "sp-pattern.h"
70 #include "sp-mask.h"
71 #include "sp-clippath.h"
73 #include <unit-constants.h>
74 #include "helper/png-write.h"
75 #include "helper/pixbuf-ops.h"
77 #include "cairo-renderer.h"
78 #include "cairo-render-context.h"
79 #include "extension/system.h"
81 #include "io/sys.h"
83 #include <cairo.h>
85 // include support for only the compiled-in surface types
86 #ifdef CAIRO_HAS_PDF_SURFACE
87 #include <cairo-pdf.h>
88 #endif
89 #ifdef CAIRO_HAS_PS_SURFACE
90 #include <cairo-ps.h>
91 #endif
93 //#define TRACE(_args) g_printf _args
94 #define TRACE(_args)
96 // FIXME: expose these from sp-clippath/mask.cpp
97 struct SPClipPathView {
98 SPClipPathView *next;
99 unsigned int key;
100 NRArenaItem *arenaitem;
101 NRRect bbox;
102 };
104 struct SPMaskView {
105 SPMaskView *next;
106 unsigned int key;
107 NRArenaItem *arenaitem;
108 NRRect bbox;
109 };
111 namespace Inkscape {
112 namespace Extension {
113 namespace Internal {
115 CairoRenderer::CairoRenderer(void)
116 {}
118 CairoRenderer::~CairoRenderer(void)
119 {
120 /* restore default signal handling for SIGPIPE */
121 #if !defined(_WIN32) && !defined(__WIN32__)
122 (void) signal(SIGPIPE, SIG_DFL);
123 #endif
125 return;
126 }
128 CairoRenderContext*
129 CairoRenderer::createContext(void)
130 {
131 CairoRenderContext *new_context = new CairoRenderContext(this);
132 g_assert( new_context != NULL );
134 new_context->_state_stack = NULL;
135 new_context->_state = NULL;
137 // create initial render state
138 CairoRenderState *state = new_context->_createState();
139 state->transform.set_identity();
140 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
141 new_context->_state = state;
143 return new_context;
144 }
146 void
147 CairoRenderer::destroyContext(CairoRenderContext *ctx)
148 {
149 delete ctx;
150 }
152 /*
154 Here comes the rendering part which could be put into the 'render' methods of SPItems'
156 */
158 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
159 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
161 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
162 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
163 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
164 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
165 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
166 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
167 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
169 /* 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 ) */
170 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
171 {
172 NRRect pbox;
174 SPShape *shape = SP_SHAPE(item);
176 if (!shape->curve) return;
178 /* fixme: Think (Lauris) */
179 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
181 SPStyle* style = SP_OBJECT_STYLE (item);
182 CairoRenderer *renderer = ctx->getRenderer();
184 const_NRBPath bp;
185 bp.path = SP_CURVE_BPATH(shape->curve);
187 ctx->renderPath(&bp, style, &pbox);
189 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
190 Geom::PathVector const & pathv = shape->curve->get_pathvector();
191 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
192 if ( shape->marker[SP_MARKER_LOC_START] ) {
193 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
194 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
196 Geom::Point p = path_it->front().pointAt(0);
197 Geom::Point tang = path_it->front().unitTangentAt(0);
198 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
200 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
201 tr = NR::scale(style->stroke_width.computed) * tr;
202 }
204 tr = marker_item->transform * marker->c2p * tr;
206 NR::Matrix old_tr = marker_item->transform;
207 marker_item->transform = tr;
208 renderer->renderItem (ctx, marker_item);
209 marker_item->transform = old_tr;
210 }
212 if ( shape->marker[SP_MARKER_LOC_MID] ) {
213 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
214 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
215 while (curve_it2 != path_it->end_default())
216 {
217 /* Put marker between curve_it1 and curve_it2.
218 * Loop to end_default (so including closing segment), because when a path is closed,
219 * there should be a midpoint marker between last segment and closing straight line segment */
221 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
222 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
224 Geom::Point p = curve_it1->pointAt(1);
225 Geom::Point tang1 = curve_it1->unitTangentAt(1);
226 Geom::Point tang2 = curve_it2->unitTangentAt(0);
227 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang1, tang2)));
229 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
230 tr = NR::scale(style->stroke_width.computed) * tr;
231 }
233 tr = marker_item->transform * marker->c2p * tr;
235 NR::Matrix old_tr = marker_item->transform;
236 marker_item->transform = tr;
237 renderer->renderItem (ctx, marker_item);
238 marker_item->transform = old_tr;
240 ++curve_it1;
241 ++curve_it2;
242 }
243 }
245 if ( shape->marker[SP_MARKER_LOC_END] ) {
246 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
247 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
249 Geom::Point p = path_it->back_default().pointAt(1);
250 Geom::Point tang = path_it->back_default().unitTangentAt(1);
251 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(p, tang, tang)));
253 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
254 tr = NR::scale(style->stroke_width.computed) * tr;
255 }
257 tr = marker_item->transform * marker->c2p * tr;
259 NR::Matrix old_tr = marker_item->transform;
260 marker_item->transform = tr;
261 renderer->renderItem (ctx, marker_item);
262 marker_item->transform = old_tr;
263 }
264 }
265 }
267 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
268 {
269 SPGroup *group = SP_GROUP(item);
270 CairoRenderer *renderer = ctx->getRenderer();
271 TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
273 GSList *l = g_slist_reverse(group->childList(false));
274 while (l) {
275 SPObject *o = SP_OBJECT (l->data);
276 if (SP_IS_ITEM(o)) {
277 renderer->renderItem (ctx, SP_ITEM (o));
278 }
279 l = g_slist_remove (l, o);
280 }
281 }
283 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
284 {
285 bool translated = false;
286 SPUse *use = SP_USE(item);
287 CairoRenderer *renderer = ctx->getRenderer();
289 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
290 NR::Matrix tp(NR::translate(use->x.computed, use->y.computed));
291 ctx->pushState();
292 ctx->transform(&tp);
293 translated = true;
294 }
296 if (use->child && SP_IS_ITEM(use->child)) {
297 renderer->renderItem(ctx, SP_ITEM(use->child));
298 }
300 if (translated) {
301 ctx->popState();
302 }
303 }
305 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
306 {
307 SPText *group = SP_TEXT (item);
308 group->layout.showGlyphs(ctx);
309 }
311 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
312 {
313 SPFlowtext *group = SP_FLOWTEXT(item);
314 group->layout.showGlyphs(ctx);
315 }
317 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
318 {
319 SPImage *image;
320 guchar *px;
321 int w, h, rs;
323 image = SP_IMAGE (item);
325 if (!image->pixbuf) return;
326 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
328 px = gdk_pixbuf_get_pixels (image->pixbuf);
329 w = gdk_pixbuf_get_width (image->pixbuf);
330 h = gdk_pixbuf_get_height (image->pixbuf);
331 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
333 double x = image->x.computed;
334 double y = image->y.computed;
335 double width = image->width.computed;
336 double height = image->height.computed;
338 if (image->aspect_align != SP_ASPECT_NONE) {
339 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
340 &x, &y, &width, &height);
341 }
343 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
344 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
345 }
347 NR::translate tp(x, y);
348 NR::scale s(width / (double)w, height / (double)h);
349 NR::Matrix t(s * tp);
351 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
352 }
354 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
355 {
356 SPSymbol *symbol = SP_SYMBOL(item);
357 if (!SP_OBJECT_IS_CLONED (symbol))
358 return;
360 /* Cloned <symbol> is actually renderable */
361 ctx->pushState();
362 ctx->transform(&symbol->c2p);
364 // apply viewbox if set
365 if (0 /*symbol->viewBox_set*/) {
366 NR::Matrix vb2user;
367 double x, y, width, height;
368 double view_width, view_height;
369 x = 0.0;
370 y = 0.0;
371 width = 1.0;
372 height = 1.0;
374 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
375 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
377 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
378 &x, &y,&width, &height);
380 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
381 vb2user.set_identity();
382 vb2user[0] = width / view_width;
383 vb2user[3] = height / view_height;
384 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
385 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
387 ctx->transform(&vb2user);
388 }
390 sp_group_render(item, ctx);
391 ctx->popState();
392 }
394 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
395 {
396 SPRoot *root = SP_ROOT(item);
397 CairoRenderer *renderer = ctx->getRenderer();
399 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
400 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
402 ctx->pushState();
403 renderer->setStateForItem(ctx, item);
404 ctx->transform(&root->c2p);
405 sp_group_render(item, ctx);
406 ctx->popState();
407 }
409 /**
410 this function convert the item to a raster image and include the raster into the cairo renderer
411 */
412 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
413 {
414 g_warning("render as bitmap");
416 //the code now was copied from sp_selection_create_bitmap_copy
418 SPDocument *document = SP_OBJECT(item)->document;
419 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
421 // Get the bounding box of the selection
422 //NR::Maybe<NR::Rect> _bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
423 // NRRect bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
424 NRRect bbox(item->getBounds(from_2geom(sp_item_i2d_affine(item))));
427 // List of the items to show; all others will be hidden
428 GSList *items = NULL; //g_slist_copy ((GSList *) selection->itemList());
429 items = g_slist_append(items, item);
431 // Remember parent and z-order of the topmost one
432 gint pos = SP_OBJECT_REPR(g_slist_last(items)->data)->position();
433 SPObject *parent_object = SP_OBJECT_PARENT(g_slist_last(items)->data);
434 Inkscape::XML::Node *parent = SP_OBJECT_REPR(parent_object);
436 // Calculate resolution
437 double res;
438 /** @TODO reimplement the resolution stuff
439 */
440 res = ctx->getBitmapResolution();
441 if(res == 0) {
442 res = PX_PER_IN;
443 }
446 // The width and height of the bitmap in pixels
447 unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
448 unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
450 // Find out if we have to run a filter
451 gchar const *run = NULL;
452 gchar const *filter = NULL;
453 /** @TODO reimplement the filter stuff
454 //gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
455 if (filter) {
456 // filter command is given;
457 // see if we have a parameter to pass to it
458 gchar const *param1 = prefs_get_string_attribute ("options.createbitmap", "filter_param1");
459 if (param1) {
460 if (param1[strlen(param1) - 1] == '%') {
461 // if the param string ends with %, interpret it as a percentage of the image's max dimension
462 gchar p1[256];
463 g_ascii_dtostr (p1, 256, ceil (g_ascii_strtod (param1, NULL) * MAX(width, height) / 100));
464 // the first param is always the image filename, the second is param1
465 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, p1);
466 } else {
467 // otherwise pass the param1 unchanged
468 run = g_strdup_printf ("%s \"%s\" %s", filter, filepath, param1);
469 }
470 } else {
471 // run without extra parameter
472 run = g_strdup_printf ("%s \"%s\"", filter, filepath);
473 }
474 }
475 */
476 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
477 NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
478 NR::Matrix t;
480 double shift_x = bbox.x0;
481 double shift_y = bbox.y1;
482 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
483 shift_x = round (shift_x);
484 shift_y = -round (-shift_y); // this gets correct rounding despite coordinate inversion, remove the negations when the inversion is gone
485 }
486 t = (NR::Matrix)(NR::scale(1, -1) * (NR::Matrix)(NR::translate (shift_x, shift_y)* eek.inverse()));
488 //t = t * ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
490 // Do the export
491 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
492 bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
494 // Run filter, if any
495 /*
496 if (run) {
497 g_print ("Running external filter: %s\n", run);
498 system (run);
499 }
500 */
501 if (pb) {
502 unsigned char *px = gdk_pixbuf_get_pixels (pb);
503 unsigned int w = gdk_pixbuf_get_width(pb);
504 unsigned int h = gdk_pixbuf_get_height(pb);
505 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
506 NR::Matrix matrix;
507 matrix = t;
508 //matrix = ((NR::Matrix)ctx->getCurrentState()->transform).inverse();
509 //matrix.set_identity();
511 ctx->renderImage (px, w, h, rs, &matrix, SP_OBJECT_STYLE (item));
512 /*
513 // Create the repr for the image
514 Inkscape::XML::Node * repr = xml_doc->createElement("svg:image");
515 repr->setAttribute("xlink:href", filename);
516 repr->setAttribute("sodipodi:absref", filepath);
517 if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
518 sp_repr_set_svg_double(repr, "width", width);
519 sp_repr_set_svg_double(repr, "height", height);
520 } else {
521 sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
522 sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
523 }
525 // Write transform
526 gchar *c=sp_svg_transform_write(t);
527 repr->setAttribute("transform", c);
528 g_free(c);
530 // add the new repr to the parent
531 parent->appendChild(repr);
533 // move to the saved position
534 repr->setPosition(pos > 0 ? pos + 1 : 1);
535 */
536 gdk_pixbuf_unref (pb);
537 }
538 g_slist_free (items);
539 }
541 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
542 {
543 // Check item's visibility
544 if (item->isHidden()) {
545 return;
546 }
548 SPStyle* style = SP_OBJECT_STYLE (item);
549 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
550 return sp_asbitmap_render(item, ctx);
551 }
553 if (SP_IS_ROOT(item)) {
554 TRACE(("root\n"));
555 return sp_root_render(item, ctx);
556 } else if (SP_IS_GROUP(item)) {
557 TRACE(("group\n"));
558 return sp_group_render(item, ctx);
559 } else if (SP_IS_SHAPE(item)) {
560 TRACE(("shape\n"));
561 return sp_shape_render(item, ctx);
562 } else if (SP_IS_USE(item)) {
563 TRACE(("use begin---\n"));
564 sp_use_render(item, ctx);
565 TRACE(("---use end\n"));
566 } else if (SP_IS_SYMBOL(item)) {
567 TRACE(("symbol\n"));
568 return sp_symbol_render(item, ctx);
569 } else if (SP_IS_TEXT(item)) {
570 TRACE(("text\n"));
571 return sp_text_render(item, ctx);
572 } else if (SP_IS_FLOWTEXT(item)) {
573 TRACE(("flowtext\n"));
574 return sp_flowtext_render(item, ctx);
575 } else if (SP_IS_IMAGE(item)) {
576 TRACE(("image\n"));
577 return sp_image_render(item, ctx);
578 }
579 }
581 void
582 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
583 {
584 SPStyle const *style = SP_OBJECT_STYLE(item);
585 ctx->setStateForStyle(style);
587 CairoRenderState *state = ctx->getCurrentState();
588 state->clip_path = item->clip_ref->getObject();
589 state->mask = item->mask_ref->getObject();
591 // If parent_has_userspace is true the parent state's transform
592 // has to be used for the mask's/clippath's context.
593 // This is so because we use the image's/(flow)text's transform for positioning
594 // instead of explicitly specifying it and letting the renderer do the
595 // transformation before rendering the item.
596 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
597 state->parent_has_userspace = TRUE;
598 TRACE(("set op: %f\n", state->opacity));
599 }
601 void
602 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
603 {
604 ctx->pushState();
605 setStateForItem(ctx, item);
607 CairoRenderState *state = ctx->getCurrentState();
608 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
610 if (state->need_layer) {
611 state->merge_opacity = FALSE;
612 ctx->pushLayer();
613 }
614 ctx->transform(&item->transform);
615 sp_item_invoke_render(item, ctx);
617 if (state->need_layer)
618 ctx->popLayer();
620 ctx->popState();
621 }
623 bool
624 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
625 {
626 g_assert( ctx != NULL );
628 if (ctx->_vector_based_target) {
629 // width and height in pt
630 ctx->_width = sp_document_width(doc) * PT_PER_PX;
631 ctx->_height = sp_document_height(doc) * PT_PER_PX;
632 } else {
633 ctx->_width = sp_document_width(doc);
634 ctx->_height = sp_document_height(doc);
635 }
637 NRRect d;
638 bool pageBoundingBox = true;
639 if (pageBoundingBox) {
640 d.x0 = d.y0 = 0;
641 d.x1 = ceil(ctx->_width);
642 d.y1 = ceil(ctx->_height);
643 } else {
644 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
645 sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
646 if (ctx->_vector_based_target) {
647 // convert from px to pt
648 d.x0 *= PT_PER_PX;
649 d.x1 *= PT_PER_PX;
650 d.y0 *= PT_PER_PX;
651 d.y1 *= PT_PER_PX;
652 }
653 }
654 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
656 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
657 }
659 #include "macros.h" // SP_PRINT_*
661 void
662 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
663 {
664 g_assert( ctx != NULL && ctx->_is_valid );
666 if (cp == NULL)
667 return;
669 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
670 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
672 NR::Matrix saved_ctm;
673 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
674 //SP_PRINT_DRECT("clipd", cp->display->bbox);
675 NRRect clip_bbox(cp->display->bbox);
676 NR::Matrix t(NR::scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
677 t[4] = clip_bbox.x0;
678 t[5] = clip_bbox.y0;
679 t *= ctx->getCurrentState()->transform;
680 ctx->getTransform(&saved_ctm);
681 ctx->setTransform(&t);
682 }
684 TRACE(("BEGIN clip\n"));
685 SPObject *co = SP_OBJECT(cp);
686 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
687 if (SP_IS_ITEM(child)) {
688 SPItem *item = SP_ITEM(child);
689 renderItem(ctx, item);
690 }
691 }
692 TRACE(("END clip\n"));
694 // do clipping only if this was the first call to applyClipPath
695 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
696 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
697 cairo_clip(ctx->_cr);
699 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
700 ctx->setTransform(&saved_ctm);
702 ctx->setRenderMode(saved_mode);
703 }
705 void
706 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
707 {
708 g_assert( ctx != NULL && ctx->_is_valid );
710 if (mask == NULL)
711 return;
713 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
714 NRRect mask_bbox(mask->display->bbox);
715 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
716 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
717 NR::Matrix t(NR::scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
718 t[4] = mask_bbox.x0;
719 t[5] = mask_bbox.y0;
720 t *= ctx->getCurrentState()->transform;
721 ctx->setTransform(&t);
722 }
724 // clip mask contents
725 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
727 ctx->pushState();
729 TRACE(("BEGIN mask\n"));
730 SPObject *co = SP_OBJECT(mask);
731 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
732 if (SP_IS_ITEM(child)) {
733 SPItem *item = SP_ITEM(child);
734 renderItem(ctx, item);
735 }
736 }
737 TRACE(("END mask\n"));
739 ctx->popState();
740 }
742 void
743 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
744 double *x, double *y, double *width, double *height)
745 {
746 if (aspect_align == SP_ASPECT_NONE)
747 return;
749 double scalex, scaley, scale;
750 double new_width, new_height;
751 scalex = *width / vp_width;
752 scaley = *height / vp_height;
753 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
754 new_width = vp_width * scale;
755 new_height = vp_height * scale;
756 /* Now place viewbox to requested position */
757 switch (aspect_align) {
758 case SP_ASPECT_XMIN_YMIN:
759 break;
760 case SP_ASPECT_XMID_YMIN:
761 *x -= 0.5 * (new_width - *width);
762 break;
763 case SP_ASPECT_XMAX_YMIN:
764 *x -= 1.0 * (new_width - *width);
765 break;
766 case SP_ASPECT_XMIN_YMID:
767 *y -= 0.5 * (new_height - *height);
768 break;
769 case SP_ASPECT_XMID_YMID:
770 *x -= 0.5 * (new_width - *width);
771 *y -= 0.5 * (new_height - *height);
772 break;
773 case SP_ASPECT_XMAX_YMID:
774 *x -= 1.0 * (new_width - *width);
775 *y -= 0.5 * (new_height - *height);
776 break;
777 case SP_ASPECT_XMIN_YMAX:
778 *y -= 1.0 * (new_height - *height);
779 break;
780 case SP_ASPECT_XMID_YMAX:
781 *x -= 0.5 * (new_width - *width);
782 *y -= 1.0 * (new_height - *height);
783 break;
784 case SP_ASPECT_XMAX_YMAX:
785 *x -= 1.0 * (new_width - *width);
786 *y -= 1.0 * (new_height - *height);
787 break;
788 default:
789 break;
790 }
791 *width = new_width;
792 *height = new_height;
793 }
795 #include "clear-n_.h"
797 } /* namespace Internal */
798 } /* namespace Extension */
799 } /* namespace Inkscape */
801 #undef TRACE
803 /* End of GNU GPL code */
805 /*
806 Local Variables:
807 mode:c++
808 c-file-style:"stroustrup"
809 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
810 indent-tabs-mode:nil
811 fill-column:99
812 End:
813 */
814 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :