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