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/TransformSimplifier.cpp | |
parent | 25a647b97ef526aefcc75269dd19c46dbe3ae4c1 (diff) |
CTAN sync 201910290300
Diffstat (limited to 'dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp')
-rw-r--r-- | dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp | 49 |
1 files changed, 41 insertions, 8 deletions
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); |