2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Copyright notice for the EFL:
26 * Copyright (C) EFL developers (see AUTHORS)
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
39 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
45 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
46 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
47 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
48 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 #include "tvgSvgLoaderCommon.h"
55 #include "tvgSvgSceneBuilder.h"
56 #include "tvgSvgPath.h"
57 #include "tvgSvgUtil.h"
59 static bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh, const string& svgPath);
60 static unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath, bool mask);
62 /************************************************************************/
63 /* Internal Class Implementation */
64 /************************************************************************/
66 static inline bool _isGroupType(SvgNodeType type)
68 if (type == SvgNodeType::Doc || type == SvgNodeType::G || type == SvgNodeType::Use || type == SvgNodeType::ClipPath) return true;
73 static void _transformMultiply(const Matrix* mBBox, Matrix* gradTransf)
75 gradTransf->e13 = gradTransf->e13 * mBBox->e11 + mBBox->e13;
76 gradTransf->e12 *= mBBox->e11;
77 gradTransf->e11 *= mBBox->e11;
79 gradTransf->e23 = gradTransf->e23 * mBBox->e22 + mBBox->e23;
80 gradTransf->e22 *= mBBox->e22;
81 gradTransf->e21 *= mBBox->e22;
85 static unique_ptr<LinearGradient> _applyLinearGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh, int opacity)
87 Fill::ColorStop* stops;
89 auto fillGrad = LinearGradient::gen();
91 bool isTransform = (g->transform ? true : false);
92 Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
93 if (isTransform) finalTransform = *g->transform;
96 g->linear->x1 = g->linear->x1 * rw;
97 g->linear->y1 = g->linear->y1 * rh;
98 g->linear->x2 = g->linear->x2 * rw;
99 g->linear->y2 = g->linear->y2 * rh;
101 Matrix m = {rw, 0, rx, 0, rh, ry, 0, 0, 1};
102 if (isTransform) _transformMultiply(&m, &finalTransform);
109 if (isTransform) fillGrad->transform(finalTransform);
111 fillGrad->linear(g->linear->x1, g->linear->y1, g->linear->x2, g->linear->y2);
112 fillGrad->spread(g->spread);
115 stopCount = g->stops.count;
117 stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop));
118 if (!stops) return fillGrad;
119 auto prevOffset = 0.0f;
120 for (uint32_t i = 0; i < g->stops.count; ++i) {
121 auto colorStop = &g->stops.data[i];
122 //Use premultiplied color
123 stops[i].r = colorStop->r;
124 stops[i].g = colorStop->g;
125 stops[i].b = colorStop->b;
126 stops[i].a = static_cast<uint8_t>((colorStop->a * opacity) / 255);
127 stops[i].offset = colorStop->offset;
128 //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes
129 if (colorStop->offset < prevOffset) stops[i].offset = prevOffset;
130 else if (colorStop->offset > 1) stops[i].offset = 1;
131 prevOffset = stops[i].offset;
133 fillGrad->colorStops(stops, stopCount);
140 static unique_ptr<RadialGradient> _applyRadialGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh, int opacity)
142 Fill::ColorStop *stops;
144 auto fillGrad = RadialGradient::gen();
146 bool isTransform = (g->transform ? true : false);
147 Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
148 if (isTransform) finalTransform = *g->transform;
151 //The radius scalling is done according to the Units section:
152 //https://www.w3.org/TR/2015/WD-SVG2-20150915/coords.html
153 g->radial->cx = g->radial->cx * rw;
154 g->radial->cy = g->radial->cy * rh;
155 g->radial->r = g->radial->r * sqrtf(powf(rw, 2.0f) + powf(rh, 2.0f)) / sqrtf(2.0f);
156 g->radial->fx = g->radial->fx * rw;
157 g->radial->fy = g->radial->fy * rh;
159 Matrix m = {rw, 0, rx, 0, rh, ry, 0, 0, 1};
160 if (isTransform) _transformMultiply(&m, &finalTransform);
167 if (isTransform) fillGrad->transform(finalTransform);
169 //TODO: Tvg is not support to focal
170 //if (g->radial->fx != 0 && g->radial->fy != 0) {
171 // fillGrad->radial(g->radial->fx, g->radial->fy, g->radial->r);
173 fillGrad->radial(g->radial->cx, g->radial->cy, g->radial->r);
174 fillGrad->spread(g->spread);
177 stopCount = g->stops.count;
179 stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop));
180 if (!stops) return fillGrad;
181 auto prevOffset = 0.0f;
182 for (uint32_t i = 0; i < g->stops.count; ++i) {
183 auto colorStop = &g->stops.data[i];
184 //Use premultiplied color
185 stops[i].r = colorStop->r;
186 stops[i].g = colorStop->g;
187 stops[i].b = colorStop->b;
188 stops[i].a = static_cast<uint8_t>((colorStop->a * opacity) / 255);
189 stops[i].offset = colorStop->offset;
190 //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes
191 if (colorStop->offset < prevOffset) stops[i].offset = prevOffset;
192 else if (colorStop->offset > 1) stops[i].offset = 1;
193 prevOffset = stops[i].offset;
195 fillGrad->colorStops(stops, stopCount);
202 static bool _appendChildShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh, const string& svgPath)
206 if (_appendShape(node, shape, vx, vy, vw, vh, svgPath)) valid = true;
208 if (node->child.count > 0) {
209 auto child = node->child.data;
210 for (uint32_t i = 0; i < node->child.count; ++i, ++child) {
211 if (_appendChildShape(*child, shape, vx, vy, vw, vh, svgPath)) valid = true;
219 static void _applyComposition(Paint* paint, const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath)
222 /* Do not drop in Circular Dependency for ClipPath.
223 Composition can be applied recursively if its children nodes have composition target to this one. */
224 if (node->style->clipPath.applying) {
225 TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?");
227 auto compNode = node->style->clipPath.node;
228 if (compNode && compNode->child.count > 0) {
229 node->style->clipPath.applying = true;
231 auto comp = Shape::gen();
232 comp->fill(255, 255, 255, 255);
233 if (node->transform) comp->transform(*node->transform);
235 auto child = compNode->child.data;
236 auto valid = false; //Composite only when valid shapes are existed
238 for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) {
239 if (_appendChildShape(*child, comp.get(), vx, vy, vw, vh, svgPath)) valid = true;
242 if (valid) paint->composite(move(comp), CompositeMethod::ClipPath);
244 node->style->clipPath.applying = false;
249 /* Do not drop in Circular Dependency for Mask.
250 Composition can be applied recursively if its children nodes have composition target to this one. */
251 if (node->style->mask.applying) {
252 TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?");
254 auto compNode = node->style->mask.node;
255 if (compNode && compNode->child.count > 0) {
256 node->style->mask.applying = true;
258 auto comp = _sceneBuildHelper(compNode, vx, vy, vw, vh, svgPath, true);
260 if (node->transform) comp->transform(*node->transform);
261 paint->composite(move(comp), CompositeMethod::AlphaMask);
264 node->style->mask.applying = false;
270 static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, float vh, const string& svgPath)
272 SvgStyleProperty* style = node->style;
274 if (node->transform) vg->transform(*node->transform);
275 if (node->type == SvgNodeType::Doc || !node->display) return;
277 //If fill property is nullptr then do nothing
278 if (style->fill.paint.none) {
280 } else if (style->fill.paint.gradient) {
281 if (!style->fill.paint.gradient->userSpace) {
282 vg->bounds(&vx, &vy, &vw, &vh, false);
283 //According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph)
284 //a stroke width should be ignored for bounding box calculations
285 if (auto strokeW = vg->strokeWidth()) {
286 vx += 0.5f * strokeW;
287 vy += 0.5f * strokeW;
293 if (style->fill.paint.gradient->type == SvgGradientType::Linear) {
294 auto linear = _applyLinearGradientProperty(style->fill.paint.gradient, vg, vx, vy, vw, vh, style->fill.opacity);
295 vg->fill(move(linear));
296 } else if (style->fill.paint.gradient->type == SvgGradientType::Radial) {
297 auto radial = _applyRadialGradientProperty(style->fill.paint.gradient, vg, vx, vy, vw, vh, style->fill.opacity);
298 vg->fill(move(radial));
300 } else if (style->fill.paint.url) {
301 //TODO: Apply the color pointed by url
302 } else if (style->fill.paint.curColor) {
303 //Apply the current style color
304 vg->fill(style->color.r, style->color.g, style->color.b, style->fill.opacity);
306 //Apply the fill color
307 vg->fill(style->fill.paint.color.r, style->fill.paint.color.g, style->fill.paint.color.b, style->fill.opacity);
310 //Apply the fill rule
311 vg->fill((tvg::FillRule)style->fill.fillRule);
314 if (style->opacity < 255) vg->opacity(style->opacity);
316 if (node->type == SvgNodeType::G || node->type == SvgNodeType::Use) return;
318 //Apply the stroke style property
319 vg->stroke(style->stroke.width);
320 vg->stroke(style->stroke.cap);
321 vg->stroke(style->stroke.join);
322 if (style->stroke.dash.array.count > 0) {
323 vg->stroke(style->stroke.dash.array.data, style->stroke.dash.array.count);
326 //If stroke property is nullptr then do nothing
327 if (style->stroke.paint.none) {
329 } else if (style->stroke.paint.gradient) {
330 if (!style->stroke.paint.gradient->userSpace) {
331 //According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph)
332 //a stroke width should be ignored for bounding box calculations
333 vg->bounds(&vx, &vy, &vw, &vh, false);
334 if (auto strokeW = vg->strokeWidth()) {
335 vx += 0.5f * strokeW;
336 vy += 0.5f * strokeW;
342 if (style->stroke.paint.gradient->type == SvgGradientType::Linear) {
343 auto linear = _applyLinearGradientProperty(style->stroke.paint.gradient, vg, vx, vy, vw, vh, style->stroke.opacity);
344 vg->stroke(move(linear));
345 } else if (style->stroke.paint.gradient->type == SvgGradientType::Radial) {
346 auto radial = _applyRadialGradientProperty(style->stroke.paint.gradient, vg, vx, vy, vw, vh, style->stroke.opacity);
347 vg->stroke(move(radial));
349 } else if (style->stroke.paint.url) {
350 //TODO: Apply the color pointed by url
351 } else if (style->stroke.paint.curColor) {
352 //Apply the current style color
353 vg->stroke(style->color.r, style->color.g, style->color.b, style->stroke.opacity);
355 //Apply the stroke color
356 vg->stroke(style->stroke.paint.color.r, style->stroke.paint.color.g, style->stroke.paint.color.b, style->stroke.opacity);
359 _applyComposition(vg, node, vx, vy, vw, vh, svgPath);
363 static unique_ptr<Shape> _shapeBuildHelper(SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath)
365 auto shape = Shape::gen();
366 if (_appendShape(node, shape.get(), vx, vy, vw, vh, svgPath)) return shape;
371 static bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh, const string& svgPath)
373 Array<PathCommand> cmds;
376 switch (node->type) {
377 case SvgNodeType::Path: {
378 if (node->node.path.path) {
379 if (svgPathToTvgPath(node->node.path.path, cmds, pts)) {
380 shape->appendPath(cmds.data, cmds.count, pts.data, pts.count);
385 case SvgNodeType::Ellipse: {
386 shape->appendCircle(node->node.ellipse.cx, node->node.ellipse.cy, node->node.ellipse.rx, node->node.ellipse.ry);
389 case SvgNodeType::Polygon: {
390 if (node->node.polygon.pointsCount < 2) break;
391 shape->moveTo(node->node.polygon.points[0], node->node.polygon.points[1]);
392 for (int i = 2; i < node->node.polygon.pointsCount - 1; i += 2) {
393 shape->lineTo(node->node.polygon.points[i], node->node.polygon.points[i + 1]);
398 case SvgNodeType::Polyline: {
399 if (node->node.polygon.pointsCount < 2) break;
400 shape->moveTo(node->node.polygon.points[0], node->node.polygon.points[1]);
401 for (int i = 2; i < node->node.polygon.pointsCount - 1; i += 2) {
402 shape->lineTo(node->node.polygon.points[i], node->node.polygon.points[i + 1]);
406 case SvgNodeType::Circle: {
407 shape->appendCircle(node->node.circle.cx, node->node.circle.cy, node->node.circle.r, node->node.circle.r);
410 case SvgNodeType::Rect: {
411 shape->appendRect(node->node.rect.x, node->node.rect.y, node->node.rect.w, node->node.rect.h, node->node.rect.rx, node->node.rect.ry);
414 case SvgNodeType::Line: {
415 shape->moveTo(node->node.line.x1, node->node.line.y1);
416 shape->lineTo(node->node.line.x2, node->node.line.y2);
424 _applyProperty(node, shape, vx, vy, vw, vh, svgPath);
429 enum class imageMimeTypeEncoding
434 constexpr imageMimeTypeEncoding operator|(imageMimeTypeEncoding a, imageMimeTypeEncoding b) {
435 return static_cast<imageMimeTypeEncoding>(static_cast<int>(a) | static_cast<int>(b));
437 constexpr bool operator&(imageMimeTypeEncoding a, imageMimeTypeEncoding b) {
438 return (static_cast<int>(a) & static_cast<int>(b));
442 static constexpr struct
446 imageMimeTypeEncoding encoding;
447 } imageMimeTypes[] = {
448 {"jpeg", sizeof("jpeg"), imageMimeTypeEncoding::base64},
449 {"png", sizeof("png"), imageMimeTypeEncoding::base64},
450 {"svg+xml", sizeof("svg+xml"), imageMimeTypeEncoding::base64 | imageMimeTypeEncoding::utf8},
454 static bool _isValidImageMimeTypeAndEncoding(const char** href, const char** mimetype, imageMimeTypeEncoding* encoding) {
455 if (strncmp(*href, "image/", sizeof("image/") - 1)) return false; //not allowed mime type
456 *href += sizeof("image/") - 1;
458 //RFC2397 data:[<mediatype>][;base64],<data>
459 //mediatype := [ type "/" subtype ] *( ";" parameter )
460 //parameter := attribute "=" value
461 for (unsigned int i = 0; i < sizeof(imageMimeTypes) / sizeof(imageMimeTypes[0]); i++) {
462 if (!strncmp(*href, imageMimeTypes[i].name, imageMimeTypes[i].sz - 1)) {
463 *href += imageMimeTypes[i].sz - 1;
464 *mimetype = imageMimeTypes[i].name;
466 while (**href && **href != ',') {
467 while (**href && **href != ';') ++(*href);
468 if (!**href) return false;
471 if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::base64) {
472 if (!strncmp(*href, "base64,", sizeof("base64,") - 1)) {
473 *href += sizeof("base64,") - 1;
474 *encoding = imageMimeTypeEncoding::base64;
475 return true; //valid base64
478 if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8) {
479 if (!strncmp(*href, "utf8,", sizeof("utf8,") - 1)) {
480 *href += sizeof("utf8,") - 1;
481 *encoding = imageMimeTypeEncoding::utf8;
482 return true; //valid utf8
486 //no encoding defined
487 if (**href == ',' && (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8)) {
489 *encoding = imageMimeTypeEncoding::utf8;
490 return true; //allow no encoding defined if utf8 expected
499 static unique_ptr<Picture> _imageBuildHelper(SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath)
501 if (!node->node.image.href) return nullptr;
502 auto picture = Picture::gen();
504 const char* href = node->node.image.href;
505 if (!strncmp(href, "data:", sizeof("data:") - 1)) {
506 href += sizeof("data:") - 1;
507 const char* mimetype;
508 imageMimeTypeEncoding encoding;
509 if (!_isValidImageMimeTypeAndEncoding(&href, &mimetype, &encoding)) return nullptr; //not allowed mime type or encoding
510 if (encoding == imageMimeTypeEncoding::base64) {
511 string decoded = svgUtilBase64Decode(href);
512 if (picture->load(decoded.c_str(), decoded.size(), mimetype, true) != Result::Success) return nullptr;
514 string decoded = svgUtilURLDecode(href);
515 if (picture->load(decoded.c_str(), decoded.size(), mimetype, true) != Result::Success) return nullptr;
518 if (!strncmp(href, "file://", sizeof("file://") - 1)) href += sizeof("file://") - 1;
519 //TODO: protect against recursive svg image loading
520 //Temporarily disable embedded svg:
521 const char *dot = strrchr(href, '.');
522 if (dot && !strcmp(dot, ".svg")) {
523 TVGLOG("SVG", "Embedded svg file is disabled.");
526 string imagePath = href;
527 if (strncmp(href, "/", 1)) {
528 auto last = svgPath.find_last_of("/");
529 imagePath = svgPath.substr(0, (last == string::npos ? 0 : last + 1 )) + imagePath;
531 if (picture->load(imagePath) != Result::Success) return nullptr;
535 if (picture->size(&w, &h) == Result::Success && w > 0 && h > 0) {
536 auto sx = node->node.image.w / w;
537 auto sy = node->node.image.h / h;
538 Matrix m = {sx, 0, node->node.image.x, 0, sy, node->node.image.y, 0, 0, 1};
539 picture->transform(m);
542 _applyComposition(picture.get(), node, vx, vy, vw, vh, svgPath);
547 static unique_ptr<Scene> _useBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath)
549 auto scene = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath, false);
550 if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) {
551 scene->translate(node->node.use.x, node->node.use.y);
553 if (node->node.use.w > 0.0f && node->node.use.h > 0.0f) {
554 //TODO: handle width/height properties
560 static unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath, bool mask)
562 if (_isGroupType(node->type) || mask) {
563 auto scene = Scene::gen();
564 if (!mask && node->transform) scene->transform(*node->transform);
566 if (node->display && node->style->opacity != 0) {
567 auto child = node->child.data;
568 for (uint32_t i = 0; i < node->child.count; ++i, ++child) {
569 if (_isGroupType((*child)->type)) {
570 if ((*child)->type == SvgNodeType::Use)
571 scene->push(_useBuildHelper(*child, vx, vy, vw, vh, svgPath));
573 scene->push(_sceneBuildHelper(*child, vx, vy, vw, vh, svgPath, false));
574 } else if ((*child)->type == SvgNodeType::Image) {
575 auto image = _imageBuildHelper(*child, vx, vy, vw, vh, svgPath);
576 if (image) scene->push(move(image));
577 } else if ((*child)->type != SvgNodeType::Mask) {
578 auto shape = _shapeBuildHelper(*child, vx, vy, vw, vh, svgPath);
579 if (shape) scene->push(move(shape));
582 _applyComposition(scene.get(), node, vx, vy, vw, vh, svgPath);
583 scene->opacity(node->style->opacity);
591 /************************************************************************/
592 /* External Class Implementation */
593 /************************************************************************/
595 unique_ptr<Scene> svgSceneBuild(SvgNode* node, float vx, float vy, float vw, float vh, float w, float h, bool preserveAspect, const string& svgPath)
597 if (!node || (node->type != SvgNodeType::Doc)) return nullptr;
599 auto docNode = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath, false);
601 if (!mathEqual(w, vw) || !mathEqual(h, vh)) {
605 if (preserveAspect) {
607 auto scale = sx < sy ? sx : sy;
608 docNode->scale(scale);
610 auto tvx = vx * scale;
611 auto tvy = vy * scale;
612 auto tvw = vw * scale;
613 auto tvh = vh * scale;
614 if (vw > vh) tvy -= (h - tvh) * 0.5f;
615 else tvx -= (w - tvw) * 0.5f;
616 docNode->translate(-tvx, -tvy);
623 if (tvw > tvh) tvy -= (h - tvh) * 0.5f;
624 else tvx -= (w - tvw) * 0.5f;
625 Matrix m = {sx, 0, -tvx, 0, sy, -tvy, 0, 0, 1};
626 docNode->transform(m);
628 } else if (!mathZero(vx) || !mathZero(vy)) {
629 docNode->translate(-vx, -vy);
632 auto viewBoxClip = Shape::gen();
633 viewBoxClip->appendRect(0, 0, w, h, 0, 0);
634 viewBoxClip->fill(0, 0, 0, 255);
636 auto compositeLayer = Scene::gen();
637 compositeLayer->composite(move(viewBoxClip), CompositeMethod::ClipPath);
638 compositeLayer->push(move(docNode));
640 auto root = Scene::gen();
641 root->push(move(compositeLayer));