index dbb89818317fe443a895841a1f96c6715f0bdcd1..8cc38613538c1371b212c423750722649c230340 100644 (file)
#include <signal.h>
#include <errno.h>
-#include <libnr/n-art-bpath.h>
-#include <libnr/nr-matrix-ops.h>
-#include <libnr/nr-matrix-fns.h>
-#include <libnr/nr-matrix-translate-ops.h>
-#include <libnr/nr-scale-matrix-ops.h>
+#include "libnr/nr-rect.h"
+#include <2geom/transforms.h>
+#include <2geom/pathvector.h>
#include <glib/gmem.h>
#include "sp-clippath.h"
#include <unit-constants.h>
+#include "helper/png-write.h"
+#include "helper/pixbuf-ops.h"
#include "cairo-renderer.h"
#include "cairo-render-context.h"
//#define TRACE(_args) g_printf _args
#define TRACE(_args)
+//#define TEST(_args) _args
+#define TEST(_args)
// FIXME: expose these from sp-clippath/mask.cpp
struct SPClipPathView {
// create initial render state
CairoRenderState *state = new_context->_createState();
- nr_matrix_set_identity(&state->transform);
+ state->transform = Geom::identity();
new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
new_context->_state = state;
static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
+static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
+
+static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
+{
+ bool render = true;
+ if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
+ if (style->stroke_width.computed > 1e-9) {
+ tr = Geom::Scale(style->stroke_width.computed) * tr;
+ } else {
+ render = false; // stroke width zero and marker is thus scaled down to zero, skip
+ }
+ }
+
+ if (render) {
+ SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
+ tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
+ Geom::Matrix old_tr = marker_item->transform;
+ marker_item->transform = tr;
+ ctx->getRenderer()->renderItem (ctx, marker_item);
+ marker_item->transform = old_tr;
+ }
+}
static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
{
if (!shape->curve) return;
- /* fixme: Think (Lauris) */
- sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
- NR::Matrix const i2d = sp_item_i2d_affine(item);
+ sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
SPStyle* style = SP_OBJECT_STYLE (item);
- CairoRenderer *renderer = ctx->getRenderer();
- NRBPath bp;
- bp.path = SP_CURVE_BPATH(shape->curve);
+ Geom::PathVector const & pathv = shape->curve->get_pathvector();
- ctx->renderPath(&bp, style, &pbox);
+ ctx->renderPathVector(pathv, style, &pbox);
- for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
- for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
- if (sp_shape_marker_required (shape, m, bp)) {
-
- SPMarker* marker = SP_MARKER (shape->marker[m]);
- SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
-
- NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
-
- if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
- tr = NR::scale(style->stroke_width.computed) * tr;
+ // START marker
+ for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
+ if ( shape->marker[i] ) {
+ SPMarker* marker = SP_MARKER (shape->marker[i]);
+ Geom::Matrix tr;
+ if (marker->orient_auto) {
+ tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
+ } else {
+ tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
+ }
+ sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
+ }
+ }
+ // MID marker
+ for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
+ if ( !shape->marker[i] ) continue;
+ SPMarker* marker = SP_MARKER (shape->marker[i]);
+ for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
+ // START position
+ if ( path_it != pathv.begin()
+ && ! ((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
+ {
+ Geom::Matrix tr;
+ if (marker->orient_auto) {
+ tr = sp_shape_marker_get_transform_at_start(path_it->front());
+ } else {
+ tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
}
+ sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
+ }
+ // MID position
+ if (path_it->size_default() > 1) {
+ Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
+ Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
+ while (curve_it2 != path_it->end_default())
+ {
+ /* Put marker between curve_it1 and curve_it2.
+ * Loop to end_default (so including closing segment), because when a path is closed,
+ * there should be a midpoint marker between last segment and closing straight line segment */
+ Geom::Matrix tr;
+ if (marker->orient_auto) {
+ tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
+ } else {
+ tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
+ }
+
+ sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
+
+ ++curve_it1;
+ ++curve_it2;
+ }
+ }
+ // END position
+ if ( path_it != (pathv.end()-1) && !path_it->empty()) {
+ Geom::Curve const &lastcurve = path_it->back_default();
+ Geom::Matrix tr;
+ if (marker->orient_auto) {
+ tr = sp_shape_marker_get_transform_at_end(lastcurve);
+ } else {
+ tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
+ }
+ sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
+ }
+ }
+ }
+ // END marker
+ for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
+ if ( shape->marker[i] ) {
+ SPMarker* marker = SP_MARKER (shape->marker[i]);
+
+ /* Get reference to last curve in the path.
+ * For moveto-only path, this returns the "closing line segment". */
+ Geom::Path const &path_last = pathv.back();
+ unsigned int index = path_last.size_default();
+ if (index > 0) {
+ index--;
+ }
+ Geom::Curve const &lastcurve = path_last[index];
- tr = marker_item->transform * marker->c2p * tr;
-
- NR::Matrix old_tr = marker_item->transform;
- marker_item->transform = tr;
- renderer->renderItem (ctx, marker_item);
- marker_item->transform = old_tr;
+ Geom::Matrix tr;
+ if (marker->orient_auto) {
+ tr = sp_shape_marker_get_transform_at_end(lastcurve);
+ } else {
+ tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
}
+
+ sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
}
}
}
{
SPGroup *group = SP_GROUP(item);
CairoRenderer *renderer = ctx->getRenderer();
- TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
+ TRACE(("sp_group_render opacity: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
GSList *l = g_slist_reverse(group->childList(false));
while (l) {
static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
{
bool translated = false;
- NRMatrix tp;
SPUse *use = SP_USE(item);
CairoRenderer *renderer = ctx->getRenderer();
if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
- nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
+ Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
ctx->pushState();
ctx->transform(&tp);
translated = true;
static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
{
SPImage *image;
- NRMatrix tp, s, t;
guchar *px;
int w, h, rs;
ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
}
- nr_matrix_set_translate (&tp, x, y);
- nr_matrix_set_scale (&s, width / (double)w, height / (double)h);
- nr_matrix_multiply (&t, &s, &tp);
+ Geom::Translate tp(x, y);
+ Geom::Scale s(width / (double)w, height / (double)h);
+ Geom::Matrix t(s * tp);
ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
}
// apply viewbox if set
if (0 /*symbol->viewBox_set*/) {
- NRMatrix vb2user;
+ Geom::Matrix vb2user;
double x, y, width, height;
double view_width, view_height;
x = 0.0;
&x, &y,&width, &height);
// [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
- nr_matrix_set_identity(&vb2user);
+ vb2user = Geom::identity();
vb2user[0] = width / view_width;
vb2user[3] = height / view_height;
vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
ctx->pushState();
renderer->setStateForItem(ctx, item);
- ctx->transform(root->c2p);
+ Geom::Matrix tempmat (root->c2p);
+ ctx->transform(&tempmat);
sp_group_render(item, ctx);
ctx->popState();
}
-static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
+/**
+ This function converts the item to a raster image and includes the image into the cairo renderer.
+ It is only used for filters and then only when rendering filters as bitmaps is requested.
+*/
+static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
{
- /*
- if(ctx->_state->has_filtereffect)
- printf("\nhas filtereffects");
+
+ // The code was adapted from sp_selection_create_bitmap_copy in selection-chemistry.cpp
+
+ // Calculate resolution
+ double res;
+ /** @TODO reimplement the resolution stuff (WHY?)
*/
+ res = ctx->getBitmapResolution();
+ if(res == 0) {
+ res = PX_PER_IN;
+ }
+ TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
+
+ // Get the bounding box of the selection in document coordinates.
+ Geom::OptRect bbox =
+ item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
+
+ if (!bbox) // no bbox, e.g. empty group
+ return;
+
+ // The width and height of the bitmap in pixels
+ unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
+ unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
+
+ // Scale to exactly fit integer bitmap inside bounding box
+ double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
+ double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
+
+ // Location of bounding box in document coordinates.
+ double shift_x = bbox->min()[Geom::X];
+ double shift_y = bbox->max()[Geom::Y];
+
+ // For default 90 dpi, snap bitmap to pixel grid
+ if (res == PX_PER_IN) {
+ shift_x = round (shift_x);
+ shift_y = -round (-shift_y); // Correct rounding despite coordinate inversion.
+ // Remove the negations when the inversion is gone.
+ }
+
+ // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
+
+ // Matix to put bitmap in correct place on document
+ Geom::Matrix t_on_document = (Geom::Matrix)(Geom::Scale (scale_x, -scale_y)) *
+ (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
+
+ // ctx matrix already includes item transformation. We must substract.
+ Geom::Matrix t_item = sp_item_i2d_affine (item);
+ Geom::Matrix t = t_on_document * t_item.inverse();
+
+ // Do the export
+ SPDocument *document = SP_OBJECT(item)->document;
+ GSList *items = NULL;
+ items = g_slist_append(items, item);
+
+ GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
+ bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y],
+ width, height, res, res, (guint32) 0xffffff00, items );
+
+ if (pb) {
+ TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
+ unsigned char *px = gdk_pixbuf_get_pixels (pb);
+ unsigned int w = gdk_pixbuf_get_width(pb);
+ unsigned int h = gdk_pixbuf_get_height(pb);
+ unsigned int rs = gdk_pixbuf_get_rowstride(pb);
+ ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
+ gdk_pixbuf_unref (pb);
+ }
+ g_slist_free (items);
+}
+
+
+static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
+{
+ // Check item's visibility
+ if (item->isHidden()) {
+ return;
+ }
+
+ SPStyle* style = SP_OBJECT_STYLE (item);
+ if((ctx->getFilterToBitmap() == TRUE) && (style->filter.set != 0)) {
+ return sp_asbitmap_render(item, ctx);
+ }
if (SP_IS_ROOT(item)) {
TRACE(("root\n"));
CairoRenderState *state = ctx->getCurrentState();
state->clip_path = item->clip_ref->getObject();
state->mask = item->mask_ref->getObject();
+ state->item_transform = Geom::Matrix (item->transform);
// If parent_has_userspace is true the parent state's transform
// has to be used for the mask's/clippath's context.
// transformation before rendering the item.
if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
state->parent_has_userspace = TRUE;
- TRACE(("set op: %f\n", state->opacity));
+ TRACE(("setStateForItem opacity: %f\n", state->opacity));
}
void
CairoRenderState *state = ctx->getCurrentState();
state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
+ // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
if (state->need_layer) {
state->merge_opacity = FALSE;
ctx->pushLayer();
}
- ctx->transform(item->transform);
+ Geom::Matrix tempmat (item->transform);
+ ctx->transform(&tempmat);
sp_item_invoke_render(item, ctx);
if (state->need_layer)
}
bool
-CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
+CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
{
g_assert( ctx != NULL );
- if (ctx->_vector_based_target) {
- // width and height in pt
- ctx->_width = sp_document_width(doc) * PT_PER_PX;
- ctx->_height = sp_document_height(doc) * PT_PER_PX;
- } else {
- ctx->_width = sp_document_width(doc);
- ctx->_height = sp_document_height(doc);
- }
+ if (!base)
+ base = SP_ITEM(sp_document_root(doc));
NRRect d;
- bool pageBoundingBox = true;
if (pageBoundingBox) {
d.x0 = d.y0 = 0;
- d.x1 = ceil(ctx->_width);
- d.y1 = ceil(ctx->_height);
+ d.x1 = ceil(sp_document_width(doc));
+ d.y1 = ceil(sp_document_height(doc));
} else {
- SPItem* doc_item = SP_ITEM(sp_document_root(doc));
- sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
- if (ctx->_vector_based_target) {
- // convert from px to pt
- d.x0 *= PT_PER_PX;
- d.x1 *= PT_PER_PX;
- d.y0 *= PT_PER_PX;
- d.y1 *= PT_PER_PX;
- }
+ sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
+ }
+
+ if (ctx->_vector_based_target) {
+ // convert from px to pt
+ d.x0 *= PT_PER_PX;
+ d.x1 *= PT_PER_PX;
+ d.y0 *= PT_PER_PX;
+ d.y1 *= PT_PER_PX;
}
- TRACE(("%f x %f\n", ctx->_width, ctx->_height));
- return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
+ ctx->_width = d.x1-d.x0;
+ ctx->_height = d.y1-d.y0;
+
+ TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
+
+ bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
+
+ if (ret && !pageBoundingBox)
+ {
+ double high = sp_document_height(doc);
+ if (ctx->_vector_based_target)
+ high *= PT_PER_PX;
+
+ Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
+ (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
+ ctx->transform(&tp);
+ }
+
+ return ret;
}
#include "macros.h" // SP_PRINT_*
+// Apply an SVG clip path
void
CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
{
CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
- NRMatrix saved_ctm;
+ Geom::Matrix saved_ctm;
if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
- NRMatrix t;
//SP_PRINT_DRECT("clipd", cp->display->bbox);
NRRect clip_bbox(cp->display->bbox);
- nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
- t.c[4] = clip_bbox.x0;
- t.c[5] = clip_bbox.y0;
- nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
+ Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
+ t[4] = clip_bbox.x0;
+ t[5] = clip_bbox.y0;
+ t *= ctx->getCurrentState()->transform;
ctx->getTransform(&saved_ctm);
ctx->setTransform(&t);
}
for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
SPItem *item = SP_ITEM(child);
- renderItem(ctx, item);
+
+ // combine transform of the item in clippath and the item using clippath:
+ Geom::Matrix tempmat (item->transform);
+ tempmat = tempmat * (ctx->getCurrentState()->item_transform);
+
+ // render this item in clippath
+ ctx->pushState();
+ ctx->transform(&tempmat);
+ setStateForItem(ctx, item);
+ sp_item_invoke_render(item, ctx);
+ ctx->popState();
}
}
TRACE(("END clip\n"));
ctx->setRenderMode(saved_mode);
}
+// Apply an SVG mask
void
CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
{
NRRect mask_bbox(mask->display->bbox);
// TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
- NRMatrix t;
- nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
- t.c[4] = mask_bbox.x0;
- t.c[5] = mask_bbox.y0;
- nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
+ Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
+ t[4] = mask_bbox.x0;
+ t[5] = mask_bbox.y0;
+ t *= ctx->getCurrentState()->transform;
ctx->setTransform(&t);
}
- // clip mask contents
- ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
+ // Clip mask contents... but...
+ // The mask's bounding box is the "geometric bounding box" which doesn't allow for
+ // filters which extend outside the bounding box. So don't clip.
+ // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
ctx->pushState();