| 1 | /* |
| 2 | * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 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: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 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 |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "tvgMath.h" /* to include math.h before cstring */ |
| 24 | #include <cstring> |
| 25 | #include <string> |
| 26 | #include "tvgShape.h" |
| 27 | #include "tvgCompressor.h" |
| 28 | #include "tvgPaint.h" |
| 29 | #include "tvgFill.h" |
| 30 | #include "tvgStr.h" |
| 31 | #include "tvgSvgLoaderCommon.h" |
| 32 | #include "tvgSvgSceneBuilder.h" |
| 33 | #include "tvgSvgPath.h" |
| 34 | #include "tvgSvgUtil.h" |
| 35 | |
| 36 | /************************************************************************/ |
| 37 | /* Internal Class Implementation */ |
| 38 | /************************************************************************/ |
| 39 | |
| 40 | static bool _appendShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath); |
| 41 | static bool _appendClipShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, const Matrix* transform); |
| 42 | static unique_ptr<Scene> _sceneBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, bool mask, int depth, bool* isMaskWhite = nullptr); |
| 43 | |
| 44 | |
| 45 | static inline bool _isGroupType(SvgNodeType type) |
| 46 | { |
| 47 | if (type == SvgNodeType::Doc || type == SvgNodeType::G || type == SvgNodeType::Use || type == SvgNodeType::ClipPath || type == SvgNodeType::Symbol) return true; |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | //According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph) |
| 53 | //a stroke width should be ignored for bounding box calculations |
| 54 | static Box _boundingBox(const Shape* shape) |
| 55 | { |
| 56 | float x, y, w, h; |
| 57 | shape->bounds(&x, &y, &w, &h, false); |
| 58 | |
| 59 | if (auto strokeW = shape->strokeWidth()) { |
| 60 | x += 0.5f * strokeW; |
| 61 | y += 0.5f * strokeW; |
| 62 | w -= strokeW; |
| 63 | h -= strokeW; |
| 64 | } |
| 65 | |
| 66 | return {x, y, w, h}; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static Box _boundingBox(const Text* text) |
| 71 | { |
| 72 | float x, y, w, h; |
| 73 | text->bounds(&x, &y, &w, &h, false); |
| 74 | return {x, y, w, h}; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static void _transformMultiply(const Matrix* mBBox, Matrix* gradTransf) |
| 79 | { |
| 80 | gradTransf->e13 = gradTransf->e13 * mBBox->e11 + mBBox->e13; |
| 81 | gradTransf->e12 *= mBBox->e11; |
| 82 | gradTransf->e11 *= mBBox->e11; |
| 83 | |
| 84 | gradTransf->e23 = gradTransf->e23 * mBBox->e22 + mBBox->e23; |
| 85 | gradTransf->e22 *= mBBox->e22; |
| 86 | gradTransf->e21 *= mBBox->e22; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static unique_ptr<LinearGradient> _applyLinearGradientProperty(SvgStyleGradient* g, const Box& vBox, int opacity) |
| 91 | { |
| 92 | Fill::ColorStop* stops; |
| 93 | int stopCount = 0; |
| 94 | auto fillGrad = LinearGradient::gen(); |
| 95 | |
| 96 | bool isTransform = (g->transform ? true : false); |
| 97 | Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 98 | if (isTransform) finalTransform = *g->transform; |
| 99 | |
| 100 | if (g->userSpace) { |
| 101 | g->linear->x1 = g->linear->x1 * vBox.w; |
| 102 | g->linear->y1 = g->linear->y1 * vBox.h; |
| 103 | g->linear->x2 = g->linear->x2 * vBox.w; |
| 104 | g->linear->y2 = g->linear->y2 * vBox.h; |
| 105 | } else { |
| 106 | Matrix m = {vBox.w, 0, vBox.x, 0, vBox.h, vBox.y, 0, 0, 1}; |
| 107 | if (isTransform) _transformMultiply(&m, &finalTransform); |
| 108 | else { |
| 109 | finalTransform = m; |
| 110 | isTransform = true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (isTransform) fillGrad->transform(finalTransform); |
| 115 | |
| 116 | fillGrad->linear(g->linear->x1, g->linear->y1, g->linear->x2, g->linear->y2); |
| 117 | fillGrad->spread(g->spread); |
| 118 | |
| 119 | //Update the stops |
| 120 | stopCount = g->stops.count; |
| 121 | if (stopCount > 0) { |
| 122 | stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop)); |
| 123 | if (!stops) return fillGrad; |
| 124 | auto prevOffset = 0.0f; |
| 125 | for (uint32_t i = 0; i < g->stops.count; ++i) { |
| 126 | auto colorStop = &g->stops[i]; |
| 127 | //Use premultiplied color |
| 128 | stops[i].r = colorStop->r; |
| 129 | stops[i].g = colorStop->g; |
| 130 | stops[i].b = colorStop->b; |
| 131 | stops[i].a = static_cast<uint8_t>((colorStop->a * opacity) / 255); |
| 132 | stops[i].offset = colorStop->offset; |
| 133 | //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes |
| 134 | if (colorStop->offset < prevOffset) stops[i].offset = prevOffset; |
| 135 | else if (colorStop->offset > 1) stops[i].offset = 1; |
| 136 | prevOffset = stops[i].offset; |
| 137 | } |
| 138 | fillGrad->colorStops(stops, stopCount); |
| 139 | free(stops); |
| 140 | } |
| 141 | return fillGrad; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static unique_ptr<RadialGradient> _applyRadialGradientProperty(SvgStyleGradient* g, const Box& vBox, int opacity) |
| 146 | { |
| 147 | Fill::ColorStop *stops; |
| 148 | int stopCount = 0; |
| 149 | auto fillGrad = RadialGradient::gen(); |
| 150 | |
| 151 | bool isTransform = (g->transform ? true : false); |
| 152 | Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 153 | if (isTransform) finalTransform = *g->transform; |
| 154 | |
| 155 | if (g->userSpace) { |
| 156 | //The radius scaling is done according to the Units section: |
| 157 | //https://www.w3.org/TR/2015/WD-SVG2-20150915/coords.html |
| 158 | g->radial->cx = g->radial->cx * vBox.w; |
| 159 | g->radial->cy = g->radial->cy * vBox.h; |
| 160 | g->radial->r = g->radial->r * sqrtf(powf(vBox.w, 2.0f) + powf(vBox.h, 2.0f)) / sqrtf(2.0f); |
| 161 | g->radial->fx = g->radial->fx * vBox.w; |
| 162 | g->radial->fy = g->radial->fy * vBox.h; |
| 163 | g->radial->fr = g->radial->fr * sqrtf(powf(vBox.w, 2.0f) + powf(vBox.h, 2.0f)) / sqrtf(2.0f); |
| 164 | } else { |
| 165 | Matrix m = {vBox.w, 0, vBox.x, 0, vBox.h, vBox.y, 0, 0, 1}; |
| 166 | if (isTransform) _transformMultiply(&m, &finalTransform); |
| 167 | else { |
| 168 | finalTransform = m; |
| 169 | isTransform = true; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (isTransform) fillGrad->transform(finalTransform); |
| 174 | |
| 175 | P(fillGrad)->radial(g->radial->cx, g->radial->cy, g->radial->r, g->radial->fx, g->radial->fy, g->radial->fr); |
| 176 | fillGrad->spread(g->spread); |
| 177 | |
| 178 | //Update the stops |
| 179 | stopCount = g->stops.count; |
| 180 | if (stopCount > 0) { |
| 181 | stops = (Fill::ColorStop*)calloc(stopCount, sizeof(Fill::ColorStop)); |
| 182 | if (!stops) return fillGrad; |
| 183 | auto prevOffset = 0.0f; |
| 184 | for (uint32_t i = 0; i < g->stops.count; ++i) { |
| 185 | auto colorStop = &g->stops[i]; |
| 186 | //Use premultiplied color |
| 187 | stops[i].r = colorStop->r; |
| 188 | stops[i].g = colorStop->g; |
| 189 | stops[i].b = colorStop->b; |
| 190 | stops[i].a = static_cast<uint8_t>((colorStop->a * opacity) / 255); |
| 191 | stops[i].offset = colorStop->offset; |
| 192 | //check the offset corner cases - refer to: https://svgwg.org/svg2-draft/pservers.html#StopNotes |
| 193 | if (colorStop->offset < prevOffset) stops[i].offset = prevOffset; |
| 194 | else if (colorStop->offset > 1) stops[i].offset = 1; |
| 195 | prevOffset = stops[i].offset; |
| 196 | } |
| 197 | fillGrad->colorStops(stops, stopCount); |
| 198 | free(stops); |
| 199 | } |
| 200 | return fillGrad; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | //The SVG standard allows only for 'use' nodes that point directly to a basic shape. |
| 205 | static bool _appendClipUseNode(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath) |
| 206 | { |
| 207 | if (node->child.count != 1) return false; |
| 208 | auto child = *(node->child.data); |
| 209 | |
| 210 | Matrix finalTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 211 | if (node->transform) finalTransform = *node->transform; |
| 212 | if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) { |
| 213 | Matrix m = {1, 0, node->node.use.x, 0, 1, node->node.use.y, 0, 0, 1}; |
| 214 | finalTransform *= m; |
| 215 | } |
| 216 | if (child->transform) finalTransform *= *child->transform; |
| 217 | |
| 218 | return _appendClipShape(loaderData, child, shape, vBox, svgPath, identity((const Matrix*)(&finalTransform)) ? nullptr : &finalTransform); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static bool _appendClipChild(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, bool clip) |
| 223 | { |
| 224 | if (node->type == SvgNodeType::Use) { |
| 225 | return _appendClipUseNode(loaderData, node, shape, vBox, svgPath); |
| 226 | } |
| 227 | return _appendClipShape(loaderData, node, shape, vBox, svgPath, nullptr); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static Matrix _compositionTransform(Paint* paint, const SvgNode* node, const SvgNode* compNode, SvgNodeType type) |
| 232 | { |
| 233 | Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 234 | //The initial mask transformation ignored according to the SVG standard. |
| 235 | if (node->transform && type != SvgNodeType::Mask) { |
| 236 | m = *node->transform; |
| 237 | } |
| 238 | if (compNode->transform) { |
| 239 | m *= *compNode->transform; |
| 240 | } |
| 241 | if (!compNode->node.clip.userSpace) { |
| 242 | float x, y, w, h; |
| 243 | P(paint)->bounds(&x, &y, &w, &h, false, false); |
| 244 | Matrix mBBox = {w, 0, x, 0, h, y, 0, 0, 1}; |
| 245 | m *= mBBox; |
| 246 | } |
| 247 | return m; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | static void _applyComposition(SvgLoaderData& loaderData, Paint* paint, const SvgNode* node, const Box& vBox, const string& svgPath) |
| 252 | { |
| 253 | /* ClipPath */ |
| 254 | /* Do not drop in Circular Dependency for ClipPath. |
| 255 | Composition can be applied recursively if its children nodes have composition target to this one. */ |
| 256 | if (node->style->clipPath.applying) { |
| 257 | TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?"); |
| 258 | } else { |
| 259 | auto compNode = node->style->clipPath.node; |
| 260 | if (compNode && compNode->child.count > 0) { |
| 261 | node->style->clipPath.applying = true; |
| 262 | |
| 263 | auto comp = Shape::gen(); |
| 264 | |
| 265 | auto child = compNode->child.data; |
| 266 | auto valid = false; //Composite only when valid shapes exist |
| 267 | |
| 268 | for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) { |
| 269 | if (_appendClipChild(loaderData, *child, comp.get(), vBox, svgPath, compNode->child.count > 1)) valid = true; |
| 270 | } |
| 271 | |
| 272 | if (valid) { |
| 273 | Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::ClipPath); |
| 274 | comp->transform(finalTransform); |
| 275 | paint->clip(std::move(comp)); |
| 276 | } |
| 277 | |
| 278 | node->style->clipPath.applying = false; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* Mask */ |
| 283 | /* Do not drop in Circular Dependency for Mask. |
| 284 | Composition can be applied recursively if its children nodes have composition target to this one. */ |
| 285 | if (node->style->mask.applying) { |
| 286 | TVGLOG("SVG", "Multiple Composition Tried! Check out Circular dependency?"); |
| 287 | } else { |
| 288 | auto compNode = node->style->mask.node; |
| 289 | if (compNode && compNode->child.count > 0) { |
| 290 | node->style->mask.applying = true; |
| 291 | |
| 292 | bool isMaskWhite = true; |
| 293 | if (auto comp = _sceneBuildHelper(loaderData, compNode, vBox, svgPath, true, 0, &isMaskWhite)) { |
| 294 | if (!compNode->node.mask.userSpace) { |
| 295 | Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::Mask); |
| 296 | comp->transform(finalTransform); |
| 297 | } else { |
| 298 | if (node->transform) comp->transform(*node->transform); |
| 299 | } |
| 300 | |
| 301 | if (compNode->node.mask.type == SvgMaskType::Luminance && !isMaskWhite) { |
| 302 | paint->composite(std::move(comp), CompositeMethod::LumaMask); |
| 303 | } else { |
| 304 | paint->composite(std::move(comp), CompositeMethod::AlphaMask); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | node->style->mask.applying = false; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static void _applyProperty(SvgLoaderData& loaderData, SvgNode* node, Shape* vg, const Box& vBox, const string& svgPath, bool clip) |
| 315 | { |
| 316 | SvgStyleProperty* style = node->style; |
| 317 | |
| 318 | //Clip transformation is applied directly to the path in the _appendClipShape function |
| 319 | if (node->transform && !clip) vg->transform(*node->transform); |
| 320 | if (node->type == SvgNodeType::Doc || !node->style->display) return; |
| 321 | |
| 322 | //If fill property is nullptr then do nothing |
| 323 | if (style->fill.paint.none) { |
| 324 | //Do nothing |
| 325 | } else if (style->fill.paint.gradient) { |
| 326 | Box bBox = vBox; |
| 327 | if (!style->fill.paint.gradient->userSpace) bBox = _boundingBox(vg); |
| 328 | |
| 329 | if (style->fill.paint.gradient->type == SvgGradientType::Linear) { |
| 330 | auto linear = _applyLinearGradientProperty(style->fill.paint.gradient, bBox, style->fill.opacity); |
| 331 | vg->fill(std::move(linear)); |
| 332 | } else if (style->fill.paint.gradient->type == SvgGradientType::Radial) { |
| 333 | auto radial = _applyRadialGradientProperty(style->fill.paint.gradient, bBox, style->fill.opacity); |
| 334 | vg->fill(std::move(radial)); |
| 335 | } |
| 336 | } else if (style->fill.paint.url) { |
| 337 | //TODO: Apply the color pointed by url |
| 338 | TVGLOG("SVG", "The fill's url not supported."); |
| 339 | } else if (style->fill.paint.curColor) { |
| 340 | //Apply the current style color |
| 341 | vg->fill(style->color.r, style->color.g, style->color.b, style->fill.opacity); |
| 342 | } else { |
| 343 | //Apply the fill color |
| 344 | vg->fill(style->fill.paint.color.r, style->fill.paint.color.g, style->fill.paint.color.b, style->fill.opacity); |
| 345 | } |
| 346 | |
| 347 | //Apply the fill rule |
| 348 | vg->fill((tvg::FillRule)style->fill.fillRule); |
| 349 | //Rendering order |
| 350 | vg->order(!style->paintOrder); |
| 351 | |
| 352 | //Apply node opacity |
| 353 | if (style->opacity < 255) vg->opacity(style->opacity); |
| 354 | |
| 355 | if (node->type == SvgNodeType::G || node->type == SvgNodeType::Use) return; |
| 356 | |
| 357 | //Apply the stroke style property |
| 358 | vg->stroke(style->stroke.width); |
| 359 | vg->stroke(style->stroke.cap); |
| 360 | vg->stroke(style->stroke.join); |
| 361 | vg->strokeMiterlimit(style->stroke.miterlimit); |
| 362 | if (style->stroke.dash.array.count > 0) { |
| 363 | P(vg)->strokeDash(style->stroke.dash.array.data, style->stroke.dash.array.count, style->stroke.dash.offset); |
| 364 | } |
| 365 | |
| 366 | //If stroke property is nullptr then do nothing |
| 367 | if (style->stroke.paint.none) { |
| 368 | vg->stroke(0.0f); |
| 369 | } else if (style->stroke.paint.gradient) { |
| 370 | Box bBox = vBox; |
| 371 | if (!style->stroke.paint.gradient->userSpace) bBox = _boundingBox(vg); |
| 372 | |
| 373 | if (style->stroke.paint.gradient->type == SvgGradientType::Linear) { |
| 374 | auto linear = _applyLinearGradientProperty(style->stroke.paint.gradient, bBox, style->stroke.opacity); |
| 375 | vg->stroke(std::move(linear)); |
| 376 | } else if (style->stroke.paint.gradient->type == SvgGradientType::Radial) { |
| 377 | auto radial = _applyRadialGradientProperty(style->stroke.paint.gradient, bBox, style->stroke.opacity); |
| 378 | vg->stroke(std::move(radial)); |
| 379 | } |
| 380 | } else if (style->stroke.paint.url) { |
| 381 | //TODO: Apply the color pointed by url |
| 382 | TVGLOG("SVG", "The stroke's url not supported."); |
| 383 | } else if (style->stroke.paint.curColor) { |
| 384 | //Apply the current style color |
| 385 | vg->stroke(style->color.r, style->color.g, style->color.b, style->stroke.opacity); |
| 386 | } else { |
| 387 | //Apply the stroke color |
| 388 | vg->stroke(style->stroke.paint.color.r, style->stroke.paint.color.g, style->stroke.paint.color.b, style->stroke.opacity); |
| 389 | } |
| 390 | |
| 391 | _applyComposition(loaderData, vg, node, vBox, svgPath); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | static unique_ptr<Shape> _shapeBuildHelper(SvgLoaderData& loaderData, SvgNode* node, const Box& vBox, const string& svgPath) |
| 396 | { |
| 397 | auto shape = Shape::gen(); |
| 398 | if (_appendShape(loaderData, node, shape.get(), vBox, svgPath)) return shape; |
| 399 | else return nullptr; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static bool _recognizeShape(SvgNode* node, Shape* shape) |
| 404 | { |
| 405 | switch (node->type) { |
| 406 | case SvgNodeType::Path: { |
| 407 | if (node->node.path.path) { |
| 408 | if (!svgPathToShape(node->node.path.path, shape)) { |
| 409 | TVGERR("SVG", "Invalid path information."); |
| 410 | return false; |
| 411 | } |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | case SvgNodeType::Ellipse: { |
| 416 | shape->appendCircle(node->node.ellipse.cx, node->node.ellipse.cy, node->node.ellipse.rx, node->node.ellipse.ry); |
| 417 | break; |
| 418 | } |
| 419 | case SvgNodeType::Polygon: { |
| 420 | if (node->node.polygon.pts.count < 2) break; |
| 421 | auto pts = node->node.polygon.pts.begin(); |
| 422 | shape->moveTo(pts[0], pts[1]); |
| 423 | for (pts += 2; pts < node->node.polygon.pts.end(); pts += 2) { |
| 424 | shape->lineTo(pts[0], pts[1]); |
| 425 | } |
| 426 | shape->close(); |
| 427 | break; |
| 428 | } |
| 429 | case SvgNodeType::Polyline: { |
| 430 | if (node->node.polyline.pts.count < 2) break; |
| 431 | auto pts = node->node.polyline.pts.begin(); |
| 432 | shape->moveTo(pts[0], pts[1]); |
| 433 | for (pts += 2; pts < node->node.polyline.pts.end(); pts += 2) { |
| 434 | shape->lineTo(pts[0], pts[1]); |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | case SvgNodeType::Circle: { |
| 439 | shape->appendCircle(node->node.circle.cx, node->node.circle.cy, node->node.circle.r, node->node.circle.r); |
| 440 | break; |
| 441 | } |
| 442 | case SvgNodeType::Rect: { |
| 443 | 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); |
| 444 | break; |
| 445 | } |
| 446 | case SvgNodeType::Line: { |
| 447 | shape->moveTo(node->node.line.x1, node->node.line.y1); |
| 448 | shape->lineTo(node->node.line.x2, node->node.line.y2); |
| 449 | break; |
| 450 | } |
| 451 | default: { |
| 452 | return false; |
| 453 | } |
| 454 | } |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | static bool _appendShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath) |
| 460 | { |
| 461 | if (!_recognizeShape(node, shape)) return false; |
| 462 | |
| 463 | _applyProperty(loaderData, node, shape, vBox, svgPath, false); |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static bool _appendClipShape(SvgLoaderData& loaderData, SvgNode* node, Shape* shape, const Box& vBox, const string& svgPath, const Matrix* transform) |
| 469 | { |
| 470 | //The 'transform' matrix has higher priority than the node->transform, since it already contains it |
| 471 | auto m = transform ? transform : (node->transform ? node->transform : nullptr); |
| 472 | |
| 473 | uint32_t currentPtsCnt = 0; |
| 474 | if (m) { |
| 475 | const Point *tmp = nullptr; |
| 476 | currentPtsCnt = shape->pathCoords(&tmp); |
| 477 | } |
| 478 | |
| 479 | if (!_recognizeShape(node, shape)) return false; |
| 480 | |
| 481 | if (m) { |
| 482 | const Point *pts = nullptr; |
| 483 | auto ptsCnt = shape->pathCoords(&pts); |
| 484 | |
| 485 | auto p = const_cast<Point*>(pts) + currentPtsCnt; |
| 486 | while (currentPtsCnt++ < ptsCnt) { |
| 487 | *p *= *m; |
| 488 | ++p; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | _applyProperty(loaderData, node, shape, vBox, svgPath, true); |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | enum class imageMimeTypeEncoding |
| 498 | { |
| 499 | base64 = 0x1, |
| 500 | utf8 = 0x2 |
| 501 | }; |
| 502 | |
| 503 | constexpr imageMimeTypeEncoding operator|(imageMimeTypeEncoding a, imageMimeTypeEncoding b) { |
| 504 | return static_cast<imageMimeTypeEncoding>(static_cast<int>(a) | static_cast<int>(b)); |
| 505 | } |
| 506 | |
| 507 | constexpr bool operator&(imageMimeTypeEncoding a, imageMimeTypeEncoding b) { |
| 508 | return (static_cast<int>(a) & static_cast<int>(b)); |
| 509 | } |
| 510 | |
| 511 | |
| 512 | static constexpr struct |
| 513 | { |
| 514 | const char* name; |
| 515 | int sz; |
| 516 | imageMimeTypeEncoding encoding; |
| 517 | } imageMimeTypes[] = { |
| 518 | {"jpeg", sizeof("jpeg"), imageMimeTypeEncoding::base64}, |
| 519 | {"png", sizeof("png"), imageMimeTypeEncoding::base64}, |
| 520 | {"webp", sizeof("webp"), imageMimeTypeEncoding::base64}, |
| 521 | {"svg+xml", sizeof("svg+xml"), imageMimeTypeEncoding::base64 | imageMimeTypeEncoding::utf8}, |
| 522 | }; |
| 523 | |
| 524 | |
| 525 | static bool _isValidImageMimeTypeAndEncoding(const char** href, const char** mimetype, imageMimeTypeEncoding* encoding) { |
| 526 | if (strncmp(*href, "image/", sizeof("image/") - 1)) return false; //not allowed mime type |
| 527 | *href += sizeof("image/") - 1; |
| 528 | |
| 529 | //RFC2397 data:[<mediatype>][;base64],<data> |
| 530 | //mediatype := [ type "/" subtype ] *( ";" parameter ) |
| 531 | //parameter := attribute "=" value |
| 532 | for (unsigned int i = 0; i < sizeof(imageMimeTypes) / sizeof(imageMimeTypes[0]); i++) { |
| 533 | if (!strncmp(*href, imageMimeTypes[i].name, imageMimeTypes[i].sz - 1)) { |
| 534 | *href += imageMimeTypes[i].sz - 1; |
| 535 | *mimetype = imageMimeTypes[i].name; |
| 536 | |
| 537 | while (**href && **href != ',') { |
| 538 | while (**href && **href != ';') ++(*href); |
| 539 | if (!**href) return false; |
| 540 | ++(*href); |
| 541 | |
| 542 | if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::base64) { |
| 543 | if (!strncmp(*href, "base64,", sizeof("base64,") - 1)) { |
| 544 | *href += sizeof("base64,") - 1; |
| 545 | *encoding = imageMimeTypeEncoding::base64; |
| 546 | return true; //valid base64 |
| 547 | } |
| 548 | } |
| 549 | if (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8) { |
| 550 | if (!strncmp(*href, "utf8,", sizeof("utf8,") - 1)) { |
| 551 | *href += sizeof("utf8,") - 1; |
| 552 | *encoding = imageMimeTypeEncoding::utf8; |
| 553 | return true; //valid utf8 |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | //no encoding defined |
| 558 | if (**href == ',' && (imageMimeTypes[i].encoding & imageMimeTypeEncoding::utf8)) { |
| 559 | ++(*href); |
| 560 | *encoding = imageMimeTypeEncoding::utf8; |
| 561 | return true; //allow no encoding defined if utf8 expected |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | #include "tvgTaskScheduler.h" |
| 570 | |
| 571 | static unique_ptr<Picture> _imageBuildHelper(SvgLoaderData& loaderData, SvgNode* node, const Box& vBox, const string& svgPath) |
| 572 | { |
| 573 | if (!node->node.image.href || !strlen(node->node.image.href)) return nullptr; |
| 574 | auto picture = Picture::gen(); |
| 575 | |
| 576 | const char* href = node->node.image.href; |
| 577 | if (!strncmp(href, "data:", sizeof("data:") - 1)) { |
| 578 | href += sizeof("data:") - 1; |
| 579 | const char* mimetype; |
| 580 | imageMimeTypeEncoding encoding; |
| 581 | if (!_isValidImageMimeTypeAndEncoding(&href, &mimetype, &encoding)) return nullptr; //not allowed mime type or encoding |
| 582 | char *decoded = nullptr; |
| 583 | if (encoding == imageMimeTypeEncoding::base64) { |
| 584 | auto size = b64Decode(href, strlen(href), &decoded); |
| 585 | if (picture->load(decoded, size, mimetype, false) != Result::Success) { |
| 586 | free(decoded); |
| 587 | return nullptr; |
| 588 | } |
| 589 | } else { |
| 590 | auto size = svgUtilURLDecode(href, &decoded); |
| 591 | if (picture->load(decoded, size, mimetype, false) != Result::Success) { |
| 592 | free(decoded); |
| 593 | return nullptr; |
| 594 | } |
| 595 | } |
| 596 | loaderData.images.push(decoded); |
| 597 | } else { |
| 598 | if (!strncmp(href, "file://", sizeof("file://") - 1)) href += sizeof("file://") - 1; |
| 599 | //TODO: protect against recursive svg image loading |
| 600 | //Temporarily disable embedded svg: |
| 601 | const char *dot = strrchr(href, '.'); |
| 602 | if (dot && !strcmp(dot, ".svg")) { |
| 603 | TVGLOG("SVG", "Embedded svg file is disabled."); |
| 604 | return nullptr; |
| 605 | } |
| 606 | string imagePath = href; |
| 607 | if (strncmp(href, "/", 1)) { |
| 608 | auto last = svgPath.find_last_of("/"); |
| 609 | imagePath = svgPath.substr(0, (last == string::npos ? 0 : last + 1)) + imagePath; |
| 610 | } |
| 611 | if (picture->load(imagePath) != Result::Success) { |
| 612 | return nullptr; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | float w, h; |
| 617 | Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 618 | if (picture->size(&w, &h) == Result::Success && w > 0 && h > 0) { |
| 619 | auto sx = node->node.image.w / w; |
| 620 | auto sy = node->node.image.h / h; |
| 621 | m = {sx, 0, node->node.image.x, 0, sy, node->node.image.y, 0, 0, 1}; |
| 622 | } |
| 623 | if (node->transform) m = *node->transform * m; |
| 624 | picture->transform(m); |
| 625 | |
| 626 | _applyComposition(loaderData, picture.get(), node, vBox, svgPath); |
| 627 | |
| 628 | return picture; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | static Matrix _calculateAspectRatioMatrix(AspectRatioAlign align, AspectRatioMeetOrSlice meetOrSlice, float width, float height, const Box& box) |
| 633 | { |
| 634 | auto sx = width / box.w; |
| 635 | auto sy = height / box.h; |
| 636 | auto tvx = box.x * sx; |
| 637 | auto tvy = box.y * sy; |
| 638 | |
| 639 | if (align == AspectRatioAlign::None) |
| 640 | return {sx, 0, -tvx, 0, sy, -tvy, 0, 0, 1}; |
| 641 | |
| 642 | //Scale |
| 643 | if (meetOrSlice == AspectRatioMeetOrSlice::Meet) { |
| 644 | if (sx < sy) sy = sx; |
| 645 | else sx = sy; |
| 646 | } else { |
| 647 | if (sx < sy) sx = sy; |
| 648 | else sy = sx; |
| 649 | } |
| 650 | |
| 651 | //Align |
| 652 | tvx = box.x * sx; |
| 653 | tvy = box.y * sy; |
| 654 | auto tvw = box.w * sx; |
| 655 | auto tvh = box.h * sy; |
| 656 | |
| 657 | switch (align) { |
| 658 | case AspectRatioAlign::XMinYMin: { |
| 659 | break; |
| 660 | } |
| 661 | case AspectRatioAlign::XMidYMin: { |
| 662 | tvx -= (width - tvw) * 0.5f; |
| 663 | break; |
| 664 | } |
| 665 | case AspectRatioAlign::XMaxYMin: { |
| 666 | tvx -= width - tvw; |
| 667 | break; |
| 668 | } |
| 669 | case AspectRatioAlign::XMinYMid: { |
| 670 | tvy -= (height - tvh) * 0.5f; |
| 671 | break; |
| 672 | } |
| 673 | case AspectRatioAlign::XMidYMid: { |
| 674 | tvx -= (width - tvw) * 0.5f; |
| 675 | tvy -= (height - tvh) * 0.5f; |
| 676 | break; |
| 677 | } |
| 678 | case AspectRatioAlign::XMaxYMid: { |
| 679 | tvx -= width - tvw; |
| 680 | tvy -= (height - tvh) * 0.5f; |
| 681 | break; |
| 682 | } |
| 683 | case AspectRatioAlign::XMinYMax: { |
| 684 | tvy -= height - tvh; |
| 685 | break; |
| 686 | } |
| 687 | case AspectRatioAlign::XMidYMax: { |
| 688 | tvx -= (width - tvw) * 0.5f; |
| 689 | tvy -= height - tvh; |
| 690 | break; |
| 691 | } |
| 692 | case AspectRatioAlign::XMaxYMax: { |
| 693 | tvx -= width - tvw; |
| 694 | tvy -= height - tvh; |
| 695 | break; |
| 696 | } |
| 697 | default: { |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return {sx, 0, -tvx, 0, sy, -tvy, 0, 0, 1}; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | static unique_ptr<Scene> _useBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, int depth, bool* isMaskWhite) |
| 707 | { |
| 708 | auto scene = _sceneBuildHelper(loaderData, node, vBox, svgPath, false, depth + 1, isMaskWhite); |
| 709 | |
| 710 | // mUseTransform = mUseTransform * mTranslate |
| 711 | Matrix mUseTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 712 | if (node->transform) mUseTransform = *node->transform; |
| 713 | if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) { |
| 714 | Matrix mTranslate = {1, 0, node->node.use.x, 0, 1, node->node.use.y, 0, 0, 1}; |
| 715 | mUseTransform *= mTranslate; |
| 716 | } |
| 717 | |
| 718 | if (node->node.use.symbol) { |
| 719 | auto symbol = node->node.use.symbol->node.symbol; |
| 720 | |
| 721 | auto width = (symbol.hasWidth ? symbol.w : vBox.w); |
| 722 | if (node->node.use.isWidthSet) width = node->node.use.w; |
| 723 | auto height = (symbol.hasHeight ? symbol.h : vBox.h);; |
| 724 | if (node->node.use.isHeightSet) height = node->node.use.h; |
| 725 | auto vw = (symbol.hasViewBox ? symbol.vw : width); |
| 726 | auto vh = (symbol.hasViewBox ? symbol.vh : height); |
| 727 | |
| 728 | Matrix mViewBox = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 729 | if ((!tvg::equal(width, vw) || !tvg::equal(height, vh)) && vw > 0 && vh > 0) { |
| 730 | Box box = {symbol.vx, symbol.vy, vw, vh}; |
| 731 | mViewBox = _calculateAspectRatioMatrix(symbol.align, symbol.meetOrSlice, width, height, box); |
| 732 | } else if (!tvg::zero(symbol.vx) || !tvg::zero(symbol.vy)) { |
| 733 | mViewBox = {1, 0, -symbol.vx, 0, 1, -symbol.vy, 0, 0, 1}; |
| 734 | } |
| 735 | |
| 736 | // mSceneTransform = mUseTransform * mSymbolTransform * mViewBox |
| 737 | Matrix mSceneTransform = mViewBox; |
| 738 | if (node->node.use.symbol->transform) { |
| 739 | mSceneTransform = *node->node.use.symbol->transform * mViewBox; |
| 740 | } |
| 741 | mSceneTransform = mUseTransform * mSceneTransform; |
| 742 | scene->transform(mSceneTransform); |
| 743 | |
| 744 | if (!node->node.use.symbol->node.symbol.overflowVisible) { |
| 745 | auto viewBoxClip = Shape::gen(); |
| 746 | viewBoxClip->appendRect(0, 0, width, height, 0, 0); |
| 747 | |
| 748 | // mClipTransform = mUseTransform * mSymbolTransform |
| 749 | Matrix mClipTransform = mUseTransform; |
| 750 | if (node->node.use.symbol->transform) { |
| 751 | mClipTransform = mUseTransform * *node->node.use.symbol->transform; |
| 752 | } |
| 753 | viewBoxClip->transform(mClipTransform); |
| 754 | |
| 755 | auto clippingLayer = Scene::gen(); |
| 756 | clippingLayer->clip(std::move(viewBoxClip)); |
| 757 | clippingLayer->push(std::move(scene)); |
| 758 | return clippingLayer; |
| 759 | } |
| 760 | return scene; |
| 761 | } |
| 762 | |
| 763 | if (auto clipper = scene->Paint::pImpl->clipper) { |
| 764 | auto clipTransform = clipper->transform(); |
| 765 | Matrix inv; |
| 766 | if (node->transform && inverse(node->transform, &inv)) clipTransform = inv * clipTransform; |
| 767 | clipper->transform(mUseTransform * clipTransform); |
| 768 | } |
| 769 | |
| 770 | scene->transform(mUseTransform); |
| 771 | return scene; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | static void _applyTextFill(SvgStyleProperty* style, Text* text, const Box& vBox) |
| 776 | { |
| 777 | //If fill property is nullptr then do nothing |
| 778 | if (style->fill.paint.none) { |
| 779 | //Do nothing |
| 780 | } else if (style->fill.paint.gradient) { |
| 781 | Box bBox = vBox; |
| 782 | if (!style->fill.paint.gradient->userSpace) bBox = _boundingBox(text); |
| 783 | |
| 784 | if (style->fill.paint.gradient->type == SvgGradientType::Linear) { |
| 785 | auto linear = _applyLinearGradientProperty(style->fill.paint.gradient, bBox, style->fill.opacity); |
| 786 | text->fill(std::move(linear)); |
| 787 | } else if (style->fill.paint.gradient->type == SvgGradientType::Radial) { |
| 788 | auto radial = _applyRadialGradientProperty(style->fill.paint.gradient, bBox, style->fill.opacity); |
| 789 | text->fill(std::move(radial)); |
| 790 | } |
| 791 | } else if (style->fill.paint.url) { |
| 792 | //TODO: Apply the color pointed by url |
| 793 | TVGLOG("SVG", "The fill's url not supported."); |
| 794 | } else if (style->fill.paint.curColor) { |
| 795 | //Apply the current style color |
| 796 | text->fill(style->color.r, style->color.g, style->color.b); |
| 797 | text->opacity(style->fill.opacity); |
| 798 | } else { |
| 799 | //Apply the fill color |
| 800 | text->fill(style->fill.paint.color.r, style->fill.paint.color.g, style->fill.paint.color.b); |
| 801 | text->opacity(style->fill.opacity); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | |
| 806 | static unique_ptr<Text> _textBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath) |
| 807 | { |
| 808 | auto textNode = &node->node.text; |
| 809 | if (!textNode->text) return nullptr; |
| 810 | auto text = Text::gen(); |
| 811 | |
| 812 | Matrix textTransform = {1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 813 | if (node->transform) textTransform = *node->transform; |
| 814 | translateR(&textTransform, node->node.text.x, node->node.text.y - textNode->fontSize); |
| 815 | text->transform(textTransform); |
| 816 | |
| 817 | //TODO: handle def values of font and size as used in a system? |
| 818 | const float ptPerPx = 0.75f; //1 pt = 1/72; 1 in = 96 px; -> 72/96 = 0.75 |
| 819 | auto fontSizePt = textNode->fontSize * ptPerPx; |
| 820 | if (textNode->fontFamily) text->font(textNode->fontFamily, fontSizePt); |
| 821 | text->text(textNode->text); |
| 822 | |
| 823 | _applyTextFill(node->style, text.get(), vBox); |
| 824 | _applyComposition(loaderData, text.get(), node, vBox, svgPath); |
| 825 | |
| 826 | return text; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | static unique_ptr<Scene> _sceneBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, const Box& vBox, const string& svgPath, bool mask, int depth, bool* isMaskWhite) |
| 831 | { |
| 832 | /* Exception handling: Prevent invalid SVG data input. |
| 833 | The size is the arbitrary value, we need an experimental size. */ |
| 834 | if (depth > 2192) { |
| 835 | TVGERR("SVG", "Infinite recursive call - stopped after %d calls! Svg file may be incorrectly formatted.", depth); |
| 836 | return nullptr; |
| 837 | } |
| 838 | |
| 839 | if (_isGroupType(node->type) || mask) { |
| 840 | auto scene = Scene::gen(); |
| 841 | // For a Symbol node, the viewBox transformation has to be applied first - see _useBuildHelper() |
| 842 | if (!mask && node->transform && node->type != SvgNodeType::Symbol) scene->transform(*node->transform); |
| 843 | |
| 844 | if (node->style->display && node->style->opacity != 0) { |
| 845 | auto child = node->child.data; |
| 846 | for (uint32_t i = 0; i < node->child.count; ++i, ++child) { |
| 847 | if (_isGroupType((*child)->type)) { |
| 848 | if ((*child)->type == SvgNodeType::Use) |
| 849 | scene->push(_useBuildHelper(loaderData, *child, vBox, svgPath, depth + 1, isMaskWhite)); |
| 850 | else if (!((*child)->type == SvgNodeType::Symbol && node->type != SvgNodeType::Use)) |
| 851 | scene->push(_sceneBuildHelper(loaderData, *child, vBox, svgPath, false, depth + 1, isMaskWhite)); |
| 852 | } else if ((*child)->type == SvgNodeType::Image) { |
| 853 | auto image = _imageBuildHelper(loaderData, *child, vBox, svgPath); |
| 854 | if (image) { |
| 855 | scene->push(std::move(image)); |
| 856 | if (isMaskWhite) *isMaskWhite = false; |
| 857 | } |
| 858 | } else if ((*child)->type == SvgNodeType::Text) { |
| 859 | auto text = _textBuildHelper(loaderData, *child, vBox, svgPath); |
| 860 | if (text) scene->push(std::move(text)); |
| 861 | } else if ((*child)->type != SvgNodeType::Mask) { |
| 862 | auto shape = _shapeBuildHelper(loaderData, *child, vBox, svgPath); |
| 863 | if (shape) { |
| 864 | if (isMaskWhite) { |
| 865 | uint8_t r, g, b; |
| 866 | shape->fillColor(&r, &g, &b); |
| 867 | if (shape->fill() || r < 255 || g < 255 || b < 255 || shape->strokeFill() || |
| 868 | (shape->strokeColor(&r, &g, &b) == Result::Success && (r < 255 || g < 255 || b < 255))) { |
| 869 | *isMaskWhite = false; |
| 870 | } |
| 871 | } |
| 872 | scene->push(std::move(shape)); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | _applyComposition(loaderData, scene.get(), node, vBox, svgPath); |
| 877 | scene->opacity(node->style->opacity); |
| 878 | } |
| 879 | return scene; |
| 880 | } |
| 881 | return nullptr; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | static void _updateInvalidViewSize(const Scene* scene, Box& vBox, float& w, float& h, SvgViewFlag viewFlag) |
| 886 | { |
| 887 | bool validWidth = (viewFlag & SvgViewFlag::Width); |
| 888 | bool validHeight = (viewFlag & SvgViewFlag::Height); |
| 889 | |
| 890 | float x, y; |
| 891 | scene->bounds(&x, &y, &vBox.w, &vBox.h, false); |
| 892 | if (!validWidth && !validHeight) { |
| 893 | vBox.x = x; |
| 894 | vBox.y = y; |
| 895 | } else { |
| 896 | if (validWidth) vBox.w = w; |
| 897 | if (validHeight) vBox.h = h; |
| 898 | } |
| 899 | |
| 900 | //the size would have 1x1 or percentage values. |
| 901 | if (!validWidth) w *= vBox.w; |
| 902 | if (!validHeight) h *= vBox.h; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | static void _loadFonts(Array<FontFace>& fonts) |
| 907 | { |
| 908 | if (fonts.empty()) return; |
| 909 | |
| 910 | static constexpr struct { |
| 911 | const char* prefix; |
| 912 | size_t len; |
| 913 | } prefixes[] = { |
| 914 | {"data:font/ttf;base64,", sizeof("data:font/ttf;base64,") - 1}, |
| 915 | {"data:application/font-ttf;base64,", sizeof("data:application/font-ttf;base64,") - 1} |
| 916 | }; |
| 917 | |
| 918 | for (uint32_t i = 0; i < fonts.count; ++i) { |
| 919 | auto p = &fonts[i]; |
| 920 | if (!p->name) continue; |
| 921 | |
| 922 | size_t shift = 0; |
| 923 | for (const auto& prefix : prefixes) { |
| 924 | if (p->srcLen > prefix.len && !memcmp(p->src, prefix.prefix, prefix.len)) { |
| 925 | shift = prefix.len; |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | if (shift == 0) { |
| 930 | TVGLOG("SVG", "The embedded font \"%s\" data not loaded properly.", p->name); |
| 931 | continue; |
| 932 | } |
| 933 | |
| 934 | auto size = b64Decode(p->src + shift, p->srcLen - shift, &p->decoded); |
| 935 | |
| 936 | if (Text::load(p->name, p->decoded, size) != Result::Success) TVGERR("SVG", "Error while loading the ttf font named \"%s\".", p->name); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /************************************************************************/ |
| 941 | /* External Class Implementation */ |
| 942 | /************************************************************************/ |
| 943 | |
| 944 | Scene* svgSceneBuild(SvgLoaderData& loaderData, Box vBox, float w, float h, AspectRatioAlign align, AspectRatioMeetOrSlice meetOrSlice, const string& svgPath, SvgViewFlag viewFlag) |
| 945 | { |
| 946 | //TODO: aspect ratio is valid only if viewBox was set |
| 947 | |
| 948 | if (!loaderData.doc || (loaderData.doc->type != SvgNodeType::Doc)) return nullptr; |
| 949 | |
| 950 | _loadFonts(loaderData.fonts); |
| 951 | |
| 952 | auto docNode = _sceneBuildHelper(loaderData, loaderData.doc, vBox, svgPath, false, 0); |
| 953 | |
| 954 | if (!(viewFlag & SvgViewFlag::Viewbox)) _updateInvalidViewSize(docNode.get(), vBox, w, h, viewFlag); |
| 955 | |
| 956 | if (!tvg::equal(w, vBox.w) || !tvg::equal(h, vBox.h)) { |
| 957 | Matrix m = _calculateAspectRatioMatrix(align, meetOrSlice, w, h, vBox); |
| 958 | docNode->transform(m); |
| 959 | } else if (!tvg::zero(vBox.x) || !tvg::zero(vBox.y)) { |
| 960 | docNode->translate(-vBox.x, -vBox.y); |
| 961 | } |
| 962 | |
| 963 | auto viewBoxClip = Shape::gen(); |
| 964 | viewBoxClip->appendRect(0, 0, w, h); |
| 965 | |
| 966 | auto compositeLayer = Scene::gen(); |
| 967 | compositeLayer->clip(std::move(viewBoxClip)); |
| 968 | compositeLayer->push(std::move(docNode)); |
| 969 | |
| 970 | auto root = Scene::gen(); |
| 971 | root->push(std::move(compositeLayer)); |
| 972 | |
| 973 | loaderData.doc->node.doc.vx = vBox.x; |
| 974 | loaderData.doc->node.doc.vy = vBox.y; |
| 975 | loaderData.doc->node.doc.vw = vBox.w; |
| 976 | loaderData.doc->node.doc.vh = vBox.h; |
| 977 | loaderData.doc->node.doc.w = w; |
| 978 | loaderData.doc->node.doc.h = h; |
| 979 | |
| 980 | return root.release(); |
| 981 | } |