svg-builder.cpp revision 03e63790ef0fa2919fc5f9f3e0d018adf317919d
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /** \file
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Native PDF import using libpoppler.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Authors:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * miklos erdelyi
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Copyright (C) 2007 Authors
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Released under GNU GPL, read the file 'COPYING' for more information
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#ifdef HAVE_CONFIG_H
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh# include <config.h>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#endif
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#ifdef HAVE_POPPLER
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg-builder.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "pdf-parser.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include <png.h>
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "document-private.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "xml/document.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "xml/node.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "xml/repr.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg/svg.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg/path-string.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg/css-ostringstream.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg/svg-color.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "color.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "unit-constants.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "io/stringstream.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "io/base64stream.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "libnr/nr-matrix-ops.h"
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh#include "libnr/nr-macros.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "libnrtype/font-instance.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "Function.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "GfxState.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "GfxFont.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "Stream.h"
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh#include "Page.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "UnicodeMap.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "GlobalParams.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshnamespace Inkscape {
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshnamespace Extension {
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshnamespace Internal {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh//#define IFTRACE(_code) _code
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#define IFTRACE(_code)
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#define TRACE(_args) IFTRACE(g_print _args)
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh/**
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \struct SvgTransparencyGroup
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \brief Holds information about a PDF transparency group
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshstruct SvgTransparencyGroup {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh double bbox[6];
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh Inkscape::XML::Node *container;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh bool isolated;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh bool knockout;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh bool for_softmask;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgTransparencyGroup *next;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh};
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \class SvgBuilder
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
68664e00e2372534b4df2fdc5f54f836bafece18mikloshSvgBuilder::SvgBuilder(SPDocument *document, gchar *docname, XRef *xref) {
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh _is_top_level = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _doc = document;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh _docname = docname;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh _xref = xref;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _xml_doc = sp_document_repr_doc(_doc);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container = _root = _doc->rroot;
a4d12a5147f3d1d6b568a326e39ef5dca384248dmiklosh _root->setAttribute("xml:space", "preserve");
1667116521643e2475184b048e0abb77a2aa9735miklosh _init();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshSvgBuilder::SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root) {
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh _is_top_level = false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _doc = parent->_doc;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh _docname = parent->_docname;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh _xref = parent->_xref;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _xml_doc = parent->_xml_doc;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container = this->_root = root;
1667116521643e2475184b048e0abb77a2aa9735miklosh _init();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshSvgBuilder::~SvgBuilder() {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
1667116521643e2475184b048e0abb77a2aa9735mikloshvoid SvgBuilder::_init() {
1667116521643e2475184b048e0abb77a2aa9735miklosh _in_text_object = false;
1667116521643e2475184b048e0abb77a2aa9735miklosh _need_font_update = true;
1667116521643e2475184b048e0abb77a2aa9735miklosh _invalidated_style = true;
1667116521643e2475184b048e0abb77a2aa9735miklosh _font_style = NULL;
1667116521643e2475184b048e0abb77a2aa9735miklosh _current_font = NULL;
1667116521643e2475184b048e0abb77a2aa9735miklosh _current_state = NULL;
1667116521643e2475184b048e0abb77a2aa9735miklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _transp_group_stack = NULL;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgGraphicsState initial_state;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh initial_state.softmask = NULL;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh initial_state.group_depth = 0;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.push_back(initial_state);
1667116521643e2475184b048e0abb77a2aa9735miklosh _node_stack.push_back(_container);
1667116521643e2475184b048e0abb77a2aa9735miklosh}
1667116521643e2475184b048e0abb77a2aa9735miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::setDocumentSize(double width, double height) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_set_svg_double(_root, "width", width);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_set_svg_double(_root, "height", height);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->_width = width;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->_height = height;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
68664e00e2372534b4df2fdc5f54f836bafece18miklosh/**
68664e00e2372534b4df2fdc5f54f836bafece18miklosh * \brief Sets groupmode of the current container to 'layer' and sets its label if given
68664e00e2372534b4df2fdc5f54f836bafece18miklosh */
68664e00e2372534b4df2fdc5f54f836bafece18mikloshvoid SvgBuilder::setAsLayer(char *layer_name) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh _container->setAttribute("inkscape:groupmode", "layer");
68664e00e2372534b4df2fdc5f54f836bafece18miklosh if (layer_name) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh _container->setAttribute("inkscape:label", layer_name);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
68664e00e2372534b4df2fdc5f54f836bafece18miklosh}
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh/**
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \brief Sets the current container's opacity
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::setGroupOpacity(double opacity) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh sp_repr_set_svg_double(_container, "opacity", CLAMP(opacity, 0.0, 1.0));
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::saveState() {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgGraphicsState new_state;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh new_state.group_depth = 0;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh new_state.softmask = _state_stack.back().softmask;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.push_back(new_state);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pushGroup();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::restoreState() {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh while( _state_stack.back().group_depth > 0 ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh popGroup();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.pop_back();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
1667116521643e2475184b048e0abb77a2aa9735mikloshInkscape::XML::Node *SvgBuilder::pushNode(const char *name) {
1667116521643e2475184b048e0abb77a2aa9735miklosh Inkscape::XML::Node *node = _xml_doc->createElement(name);
1667116521643e2475184b048e0abb77a2aa9735miklosh _node_stack.push_back(node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container = node;
1667116521643e2475184b048e0abb77a2aa9735miklosh return node;
1667116521643e2475184b048e0abb77a2aa9735miklosh}
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735mikloshInkscape::XML::Node *SvgBuilder::popNode() {
1667116521643e2475184b048e0abb77a2aa9735miklosh Inkscape::XML::Node *node = NULL;
1667116521643e2475184b048e0abb77a2aa9735miklosh if ( _node_stack.size() > 1 ) {
1667116521643e2475184b048e0abb77a2aa9735miklosh node = _node_stack.back();
1667116521643e2475184b048e0abb77a2aa9735miklosh _node_stack.pop_back();
1667116521643e2475184b048e0abb77a2aa9735miklosh _container = _node_stack.back(); // Re-set container
1667116521643e2475184b048e0abb77a2aa9735miklosh } else {
1667116521643e2475184b048e0abb77a2aa9735miklosh TRACE(("popNode() called when stack is empty\n"));
1667116521643e2475184b048e0abb77a2aa9735miklosh node = _root;
1667116521643e2475184b048e0abb77a2aa9735miklosh }
1667116521643e2475184b048e0abb77a2aa9735miklosh return node;
1667116521643e2475184b048e0abb77a2aa9735miklosh}
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735mikloshInkscape::XML::Node *SvgBuilder::pushGroup() {
1667116521643e2475184b048e0abb77a2aa9735miklosh Inkscape::XML::Node *saved_container = _container;
1667116521643e2475184b048e0abb77a2aa9735miklosh Inkscape::XML::Node *node = pushNode("svg:g");
1667116521643e2475184b048e0abb77a2aa9735miklosh saved_container->appendChild(node);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(node);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.back().group_depth++;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh // Set as a layer if this is a top-level group
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh if ( _container->parent() == _root && _is_top_level ) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh static int layer_count = 1;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh if ( layer_count > 1 ) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh gchar *layer_name = g_strdup_printf("%s%d", _docname, layer_count);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh setAsLayer(layer_name);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh g_free(layer_name);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh } else {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh setAsLayer(_docname);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return _container;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshInkscape::XML::Node *SvgBuilder::popGroup() {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (_container != _root) { // Pop if the current container isn't root
1667116521643e2475184b048e0abb77a2aa9735miklosh popNode();
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.back().group_depth--;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return _container;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshInkscape::XML::Node *SvgBuilder::getContainer() {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return _container;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic gchar *svgConvertRGBToText(double r, double g, double b) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh static gchar tmp[1023] = {0};
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh snprintf(tmp, 1023,
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh "#%02x%02x%02x",
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh CLAMP(SP_COLOR_F_TO_U(r), 0, 255),
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh CLAMP(SP_COLOR_F_TO_U(g), 0, 255),
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh CLAMP(SP_COLOR_F_TO_U(b), 0, 255));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return (gchar *)&tmp;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic gchar *svgConvertGfxRGB(GfxRGB *color) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh double r = (double)color->r / 65535.0;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh double g = (double)color->g / 65535.0;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh double b = (double)color->b / 65535.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return svgConvertRGBToText(r, g, b);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshstatic void svgSetTransform(Inkscape::XML::Node *node, double c0, double c1,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh double c2, double c3, double c4, double c5) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh NR::Matrix matrix(c0, c1, c2, c3, c4, c5);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar *transform_text = sp_svg_transform_write(matrix);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh node->setAttribute("transform", transform_text);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh g_free(transform_text);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshstatic void svgSetTransform(Inkscape::XML::Node *node, double *transform) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh svgSetTransform(node, transform[0], transform[1], transform[2], transform[3],
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh transform[4], transform[5]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Generates a SVG path string from poppler's data structure
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic gchar *svgInterpretPath(GfxPath *path) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxSubpath *subpath;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::SVG::PathString pathString;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int i, j;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( i = 0 ; i < path->getNumSubpaths() ; ++i ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh subpath = path->getSubpath(i);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (subpath->getNumPoints() > 0) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pathString.moveTo(subpath->getX(0), subpath->getY(0));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh j = 1;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh while (j < subpath->getNumPoints()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (subpath->getCurve(j)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pathString.curveTo(subpath->getX(j), subpath->getY(j),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh subpath->getX(j+1), subpath->getY(j+1),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh subpath->getX(j+2), subpath->getY(j+2));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh j += 3;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pathString.lineTo(subpath->getX(j), subpath->getY(j));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ++j;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (subpath->isClosed()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pathString.closePath();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return g_strdup(pathString.c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Sets stroke style from poppler's GfxState data structure
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Uses the given SPCSSAttr for storing the style properties
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::_setStrokeStyle(SPCSSAttr *css, GfxState *state) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Check line width
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( state->getLineWidth() <= 0.0 ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Ignore stroke
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(css, "stroke", "none");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Stroke color/pattern
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( state->getStrokeColorSpace()->getMode() == csPattern ) {
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh gchar *urltext = _createPattern(state->getStrokePattern(), state, true);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke", urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (urltext) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxRGB stroke_color;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh state->getStrokeRGB(&stroke_color);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke", svgConvertGfxRGB(&stroke_color));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Opacity
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_opacity;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_opacity << state->getStrokeOpacity();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-opacity", os_opacity.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Line width
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_width;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_width << state->getLineWidth();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Line cap
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh switch (state->getLineCap()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 0:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linecap", "butt");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 1:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linecap", "round");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 2:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linecap", "square");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Line join
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh switch (state->getLineJoin()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 0:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linejoin", "miter");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 1:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linejoin", "round");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case 2:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-linejoin", "bevel");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Miterlimit
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_ml;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_ml << state->getMiterLimit();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Line dash
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double *dash_pattern;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int dash_length;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double dash_start;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh state->getLineDash(&dash_pattern, &dash_length, &dash_start);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( dash_length > 0 ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_array;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0 ; i < dash_length ; i++ ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_array << dash_pattern[i];
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (i < (dash_length - 1)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_array << ",";
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-dasharray", os_array.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_offset;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_offset << dash_start;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-dashoffset", os_offset.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-dasharray", "none");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Sets fill style from poppler's GfxState data structure
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Uses the given SPCSSAttr for storing the style properties.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::_setFillStyle(SPCSSAttr *css, GfxState *state, bool even_odd) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Fill color/pattern
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( state->getFillColorSpace()->getMode() == csPattern ) {
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh gchar *urltext = _createPattern(state->getFillPattern(), state);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "fill", urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (urltext) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxRGB fill_color;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh state->getFillRGB(&fill_color);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "fill", svgConvertGfxRGB(&fill_color));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Opacity
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::CSSOStringStream os_opacity;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh os_opacity << state->getFillOpacity();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "fill-opacity", os_opacity.str().c_str());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Fill rule
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "fill-rule", even_odd ? "evenodd" : "nonzero");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Sets style properties from poppler's GfxState data structure
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \return SPCSSAttr with all the relevant properties set
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshSPCSSAttr *SvgBuilder::_setStyle(GfxState *state, bool fill, bool stroke, bool even_odd) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPCSSAttr *css = sp_repr_css_attr_new();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (fill) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _setFillStyle(css, state, even_odd);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "fill", "none");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (stroke) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _setStrokeStyle(css, state);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_set_property(css, "stroke", "none");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return css;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Emits the current path in poppler's GfxState data structure
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Can be used to do filling and stroking at once.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \param fill whether the path should be filled
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \param stroke whether the path should be stroked
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \param even_odd whether the even-odd rule should be used when filling the path
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::addPath(GfxState *state, bool fill, bool stroke, bool even_odd) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *path = _xml_doc->createElement("svg:path");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *pathtext = svgInterpretPath(state->getPath());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh path->setAttribute("d", pathtext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(pathtext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Set style
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh SPCSSAttr *css = _setStyle(state, fill, stroke, even_odd);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_change(path, css, "style");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_css_attr_unref(css);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container->appendChild(path);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(path);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh/**
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * \brief Emits the current path in poppler's GfxState data structure
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * The path is set to be filled with the given shading.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh */
3686c32a570c3df738a09b34e85fc5d6bd50d020mikloshvoid SvgBuilder::addShadedFill(GfxShading *shading, double *matrix, GfxPath *path,
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh bool even_odd) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh Inkscape::XML::Node *path_node = _xml_doc->createElement("svg:path");
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh gchar *pathtext = svgInterpretPath(path);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh path_node->setAttribute("d", pathtext);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh g_free(pathtext);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh // Set style
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh SPCSSAttr *css = sp_repr_css_attr_new();
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh gchar *id = _createGradient(shading, matrix, true);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (id) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh gchar *urltext = g_strdup_printf ("url(#%s)", id);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_set_property(css, "fill", urltext);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh g_free(urltext);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh g_free(id);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh } else {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_attr_unref(css);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh Inkscape::GC::release(path_node);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh return;
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (even_odd) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_set_property(css, "fill-rule", "evenodd");
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_set_property(css, "stroke", "none");
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_change(path_node, css, "style");
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_repr_css_attr_unref(css);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh _container->appendChild(path_node);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh Inkscape::GC::release(path_node);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh}
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Clips to the current path set in GfxState
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \param state poppler's data structure
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \param even_odd whether the even-odd rule should be applied
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::clip(GfxState *state, bool even_odd) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh pushGroup();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh setClipPath(state, even_odd);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::setClipPath(GfxState *state, bool even_odd) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Create the clipPath repr
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *clip_path = _xml_doc->createElement("svg:clipPath");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh clip_path->setAttribute("clipPathUnits", "userSpaceOnUse");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Create the path
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *path = _xml_doc->createElement("svg:path");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *pathtext = svgInterpretPath(state->getPath());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh path->setAttribute("d", pathtext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(pathtext);
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh if (even_odd) {
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh path->setAttribute("clip-rule", "evenodd");
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh clip_path->appendChild(path);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(path);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Append clipPath to defs and get id
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh SP_OBJECT_REPR (SP_DOCUMENT_DEFS (_doc))->appendChild(clip_path);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *urltext = g_strdup_printf ("url(#%s)", clip_path->attribute("id"));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(clip_path);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container->setAttribute("clip-path", urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(urltext);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Fills the given array with the current container's transform, if set
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \param transform array of doubles to be filled
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \return true on success; false on invalid transformation
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshbool SvgBuilder::getTransform(double *transform) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh NR::Matrix svd;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar const *tr = _container->attribute("transform");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh bool valid = sp_svg_transform_read(tr, &svd);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (valid) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0 ; i < 6 ; i++ ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh transform[i] = svd[i];
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return true;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return false;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Sets the transformation matrix of the current container
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::setTransform(double c0, double c1, double c2, double c3,
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double c4, double c5) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh TRACE(("setTransform: %f %f %f %f %f %f\n", c0, c1, c2, c3, c4, c5));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh svgSetTransform(_container, c0, c1, c2, c3, c4, c5);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SvgBuilder::setTransform(double *transform) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh setTransform(transform[0], transform[1], transform[2], transform[3],
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh transform[4], transform[5]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Checks whether the given pattern type can be represented in SVG
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Used by PdfParser to decide when to do fallback operations.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshbool SvgBuilder::isPatternTypeSupported(GfxPattern *pattern) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( pattern != NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( pattern->getType() == 2 ) { // shading pattern
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxShading *shading = ((GfxShadingPattern *)pattern)->getShading();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int shadingType = shading->getType();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( shadingType == 2 || // axial shading
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh shadingType == 3 ) { // radial shading
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return true;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if ( pattern->getType() == 1 ) { // tiling pattern
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return true;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return false;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Creates a pattern from poppler's data structure
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Handles linear and radial gradients. Creates a new PdfParser and uses it to
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * build a tiling pattern.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \return an url pointing to the created pattern
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7mikloshgchar *SvgBuilder::_createPattern(GfxPattern *pattern, GfxState *state, bool is_stroke) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *id = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( pattern != NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( pattern->getType() == 2 ) { // Shading pattern
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh GfxShadingPattern *shading_pattern = (GfxShadingPattern*)pattern;
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh id = _createGradient(shading_pattern->getShading(),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh shading_pattern->getMatrix());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if ( pattern->getType() == 1 ) { // Tiling pattern
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh id = _createTilingPattern((GfxTilingPattern*)pattern, state, is_stroke);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return NULL;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *urltext = g_strdup_printf ("url(#%s)", id);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(id);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return urltext;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh/**
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh * \brief Creates a tiling pattern from poppler's data structure
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh * Creates a sub-page PdfParser and uses it to parse the pattern's content stream.
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh * \return id of the created pattern
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh */
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7mikloshgchar *SvgBuilder::_createTilingPattern(GfxTilingPattern *tiling_pattern,
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh GfxState *state, bool is_stroke) {
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh Inkscape::XML::Node *pattern_node = _xml_doc->createElement("svg:pattern");
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Set pattern transform matrix
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh double *p2u = tiling_pattern->getMatrix();
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh NR::Matrix pat_matrix(p2u[0], p2u[1], p2u[2], p2u[3], p2u[4], p2u[5]);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh gchar *transform_text = sp_svg_transform_write(pat_matrix);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_node->setAttribute("patternTransform", transform_text);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh g_free(transform_text);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_node->setAttribute("patternUnits", "userSpaceOnUse");
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Set pattern tiling
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // FIXME: don't ignore XStep and YStep
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh double *bbox = tiling_pattern->getBBox();
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh sp_repr_set_svg_double(pattern_node, "x", 0.0);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh sp_repr_set_svg_double(pattern_node, "y", 0.0);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh sp_repr_set_svg_double(pattern_node, "width", bbox[2] - bbox[0]);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh sp_repr_set_svg_double(pattern_node, "height", bbox[3] - bbox[1]);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Convert BBox for PdfParser
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh PDFRectangle box;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh box.x1 = bbox[0];
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh box.y1 = bbox[1];
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh box.x2 = bbox[2];
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh box.y2 = bbox[3];
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Create new SvgBuilder and sub-page PdfParser
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh SvgBuilder *pattern_builder = new SvgBuilder(this, pattern_node);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh PdfParser *pdf_parser = new PdfParser(_xref, pattern_builder, tiling_pattern->getResDict(),
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh &box);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Get pattern color space
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh GfxPatternColorSpace *pat_cs = (GfxPatternColorSpace *)( is_stroke ? state->getStrokeColorSpace()
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh : state->getFillColorSpace() );
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Set fill/stroke colors if this is an uncolored tiling pattern
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh GfxColorSpace *cs = NULL;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh if ( tiling_pattern->getPaintType() == 2 && ( cs = pat_cs->getUnder() ) ) {
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh GfxState *pattern_state = pdf_parser->getState();
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_state->setFillColorSpace(cs->copy());
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_state->setFillColor(state->getFillColor());
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_state->setStrokeColorSpace(cs->copy());
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pattern_state->setStrokeColor(state->getFillColor());
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh }
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Generate the SVG pattern
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh pdf_parser->parse(tiling_pattern->getContentStream());
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Cleanup
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh delete pdf_parser;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh delete pattern_builder;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // Append the pattern to defs
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh SP_OBJECT_REPR (SP_DOCUMENT_DEFS (_doc))->appendChild(pattern_node);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh gchar *id = g_strdup(pattern_node->attribute("id"));
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh Inkscape::GC::release(pattern_node);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh return id;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \brief Creates a linear or radial gradient from poppler's data structure
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh * \param shading poppler's data structure for the shading
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh * \param matrix gradient transformation, can be null
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh * \param for_shading true if we're creating this for a shading operator; false otherwise
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \return id of the created object
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
d27f5758e12c3107ee69e66702043931e0756f6bmikloshgchar *SvgBuilder::_createGradient(GfxShading *shading, double *matrix, bool for_shading) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::XML::Node *gradient;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Function *func;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int num_funcs;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh bool extend0, extend1;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( shading->getType() == 2 ) { // Axial shading
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gradient = _xml_doc->createElement("svg:linearGradient");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxAxialShading *axial_shading = (GfxAxialShading*)shading;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double x1, y1, x2, y2;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh axial_shading->getCoords(&x1, &y1, &x2, &y2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "x1", x1);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "y1", y1);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "x2", x2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "y2", y2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh extend0 = axial_shading->getExtend0();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh extend1 = axial_shading->getExtend1();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh num_funcs = axial_shading->getNFuncs();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh func = axial_shading->getFunc(0);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else if (shading->getType() == 3) { // Radial shading
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gradient = _xml_doc->createElement("svg:radialGradient");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh GfxRadialShading *radial_shading = (GfxRadialShading*)shading;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double x1, y1, r1, x2, y2, r2;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh radial_shading->getCoords(&x1, &y1, &r1, &x2, &y2, &r2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // FIXME: the inner circle's radius is ignored here
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "fx", x1);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "fy", y1);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "cx", x2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "cy", y2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(gradient, "r", r2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh extend0 = radial_shading->getExtend0();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh extend1 = radial_shading->getExtend1();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh num_funcs = radial_shading->getNFuncs();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh func = radial_shading->getFunc(0);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else { // Unsupported shading type
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return NULL;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gradient->setAttribute("gradientUnits", "userSpaceOnUse");
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh // If needed, flip the gradient transform around the y axis
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (matrix) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh NR::Matrix pat_matrix(matrix[0], matrix[1], matrix[2], matrix[3],
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh matrix[4], matrix[5]);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( !for_shading && _is_top_level ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh NR::Matrix flip(1.0, 0.0, 0.0, -1.0, 0.0, _height * PT_PER_PX);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh pat_matrix *= flip;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh gchar *transform_text = sp_svg_transform_write(pat_matrix);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh gradient->setAttribute("gradientTransform", transform_text);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh g_free(transform_text);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( extend0 && extend1 ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gradient->setAttribute("spreadMethod", "pad");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( num_funcs > 1 || !_addGradientStops(gradient, shading, func) ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(gradient);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return NULL;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *defs = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (_doc));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh defs->appendChild(gradient);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gchar *id = g_strdup(gradient->attribute("id"));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::GC::release(gradient);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return id;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#define EPSILON 0.0001
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh/**
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh * \brief Adds a stop with the given properties to the gradient's representation
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh */
d27f5758e12c3107ee69e66702043931e0756f6bmikloshvoid SvgBuilder::_addStopToGradient(Inkscape::XML::Node *gradient, double offset,
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxRGB *color, double opacity) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh Inkscape::XML::Node *stop = _xml_doc->createElement("svg:stop");
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh SPCSSAttr *css = sp_repr_css_attr_new();
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh Inkscape::CSSOStringStream os_opacity;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh gchar *color_text = NULL;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh if ( _transp_group_stack != NULL && _transp_group_stack->for_softmask ) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh double gray = (double)color->r / 65535.0;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh gray = CLAMP(gray, 0.0, 1.0);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh os_opacity << gray;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh color_text = "#ffffff";
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh } else {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh os_opacity << opacity;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh color_text = svgConvertGfxRGB(color);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_repr_css_set_property(css, "stop-opacity", os_opacity.str().c_str());
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_repr_css_set_property(css, "stop-color", color_text);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_repr_css_change(stop, css, "style");
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_repr_css_attr_unref(css);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_repr_set_css_double(stop, "offset", offset);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh gradient->appendChild(stop);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh Inkscape::GC::release(stop);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
d27f5758e12c3107ee69e66702043931e0756f6bmikloshstatic bool svgGetShadingColorRGB(GfxShading *shading, double offset, GfxRGB *result) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxColorSpace *color_space = shading->getColorSpace();
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxColor temp;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( shading->getType() == 2 ) { // Axial shading
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh ((GfxAxialShading*)shading)->getColor(offset, &temp);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh } else if ( shading->getType() == 3 ) { // Radial shading
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh ((GfxRadialShading*)shading)->getColor(offset, &temp);
8ec52d39f409ecf67125cd4d878b844e9391e7e8miklosh } else {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh return false;
8ec52d39f409ecf67125cd4d878b844e9391e7e8miklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh // Convert it to RGB
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh color_space->getRGB(&temp, result);
8ec52d39f409ecf67125cd4d878b844e9391e7e8miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return true;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh#define INT_EPSILON 8
d27f5758e12c3107ee69e66702043931e0756f6bmikloshbool SvgBuilder::_addGradientStops(Inkscape::XML::Node *gradient, GfxShading *shading,
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh Function *func) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int type = func->getType();
8ec52d39f409ecf67125cd4d878b844e9391e7e8miklosh if ( type == 0 || type == 2 ) { // Sampled or exponential function
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxRGB stop1, stop2;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( !svgGetShadingColorRGB(shading, 0.0, &stop1) ||
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh !svgGetShadingColorRGB(shading, 1.0, &stop2) ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh return false;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh } else {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh _addStopToGradient(gradient, 0.0, &stop1, 1.0);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh _addStopToGradient(gradient, 1.0, &stop2, 1.0);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if ( type == 3 ) { // Stitching
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh StitchingFunction *stitchingFunc = (StitchingFunction*)func;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh double *bounds = stitchingFunc->getBounds();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh int num_funcs = stitchingFunc->getNumFuncs();
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh // Add stops from all the stitched functions
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0 ; i < num_funcs ; i++ ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxRGB color;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh svgGetShadingColorRGB(shading, bounds[i], &color);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh bool is_continuation = false;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( i > 0 ) { // Compare to previous stop
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxRGB prev_color;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh svgGetShadingColorRGB(shading, bounds[i-1], &prev_color);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( abs(color.r - prev_color.r) < INT_EPSILON &&
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh abs(color.g - prev_color.g) < INT_EPSILON &&
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh abs(color.b - prev_color.b) < INT_EPSILON ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh is_continuation = true;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh // Add stops
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( !is_continuation ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh _addStopToGradient(gradient, bounds[i], &color, 1.0);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh if ( is_continuation || ( i == num_funcs - 1 ) ) {
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh GfxRGB next_color;
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh svgGetShadingColorRGB(shading, bounds[i+1], &next_color);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh _addStopToGradient(gradient, bounds[i+1], &next_color, 1.0);
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else { // Unsupported function type
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return false;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return true;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Sets _invalidated_style to true to indicate that styles have to be updated
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * Used for text output when glyphs are buffered till a font change
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::updateStyle(GfxState *state) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (_in_text_object) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _invalidated_style = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _current_state = state;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh/**
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh * This array holds info about translating font weight names to more or less CSS equivalents
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh */
c4723fe0caa2096d00cb31a7d1506351ba8102dbmikloshstatic char *font_weight_translator[][2] = {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"bold", "bold"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"light", "300"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"black", "900"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"heavy", "900"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"ultrabold", "800"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"extrabold", "800"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"demibold", "600"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"semibold", "600"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"medium", "500"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"book", "normal"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"regular", "normal"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"roman", "normal"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"normal", "normal"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"ultralight", "200"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"extralight", "200"},
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh {"thin", "100"}
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh};
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Updates _font_style according to the font set in parameter state
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::updateFont(GfxState *state) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh TRACE(("updateFont()\n"));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _need_font_update = false;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh updateTextMatrix(state); // Ensure that we have a text matrix built
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (_font_style) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh //sp_repr_css_attr_unref(_font_style);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _font_style = sp_repr_css_attr_new();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GfxFont *font = state->getFont();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Store original name
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (font->getOrigName()) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _font_specification = font->getOrigName()->getCString();
082cac3fb4c7fc60e92bbd4de269550801dcea6bmiklosh } else if (font->getName()) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _font_specification = font->getName()->getCString();
082cac3fb4c7fc60e92bbd4de269550801dcea6bmiklosh } else {
082cac3fb4c7fc60e92bbd4de269550801dcea6bmiklosh _font_specification = "Arial";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Prune the font name to get the correct font family name
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // In a PDF font names can look like this: IONIPB+MetaPlusBold-Italic
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh char *font_family = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh char *font_style = NULL;
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh char *font_style_lowercase = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh char *plus_sign = strstr(_font_specification, "+");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (plus_sign) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh font_family = g_strdup(plus_sign + 1);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _font_specification = plus_sign + 1;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh font_family = g_strdup(_font_specification);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
77364929ced3ec0bc5c9f47440606615c559084emiklosh char *style_delim = NULL;
77364929ced3ec0bc5c9f47440606615c559084emiklosh if ( ( style_delim = g_strrstr(font_family, "-") ) ||
77364929ced3ec0bc5c9f47440606615c559084emiklosh ( style_delim = g_strrstr(font_family, ",") ) ) {
77364929ced3ec0bc5c9f47440606615c559084emiklosh font_style = style_delim + 1;
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh font_style_lowercase = g_ascii_strdown(font_style, -1);
77364929ced3ec0bc5c9f47440606615c559084emiklosh style_delim[0] = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font family
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (font->getFamily()) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-family", font->getFamily()->getCString());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-family", font_family);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font style
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (font->isItalic()) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-style", "italic");
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh } else if (font_style) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh if ( strstr(font_style_lowercase, "italic") ||
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh strstr(font_style_lowercase, "slanted") ) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh sp_repr_css_set_property(_font_style, "font-style", "italic");
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh } else if (strstr(font_style_lowercase, "oblique")) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh sp_repr_css_set_property(_font_style, "font-style", "oblique");
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font variant -- default 'normal' value
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-variant", "normal");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font weight
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GfxFont::Weight font_weight = font->getWeight();
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh char *css_font_weight = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( font_weight != GfxFont::WeightNotDefined ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( font_weight == GfxFont::W400 ) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh css_font_weight = "normal";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if ( font_weight == GfxFont::W700 ) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh css_font_weight = "bold";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar weight_num[4] = "100";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh weight_num[0] = (gchar)( '1' + (font_weight - GfxFont::W100) );
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-weight", (gchar *)&weight_num);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh } else if (font_style) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh // Apply the font weight translations
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh int num_translations = sizeof(font_weight_translator) / ( 2 * sizeof(char *) );
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh for ( int i = 0 ; i < num_translations ; i++ ) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh if (strstr(font_style_lowercase, font_weight_translator[i][0])) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh css_font_weight = font_weight_translator[i][1];
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh }
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh }
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh } else {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh css_font_weight = "normal";
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh }
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh if (css_font_weight) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh sp_repr_css_set_property(_font_style, "font-weight", css_font_weight);
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh }
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh g_free(font_family);
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh if (font_style_lowercase) {
c4723fe0caa2096d00cb31a7d1506351ba8102dbmiklosh g_free(font_style_lowercase);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font stretch
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GfxFont::Stretch font_stretch = font->getStretch();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar *stretch_value = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh switch (font_stretch) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::UltraCondensed:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "ultra-condensed";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::ExtraCondensed:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "extra-condensed";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::Condensed:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "condensed";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::SemiCondensed:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "semi-condensed";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::Normal:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "normal";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::SemiExpanded:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "semi-expanded";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::Expanded:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "expanded";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::ExtraExpanded:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "extra-expanded";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case GfxFont::UltraExpanded:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stretch_value = "ultra-expanded";
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh default:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( stretch_value != NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-stretch", stretch_value);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Font size
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::CSSOStringStream os_font_size;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh double css_font_size = _font_scaling * state->getFontSize();
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh if ( font->getType() == fontType3 ) {
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh double *font_matrix = font->getFontMatrix();
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh if ( font_matrix[0] != 0.0 ) {
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh css_font_size *= font_matrix[3] / font_matrix[0];
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh }
fd39535b3a5276f8962a3f99072668f3e63421edmiklosh }
77364929ced3ec0bc5c9f47440606615c559084emiklosh os_font_size << css_font_size;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "font-size", os_font_size.str().c_str());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Writing mode
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( font->getWMode() == 0 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "writing-mode", "lr");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_set_property(_font_style, "writing-mode", "tb");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _current_font = font;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh _invalidated_style = true;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh}
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh/**
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh * \brief Shifts the current text position by the given amount (specified in text space)
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh */
e45563a3c46261d8c32014f8e516857ba01bd7b7mikloshvoid SvgBuilder::updateTextShift(GfxState *state, double shift) {
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh double shift_value = -shift * 0.001 * fabs(state->getFontSize());
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh if (state->getFont()->getWMode()) {
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh _text_position[1] += shift_value;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh } else {
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh _text_position[0] += shift_value;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh }
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh}
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh/**
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh * \brief Updates current text position
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh */
e45563a3c46261d8c32014f8e516857ba01bd7b7mikloshvoid SvgBuilder::updateTextPosition(double tx, double ty) {
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh NR::Point new_position(tx, ty);
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh _text_position = new_position;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh}
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh/**
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh * \brief Flushes the buffered characters
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh */
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamikloshvoid SvgBuilder::updateTextMatrix(GfxState *state) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh _flushText();
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh // Update text matrix
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh double *text_matrix = state->getTextMat();
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh double w_scale = sqrt( text_matrix[0] * text_matrix[0] + text_matrix[2] * text_matrix[2] );
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh double h_scale = sqrt( text_matrix[1] * text_matrix[1] + text_matrix[3] * text_matrix[3] );
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh double max_scale;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh if ( w_scale > h_scale ) {
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh max_scale = w_scale;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh } else {
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh max_scale = h_scale;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh }
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh // Calculate new text matrix
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh NR::Matrix new_text_matrix(text_matrix[0] * state->getHorizScaling(),
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh text_matrix[1] * state->getHorizScaling(),
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh -text_matrix[2], -text_matrix[3],
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh 0.0, 0.0);
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh if ( fabs( max_scale - 1.0 ) > EPSILON ) {
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh // Cancel out scaling by font size in text matrix
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh for ( int i = 0 ; i < 4 ; i++ ) {
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh new_text_matrix[i] /= max_scale;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh }
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh }
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh _text_matrix = new_text_matrix;
03e63790ef0fa2919fc5f9f3e0d018adf317919dmiklosh _font_scaling = max_scale;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Writes the buffered characters to the SVG document
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::_flushText() {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Ignore empty strings
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( _glyphs.size() < 1 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _glyphs.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh std::vector<SvgGlyph>::iterator i = _glyphs.begin();
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh const SvgGlyph& first_glyph = (*i);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh int render_mode = first_glyph.render_mode;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Ignore invisible characters
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( render_mode == 3 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _glyphs.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *text_node = _xml_doc->createElement("svg:text");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set text matrix
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh NR::Matrix text_transform(_text_matrix);
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh text_transform[4] = first_glyph.position[0];
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh text_transform[5] = first_glyph.position[1];
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh gchar *transform = sp_svg_transform_write(text_transform);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh text_node->setAttribute("transform", transform);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh g_free(transform);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool new_tspan = true;
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh bool same_coords[2] = {true, true};
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh NR::Point last_delta_pos;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh unsigned int glyphs_in_a_row = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *tspan_node = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Glib::ustring x_coords;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Glib::ustring y_coords;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Glib::ustring text_buffer;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool is_vertical = !strcmp(sp_repr_css_property(_font_style, "writing-mode", "lr"), "tb"); // FIXME
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Output all buffered glyphs
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh while (1) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh const SvgGlyph& glyph = (*i);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh std::vector<SvgGlyph>::iterator prev_iterator = i - 1;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Check if we need to make a new tspan
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (glyph.style_changed) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_tspan = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if ( i != _glyphs.begin() ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh const SvgGlyph& prev_glyph = (*prev_iterator);
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh if ( !( ( glyph.dy == 0.0 && prev_glyph.dy == 0.0 &&
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh glyph.text_position[1] == prev_glyph.text_position[1] ) ||
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh ( glyph.dx == 0.0 && prev_glyph.dx == 0.0 &&
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh glyph.text_position[0] == prev_glyph.text_position[0] ) ) ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_tspan = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Create tspan node if needed
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( new_tspan || i == _glyphs.end() ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (tspan_node) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set the x and y coordinate arrays
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh if ( same_coords[0] ) {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh sp_repr_set_svg_double(tspan_node, "x", last_delta_pos[0]);
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh } else {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh tspan_node->setAttribute("x", x_coords.c_str());
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh }
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh if ( same_coords[1] ) {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh sp_repr_set_svg_double(tspan_node, "y", last_delta_pos[1]);
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh } else {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh tspan_node->setAttribute("y", y_coords.c_str());
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh TRACE(("tspan content: %s\n", text_buffer.c_str()));
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh if ( glyphs_in_a_row > 1 ) {
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh tspan_node->setAttribute("sodipodi:role", "line");
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Add text content node to tspan
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *text_content = _xml_doc->createTextNode(text_buffer.c_str());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh tspan_node->appendChild(text_content);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::GC::release(text_content);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh text_node->appendChild(tspan_node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Clear temporary buffers
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh x_coords.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh y_coords.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh text_buffer.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::GC::release(tspan_node);
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh glyphs_in_a_row = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( i == _glyphs.end() ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_attr_unref((*prev_iterator).style);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh tspan_node = _xml_doc->createElement("svg:tspan");
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh tspan_node->setAttribute("inkscape:font-specification", glyph.font_specification);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set style and unref SPCSSAttr if it won't be needed anymore
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh sp_repr_css_change(tspan_node, glyph.style, "style");
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh if ( glyph.style_changed && i != _glyphs.begin() ) { // Free previous style
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh sp_repr_css_attr_unref((*prev_iterator).style);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_tspan = false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh if ( glyphs_in_a_row > 0 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh x_coords.append(" ");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh y_coords.append(" ");
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh // Check if we have the same coordinates
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh const SvgGlyph& prev_glyph = (*prev_iterator);
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh for ( int p = 0 ; p < 2 ; p++ ) {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh if ( glyph.text_position[p] != prev_glyph.text_position[p] ) {
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh same_coords[p] = false;
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh }
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Append the coordinates to their respective strings
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh NR::Point delta_pos( glyph.text_position - first_glyph.text_position );
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh delta_pos[1] += glyph.rise;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh delta_pos[1] *= -1.0; // flip it
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh delta_pos *= _font_scaling;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::CSSOStringStream os_x;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh os_x << delta_pos[0];
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh x_coords.append(os_x.str());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::CSSOStringStream os_y;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh os_y << delta_pos[1];
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh y_coords.append(os_y.str());
522aa9b8f493ba0c8e8b0bb536a563c96f5430a8miklosh last_delta_pos = delta_pos;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Append the character to the text buffer
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh text_buffer.append((char *)&glyph.code, 1);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh glyphs_in_a_row++;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh i++;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container->appendChild(text_node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::GC::release(text_node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _glyphs.clear();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::beginString(GfxState *state, GooString *s) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (_need_font_update) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh updateFont(state);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh IFTRACE(double *m = state->getTextMat());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh TRACE(("tm: %f %f %f %f %f %f\n",m[0], m[1],m[2], m[3], m[4], m[5]));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh IFTRACE(m = state->getCTM());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh TRACE(("ctm: %f %f %f %f %f %f\n",m[0], m[1],m[2], m[3], m[4], m[5]));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Adds the specified character to the text buffer
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * Takes care of converting it to UTF-8 and generates a new style repr if style
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * has changed since the last call.
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::addChar(GfxState *state, double x, double y,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh double dx, double dy,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh double originX, double originY,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh CharCode code, int nBytes, Unicode *u, int uLen) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool is_space = ( uLen == 1 && u[0] == 32 );
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Skip beginning space
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( is_space && _glyphs.size() < 1 ) {
797add969622b3786436365fdd275aff9a9a6373miklosh NR::Point delta(dx, dy);
797add969622b3786436365fdd275aff9a9a6373miklosh _text_position += delta;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Allow only one space in a row
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( is_space && _glyphs[_glyphs.size() - 1].code_size == 1 &&
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _glyphs[_glyphs.size() - 1].code[0] == 32 ) {
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh NR::Point delta(dx, dy);
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh _text_position += delta;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh SvgGlyph new_glyph;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.is_space = is_space;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.position = NR::Point( x - originX, y - originY );
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh new_glyph.text_position = _text_position;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh new_glyph.dx = dx;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh new_glyph.dy = dy;
7ec85862d9730e449ed5c2a86201bc9ca1daa0aamiklosh NR::Point delta(dx, dy);
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh _text_position += delta;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Convert the character to UTF-8 since that's our SVG document's encoding
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh static UnicodeMap *u_map = NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( u_map == NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GooString *enc = new GooString("UTF-8");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh u_map = globalParams->getUnicodeMap(enc);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh u_map->incRefCnt();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh delete enc;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh int code_size = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0 ; i < uLen ; i++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh code_size += u_map->mapUnicode(u[i], (char *)&new_glyph.code[code_size], sizeof(new_glyph.code) - code_size);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.code_size = code_size;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Copy current style if it has changed since the previous glyph
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (_invalidated_style || _glyphs.size() == 0 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.style_changed = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh int render_mode = state->getRender();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set style
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool has_fill = !( render_mode & 1 );
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool has_stroke = ( render_mode & 3 ) == 1 || ( render_mode & 3 ) == 2;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.style = _setStyle(state, has_fill, has_stroke);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.render_mode = render_mode;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_css_merge(new_glyph.style, _font_style); // Merge with font style
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _invalidated_style = false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.style_changed = false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Point to previous glyph's style information
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh const SvgGlyph& prev_glyph = _glyphs.back();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.style = prev_glyph.style;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh new_glyph.render_mode = prev_glyph.render_mode;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh new_glyph.font_specification = _font_specification;
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh new_glyph.rise = state->getRise();
e45563a3c46261d8c32014f8e516857ba01bd7b7miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _glyphs.push_back(new_glyph);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::endString(GfxState *state) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::beginTextObject(GfxState *state) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _in_text_object = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _invalidated_style = true; // Force copying of current state
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _current_state = state;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::endTextObject(GfxState *state) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _flushText();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // TODO: clip if render_mode >= 4
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _in_text_object = false;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * Helper functions for supporting direct PNG output into a base64 encoded stream
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid png_write_base64stream(png_structp png_ptr, png_bytep data, png_size_t length)
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh{
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::IO::Base64OutputStream *stream =
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh (Inkscape::IO::Base64OutputStream*)png_get_io_ptr(png_ptr); // Get pointer to stream
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( unsigned i = 0 ; i < length ; i++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stream->put((int)data[i]);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid png_flush_base64stream(png_structp png_ptr)
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh{
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::IO::Base64OutputStream *stream =
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh (Inkscape::IO::Base64OutputStream*)png_get_io_ptr(png_ptr); // Get pointer to stream
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh stream->flush();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \brief Creates an <image> element containing the given ImageStream as a PNG
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh *
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh */
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshInkscape::XML::Node *SvgBuilder::_createImage(Stream *str, int width, int height,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GfxImageColorMap *color_map, int *mask_colors, bool alpha_only, bool invert_alpha) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Create PNG write struct
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( png_ptr == NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Create PNG info struct
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_infop info_ptr = png_create_info_struct(png_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( info_ptr == NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_destroy_write_struct(&png_ptr, NULL);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set error handler
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (setjmp(png_ptr->jmpbuf)) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_destroy_write_struct(&png_ptr, &info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set read/write functions
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::IO::StringOutputStream base64_string;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::IO::Base64OutputStream base64_stream(base64_string);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh FILE *fp;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar *file_name;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool embed_image = true;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (embed_image) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh base64_stream.setColumnWidth(0); // Disable line breaks
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_write_fn(png_ptr, &base64_stream, png_write_base64stream, png_flush_base64stream);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh static int counter = 0;
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh file_name = g_strdup_printf("%s_img%d.png", _docname, counter++);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh fp = fopen(file_name, "wb");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( fp == NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_destroy_write_struct(&png_ptr, &info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh g_free(file_name);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_init_io(png_ptr, fp);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set header data
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( !invert_alpha && !alpha_only ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_invert_alpha(png_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_color_8 sig_bit;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (alpha_only) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_IHDR(png_ptr, info_ptr,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh width,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh height,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh 8, /* bit_depth */
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_COLOR_TYPE_GRAY,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_INTERLACE_NONE,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_COMPRESSION_TYPE_BASE,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_FILTER_TYPE_BASE);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.red = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.green = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.blue = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.gray = 8;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.alpha = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_IHDR(png_ptr, info_ptr,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh width,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh height,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh 8, /* bit_depth */
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_COLOR_TYPE_RGB_ALPHA,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_INTERLACE_NONE,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_COMPRESSION_TYPE_BASE,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh PNG_FILTER_TYPE_BASE);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.red = 8;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.green = 8;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.blue = 8;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sig_bit.alpha = 8;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_sBIT(png_ptr, info_ptr, &sig_bit);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_set_bgr(png_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Write the file header
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_write_info(png_ptr, info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Convert pixels
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh ImageStream *image_stream;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (alpha_only) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (color_map) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_stream = new ImageStream(str, width, color_map->getNumPixelComps(),
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getBits());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_stream = new ImageStream(str, width, 1, 1);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_stream->reset();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Convert grayscale values
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh unsigned char *buffer = NULL;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( color_map || invert_alpha ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh buffer = new unsigned char[width];
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( color_map ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int y = 0 ; y < height ; y++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned char *row = image_stream->getLine();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getGrayLine(row, buffer, width);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (invert_alpha) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh unsigned char *buf_ptr = buffer;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh for ( int x = 0 ; x < width ; x++ ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh *buf_ptr++ = ~(*buf_ptr);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_write_row(png_ptr, (png_bytep)buffer);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int y = 0 ; y < height ; y++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned char *row = image_stream->getLine();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (invert_alpha) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh unsigned char *buf_ptr = buffer;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh for ( int x = 0 ; x < width ; x++ ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh *buf_ptr++ = ~row[x];
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh png_write_row(png_ptr, (png_bytep)buffer);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh } else {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh png_write_row(png_ptr, (png_bytep)row);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (buffer) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh delete buffer;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else if (color_map) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_stream = new ImageStream(str, width,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getNumPixelComps(),
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getBits());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_stream->reset();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Convert RGB values
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned int *buffer = new unsigned int[width];
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (mask_colors) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int y = 0 ; y < height ; y++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned char *row = image_stream->getLine();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getRGBLine(row, buffer, width);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned int *dest = buffer;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int x = 0 ; x < width ; x++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Check each color component against the mask
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0; i < color_map->getNumPixelComps() ; i++) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( row[i] < mask_colors[2*i] * 255 ||
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh row[i] > mask_colors[2*i + 1] * 255 ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh *dest = *dest | 0xff000000;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Advance to the next pixel
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh row += color_map->getNumPixelComps();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh dest++;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Write it to the PNG
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_write_row(png_ptr, (png_bytep)buffer);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh for ( int i = 0 ; i < height ; i++ ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh unsigned char *row = image_stream->getLine();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh memset((void*)buffer, 0xff, sizeof(int) * width);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh color_map->getRGBLine(row, buffer, width);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_write_row(png_ptr, (png_bytep)buffer);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh delete buffer;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else { // A colormap must be provided, so quit
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_destroy_write_struct(&png_ptr, &info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (!embed_image) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh fclose(fp);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh g_free(file_name);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return NULL;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh delete image_stream;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh str->close();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Close PNG
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_write_end(png_ptr, info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_destroy_write_struct(&png_ptr, &info_ptr);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh base64_stream.close();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Create repr
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *image_node = _xml_doc->createElement("svg:image");
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh sp_repr_set_svg_double(image_node, "width", 1);
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh sp_repr_set_svg_double(image_node, "height", 1);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Set transformation
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh if (_is_top_level) {
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh svgSetTransform(image_node, 1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
ed09b99cb3592f9afb57d2ae3fb88e7f1aca0eb4miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Create href
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (embed_image) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Append format specification to the URI
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Glib::ustring& png_data = base64_string.getString();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh png_data.insert(0, "data:image/png;base64,");
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_node->setAttribute("xlink:href", png_data.c_str());
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh } else {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh fclose(fp);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh image_node->setAttribute("xlink:href", file_name);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh g_free(file_name);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return image_node;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh/**
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh * \brief Creates a <mask> with the specified width and height and adds to <defs>
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh * If we're not the top-level SvgBuilder, creates a <defs> too and adds the mask to it.
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh * \return the created XML node
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh */
1dd83d492fcbfaebb3603846cc163296d1256c97mikloshInkscape::XML::Node *SvgBuilder::_createMask(double width, double height) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_node = _xml_doc->createElement("svg:mask");
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->setAttribute("maskUnits", "userSpaceOnUse");
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(mask_node, "x", 0.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(mask_node, "y", 0.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(mask_node, "width", width);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(mask_node, "height", height);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Append mask to defs
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (_is_top_level) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh SP_OBJECT_REPR (SP_DOCUMENT_DEFS (_doc))->appendChild(mask_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(mask_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh return SP_OBJECT_REPR (SP_DOCUMENT_DEFS (_doc))->lastChild();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh } else { // Work around for renderer bug when mask isn't defined in pattern
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh static int mask_count = 0;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *defs = _root->firstChild();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *result = NULL;
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( !( defs && !strcmp(defs->name(), "svg:defs") ) ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Create <defs> node
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh defs = _xml_doc->createElement("svg:defs");
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh _root->addChild(defs, NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(defs);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh defs = _root->firstChild();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh gchar *mask_id = g_strdup_printf("_mask%d", mask_count++);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->setAttribute("id", mask_id);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh g_free(mask_id);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh defs->appendChild(mask_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(mask_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh return defs->lastChild();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh}
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SvgBuilder::addImage(GfxState *state, Stream *str, int width, int height,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh GfxImageColorMap *color_map, int *mask_colors) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::XML::Node *image_node = _createImage(str, width, height, color_map, mask_colors);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (image_node) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh _container->appendChild(image_node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::GC::release(image_node);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97mikloshvoid SvgBuilder::addImageMask(GfxState *state, Stream *str, int width, int height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh bool invert) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Create a rectangle
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *rect = _xml_doc->createElement("svg:rect");
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(rect, "x", 0.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(rect, "y", 0.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(rect, "width", 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_set_svg_double(rect, "height", 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh svgSetTransform(rect, 1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Get current fill style and set it on the rectangle
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh SPCSSAttr *css = sp_repr_css_attr_new();
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh _setFillStyle(css, state, false);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_css_change(rect, css, "style");
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh sp_repr_css_attr_unref(css);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Scaling 1x1 surfaces might not work so skip setting a mask with this size
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( width > 1 || height > 1 ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_image_node = _createImage(str, width, height, NULL, NULL, true, invert);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (mask_image_node) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Create the mask
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_node = _createMask(1.0, 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Remove unnecessary transformation from the mask image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_image_node->setAttribute("transform", NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->appendChild(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh gchar *mask_url = g_strdup_printf("url(#%s)", mask_node->attribute("id"));
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh rect->setAttribute("mask", mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh g_free(mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Add the rectangle to the container
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh _container->appendChild(rect);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(rect);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh}
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97mikloshvoid SvgBuilder::addMaskedImage(GfxState *state, Stream *str, int width, int height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh GfxImageColorMap *color_map,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Stream *mask_str, int mask_width, int mask_height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh bool invert_mask) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_image_node = _createImage(mask_str, mask_width, mask_height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh NULL, NULL, true, invert_mask);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *image_node = _createImage(str, width, height, color_map, NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( mask_image_node && image_node ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Create mask for the image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_node = _createMask(1.0, 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Remove unnecessary transformation from the mask image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_image_node->setAttribute("transform", NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->appendChild(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Scale the mask to the size of the image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh NR::Matrix mask_transform((double)width, 0.0, 0.0, (double)height, 0.0, 0.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh gchar *transform_text = sp_svg_transform_write(mask_transform);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->setAttribute("maskTransform", transform_text);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh g_free(transform_text);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Set mask and add image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh gchar *mask_url = g_strdup_printf("url(#%s)", mask_node->attribute("id"));
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh image_node->setAttribute("mask", mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh g_free(mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh _container->appendChild(image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (mask_image_node) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (image_node) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh}
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97mikloshvoid SvgBuilder::addSoftMaskedImage(GfxState *state, Stream *str, int width, int height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh GfxImageColorMap *color_map,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Stream *mask_str, int mask_width, int mask_height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh GfxImageColorMap *mask_color_map) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_image_node = _createImage(mask_str, mask_width, mask_height,
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_color_map, NULL, true);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *image_node = _createImage(str, width, height, color_map, NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if ( mask_image_node && image_node ) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Create mask for the image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::XML::Node *mask_node = _createMask(1.0, 1.0);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Remove unnecessary transformation from the mask image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_image_node->setAttribute("transform", NULL);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh mask_node->appendChild(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh // Set mask and add image
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh gchar *mask_url = g_strdup_printf("url(#%s)", mask_node->attribute("id"));
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh image_node->setAttribute("mask", mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh g_free(mask_url);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh _container->appendChild(image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (mask_image_node) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(mask_image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh if (image_node) {
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh Inkscape::GC::release(image_node);
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh }
1dd83d492fcbfaebb3603846cc163296d1256c97miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh/**
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \brief Starts building a new transparency group
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::pushTransparencyGroup(GfxState *state, double *bbox,
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh GfxColorSpace *blending_color_space,
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh bool isolated, bool knockout,
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh bool for_softmask) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Push node stack
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh pushNode("svg:g");
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Setup new transparency group
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgTransparencyGroup *transpGroup = new SvgTransparencyGroup;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh memcpy(&transpGroup->bbox, bbox, sizeof(bbox));
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh transpGroup->isolated = isolated;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh transpGroup->knockout = knockout;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh transpGroup->for_softmask = for_softmask;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh transpGroup->container = _container;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Push onto the stack
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh transpGroup->next = _transp_group_stack;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _transp_group_stack = transpGroup;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::popTransparencyGroup(GfxState *state) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Restore node stack
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh popNode();
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh/**
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \brief Places the current transparency group into the current container
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::paintTransparencyGroup(GfxState *state, double *bbox) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgTransparencyGroup *transpGroup = _transp_group_stack;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _container->appendChild(transpGroup->container);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh Inkscape::GC::release(transpGroup->container);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Pop the stack
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _transp_group_stack = transpGroup->next;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh delete transpGroup;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh/**
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * \brief Creates a mask using the current transparency group as its content
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::setSoftMask(GfxState *state, double *bbox, bool alpha,
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh Function *transfer_func, GfxColor *backdrop_color) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Create mask
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh Inkscape::XML::Node *mask_node = _createMask(1.0, 1.0);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Add the softmask content to it
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SvgTransparencyGroup *transpGroup = _transp_group_stack;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh mask_node->appendChild(transpGroup->container);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh Inkscape::GC::release(transpGroup->container);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Apply the mask
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.back().softmask = mask_node;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh pushGroup();
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh gchar *mask_url = g_strdup_printf("url(#%s)", mask_node->attribute("id"));
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _container->setAttribute("mask", mask_url);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh g_free(mask_url);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // Pop the stack
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _transp_group_stack = transpGroup->next;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh delete transpGroup;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SvgBuilder::clearSoftMask(GfxState *state) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh if (_state_stack.back().softmask) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh _state_stack.back().softmask = NULL;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh popGroup();
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh }
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh} } } /* namespace Inkscape, Extension, Internal */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#endif /* HAVE_POPPLER */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/*
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Local Variables:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh mode:c++
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c-file-style:"stroustrup"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c-file-offsets:((innamespace . 0)(inline-open . 0))
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh indent-tabs-mode:nil
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh fill-column:99
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh End:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh*/
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :