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 #ifdef HAVE_CAIRO_PDF
21 #ifndef PANGO_ENABLE_BACKEND
22 #define PANGO_ENABLE_BACKEND
23 #endif
25 #ifndef PANGO_ENABLE_ENGINE
26 #define PANGO_ENABLE_ENGINE
27 #endif
30 #include <signal.h>
31 #include <errno.h>
33 #include <libnr/n-art-bpath.h>
34 #include <libnr/nr-matrix-ops.h>
35 #include <libnr/nr-matrix-fns.h>
36 #include <libnr/nr-matrix-translate-ops.h>
37 #include <libnr/nr-scale-matrix-ops.h>
39 #include <glib/gmem.h>
41 #include <glibmm/i18n.h>
42 #include "display/nr-arena.h"
43 #include "display/nr-arena-item.h"
44 #include "display/nr-arena-group.h"
45 #include "display/curve.h"
46 #include "display/canvas-bpath.h"
47 #include "sp-item.h"
48 #include "sp-item-group.h"
49 #include "marker-status.h"
50 #include "style.h"
51 #include "sp-marker.h"
52 #include "sp-linear-gradient.h"
53 #include "sp-radial-gradient.h"
54 #include "sp-root.h"
55 #include "sp-shape.h"
56 #include "sp-use.h"
57 #include "sp-text.h"
58 #include "sp-flowtext.h"
59 #include "sp-image.h"
60 #include "sp-symbol.h"
61 #include "sp-pattern.h"
62 #include "sp-mask.h"
63 #include "sp-clippath.h"
65 #include <unit-constants.h>
67 #include "cairo-renderer.h"
68 #include "cairo-render-context.h"
69 #include "extension/system.h"
71 #include "io/sys.h"
73 #include <cairo.h>
75 // include support for only the compiled-in surface types
76 #ifdef CAIRO_HAS_PDF_SURFACE
77 #include <cairo-pdf.h>
78 #endif
79 #ifdef CAIRO_HAS_PS_SURFACE
80 #include <cairo-ps.h>
81 #endif
83 //#define TRACE(_args) g_printf _args
84 #define TRACE(_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 {}
108 CairoRenderer::~CairoRenderer(void)
109 {
110 /* restore default signal handling for SIGPIPE */
111 #if !defined(_WIN32) && !defined(__WIN32__)
112 (void) signal(SIGPIPE, SIG_DFL);
113 #endif
115 return;
116 }
118 CairoRenderContext*
119 CairoRenderer::createContext(void)
120 {
121 CairoRenderContext *new_context = new CairoRenderContext(this);
122 g_assert( new_context != NULL );
124 new_context->_state_stack = NULL;
125 new_context->_state = NULL;
127 // create initial render state
128 CairoRenderState *state = new_context->_createState();
129 nr_matrix_set_identity(&state->transform);
130 new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
131 new_context->_state = state;
133 return new_context;
134 }
136 void
137 CairoRenderer::destroyContext(CairoRenderContext *ctx)
138 {
139 delete ctx;
140 }
142 /*
144 Here comes the rendering part which could be put into the 'render' methods of SPItems'
146 */
148 /* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
149 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
150 static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
151 static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
152 static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
153 static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
154 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
155 static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
156 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
158 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
159 {
160 NRRect pbox;
162 SPShape *shape = SP_SHAPE(item);
164 if (!shape->curve) return;
166 /* fixme: Think (Lauris) */
167 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
168 NR::Matrix const i2d = sp_item_i2d_affine(item);
170 SPStyle* style = SP_OBJECT_STYLE (item);
171 CairoRenderer *renderer = ctx->getRenderer();
173 NRBPath bp;
174 bp.path = SP_CURVE_BPATH(shape->curve);
176 ctx->renderPath(&bp, style, &pbox);
178 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
179 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
180 if (sp_shape_marker_required (shape, m, bp)) {
182 SPMarker* marker = SP_MARKER (shape->marker[m]);
183 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
185 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
187 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
188 tr = NR::scale(style->stroke_width.computed) * tr;
189 }
191 tr = marker_item->transform * marker->c2p * tr;
193 NR::Matrix old_tr = marker_item->transform;
194 marker_item->transform = tr;
195 renderer->renderItem (ctx, marker_item);
196 marker_item->transform = old_tr;
197 }
198 }
199 }
200 }
202 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
203 {
204 SPGroup *group = SP_GROUP(item);
205 CairoRenderer *renderer = ctx->getRenderer();
206 TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
208 GSList *l = g_slist_reverse(group->childList(false));
209 while (l) {
210 SPObject *o = SP_OBJECT (l->data);
211 if (SP_IS_ITEM(o)) {
212 renderer->renderItem (ctx, SP_ITEM (o));
213 }
214 l = g_slist_remove (l, o);
215 }
216 }
218 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
219 {
220 bool translated = false;
221 NRMatrix tp;
222 SPUse *use = SP_USE(item);
223 CairoRenderer *renderer = ctx->getRenderer();
225 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
226 nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
227 ctx->pushState();
228 ctx->transform(&tp);
229 translated = true;
230 }
232 if (use->child && SP_IS_ITEM(use->child)) {
233 renderer->renderItem(ctx, SP_ITEM(use->child));
234 }
236 if (translated) {
237 ctx->popState();
238 }
239 }
241 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
242 {
243 SPText *group = SP_TEXT (item);
244 group->layout.showGlyphs(ctx);
245 }
247 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
248 {
249 SPFlowtext *group = SP_FLOWTEXT(item);
250 group->layout.showGlyphs(ctx);
251 }
253 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
254 {
255 SPImage *image;
256 NRMatrix tp, s, t;
257 guchar *px;
258 int w, h, rs;
260 image = SP_IMAGE (item);
262 if (!image->pixbuf) return;
263 if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
265 px = gdk_pixbuf_get_pixels (image->pixbuf);
266 w = gdk_pixbuf_get_width (image->pixbuf);
267 h = gdk_pixbuf_get_height (image->pixbuf);
268 rs = gdk_pixbuf_get_rowstride (image->pixbuf);
270 double x = image->x.computed;
271 double y = image->y.computed;
272 double width = image->width.computed;
273 double height = image->height.computed;
275 if (image->aspect_align != SP_ASPECT_NONE) {
276 calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
277 &x, &y, &width, &height);
278 }
280 if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
281 ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
282 }
284 nr_matrix_set_translate (&tp, x, y);
285 nr_matrix_set_scale (&s, width / (double)w, height / (double)h);
286 nr_matrix_multiply (&t, &s, &tp);
288 ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
289 }
291 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
292 {
293 SPSymbol *symbol = SP_SYMBOL(item);
294 if (!SP_OBJECT_IS_CLONED (symbol))
295 return;
297 /* Cloned <symbol> is actually renderable */
298 ctx->pushState();
299 ctx->transform(&symbol->c2p);
301 // apply viewbox if set
302 if (0 /*symbol->viewBox_set*/) {
303 NRMatrix vb2user;
304 double x, y, width, height;
305 double view_width, view_height;
306 x = 0.0;
307 y = 0.0;
308 width = 1.0;
309 height = 1.0;
311 view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
312 view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
314 calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
315 &x, &y,&width, &height);
317 // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
318 nr_matrix_set_identity(&vb2user);
319 vb2user[0] = width / view_width;
320 vb2user[3] = height / view_height;
321 vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
322 vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
324 ctx->transform(&vb2user);
325 }
327 sp_group_render(item, ctx);
328 ctx->popState();
329 }
331 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
332 {
333 SPRoot *root = SP_ROOT(item);
334 CairoRenderer *renderer = ctx->getRenderer();
336 if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
337 ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
339 ctx->pushState();
340 renderer->setStateForItem(ctx, item);
341 ctx->transform(root->c2p);
342 sp_group_render(item, ctx);
343 ctx->popState();
344 }
346 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
347 {
348 if (SP_IS_ROOT(item)) {
349 TRACE(("root\n"));
350 return sp_root_render(item, ctx);
351 } else if (SP_IS_GROUP(item)) {
352 TRACE(("group\n"));
353 return sp_group_render(item, ctx);
354 } else if (SP_IS_SHAPE(item)) {
355 TRACE(("shape\n"));
356 return sp_shape_render(item, ctx);
357 } else if (SP_IS_USE(item)) {
358 TRACE(("use begin---\n"));
359 sp_use_render(item, ctx);
360 TRACE(("---use end\n"));
361 } else if (SP_IS_SYMBOL(item)) {
362 TRACE(("symbol\n"));
363 return sp_symbol_render(item, ctx);
364 } else if (SP_IS_TEXT(item)) {
365 TRACE(("text\n"));
366 return sp_text_render(item, ctx);
367 } else if (SP_IS_FLOWTEXT(item)) {
368 TRACE(("flowtext\n"));
369 return sp_flowtext_render(item, ctx);
370 } else if (SP_IS_IMAGE(item)) {
371 TRACE(("image\n"));
372 return sp_image_render(item, ctx);
373 }
374 }
376 void
377 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
378 {
379 SPStyle const *style = SP_OBJECT_STYLE(item);
380 ctx->setStateForStyle(style);
382 CairoRenderState *state = ctx->getCurrentState();
383 state->clip_path = item->clip_ref->getObject();
384 state->mask = item->mask_ref->getObject();
386 // If parent_has_userspace is true the parent state's transform
387 // has to be used for the mask's/clippath's context.
388 // This is so because we use the image's/(flow)text's transform for positioning
389 // instead of explicitly specifying it and letting the renderer do the
390 // transformation before rendering the item.
391 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
392 state->parent_has_userspace = TRUE;
393 TRACE(("set op: %f\n", state->opacity));
394 }
396 void
397 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
398 {
399 ctx->pushState();
400 setStateForItem(ctx, item);
402 CairoRenderState *state = ctx->getCurrentState();
403 state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
405 if (state->need_layer) {
406 state->merge_opacity = FALSE;
407 ctx->pushLayer();
408 }
409 ctx->transform(item->transform);
410 sp_item_invoke_render(item, ctx);
412 if (state->need_layer)
413 ctx->popLayer();
415 ctx->popState();
416 }
418 bool
419 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
420 {
421 g_assert( ctx != NULL );
423 if (ctx->_vector_based_target) {
424 // width and height in pt
425 ctx->_width = sp_document_width(doc) * PT_PER_PX;
426 ctx->_height = sp_document_height(doc) * PT_PER_PX;
427 } else {
428 ctx->_width = sp_document_width(doc);
429 ctx->_height = sp_document_height(doc);
430 }
432 NRRect d;
433 bool pageBoundingBox = true;
434 if (pageBoundingBox) {
435 d.x0 = d.y0 = 0;
436 d.x1 = ceil(ctx->_width);
437 d.y1 = ceil(ctx->_height);
438 } else {
439 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
440 sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
441 if (ctx->_vector_based_target) {
442 // convert from px to pt
443 d.x0 *= PT_PER_PX;
444 d.x1 *= PT_PER_PX;
445 d.y0 *= PT_PER_PX;
446 d.y1 *= PT_PER_PX;
447 }
448 }
449 TRACE(("%f x %f\n", ctx->_width, ctx->_height));
451 return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
452 }
454 #include "macros.h" // SP_PRINT_*
456 void
457 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
458 {
459 g_assert( ctx != NULL && ctx->_is_valid );
461 if (cp == NULL)
462 return;
464 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
465 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
467 NRMatrix saved_ctm;
468 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
469 NRMatrix t;
470 //SP_PRINT_DRECT("clipd", cp->display->bbox);
471 NRRect clip_bbox(cp->display->bbox);
472 nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
473 t.c[4] = clip_bbox.x0;
474 t.c[5] = clip_bbox.y0;
475 nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
476 ctx->getTransform(&saved_ctm);
477 ctx->setTransform(&t);
478 }
480 TRACE(("BEGIN clip\n"));
481 SPObject *co = SP_OBJECT(cp);
482 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
483 if (SP_IS_ITEM(child)) {
484 SPItem *item = SP_ITEM(child);
485 renderItem(ctx, item);
486 }
487 }
488 TRACE(("END clip\n"));
490 // do clipping only if this was the first call to applyClipPath
491 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
492 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
493 cairo_clip(ctx->_cr);
495 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
496 ctx->setTransform(&saved_ctm);
498 ctx->setRenderMode(saved_mode);
499 }
501 void
502 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
503 {
504 g_assert( ctx != NULL && ctx->_is_valid );
506 if (mask == NULL)
507 return;
509 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
510 NRRect mask_bbox(mask->display->bbox);
511 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
512 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
513 NRMatrix t;
514 nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
515 t.c[4] = mask_bbox.x0;
516 t.c[5] = mask_bbox.y0;
517 nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
518 ctx->setTransform(&t);
519 }
521 // clip mask contents
522 ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
524 ctx->pushState();
526 TRACE(("BEGIN mask\n"));
527 SPObject *co = SP_OBJECT(mask);
528 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
529 if (SP_IS_ITEM(child)) {
530 SPItem *item = SP_ITEM(child);
531 renderItem(ctx, item);
532 }
533 }
534 TRACE(("END mask\n"));
536 ctx->popState();
537 }
539 void
540 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
541 double *x, double *y, double *width, double *height)
542 {
543 if (aspect_align == SP_ASPECT_NONE)
544 return;
546 double scalex, scaley, scale;
547 double new_width, new_height;
548 scalex = *width / vp_width;
549 scaley = *height / vp_height;
550 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
551 new_width = vp_width * scale;
552 new_height = vp_height * scale;
553 /* Now place viewbox to requested position */
554 switch (aspect_align) {
555 case SP_ASPECT_XMIN_YMIN:
556 break;
557 case SP_ASPECT_XMID_YMIN:
558 *x -= 0.5 * (new_width - *width);
559 break;
560 case SP_ASPECT_XMAX_YMIN:
561 *x -= 1.0 * (new_width - *width);
562 break;
563 case SP_ASPECT_XMIN_YMID:
564 *y -= 0.5 * (new_height - *height);
565 break;
566 case SP_ASPECT_XMID_YMID:
567 *x -= 0.5 * (new_width - *width);
568 *y -= 0.5 * (new_height - *height);
569 break;
570 case SP_ASPECT_XMAX_YMID:
571 *x -= 1.0 * (new_width - *width);
572 *y -= 0.5 * (new_height - *height);
573 break;
574 case SP_ASPECT_XMIN_YMAX:
575 *y -= 1.0 * (new_height - *height);
576 break;
577 case SP_ASPECT_XMID_YMAX:
578 *x -= 0.5 * (new_width - *width);
579 *y -= 1.0 * (new_height - *height);
580 break;
581 case SP_ASPECT_XMAX_YMAX:
582 *x -= 1.0 * (new_width - *width);
583 *y -= 1.0 * (new_height - *height);
584 break;
585 default:
586 break;
587 }
588 *width = new_width;
589 *height = new_height;
590 }
592 #include "clear-n_.h"
594 } /* namespace Internal */
595 } /* namespace Extension */
596 } /* namespace Inkscape */
598 #undef TRACE
600 /* End of GNU GPL code */
602 #endif /* HAVE_CAIRO_PDF */
605 /*
606 Local Variables:
607 mode:c++
608 c-file-style:"stroustrup"
609 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
610 indent-tabs-mode:nil
611 fill-column:99
612 End:
613 */
614 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :