Code

more on cairo ps/pdf options
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
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)
107     /* restore default signal handling for SIGPIPE */
108 #if !defined(_WIN32) && !defined(__WIN32__)
109     (void) signal(SIGPIPE, SIG_DFL);
110 #endif
112     return;
115 CairoRenderContext*
116 CairoRenderer::createContext(void)
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;
133 void
134 CairoRenderer::destroyContext(CairoRenderContext *ctx)
136     delete ctx;
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)
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     }
199 static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
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     }
215 static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
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     }
238 static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
240     SPText *group = SP_TEXT (item);
241     group->layout.showGlyphs(ctx);
244 static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
246     SPFlowtext *group = SP_FLOWTEXT(item);
247     group->layout.showGlyphs(ctx);
250 static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
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));
288 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
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();
328 static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
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();
343 static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
345     /*
346     if(ctx->_state->has_filtereffect)
347         printf("\nhas filtereffects");
348     */
350     if (SP_IS_ROOT(item)) {
351         TRACE(("root\n"));
352         return sp_root_render(item, ctx);
353     } else if (SP_IS_GROUP(item)) {
354         TRACE(("group\n"));
355         return sp_group_render(item, ctx);
356     } else if (SP_IS_SHAPE(item)) {
357         TRACE(("shape\n"));
358         return sp_shape_render(item, ctx);
359     } else if (SP_IS_USE(item)) {
360         TRACE(("use begin---\n"));
361         sp_use_render(item, ctx);
362         TRACE(("---use end\n"));
363     } else if (SP_IS_SYMBOL(item)) {
364         TRACE(("symbol\n"));
365         return sp_symbol_render(item, ctx);
366     } else if (SP_IS_TEXT(item)) {
367         TRACE(("text\n"));
368         return sp_text_render(item, ctx);
369     } else if (SP_IS_FLOWTEXT(item)) {
370         TRACE(("flowtext\n"));
371         return sp_flowtext_render(item, ctx);
372     } else if (SP_IS_IMAGE(item)) {
373         TRACE(("image\n"));
374         return sp_image_render(item, ctx);
375     }
378 void
379 CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
381     SPStyle const *style = SP_OBJECT_STYLE(item);
382     ctx->setStateForStyle(style);
384     CairoRenderState *state = ctx->getCurrentState();
385     state->clip_path = item->clip_ref->getObject();
386     state->mask = item->mask_ref->getObject();
388     // If parent_has_userspace is true the parent state's transform
389     // has to be used for the mask's/clippath's context.
390     // This is so because we use the image's/(flow)text's transform for positioning
391     // instead of explicitly specifying it and letting the renderer do the
392     // transformation before rendering the item.
393     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
394         state->parent_has_userspace = TRUE;
395     TRACE(("set op: %f\n", state->opacity));
398 void
399 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
401     ctx->pushState();
402     setStateForItem(ctx, item);
404     CairoRenderState *state = ctx->getCurrentState();
405     state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
407     if (state->need_layer) {
408         state->merge_opacity = FALSE;
409         ctx->pushLayer();
410     }
411     ctx->transform(item->transform);
412     sp_item_invoke_render(item, ctx);
414     if (state->need_layer)
415         ctx->popLayer();
417     ctx->popState();
420 bool
421 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
423     g_assert( ctx != NULL );
425     if (ctx->_vector_based_target) {
426         // width and height in pt
427         ctx->_width = sp_document_width(doc) * PT_PER_PX;
428         ctx->_height = sp_document_height(doc) * PT_PER_PX;
429     } else {
430         ctx->_width = sp_document_width(doc);
431         ctx->_height = sp_document_height(doc);
432     }
434     NRRect d;
435     bool pageBoundingBox = true;
436     if (pageBoundingBox) {
437         d.x0 = d.y0 = 0;
438         d.x1 = ceil(ctx->_width);
439         d.y1 = ceil(ctx->_height);
440     } else {
441         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
442         sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
443         if (ctx->_vector_based_target) {
444             // convert from px to pt
445             d.x0 *= PT_PER_PX;
446             d.x1 *= PT_PER_PX;
447             d.y0 *= PT_PER_PX;
448             d.y1 *= PT_PER_PX;
449         }
450     }
451     TRACE(("%f x %f\n", ctx->_width, ctx->_height));
453     return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
456 #include "macros.h" // SP_PRINT_*
458 void
459 CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
461     g_assert( ctx != NULL && ctx->_is_valid );
463     if (cp == NULL)
464         return;
466     CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
467     ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
469     NRMatrix saved_ctm;
470     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
471         NRMatrix t;
472         //SP_PRINT_DRECT("clipd", cp->display->bbox);
473         NRRect clip_bbox(cp->display->bbox);
474         nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
475         t.c[4] = clip_bbox.x0;
476         t.c[5] = clip_bbox.y0;
477         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
478         ctx->getTransform(&saved_ctm);
479         ctx->setTransform(&t);
480     }
482     TRACE(("BEGIN clip\n"));
483     SPObject *co = SP_OBJECT(cp);
484     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
485         if (SP_IS_ITEM(child)) {
486             SPItem *item = SP_ITEM(child);
487             renderItem(ctx, item);
488         }
489     }
490     TRACE(("END clip\n"));
492     // do clipping only if this was the first call to applyClipPath
493     if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
494         && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
495         cairo_clip(ctx->_cr);
497     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
498         ctx->setTransform(&saved_ctm);
500     ctx->setRenderMode(saved_mode);
503 void
504 CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
506     g_assert( ctx != NULL && ctx->_is_valid );
508     if (mask == NULL)
509         return;
511     //SP_PRINT_DRECT("maskd", &mask->display->bbox);
512     NRRect mask_bbox(mask->display->bbox);
513     // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
514     if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
515         NRMatrix t;
516         nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
517         t.c[4] = mask_bbox.x0;
518         t.c[5] = mask_bbox.y0;
519         nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
520         ctx->setTransform(&t);
521     }
523     // clip mask contents
524     ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
526     ctx->pushState();
528     TRACE(("BEGIN mask\n"));
529     SPObject *co = SP_OBJECT(mask);
530     for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
531         if (SP_IS_ITEM(child)) {
532             SPItem *item = SP_ITEM(child);
533             renderItem(ctx, item);
534         }
535     }
536     TRACE(("END mask\n"));
538     ctx->popState();
541 void
542 calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
543                              double *x, double *y, double *width, double *height)
545     if (aspect_align == SP_ASPECT_NONE)
546         return;
548     double scalex, scaley, scale;
549     double new_width, new_height;
550     scalex = *width / vp_width;
551     scaley = *height / vp_height;
552     scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
553     new_width = vp_width * scale;
554     new_height = vp_height * scale;
555     /* Now place viewbox to requested position */
556     switch (aspect_align) {
557         case SP_ASPECT_XMIN_YMIN:
558             break;
559         case SP_ASPECT_XMID_YMIN:
560             *x -= 0.5 * (new_width - *width);
561             break;
562         case SP_ASPECT_XMAX_YMIN:
563             *x -= 1.0 * (new_width - *width);
564             break;
565         case SP_ASPECT_XMIN_YMID:
566             *y -= 0.5 * (new_height - *height);
567             break;
568         case SP_ASPECT_XMID_YMID:
569             *x -= 0.5 * (new_width - *width);
570             *y -= 0.5 * (new_height - *height);
571             break;
572         case SP_ASPECT_XMAX_YMID:
573             *x -= 1.0 * (new_width - *width);
574             *y -= 0.5 * (new_height - *height);
575             break;
576         case SP_ASPECT_XMIN_YMAX:
577             *y -= 1.0 * (new_height - *height);
578             break;
579         case SP_ASPECT_XMID_YMAX:
580             *x -= 0.5 * (new_width - *width);
581             *y -= 1.0 * (new_height - *height);
582             break;
583         case SP_ASPECT_XMAX_YMAX:
584             *x -= 1.0 * (new_width - *width);
585             *y -= 1.0 * (new_height - *height);
586             break;
587         default:
588             break;
589     }
590     *width = new_width;
591     *height = new_height;
594 #include "clear-n_.h"
596 }  /* namespace Internal */
597 }  /* namespace Extension */
598 }  /* namespace Inkscape */
600 #undef TRACE
602 /* End of GNU GPL code */
604 /*
605   Local Variables:
606   mode:c++
607   c-file-style:"stroustrup"
608   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609   indent-tabs-mode:nil
610   fill-column:99
611   End:
612 */
613 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :