1 /** \file
2 * Rendering with Cairo.
3 */
4 /*
5 * Author:
6 * Miklos Erdelyi <erdelyim@gmail.com>
7 * Jon A. Cruz <jon@joncruz.org>
8 * Abhishek Sharma
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-rect.h"
32 #include "libnrtype/Layout-TNG.h"
33 #include <2geom/transforms.h>
34 #include <2geom/pathvector.h>
36 #include <glib/gmem.h>
38 #include <glibmm/i18n.h>
39 #include "display/nr-arena.h"
40 #include "display/nr-arena-item.h"
41 #include "display/nr-arena-group.h"
42 #include "display/curve.h"
43 #include "display/canvas-bpath.h"
44 #include "sp-item.h"
45 #include "sp-item-group.h"
46 #include "style.h"
47 #include "marker.h"
48 #include "sp-linear-gradient.h"
49 #include "sp-radial-gradient.h"
50 #include "sp-root.h"
51 #include "sp-shape.h"
52 #include "sp-use.h"
53 #include "sp-text.h"
54 #include "sp-flowtext.h"
55 #include "sp-image.h"
56 #include "sp-symbol.h"
57 #include "sp-pattern.h"
58 #include "sp-mask.h"
59 #include "sp-clippath.h"
61 #include <unit-constants.h>
62 #include "helper/png-write.h"
63 #include "helper/pixbuf-ops.h"
65 #include "cairo-renderer.h"
66 #include "cairo-render-context.h"
67 #include "extension/system.h"
69 #include "io/sys.h"
71 #include <cairo.h>
73 // include support for only the compiled-in surface types
74 #ifdef CAIRO_HAS_PDF_SURFACE
75 #include <cairo-pdf.h>
76 #endif
77 #ifdef CAIRO_HAS_PS_SURFACE
78 #include <cairo-ps.h>
79 #endif
81 //#define TRACE(_args) g_printf _args
82 #define TRACE(_args)
83 //#define TEST(_args) _args
84 #define TEST(_args)
86 // FIXME: expose these from sp-clippath/mask.cpp
87 struct SPClipPathView {
88 SPClipPathView *next;
89 unsigned int key;
90 NRArenaItem *arenaitem;
91 NRRect bbox;
92 };
94 struct SPMaskView {
95 SPMaskView *next;
96 unsigned int key;
97 NRArenaItem *arenaitem;
98 NRRect bbox;
99 };
101 namespace Inkscape {
102 namespace Extension {
103 namespace Internal {
105 CairoRenderer::CairoRenderer(void)
106 : _omitText(false)
107 {}
109 CairoRenderer::~CairoRenderer(void)
110 {
111 /* restore default signal handling for SIGPIPE */
112 #if !defined(_WIN32) && !defined(__WIN32__)
113 (void) signal(SIGPIPE, SIG_DFL);
114 #endif
116 return;
117 }
119 CairoRenderContext*
120 CairoRenderer::createContext(void)
121 {
122 CairoRenderContext *new_context = new CairoRenderContext(this);
123 g_assert( new_context != NULL );
125 new_context->_state_stack = NULL;
126 new_context->_state = NULL;
128 // create initial render state
129 CairoRenderState *state = new_context->_createState();
130 state->transform = Geom::identity();
131 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
132 new_context->_state = state;
134 return new_context;
135 }
137 void
138 CairoRenderer::destroyContext(CairoRenderContext *ctx)
139 {
140 delete ctx;
141 }
143 /*
145 Here comes the rendering part which could be put into the 'render' methods of SPItems'
147 */
149 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
150 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
157 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
160 static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
161 {
162 bool render = true;
163 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
164 if (style->stroke_width.computed > 1e-9) {
165 tr = Geom::Scale(style->stroke_width.computed) * tr;
166 } else {
167 render = false; // stroke width zero and marker is thus scaled down to zero, skip
168 }
169 }
171 if (render) {
172 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
173 tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
174 Geom::Matrix old_tr = marker_item->transform;
175 marker_item->transform = tr;
176 ctx->getRenderer()->renderItem (ctx, marker_item);
177 marker_item->transform = old_tr;
178 }
179 }
181 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
182 {
183 NRRect pbox;
185 SPShape *shape = SP_SHAPE(item);
187 if (!shape->curve) return;
189 item->invoke_bbox( &pbox, Geom::identity(), TRUE);
191 SPStyle* style = SP_OBJECT_STYLE (item);
193 Geom::PathVector const & pathv = shape->curve->get_pathvector();
194 if (pathv.empty()) return;
196 ctx->renderPathVector(pathv, style, &pbox);
198 // START marker
199 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
200 if ( shape->marker[i] ) {
201 SPMarker* marker = SP_MARKER (shape->marker[i]);
202 Geom::Matrix tr;
203 if (marker->orient_auto) {
204 tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
205 } else {
206 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
207 }
208 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
209 }
210 }
211 // MID marker
212 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
213 if ( !shape->marker[i] ) continue;
214 SPMarker* marker = SP_MARKER (shape->marker[i]);
215 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
216 // START position
217 if ( path_it != pathv.begin()
218 && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
219 {
220 Geom::Matrix tr;
221 if (marker->orient_auto) {
222 tr = sp_shape_marker_get_transform_at_start(path_it->front());
223 } else {
224 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
225 }
226 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
227 }
228 // MID position
229 if (path_it->size_default() > 1) {
230 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
231 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
232 while (curve_it2 != path_it->end_default())
233 {
234 /* Put marker between curve_it1 and curve_it2.
235 * Loop to end_default (so including closing segment), because when a path is closed,
236 * there should be a midpoint marker between last segment and closing straight line segment */
237 Geom::Matrix tr;
238 if (marker->orient_auto) {
239 tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
240 } else {
241 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
242 }
244 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
246 ++curve_it1;
247 ++curve_it2;
248 }
249 }
250 // END position
251 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
252 Geom::Curve const &lastcurve = path_it->back_default();
253 Geom::Matrix tr;
254 if (marker->orient_auto) {
255 tr = sp_shape_marker_get_transform_at_end(lastcurve);
256 } else {
257 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
258 }
259 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
260 }
261 }
262 }
263 // END marker
264 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
265 if ( shape->marker[i] ) {
266 SPMarker* marker = SP_MARKER (shape->marker[i]);
268 /* Get reference to last curve in the path.
269 * For moveto-only path, this returns the "closing line segment". */
270 Geom::Path const &path_last = pathv.back();
271 unsigned int index = path_last.size_default();
272 if (index > 0) {
273 index--;
274 }
275 Geom::Curve const &lastcurve = path_last[index];
277 Geom::Matrix tr;
278 if (marker->orient_auto) {
279 tr = sp_shape_marker_get_transform_at_end(lastcurve);
280 } else {
281 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
282 }
284 sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
285 }
286 }
287 }
289 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
290 {
291 SPGroup *group = SP_GROUP(item);
292 CairoRenderer *renderer = ctx->getRenderer();
293 TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
295 GSList *l = g_slist_reverse(group->childList(false));
296 while (l) {
297 SPObject *o = SP_OBJECT (l->data);
298 if (SP_IS_ITEM(o)) {
299 renderer->renderItem (ctx, SP_ITEM (o));
300 }
301 l = g_slist_remove (l, o);
302 }
303 }
305 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
306 {
307 bool translated = false;
308 SPUse *use = SP_USE(item);
309 CairoRenderer *renderer = ctx->getRenderer();
311 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
312 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
313 ctx->pushState();
314 ctx->transform(&tp);
315 translated = true;
316 }
318 if (use->child && SP_IS_ITEM(use->child)) {
319 renderer->renderItem(ctx, SP_ITEM(use->child));
320 }
322 if (translated) {
323 ctx->popState();
324 }
325 }
327 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
328 {
329 SPText *group = SP_TEXT (item);
330 group->layout.showGlyphs(ctx);
331 }
333 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
334 {
335 SPFlowtext *group = SP_FLOWTEXT(item);
336 group->layout.showGlyphs(ctx);
337 }
339 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
340 {
341 SPImage *image;
342 guchar *px;
343 int w, h, rs;
345 image = SP_IMAGE (item);
347 if (!image->pixbuf) return;
348 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
350 px = gdk_pixbuf_get_pixels (image->pixbuf);
351 w = gdk_pixbuf_get_width (image->pixbuf);
352 h = gdk_pixbuf_get_height (image->pixbuf);
353 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
355 double x = image->x.computed;
356 double y = image->y.computed;
357 double width = image->width.computed;
358 double height = image->height.computed;
360 if (image->aspect_align != SP_ASPECT_NONE) {
361 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
362 &x, &y, &width, &height);
363 }
365 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
366 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
367 }
369 Geom::Translate tp(x, y);
370 Geom::Scale s(width / (double)w, height / (double)h);
371 Geom::Matrix t(s * tp);
373 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
374 }
376 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
377 {
378 SPSymbol *symbol = SP_SYMBOL(item);
379 if (!SP_OBJECT_IS_CLONED (symbol))
380 return;
382 /* Cloned <symbol> is actually renderable */
383 ctx->pushState();
384 ctx->transform(&symbol->c2p);
386 // apply viewbox if set
387 if (0 /*symbol->viewBox_set*/) {
388 Geom::Matrix vb2user;
389 double x, y, width, height;
390 double view_width, view_height;
391 x = 0.0;
392 y = 0.0;
393 width = 1.0;
394 height = 1.0;
396 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
397 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
399 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
400 &x, &y,&width, &height);
402 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
403 vb2user = Geom::identity();
404 vb2user[0] = width / view_width;
405 vb2user[3] = height / view_height;
406 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
407 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
409 ctx->transform(&vb2user);
410 }
412 sp_group_render(item, ctx);
413 ctx->popState();
414 }
416 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
417 {
418 SPRoot *root = SP_ROOT(item);
419 CairoRenderer *renderer = ctx->getRenderer();
421 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
422 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
424 ctx->pushState();
425 renderer->setStateForItem(ctx, item);
426 Geom::Matrix tempmat (root->c2p);
427 ctx->transform(&tempmat);
428 sp_group_render(item, ctx);
429 ctx->popState();
430 }
432 /**
433 This function converts the item to a raster image and includes the image into the cairo renderer.
434 It is only used for filters and then only when rendering filters as bitmaps is requested.
435 */
436 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
437 {
439 // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
441 // Calculate resolution
442 double res;
443 /** @TODO reimplement the resolution stuff (WHY?)
444 */
445 res = ctx->getBitmapResolution();
446 if(res == 0) {
447 res = PX_PER_IN;
448 }
449 TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
451 // Get the bounding box of the selection in document coordinates.
452 Geom::OptRect bbox =
453 item->getBounds(item->i2d_affine(), SPItem::RENDERING_BBOX);
455 // no bbox, e.g. empty group
456 if (!bbox) {
457 return;
458 }
460 Geom::Rect docrect(Geom::Rect(Geom::Point(0, 0), SP_OBJECT(item)->document->getDimensions()));
461 Geom::Rect bboxrect(Geom::Rect(Geom::Point(bbox->min()[Geom::X], bbox->min()[Geom::Y]), Geom::Point(bbox->max()[Geom::X], bbox->max()[Geom::Y])));
463 Geom::OptRect _bbox = Geom::intersect(docrect, bboxrect);
465 // assign the object dimension clipped on the document, no need to draw on area not on canvas
466 bbox = _bbox;
468 // no bbox, e.g. empty group
469 if (!bbox) {
470 return;
471 }
473 // The width and height of the bitmap in pixels
474 unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
475 unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
477 // Scale to exactly fit integer bitmap inside bounding box
478 double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
479 double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
481 // Location of bounding box in document coordinates.
482 double shift_x = bbox->min()[Geom::X];
483 double shift_y = bbox->max()[Geom::Y];
485 // For default 90 dpi, snap bitmap to pixel grid
486 if (res == PX_PER_IN) {
487 shift_x = round (shift_x);
488 shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
489 // Remove the negations when the inversion is gone.
490 }
492 // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
494 // Matix to put bitmap in correct place on document
495 Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
496 (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
498 // ctx matrix already includes item transformation. We must substract.
499 Geom::Matrix t_item = item->i2d_affine ();
500 Geom::Matrix t = t_on_document * t_item.inverse();
502 // Do the export
503 SPDocument *document = SP_OBJECT(item)->document;
504 GSList *items = NULL;
505 items = g_slist_append(items, item);
507 GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
508 bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
509 width, height, res, res, (guint32) 0xffffff00, items );
511 if (pb) {
512 TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
513 unsigned char *px = gdk_pixbuf_get_pixels (pb);
514 unsigned int w = gdk_pixbuf_get_width(pb);
515 unsigned int h = gdk_pixbuf_get_height(pb);
516 unsigned int rs = gdk_pixbuf_get_rowstride(pb);
517 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
518 gdk_pixbuf_unref (pb);
519 }
520 g_slist_free (items);
521 }
524 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
525 {
526 // Check item's visibility
527 if (item->isHidden()) {
528 return;
529 }
531 SPStyle* style = SP_OBJECT_STYLE (item);
532 if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
533 return sp_asbitmap_render(item, ctx);
534 }
536 if (SP_IS_ROOT(item)) {
537 TRACE(("root\n"));
538 return sp_root_render(item, ctx);
539 } else if (SP_IS_GROUP(item)) {
540 TRACE(("group\n"));
541 return sp_group_render(item, ctx);
542 } else if (SP_IS_SHAPE(item)) {
543 TRACE(("shape\n"));
544 return sp_shape_render(item, ctx);
545 } else if (SP_IS_USE(item)) {
546 TRACE(("use begin---\n"));
547 sp_use_render(item, ctx);
548 TRACE(("---use end\n"));
549 } else if (SP_IS_SYMBOL(item)) {
550 TRACE(("symbol\n"));
551 return sp_symbol_render(item, ctx);
552 } else if (SP_IS_TEXT(item)) {
553 TRACE(("text\n"));
554 return sp_text_render(item, ctx);
555 } else if (SP_IS_FLOWTEXT(item)) {
556 TRACE(("flowtext\n"));
557 return sp_flowtext_render(item, ctx);
558 } else if (SP_IS_IMAGE(item)) {
559 TRACE(("image\n"));
560 return sp_image_render(item, ctx);
561 }
562 }
564 void
565 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
566 {
567 SPStyle const *style = SP_OBJECT_STYLE(item);
568 ctx->setStateForStyle(style);
570 CairoRenderState *state = ctx->getCurrentState();
571 state->clip_path = item->clip_ref->getObject();
572 state->mask = item->mask_ref->getObject();
573 state->item_transform = Geom::Matrix (item->transform);
575 // If parent_has_userspace is true the parent state's transform
576 // has to be used for the mask's/clippath's context.
577 // This is so because we use the image's/(flow)text's transform for positioning
578 // instead of explicitly specifying it and letting the renderer do the
579 // transformation before rendering the item.
580 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
581 state->parent_has_userspace = TRUE;
582 TRACE(("setStateForItem opacity: %f\n", state->opacity));
583 }
585 void
586 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
587 {
588 if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
589 // skip text if _omitText is true
590 return;
591 }
593 ctx->pushState();
594 setStateForItem(ctx, item);
596 CairoRenderState *state = ctx->getCurrentState();
597 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
599 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
600 if (state->need_layer) {
601 state->merge_opacity = FALSE;
602 ctx->pushLayer();
603 }
604 Geom::Matrix tempmat (item->transform);
605 ctx->transform(&tempmat);
606 sp_item_invoke_render(item, ctx);
608 if (state->need_layer)
609 ctx->popLayer();
611 ctx->popState();
612 }
614 bool
615 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
616 {
617 // PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
619 g_assert( ctx != NULL );
621 if (!base) {
622 base = SP_ITEM(doc->getRoot());
623 }
625 NRRect d;
626 if (pageBoundingBox) {
627 d.x0 = d.y0 = 0;
628 d.x1 = doc->getWidth();
629 d.y1 = doc->getHeight();
630 } else {
631 base->invoke_bbox( &d, base->i2d_affine(), TRUE, SPItem::RENDERING_BBOX);
632 }
634 if (ctx->_vector_based_target) {
635 // convert from px to pt
636 d.x0 *= PT_PER_PX;
637 d.x1 *= PT_PER_PX;
638 d.y0 *= PT_PER_PX;
639 d.y1 *= PT_PER_PX;
640 }
642 ctx->_width = d.x1-d.x0;
643 ctx->_height = d.y1-d.y0;
645 TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
647 bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
649 if (ret && !pageBoundingBox)
650 {
651 double high = doc->getHeight();
652 if (ctx->_vector_based_target)
653 high *= PT_PER_PX;
655 Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
656 (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
657 ctx->transform(&tp);
658 }
660 return ret;
661 }
663 #include "macros.h" // SP_PRINT_*
665 // Apply an SVG clip path
666 void
667 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
668 {
669 g_assert( ctx != NULL && ctx->_is_valid );
671 if (cp == NULL)
672 return;
674 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
675 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
677 Geom::Matrix saved_ctm;
678 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
679 //SP_PRINT_DRECT("clipd", cp->display->bbox);
680 NRRect clip_bbox(cp->display->bbox);
681 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
682 t[4] = clip_bbox.x0;
683 t[5] = clip_bbox.y0;
684 t *= ctx->getCurrentState()->transform;
685 ctx->getTransform(&saved_ctm);
686 ctx->setTransform(&t);
687 }
689 TRACE(("BEGIN clip\n"));
690 SPObject *co = SP_OBJECT(cp);
691 for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
692 if (SP_IS_ITEM(child)) {
693 SPItem *item = SP_ITEM(child);
695 // combine transform of the item in clippath and the item using clippath:
696 Geom::Matrix tempmat (item->transform);
697 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
699 // render this item in clippath
700 ctx->pushState();
701 ctx->transform(&tempmat);
702 setStateForItem(ctx, item);
703 sp_item_invoke_render(item, ctx);
704 ctx->popState();
705 }
706 }
707 TRACE(("END clip\n"));
709 // do clipping only if this was the first call to applyClipPath
710 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
711 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
712 cairo_clip(ctx->_cr);
714 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
715 ctx->setTransform(&saved_ctm);
717 ctx->setRenderMode(saved_mode);
718 }
720 // Apply an SVG mask
721 void
722 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
723 {
724 g_assert( ctx != NULL && ctx->_is_valid );
726 if (mask == NULL)
727 return;
729 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
730 NRRect mask_bbox(mask->display->bbox);
731 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
732 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
733 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
734 t[4] = mask_bbox.x0;
735 t[5] = mask_bbox.y0;
736 t *= ctx->getCurrentState()->transform;
737 ctx->setTransform(&t);
738 }
740 // Clip mask contents... but...
741 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
742 // filters which extend outside the bounding box. So don't clip.
743 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
745 ctx->pushState();
747 TRACE(("BEGIN mask\n"));
748 SPObject *co = SP_OBJECT(mask);
749 for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
750 if (SP_IS_ITEM(child)) {
751 SPItem *item = SP_ITEM(child);
752 renderItem(ctx, item);
753 }
754 }
755 TRACE(("END mask\n"));
757 ctx->popState();
758 }
760 void
761 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
762 double *x, double *y, double *width, double *height)
763 {
764 if (aspect_align == SP_ASPECT_NONE)
765 return;
767 double scalex, scaley, scale;
768 double new_width, new_height;
769 scalex = *width / vp_width;
770 scaley = *height / vp_height;
771 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
772 new_width = vp_width * scale;
773 new_height = vp_height * scale;
774 /* Now place viewbox to requested position */
775 switch (aspect_align) {
776 case SP_ASPECT_XMIN_YMIN:
777 break;
778 case SP_ASPECT_XMID_YMIN:
779 *x -= 0.5 * (new_width - *width);
780 break;
781 case SP_ASPECT_XMAX_YMIN:
782 *x -= 1.0 * (new_width - *width);
783 break;
784 case SP_ASPECT_XMIN_YMID:
785 *y -= 0.5 * (new_height - *height);
786 break;
787 case SP_ASPECT_XMID_YMID:
788 *x -= 0.5 * (new_width - *width);
789 *y -= 0.5 * (new_height - *height);
790 break;
791 case SP_ASPECT_XMAX_YMID:
792 *x -= 1.0 * (new_width - *width);
793 *y -= 0.5 * (new_height - *height);
794 break;
795 case SP_ASPECT_XMIN_YMAX:
796 *y -= 1.0 * (new_height - *height);
797 break;
798 case SP_ASPECT_XMID_YMAX:
799 *x -= 0.5 * (new_width - *width);
800 *y -= 1.0 * (new_height - *height);
801 break;
802 case SP_ASPECT_XMAX_YMAX:
803 *x -= 1.0 * (new_width - *width);
804 *y -= 1.0 * (new_height - *height);
805 break;
806 default:
807 break;
808 }
809 *width = new_width;
810 *height = new_height;
811 }
813 #include "clear-n_.h"
815 } /* namespace Internal */
816 } /* namespace Extension */
817 } /* namespace Inkscape */
819 #undef TRACE
821 /* End of GNU GPL code */
823 /*
824 Local Variables:
825 mode:c++
826 c-file-style:"stroustrup"
827 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
828 indent-tabs-mode:nil
829 fill-column:99
830 End:
831 */
832 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :