b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Rendering with Cairo.
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Miklos Erdelyi <erdelyim@gmail.com>
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz * Jon A. Cruz <jon@joncruz.org>
9dc68827cbd515262ecb8d5ae8547d9e82c72e00Jon A. Cruz * Abhishek Sharma
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Copyright (C) 2006 Miklos Erdelyi
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh * Licensed under GNU GPL
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh// include support for only the compiled-in surface types
21e45fc4c9994c35cebdf8ea0d648f72a8b0da28tavmjong-free//#define TRACE(_args) g_printf _args
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould//#define TEST(_args) _args
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh// FIXME: expose these from sp-clippath/mask.cpp
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* restore default signal handling for SIGPIPE */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderContext *new_context = new CairoRenderContext(this);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // create initial render state
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderState *state = new_context->_createState();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::destroyContext(CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshHere comes the rendering part which could be put into the 'render' methods of SPItems'
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_group_render(SPGroup *group, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_use_render(SPUse *use, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_shape_render(SPShape *shape, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_text_render(SPText *text, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_flowtext_render(SPFlowtext *flowtext, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_image_render(SPImage *image, CairoRenderContext *ctx);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_symbol_render(SPSymbol *symbol, CairoRenderContext *ctx);
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadibstatic void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelenstatic void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Affine tr, SPStyle* style, CairoRenderContext *ctx)
05a66d0771cb563225b8690de04e9490b35453f2johanengelen if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = Geom::Scale(style->stroke_width.computed) * tr;
05a66d0771cb563225b8690de04e9490b35453f2johanengelen render = false; // stroke width zero and marker is thus scaled down to zero, skip
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz SPItem* marker_item = sp_item_first_item_child(marker);
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen tr = (Geom::Affine)marker_item->transform * (Geom::Affine)marker->c2p * tr;
4b883d02d308812fcf976f6292838c52a6230137Johan Engelen ctx->getRenderer()->renderItem (ctx, marker_item);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_shape_render(SPShape *shape, CairoRenderContext *ctx)
620d9f76e90641a5a91f0c45fcb9a2f07b3881dbJohan Engelen Geom::PathVector const & pathv = shape->_curve->get_pathvector();
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free if (style->paint_order.layer[0] == SP_CSS_PAINT_ORDER_NORMAL ||
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free (style->paint_order.layer[0] == SP_CSS_PAINT_ORDER_FILL &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_STROKE)) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::STROKE_OVER_FILL);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[0] == SP_CSS_PAINT_ORDER_STROKE &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_FILL ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::FILL_OVER_STROKE);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[0] == SP_CSS_PAINT_ORDER_STROKE &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_MARKER ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::STROKE_ONLY);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[0] == SP_CSS_PAINT_ORDER_FILL &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_MARKER ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::FILL_ONLY);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // START marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free if (marker->orient_mode == MARKER_ORIENT_AUTO) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free } else if (marker->orient_mode == MARKER_ORIENT_AUTO_START_REVERSE) {
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free tr = Geom::Rotate::from_degrees( 180.0 ) * sp_shape_marker_get_transform_at_start(pathv.begin()->front());
dd01876bd8c0378d9398a8b6f9b65c384e982e35tavmjong-free tr = Geom::Rotate::from_degrees(marker->orient.computed) * Geom::Translate(pathv.begin()->front().pointAt(0));
68b9e386db1a231abaddbfed4d05f7539bdac786scislac sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // MID marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // START position
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
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free if (marker->orient_mode != MARKER_ORIENT_ANGLE) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform_at_start(path_it->front());
dd01876bd8c0378d9398a8b6f9b65c384e982e35tavmjong-free tr = Geom::Rotate::from_degrees(marker->orient.computed) * Geom::Translate(path_it->front().pointAt(0));
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // MID position
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 /* 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 */
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free if (marker->orient_mode != MARKER_ORIENT_ANGLE) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
dd01876bd8c0378d9398a8b6f9b65c384e982e35tavmjong-free tr = Geom::Rotate::from_degrees(marker->orient.computed) * Geom::Translate(curve_it1->pointAt(1));
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // END position
68b9e386db1a231abaddbfed4d05f7539bdac786scislac if ( path_it != (pathv.end()-1) && !path_it->empty()) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac Geom::Curve const &lastcurve = path_it->back_default();
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free if (marker->orient_mode != MARKER_ORIENT_ANGLE) {
05a66d0771cb563225b8690de04e9490b35453f2johanengelen tr = sp_shape_marker_get_transform_at_end(lastcurve);
dd01876bd8c0378d9398a8b6f9b65c384e982e35tavmjong-free tr = Geom::Rotate::from_degrees(marker->orient.computed) * Geom::Translate(lastcurve.pointAt(1));
05a66d0771cb563225b8690de04e9490b35453f2johanengelen sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
68b9e386db1a231abaddbfed4d05f7539bdac786scislac // END marker
68b9e386db1a231abaddbfed4d05f7539bdac786scislac for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
68b9e386db1a231abaddbfed4d05f7539bdac786scislac /* Get reference to last curve in the path.
68b9e386db1a231abaddbfed4d05f7539bdac786scislac * For moveto-only path, this returns the "closing line segment". */
619d2e2137be2f2ce8b3f03925498aadc0ab1664tavmjong-free if (marker->orient_mode != MARKER_ORIENT_ANGLE) {
68b9e386db1a231abaddbfed4d05f7539bdac786scislac tr = sp_shape_marker_get_transform_at_end(lastcurve);
dd01876bd8c0378d9398a8b6f9b65c384e982e35tavmjong-free tr = Geom::Rotate::from_degrees(marker->orient.computed) * Geom::Translate(lastcurve.pointAt(1));
68b9e386db1a231abaddbfed4d05f7539bdac786scislac sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free if (style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_FILL &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[2] == SP_CSS_PAINT_ORDER_STROKE) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::STROKE_OVER_FILL);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_STROKE &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[2] == SP_CSS_PAINT_ORDER_FILL ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::FILL_OVER_STROKE);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[2] == SP_CSS_PAINT_ORDER_STROKE &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_MARKER ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::STROKE_ONLY);
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free } else if (style->paint_order.layer[2] == SP_CSS_PAINT_ORDER_FILL &&
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free style->paint_order.layer[1] == SP_CSS_PAINT_ORDER_MARKER ) {
cda3e65e6e936d98544a5afb15d30f0241c250f5tavmjong-free ctx->renderPathVector(pathv, style, pbox, CairoRenderContext::FILL_ONLY);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_group_render(SPGroup *group, CairoRenderContext *ctx)
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(item->style->opacity.value)));
1f6daa293cacc62d85a023412c70534a12f6e99aMarc Jeanmougin std::vector<SPObject*> l(group->childList(false));
b1b352ba8dc52b86acfbf68071a62dfb9a9792c3Kris De Gussem for(std::vector<SPObject*>::const_iterator x = l.begin(); x!= l.end(); ++x){
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_use_render(SPUse *use, CairoRenderContext *ctx)
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));
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_text_render(SPText *text, CairoRenderContext *ctx)
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_flowtext_render(SPFlowtext *flowtext, CairoRenderContext *ctx)
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_image_render(SPImage *image, CairoRenderContext *ctx)
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
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);
17d87f5698f5c2958d38c6a6207c7b322a7adaf9johanengelen Geom::Scale s(width / (double)w, height / (double)h);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz ctx->renderImage(image->pixbuf, t, image->style);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruzstatic void sp_symbol_render(SPSymbol *symbol, CairoRenderContext *ctx)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* Cloned <symbol> is actually renderable */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // apply viewbox if set
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh if (0 /*symbol->viewBox_set*/) {
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh // [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski vb2user[4] = x - symbol->viewBox.left() * vb2user[0];
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski vb2user[5] = y - symbol->viewBox.top() * vb2user[3];
07b7f1aaaf1087716e784a50cf574a059f7018d3Jon A. Cruzstatic void sp_root_render(SPRoot *root, CairoRenderContext *ctx)
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);
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.
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadibstatic void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // Calculate resolution
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould /** @TODO reimplement the resolution stuff (WHY?)
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff res = Inkscape::Util::Quantity::convert(1, "in", "px");
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski // Get the bounding box of the selection in desktop coordinates.
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::OptRect bbox = item->desktopVisualBounds();
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib // no bbox, e.g. empty group
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz Geom::Rect docrect(Geom::Rect(Geom::Point(0, 0), item->document->getDimensions()));
6c28c26c5dc911196633a0c332b56f712085fa3ftheAdib // no bbox, e.g. empty group
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // The width and height of the bitmap in pixels
35636e0c762d50c37515a072b12bdc0197afd24cMatthew Petroff unsigned width = ceil(bbox->width() * Inkscape::Util::Quantity::convert(res, "px", "in"));
35636e0c762d50c37515a072b12bdc0197afd24cMatthew Petroff unsigned height = ceil(bbox->height() * Inkscape::Util::Quantity::convert(res, "px", "in"));
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Scale to exactly fit integer bitmap inside bounding box
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski double scale_y = bbox->height() / height;
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Location of bounding box in document coordinates.
ccf9b93e83dfde6c33afe97636f5c3ce77ee5ca8apenner // For default 96 dpi, snap bitmap to pixel grid
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff if (res == Inkscape::Util::Quantity::convert(1, "in", "px")) {
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Remove the negations when the inversion is gone.
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
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 // ctx matrix already includes item transformation. We must substract.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Geom::Affine t = t_on_document * t_item.inverse();
5c45c5153b0415f7573f69f4ee3e946b5872a8d1theadib // Do the export
e4b626a9206e4fabf4fc7601dfa14b133c2a27f0Krzysztof Kosiński sp_generate_internal_bitmap(document, NULL,
e4b626a9206e4fabf4fc7601dfa14b133c2a27f0Krzysztof Kosiński bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
e4b626a9206e4fabf4fc7601dfa14b133c2a27f0Krzysztof Kosiński width, height, res, res, (guint32) 0xffffff00, items ));
e4b626a9206e4fabf4fc7601dfa14b133c2a27f0Krzysztof Kosiński //TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
e4b626a9206e4fabf4fc7601dfa14b133c2a27f0Krzysztof Kosiński ctx->renderImage(pb.get(), t, item->style);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshstatic void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
1babefb17e4c8157ca2a0f588625a8ac0258dc53buliabyak // Check item's visibility
861fa436e7a6d6ff3eaa889b2298e0b82a0b238ctheadib if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz SPSymbol *symbol = dynamic_cast<SPSymbol *>(item);
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz SPFlowtext *flowtext = dynamic_cast<SPFlowtext *>(item);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen state->item_transform = Geom::Affine (item->transform);
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.
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz if (dynamic_cast<SPText const *>(item) || dynamic_cast<SPFlowtext const *>(item) || dynamic_cast<SPImage const *>(item)) {
16dc3cff4d07c7f5afdd8c74cf34530b0a6ee93btavmjong TRACE(("setStateForItem opacity: %f\n", state->opacity));
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz// TODO change this to accept a const SPItem:
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruzvoid CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
207c61f1ba5cc919707ca53a36156204a916c5c3tavmjong-free state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
207c61f1ba5cc919707ca53a36156204a916c5c3tavmjong-free // Draw item on a temporary surface so a mask, clip-path, or opacity can be applied to it.
207c61f1ba5cc919707ca53a36156204a916c5c3tavmjong-free ctx->popLayer(); // This applies clipping/masking
b54fcbf691761f7356ee5239114b8a0adf292cc3Tomasz Boczkowskivoid CairoRenderer::renderHatchPath(CairoRenderContext *ctx, SPHatchPath const &hatchPath, unsigned key) {
b54fcbf691761f7356ee5239114b8a0adf292cc3Tomasz Boczkowski ctx->transform(Geom::Translate(hatchPath.offset.computed, 0));
b54fcbf691761f7356ee5239114b8a0adf292cc3Tomasz Boczkowski SPCurve *curve = hatchPath.calculateRenderCurve(key);
b54fcbf691761f7356ee5239114b8a0adf292cc3Tomasz Boczkowski Geom::PathVector const & pathv =curve->get_pathvector();
b54fcbf691761f7356ee5239114b8a0adf292cc3Tomasz Boczkowski ctx->renderPathVector(pathv, hatchPath.style, Geom::OptRect());
8c32a3bc6f42bceafbd1fdb825194b990d97fae0Johan B. C. EngelenCairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, float bleedmargin_px, SPItem *base)
6825a6097da27fdfdf22859c0f81c208e7e22bb4Johan Engelen// PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski d = Geom::Rect::from_xywh(Geom::Point(0,0), doc->getDimensions());
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski Geom::OptRect bbox = base->desktopVisualBounds();
36bb2154f1627a17c3591eb4d7f89335e8b5daddKrzysztof Kosinski g_message("CairoRenderer: empty bounding box.");
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould // convert from px to pt
c751b68e56ace7b9d649ee20b7f25ed1f65922e3Matthew Petroff d *= Geom::Scale(Inkscape::Util::Quantity::convert(1, "px", "pt"));
21e45fc4c9994c35cebdf8ea0d648f72a8b0da28tavmjong-free TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
e9b6af083e34e2397a8ddbe9781920733d09d151Ted Gould bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen // translate to set bleed/margin
adeb42ef9b116c0ac1f3d14ef19de10f9dfad026Johan B. C. Engelen Geom::Affine tp( Geom::Translate( bleedmargin_px, bleedmargin_px ) );
d16495c17bbc27ff37e18ea3eb61f6b07448152aMatthew Petroff high = Inkscape::Util::Quantity::convert(high, "px", "pt");
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)));
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould// Apply an SVG clip path
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
20210df3c28b5455f6eccc8841f9f7dd9a6b1d27Krzysztof Kosinski // FIXME: the access to the first clippath view to obtain the bbox is completely bogus
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()));
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz for ( SPObject const *child = co->firstChild() ; child; child = child->getNext() ) {
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz SPItem const *item = dynamic_cast<SPItem const *>(child);
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 // render this item in clippath
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz // TODO fix this call to accept const items
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz sp_item_invoke_render(const_cast<SPItem *>(item), ctx);
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 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
63d6ddd517060979dd8b8fd41aad3faca7be3c5cTed Gould// Apply an SVG mask
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6mikloshCairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
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()));
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);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz for ( SPObject const *child = co->firstChild() ; child; child = child->getNext() ) {
f4d474ff2d58b3d32dacd5feed0c164e8df4936cJon A. Cruz SPItem const *item = dynamic_cast<SPItem const *>(child);
ca4d1d8972d64c1d1bfdd4b09d31f7b372074940Jon A. Cruz // TODO fix const correctness:
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 scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh /* Now place viewbox to requested position */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Internal */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Extension */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh} /* namespace Inkscape */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh/* End of GNU GPL code */
b7f07692f95074fdcf74c7350fbf5f3099ffc1b6miklosh Local Variables:
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
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :