-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathSVGDocumentImpl.cpp
More file actions
1144 lines (1019 loc) · 41.9 KB
/
Copy pathSVGDocumentImpl.cpp
File metadata and controls
1144 lines (1019 loc) · 41.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2014 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
#include "SVGDocumentImpl.h"
#include "Constants.h"
#include "svgnative/SVGDocument.h"
#include "svgnative/SVGRenderer.h"
#include "SVGStringParser.h"
#include "xml/XMLParser.h"
#include <cmath>
#include <limits>
using namespace SVGNative::xml;
namespace SVGNative
{
constexpr std::array<const char*, 14> gInheritedPropertyNames{{
kColorProp,
kClipRuleProp,
kFillProp,
kFillRuleProp,
kFillOpacityProp,
kStrokeProp,
kStrokeDasharrayProp,
kStrokeDashoffsetProp,
kStrokeLinecapProp,
kStrokeLinejoinProp,
kStrokeMiterlimitProp,
kStrokeOpacityProp,
kStrokeWidthProp,
kVisibilityProp
}};
constexpr std::array<const char*, 5> gNonInheritedPropertyNames{{
kClipPathProp,
kDisplayProp,
kOpacityProp,
kStopOpacityProp,
kStopColorProp
}};
template <typename T>
bool isCloseToZero(T x)
{
return std::abs(x) < std::numeric_limits<T>::epsilon();
}
SVGDocumentImpl::SVGDocumentImpl(std::shared_ptr<SVGRenderer> renderer)
: mViewBox{{0, 0, 320.0f, 200.0f}}
, mRenderer{renderer}
{
mFillStyleStack.push(FillStyleImpl());
mStrokeStyleStack.push(StrokeStyleImpl());
GraphicStyleImpl graphicStyle{};
std::set<std::string> classNames;
mGroup = std::make_shared<Group>(graphicStyle, classNames);
mGroupStack.push(mGroup);
}
void SVGDocumentImpl::TraverseSVGTree(XMLNode* rootNode)
{
if (!rootNode || strcmp(rootNode->GetName(), kSvgElem))
return;
mRootNode = rootNode;
auto viewBoxAttr = rootNode->GetAttribute(kViewBoxAttr);
if (!viewBoxAttr.found)
{
mViewBox[0] = SVGDocumentImpl::ParseLengthFromAttr(rootNode, kXAttr, LengthType::kHorizontal, mViewBox[0]);
mViewBox[1] = SVGDocumentImpl::ParseLengthFromAttr(rootNode, kYAttr, LengthType::kVertical, mViewBox[1]);
mViewBox[2] = SVGDocumentImpl::ParseLengthFromAttr(rootNode, kWidthAttr, LengthType::kHorizontal, mViewBox[2]);
mViewBox[3] = SVGDocumentImpl::ParseLengthFromAttr(rootNode, kHeightAttr, LengthType::kVertical, mViewBox[3]);
}
else
{
std::vector<float> numberList;
if (SVGStringParser::ParseListOfNumbers(viewBoxAttr.value, numberList) && numberList.size() == 4)
mViewBox = {{numberList[0], numberList[1], numberList[2], numberList[3]}};
}
#if DEBUG
auto dataNameAttr = rootNode->GetAttribute(kDataNameAttr);
if (dataNameAttr.found)
mTitle = dataNameAttr.value;
#endif
ParseChild(rootNode);
// Clear all temporary sets
mGradients.clear();
mClippingPaths.clear();
mRootNode = nullptr;
}
float SVGDocumentImpl::RelativeLength(LengthType lengthType) const
{
float relLength{};
switch (lengthType)
{
case LengthType::kHorizontal:
relLength = mViewBox[2];
break;
case LengthType::kVertical:
relLength = mViewBox[3];
break;
case LengthType::kDiagonal:
relLength = sqrtf(mViewBox[2] * mViewBox[2] + mViewBox[3] * mViewBox[3]);
break;
default:
break;
}
return relLength;
}
float SVGDocumentImpl::ParseLengthFromAttr(const XMLNode* node, const char* attrName, LengthType lengthType, float fallback)
{
if (!node)
return fallback;
float number{};
auto attr = node->GetAttribute(attrName);
if (!attr.found || !SVGStringParser::ParseLengthOrPercentage(attr.value, RelativeLength(lengthType), number, true))
return fallback;
return number;
}
void SVGDocumentImpl::ParseChildren(XMLNode* node)
{
SVG_ASSERT(node != nullptr);
for (auto child = node->GetFirstNode(); child != nullptr; child = child->GetNextSibling())
{
ParseChild(child.get());
}
}
void SVGDocumentImpl::ParseChild(XMLNode* child)
{
SVG_ASSERT(child != nullptr);
auto fillStyle = mFillStyleStack.top();
auto strokeStyle = mStrokeStyleStack.top();
std::set<std::string> classNames;
auto graphicStyle = ParseGraphic(child, fillStyle, strokeStyle, classNames);
std::string idString;
auto idAttr = child->GetAttribute(kIdAttr);
if (idAttr.found)
idString = idAttr.value;
// Check if we have a shape rect, circle, ellipse, line, polygon, polyline
// or path first.
if (auto path = ParseShape(child))
{
AddChildToCurrentGroup(std::unique_ptr<Graphic>(new Graphic(graphicStyle, classNames, fillStyle, strokeStyle, std::move(path))), std::move(idString));
return;
}
// Look at all elements that are no shapes.
const auto elementName = child->GetName();
if (!strcmp(elementName, kGElem) || (!strcmp(elementName, kSvgElem) && child == mRootNode))
{
mFillStyleStack.push(fillStyle);
mStrokeStyleStack.push(strokeStyle);
auto group = std::make_shared<Group>(graphicStyle, classNames);
AddChildToCurrentGroup(group, std::move(idString));
mGroupStack.push(group);
ParseChildren(child);
mGroupStack.pop();
mFillStyleStack.pop();
mStrokeStyleStack.pop();
}
else if (!strcmp(elementName, kDefsElem))
{
mFillStyleStack.push(fillStyle);
mStrokeStyleStack.push(strokeStyle);
// Create dummmy group. All children w/o id will get cleaned up.
mGroupStack.push(std::make_shared<Group>(graphicStyle, classNames));
ParseChildren(child);
mGroupStack.pop();
mFillStyleStack.pop();
mStrokeStyleStack.pop();
}
else if (!strcmp(elementName, kImageElem))
{
std::unique_ptr<ImageData> imageData;
auto hrefAttr = child->GetAttribute(kHrefAttr, kXlinkNS);
if (hrefAttr.found)
{
const std::string dataURL = hrefAttr.value;
ImageEncoding encoding{};
unsigned short base64Offset{22};
if (dataURL.find(kDataUrlPngVal) == 0)
encoding = ImageEncoding::kPNG;
else if (dataURL.find(kDataUrlJpgVal) == 0)
encoding = ImageEncoding::kJPEG;
else if (dataURL.find(kDataUrlJpegVal) == 0)
{
encoding = ImageEncoding::kJPEG;
base64Offset = 23;
}
else
return;
imageData = mRenderer->CreateImageData(dataURL.substr(base64Offset), encoding);
}
if (imageData)
{
const float imageWidth = imageData->Width();
const float imageHeight = imageData->Height();
Rect clipArea{ParseLengthFromAttr(child, kXAttr, LengthType::kHorizontal),
ParseLengthFromAttr(child, kYAttr, LengthType::kVertical),
ParseLengthFromAttr(child, kWidthAttr, LengthType::kHorizontal, imageWidth),
ParseLengthFromAttr(child, kHeightAttr, LengthType::kVertical, imageHeight)};
std::string align;
std::string meetOrSlice;
std::vector<std::string> attrStringValues;
auto preserveAspectRatioAttr = child->GetAttribute(kPreserveAspectRatioAttr);
if (preserveAspectRatioAttr.found
&& SVGStringParser::ParseListOfStrings(preserveAspectRatioAttr.value, attrStringValues)
&& attrStringValues.size() >= 1 && attrStringValues.size() <= 2)
{
align = attrStringValues[0];
if (attrStringValues.size() == 2)
meetOrSlice = attrStringValues[1];
}
Rect fillArea{clipArea};
if (align.compare(kNoneVal) != 0)
{
fillArea.width = imageWidth;
fillArea.height = imageHeight;
float scaleX = clipArea.width / imageWidth;
float scaleY = clipArea.height / imageHeight;
float scale{};
if (meetOrSlice.compare(kSliceVal) == 0)
scale = std::max(scaleX, scaleY);
else
scale = std::min(scaleX, scaleY);
fillArea.width *= scale;
fillArea.height *= scale;
if (align.compare(kXMinYMinVal) == 0)
{
fillArea.x = clipArea.x;
fillArea.y = clipArea.y;
}
else if (align.compare(kXMidYMinVal) == 0)
{
fillArea.x = (clipArea.x + clipArea.width / 2) - fillArea.width / 2;
fillArea.y = clipArea.y;
}
else if (align.compare(kXMaxYMinVal) == 0)
{
fillArea.x = clipArea.x + clipArea.width - fillArea.width;
fillArea.y = clipArea.y;
}
else if (align.compare(kXMinYMidVal) == 0)
{
fillArea.x = clipArea.x;
fillArea.y = (clipArea.y + clipArea.height / 2) - fillArea.height / 2;
}
else if (align.compare(kXMaxYMidVal) == 0)
{
fillArea.x = clipArea.x + clipArea.width - fillArea.width;
fillArea.y = (clipArea.y + clipArea.height / 2) - fillArea.height / 2;
}
else if (align.compare(kXMinYMaxVal) == 0)
{
fillArea.x = clipArea.x;
fillArea.y = clipArea.y + clipArea.height - fillArea.height;
}
else if (align.compare(kXMidYMaxVal) == 0)
{
fillArea.x = (clipArea.x + clipArea.width / 2) - fillArea.width / 2;
fillArea.y = clipArea.y + clipArea.height - fillArea.height;
}
else if (align.compare(kXMaxYMaxVal) == 0)
{
fillArea.x = clipArea.x + clipArea.width - fillArea.width;
fillArea.y = clipArea.y + clipArea.height - fillArea.height;
}
else // default and "xMidYMid"
{
fillArea.x = (clipArea.x + clipArea.width / 2) - fillArea.width / 2;
fillArea.y = (clipArea.y + clipArea.height / 2) - fillArea.height / 2;
}
}
// Do not render 0-sized elements.
if (imageWidth && imageHeight && clipArea.width && clipArea.height && fillArea.width && fillArea.height)
{
auto image = std::unique_ptr<Image>(new Image(graphicStyle, classNames, std::move(imageData), clipArea, fillArea));
AddChildToCurrentGroup(std::move(image), std::move(idString));
}
}
}
else if (!strcmp(elementName, kUseElem))
{
auto hrefAttr = child->GetAttribute(kHrefAttr, kXlinkNS);
if (!hrefAttr.found || !hrefAttr.value || hrefAttr.value[0] != '#')
return;
const float x = ParseLengthFromAttr(child, kXAttr, LengthType::kHorizontal);
const float y = ParseLengthFromAttr(child, kYAttr, LengthType::kVertical);
if (!isCloseToZero(x) || !isCloseToZero(y))
{
if (!graphicStyle.transform)
graphicStyle.transform = mRenderer->CreateTransform();
graphicStyle.transform->Concat(1, 0, 0, 1, x, y);
}
std::string href{(hrefAttr.value + 1)};
AddChildToCurrentGroup(std::make_shared<Reference>(graphicStyle, classNames, fillStyle, strokeStyle, std::move(href)), std::move(idString));
}
else if (!strcmp(elementName, kSymbolElem))
{
// FIXME: Do not render <symbol> outside of <defs> section.
// FIXME: Remove support for symbol ASAP.
auto attr = child->GetAttribute(kViewBoxAttr);
if (attr.found)
{
std::vector<float> numberList;
if (SVGStringParser::ParseListOfNumbers(attr.value, numberList) && numberList.size() == 4)
graphicStyle.transform = mRenderer->CreateTransform(1, 0, 0, 1, -numberList[0], -numberList[1]);
}
auto group = std::make_shared<Group>(graphicStyle, classNames);
AddChildToCurrentGroup(group, std::move(idString));
mGroupStack.push(group);
ParseChildren(child);
mGroupStack.pop();
}
else if (!strcmp(elementName, kLinearGradientElem) || !strcmp(elementName, kRadialGradientElem))
{
mFillStyleStack.push(fillStyle);
mStrokeStyleStack.push(strokeStyle);
ParseGradient(child);
mFillStyleStack.pop();
mStrokeStyleStack.pop();
}
#ifdef STYLE_SUPPORT
else if (!strcmp(elementName, kStyleElem))
ParseStyle(child);
#endif
else if (!strcmp(elementName, kClipPathElem))
{
auto id = child->GetAttribute(kIdAttr);
if (!id.found)
return;
mFillStyleStack.push(fillStyle);
mStrokeStyleStack.push(strokeStyle);
// SVG only allows shapes (and <use> elements referencing shapes) as children of
// <clipPath>. Ignore all other elements.
bool hasClipContent{false};
for (auto clipPathChild = child->GetFirstNode(); clipPathChild != nullptr; clipPathChild = clipPathChild->GetNextSibling())
{
// WebKit and Blink allow the clipping path if there is at least one valid basic shape child.
if (auto path = ParseShape(clipPathChild.get()))
{
std::unique_ptr<Transform> transform;
auto attr = clipPathChild->GetAttribute(kTransformAttr);
if (attr.found)
{
SVG_ASSERT(mRenderer != nullptr);
transform = mRenderer->CreateTransform();
if (!SVGStringParser::ParseTransform(attr.value, *transform))
transform.reset();
}
auto fillStyleChild = mFillStyleStack.top();
auto strokeStyleChild = mStrokeStyleStack.top();
std::set<std::string> classNames;
ParseGraphic(child, fillStyleChild, strokeStyleChild, classNames);
mClippingPaths[id.value] = std::make_shared<ClippingPath>(true, fillStyleChild.clipRule, std::move(path), std::move(transform));
hasClipContent = true;
break;
}
}
if (!hasClipContent)
mClippingPaths[id.value] = std::make_shared<ClippingPath>(false, WindingRule::kNonZero, nullptr, nullptr);
mFillStyleStack.pop();
mStrokeStyleStack.pop();
}
}
std::unique_ptr<Path> SVGDocumentImpl::ParseShape(XMLNode* child)
{
SVG_ASSERT(child != nullptr);
const auto elementName = child->GetName();
if (!strcmp(elementName, kRectElem))
{
float x = ParseLengthFromAttr(child, kXAttr, LengthType::kHorizontal);
float y = ParseLengthFromAttr(child, kYAttr, LengthType::kVertical);
float width = ParseLengthFromAttr(child, kWidthAttr, LengthType::kHorizontal);
float height = ParseLengthFromAttr(child, kHeightAttr, LengthType::kVertical);
// SVG requires to disable rendering if width or height are 0.
if (isCloseToZero(width) || isCloseToZero(height))
return nullptr;
auto rxAttr = child->GetAttribute(kRxAttr);
auto ryAttr = child->GetAttribute(kRyAttr);
float rx{};
float ry{};
if (rxAttr.found && ryAttr.found)
{
rx = ParseLengthFromAttr(child, kRxAttr, LengthType::kHorizontal);
ry = ParseLengthFromAttr(child, kRyAttr, LengthType::kVertical);
}
else if (rxAttr.found)
{
// the svg spec says that rect elements that specify a rx but not a ry
// should use the rx value for ry
rx = ParseLengthFromAttr(child, kRxAttr, LengthType::kHorizontal);
ry = rx;
}
else if (ryAttr.found)
{
// the svg spec says that rect elements that specify a ry but not a rx
// should use the ry value for rx
ry = ParseLengthFromAttr(child, kRyAttr, LengthType::kVertical);
rx = ry;
}
else
{
rx = 0;
ry = 0;
}
rx = std::min(rx, width / 2.0f);
ry = std::min(ry, height / 2.0f);
auto path = mRenderer->CreatePath();
if (isCloseToZero(rx) || isCloseToZero(ry))
{
path->Rect(x, y, width, height);
}
else
{
path->RoundedRect(x, y, width, height, rx, ry);
}
return path;
}
else if (!strcmp(elementName, kEllipseElem) || !strcmp(elementName, kCircleElem))
{
float rx{}, ry{};
if (!strcmp(elementName, kEllipseElem))
{
rx = ParseLengthFromAttr(child, kRxAttr, LengthType::kHorizontal);
ry = ParseLengthFromAttr(child, kRyAttr, LengthType::kVertical);
}
else
{
rx = ParseLengthFromAttr(child, kRAttr, LengthType::kDiagonal);
ry = rx;
}
// SVG requires to disable rendering if rx or ry are 0.
if (isCloseToZero(rx) || isCloseToZero(ry))
return nullptr;
float cx = ParseLengthFromAttr(child, kCxAttr, LengthType::kHorizontal);
float cy = ParseLengthFromAttr(child, kCyAttr, LengthType::kVertical);
auto path = mRenderer->CreatePath();
path->Ellipse(cx, cy, rx, ry);
return path;
}
else if (!strcmp(elementName, kPolygonElem) || !strcmp(elementName, kPolylineElem))
{
auto attr = child->GetAttribute(kPointsAttr);
if (!attr.found)
return nullptr;
// This does not follow the spec which requires at least one space or comma between
// coordinate pairs. However, Blink and WebKit do it the same way.
std::vector<float> numberList;
SVGStringParser::ParseListOfNumbers(attr.value, numberList);
auto size = numberList.size();
auto path = mRenderer->CreatePath();
if (size > 1)
{
if (size % 2 == 1)
--size;
decltype(size) i{};
path->MoveTo(numberList[i], numberList[i + 1]);
i += 2;
for (; i < size; i += 2)
path->LineTo(numberList[i], numberList[i + 1]);
if (!strcmp(elementName, kPolygonElem))
path->ClosePath();
}
return path;
}
else if (!strcmp(elementName, kPathElem))
{
auto attr = child->GetAttribute(kDAttr);
if (!attr.found)
return nullptr;
auto path = mRenderer->CreatePath();
SVGStringParser::ParsePathString(attr.value, *path);
return path;
}
else if (!strcmp(elementName, kLineElem))
{
auto path = mRenderer->CreatePath();
path->MoveTo(ParseLengthFromAttr(child, kX1Attr, LengthType::kHorizontal), ParseLengthFromAttr(child, kY1Attr, LengthType::kVertical));
path->LineTo(ParseLengthFromAttr(child, kX2Attr, LengthType::kHorizontal), ParseLengthFromAttr(child, kY2Attr, LengthType::kVertical));
return path;
}
return nullptr;
}
GraphicStyleImpl SVGDocumentImpl::ParseGraphic(
const XMLNode* node, FillStyleImpl& fillStyle, StrokeStyleImpl& strokeStyle, std::set<std::string>& classNames)
{
SVG_ASSERT(node != nullptr);
std::vector<PropertySet> propertySets;
propertySets.push_back(ParsePresentationAttributes(node));
ParseStyleAttr(node, propertySets, classNames);
GraphicStyleImpl graphicStyle{};
for (const auto& propertySet : propertySets)
{
ParseGraphicsProperties(graphicStyle, propertySet);
ParseFillProperties(fillStyle, propertySet);
ParseStrokeProperties(strokeStyle, propertySet);
}
auto transformAttr = node->GetAttribute(kTransformAttr);
if (transformAttr.found && node != mRootNode) // Ignore transforms on root SVG node
{
SVG_ASSERT(mRenderer != nullptr);
graphicStyle.transform = mRenderer->CreateTransform();
if (!SVGStringParser::ParseTransform(transformAttr.value, *graphicStyle.transform))
graphicStyle.transform.reset();
}
return graphicStyle;
}
static inline void AddDetectedProperty(const XMLNode* node, PropertySet& propertySet, const char* propertyName)
{
auto attr = node->GetAttribute(propertyName);
if (attr.found)
propertySet.insert({propertyName, attr.value});
}
PropertySet SVGDocumentImpl::ParsePresentationAttributes(const XMLNode* node)
{
SVG_ASSERT(node != nullptr);
PropertySet propertySet;
for (const auto& propertyName : gInheritedPropertyNames)
AddDetectedProperty(node, propertySet, propertyName);
for (const auto& propertyName : gNonInheritedPropertyNames)
AddDetectedProperty(node, propertySet, propertyName);
return propertySet;
}
void SVGDocumentImpl::ParseFillProperties(FillStyleImpl& fillStyle, const PropertySet& propertySet)
{
auto prop = propertySet.find(kFillProp);
auto iterEnd = propertySet.end();
if (prop != iterEnd)
{
auto result = SVGStringParser::ParsePaint(prop->second, mGradients, mViewBox, fillStyle.internalPaint);
if (result == SVGDocumentImpl::Result::kDisabled)
fillStyle.hasFill = false;
else if (result == SVGDocumentImpl::Result::kSuccess)
fillStyle.hasFill = true;
}
prop = propertySet.find(kFillOpacityProp);
if (prop != iterEnd)
{
float opacity{};
if (SVGStringParser::ParseNumber(prop->second, opacity))
fillStyle.fillOpacity = std::max<float>(0.0, std::min<float>(1.0, opacity));
}
prop = propertySet.find(kFillRuleProp);
if (prop != iterEnd)
{
if (prop->second == kEvenoddVal)
fillStyle.fillRule = WindingRule::kEvenOdd;
else if (prop->second == kNonzeroVal)
fillStyle.fillRule = WindingRule::kNonZero;
}
// Other inherited properties
prop = propertySet.find(kColorProp);
if (prop != iterEnd)
{
ColorImpl color = Color{{0.0f, 0.0f, 0.0f, 1.0f}};
auto result = SVGStringParser::ParseColor(prop->second, color, false);
if (result == SVGDocumentImpl::Result::kSuccess)
fillStyle.color = color;
}
prop = propertySet.find(kVisibilityProp);
if (prop != iterEnd)
{
const auto& visibilityString = prop->second;
if (visibilityString == kHiddenVal)
fillStyle.visibility = false;
else if (visibilityString == kCollapseVal || visibilityString == kVisibleVal)
fillStyle.visibility = true;
}
prop = propertySet.find(kClipRuleProp);
if (prop != iterEnd)
{
if (prop->second == kEvenoddVal)
fillStyle.clipRule = WindingRule::kEvenOdd;
else if (prop->second == kNonzeroVal)
fillStyle.clipRule = WindingRule::kNonZero;
}
}
void SVGDocumentImpl::ParseStrokeProperties(StrokeStyleImpl& strokeStyle, const PropertySet& propertySet)
{
auto prop = propertySet.find(kStrokeProp);
auto iterEnd = propertySet.end();
if (prop != iterEnd)
{
auto result = SVGStringParser::ParsePaint(prop->second, mGradients, mViewBox, strokeStyle.internalPaint);
if (result == SVGDocumentImpl::Result::kDisabled)
strokeStyle.hasStroke = false;
else if (result == SVGDocumentImpl::Result::kSuccess)
strokeStyle.hasStroke = true;
}
prop = propertySet.find(kStrokeWidthProp);
if (prop != iterEnd)
{
float strokeWidth{};
// Ignore stroke-width if invalid or negative.
if (SVGStringParser::ParseLengthOrPercentage(prop->second, RelativeLength(LengthType::kDiagonal), strokeWidth, true)
&& strokeWidth >= 0)
strokeStyle.lineWidth = strokeWidth;
// Disable stroke on a stroke-width of 0.
if (strokeWidth == 0.0)
strokeStyle.hasStroke = false;
}
prop = propertySet.find(kStrokeLinecapProp);
if (prop != iterEnd)
{
if (prop->second == kRoundVal)
strokeStyle.lineCap = LineCap::kRound;
else if (prop->second == kSquareVal)
strokeStyle.lineCap = LineCap::kSquare;
}
prop = propertySet.find(kStrokeLinejoinProp);
if (prop != iterEnd)
{
if (prop->second == kRoundVal)
strokeStyle.lineJoin = LineJoin::kRound;
else if (prop->second == kBevelVal)
strokeStyle.lineJoin = LineJoin::kBevel;
}
prop = propertySet.find(kStrokeMiterlimitProp);
if (prop != iterEnd)
{
float miter{};
// Miter must be bigger 1. Otherwise ignore.
if (SVGStringParser::ParseNumber(prop->second, miter) && miter >= 1)
strokeStyle.miterLimit = miter;
}
prop = propertySet.find(kStrokeDashoffsetProp);
if (prop != iterEnd)
{
float strokeDashoffset{};
if (SVGStringParser::ParseLengthOrPercentage(prop->second, RelativeLength(LengthType::kDiagonal), strokeDashoffset, true))
strokeStyle.dashOffset = strokeDashoffset;
}
prop = propertySet.find(kStrokeDasharrayProp);
if (prop != iterEnd)
{
float diagonal = sqrtf(mViewBox[2] * mViewBox[2] + mViewBox[3] * mViewBox[3]);
if (!SVGStringParser::ParseListOfLengthOrPercentage(prop->second.c_str(), diagonal, strokeStyle.dashArray, true))
strokeStyle.dashArray.clear();
for (auto it = strokeStyle.dashArray.begin(); it < strokeStyle.dashArray.end(); ++it)
{
if (*it < 0)
{
strokeStyle.dashArray.clear();
break;
}
}
const auto sizeOfDashArray = strokeStyle.dashArray.size();
if (sizeOfDashArray % 2 != 0)
{
// If stroke-dasharray[] is odd-sized, the array should be twiced.
// See SVG 1.1 (2nd ed), 11.4 "Stroke Properties" for detail.
strokeStyle.dashArray.reserve(sizeOfDashArray * 2);
for (size_t i = 0; i < sizeOfDashArray; ++i)
strokeStyle.dashArray.push_back(strokeStyle.dashArray[i]);
}
}
prop = propertySet.find(kStrokeOpacityProp);
if (prop != iterEnd)
{
float opacity{};
if (SVGStringParser::ParseNumber(prop->second, opacity))
strokeStyle.strokeOpacity = std::max<float>(0.0, std::min<float>(1.0, opacity));
}
}
void SVGDocumentImpl::ParseGraphicsProperties(GraphicStyleImpl& graphicStyle, const PropertySet& propertySet)
{
auto prop = propertySet.find(kOpacityProp);
auto iterEnd = propertySet.end();
if (prop != iterEnd)
{
float opacity{};
if (SVGStringParser::ParseNumber(prop->second, opacity))
graphicStyle.opacity = std::max<float>(0.0, std::min<float>(1.0, opacity));
}
prop = propertySet.find(kClipPathProp);
if (prop != iterEnd)
{
// FIXME: Use proper parser.
const auto urlLength = strlen(kUrlVal);
const auto& valueString = prop->second;
auto id = valueString.substr(urlLength, valueString.size() - urlLength - 1);
auto clippingPathIt = mClippingPaths.find(id);
if (clippingPathIt != mClippingPaths.end())
graphicStyle.clippingPath = clippingPathIt->second;
}
prop = propertySet.find(kDisplayProp);
if (prop != iterEnd)
{
if (prop->second.compare(kNoneVal))
graphicStyle.display = false;
}
prop = propertySet.find(kStopOpacityProp);
if (prop != iterEnd)
{
float opacity{};
if (SVGStringParser::ParseNumber(prop->second, opacity))
graphicStyle.stopOpacity = std::max<float>(0.0, std::min<float>(1.0, opacity));
}
prop = propertySet.find(kStopColorProp);
if (prop != iterEnd)
{
ColorImpl color = Color{{0.0f, 0.0f, 0.0f, 1.0f}};
const auto result = SVGStringParser::ParseColor(prop->second, color, true);
if (result == SVGDocumentImpl::Result::kSuccess)
graphicStyle.stopColor = color;
}
}
float SVGDocumentImpl::ParseColorStop(const XMLNode* node, std::vector<ColorStopImpl>& colorStops, float lastOffset)
{
SVG_ASSERT(node != nullptr);
auto fillStyle = mFillStyleStack.top();
auto strokeStyle = mStrokeStyleStack.top();
std::set<std::string> classNames;
auto graphicStyle = ParseGraphic(node, fillStyle, strokeStyle, classNames);
// * New stops may never appear before previous stops. Use offset of previous stop otherwise.
// * Stops must be in the range [0.0, 1.0].
float offset{};
auto attr = node->GetAttribute(kOffsetAttr);
offset = (attr.found && SVGStringParser::ParseNumber(attr.value, offset)) ? offset : lastOffset;
offset = std::max<float>(lastOffset, offset);
offset = std::min<float>(1.0, std::max<float>(0.0, offset));
ColorImpl& paint = graphicStyle.stopColor;
if (paint.type() == typeid(ColorKeys))
{
// Value is "currentColor". Simply set value to CSS color property.
paint = fillStyle.color;
}
graphicStyle.stopOpacity = std::max<float>(0.0, std::min<float>(1.0, graphicStyle.stopOpacity));
colorStops.push_back(std::make_tuple(offset, paint, graphicStyle.stopOpacity));
return offset;
}
void SVGDocumentImpl::ParseColorStops(XMLNode* node, GradientImpl& gradient)
{
SVG_ASSERT(node != nullptr);
float lastOffset{};
std::vector<ColorStopImpl> colorStops;
for (auto child = node->GetFirstNode(); child != nullptr; child = child->GetNextSibling())
{
if (!strcmp(child->GetName(), kStopElem))
lastOffset = ParseColorStop(child.get(), colorStops, lastOffset);
}
// Make sure we always have stops in the range 0% and 100%.
if (colorStops.size() > 1)
{
const auto& firstStop = colorStops.front();
if (std::get<0>(firstStop) != 0.0f)
colorStops.insert(colorStops.begin(), std::make_tuple(0.0f, std::get<1>(firstStop), std::get<2>(firstStop)));
const auto& lastStop = colorStops.back();
if (std::get<0>(lastStop) != 1.0f)
colorStops.push_back(std::make_tuple(1.0f, std::get<1>(lastStop), std::get<2>(lastStop)));
}
// Keep the color stops from referenced gradients if the current gradient
// has none.
if (!colorStops.empty())
gradient.internalColorStops = colorStops;
}
void SVGDocumentImpl::ParseGradient(XMLNode* node)
{
SVG_ASSERT(node != nullptr);
GradientImpl gradient{};
// SVG allows referencing other gradients. For now, we only look at already parsed
// gradients. Since we add the current gradient after successful parsing,
// this also avoids circular references.
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementHrefAttribute
auto attr = node->GetAttribute(kHrefAttr, kXlinkNS);
if (attr.found)
{
std::string href{attr.value};
// href starts with a #, ignore it.
auto it = mGradients.find(href.substr(1));
if (it != mGradients.end())
gradient = it->second;
}
ParseColorStops(node, gradient);
const auto elementName = node->GetName();
if (!strcmp(elementName, kLinearGradientElem))
gradient.type = GradientType::kLinearGradient;
else if (!strcmp(elementName, kRadialGradientElem))
gradient.type = GradientType::kRadialGradient;
else
{
SVG_ASSERT_MSG(false, "Gradient parser called with invalid element");
return;
}
// TODO: Do we want to support `gradientUnits="objectBoundingBox"` at all?
// This would require us to get the bounding box of the filled/stroked shape
// when the gradient gets applied.
if (gradient.type == GradientType::kLinearGradient)
{
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradients
gradient.x1 = ParseLengthFromAttr(node, kX1Attr, LengthType::kHorizontal, gradient.x1);
gradient.y1 = ParseLengthFromAttr(node, kY1Attr, LengthType::kVertical, gradient.y1);
gradient.x2 = ParseLengthFromAttr(node, kX2Attr, LengthType::kHorizontal, gradient.x2);
gradient.y2 = ParseLengthFromAttr(node, kY2Attr, LengthType::kVertical, gradient.y2);
}
else
{
// https://www.w3.org/TR/SVG11/pservers.html#RadialGradients
gradient.cx = ParseLengthFromAttr(node, kCxAttr, LengthType::kHorizontal, gradient.cx);
gradient.cy = ParseLengthFromAttr(node, kCyAttr, LengthType::kVertical, gradient.cy);
gradient.fx = ParseLengthFromAttr(node, kFxAttr, LengthType::kHorizontal, gradient.fx);
gradient.fy = ParseLengthFromAttr(node, kFyAttr, LengthType::kVertical, gradient.fy);
gradient.r = ParseLengthFromAttr(node, kRAttr, LengthType::kDiagonal, gradient.r);
}
attr = node->GetAttribute(kSpreadMethodAttr);
if (attr.found)
{
const auto spreadMethodString = attr.value;
if (!strcmp(spreadMethodString, kPadVal))
gradient.method = SpreadMethod::kPad;
else if (!strcmp(spreadMethodString, kReflectVal))
gradient.method = SpreadMethod::kReflect;
else if (!strcmp(spreadMethodString, kRepeatVal))
gradient.method = SpreadMethod::kRepeat;
}
attr = node->GetAttribute(kGradientTransformAttr);
if (attr.found)
{
SVG_ASSERT(mRenderer != nullptr);
gradient.transform = mRenderer->CreateTransform();
if (!SVGStringParser::ParseTransform(attr.value, *gradient.transform))
gradient.transform.reset();
}
attr = node->GetAttribute(kIdAttr);
if (attr.found)
mGradients.insert({attr.value, gradient});
}
void SVGDocumentImpl::Render(const ColorMap& colorMap, float width, float height)
{
SVG_ASSERT(mGroup);
if (!mGroup)
return;
RenderElement(*mGroup, colorMap, width, height);
}
void SVGDocumentImpl::Render(const char* id, const ColorMap& colorMap, float width, float height)
{
// Referenced glyph identifiers shall be rendered as if they were contained in a <defs> section under
// the root SVG element:
// Therefore, the referenced shape/group should:
// * inherit property values from the root SVG element,
// * ignore all styling and transforms on ancestors.
// https://docs.microsoft.com/en-us/typography/opentype/spec/svg#glyph-identifiers
auto elementIter = mIdToElementMap.find(id);
if (elementIter != mIdToElementMap.end())
RenderElement(*elementIter->second, colorMap, width, height);
}
void SVGDocumentImpl::RenderElement(const Element& element, const ColorMap& colorMap, float width, float height)
{
float scale = width / mViewBox[2];
if (scale > height / mViewBox[3])
scale = height / mViewBox[3];
GraphicStyleImpl graphicStyle{};
graphicStyle.transform = mRenderer->CreateTransform();
graphicStyle.transform->Translate(-1 * mViewBox[0], -1 * mViewBox[1]);
graphicStyle.transform->Scale(scale, scale);
auto saveRestore = SaveRestoreHelper{mRenderer, graphicStyle};
TraverseTree(colorMap, element);
SVG_ASSERT(mVisitedElements.empty());
}
void SVGDocumentImpl::AddChildToCurrentGroup(std::shared_ptr<Element> element, std::string idString)
{
SVG_ASSERT(!mGroupStack.empty());
if (mGroupStack.empty())
return;
mGroupStack.top()->children.push_back(element);
if (!idString.empty() && mIdToElementMap.find(idString) == mIdToElementMap.end())
mIdToElementMap.emplace(std::move(idString), element);
}
static void ResolveColorImpl(const ColorMap& colorMap, const ColorImpl& colorImpl, Color& color)
{
if (colorImpl.type() == typeid(Variable))
{
const auto& var = boost::get<Variable>(colorImpl);
const auto colorIt = colorMap.find(var.first);
color = colorIt != colorMap.end() ? colorIt->second : var.second;
}
else if (colorImpl.type() == typeid(Color))
color = boost::get<Color>(colorImpl);
else
// Can only be reached if fallback color value of var() is currentColor.
color = Color{{0.0f, 0.0f, 0.0f, 1.0f}};