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