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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file 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 .
*/
#include <svgtools.hxx>
#include <sal/log.hxx>
#include <tools/color.hxx>
#include <rtl/math.hxx>
#include <o3tl/string_view.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <svgtoken.hxx>
#include <unordered_map>
namespace svgio::svgreader
{
basegfx::B2DHomMatrix SvgAspectRatio::createLinearMapping(const basegfx::B2DRange& rTarget, const basegfx::B2DRange& rSource)
{
basegfx::B2DHomMatrix aRetval;
const double fSWidth(rSource.getWidth());
const double fSHeight(rSource.getHeight());
const bool bNoSWidth(basegfx::fTools::equalZero(fSWidth));
const bool bNoSHeight(basegfx::fTools::equalZero(fSHeight));
// transform from source state to unit range
aRetval.translate(-rSource.getMinX(), -rSource.getMinY());
aRetval.scale(
(bNoSWidth ? 1.0 : 1.0 / fSWidth) * rTarget.getWidth(),
(bNoSHeight ? 1.0 : 1.0 / fSHeight) * rTarget.getHeight());
// transform from unit rage to target range
aRetval.translate(rTarget.getMinX(), rTarget.getMinY());
return aRetval;
}
basegfx::B2DHomMatrix SvgAspectRatio::createMapping(const basegfx::B2DRange& rTarget, const basegfx::B2DRange& rSource) const
{
// removed !isSet() from below. Due to correct defaults in the constructor an instance
// of this class is perfectly useful without being set by any importer
if(SvgAlign::none == getSvgAlign())
{
// create linear mapping (default)
return createLinearMapping(rTarget, rSource);
}
basegfx::B2DHomMatrix aRetval;
const double fSWidth(rSource.getWidth());
const double fSHeight(rSource.getHeight());
const bool bNoSWidth(basegfx::fTools::equalZero(fSWidth));
const bool bNoSHeight(basegfx::fTools::equalZero(fSHeight));
const double fScaleX((bNoSWidth ? 1.0 : 1.0 / fSWidth) * rTarget.getWidth());
const double fScaleY((bNoSHeight ? 1.0 : 1.0 / fSHeight) * rTarget.getHeight());
const double fScale(isMeetOrSlice() ? std::min(fScaleX, fScaleY) : std::max(fScaleX, fScaleY));
// remove source translation, apply scale
aRetval.translate(-rSource.getMinX(), -rSource.getMinY());
aRetval.scale(fScale, fScale);
// evaluate horizontal alignment
const double fNewWidth(fSWidth * fScale);
double fTransX(0.0);
switch(getSvgAlign())
{
case SvgAlign::xMidYMin:
case SvgAlign::xMidYMid:
case SvgAlign::xMidYMax:
{
// centerX
const double fFreeSpace(rTarget.getWidth() - fNewWidth);
fTransX = fFreeSpace * 0.5;
break;
}
case SvgAlign::xMaxYMin:
case SvgAlign::xMaxYMid:
case SvgAlign::xMaxYMax:
{
// Right align
const double fFreeSpace(rTarget.getWidth() - fNewWidth);
fTransX = fFreeSpace;
break;
}
default: break;
}
// evaluate vertical alignment
const double fNewHeight(fSHeight * fScale);
double fTransY(0.0);
switch(getSvgAlign())
{
case SvgAlign::xMinYMid:
case SvgAlign::xMidYMid:
case SvgAlign::xMaxYMid:
{
// centerY
const double fFreeSpace(rTarget.getHeight() - fNewHeight);
fTransY = fFreeSpace * 0.5;
break;
}
case SvgAlign::xMinYMax:
case SvgAlign::xMidYMax:
case SvgAlign::xMaxYMax:
{
// Bottom align
const double fFreeSpace(rTarget.getHeight() - fNewHeight);
fTransY = fFreeSpace;
break;
}
default: break;
}
// add target translation
aRetval.translate(
rTarget.getMinX() + fTransX,
rTarget.getMinY() + fTransY);
return aRetval;
}
void skip_char(std::u16string_view rCandidate, sal_Unicode nChar, sal_Int32& nPos, const sal_Int32 nLen)
{
while(nPos < nLen && nChar == rCandidate[nPos])
{
nPos++;
}
}
void skip_char(std::u16string_view rCandidate, sal_Unicode nCharA, sal_Unicode nCharB, sal_Int32& nPos, const sal_Int32 nLen)
{
while(nPos < nLen && (nCharA == rCandidate[nPos] || nCharB == rCandidate[nPos]))
{
nPos++;
}
}
void copySign(std::u16string_view rCandidate, sal_Int32& nPos, OUStringBuffer& rTarget, const sal_Int32 nLen)
{
if(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
if('+' == aChar || '-' == aChar)
{
rTarget.append(aChar);
nPos++;
}
}
}
void copyNumber(std::u16string_view rCandidate, sal_Int32& nPos, OUStringBuffer& rTarget, const sal_Int32 nLen)
{
bool bOnNumber(true);
while(bOnNumber && nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
bOnNumber = ('0' <= aChar && '9' >= aChar) || '.' == aChar;
if(bOnNumber)
{
rTarget.append(aChar);
nPos++;
}
}
}
void copyHex(std::u16string_view rCandidate, sal_Int32& nPos, OUStringBuffer& rTarget, const sal_Int32 nLen)
{
bool bOnHex(true);
while(bOnHex && nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
bOnHex = ('0' <= aChar && '9' >= aChar)
|| ('A' <= aChar && 'F' >= aChar)
|| ('a' <= aChar && 'f' >= aChar);
if(bOnHex)
{
rTarget.append(aChar);
nPos++;
}
}
}
void copyString(std::u16string_view rCandidate, sal_Int32& nPos, OUStringBuffer& rTarget, const sal_Int32 nLen)
{
bool bOnChar(true);
while(bOnChar && nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
bOnChar = ('a' <= aChar && 'z' >= aChar)
|| ('A' <= aChar && 'Z' >= aChar)
|| '-' == aChar;
if(bOnChar)
{
rTarget.append(aChar);
nPos++;
}
}
}
void copyToLimiter(std::u16string_view rCandidate, sal_Unicode nLimiter, sal_Int32& nPos, OUStringBuffer& rTarget, const sal_Int32 nLen)
{
while(nPos < nLen && nLimiter != rCandidate[nPos])
{
rTarget.append(rCandidate[nPos]);
nPos++;
}
}
bool readNumber(std::u16string_view rCandidate, sal_Int32& nPos, double& fNum, const sal_Int32 nLen)
{
if(nPos < nLen)
{
OUStringBuffer aNum;
copySign(rCandidate, nPos, aNum, nLen);
copyNumber(rCandidate, nPos, aNum, nLen);
if(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
if('e' == aChar || 'E' == aChar)
{
// try to read exponential number, but be careful. I had
// a case where dx="2em" was used, thus the 'e' was consumed
// by error. First try if there are numbers after the 'e',
// safe current state
nPos++;
const OUStringBuffer aNum2(aNum);
const sal_Int32 nPosAfterE(nPos);
aNum.append(aChar);
copySign(rCandidate, nPos, aNum, nLen);
copyNumber(rCandidate, nPos, aNum, nLen);
if(nPosAfterE == nPos)
{
// no number after 'e', go back. Do not
// return false, it's still a valid integer number
aNum = aNum2;
nPos--;
}
}
}
if(!aNum.isEmpty())
{
rtl_math_ConversionStatus eStatus;
fNum = rtl::math::stringToDouble(
aNum, '.', ',',
&eStatus);
return eStatus == rtl_math_ConversionStatus_Ok;
}
}
return false;
}
SvgUnit readUnit(std::u16string_view rCandidate, sal_Int32& nPos, const sal_Int32 nLen)
{
SvgUnit aRetval(SvgUnit::px);
if(nPos < nLen)
{
const sal_Unicode aCharA(rCandidate[nPos]);
if(nPos + 1 < nLen)
{
const sal_Unicode aCharB(rCandidate[nPos + 1]);
bool bTwoCharValid(false);
switch(aCharA)
{
case u'e' :
{
if('m' == aCharB)
{
// 'em' Relative to current font size
aRetval = SvgUnit::em;
bTwoCharValid = true;
}
else if('x' == aCharB)
{
// 'ex' Relative to current font x-height
aRetval = SvgUnit::ex;
bTwoCharValid = true;
}
break;
}
case u'p' :
{
if('x' == aCharB)
{
// 'px' UserUnit (default)
bTwoCharValid = true;
}
else if('t' == aCharB)
{
// 'pt' == 4/3 px
aRetval = SvgUnit::pt;
bTwoCharValid = true;
}
else if('c' == aCharB)
{
// 'pc' == 16 px
aRetval = SvgUnit::pc;
bTwoCharValid = true;
}
break;
}
case u'i' :
{
if('n' == aCharB)
{
// 'in' == 96 px, since CSS 2.1
aRetval = SvgUnit::in;
bTwoCharValid = true;
}
break;
}
case u'c' :
{
if('m' == aCharB)
{
// 'cm' == 37.79527559 px
aRetval = SvgUnit::cm;
bTwoCharValid = true;
}
break;
}
case u'm' :
{
if('m' == aCharB)
{
// 'mm' == 3.779528 px
aRetval = SvgUnit::mm;
bTwoCharValid = true;
}
break;
}
}
if(bTwoCharValid)
{
nPos += 2;
}
}
else
{
if('%' == aCharA)
{
// percent used, relative to current
nPos++;
aRetval = SvgUnit::percent;
}
}
}
return aRetval;
}
bool readNumberAndUnit(std::u16string_view rCandidate, sal_Int32& nPos, SvgNumber& aNum, const sal_Int32 nLen)
{
double fNum(0.0);
if(readNumber(rCandidate, nPos, fNum, nLen))
{
skip_char(rCandidate, ' ', nPos, nLen);
aNum = SvgNumber(fNum, readUnit(rCandidate, nPos, nLen));
return true;
}
return false;
}
bool readAngle(std::u16string_view rCandidate, sal_Int32& nPos, double& fAngle, const sal_Int32 nLen)
{
if(readNumber(rCandidate, nPos, fAngle, nLen))
{
skip_char(rCandidate, ' ', nPos, nLen);
enum class DegreeType
{
deg,
grad,
rad
} aType(DegreeType::deg); // degrees is default
if(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
static constexpr std::u16string_view aStrGrad = u"grad";
static constexpr std::u16string_view aStrRad = u"rad";
switch(aChar)
{
case u'g' :
case u'G' :
{
if(o3tl::matchIgnoreAsciiCase(rCandidate, aStrGrad, nPos))
{
// angle in grad
nPos += aStrGrad.size();
aType = DegreeType::grad;
}
break;
}
case u'r' :
case u'R' :
{
if(o3tl::matchIgnoreAsciiCase(rCandidate, aStrRad, nPos))
{
// angle in radians
nPos += aStrRad.size();
aType = DegreeType::rad;
}
break;
}
}
}
// convert to radians
if (DegreeType::deg == aType)
{
fAngle = basegfx::deg2rad(fAngle);
}
else if (DegreeType::grad == aType)
{
// looks like 100 grad is 90 degrees
fAngle *= M_PI / 200.0;
}
return true;
}
return false;
}
sal_Int32 read_hex(sal_Unicode nChar)
{
if(nChar >= '0' && nChar <= '9')
{
return nChar - u'0';
}
else if(nChar >= 'A' && nChar <= 'F')
{
return 10 + sal_Int32(nChar - u'A');
}
else if(nChar >= 'a' && nChar <= 'f')
{
return 10 + sal_Int32(nChar - u'a');
}
else
{
// error
return 0;
}
}
bool match_colorKeyword(basegfx::BColor& rColor, const OUString& rName)
{
typedef std::unordered_map< OUString, Color > ColorTokenMapper;
typedef std::pair< OUString, Color > ColorTokenValueType;
static const ColorTokenMapper aColorTokenMapperList {
{ ColorTokenValueType(OUString("aliceblue"), Color(240, 248, 255)) },
{ ColorTokenValueType(OUString("antiquewhite"), Color(250, 235, 215) ) },
{ ColorTokenValueType(OUString("aqua"), Color( 0, 255, 255) ) },
{ ColorTokenValueType(OUString("aquamarine"), Color(127, 255, 212) ) },
{ ColorTokenValueType(OUString("azure"), Color(240, 255, 255) ) },
{ ColorTokenValueType(OUString("beige"), Color(245, 245, 220) ) },
{ ColorTokenValueType(OUString("bisque"), Color(255, 228, 196) ) },
{ ColorTokenValueType(OUString("black"), Color( 0, 0, 0) ) },
{ ColorTokenValueType(OUString("blanchedalmond"), Color(255, 235, 205) ) },
{ ColorTokenValueType(OUString("blue"), Color( 0, 0, 255) ) },
{ ColorTokenValueType(OUString("blueviolet"), Color(138, 43, 226) ) },
{ ColorTokenValueType(OUString("brown"), Color(165, 42, 42) ) },
{ ColorTokenValueType(OUString("burlywood"), Color(222, 184, 135) ) },
{ ColorTokenValueType(OUString("cadetblue"), Color( 95, 158, 160) ) },
{ ColorTokenValueType(OUString("chartreuse"), Color(127, 255, 0) ) },
{ ColorTokenValueType(OUString("chocolate"), Color(210, 105, 30) ) },
{ ColorTokenValueType(OUString("coral"), Color(255, 127, 80) ) },
{ ColorTokenValueType(OUString("cornflowerblue"), Color(100, 149, 237) ) },
{ ColorTokenValueType(OUString("cornsilk"), Color(255, 248, 220) ) },
{ ColorTokenValueType(OUString("crimson"), Color(220, 20, 60) ) },
{ ColorTokenValueType(OUString("cyan"), Color( 0, 255, 255) ) },
{ ColorTokenValueType(OUString("darkblue"), Color( 0, 0, 139) ) },
{ ColorTokenValueType(OUString("darkcyan"), Color( 0, 139, 139) ) },
{ ColorTokenValueType(OUString("darkgoldenrod"), Color(184, 134, 11) ) },
{ ColorTokenValueType(OUString("darkgray"), Color(169, 169, 169) ) },
{ ColorTokenValueType(OUString("darkgreen"), Color( 0, 100, 0) ) },
{ ColorTokenValueType(OUString("darkgrey"), Color(169, 169, 169) ) },
{ ColorTokenValueType(OUString("darkkhaki"), Color(189, 183, 107) ) },
{ ColorTokenValueType(OUString("darkmagenta"), Color(139, 0, 139) ) },
{ ColorTokenValueType(OUString("darkolivegreen"), Color( 85, 107, 47) ) },
{ ColorTokenValueType(OUString("darkorange"), Color(255, 140, 0) ) },
{ ColorTokenValueType(OUString("darkorchid"), Color(153, 50, 204) ) },
{ ColorTokenValueType(OUString("darkred"), Color(139, 0, 0) ) },
{ ColorTokenValueType(OUString("darksalmon"), Color(233, 150, 122) ) },
{ ColorTokenValueType(OUString("darkseagreen"), Color(143, 188, 143) ) },
{ ColorTokenValueType(OUString("darkslateblue"), Color( 72, 61, 139) ) },
{ ColorTokenValueType(OUString("darkslategray"), Color( 47, 79, 79) ) },
{ ColorTokenValueType(OUString("darkslategrey"), Color( 47, 79, 79) ) },
{ ColorTokenValueType(OUString("darkturquoise"), Color( 0, 206, 209) ) },
{ ColorTokenValueType(OUString("darkviolet"), Color(148, 0, 211) ) },
{ ColorTokenValueType(OUString("deeppink"), Color(255, 20, 147) ) },
{ ColorTokenValueType(OUString("deepskyblue"), Color( 0, 191, 255) ) },
{ ColorTokenValueType(OUString("dimgray"), Color(105, 105, 105) ) },
{ ColorTokenValueType(OUString("dimgrey"), Color(105, 105, 105) ) },
{ ColorTokenValueType(OUString("dodgerblue"), Color( 30, 144, 255) ) },
{ ColorTokenValueType(OUString("firebrick"), Color(178, 34, 34) ) },
{ ColorTokenValueType(OUString("floralwhite"), Color(255, 250, 240) ) },
{ ColorTokenValueType(OUString("forestgreen"), Color( 34, 139, 34) ) },
{ ColorTokenValueType(OUString("fuchsia"), Color(255, 0, 255) ) },
{ ColorTokenValueType(OUString("gainsboro"), Color(220, 220, 220) ) },
{ ColorTokenValueType(OUString("ghostwhite"), Color(248, 248, 255) ) },
{ ColorTokenValueType(OUString("gold"), Color(255, 215, 0) ) },
{ ColorTokenValueType(OUString("goldenrod"), Color(218, 165, 32) ) },
{ ColorTokenValueType(OUString("gray"), Color(128, 128, 128) ) },
{ ColorTokenValueType(OUString("grey"), Color(128, 128, 128) ) },
{ ColorTokenValueType(OUString("green"), Color(0, 128, 0) ) },
{ ColorTokenValueType(OUString("greenyellow"), Color(173, 255, 47) ) },
{ ColorTokenValueType(OUString("honeydew"), Color(240, 255, 240) ) },
{ ColorTokenValueType(OUString("hotpink"), Color(255, 105, 180) ) },
{ ColorTokenValueType(OUString("indianred"), Color(205, 92, 92) ) },
{ ColorTokenValueType(OUString("indigo"), Color( 75, 0, 130) ) },
{ ColorTokenValueType(OUString("ivory"), Color(255, 255, 240) ) },
{ ColorTokenValueType(OUString("khaki"), Color(240, 230, 140) ) },
{ ColorTokenValueType(OUString("lavender"), Color(230, 230, 250) ) },
{ ColorTokenValueType(OUString("lavenderblush"), Color(255, 240, 245) ) },
{ ColorTokenValueType(OUString("lawngreen"), Color(124, 252, 0) ) },
{ ColorTokenValueType(OUString("lemonchiffon"), Color(255, 250, 205) ) },
{ ColorTokenValueType(OUString("lightblue"), Color(173, 216, 230) ) },
{ ColorTokenValueType(OUString("lightcoral"), Color(240, 128, 128) ) },
{ ColorTokenValueType(OUString("lightcyan"), Color(224, 255, 255) ) },
{ ColorTokenValueType(OUString("lightgoldenrodyellow"), Color(250, 250, 210) ) },
{ ColorTokenValueType(OUString("lightgray"), Color(211, 211, 211) ) },
{ ColorTokenValueType(OUString("lightgreen"), Color(144, 238, 144) ) },
{ ColorTokenValueType(OUString("lightgrey"), Color(211, 211, 211) ) },
{ ColorTokenValueType(OUString("lightpink"), Color(255, 182, 193) ) },
{ ColorTokenValueType(OUString("lightsalmon"), Color(255, 160, 122) ) },
{ ColorTokenValueType(OUString("lightseagreen"), Color( 32, 178, 170) ) },
{ ColorTokenValueType(OUString("lightskyblue"), Color(135, 206, 250) ) },
{ ColorTokenValueType(OUString("lightslategray"), Color(119, 136, 153) ) },
{ ColorTokenValueType(OUString("lightslategrey"), Color(119, 136, 153) ) },
{ ColorTokenValueType(OUString("lightsteelblue"), Color(176, 196, 222) ) },
{ ColorTokenValueType(OUString("lightyellow"), Color(255, 255, 224) ) },
{ ColorTokenValueType(OUString("lime"), Color( 0, 255, 0) ) },
{ ColorTokenValueType(OUString("limegreen"), Color( 50, 205, 50) ) },
{ ColorTokenValueType(OUString("linen"), Color(250, 240, 230) ) },
{ ColorTokenValueType(OUString("magenta"), Color(255, 0, 255) ) },
{ ColorTokenValueType(OUString("maroon"), Color(128, 0, 0) ) },
{ ColorTokenValueType(OUString("mediumaquamarine"), Color(102, 205, 170) ) },
{ ColorTokenValueType(OUString("mediumblue"), Color( 0, 0, 205) ) },
{ ColorTokenValueType(OUString("mediumorchid"), Color(186, 85, 211) ) },
{ ColorTokenValueType(OUString("mediumpurple"), Color(147, 112, 219) ) },
{ ColorTokenValueType(OUString("mediumseagreen"), Color( 60, 179, 113) ) },
{ ColorTokenValueType(OUString("mediumslateblue"), Color(123, 104, 238) ) },
{ ColorTokenValueType(OUString("mediumspringgreen"), Color( 0, 250, 154) ) },
{ ColorTokenValueType(OUString("mediumturquoise"), Color( 72, 209, 204) ) },
{ ColorTokenValueType(OUString("mediumvioletred"), Color(199, 21, 133) ) },
{ ColorTokenValueType(OUString("midnightblue"), Color( 25, 25, 112) ) },
{ ColorTokenValueType(OUString("mintcream"), Color(245, 255, 250) ) },
{ ColorTokenValueType(OUString("mistyrose"), Color(255, 228, 225) ) },
{ ColorTokenValueType(OUString("moccasin"), Color(255, 228, 181) ) },
{ ColorTokenValueType(OUString("navajowhite"), Color(255, 222, 173) ) },
{ ColorTokenValueType(OUString("navy"), Color( 0, 0, 128) ) },
{ ColorTokenValueType(OUString("oldlace"), Color(253, 245, 230) ) },
{ ColorTokenValueType(OUString("olive"), Color(128, 128, 0) ) },
{ ColorTokenValueType(OUString("olivedrab"), Color(107, 142, 35) ) },
{ ColorTokenValueType(OUString("orange"), Color(255, 165, 0) ) },
{ ColorTokenValueType(OUString("orangered"), Color(255, 69, 0) ) },
{ ColorTokenValueType(OUString("orchid"), Color(218, 112, 214) ) },
{ ColorTokenValueType(OUString("palegoldenrod"), Color(238, 232, 170) ) },
{ ColorTokenValueType(OUString("palegreen"), Color(152, 251, 152) ) },
{ ColorTokenValueType(OUString("paleturquoise"), Color(175, 238, 238) ) },
{ ColorTokenValueType(OUString("palevioletred"), Color(219, 112, 147) ) },
{ ColorTokenValueType(OUString("papayawhip"), Color(255, 239, 213) ) },
{ ColorTokenValueType(OUString("peachpuff"), Color(255, 218, 185) ) },
{ ColorTokenValueType(OUString("peru"), Color(205, 133, 63) ) },
{ ColorTokenValueType(OUString("pink"), Color(255, 192, 203) ) },
{ ColorTokenValueType(OUString("plum"), Color(221, 160, 221) ) },
{ ColorTokenValueType(OUString("powderblue"), Color(176, 224, 230) ) },
{ ColorTokenValueType(OUString("purple"), Color(128, 0, 128) ) },
{ ColorTokenValueType(OUString("red"), Color(255, 0, 0) ) },
{ ColorTokenValueType(OUString("rosybrown"), Color(188, 143, 143) ) },
{ ColorTokenValueType(OUString("royalblue"), Color( 65, 105, 225) ) },
{ ColorTokenValueType(OUString("saddlebrown"), Color(139, 69, 19) ) },
{ ColorTokenValueType(OUString("salmon"), Color(250, 128, 114) ) },
{ ColorTokenValueType(OUString("sandybrown"), Color(244, 164, 96) ) },
{ ColorTokenValueType(OUString("seagreen"), Color( 46, 139, 87) ) },
{ ColorTokenValueType(OUString("seashell"), Color(255, 245, 238) ) },
{ ColorTokenValueType(OUString("sienna"), Color(160, 82, 45) ) },
{ ColorTokenValueType(OUString("silver"), Color(192, 192, 192) ) },
{ ColorTokenValueType(OUString("skyblue"), Color(135, 206, 235) ) },
{ ColorTokenValueType(OUString("slateblue"), Color(106, 90, 205) ) },
{ ColorTokenValueType(OUString("slategray"), Color(112, 128, 144) ) },
{ ColorTokenValueType(OUString("slategrey"), Color(112, 128, 144) ) },
{ ColorTokenValueType(OUString("snow"), Color(255, 250, 250) ) },
{ ColorTokenValueType(OUString("springgreen"), Color( 0, 255, 127) ) },
{ ColorTokenValueType(OUString("steelblue"), Color( 70, 130, 180) ) },
{ ColorTokenValueType(OUString("tan"), Color(210, 180, 140) ) },
{ ColorTokenValueType(OUString("teal"), Color( 0, 128, 128) ) },
{ ColorTokenValueType(OUString("thistle"), Color(216, 191, 216) ) },
{ ColorTokenValueType(OUString("tomato"), Color(255, 99, 71) ) },
{ ColorTokenValueType(OUString("turquoise"), Color( 64, 224, 208) ) },
{ ColorTokenValueType(OUString("violet"), Color(238, 130, 238) ) },
{ ColorTokenValueType(OUString("wheat"), Color(245, 222, 179) ) },
{ ColorTokenValueType(OUString("white"), Color(255, 255, 255) ) },
{ ColorTokenValueType(OUString("whitesmoke"), Color(245, 245, 245) ) },
{ ColorTokenValueType(OUString("yellow"), Color(255, 255, 0) ) },
{ ColorTokenValueType(OUString("yellowgreen"), Color(154, 205, 50) ) },
};
ColorTokenMapper::const_iterator aResult(aColorTokenMapperList.find(rName.toAsciiLowerCase().trim()));
if(aResult == aColorTokenMapperList.end())
{
return false;
}
else
{
rColor = aResult->second.getBColor();
return true;
}
}
bool read_color(const OUString& rCandidate, basegfx::BColor& rColor, SvgNumber& rOpacity)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
const sal_Unicode aChar(rCandidate[0]);
const double fFactor(1.0 / 255.0);
if(aChar == '#')
{
// hex definition
OUStringBuffer aNum;
sal_Int32 nPos(1);
copyHex(rCandidate, nPos, aNum, nLen);
const sal_Int32 nLength(aNum.getLength());
if(3 == nLength)
{
const sal_Int32 nR(read_hex(aNum[0]));
const sal_Int32 nG(read_hex(aNum[1]));
const sal_Int32 nB(read_hex(aNum[2]));
rColor.setRed((nR | (nR << 4)) * fFactor);
rColor.setGreen((nG | (nG << 4)) * fFactor);
rColor.setBlue((nB | (nB << 4)) * fFactor);
return true;
}
else if(6 == nLength)
{
const sal_Int32 nR1(read_hex(aNum[0]));
const sal_Int32 nR2(read_hex(aNum[1]));
const sal_Int32 nG1(read_hex(aNum[2]));
const sal_Int32 nG2(read_hex(aNum[3]));
const sal_Int32 nB1(read_hex(aNum[4]));
const sal_Int32 nB2(read_hex(aNum[5]));
rColor.setRed((nR2 | (nR1 << 4)) * fFactor);
rColor.setGreen((nG2 | (nG1 << 4)) * fFactor);
rColor.setBlue((nB2 | (nB1 << 4)) * fFactor);
return true;
}
}
else
{
static const char aStrRgb[] = "rgb";
if(rCandidate.matchIgnoreAsciiCase(aStrRgb, 0))
{
// rgb/rgba definition
sal_Int32 nPos(strlen(aStrRgb));
bool bIsRGBA = false;
if('a' == rCandidate[nPos])
{
//Delete the 'a' from 'rbga'
skip_char(rCandidate, 'a', nPos, nPos + 1);
bIsRGBA = true;
}
skip_char(rCandidate, ' ', '(', nPos, nLen);
double fR(0.0);
if(readNumber(rCandidate, nPos, fR, nLen))
{
skip_char(rCandidate, ' ', nPos, nLen);
if(nPos < nLen)
{
const sal_Unicode aPercentChar(rCandidate[nPos]);
const bool bIsPercent('%' == aPercentChar);
double fG(0.0);
if(bIsPercent)
{
skip_char(rCandidate, '%', nPos, nLen);
}
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumber(rCandidate, nPos, fG, nLen))
{
double fB(0.0);
if(bIsPercent)
{
skip_char(rCandidate, '%', nPos, nLen);
}
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumber(rCandidate, nPos, fB, nLen))
{
double fA(1.0);
if(bIsPercent)
{
skip_char(rCandidate, '%', nPos, nLen);
}
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumber(rCandidate, nPos, fA, nLen))
{
if(bIsRGBA)
{
const double fFac(bIsPercent ? 0.01 : 1);
rOpacity = SvgNumber(fA * fFac);
if(bIsPercent)
{
skip_char(rCandidate, '%', nPos, nLen);
}
}
else
{
return false;
}
}
const double fFac(bIsPercent ? 0.01 : fFactor);
rColor.setRed(fR * fFac);
rColor.setGreen(fG * fFac);
rColor.setBlue(fB * fFac);
skip_char(rCandidate, ' ', ')', nPos, nLen);
return true;
}
}
}
}
}
else
{
// color keyword
if(match_colorKeyword(rColor, rCandidate))
{
return true;
}
}
}
}
return false;
}
basegfx::B2DRange readViewBox(const OUString& rCandidate, InfoProvider const & rInfoProvider)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
SvgNumber aMinX;
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aMinX, nLen))
{
SvgNumber aMinY;
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aMinY, nLen))
{
SvgNumber aWidth;
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aWidth, nLen))
{
SvgNumber aHeight;
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aHeight, nLen))
{
double fX(aMinX.solve(rInfoProvider, NumberType::xcoordinate));
double fY(aMinY.solve(rInfoProvider, NumberType::ycoordinate));
double fW(aWidth.solve(rInfoProvider, NumberType::xcoordinate));
double fH(aHeight.solve(rInfoProvider, NumberType::ycoordinate));
return basegfx::B2DRange(fX,fY,fX+fW,fY+fH);
}
}
}
}
}
return basegfx::B2DRange();
}
basegfx::B2DHomMatrix readTransform(const OUString& rCandidate, InfoProvider const & rInfoProvider)
{
basegfx::B2DHomMatrix aMatrix;
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
skip_char(rCandidate, ' ', ',', nPos, nLen);
while(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
const sal_Int32 nInitPos(nPos);
static const char aStrMatrix[] = "matrix";
static const char aStrTranslate[] = "translate";
static const char aStrScale[] = "scale";
static const char aStrRotate[] = "rotate";
static const char aStrSkewX[] = "skewX";
static const char aStrSkewY[] = "skewY";
switch(aChar)
{
case u'm' :
{
if(rCandidate.match(aStrMatrix, nPos))
{
// matrix element
nPos += strlen(aStrMatrix);
skip_char(rCandidate, ' ', '(', nPos, nLen);
SvgNumber aVal;
basegfx::B2DHomMatrix aNew;
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(0, 0, aVal.solve(rInfoProvider)); // Element A
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(1, 0, aVal.solve(rInfoProvider)); // Element B
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(0, 1, aVal.solve(rInfoProvider)); // Element C
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(1, 1, aVal.solve(rInfoProvider)); // Element D
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(0, 2, aVal.solve(rInfoProvider, NumberType::xcoordinate)); // Element E
skip_char(rCandidate, ' ', ',', nPos, nLen);
if(readNumberAndUnit(rCandidate, nPos, aVal, nLen))
{
aNew.set(1, 2, aVal.solve(rInfoProvider, NumberType::ycoordinate)); // Element F
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
// caution: String is evaluated from left to right, but matrix multiplication
// in SVG is right to left, so put the new transformation before the current
// one by multiplicating from the right side
aMatrix = aMatrix * aNew;
}
}
}
}
}
}
}
break;
}
case u't' :
{
if(rCandidate.match(aStrTranslate, nPos))
{
// translate element
nPos += strlen(aStrTranslate);
skip_char(rCandidate, ' ', '(', nPos, nLen);
SvgNumber aTransX;
if(readNumberAndUnit(rCandidate, nPos, aTransX, nLen))
{
skip_char(rCandidate, ' ', ',', nPos, nLen);
SvgNumber aTransY;
readNumberAndUnit(rCandidate, nPos, aTransY, nLen);
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
aMatrix = aMatrix * basegfx::utils::createTranslateB2DHomMatrix(
aTransX.solve(rInfoProvider, NumberType::xcoordinate),
aTransY.solve(rInfoProvider, NumberType::ycoordinate));
}
}
break;
}
case u's' :
{
if(rCandidate.match(aStrScale, nPos))
{
// scale element
nPos += strlen(aStrScale);
skip_char(rCandidate, ' ', '(', nPos, nLen);
SvgNumber aScaleX;
if(readNumberAndUnit(rCandidate, nPos, aScaleX, nLen))
{
skip_char(rCandidate, ' ', ',', nPos, nLen);
SvgNumber aScaleY(aScaleX);
readNumberAndUnit(rCandidate, nPos, aScaleY, nLen);
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
aMatrix = aMatrix * basegfx::utils::createScaleB2DHomMatrix(
aScaleX.solve(rInfoProvider),
aScaleY.solve(rInfoProvider));
}
}
else if(rCandidate.match(aStrSkewX, nPos))
{
// skewx element
nPos += strlen(aStrSkewX);
skip_char(rCandidate, ' ', '(', nPos, nLen);
double fSkewX(0.0);
if(readAngle(rCandidate, nPos, fSkewX, nLen))
{
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
aMatrix = aMatrix * basegfx::utils::createShearXB2DHomMatrix(tan(fSkewX));
}
}
else if(rCandidate.match(aStrSkewY, nPos))
{
// skewy element
nPos += strlen(aStrSkewY);
skip_char(rCandidate, ' ', '(', nPos, nLen);
double fSkewY(0.0);
if(readAngle(rCandidate, nPos, fSkewY, nLen))
{
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
aMatrix = aMatrix * basegfx::utils::createShearYB2DHomMatrix(tan(fSkewY));
}
}
break;
}
case u'r' :
{
if(rCandidate.match(aStrRotate, nPos))
{
// rotate element
nPos += strlen(aStrRotate);
skip_char(rCandidate, ' ', '(', nPos, nLen);
double fAngle(0.0);
if(readAngle(rCandidate, nPos, fAngle, nLen))
{
skip_char(rCandidate, ' ', ',', nPos, nLen);
SvgNumber aX;
readNumberAndUnit(rCandidate, nPos, aX, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
SvgNumber aY;
readNumberAndUnit(rCandidate, nPos, aY, nLen);
skip_char(rCandidate, ' ', ')', nPos, nLen);
skip_char(rCandidate, ' ', ',', nPos, nLen);
const double fX(aX.isSet() ? aX.solve(rInfoProvider, NumberType::xcoordinate) : 0.0);
const double fY(aY.isSet() ? aY.solve(rInfoProvider, NumberType::ycoordinate) : 0.0);
if(!basegfx::fTools::equalZero(fX) || !basegfx::fTools::equalZero(fY))
{
// rotate around point
aMatrix = aMatrix * basegfx::utils::createRotateAroundPoint(fX, fY, fAngle);
}
else
{
// rotate
aMatrix = aMatrix * basegfx::utils::createRotateB2DHomMatrix(fAngle);
}
}
}
break;
}
}
if(nInitPos == nPos)
{
SAL_WARN("svgio", "Could not interpret on current position (!)");
nPos++;
}
}
}
return aMatrix;
}
bool readSingleNumber(const OUString& rCandidate, SvgNumber& aNum)
{
const sal_Int32 nLen(rCandidate.getLength());
sal_Int32 nPos(0);
return readNumberAndUnit(rCandidate, nPos, aNum, nLen);
}
bool readLocalUrl(const OUString& rCandidate, OUString& rURL)
{
static const char aStrUrl[] = "url(";
if(rCandidate.startsWithIgnoreAsciiCase(aStrUrl))
{
const sal_Int32 nLen(rCandidate.getLength());
sal_Int32 nPos(strlen(aStrUrl));
sal_Unicode aLimiter(')');
skip_char(rCandidate, ' ', nPos, nLen);
if('"' == rCandidate[nPos])
{
aLimiter = '"';
++nPos;
}
else if('\'' == rCandidate[nPos])
{
aLimiter = '\'';
++nPos;
}
skip_char(rCandidate, ' ', nPos, nLen);
skip_char(rCandidate, '#', nPos, nPos + 1);
OUStringBuffer aTokenValue;
copyToLimiter(rCandidate, aLimiter, nPos, aTokenValue, nLen);
rURL = aTokenValue.makeStringAndClear();
return true;
}
return false;
}
bool readSvgPaint(const OUString& rCandidate, SvgPaint& rSvgPaint,
OUString& rURL, SvgNumber& rOpacity)
{
if( !rCandidate.isEmpty() )
{
basegfx::BColor aColor;
if(read_color(rCandidate, aColor, rOpacity))
{
rSvgPaint = SvgPaint(aColor, true, true);
return true;
}
else
{
if(rCandidate.startsWith("none"))
{
rSvgPaint = SvgPaint(aColor, true, false, false);
return true;
}
else if(readLocalUrl(rCandidate, rURL))
{
/// Url is copied to rURL, but needs to be solved outside this helper
return false;
}
else if(rCandidate.startsWith("currentColor"))
{
rSvgPaint = SvgPaint(aColor, true, true, true);
return true;
}
}
}
return false;
}
bool readSvgNumberVector(const OUString& rCandidate, SvgNumberVector& rSvgNumberVector)
{
const sal_Int32 nLen(rCandidate.getLength());
rSvgNumberVector.clear();
if(nLen)
{
sal_Int32 nPos(0);
SvgNumber aNum;
skip_char(rCandidate, ' ', ',', nPos, nLen);
while(readNumberAndUnit(rCandidate, nPos, aNum, nLen))
{
rSvgNumberVector.push_back(aNum);
skip_char(rCandidate, ' ', ',', nPos, nLen);
}
return !rSvgNumberVector.empty();
}
return false;
}
SvgAspectRatio readSvgAspectRatio(const OUString& rCandidate)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
SvgAlign aSvgAlign(SvgAlign::xMidYMid);
bool bMeetOrSlice(true);
bool bChanged(false);
while(nPos < nLen)
{
const sal_Int32 nInitPos(nPos);
skip_char(rCandidate, ' ', nPos, nLen);
OUStringBuffer aTokenName;
copyString(rCandidate, nPos, aTokenName, nLen);
if(!aTokenName.isEmpty())
{
switch(StrToSVGToken(aTokenName.makeStringAndClear(), false))
{
case SVGToken::Defer:
{
bChanged = true;
break;
}
case SVGToken::None:
{
aSvgAlign = SvgAlign::none;
bChanged = true;
break;
}
case SVGToken::XMinYMin:
{
aSvgAlign = SvgAlign::xMinYMin;
bChanged = true;
break;
}
case SVGToken::XMidYMin:
{
aSvgAlign = SvgAlign::xMidYMin;
bChanged = true;
break;
}
case SVGToken::XMaxYMin:
{
aSvgAlign = SvgAlign::xMaxYMin;
bChanged = true;
break;
}
case SVGToken::XMinYMid:
{
aSvgAlign = SvgAlign::xMinYMid;
bChanged = true;
break;
}
case SVGToken::XMidYMid:
{
aSvgAlign = SvgAlign::xMidYMid;
bChanged = true;
break;
}
case SVGToken::XMaxYMid:
{
aSvgAlign = SvgAlign::xMaxYMid;
bChanged = true;
break;
}
case SVGToken::XMinYMax:
{
aSvgAlign = SvgAlign::xMinYMax;
bChanged = true;
break;
}
case SVGToken::XMidYMax:
{
aSvgAlign = SvgAlign::xMidYMax;
bChanged = true;
break;
}
case SVGToken::XMaxYMax:
{
aSvgAlign = SvgAlign::xMaxYMax;
bChanged = true;
break;
}
case SVGToken::Meet:
{
bMeetOrSlice = true;
bChanged = true;
break;
}
case SVGToken::Slice:
{
bMeetOrSlice = false;
bChanged = true;
break;
}
default:
{
break;
}
}
}
if(nInitPos == nPos)
{
SAL_WARN("svgio", "Could not interpret on current position (!)");
nPos++;
}
}
if(bChanged)
{
return SvgAspectRatio(aSvgAlign, bMeetOrSlice);
}
}
return SvgAspectRatio();
}
bool readSvgStringVector(const OUString& rCandidate, SvgStringVector& rSvgStringVector)
{
rSvgStringVector.clear();
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
OUStringBuffer aTokenValue;
skip_char(rCandidate, ' ', ',', nPos, nLen);
while(nPos < nLen)
{
copyToLimiter(rCandidate, ',', nPos, aTokenValue, nLen);
skip_char(rCandidate, ',', ' ', nPos, nLen);
const OUString aString = aTokenValue.makeStringAndClear();
if(!aString.isEmpty())
{
rSvgStringVector.push_back(aString);
}
}
}
return !rSvgStringVector.empty();
}
void readImageLink(const OUString& rCandidate, OUString& rXLink, OUString& rUrl, OUString& rMimeType, OUString& rData)
{
rXLink.clear();
rUrl.clear();
rMimeType.clear();
rData.clear();
if('#' == rCandidate[0])
{
// local link
rXLink = rCandidate.copy(1);
}
else
{
static const char aStrData[] = "data:";
if(rCandidate.match(aStrData, 0))
{
// embedded data
sal_Int32 nPos(strlen(aStrData));
sal_Int32 nLen(rCandidate.getLength());
OUStringBuffer aBuffer;
// read mime type
skip_char(rCandidate, ' ', nPos, nLen);
copyToLimiter(rCandidate, ';', nPos, aBuffer, nLen);
skip_char(rCandidate, ' ', ';', nPos, nLen);
rMimeType = aBuffer.makeStringAndClear();
if(!rMimeType.isEmpty() && nPos < nLen)
{
if(rMimeType.startsWith("image"))
{
// image data
std::u16string_view aData(rCandidate.subView(nPos));
static constexpr std::u16string_view aStrBase64 = u"base64";
if(o3tl::starts_with(aData, aStrBase64))
{
// base64 encoded
nPos = aStrBase64.size();
nLen = aData.size();
skip_char(aData, ' ', ',', nPos, nLen);
if(nPos < nLen)
{
rData = aData.substr(nPos);
}
}
}
}
}
else
{
// Url (path and filename)
rUrl = rCandidate;
}
}
}
OUString convert(const OUString& rCandidate, sal_Unicode nPattern, sal_Unicode nNew, bool bRemove)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
OUStringBuffer aBuffer;
bool bChanged(false);
while(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
if(nPattern == aChar)
{
bChanged = true;
if(!bRemove)
{
aBuffer.append(nNew);
}
}
else
{
aBuffer.append(aChar);
}
nPos++;
}
if(bChanged)
{
return aBuffer.makeStringAndClear();
}
}
return rCandidate;
}
// #i125325#
OUString removeBlockComments(const OUString& rCandidate)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
OUStringBuffer aBuffer;
bool bChanged(false);
sal_Int32 nInsideComment(0);
const sal_Unicode aCommentSlash('/');
const sal_Unicode aCommentStar('*');
while(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
const bool bStart(aCommentSlash == aChar && nPos + 1 < nLen && aCommentStar == rCandidate[nPos + 1]);
const bool bEnd(aCommentStar == aChar && nPos + 1 < nLen && aCommentSlash == rCandidate[nPos + 1]);
if(bStart)
{
nPos += 2;
nInsideComment++;
bChanged = true;
}
else if(bEnd)
{
nPos += 2;
nInsideComment--;
}
else
{
if(!nInsideComment)
{
aBuffer.append(aChar);
}
nPos++;
}
}
if(bChanged)
{
return aBuffer.makeStringAndClear();
}
}
return rCandidate;
}
OUString consolidateContiguousSpace(const OUString& rCandidate)
{
const sal_Int32 nLen(rCandidate.getLength());
if(nLen)
{
sal_Int32 nPos(0);
OUStringBuffer aBuffer;
bool bInsideSpace(false);
const sal_Unicode aSpace(' ');
while(nPos < nLen)
{
const sal_Unicode aChar(rCandidate[nPos]);
if(aSpace == aChar)
{
bInsideSpace = true;
}
else
{
if(bInsideSpace)
{
bInsideSpace = false;
aBuffer.append(aSpace);
}
aBuffer.append(aChar);
}
nPos++;
}
if(bInsideSpace)
{
aBuffer.append(aSpace);
}
if(aBuffer.getLength() != nLen)
{
return aBuffer.makeStringAndClear();
}
}
return rCandidate;
}
OUString xmlSpaceHandling(const OUString& rCandidate, bool bIsDefault)
{
const sal_Unicode aNewline('\n');
const sal_Unicode aTab('\t');
const sal_Unicode aSpace(' ');
// remove all newline characters
OUString aRetval(convert(rCandidate, aNewline, aSpace, bIsDefault));
// convert tab to space
aRetval = convert(aRetval, aTab, aSpace, false);
if(bIsDefault)
{
// strip of all leading and trailing spaces
// and consolidate contiguous space
aRetval = consolidateContiguousSpace(aRetval.trim());
}
return aRetval;
}
::std::vector< double > solveSvgNumberVector(const SvgNumberVector& rInput, const InfoProvider& rInfoProvider)
{
::std::vector< double > aRetval;
if(!rInput.empty())
{
const double nCount(rInput.size());
aRetval.reserve(nCount);
for(sal_uInt32 a(0); a < nCount; a++)
{
aRetval.push_back(rInput[a].solve(rInfoProvider));
}
}
return aRetval;
}
} // end of namespace svgio::svgreader
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|