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
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
|
/* -*- 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 <osl/endian.h>
#include <osl/diagnose.h>
#include <sal/log.hxx>
#include <tools/bigint.hxx>
#include <tools/debug.hxx>
#include <tools/helpers.hxx>
#include <tools/stream.hxx>
#include <tools/vcompat.hxx>
#include <tools/gen.hxx>
#include <poly.h>
#include <o3tl/safeint.hxx>
#include <tools/line.hxx>
#include <tools/poly.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/point/b2dpoint.hxx>
#include <basegfx/vector/b2dvector.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/curve/b2dcubicbezier.hxx>
#include <memory>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <limits.h>
#include <cmath>
constexpr int EDGE_LEFT = 1;
constexpr int EDGE_TOP = 2;
constexpr int EDGE_RIGHT = 4;
constexpr int EDGE_BOTTOM = 8;
constexpr int EDGE_HORZ = EDGE_RIGHT | EDGE_LEFT;
constexpr int EDGE_VERT = EDGE_TOP | EDGE_BOTTOM;
constexpr double SMALL_DVALUE = 0.0000001;
#define FSQRT2 1.4142135623730950488016887242097
static double ImplGetParameter( const Point& rCenter, const Point& rPt, double fWR, double fHR )
{
const double nDX = static_cast<double>(rPt.X()) - rCenter.X();
const double nDY = static_cast<double>(rCenter.Y()) - rPt.Y();
double fAngle = atan2(nDY, nDX);
return atan2(fWR*sin(fAngle), fHR*cos(fAngle));
}
ImplPolygon::ImplPolygon( sal_uInt16 nInitSize )
{
ImplInitSize(nInitSize, false);
}
ImplPolygon::ImplPolygon( const ImplPolygon& rImpPoly )
{
if ( rImpPoly.mnPoints )
{
mxPointAry.reset(new Point[rImpPoly.mnPoints]);
memcpy(mxPointAry.get(), rImpPoly.mxPointAry.get(), rImpPoly.mnPoints * sizeof(Point));
if( rImpPoly.mxFlagAry )
{
mxFlagAry.reset(new PolyFlags[rImpPoly.mnPoints]);
memcpy(mxFlagAry.get(), rImpPoly.mxFlagAry.get(), rImpPoly.mnPoints);
}
}
mnPoints = rImpPoly.mnPoints;
}
ImplPolygon::ImplPolygon( sal_uInt16 nInitSize, const Point* pInitAry, const PolyFlags* pInitFlags )
{
if ( nInitSize )
{
mxPointAry.reset(new Point[nInitSize]);
memcpy(mxPointAry.get(), pInitAry, nInitSize * sizeof(Point));
if( pInitFlags )
{
mxFlagAry.reset(new PolyFlags[nInitSize]);
memcpy(mxFlagAry.get(), pInitFlags, nInitSize);
}
}
mnPoints = nInitSize;
}
ImplPolygon::ImplPolygon( const tools::Rectangle& rRect )
{
if ( !rRect.IsEmpty() )
{
ImplInitSize(5);
mxPointAry[0] = rRect.TopLeft();
mxPointAry[1] = rRect.TopRight();
mxPointAry[2] = rRect.BottomRight();
mxPointAry[3] = rRect.BottomLeft();
mxPointAry[4] = rRect.TopLeft();
}
else
mnPoints = 0;
}
ImplPolygon::ImplPolygon( const tools::Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
{
if ( !rRect.IsEmpty() )
{
tools::Rectangle aRect( rRect );
aRect.Normalize(); // SJ: i9140
nHorzRound = std::min( nHorzRound, static_cast<sal_uInt32>(std::abs( aRect.GetWidth() >> 1 )) );
nVertRound = std::min( nVertRound, static_cast<sal_uInt32>(std::abs( aRect.GetHeight() >> 1 )) );
if( !nHorzRound && !nVertRound )
{
ImplInitSize(5);
mxPointAry[0] = aRect.TopLeft();
mxPointAry[1] = aRect.TopRight();
mxPointAry[2] = aRect.BottomRight();
mxPointAry[3] = aRect.BottomLeft();
mxPointAry[4] = aRect.TopLeft();
}
else
{
const Point aTL( aRect.Left() + nHorzRound, aRect.Top() + nVertRound );
const Point aTR( aRect.Right() - nHorzRound, aRect.Top() + nVertRound );
const Point aBR( aRect.Right() - nHorzRound, aRect.Bottom() - nVertRound );
const Point aBL( aRect.Left() + nHorzRound, aRect.Bottom() - nVertRound );
tools::Polygon aEllipsePoly( Point(), nHorzRound, nVertRound );
sal_uInt16 i, nEnd, nSize4 = aEllipsePoly.GetSize() >> 2;
ImplInitSize(aEllipsePoly.GetSize() + 1);
const Point* pSrcAry = aEllipsePoly.GetConstPointAry();
Point* pDstAry = mxPointAry.get();
for( i = 0, nEnd = nSize4; i < nEnd; i++ )
pDstAry[ i ] = pSrcAry[ i ] + aTR;
for( nEnd = nEnd + nSize4; i < nEnd; i++ )
pDstAry[ i ] = pSrcAry[ i ] + aTL;
for( nEnd = nEnd + nSize4; i < nEnd; i++ )
pDstAry[ i ] = pSrcAry[ i ] + aBL;
for( nEnd = nEnd + nSize4; i < nEnd; i++ )
pDstAry[ i ] = pSrcAry[ i ] + aBR;
pDstAry[ nEnd ] = pDstAry[ 0 ];
}
}
else
mnPoints = 0;
}
ImplPolygon::ImplPolygon( const Point& rCenter, tools::Long nRadX, tools::Long nRadY )
{
if( nRadX && nRadY )
{
sal_uInt16 nPoints;
// Compute default (depends on size)
tools::Long nRadXY;
const bool bOverflow = o3tl::checked_multiply(nRadX, nRadY, nRadXY);
if (!bOverflow)
{
nPoints = std::clamp(
( M_PI * ( 1.5 * ( nRadX + nRadY ) -
sqrt( static_cast<double>(std::abs(nRadXY)) ) ) ),
32.0, 256.0 );
}
else
{
nPoints = 256;
}
if( ( nRadX > 32 ) && ( nRadY > 32 ) && ( nRadX + nRadY ) < 8192 )
nPoints >>= 1;
// Ceil number of points until divisible by four
nPoints = (nPoints + 3) & ~3;
ImplInitSize(nPoints);
sal_uInt16 i;
sal_uInt16 nPoints2 = nPoints >> 1;
sal_uInt16 nPoints4 = nPoints >> 2;
double nAngle;
double nAngleStep = M_PI_2 / ( nPoints4 - 1 );
for( i=0, nAngle = 0.0; i < nPoints4; i++, nAngle += nAngleStep )
{
tools::Long nX = FRound( nRadX * cos( nAngle ) );
tools::Long nY = FRound( -nRadY * sin( nAngle ) );
Point* pPt = &(mxPointAry[i]);
pPt->setX( nX + rCenter.X() );
pPt->setY( nY + rCenter.Y() );
pPt = &(mxPointAry[nPoints2-i-1]);
pPt->setX( -nX + rCenter.X() );
pPt->setY( nY + rCenter.Y() );
pPt = &(mxPointAry[i+nPoints2]);
pPt->setX( -nX + rCenter.X() );
pPt->setY( -nY + rCenter.Y() );
pPt = &(mxPointAry[nPoints-i-1]);
pPt->setX( nX + rCenter.X() );
pPt->setY( -nY + rCenter.Y() );
}
}
else
mnPoints = 0;
}
ImplPolygon::ImplPolygon(const tools::Rectangle& rBound, const Point& rStart, const Point& rEnd,
PolyStyle eStyle, const bool bClockWiseArcDirection)
{
const auto nWidth = rBound.GetWidth();
const auto nHeight = rBound.GetHeight();
if ((nWidth != 0) && (nHeight != 0))
{
const Point aCenter(rBound.Center());
// tdf#142268 Get Top Left corner of rectangle (the rectangle is not always correctly created)
const auto aBoundLeft = rBound.Left() < aCenter.X() ? rBound.Left() : rBound.Right();
const auto aBoundTop = rBound.Top() < aCenter.Y() ? rBound.Top() : rBound.Bottom();
const auto nRadX = o3tl::saturating_sub(aCenter.X(), aBoundLeft);
const auto nRadY = o3tl::saturating_sub(aCenter.Y(), aBoundTop);
sal_uInt16 nPoints;
tools::Long nRadXY;
const bool bOverflow = o3tl::checked_multiply(nRadX, nRadY, nRadXY);
if (!bOverflow)
{
nPoints = std::clamp(
( M_PI * ( 1.5 * ( nRadX + nRadY ) -
sqrt( static_cast<double>(std::abs(nRadXY)) ) ) ),
32.0, 256.0 );
}
else
{
nPoints = 256;
}
if (nRadX > 32 && nRadY > 32 && o3tl::saturating_add(nRadX, nRadY) < 8192)
nPoints >>= 1;
// compute threshold
const double fRadX = nRadX;
const double fRadY = nRadY;
const double fCenterX = aCenter.X();
const double fCenterY = aCenter.Y();
double fStart = ImplGetParameter( aCenter, rStart, fRadX, fRadY );
double fEnd = ImplGetParameter( aCenter, rEnd, fRadX, fRadY );
double fDiff = fEnd - fStart;
double fStep;
sal_uInt16 nStart;
sal_uInt16 nEnd;
if (bClockWiseArcDirection == false)
{
// #i73608# If startPoint is equal to endPoint, then draw full circle instead of nothing (as Metafiles spec)
if (fDiff <= 0.)
fDiff += 2. * M_PI;
}
else
{
fDiff = (2. * M_PI) - fDiff;
if (fDiff > 2. * M_PI)
fDiff -= 2. * M_PI;
}
// Proportionally shrink number of points( fDiff / (2PI) );
nPoints = std::max(static_cast<sal_uInt16>((fDiff / (2. * M_PI)) * nPoints), sal_uInt16(16));
fStep = fDiff / (nPoints - 1);
if (bClockWiseArcDirection == true)
fStep = -fStep;
if (PolyStyle::Pie == eStyle)
{
const Point aCenter2(FRound(fCenterX), FRound(fCenterY));
nStart = 1;
nEnd = nPoints + 1;
ImplInitSize(nPoints + 2);
mxPointAry[0] = aCenter2;
mxPointAry[nEnd] = aCenter2;
}
else
{
ImplInitSize( ( PolyStyle::Chord == eStyle ) ? ( nPoints + 1 ) : nPoints );
nStart = 0;
nEnd = nPoints;
}
for(; nStart < nEnd; nStart++, fStart += fStep )
{
Point& rPt = mxPointAry[nStart];
rPt.setX( FRound( fCenterX + fRadX * cos( fStart ) ) );
rPt.setY( FRound( fCenterY - fRadY * sin( fStart ) ) );
}
if( PolyStyle::Chord == eStyle )
mxPointAry[nPoints] = mxPointAry[0];
}
else
mnPoints = 0;
}
ImplPolygon::ImplPolygon( const Point& rBezPt1, const Point& rCtrlPt1,
const Point& rBezPt2, const Point& rCtrlPt2, sal_uInt16 nPoints )
{
nPoints = ( 0 == nPoints ) ? 25 : ( ( nPoints < 2 ) ? 2 : nPoints );
const double fInc = 1.0 / ( nPoints - 1 );
double fK_1 = 0.0, fK1_1 = 1.0;
double fK_2, fK_3, fK1_2, fK1_3;
const double fX0 = rBezPt1.X();
const double fY0 = rBezPt1.Y();
const double fX1 = 3.0 * rCtrlPt1.X();
const double fY1 = 3.0 * rCtrlPt1.Y();
const double fX2 = 3.0 * rCtrlPt2.X();
const double fY2 = 3.0 * rCtrlPt2.Y();
const double fX3 = rBezPt2.X();
const double fY3 = rBezPt2.Y();
ImplInitSize(nPoints);
for( sal_uInt16 i = 0; i < nPoints; i++, fK_1 += fInc, fK1_1 -= fInc )
{
Point& rPt = mxPointAry[i];
fK_2 = fK_1;
fK_2 *= fK_1;
fK_3 = fK_2;
fK_3 *= fK_1;
fK1_2 = fK1_1;
fK1_2 *= fK1_1;
fK1_3 = fK1_2;
fK1_3 *= fK1_1;
double fK12 = fK_1 * fK1_2;
double fK21 = fK_2 * fK1_1;
rPt.setX( FRound( fK1_3 * fX0 + fK12 * fX1 + fK21 * fX2 + fK_3 * fX3 ) );
rPt.setY( FRound( fK1_3 * fY0 + fK12 * fY1 + fK21 * fY2 + fK_3 * fY3 ) );
}
}
// constructor to convert from basegfx::B2DPolygon
// #i76891# Needed to change from adding all control points (even for unused
// edges) and creating a fixed-size Polygon in the first run to creating the
// minimal Polygon. This requires a temporary Point- and Flag-Array for curves
// and a memcopy at ImplPolygon creation, but contains no zero-controlpoints
// for straight edges.
ImplPolygon::ImplPolygon(const basegfx::B2DPolygon& rPolygon)
: mnPoints(0)
{
const bool bCurve(rPolygon.areControlPointsUsed());
const bool bClosed(rPolygon.isClosed());
sal_uInt32 nB2DLocalCount(rPolygon.count());
if(bCurve)
{
// #127979# Reduce source point count hard to the limit of the tools Polygon
if(nB2DLocalCount > ((0x0000ffff / 3) - 1))
{
OSL_FAIL("Polygon::Polygon: Too many points in given B2DPolygon, need to reduce hard to maximum of tools Polygon (!)");
nB2DLocalCount = ((0x0000ffff / 3) - 1);
}
// calculate target point count
const sal_uInt32 nLoopCount(bClosed ? nB2DLocalCount : (nB2DLocalCount ? nB2DLocalCount - 1 : 0 ));
if(nLoopCount)
{
// calculate maximum array size and allocate; prepare insert index
const sal_uInt32 nMaxTargetCount((nLoopCount * 3) + 1);
ImplInitSize(static_cast< sal_uInt16 >(nMaxTargetCount), true);
// prepare insert index and current point
sal_uInt32 nArrayInsert(0);
basegfx::B2DCubicBezier aBezier;
aBezier.setStartPoint(rPolygon.getB2DPoint(0));
for(sal_uInt32 a(0); a < nLoopCount; a++)
{
// add current point (always) and remember StartPointIndex for evtl. later corrections
const Point aStartPoint(FRound(aBezier.getStartPoint().getX()), FRound(aBezier.getStartPoint().getY()));
const sal_uInt32 nStartPointIndex(nArrayInsert);
mxPointAry[nStartPointIndex] = aStartPoint;
mxFlagAry[nStartPointIndex] = PolyFlags::Normal;
nArrayInsert++;
// prepare next segment
const sal_uInt32 nNextIndex((a + 1) % nB2DLocalCount);
aBezier.setEndPoint(rPolygon.getB2DPoint(nNextIndex));
aBezier.setControlPointA(rPolygon.getNextControlPoint(a));
aBezier.setControlPointB(rPolygon.getPrevControlPoint(nNextIndex));
if(aBezier.isBezier())
{
// if one is used, add always two control points due to the old schema
mxPointAry[nArrayInsert] = Point(FRound(aBezier.getControlPointA().getX()), FRound(aBezier.getControlPointA().getY()));
mxFlagAry[nArrayInsert] = PolyFlags::Control;
nArrayInsert++;
mxPointAry[nArrayInsert] = Point(FRound(aBezier.getControlPointB().getX()), FRound(aBezier.getControlPointB().getY()));
mxFlagAry[nArrayInsert] = PolyFlags::Control;
nArrayInsert++;
}
// test continuity with previous control point to set flag value
if(aBezier.getControlPointA() != aBezier.getStartPoint() && (bClosed || a))
{
const basegfx::B2VectorContinuity eCont(rPolygon.getContinuityInPoint(a));
if(basegfx::B2VectorContinuity::C1 == eCont)
{
mxFlagAry[nStartPointIndex] = PolyFlags::Smooth;
}
else if(basegfx::B2VectorContinuity::C2 == eCont)
{
mxFlagAry[nStartPointIndex] = PolyFlags::Symmetric;
}
}
// prepare next polygon step
aBezier.setStartPoint(aBezier.getEndPoint());
}
if(bClosed)
{
// add first point again as closing point due to old definition
mxPointAry[nArrayInsert] = mxPointAry[0];
mxFlagAry[nArrayInsert] = PolyFlags::Normal;
nArrayInsert++;
}
else
{
// add last point as closing point
const basegfx::B2DPoint aClosingPoint(rPolygon.getB2DPoint(nB2DLocalCount - 1));
const Point aEnd(FRound(aClosingPoint.getX()), FRound(aClosingPoint.getY()));
mxPointAry[nArrayInsert] = aEnd;
mxFlagAry[nArrayInsert] = PolyFlags::Normal;
nArrayInsert++;
}
DBG_ASSERT(nArrayInsert <= nMaxTargetCount, "Polygon::Polygon from basegfx::B2DPolygon: wrong max point count estimation (!)");
if(nArrayInsert != nMaxTargetCount)
{
ImplSetSize(static_cast< sal_uInt16 >(nArrayInsert));
}
}
}
else
{
// #127979# Reduce source point count hard to the limit of the tools Polygon
if(nB2DLocalCount > (0x0000ffff - 1))
{
OSL_FAIL("Polygon::Polygon: Too many points in given B2DPolygon, need to reduce hard to maximum of tools Polygon (!)");
nB2DLocalCount = (0x0000ffff - 1);
}
if(nB2DLocalCount)
{
// point list creation
const sal_uInt32 nTargetCount(nB2DLocalCount + (bClosed ? 1 : 0));
ImplInitSize(static_cast< sal_uInt16 >(nTargetCount));
sal_uInt16 nIndex(0);
for(sal_uInt32 a(0); a < nB2DLocalCount; a++)
{
basegfx::B2DPoint aB2DPoint(rPolygon.getB2DPoint(a));
Point aPoint(FRound(aB2DPoint.getX()), FRound(aB2DPoint.getY()));
mxPointAry[nIndex++] = aPoint;
}
if(bClosed)
{
// add first point as closing point
mxPointAry[nIndex] = mxPointAry[0];
}
}
}
}
bool ImplPolygon::operator==( const ImplPolygon& rCandidate) const
{
return mnPoints == rCandidate.mnPoints &&
mxFlagAry.get() == rCandidate.mxFlagAry.get() &&
mxPointAry.get() == rCandidate.mxPointAry.get();
}
void ImplPolygon::ImplInitSize(sal_uInt16 nInitSize, bool bFlags)
{
if (nInitSize)
{
mxPointAry.reset(new Point[nInitSize]);
}
if (bFlags)
{
mxFlagAry.reset(new PolyFlags[nInitSize]);
memset(mxFlagAry.get(), 0, nInitSize);
}
mnPoints = nInitSize;
}
void ImplPolygon::ImplSetSize( sal_uInt16 nNewSize, bool bResize )
{
if( mnPoints == nNewSize )
return;
std::unique_ptr<Point[]> xNewAry;
if (nNewSize)
{
const std::size_t nNewSz(static_cast<std::size_t>(nNewSize)*sizeof(Point));
xNewAry.reset(new Point[nNewSize]);
if ( bResize )
{
// Copy the old points
if ( mnPoints < nNewSize )
{
// New points are already implicitly initialized to zero
const std::size_t nOldSz(mnPoints * sizeof(Point));
if (mxPointAry)
memcpy(xNewAry.get(), mxPointAry.get(), nOldSz);
}
else
{
if (mxPointAry)
memcpy(xNewAry.get(), mxPointAry.get(), nNewSz);
}
}
}
mxPointAry = std::move(xNewAry);
// take FlagArray into account, if applicable
if( mxFlagAry )
{
std::unique_ptr<PolyFlags[]> xNewFlagAry;
if( nNewSize )
{
xNewFlagAry.reset(new PolyFlags[nNewSize]);
if( bResize )
{
// copy the old flags
if ( mnPoints < nNewSize )
{
// initialize new flags to zero
memset(xNewFlagAry.get() + mnPoints, 0, nNewSize-mnPoints);
memcpy(xNewFlagAry.get(), mxFlagAry.get(), mnPoints);
}
else
memcpy(xNewFlagAry.get(), mxFlagAry.get(), nNewSize);
}
}
mxFlagAry = std::move(xNewFlagAry);
}
mnPoints = nNewSize;
}
bool ImplPolygon::ImplSplit( sal_uInt16 nPos, sal_uInt16 nSpace, ImplPolygon const * pInitPoly )
{
//Can't fit this in :-(, throw ?
if (mnPoints + nSpace > USHRT_MAX)
{
SAL_WARN("tools", "Polygon needs " << mnPoints + nSpace << " points, but only " << USHRT_MAX << " possible");
return false;
}
const sal_uInt16 nNewSize = mnPoints + nSpace;
const std::size_t nSpaceSize = static_cast<std::size_t>(nSpace) * sizeof(Point);
if( nPos >= mnPoints )
{
// Append at the back
nPos = mnPoints;
ImplSetSize( nNewSize );
if( pInitPoly )
{
memcpy(mxPointAry.get() + nPos, pInitPoly->mxPointAry.get(), nSpaceSize);
if (pInitPoly->mxFlagAry)
memcpy(mxFlagAry.get() + nPos, pInitPoly->mxFlagAry.get(), nSpace);
}
}
else
{
const sal_uInt16 nSecPos = nPos + nSpace;
const sal_uInt16 nRest = mnPoints - nPos;
std::unique_ptr<Point[]> xNewAry(new Point[nNewSize]);
memcpy(xNewAry.get(), mxPointAry.get(), nPos * sizeof(Point));
if( pInitPoly )
memcpy(xNewAry.get() + nPos, pInitPoly->mxPointAry.get(), nSpaceSize);
memcpy(xNewAry.get() + nSecPos, mxPointAry.get() + nPos, nRest * sizeof(Point));
mxPointAry = std::move(xNewAry);
// consider FlagArray
if (mxFlagAry)
{
std::unique_ptr<PolyFlags[]> xNewFlagAry(new PolyFlags[nNewSize]);
memcpy(xNewFlagAry.get(), mxFlagAry.get(), nPos);
if (pInitPoly && pInitPoly->mxFlagAry)
memcpy(xNewFlagAry.get() + nPos, pInitPoly->mxFlagAry.get(), nSpace);
else
memset(xNewFlagAry.get() + nPos, 0, nSpace);
memcpy(xNewFlagAry.get() + nSecPos, mxFlagAry.get() + nPos, nRest);
mxFlagAry = std::move(xNewFlagAry);
}
mnPoints = nNewSize;
}
return true;
}
void ImplPolygon::ImplCreateFlagArray()
{
if (!mxFlagAry)
{
mxFlagAry.reset(new PolyFlags[mnPoints]);
memset(mxFlagAry.get(), 0, mnPoints);
}
}
namespace {
class ImplPointFilter
{
public:
virtual void LastPoint() = 0;
virtual void Input( const Point& rPoint ) = 0;
protected:
~ImplPointFilter() {}
};
class ImplPolygonPointFilter : public ImplPointFilter
{
ImplPolygon maPoly;
sal_uInt16 mnSize;
public:
explicit ImplPolygonPointFilter(sal_uInt16 nDestSize)
: maPoly(nDestSize)
, mnSize(0)
{
}
virtual ~ImplPolygonPointFilter()
{
}
virtual void LastPoint() override;
virtual void Input( const Point& rPoint ) override;
ImplPolygon& get() { return maPoly; }
};
}
void ImplPolygonPointFilter::Input( const Point& rPoint )
{
if ( !mnSize || (rPoint != maPoly.mxPointAry[mnSize-1]) )
{
mnSize++;
if ( mnSize > maPoly.mnPoints )
maPoly.ImplSetSize( mnSize );
maPoly.mxPointAry[mnSize-1] = rPoint;
}
}
void ImplPolygonPointFilter::LastPoint()
{
if ( mnSize < maPoly.mnPoints )
maPoly.ImplSetSize( mnSize );
};
namespace {
class ImplEdgePointFilter : public ImplPointFilter
{
Point maFirstPoint;
Point maLastPoint;
ImplPointFilter& mrNextFilter;
const tools::Long mnLow;
const tools::Long mnHigh;
const int mnEdge;
int mnLastOutside;
bool mbFirst;
public:
ImplEdgePointFilter( int nEdge, tools::Long nLow, tools::Long nHigh,
ImplPointFilter& rNextFilter ) :
mrNextFilter( rNextFilter ),
mnLow( nLow ),
mnHigh( nHigh ),
mnEdge( nEdge ),
mnLastOutside( 0 ),
mbFirst( true )
{
}
virtual ~ImplEdgePointFilter() {}
Point EdgeSection( const Point& rPoint, int nEdge ) const;
int VisibleSide( const Point& rPoint ) const;
bool IsPolygon() const
{ return maFirstPoint == maLastPoint; }
virtual void Input( const Point& rPoint ) override;
virtual void LastPoint() override;
};
}
inline int ImplEdgePointFilter::VisibleSide( const Point& rPoint ) const
{
if ( mnEdge & EDGE_HORZ )
{
return rPoint.X() < mnLow ? EDGE_LEFT :
rPoint.X() > mnHigh ? EDGE_RIGHT : 0;
}
else
{
return rPoint.Y() < mnLow ? EDGE_TOP :
rPoint.Y() > mnHigh ? EDGE_BOTTOM : 0;
}
}
Point ImplEdgePointFilter::EdgeSection( const Point& rPoint, int nEdge ) const
{
tools::Long lx = maLastPoint.X();
tools::Long ly = maLastPoint.Y();
tools::Long md = rPoint.X() - lx;
tools::Long mn = rPoint.Y() - ly;
tools::Long nNewX;
tools::Long nNewY;
if ( nEdge & EDGE_VERT )
{
nNewY = (nEdge == EDGE_TOP) ? mnLow : mnHigh;
tools::Long dy = nNewY - ly;
if ( !md )
nNewX = lx;
else if ( (LONG_MAX / std::abs(md)) >= std::abs(dy) )
nNewX = (dy * md) / mn + lx;
else
{
BigInt ady = dy;
ady *= md;
if( ady.IsNeg() )
if( mn < 0 )
ady += mn/2;
else
ady -= (mn-1)/2;
else
if( mn < 0 )
ady -= (mn+1)/2;
else
ady += mn/2;
ady /= mn;
nNewX = static_cast<tools::Long>(ady) + lx;
}
}
else
{
nNewX = (nEdge == EDGE_LEFT) ? mnLow : mnHigh;
tools::Long dx = nNewX - lx;
if ( !mn )
nNewY = ly;
else if ( (LONG_MAX / std::abs(mn)) >= std::abs(dx) )
nNewY = (dx * mn) / md + ly;
else
{
BigInt adx = dx;
adx *= mn;
if( adx.IsNeg() )
if( md < 0 )
adx += md/2;
else
adx -= (md-1)/2;
else
if( md < 0 )
adx -= (md+1)/2;
else
adx += md/2;
adx /= md;
nNewY = static_cast<tools::Long>(adx) + ly;
}
}
return Point( nNewX, nNewY );
}
void ImplEdgePointFilter::Input( const Point& rPoint )
{
int nOutside = VisibleSide( rPoint );
if ( mbFirst )
{
maFirstPoint = rPoint;
mbFirst = false;
if ( !nOutside )
mrNextFilter.Input( rPoint );
}
else if ( rPoint == maLastPoint )
return;
else if ( !nOutside )
{
if ( mnLastOutside )
mrNextFilter.Input( EdgeSection( rPoint, mnLastOutside ) );
mrNextFilter.Input( rPoint );
}
else if ( !mnLastOutside )
mrNextFilter.Input( EdgeSection( rPoint, nOutside ) );
else if ( nOutside != mnLastOutside )
{
mrNextFilter.Input( EdgeSection( rPoint, mnLastOutside ) );
mrNextFilter.Input( EdgeSection( rPoint, nOutside ) );
}
maLastPoint = rPoint;
mnLastOutside = nOutside;
}
void ImplEdgePointFilter::LastPoint()
{
if ( !mbFirst )
{
int nOutside = VisibleSide( maFirstPoint );
if ( nOutside != mnLastOutside )
Input( maFirstPoint );
mrNextFilter.LastPoint();
}
}
namespace tools
{
tools::Polygon Polygon::SubdivideBezier( const tools::Polygon& rPoly )
{
tools::Polygon aPoly;
// #100127# Use adaptive subdivide instead of fixed 25 segments
rPoly.AdaptiveSubdivide( aPoly );
return aPoly;
}
Polygon::Polygon() : mpImplPolygon(ImplPolygon())
{
}
Polygon::Polygon( sal_uInt16 nSize ) : mpImplPolygon(ImplPolygon(nSize))
{
}
Polygon::Polygon( sal_uInt16 nPoints, const Point* pPtAry, const PolyFlags* pFlagAry ) : mpImplPolygon(ImplPolygon(nPoints, pPtAry, pFlagAry))
{
}
Polygon::Polygon( const tools::Polygon& rPoly ) : mpImplPolygon(rPoly.mpImplPolygon)
{
}
Polygon::Polygon( tools::Polygon&& rPoly) noexcept
: mpImplPolygon(std::move(rPoly.mpImplPolygon))
{
}
Polygon::Polygon( const tools::Rectangle& rRect ) : mpImplPolygon(ImplPolygon(rRect))
{
}
Polygon::Polygon( const tools::Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
: mpImplPolygon(ImplPolygon(rRect, nHorzRound, nVertRound))
{
}
Polygon::Polygon( const Point& rCenter, tools::Long nRadX, tools::Long nRadY )
: mpImplPolygon(ImplPolygon(rCenter, nRadX, nRadY))
{
}
Polygon::Polygon(const tools::Rectangle& rBound, const Point& rStart, const Point& rEnd,
PolyStyle eStyle, const bool bClockWiseArcDirection)
: mpImplPolygon(ImplPolygon(rBound, rStart, rEnd, eStyle, bClockWiseArcDirection))
{
}
Polygon::Polygon( const Point& rBezPt1, const Point& rCtrlPt1,
const Point& rBezPt2, const Point& rCtrlPt2,
sal_uInt16 nPoints ) : mpImplPolygon(ImplPolygon(rBezPt1, rCtrlPt1, rBezPt2, rCtrlPt2, nPoints))
{
}
Polygon::~Polygon()
{
}
Point * Polygon::GetPointAry()
{
return mpImplPolygon->mxPointAry.get();
}
const Point* Polygon::GetConstPointAry() const
{
return mpImplPolygon->mxPointAry.get();
}
const PolyFlags* Polygon::GetConstFlagAry() const
{
return mpImplPolygon->mxFlagAry.get();
}
void Polygon::SetPoint( const Point& rPt, sal_uInt16 nPos )
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::SetPoint(): nPos >= nPoints" );
mpImplPolygon->mxPointAry[nPos] = rPt;
}
void Polygon::SetFlags( sal_uInt16 nPos, PolyFlags eFlags )
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::SetFlags(): nPos >= nPoints" );
// we do only want to create the flag array if there
// is at least one flag different to PolyFlags::Normal
if ( eFlags != PolyFlags::Normal )
{
mpImplPolygon->ImplCreateFlagArray();
mpImplPolygon->mxFlagAry[ nPos ] = eFlags;
}
}
const Point& Polygon::GetPoint( sal_uInt16 nPos ) const
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::GetPoint(): nPos >= nPoints" );
return mpImplPolygon->mxPointAry[nPos];
}
PolyFlags Polygon::GetFlags( sal_uInt16 nPos ) const
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::GetFlags(): nPos >= nPoints" );
return mpImplPolygon->mxFlagAry
? mpImplPolygon->mxFlagAry[ nPos ]
: PolyFlags::Normal;
}
bool Polygon::HasFlags() const
{
return bool(mpImplPolygon->mxFlagAry);
}
bool Polygon::IsRect() const
{
bool bIsRect = false;
if (!mpImplPolygon->mxFlagAry)
{
if ( ( ( mpImplPolygon->mnPoints == 5 ) && ( mpImplPolygon->mxPointAry[ 0 ] == mpImplPolygon->mxPointAry[ 4 ] ) ) ||
( mpImplPolygon->mnPoints == 4 ) )
{
if ( ( mpImplPolygon->mxPointAry[ 0 ].X() == mpImplPolygon->mxPointAry[ 3 ].X() ) &&
( mpImplPolygon->mxPointAry[ 0 ].Y() == mpImplPolygon->mxPointAry[ 1 ].Y() ) &&
( mpImplPolygon->mxPointAry[ 1 ].X() == mpImplPolygon->mxPointAry[ 2 ].X() ) &&
( mpImplPolygon->mxPointAry[ 2 ].Y() == mpImplPolygon->mxPointAry[ 3 ].Y() ) )
bIsRect = true;
}
}
return bIsRect;
}
void Polygon::SetSize( sal_uInt16 nNewSize )
{
if( nNewSize != mpImplPolygon->mnPoints )
{
mpImplPolygon->ImplSetSize( nNewSize );
}
}
sal_uInt16 Polygon::GetSize() const
{
return mpImplPolygon->mnPoints;
}
void Polygon::Clear()
{
mpImplPolygon = ImplType(ImplPolygon());
}
double Polygon::CalcDistance( sal_uInt16 nP1, sal_uInt16 nP2 ) const
{
DBG_ASSERT( nP1 < mpImplPolygon->mnPoints,
"Polygon::CalcDistance(): nPos1 >= nPoints" );
DBG_ASSERT( nP2 < mpImplPolygon->mnPoints,
"Polygon::CalcDistance(): nPos2 >= nPoints" );
const Point& rP1 = mpImplPolygon->mxPointAry[ nP1 ];
const Point& rP2 = mpImplPolygon->mxPointAry[ nP2 ];
const double fDx = rP2.X() - rP1.X();
const double fDy = rP2.Y() - rP1.Y();
return std::hypot( fDx, fDy );
}
void Polygon::Optimize( PolyOptimizeFlags nOptimizeFlags )
{
sal_uInt16 nSize = mpImplPolygon->mnPoints;
if( !(bool(nOptimizeFlags) && nSize) )
return;
if( nOptimizeFlags & PolyOptimizeFlags::EDGES )
{
const tools::Rectangle aBound( GetBoundRect() );
const double fArea = ( aBound.GetWidth() + aBound.GetHeight() ) * 0.5;
const sal_uInt16 nPercent = 50;
Optimize( PolyOptimizeFlags::NO_SAME );
ImplReduceEdges( *this, fArea, nPercent );
}
else if( nOptimizeFlags & PolyOptimizeFlags::NO_SAME )
{
tools::Polygon aNewPoly;
const Point& rFirst = mpImplPolygon->mxPointAry[ 0 ];
while( nSize && ( mpImplPolygon->mxPointAry[ nSize - 1 ] == rFirst ) )
nSize--;
if( nSize > 1 )
{
sal_uInt16 nLast = 0, nNewCount = 1;
aNewPoly.SetSize( nSize );
aNewPoly[ 0 ] = rFirst;
for( sal_uInt16 i = 1; i < nSize; i++ )
{
if( mpImplPolygon->mxPointAry[ i ] != mpImplPolygon->mxPointAry[ nLast ])
{
nLast = i;
aNewPoly[ nNewCount++ ] = mpImplPolygon->mxPointAry[ i ];
}
}
if( nNewCount == 1 )
aNewPoly.Clear();
else
aNewPoly.SetSize( nNewCount );
}
*this = aNewPoly;
}
nSize = mpImplPolygon->mnPoints;
if( nSize > 1 )
{
if( ( nOptimizeFlags & PolyOptimizeFlags::CLOSE ) &&
( mpImplPolygon->mxPointAry[ 0 ] != mpImplPolygon->mxPointAry[ nSize - 1 ] ) )
{
SetSize( mpImplPolygon->mnPoints + 1 );
mpImplPolygon->mxPointAry[ mpImplPolygon->mnPoints - 1 ] = mpImplPolygon->mxPointAry[ 0 ];
}
}
}
/** Recursively subdivide cubic bezier curve via deCasteljau.
@param rPoints
Output vector, where the subdivided polylines are written to.
@param d
Squared difference of curve to a straight line
@param P*
Exactly four points, interpreted as support and control points of
a cubic bezier curve. Must be in device coordinates, since stop
criterion is based on the following assumption: the device has a
finite resolution, it is thus sufficient to stop subdivision if the
curve does not deviate more than one pixel from a straight line.
*/
static void ImplAdaptiveSubdivide( std::vector<Point>& rPoints,
const double old_d2,
int recursionDepth,
const double d2,
const double P1x, const double P1y,
const double P2x, const double P2y,
const double P3x, const double P3y,
const double P4x, const double P4y )
{
// Hard limit on recursion depth, empiric number.
enum {maxRecursionDepth=128};
// Perform bezier flatness test (lecture notes from R. Schaback,
// Mathematics of Computer-Aided Design, Uni Goettingen, 2000)
// ||P(t) - L(t)|| <= max ||b_j - b_0 - j/n(b_n - b_0)||
// 0<=j<=n
// What is calculated here is an upper bound to the distance from
// a line through b_0 and b_3 (P1 and P4 in our notation) and the
// curve. We can drop 0 and n from the running indices, since the
// argument of max becomes zero for those cases.
const double fJ1x( P2x - P1x - 1.0/3.0*(P4x - P1x) );
const double fJ1y( P2y - P1y - 1.0/3.0*(P4y - P1y) );
const double fJ2x( P3x - P1x - 2.0/3.0*(P4x - P1x) );
const double fJ2y( P3y - P1y - 2.0/3.0*(P4y - P1y) );
const double distance2( ::std::max( fJ1x*fJ1x + fJ1y*fJ1y,
fJ2x*fJ2x + fJ2y*fJ2y) );
// stop if error measure does not improve anymore. This is a
// safety guard against floating point inaccuracies.
// stop at recursion level 128. This is a safety guard against
// floating point inaccuracies.
// stop if distance from line is guaranteed to be bounded by d
if( old_d2 > d2 &&
recursionDepth < maxRecursionDepth &&
distance2 >= d2 &&
rPoints.size() < SAL_MAX_UINT16 )
{
// deCasteljau bezier arc, split at t=0.5
// Foley/vanDam, p. 508
const double L1x( P1x ), L1y( P1y );
const double L2x( (P1x + P2x)*0.5 ), L2y( (P1y + P2y)*0.5 );
const double Hx ( (P2x + P3x)*0.5 ), Hy ( (P2y + P3y)*0.5 );
const double L3x( (L2x + Hx)*0.5 ), L3y( (L2y + Hy)*0.5 );
const double R4x( P4x ), R4y( P4y );
const double R3x( (P3x + P4x)*0.5 ), R3y( (P3y + P4y)*0.5 );
const double R2x( (Hx + R3x)*0.5 ), R2y( (Hy + R3y)*0.5 );
const double R1x( (L3x + R2x)*0.5 ), R1y( (L3y + R2y)*0.5 );
const double L4x( R1x ), L4y( R1y );
// subdivide further
++recursionDepth;
ImplAdaptiveSubdivide(rPoints, distance2, recursionDepth, d2, L1x, L1y, L2x, L2y, L3x, L3y, L4x, L4y);
ImplAdaptiveSubdivide(rPoints, distance2, recursionDepth, d2, R1x, R1y, R2x, R2y, R3x, R3y, R4x, R4y);
}
else
{
// requested resolution reached.
// Add end points to output iterator.
// order is preserved, since this is so to say depth first traversal.
rPoints.push_back(Point(FRound(P1x), FRound(P1y)));
}
}
void Polygon::AdaptiveSubdivide( Polygon& rResult, const double d ) const
{
if (!mpImplPolygon->mxFlagAry)
{
rResult = *this;
}
else
{
sal_uInt16 i;
sal_uInt16 nPts( GetSize() );
::std::vector< Point > aPoints;
aPoints.reserve( nPts );
for(i=0; i<nPts;)
{
if( ( i + 3 ) < nPts )
{
PolyFlags P1( mpImplPolygon->mxFlagAry[ i ] );
PolyFlags P4( mpImplPolygon->mxFlagAry[ i + 3 ] );
if( ( PolyFlags::Normal == P1 || PolyFlags::Smooth == P1 || PolyFlags::Symmetric == P1 ) &&
( PolyFlags::Control == mpImplPolygon->mxFlagAry[ i + 1 ] ) &&
( PolyFlags::Control == mpImplPolygon->mxFlagAry[ i + 2 ] ) &&
( PolyFlags::Normal == P4 || PolyFlags::Smooth == P4 || PolyFlags::Symmetric == P4 ) )
{
ImplAdaptiveSubdivide( aPoints, d*d+1.0, 0, d*d,
mpImplPolygon->mxPointAry[ i ].X(), mpImplPolygon->mxPointAry[ i ].Y(),
mpImplPolygon->mxPointAry[ i+1 ].X(), mpImplPolygon->mxPointAry[ i+1 ].Y(),
mpImplPolygon->mxPointAry[ i+2 ].X(), mpImplPolygon->mxPointAry[ i+2 ].Y(),
mpImplPolygon->mxPointAry[ i+3 ].X(), mpImplPolygon->mxPointAry[ i+3 ].Y() );
i += 3;
continue;
}
}
aPoints.push_back(mpImplPolygon->mxPointAry[i++]);
if (aPoints.size() >= SAL_MAX_UINT16)
{
OSL_ENSURE(aPoints.size() < SAL_MAX_UINT16,
"Polygon::AdaptiveSubdivision created polygon too many points;"
" using original polygon instead");
// The resulting polygon can not hold all the points
// that we have created so far. Stop the subdivision
// and return a copy of the unmodified polygon.
rResult = *this;
return;
}
}
// fill result polygon
rResult = tools::Polygon( static_cast<sal_uInt16>(aPoints.size()) ); // ensure sufficient size for copy
::std::copy(aPoints.begin(), aPoints.end(), rResult.mpImplPolygon->mxPointAry.get());
}
}
namespace {
class Vector2D
{
private:
double mfX;
double mfY;
public:
explicit Vector2D( const Point& rPoint ) : mfX( rPoint.X() ), mfY( rPoint.Y() ) {};
double GetLength() const { return hypot( mfX, mfY ); }
Vector2D& operator-=( const Vector2D& rVec ) { mfX -= rVec.mfX; mfY -= rVec.mfY; return *this; }
double Scalar( const Vector2D& rVec ) const { return mfX * rVec.mfX + mfY * rVec.mfY ; }
Vector2D& Normalize();
bool IsPositive( Vector2D const & rVec ) const { return ( mfX * rVec.mfY - mfY * rVec.mfX ) >= 0.0; }
bool IsNegative( Vector2D const & rVec ) const { return !IsPositive( rVec ); }
};
}
Vector2D& Vector2D::Normalize()
{
double fLen = Scalar( *this );
if( ( fLen != 0.0 ) && ( fLen != 1.0 ) )
{
fLen = sqrt( fLen );
if( fLen != 0.0 )
{
mfX /= fLen;
mfY /= fLen;
}
}
return *this;
}
void Polygon::ImplReduceEdges( tools::Polygon& rPoly, const double& rArea, sal_uInt16 nPercent )
{
const double fBound = 2000.0 * ( 100 - nPercent ) * 0.01;
sal_uInt16 nNumNoChange = 0,
nNumRuns = 0;
while( nNumNoChange < 2 )
{
sal_uInt16 nPntCnt = rPoly.GetSize(), nNewPos = 0;
tools::Polygon aNewPoly( nPntCnt );
bool bChangeInThisRun = false;
for( sal_uInt16 n = 0; n < nPntCnt; n++ )
{
bool bDeletePoint = false;
if( ( n + nNumRuns ) % 2 )
{
sal_uInt16 nIndPrev = !n ? nPntCnt - 1 : n - 1;
sal_uInt16 nIndPrevPrev = !nIndPrev ? nPntCnt - 1 : nIndPrev - 1;
sal_uInt16 nIndNext = ( n == nPntCnt-1 ) ? 0 : n + 1;
sal_uInt16 nIndNextNext = ( nIndNext == nPntCnt - 1 ) ? 0 : nIndNext + 1;
Vector2D aVec1( rPoly[ nIndPrev ] ); aVec1 -= Vector2D(rPoly[ nIndPrevPrev ]);
Vector2D aVec2( rPoly[ n ] ); aVec2 -= Vector2D(rPoly[ nIndPrev ]);
Vector2D aVec3( rPoly[ nIndNext ] ); aVec3 -= Vector2D(rPoly[ n ]);
Vector2D aVec4( rPoly[ nIndNextNext ] ); aVec4 -= Vector2D(rPoly[ nIndNext ]);
double fDist1 = aVec1.GetLength(), fDist2 = aVec2.GetLength();
double fDist3 = aVec3.GetLength(), fDist4 = aVec4.GetLength();
double fTurnB = aVec2.Normalize().Scalar( aVec3.Normalize() );
if( fabs( fTurnB ) < ( 1.0 + SMALL_DVALUE ) && fabs( fTurnB ) > ( 1.0 - SMALL_DVALUE ) )
bDeletePoint = true;
else
{
Vector2D aVecB( rPoly[ nIndNext ] );
aVecB -= Vector2D(rPoly[ nIndPrev ] );
double fDistB = aVecB.GetLength();
double fLenWithB = fDist2 + fDist3;
double fLenFact = ( fDistB != 0.0 ) ? fLenWithB / fDistB : 1.0;
double fTurnPrev = aVec1.Normalize().Scalar( aVec2 );
double fTurnNext = aVec3.Scalar( aVec4.Normalize() );
double fGradPrev, fGradB, fGradNext;
if( fabs( fTurnPrev ) < ( 1.0 + SMALL_DVALUE ) && fabs( fTurnPrev ) > ( 1.0 - SMALL_DVALUE ) )
fGradPrev = 0.0;
else
fGradPrev = basegfx::rad2deg(acos(fTurnPrev)) * (aVec1.IsNegative(aVec2) ? -1 : 1);
fGradB = basegfx::rad2deg(acos(fTurnB)) * (aVec2.IsNegative(aVec3) ? -1 : 1);
if( fabs( fTurnNext ) < ( 1.0 + SMALL_DVALUE ) && fabs( fTurnNext ) > ( 1.0 - SMALL_DVALUE ) )
fGradNext = 0.0;
else
fGradNext = basegfx::rad2deg(acos(fTurnNext)) * (aVec3.IsNegative(aVec4) ? -1 : 1);
if( ( fGradPrev > 0.0 && fGradB < 0.0 && fGradNext > 0.0 ) ||
( fGradPrev < 0.0 && fGradB > 0.0 && fGradNext < 0.0 ) )
{
if( ( fLenFact < ( FSQRT2 + SMALL_DVALUE ) ) &&
( ( ( fDist1 + fDist4 ) / ( fDist2 + fDist3 ) ) * 2000.0 ) > fBound )
{
bDeletePoint = true;
}
}
else
{
double fRelLen = 1.0 - sqrt( fDistB / rArea );
if( fRelLen < 0.0 )
fRelLen = 0.0;
else if( fRelLen > 1.0 )
fRelLen = 1.0;
if( ( std::round( ( fLenFact - 1.0 ) * 1000000.0 ) < fBound ) &&
( fabs( fGradB ) <= ( fRelLen * fBound * 0.01 ) ) )
{
bDeletePoint = true;
}
}
}
}
if( !bDeletePoint )
aNewPoly[ nNewPos++ ] = rPoly[ n ];
else
bChangeInThisRun = true;
}
if( bChangeInThisRun && nNewPos )
{
aNewPoly.SetSize( nNewPos );
rPoly = aNewPoly;
nNumNoChange = 0;
}
else
nNumNoChange++;
nNumRuns++;
}
}
void Polygon::Move( tools::Long nHorzMove, tools::Long nVertMove )
{
// This check is required for DrawEngine
if ( !nHorzMove && !nVertMove )
return;
// Move points
sal_uInt16 nCount = mpImplPolygon->mnPoints;
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
Point& rPt = mpImplPolygon->mxPointAry[i];
rPt.AdjustX(nHorzMove );
rPt.AdjustY(nVertMove );
}
}
void Polygon::Translate(const Point& rTrans)
{
for ( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
mpImplPolygon->mxPointAry[ i ] += rTrans;
}
void Polygon::Scale( double fScaleX, double fScaleY )
{
for ( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
{
Point& rPnt = mpImplPolygon->mxPointAry[i];
rPnt.setX( static_cast<tools::Long>( fScaleX * rPnt.X() ) );
rPnt.setY( static_cast<tools::Long>( fScaleY * rPnt.Y() ) );
}
}
void Polygon::Rotate( const Point& rCenter, Degree10 nAngle10 )
{
nAngle10 %= 3600_deg10;
if( nAngle10 )
{
const double fAngle = toRadians(nAngle10);
Rotate( rCenter, sin( fAngle ), cos( fAngle ) );
}
}
void Polygon::Rotate( const Point& rCenter, double fSin, double fCos )
{
tools::Long nCenterX = rCenter.X();
tools::Long nCenterY = rCenter.Y();
for( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
{
Point& rPt = mpImplPolygon->mxPointAry[ i ];
const tools::Long nX = rPt.X() - nCenterX;
const tools::Long nY = rPt.Y() - nCenterY;
rPt.setX( FRound(fCos * nX + fSin * nY + nCenterX) );
rPt.setY( FRound(-(fSin * nX - fCos * nY - nCenterY)) );
}
}
void Polygon::Clip( const tools::Rectangle& rRect )
{
// This algorithm is broken for bezier curves, they would get converted to lines.
// Use PolyPolygon::Clip().
assert( !HasFlags());
// #105251# Normalize rect before edge filtering
tools::Rectangle aJustifiedRect( rRect );
aJustifiedRect.Normalize();
sal_uInt16 nSourceSize = mpImplPolygon->mnPoints;
ImplPolygonPointFilter aPolygon( nSourceSize );
ImplEdgePointFilter aHorzFilter( EDGE_HORZ, aJustifiedRect.Left(), aJustifiedRect.Right(),
aPolygon );
ImplEdgePointFilter aVertFilter( EDGE_VERT, aJustifiedRect.Top(), aJustifiedRect.Bottom(),
aHorzFilter );
for ( sal_uInt16 i = 0; i < nSourceSize; i++ )
aVertFilter.Input( mpImplPolygon->mxPointAry[i] );
if ( aVertFilter.IsPolygon() )
aVertFilter.LastPoint();
else
aPolygon.LastPoint();
mpImplPolygon = ImplType(aPolygon.get());
}
tools::Rectangle Polygon::GetBoundRect() const
{
// Removing the assert. Bezier curves have the attribute that each single
// curve segment defined by four points can not exit the four-point polygon
// defined by that points. This allows to say that the curve segment can also
// never leave the Range of its defining points.
// The result is that Polygon::GetBoundRect() may not create the minimal
// BoundRect of the Polygon (to get that, use basegfx::B2DPolygon classes),
// but will always create a valid BoundRect, at least as long as this method
// 'blindly' travels over all points, including control points.
// DBG_ASSERT( !mpImplPolygon->mxFlagAry.get(), "GetBoundRect could fail with beziers!" );
sal_uInt16 nCount = mpImplPolygon->mnPoints;
if( ! nCount )
return tools::Rectangle();
tools::Long nXMin, nXMax, nYMin, nYMax;
const Point& pFirstPt = mpImplPolygon->mxPointAry[0];
nXMin = nXMax = pFirstPt.X();
nYMin = nYMax = pFirstPt.Y();
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
const Point& rPt = mpImplPolygon->mxPointAry[i];
if (rPt.X() < nXMin)
nXMin = rPt.X();
if (rPt.X() > nXMax)
nXMax = rPt.X();
if (rPt.Y() < nYMin)
nYMin = rPt.Y();
if (rPt.Y() > nYMax)
nYMax = rPt.Y();
}
return tools::Rectangle( nXMin, nYMin, nXMax, nYMax );
}
bool Polygon::Contains( const Point& rPoint ) const
{
DBG_ASSERT( !mpImplPolygon->mxFlagAry, "IsInside could fail with beziers!" );
const tools::Rectangle aBound( GetBoundRect() );
const Line aLine( rPoint, Point( aBound.Right() + 100, rPoint.Y() ) );
sal_uInt16 nCount = mpImplPolygon->mnPoints;
sal_uInt16 nPCounter = 0;
if ( ( nCount > 2 ) && aBound.Contains( rPoint ) )
{
Point aPt1( mpImplPolygon->mxPointAry[ 0 ] );
Point aIntersection;
Point aLastIntersection;
while ( ( aPt1 == mpImplPolygon->mxPointAry[ nCount - 1 ] ) && ( nCount > 3 ) )
nCount--;
for ( sal_uInt16 i = 1; i <= nCount; i++ )
{
const Point& rPt2 = mpImplPolygon->mxPointAry[ ( i < nCount ) ? i : 0 ];
if ( aLine.Intersection( Line( aPt1, rPt2 ), aIntersection ) )
{
// This avoids insertion of double intersections
if ( nPCounter )
{
if ( aIntersection != aLastIntersection )
{
aLastIntersection = aIntersection;
nPCounter++;
}
}
else
{
aLastIntersection = aIntersection;
nPCounter++;
}
}
aPt1 = rPt2;
}
}
// is inside, if number of intersection points is odd
return ( ( nPCounter & 1 ) == 1 );
}
void Polygon::Insert( sal_uInt16 nPos, const Point& rPt )
{
if( nPos >= mpImplPolygon->mnPoints )
nPos = mpImplPolygon->mnPoints;
if (mpImplPolygon->ImplSplit(nPos, 1))
mpImplPolygon->mxPointAry[ nPos ] = rPt;
}
void Polygon::Insert( sal_uInt16 nPos, const tools::Polygon& rPoly )
{
const sal_uInt16 nInsertCount = rPoly.mpImplPolygon->mnPoints;
if( nInsertCount )
{
if( nPos >= mpImplPolygon->mnPoints )
nPos = mpImplPolygon->mnPoints;
if (rPoly.mpImplPolygon->mxFlagAry)
mpImplPolygon->ImplCreateFlagArray();
mpImplPolygon->ImplSplit( nPos, nInsertCount, rPoly.mpImplPolygon.get() );
}
}
Point& Polygon::operator[]( sal_uInt16 nPos )
{
assert( nPos < mpImplPolygon->mnPoints );
return mpImplPolygon->mxPointAry[nPos];
}
tools::Polygon& Polygon::operator=( const tools::Polygon& rPoly )
{
mpImplPolygon = rPoly.mpImplPolygon;
return *this;
}
tools::Polygon& Polygon::operator=( tools::Polygon&& rPoly ) noexcept
{
mpImplPolygon = std::move(rPoly.mpImplPolygon);
return *this;
}
bool Polygon::operator==( const tools::Polygon& rPoly ) const
{
return (mpImplPolygon == rPoly.mpImplPolygon);
}
bool Polygon::IsEqual( const tools::Polygon& rPoly ) const
{
bool bIsEqual = true;
sal_uInt16 i;
if ( GetSize() != rPoly.GetSize() )
bIsEqual = false;
else
{
for ( i = 0; i < GetSize(); i++ )
{
if ( ( GetPoint( i ) != rPoly.GetPoint( i ) ) ||
( GetFlags( i ) != rPoly.GetFlags( i ) ) )
{
bIsEqual = false;
break;
}
}
}
return bIsEqual;
}
SvStream& ReadPolygon( SvStream& rIStream, tools::Polygon& rPoly )
{
sal_uInt16 nPoints(0);
// read all points and create array
rIStream.ReadUInt16( nPoints );
const size_t nMaxRecordsPossible = rIStream.remainingSize() / (2 * sizeof(sal_Int32));
if (nPoints > nMaxRecordsPossible)
{
SAL_WARN("tools", "Polygon claims " << nPoints << " records, but only " << nMaxRecordsPossible << " possible");
nPoints = nMaxRecordsPossible;
}
rPoly.mpImplPolygon->ImplSetSize( nPoints, false );
for (sal_uInt16 i = 0; i < nPoints; i++)
{
sal_Int32 nTmpX(0), nTmpY(0);
rIStream.ReadInt32(nTmpX).ReadInt32(nTmpY);
rPoly.mpImplPolygon->mxPointAry[i].setX(nTmpX);
rPoly.mpImplPolygon->mxPointAry[i].setY(nTmpY);
}
return rIStream;
}
SvStream& WritePolygon( SvStream& rOStream, const tools::Polygon& rPoly )
{
sal_uInt16 nPoints = rPoly.GetSize();
// Write number of points
rOStream.WriteUInt16( nPoints );
for (sal_uInt16 i = 0; i < nPoints; i++)
{
rOStream.WriteInt32(rPoly.mpImplPolygon->mxPointAry[i].X())
.WriteInt32(rPoly.mpImplPolygon->mxPointAry[i].Y());
}
return rOStream;
}
void Polygon::ImplRead( SvStream& rIStream )
{
sal_uInt8 bHasPolyFlags(0);
ReadPolygon( rIStream, *this );
rIStream.ReadUChar( bHasPolyFlags );
if ( bHasPolyFlags )
{
mpImplPolygon->mxFlagAry.reset(new PolyFlags[mpImplPolygon->mnPoints]);
auto nRead = rIStream.ReadBytes(mpImplPolygon->mxFlagAry.get(), mpImplPolygon->mnPoints);
if (nRead != mpImplPolygon->mnPoints)
{
SAL_WARN("tools", "Short read");
memset(mpImplPolygon->mxFlagAry.get() + nRead, 0, mpImplPolygon->mnPoints - nRead);
}
}
}
void Polygon::Read( SvStream& rIStream )
{
VersionCompatRead aCompat(rIStream);
ImplRead( rIStream );
}
void Polygon::ImplWrite( SvStream& rOStream ) const
{
bool bHasPolyFlags(mpImplPolygon->mxFlagAry);
WritePolygon( rOStream, *this );
rOStream.WriteBool(bHasPolyFlags);
if ( bHasPolyFlags )
rOStream.WriteBytes(mpImplPolygon->mxFlagAry.get(), mpImplPolygon->mnPoints);
}
void Polygon::Write( SvStream& rOStream ) const
{
VersionCompatWrite aCompat(rOStream, 1);
ImplWrite( rOStream );
}
// #i74631#/#i115917# numerical correction method for B2DPolygon
static void impCorrectContinuity(basegfx::B2DPolygon& roPolygon, sal_uInt32 nIndex, PolyFlags nCFlag)
{
const sal_uInt32 nPointCount(roPolygon.count());
OSL_ENSURE(nIndex < nPointCount, "impCorrectContinuity: index access out of range (!)");
if(nIndex >= nPointCount || (PolyFlags::Smooth != nCFlag && PolyFlags::Symmetric != nCFlag))
return;
if(!roPolygon.isPrevControlPointUsed(nIndex) || !roPolygon.isNextControlPointUsed(nIndex))
return;
// #i115917# Patch from osnola (modified, thanks for showing the problem)
// The correction is needed because an integer polygon with control points
// is converted to double precision. When C1 or C2 is used the involved vectors
// may not have the same directions/lengths since these come from integer coordinates
// and may have been snapped to different nearest integer coordinates. The snap error
// is in the range of +-1 in y and y, thus 0.0 <= error <= sqrt(2.0). Nonetheless,
// it needs to be corrected to be able to detect the continuity in this points
// correctly.
// We only have the integer data here (already in double precision form, but no mantissa
// used), so the best correction is to use:
// for C1: The longest vector since it potentially has best preserved the original vector.
// Even better the sum of the vectors, weighted by their length. This gives the
// normal vector addition to get the vector itself, lengths need to be preserved.
// for C2: The mediated vector(s) since both should be the same, but mirrored
// extract the point and vectors
const basegfx::B2DPoint aPoint(roPolygon.getB2DPoint(nIndex));
const basegfx::B2DVector aNext(roPolygon.getNextControlPoint(nIndex) - aPoint);
const basegfx::B2DVector aPrev(aPoint - roPolygon.getPrevControlPoint(nIndex));
// calculate common direction vector, normalize
const basegfx::B2DVector aDirection(aNext + aPrev);
const double fDirectionLen = aDirection.getLength();
if (fDirectionLen == 0.0)
return;
if (PolyFlags::Smooth == nCFlag)
{
// C1: apply common direction vector, preserve individual lengths
const double fInvDirectionLen(1.0 / fDirectionLen);
roPolygon.setNextControlPoint(nIndex, basegfx::B2DPoint(aPoint + (aDirection * (aNext.getLength() * fInvDirectionLen))));
roPolygon.setPrevControlPoint(nIndex, basegfx::B2DPoint(aPoint - (aDirection * (aPrev.getLength() * fInvDirectionLen))));
}
else // PolyFlags::Symmetric
{
// C2: get mediated length. Taking half of the unnormalized direction would be
// an approximation, but not correct.
const double fMedLength((aNext.getLength() + aPrev.getLength()) * (0.5 / fDirectionLen));
const basegfx::B2DVector aScaledDirection(aDirection * fMedLength);
// Bring Direction to correct length and apply
roPolygon.setNextControlPoint(nIndex, basegfx::B2DPoint(aPoint + aScaledDirection));
roPolygon.setPrevControlPoint(nIndex, basegfx::B2DPoint(aPoint - aScaledDirection));
}
}
// convert to basegfx::B2DPolygon and return
basegfx::B2DPolygon Polygon::getB2DPolygon() const
{
basegfx::B2DPolygon aRetval;
const sal_uInt16 nCount(mpImplPolygon->mnPoints);
if (nCount)
{
if (mpImplPolygon->mxFlagAry)
{
// handling for curves. Add start point
const Point aStartPoint(mpImplPolygon->mxPointAry[0]);
PolyFlags nPointFlag(mpImplPolygon->mxFlagAry[0]);
aRetval.append(basegfx::B2DPoint(aStartPoint.X(), aStartPoint.Y()));
Point aControlA, aControlB;
for(sal_uInt16 a(1); a < nCount;)
{
bool bControlA(false);
bool bControlB(false);
if(PolyFlags::Control == mpImplPolygon->mxFlagAry[a])
{
aControlA = mpImplPolygon->mxPointAry[a++];
bControlA = true;
}
if(a < nCount && PolyFlags::Control == mpImplPolygon->mxFlagAry[a])
{
aControlB = mpImplPolygon->mxPointAry[a++];
bControlB = true;
}
// assert invalid polygons
OSL_ENSURE(bControlA == bControlB, "Polygon::getB2DPolygon: Invalid source polygon (!)");
if(a < nCount)
{
const Point aEndPoint(mpImplPolygon->mxPointAry[a]);
if(bControlA)
{
// bezier edge, add
aRetval.appendBezierSegment(
basegfx::B2DPoint(aControlA.X(), aControlA.Y()),
basegfx::B2DPoint(aControlB.X(), aControlB.Y()),
basegfx::B2DPoint(aEndPoint.X(), aEndPoint.Y()));
impCorrectContinuity(aRetval, aRetval.count() - 2, nPointFlag);
}
else
{
// no bezier edge, add end point
aRetval.append(basegfx::B2DPoint(aEndPoint.X(), aEndPoint.Y()));
}
nPointFlag = mpImplPolygon->mxFlagAry[a++];
}
}
// if exist, remove double first/last points, set closed and correct control points
basegfx::utils::checkClosed(aRetval);
if(aRetval.isClosed())
{
// closeWithGeometryChange did really close, so last point(s) were removed.
// Correct the continuity in the changed point
impCorrectContinuity(aRetval, 0, mpImplPolygon->mxFlagAry[0]);
}
}
else
{
// extra handling for non-curves (most-used case) for speedup
for(sal_uInt16 a(0); a < nCount; a++)
{
// get point and add
const Point aPoint(mpImplPolygon->mxPointAry[a]);
aRetval.append(basegfx::B2DPoint(aPoint.X(), aPoint.Y()));
}
// set closed flag
basegfx::utils::checkClosed(aRetval);
}
}
return aRetval;
}
Polygon::Polygon(const basegfx::B2DPolygon& rPolygon) : mpImplPolygon(ImplPolygon(rPolygon))
{
}
} // namespace tools
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|