cairo-renderer.cpp revision d16495c17bbc27ff37e18ea3eb61f6b07448152a
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/** \file
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Rendering with Cairo.
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/*
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Author:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Miklos Erdelyi <erdelyim@gmail.com>
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz * Jon A. Cruz <jon@joncruz.org>
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz * Abhishek Sharma
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Copyright (C) 2006 Miklos Erdelyi
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib *
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Licensed under GNU GPL
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#ifdef HAVE_CONFIG_H
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh# include "config.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#ifndef PANGO_ENABLE_BACKEND
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#define PANGO_ENABLE_BACKEND
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#ifndef PANGO_ENABLE_ENGINE
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#define PANGO_ENABLE_ENGINE
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <signal.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <errno.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
16a8c7d5e433b176636a4a1260c42ea43932110bKrzysztof Kosiński#include "libnrtype/Layout-TNG.h"
54e660c4de9d37185e3953165d053526632ef4f0johanengelen#include <2geom/transforms.h>
54e660c4de9d37185e3953165d053526632ef4f0johanengelen#include <2geom/pathvector.h>
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
c0537dcfe264414d52ad86579d57cb0cb2183dcbAlex Valavanis#include <glib.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <glibmm/i18n.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "display/curve.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "display/canvas-bpath.h"
e141c94157efa47e3af32fecda00153812986ac2Krzysztof Kosiński#include "display/cairo-utils.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-item.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-item-group.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "style.h"
dd69425007680aafc47fdd994e1985625571d252bryce#include "marker.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-linear-gradient.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-radial-gradient.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-root.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-shape.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-use.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-text.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-flowtext.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-image.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-symbol.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-pattern.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-mask.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "sp-clippath.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff#include "util/units.h"
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib#include "helper/png-write.h"
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib#include "helper/pixbuf-ops.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "cairo-renderer.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "cairo-render-context.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "extension/system.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "io/sys.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <cairo.h>
090c3e1515e9a641ea1f540e4c7563d1d489c2ebAlex Valavanis#include "document.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh// include support for only the compiled-in surface types
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#ifdef CAIRO_HAS_PDF_SURFACE
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <cairo-pdf.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#ifdef CAIRO_HAS_PS_SURFACE
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include <cairo-ps.h>
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh//#define TRACE(_args) g_printf _args
d444ed610362dab3c3e727d1b110312318b84cb8miklosh#define TRACE(_args)
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould//#define TEST(_args) _args
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould#define TEST(_args)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh// FIXME: expose these from sp-clippath/mask.cpp
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstruct SPClipPathView {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPClipPathView *next;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh unsigned int key;
d1bde559850436556ebee2e70e10f1cfc8aff636Krzysztof Kosiński Inkscape::DrawingItem *arenaitem;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::OptRect bbox;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh};
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstruct SPMaskView {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPMaskView *next;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh unsigned int key;
d1bde559850436556ebee2e70e10f1cfc8aff636Krzysztof Kosiński Inkscape::DrawingItem *arenaitem;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::OptRect bbox;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh};
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshnamespace Inkscape {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshnamespace Extension {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshnamespace Internal {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::CairoRenderer(void)
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen : _omitText(false)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::~CairoRenderer(void)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* restore default signal handling for SIGPIPE */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#if !defined(_WIN32) && !defined(__WIN32__)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh (void) signal(SIGPIPE, SIG_DFL);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#endif
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderContext*
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::createContext(void)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderContext *new_context = new CairoRenderContext(this);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh g_assert( new_context != NULL );
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_context->_state_stack = NULL;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_context->_state = NULL;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // create initial render state
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderState *state = new_context->_createState();
17d87f5698f5c2958d38c6a6207c7b322a7adaf9johanengelen state->transform = Geom::identity();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_context->_state = state;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return new_context;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshvoid
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::destroyContext(CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib delete ctx;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/*
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshHere comes the rendering part which could be put into the 'render' methods of SPItems'
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh*/
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_group_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_use_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_text_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_image_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadibstatic void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelenstatic void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Affine tr, SPStyle* style, CairoRenderContext *ctx)
05a66d0771cb563225b8690de04e9490b35453f2johanengelen{
05a66d0771cb563225b8690de04e9490b35453f2johanengelen bool render = true;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (style->stroke_width.computed > 1e-9) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = Geom::Scale(style->stroke_width.computed) * tr;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen } else {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen render = false; // stroke width zero and marker is thus scaled down to zero, skip
05a66d0771cb563225b8690de04e9490b35453f2johanengelen }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (render) {
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPItem* marker_item = sp_item_first_item_child(marker);
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen if (marker_item) {
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen tr = (Geom::Affine)marker_item->transform * (Geom::Affine)marker->c2p * tr;
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen Geom::Affine old_tr = marker_item->transform;
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen marker_item->transform = tr;
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen ctx->getRenderer()->renderItem (ctx, marker_item);
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen marker_item->transform = old_tr;
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen}
05a66d0771cb563225b8690de04e9490b35453f2johanengelen
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPShape *shape = SP_SHAPE(item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen if (!shape->_curve) {
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz return;
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::OptRect pbox = item->geometricBounds();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPStyle* style = item->style;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen Geom::PathVector const & pathv = shape->_curve->get_pathvector();
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz if (pathv.empty()) {
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz return;
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->renderPathVector(pathv, style, pbox);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // START marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen if ( shape->_marker[i] ) {
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen SPMarker* marker = SP_MARKER (shape->_marker[i]);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tr;
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if (marker->orient_auto) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
68b9e386db1a231abaddbfed4d05f7539bdac786scislac } else {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // MID marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen if ( !shape->_marker[i] ) continue;
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen SPMarker* marker = SP_MARKER (shape->_marker[i]);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // START position
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if ( path_it != pathv.begin()
68b9e386db1a231abaddbfed4d05f7539bdac786scislac && ! ((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
68b9e386db1a231abaddbfed4d05f7539bdac786scislac {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tr;
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould if (marker->orient_auto) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform_at_start(path_it->front());
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould } else {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
05a66d0771cb563225b8690de04e9490b35453f2johanengelen }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // MID position
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if (path_it->size_default() > 1) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
05a66d0771cb563225b8690de04e9490b35453f2johanengelen Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
05a66d0771cb563225b8690de04e9490b35453f2johanengelen while (curve_it2 != path_it->end_default())
05a66d0771cb563225b8690de04e9490b35453f2johanengelen {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen /* Put marker between curve_it1 and curve_it2.
05a66d0771cb563225b8690de04e9490b35453f2johanengelen * Loop to end_default (so including closing segment), because when a path is closed,
05a66d0771cb563225b8690de04e9490b35453f2johanengelen * there should be a midpoint marker between last segment and closing straight line segment */
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tr;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (marker->orient_auto) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
7079a43aa387066c2f67402d77dbe3db981b1054Ted Gould } else {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
7079a43aa387066c2f67402d77dbe3db981b1054Ted Gould }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
54e660c4de9d37185e3953165d053526632ef4f0johanengelen
05a66d0771cb563225b8690de04e9490b35453f2johanengelen ++curve_it1;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen ++curve_it2;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // END position
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if ( path_it != (pathv.end()-1) && !path_it->empty()) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac Geom::Curve const &lastcurve = path_it->back_default();
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tr;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (marker->orient_auto) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform_at_end(lastcurve);
7079a43aa387066c2f67402d77dbe3db981b1054Ted Gould } else {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
7079a43aa387066c2f67402d77dbe3db981b1054Ted Gould }
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
7079a43aa387066c2f67402d77dbe3db981b1054Ted Gould }
54e660c4de9d37185e3953165d053526632ef4f0johanengelen }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // END marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen if ( shape->_marker[i] ) {
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen SPMarker* marker = SP_MARKER (shape->_marker[i]);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac
68b9e386db1a231abaddbfed4d05f7539bdac786scislac /* Get reference to last curve in the path.
68b9e386db1a231abaddbfed4d05f7539bdac786scislac * For moveto-only path, this returns the "closing line segment". */
68b9e386db1a231abaddbfed4d05f7539bdac786scislac Geom::Path const &path_last = pathv.back();
68b9e386db1a231abaddbfed4d05f7539bdac786scislac unsigned int index = path_last.size_default();
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if (index > 0) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac index--;
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac Geom::Curve const &lastcurve = path_last[index];
68b9e386db1a231abaddbfed4d05f7539bdac786scislac
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tr;
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if (marker->orient_auto) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = sp_shape_marker_get_transform_at_end(lastcurve);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac } else {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac
68b9e386db1a231abaddbfed4d05f7539bdac786scislac sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
68b9e386db1a231abaddbfed4d05f7539bdac786scislac }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_group_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPGroup *group = SP_GROUP(item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderer *renderer = ctx->getRenderer();
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(item->style->opacity.value)));
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh GSList *l = g_slist_reverse(group->childList(false));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh while (l) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPObject *o = SP_OBJECT (l->data);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (SP_IS_ITEM(o)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh renderer->renderItem (ctx, SP_ITEM (o));
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh l = g_slist_remove (l, o);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_use_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh bool translated = false;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPUse *use = SP_USE(item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderer *renderer = ctx->getRenderer();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine tp(Geom::Translate(use->x.computed, use->y.computed));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushState();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(tp);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh translated = true;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (use->child && SP_IS_ITEM(use->child)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh renderer->renderItem(ctx, SP_ITEM(use->child));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (translated) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_text_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPText *group = SP_TEXT (item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh group->layout.showGlyphs(ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPFlowtext *group = SP_FLOWTEXT(item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh group->layout.showGlyphs(ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_image_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPImage *image;
e141c94157efa47e3af32fecda00153812986ac2Krzysztof Kosiński int w, h;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh image = SP_IMAGE (item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (!image->pixbuf) return;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh w = gdk_pixbuf_get_width (image->pixbuf);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh h = gdk_pixbuf_get_height (image->pixbuf);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double x = image->x.computed;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double y = image->y.computed;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double width = image->width.computed;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double height = image->height.computed;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (image->aspect_align != SP_ASPECT_NONE) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh &x, &y, &width, &height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
17d87f5698f5c2958d38c6a6207c7b322a7adaf9johanengelen Geom::Translate tp(x, y);
17d87f5698f5c2958d38c6a6207c7b322a7adaf9johanengelen Geom::Scale s(width / (double)w, height / (double)h);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine t(s * tp);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->renderImage (image->pixbuf, t, item->style);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh SPSymbol *symbol = SP_SYMBOL(item);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz if (!symbol->cloned) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return;
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz }
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* Cloned <symbol> is actually renderable */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushState();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(symbol->c2p);
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // apply viewbox if set
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (0 /*symbol->viewBox_set*/) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine vb2user;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double x, y, width, height;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double view_width, view_height;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh x = 0.0;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh y = 0.0;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh width = 1.0;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh height = 1.0;
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski view_width = symbol->viewBox.width();
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski view_height = symbol->viewBox.height();
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh &x, &y,&width, &height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
17d87f5698f5c2958d38c6a6207c7b322a7adaf9johanengelen vb2user = Geom::identity();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh vb2user[0] = width / view_width;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh vb2user[3] = height / view_height;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski vb2user[4] = x - symbol->viewBox.left() * vb2user[0];
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski vb2user[5] = y - symbol->viewBox.top() * vb2user[3];
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(vb2user);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh sp_group_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruzstatic void sp_root_render(SPRoot *root, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderer *renderer = ctx->getRenderer();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruz if (!ctx->getCurrentState()->has_overflow && root->parent)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushState();
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruz renderer->setStateForItem(ctx, root);
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(root->c2p);
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruz sp_group_render(root, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib/**
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould This function converts the item to a raster image and includes the image into the cairo renderer.
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould It is only used for filters and then only when rendering filters as bitmaps is requested.
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib*/
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadibstatic void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib{
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // Calculate resolution
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib double res;
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould /** @TODO reimplement the resolution stuff (WHY?)
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib */
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib res = ctx->getBitmapResolution();
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib if(res == 0) {
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff res = Inkscape::Util::Quantity::convert(1, "in", "px");
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib }
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski // Get the bounding box of the selection in desktop coordinates.
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::OptRect bbox = item->desktopVisualBounds();
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib // no bbox, e.g. empty group
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib if (!bbox) {
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould return;
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib }
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz Geom::Rect docrect(Geom::Rect(Geom::Point(0, 0), item->document->getDimensions()));
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski bbox &= docrect;
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib // no bbox, e.g. empty group
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib if (!bbox) {
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib return;
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib }
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // The width and height of the bitmap in pixels
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff unsigned width = ceil(bbox->width() * (res / Inkscape::Util::Quantity::convert(1, "in", "px")));
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff unsigned height = ceil(bbox->height() * (res / Inkscape::Util::Quantity::convert(1, "in", "px")));
e375f7b0799058a61b78199acdbc9b82696f1ae2Krzysztof Kosiński
e375f7b0799058a61b78199acdbc9b82696f1ae2Krzysztof Kosiński if (width == 0 || height == 0) return;
e375f7b0799058a61b78199acdbc9b82696f1ae2Krzysztof Kosiński
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Scale to exactly fit integer bitmap inside bounding box
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski double scale_x = bbox->width() / width;
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski double scale_y = bbox->height() / height;
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Location of bounding box in document coordinates.
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould double shift_x = bbox->min()[Geom::X];
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould double shift_y = bbox->max()[Geom::Y];
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // For default 90 dpi, snap bitmap to pixel grid
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff if (res == Inkscape::Util::Quantity::convert(1, "in", "px")) {
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib shift_x = round (shift_x);
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Remove the negations when the inversion is gone.
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib }
45d0b0d0dc24df8e321cbe8a085ab9b1f60b4a42theadib
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Matix to put bitmap in correct place on document
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine t_on_document = (Geom::Affine)(Geom::Scale (scale_x, -scale_y)) *
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen (Geom::Affine)(Geom::Translate (shift_x, shift_y));
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // ctx matrix already includes item transformation. We must substract.
e898e8dd3fbc3e21f3a35f089a0d1c1dc6d48850Johan Engelen Geom::Affine t_item = item->i2dt_affine ();
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine t = t_on_document * t_item.inverse();
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // Do the export
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPDocument *document = item->document;
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould GSList *items = NULL;
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould items = g_slist_append(items, item);
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould
45d0b0d0dc24df8e321cbe8a085ab9b1f60b4a42theadib GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
3bd80e038b72ad220f218d7053a9089685fab17cTed Gould width, height, res, res, (guint32) 0xffffff00, items );
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib if (pb) {
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
e141c94157efa47e3af32fecda00153812986ac2Krzysztof Kosiński // TODO this is stupid - we just converted to pixbuf format when generating the bitmap!
e141c94157efa47e3af32fecda00153812986ac2Krzysztof Kosiński convert_pixbuf_normal_to_argb32(pb);
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->renderImage(pb, t, item->style);
fa0af17006bbe42b4e7bee7b7d669c25e7d65e0cJohn Smith g_object_unref(pb);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz pb = 0;
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib }
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib g_slist_free (items);
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib}
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak // Check item's visibility
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak if (item->isHidden()) {
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak return;
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak }
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPStyle* style = item->style;
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
45d0b0d0dc24df8e321cbe8a085ab9b1f60b4a42theadib return sp_asbitmap_render(item, ctx);
45d0b0d0dc24df8e321cbe8a085ab9b1f60b4a42theadib }
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (SP_IS_ROOT(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("root\n"));
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruz return sp_root_render(SP_ROOT(item), ctx);
38a54b600f5dae18f0432b96d0fbda699c31b974theAdib } else if (SP_IS_SYMBOL(item)) {
38a54b600f5dae18f0432b96d0fbda699c31b974theAdib TRACE(("symbol\n"));
38a54b600f5dae18f0432b96d0fbda699c31b974theAdib return sp_symbol_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_GROUP(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("group\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return sp_group_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_SHAPE(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("shape\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return sp_shape_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_USE(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("use begin---\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh sp_use_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("---use end\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_TEXT(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("text\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return sp_text_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_FLOWTEXT(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("flowtext\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return sp_flowtext_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else if (SP_IS_IMAGE(item)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("image\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return sp_image_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshvoid
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPStyle const *style = item->style;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->setStateForStyle(style);
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderState *state = ctx->getCurrentState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh state->clip_path = item->clip_ref->getObject();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh state->mask = item->mask_ref->getObject();
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen state->item_transform = Geom::Affine (item->transform);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // If parent_has_userspace is true the parent state's transform
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // has to be used for the mask's/clippath's context.
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // This is so because we use the image's/(flow)text's transform for positioning
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // instead of explicitly specifying it and letting the renderer do the
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // transformation before rendering the item.
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh state->parent_has_userspace = TRUE;
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong TRACE(("setStateForItem opacity: %f\n", state->opacity));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz// TODO change this to accept a const SPItem:
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruzvoid CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen // skip text if _omitText is true
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen return;
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen }
e66a49aa774c6770e99591a19873105bfeb5ce74Johan Engelen
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh setStateForItem(ctx, item);
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderState *state = ctx->getCurrentState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (state->need_layer) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh state->merge_opacity = FALSE;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushLayer();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(item->transform);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh sp_item_invoke_render(item, ctx);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (state->need_layer)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popLayer();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshbool
8c32a3bc6f42bceafbd1fdb825194b990d97fae0Johan B. C. EngelenCairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, float bleedmargin_px, SPItem *base)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
6825a6097da27fdfdf22859c0f81c208e7e22bb4Johan Engelen// PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
6825a6097da27fdfdf22859c0f81c208e7e22bb4Johan Engelen
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh g_assert( ctx != NULL );
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz if (!base) {
8a0a34615f080ecd1c09986d8963d6afe5937a25Jon A. Cruz base = doc->getRoot();
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::Rect d;
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould if (pageBoundingBox) {
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski d = Geom::Rect::from_xywh(Geom::Point(0,0), doc->getDimensions());
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh } else {
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::OptRect bbox = base->desktopVisualBounds();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski if (!bbox) {
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski g_message("CairoRenderer: empty bounding box.");
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski return false;
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski }
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski d = *bbox;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
8c32a3bc6f42bceafbd1fdb825194b990d97fae0Johan B. C. Engelen d.expandBy(bleedmargin_px);
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould if (ctx->_vector_based_target) {
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould // convert from px to pt
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff d *= Geom::Scale(Inkscape::Util::Quantity::convert(1, "px", "pt"));
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould }
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->_width = d.width();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->_height = d.height();
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
ddc1f1d1291eb21b244c7328d260c7d4a43be2fcbuliabyak
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen if (ret) {
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen if (pageBoundingBox) {
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen // translate to set bleed/margin
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen Geom::Affine tp( Geom::Translate( bleedmargin_px, bleedmargin_px ) );
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen ctx->transform(tp);
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen } else {
d16495c17bbc27ff37e18ea3eb61f6b07448152aMatthew Petroff double high = doc->getHeight().value("px");
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen if (ctx->_vector_based_target)
d16495c17bbc27ff37e18ea3eb61f6b07448152aMatthew Petroff high = Inkscape::Util::Quantity::convert(high, "px", "pt");
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen // this transform translates the export drawing to a virtual page (0,0)-(width,height)
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff Geom::Affine tp(Geom::Translate(-d.left() * (ctx->_vector_based_target ? Inkscape::Util::Quantity::convert(1, "pt", "px") : 1.0),
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff (d.bottom() - high) * (ctx->_vector_based_target ? Inkscape::Util::Quantity::convert(1, "pt", "px") : 1.0)));
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen ctx->transform(tp);
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen }
ddc1f1d1291eb21b244c7328d260c7d4a43be2fcbuliabyak }
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski
ddc1f1d1291eb21b244c7328d260c7d4a43be2fcbuliabyak return ret;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "macros.h" // SP_PRINT_*
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould// Apply an SVG clip path
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshvoid
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh g_assert( ctx != NULL && ctx->_is_valid );
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (cp == NULL)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return;
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski // FIXME: the access to the first clippath view to obtain the bbox is completely bogus
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine saved_ctm;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX && cp->display->bbox) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh //SP_PRINT_DRECT("clipd", cp->display->bbox);
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::Rect clip_bbox = *cp->display->bbox;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::Affine t(Geom::Scale(clip_bbox.dimensions()));
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski t[4] = clip_bbox.left();
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski t[5] = clip_bbox.top();
3955580a5a68a873b098921626f5b9d841b964ecjaspervdg t *= ctx->getCurrentState()->transform;
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski saved_ctm = ctx->getTransform();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->setTransform(t);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("BEGIN clip\n"));
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPObject const *co = cp;
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz for ( SPObject const *child = co->firstChild() ; child; child = child->getNext() ) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (SP_IS_ITEM(child)) {
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPItem const *item = SP_ITEM(child);
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak // combine transform of the item in clippath and the item using clippath:
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::Affine tempmat = item->transform * ctx->getCurrentState()->item_transform;
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak // render this item in clippath
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak ctx->pushState();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->transform(tempmat);
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak setStateForItem(ctx, item);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz // TODO fix this call to accept const items
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz sp_item_invoke_render(const_cast<SPItem *>(item), ctx);
bf7069a487ba218c4542e4adc5497ebd746ab929buliabyak ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("END clip\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // do clipping only if this was the first call to applyClipPath
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh cairo_clip(ctx->_cr);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->setTransform(saved_ctm);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->setRenderMode(saved_mode);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould// Apply an SVG mask
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshvoid
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh g_assert( ctx != NULL && ctx->_is_valid );
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (mask == NULL)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski // FIXME: the access to the first mask view to obtain the bbox is completely bogus
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX && mask->display->bbox) {
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski //SP_PRINT_DRECT("maskd", &mask->display->bbox);
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::Rect mask_bbox = *mask->display->bbox;
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski Geom::Affine t(Geom::Scale(mask_bbox.dimensions()));
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski t[4] = mask_bbox.left();
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski t[5] = mask_bbox.top();
3955580a5a68a873b098921626f5b9d841b964ecjaspervdg t *= ctx->getCurrentState()->transform;
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski ctx->setTransform(t);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
533909ea7bb06d8f0658b7d448fa4283afd3f52ftavmjong // Clip mask contents... but...
533909ea7bb06d8f0658b7d448fa4283afd3f52ftavmjong // The mask's bounding box is the "geometric bounding box" which doesn't allow for
533909ea7bb06d8f0658b7d448fa4283afd3f52ftavmjong // filters which extend outside the bounding box. So don't clip.
533909ea7bb06d8f0658b7d448fa4283afd3f52ftavmjong // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->pushState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("BEGIN mask\n"));
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPObject const *co = mask;
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz for ( SPObject const *child = co->firstChild() ; child; child = child->getNext() ) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (SP_IS_ITEM(child)) {
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPItem const *item = SP_ITEM(child);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz // TODO fix const correctness:
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz renderItem(ctx, const_cast<SPItem*>(item));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh TRACE(("END mask\n"));
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->popState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshvoid
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshcalculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double *x, double *y, double *width, double *height)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh{
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (aspect_align == SP_ASPECT_NONE)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh return;
4b1c2be41ce8c1a88502c1b1885ad1468646fbfftheadib
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double scalex, scaley, scale;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh double new_width, new_height;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh scalex = *width / vp_width;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh scaley = *height / vp_height;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_width = vp_width * scale;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_height = vp_height * scale;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* Now place viewbox to requested position */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh switch (aspect_align) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMIN_YMIN:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMID_YMIN:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 0.5 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMAX_YMIN:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 1.0 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMIN_YMID:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 0.5 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMID_YMID:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 0.5 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 0.5 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMAX_YMID:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 1.0 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 0.5 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMIN_YMAX:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 1.0 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMID_YMAX:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 0.5 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 1.0 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh case SP_ASPECT_XMAX_YMAX:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *x -= 1.0 * (new_width - *width);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *y -= 1.0 * (new_height - *height);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh default:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh break;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh }
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *width = new_width;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh *height = new_height;
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh}
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#include "clear-n_.h"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Internal */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Extension */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Inkscape */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh#undef TRACE
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/* End of GNU GPL code */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/*
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh Local Variables:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh mode:c++
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh c-file-style:"stroustrup"
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh indent-tabs-mode:nil
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh fill-column:99
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh End:
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh*/
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :