diff options
author | Norbert Preining <norbert@preining.info> | 2019-10-29 03:00:39 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-10-29 03:00:39 +0000 |
commit | 97f5151099967e6c823d640a90d87bca92c44035 (patch) | |
tree | 685caa49488599f87d0caa4e972833b2263d8567 /dviware/dvisvgm/src/optimizer | |
parent | 25a647b97ef526aefcc75269dd19c46dbe3ae4c1 (diff) |
CTAN sync 201910290300
Diffstat (limited to 'dviware/dvisvgm/src/optimizer')
-rw-r--r-- | dviware/dvisvgm/src/optimizer/AttributeExtractor.cpp | 20 | ||||
-rw-r--r-- | dviware/dvisvgm/src/optimizer/GroupCollapser.cpp | 16 | ||||
-rw-r--r-- | dviware/dvisvgm/src/optimizer/Makefile.am | 1 | ||||
-rw-r--r-- | dviware/dvisvgm/src/optimizer/Makefile.in | 1 | ||||
-rw-r--r-- | dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp | 49 | ||||
-rw-r--r-- | dviware/dvisvgm/src/optimizer/TransformSimplifier.hpp | 1 |
6 files changed, 62 insertions, 26 deletions
diff --git a/dviware/dvisvgm/src/optimizer/AttributeExtractor.cpp b/dviware/dvisvgm/src/optimizer/AttributeExtractor.cpp index ae2a0e36d7..e26018253d 100644 --- a/dviware/dvisvgm/src/optimizer/AttributeExtractor.cpp +++ b/dviware/dvisvgm/src/optimizer/AttributeExtractor.cpp @@ -113,14 +113,14 @@ XMLNode* AttributeExtractor::extractAttribute (XMLElement *elem) { * @return true if the element is groupable */ bool AttributeExtractor::groupable (const XMLElement &elem) { // https://www.w3.org/TR/SVG/struct.html#GElement - static constexpr auto names = util::make_array( + static const char *names[] = { "a", "altGlyphDef", "animate", "animateColor", "animateMotion", "animateTransform", "circle", "clipPath", "color-profile", "cursor", "defs", "desc", "ellipse", "filter", "font", "font-face", "foreignObject", "g", "image", "line", "linearGradient", "marker", "mask", "path", "pattern", "polygon", "polyline", "radialGradient", "rect", "set", "style", "switch", "symbol", "text", "title", "use", "view" - ); - return binary_search(names.begin(), names.end(), elem.name(), [](const string &name1, const string &name2) { + }; + return binary_search(begin(names), end(names), elem.name(), [](const string &name1, const string &name2) { return name1 < name2; }); } @@ -136,15 +136,15 @@ bool AttributeExtractor::inheritable (const Attribute &attrib) { // clip-path is not inheritable but can be moved to the parent element as long as // no child gets an different clip-path attribute // https://www.w3.org/TR/SVG11/styling.html#Inheritance - static constexpr auto names = util::make_array( + static const char *names[] = { "clip-path", "clip-rule", "color", "color-interpolation", "color-interpolation-filters", "color-profile", "color-rendering", "direction", "fill", "fill-opacity", "fill-rule", "font", "font-family", "font-size", "font-size-adjust", "font-stretch", "font-style", "font-variant", "font-weight", "glyph-orientation-horizontal", "glyph-orientation-vertical", "letter-spacing", "paint-order", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "transform", "visibility", "word-spacing", "writing-mode" - ); - return binary_search(names.begin(), names.end(), attrib.name, [](const string &name1, const string &name2) { + }; + return binary_search(begin(names), end(names), attrib.name, [](const string &name1, const string &name2) { return name1 < name2; }); } @@ -159,13 +159,13 @@ bool AttributeExtractor::extractable (const Attribute &attrib, XMLElement &eleme // the 'fill' attribute of animation elements has different semantics than // that of graphics elements => don't extract it from animation nodes // https://www.w3.org/TR/SVG11/animate.html#TimingAttributes - static constexpr auto names = util::make_array( + static const char *names[] = { "animate", "animateColor", "animateMotion", "animateTransform", "set" - ); - auto it = find_if(names.begin(), names.end(), [&](const string &name) { + }; + auto it = find_if(begin(names), end(names), [&](const string &name) { return element.name() == name; }); - return it == names.end(); + return it == end(names); } diff --git a/dviware/dvisvgm/src/optimizer/GroupCollapser.cpp b/dviware/dvisvgm/src/optimizer/GroupCollapser.cpp index 5d580f5ba0..36bf3a8cd5 100644 --- a/dviware/dvisvgm/src/optimizer/GroupCollapser.cpp +++ b/dviware/dvisvgm/src/optimizer/GroupCollapser.cpp @@ -132,13 +132,13 @@ bool GroupCollapser::moveAttributes (XMLElement &source, XMLElement &dest) { bool GroupCollapser::collapsible (const XMLElement &element) { // the 'fill' attribute of animation elements has different semantics than // that of graphics elements => don't collapse them - static constexpr auto names = util::make_array( + static const char *names[] = { "animate", "animateColor", "animateMotion", "animateTransform", "set" - ); - auto it = find_if(names.begin(), names.end(), [&](const string &name) { + }; + auto it = find_if(begin(names), end(names), [&](const string &name) { return element.name() == name; }); - return it == names.end(); + return it == end(names); } @@ -155,11 +155,11 @@ bool GroupCollapser::unwrappable (const XMLElement &source, const XMLElement &de } } // these attributes prevent a group from being unwrapped - static constexpr auto attribs = util::make_array( + static const char *attribs[] = { "class", "id", "filter", "mask", "style" - ); - auto it = find_if(attribs.begin(), attribs.end(), [&](const string &name) { + }; + auto it = find_if(begin(attribs), end(attribs), [&](const string &name) { return source.hasAttribute(name) || dest.hasAttribute(name); }); - return it == attribs.end(); + return it == end(attribs); } diff --git a/dviware/dvisvgm/src/optimizer/Makefile.am b/dviware/dvisvgm/src/optimizer/Makefile.am index 41c0266009..fec405662a 100644 --- a/dviware/dvisvgm/src/optimizer/Makefile.am +++ b/dviware/dvisvgm/src/optimizer/Makefile.am @@ -10,3 +10,4 @@ liboptimizer_la_SOURCES = \ TransformSimplifier.hpp TransformSimplifier.cpp \ WSNodeRemover.hpp WSNodeRemover.cpp +AM_CXXFLAGS = -I$(dvisvgm_srcdir)/libs/variant/include diff --git a/dviware/dvisvgm/src/optimizer/Makefile.in b/dviware/dvisvgm/src/optimizer/Makefile.in index 64b288a723..45344c1933 100644 --- a/dviware/dvisvgm/src/optimizer/Makefile.in +++ b/dviware/dvisvgm/src/optimizer/Makefile.in @@ -369,6 +369,7 @@ liboptimizer_la_SOURCES = \ TransformSimplifier.hpp TransformSimplifier.cpp \ WSNodeRemover.hpp WSNodeRemover.cpp +AM_CXXFLAGS = -I$(dvisvgm_srcdir)/libs/variant/include all: all-am .SUFFIXES: diff --git a/dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp b/dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp index 06d28926d4..b9d4c3db59 100644 --- a/dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp +++ b/dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp @@ -19,6 +19,7 @@ *************************************************************************/ #include <cmath> +#include <cstdlib> #include "TransformSimplifier.hpp" #include "../Matrix.hpp" #include "../utility.hpp" @@ -38,14 +39,16 @@ void TransformSimplifier::execute (XMLElement *context) { return; if (const char *transform = context->getAttributeValue("transform")) { Matrix matrix = Matrix::parseSVGTransform(transform); - string decomp = decompose(matrix); - if (decomp.length() > matrix.toSVG().length()) - context->addAttribute("transform", matrix.toSVG()); - else { - if (decomp.empty()) - context->removeAttribute("transform"); - else - context->addAttribute("transform", decomp); + if (!incorporateTransform(context, matrix)) { + string decomp = decompose(matrix); + if (decomp.length() > matrix.toSVG().length()) + context->addAttribute("transform", matrix.toSVG()); + else { + if (decomp.empty()) + context->removeAttribute("transform"); + else + context->addAttribute("transform", decomp); + } } } // continue with child elements @@ -56,6 +59,36 @@ void TransformSimplifier::execute (XMLElement *context) { } +/** Tries to incorporate the translation and scaling components of the 'transform' attribute + * of a given element into the positional and/or size attributes of that element. If successful, + * the 'transform' attribute is removed. + * Currently, only 'image' and 'rect' elements are considered. + * @param[in] elem element to check + * @param[in] matrix matrix representing the 'transform' attribute of the element + * @return true on success */ +bool TransformSimplifier::incorporateTransform (XMLElement *elem, const Matrix &matrix) { + if ((elem->name() == "image" || elem->name() == "rect") && matrix.get(0, 1) == 0 && matrix.get(1, 0) == 0) { + double tx = matrix.get(0, 2); + double ty = matrix.get(1, 2); + double sx = matrix.get(0, 0); + double sy = matrix.get(1, 1); + if (const char *xstr = elem->getAttributeValue("x")) + tx += sx*strtod(xstr, nullptr); + if (const char *ystr = elem->getAttributeValue("y")) + ty += sy*strtod(ystr, nullptr); + if (const char *wstr = elem->getAttributeValue("width")) + elem->addAttribute("width", sx*strtod(wstr, nullptr)); + if (const char *hstr = elem->getAttributeValue("height")) + elem->addAttribute("height", sy*strtod(hstr, nullptr)); + elem->addAttribute("x", tx); // update x attribute + elem->addAttribute("y", ty); // update x attribute + elem->removeAttribute("transform"); + return true; + } + return false; +} + + static string translate_cmd (double dx, double dy) { string ret; XMLString dxstr(dx), dystr(dy); diff --git a/dviware/dvisvgm/src/optimizer/TransformSimplifier.hpp b/dviware/dvisvgm/src/optimizer/TransformSimplifier.hpp index 40ac21a465..c4b6d9ee16 100644 --- a/dviware/dvisvgm/src/optimizer/TransformSimplifier.hpp +++ b/dviware/dvisvgm/src/optimizer/TransformSimplifier.hpp @@ -32,5 +32,6 @@ class TransformSimplifier : public OptimizerModule { const char* info () const override; protected: + bool incorporateTransform (XMLElement *elem, const Matrix &matrix); std::string decompose (const Matrix &matrix); }; |