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
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
|
/* -*- 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 <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 <iterator>
#include <algorithm>
#include <cstring>
#include <limits.h>
#include <cmath>
#define EDGE_LEFT 1
#define EDGE_TOP 2
#define EDGE_RIGHT 4
#define EDGE_BOTTOM 8
#define EDGE_HORZ (EDGE_RIGHT | EDGE_LEFT)
#define EDGE_VERT (EDGE_TOP | EDGE_BOTTOM)
#define SMALL_DVALUE 0.0000001
#define FSQRT2 1.4142135623730950488016887242097
static ImplPolygonData aStaticImplPolygon =
{
nullptr, nullptr, 0, 0
};
ImplPolygon::ImplPolygon( sal_uInt16 nInitSize, bool bFlags )
{
if ( nInitSize )
{
const std::size_t nSz(static_cast<std::size_t>(nInitSize)*sizeof(Point));
mpPointAry = reinterpret_cast<Point*>(new char[nSz]);
memset( mpPointAry, 0, nSz );
}
else
mpPointAry = nullptr;
if( bFlags )
{
mpFlagAry = new sal_uInt8[ nInitSize ];
memset( mpFlagAry, 0, nInitSize );
}
else
mpFlagAry = nullptr;
mnRefCount = 1;
mnPoints = nInitSize;
}
ImplPolygon::ImplPolygon( const ImplPolygon& rImpPoly )
{
if ( rImpPoly.mnPoints )
{
const std::size_t nSz(static_cast<std::size_t>(rImpPoly.mnPoints)*sizeof(Point));
mpPointAry = reinterpret_cast<Point*>(new char[nSz]);
memcpy( mpPointAry, rImpPoly.mpPointAry, nSz );
if( rImpPoly.mpFlagAry )
{
mpFlagAry = new sal_uInt8[ rImpPoly.mnPoints ];
memcpy( mpFlagAry, rImpPoly.mpFlagAry, rImpPoly.mnPoints );
}
else
mpFlagAry = nullptr;
}
else
{
mpPointAry = nullptr;
mpFlagAry = nullptr;
}
mnRefCount = 1;
mnPoints = rImpPoly.mnPoints;
}
ImplPolygon::ImplPolygon( sal_uInt16 nInitSize, const Point* pInitAry, const sal_uInt8* pInitFlags )
{
if ( nInitSize )
{
const std::size_t nSz(static_cast<std::size_t>(nInitSize)*sizeof(Point));
mpPointAry = reinterpret_cast<Point*>(new char[nSz]);
memcpy( mpPointAry, pInitAry, nSz );
if( pInitFlags )
{
mpFlagAry = new sal_uInt8[ nInitSize ];
memcpy( mpFlagAry, pInitFlags, nInitSize );
}
else
mpFlagAry = nullptr;
}
else
{
mpPointAry = nullptr;
mpFlagAry = nullptr;
}
mnRefCount = 1;
mnPoints = nInitSize;
}
ImplPolygon::~ImplPolygon()
{
if ( mpPointAry )
{
delete[] reinterpret_cast<char*>(mpPointAry);
}
if( mpFlagAry )
delete[] mpFlagAry;
}
void ImplPolygon::ImplSetSize( sal_uInt16 nNewSize, bool bResize )
{
if( mnPoints == nNewSize )
return;
Point* pNewAry;
if ( nNewSize )
{
const std::size_t nNewSz(static_cast<std::size_t>(nNewSize)*sizeof(Point));
pNewAry = reinterpret_cast<Point*>(new char[nNewSz]);
if ( bResize )
{
// Copy the old points
if ( mnPoints < nNewSize )
{
// New points initialized to zero
const std::size_t nOldSz(static_cast<std::size_t>(mnPoints)*sizeof(Point));
memset( pNewAry+mnPoints, 0, nNewSz-nOldSz );
if ( mpPointAry )
memcpy( pNewAry, mpPointAry, nOldSz );
}
else
{
if ( mpPointAry )
memcpy( pNewAry, mpPointAry, nNewSz );
}
}
}
else
pNewAry = nullptr;
if ( mpPointAry )
delete[] reinterpret_cast<char*>(mpPointAry);
// ggf. FlagArray beruecksichtigen
if( mpFlagAry )
{
sal_uInt8* pNewFlagAry;
if( nNewSize )
{
pNewFlagAry = new sal_uInt8[ nNewSize ];
if( bResize )
{
// copy the old flags
if ( mnPoints < nNewSize )
{
// initialize new flags to zero
memset( pNewFlagAry+mnPoints, 0, nNewSize-mnPoints );
memcpy( pNewFlagAry, mpFlagAry, mnPoints );
}
else
memcpy( pNewFlagAry, mpFlagAry, nNewSize );
}
}
else
pNewFlagAry = nullptr;
delete[] mpFlagAry;
mpFlagAry = pNewFlagAry;
}
mpPointAry = pNewAry;
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( mpPointAry + nPos, pInitPoly->mpPointAry, nSpaceSize );
if( pInitPoly->mpFlagAry )
memcpy( mpFlagAry + nPos, pInitPoly->mpFlagAry, nSpace );
}
}
else
{
const sal_uInt16 nSecPos = nPos + nSpace;
const sal_uInt16 nRest = mnPoints - nPos;
Point* pNewAry = reinterpret_cast<Point*>(new char[ static_cast<std::size_t>(nNewSize) * sizeof(Point) ]);
memcpy( pNewAry, mpPointAry, nPos * sizeof( Point ) );
if( pInitPoly )
memcpy( pNewAry + nPos, pInitPoly->mpPointAry, nSpaceSize );
else
memset( pNewAry + nPos, 0, nSpaceSize );
memcpy( pNewAry + nSecPos, mpPointAry + nPos, nRest * sizeof( Point ) );
delete[] reinterpret_cast<char*>(mpPointAry);
// consider FlagArray
if( mpFlagAry )
{
sal_uInt8* pNewFlagAry = new sal_uInt8[ nNewSize ];
memcpy( pNewFlagAry, mpFlagAry, nPos );
if( pInitPoly && pInitPoly->mpFlagAry )
memcpy( pNewFlagAry + nPos, pInitPoly->mpFlagAry, nSpace );
else
memset( pNewFlagAry + nPos, 0, nSpace );
memcpy( pNewFlagAry + nSecPos, mpFlagAry + nPos, nRest );
delete[] mpFlagAry;
mpFlagAry = pNewFlagAry;
}
mpPointAry = pNewAry;
mnPoints = nNewSize;
}
return true;
}
void ImplPolygon::ImplCreateFlagArray()
{
if( !mpFlagAry )
{
mpFlagAry = new sal_uInt8[ mnPoints ];
memset( mpFlagAry, 0, mnPoints );
}
}
inline double ImplGetParameter( const Point& rCenter, const Point& rPt, double fWR, double fHR )
{
const long nDX = rPt.X() - rCenter.X();
double fAngle = atan2( -rPt.Y() + rCenter.Y(), ( ( nDX == 0L ) ? 0.000000001 : nDX ) );
return atan2(fWR*sin(fAngle), fHR*cos(fAngle));
}
class ImplPointFilter
{
public:
virtual void LastPoint() = 0;
virtual void Input( const Point& rPoint ) = 0;
protected:
~ImplPointFilter() {}
};
class ImplPolygonPointFilter : public ImplPointFilter
{
std::unique_ptr<ImplPolygon> mxPoly;
sal_uInt16 mnSize;
public:
explicit ImplPolygonPointFilter(sal_uInt16 nDestSize)
: mxPoly(new ImplPolygon(nDestSize))
, mnSize(0)
{
}
virtual ~ImplPolygonPointFilter()
{
}
virtual void LastPoint() override;
virtual void Input( const Point& rPoint ) override;
ImplPolygon* release() { return mxPoly.release(); }
};
void ImplPolygonPointFilter::Input( const Point& rPoint )
{
if ( !mnSize || (rPoint != mxPoly->mpPointAry[mnSize-1]) )
{
mnSize++;
if ( mnSize > mxPoly->mnPoints )
mxPoly->ImplSetSize( mnSize );
mxPoly->mpPointAry[mnSize-1] = rPoint;
}
}
void ImplPolygonPointFilter::LastPoint()
{
if ( mnSize < mxPoly->mnPoints )
mxPoly->ImplSetSize( mnSize );
};
class ImplEdgePointFilter : public ImplPointFilter
{
Point maFirstPoint;
Point maLastPoint;
ImplPointFilter& mrNextFilter;
const long mnLow;
const long mnHigh;
const int mnEdge;
int mnLastOutside;
bool mbFirst;
public:
ImplEdgePointFilter( int nEdge, long nLow, 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
{
long lx = maLastPoint.X();
long ly = maLastPoint.Y();
long md = rPoint.X() - lx;
long mn = rPoint.Y() - ly;
long nNewX;
long nNewY;
if ( nEdge & EDGE_VERT )
{
nNewY = (nEdge == EDGE_TOP) ? mnLow : mnHigh;
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 = (long)ady + lx;
}
}
else
{
nNewX = (nEdge == EDGE_LEFT) ? mnLow : mnHigh;
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 = (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;
}
inline void Polygon::ImplMakeUnique()
{
// copy references if any exist
if ( mpImplPolygon->mnRefCount != 1 )
{
if ( mpImplPolygon->mnRefCount )
mpImplPolygon->mnRefCount--;
mpImplPolygon = new ImplPolygon( *mpImplPolygon );
}
}
Polygon::Polygon()
{
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
Polygon::Polygon( sal_uInt16 nSize )
{
if ( nSize )
mpImplPolygon = new ImplPolygon( nSize );
else
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
Polygon::Polygon( sal_uInt16 nPoints, const Point* pPtAry, const sal_uInt8* pFlagAry )
{
if( nPoints )
mpImplPolygon = new ImplPolygon( nPoints, pPtAry, pFlagAry );
else
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
Polygon::Polygon( const tools::Polygon& rPoly )
{
DBG_ASSERT( rPoly.mpImplPolygon->mnRefCount < (SAL_MAX_UINT32-1), "Polygon: RefCount overflow" );
mpImplPolygon = rPoly.mpImplPolygon;
if ( mpImplPolygon->mnRefCount )
mpImplPolygon->mnRefCount++;
}
Polygon::Polygon( const Rectangle& rRect )
{
if ( rRect.IsEmpty() )
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
else
{
mpImplPolygon = new ImplPolygon( 5 );
mpImplPolygon->mpPointAry[0] = rRect.TopLeft();
mpImplPolygon->mpPointAry[1] = rRect.TopRight();
mpImplPolygon->mpPointAry[2] = rRect.BottomRight();
mpImplPolygon->mpPointAry[3] = rRect.BottomLeft();
mpImplPolygon->mpPointAry[4] = rRect.TopLeft();
}
}
Polygon::Polygon( const Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
{
if ( rRect.IsEmpty() )
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
else
{
Rectangle aRect( rRect );
aRect.Justify(); // SJ: i9140
nHorzRound = std::min( nHorzRound, static_cast<sal_uInt32>(labs( aRect.GetWidth() >> 1 )) );
nVertRound = std::min( nVertRound, static_cast<sal_uInt32>(labs( aRect.GetHeight() >> 1 )) );
if( !nHorzRound && !nVertRound )
{
mpImplPolygon = new ImplPolygon( 5 );
mpImplPolygon->mpPointAry[0] = aRect.TopLeft();
mpImplPolygon->mpPointAry[1] = aRect.TopRight();
mpImplPolygon->mpPointAry[2] = aRect.BottomRight();
mpImplPolygon->mpPointAry[3] = aRect.BottomLeft();
mpImplPolygon->mpPointAry[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 );
std::unique_ptr<tools::Polygon> pEllipsePoly( new tools::Polygon( Point(), nHorzRound, nVertRound ) );
sal_uInt16 i, nEnd, nSize4 = pEllipsePoly->GetSize() >> 2;
mpImplPolygon = new ImplPolygon( pEllipsePoly->GetSize() + 1 );
const Point* pSrcAry = pEllipsePoly->GetConstPointAry();
Point* pDstAry = mpImplPolygon->mpPointAry;
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 ];
}
}
}
Polygon::Polygon( const Point& rCenter, long nRadX, long nRadY )
{
if( nRadX && nRadY )
{
sal_uInt16 nPoints = 0;
// Compute default (depends on size)
nPoints = (sal_uInt16) MinMax(
( F_PI * ( 1.5 * ( nRadX + nRadY ) -
sqrt( (double) labs( nRadX * nRadY ) ) ) ),
32, 256 );
if( ( nRadX > 32 ) && ( nRadY > 32 ) && ( nRadX + nRadY ) < 8192 )
nPoints >>= 1;
// Ceil number of points until divisible by four
mpImplPolygon = new ImplPolygon( nPoints = (nPoints + 3) & ~3 );
sal_uInt16 i;
sal_uInt16 nPoints2 = nPoints >> 1;
sal_uInt16 nPoints4 = nPoints >> 2;
double nAngle;
double nAngleStep = F_PI2 / ( nPoints4 - 1 );
for( i=0, nAngle = 0.0; i < nPoints4; i++, nAngle += nAngleStep )
{
long nX = FRound( nRadX * cos( nAngle ) );
long nY = FRound( -nRadY * sin( nAngle ) );
Point* pPt = &(mpImplPolygon->mpPointAry[i]);
pPt->X() = nX + rCenter.X();
pPt->Y() = nY + rCenter.Y();
pPt = &(mpImplPolygon->mpPointAry[nPoints2-i-1]);
pPt->X() = -nX + rCenter.X();
pPt->Y() = nY + rCenter.Y();
pPt = &(mpImplPolygon->mpPointAry[i+nPoints2]);
pPt->X() = -nX + rCenter.X();
pPt->Y() = -nY + rCenter.Y();
pPt = &(mpImplPolygon->mpPointAry[nPoints-i-1]);
pPt->X() = nX + rCenter.X();
pPt->Y() = -nY + rCenter.Y();
}
}
else
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
Polygon::Polygon( const Rectangle& rBound, const Point& rStart, const Point& rEnd,
PolyStyle eStyle, bool bFullCircle )
{
const long nWidth = rBound.GetWidth();
const long nHeight = rBound.GetHeight();
if( ( nWidth > 1 ) && ( nHeight > 1 ) )
{
const Point aCenter( rBound.Center() );
const long nRadX = aCenter.X() - rBound.Left();
const long nRadY = aCenter.Y() - rBound.Top();
sal_uInt16 nPoints;
nPoints = (sal_uInt16) MinMax(
( F_PI * ( 1.5 * ( nRadX + nRadY ) -
sqrt( (double) labs( nRadX * nRadY ) ) ) ),
32, 256 );
if( ( nRadX > 32 ) && ( nRadY > 32 ) && ( 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( fDiff < 0. )
fDiff += F_2PI;
if ( bFullCircle )
fDiff = F_2PI;
// Proportionally shrink number of points( fDiff / (2PI) );
nPoints = std::max( (sal_uInt16) ( ( fDiff * 0.1591549 ) * nPoints ), (sal_uInt16) 16 );
fStep = fDiff / ( nPoints - 1 );
if( POLY_PIE == eStyle )
{
const Point aCenter2( FRound( fCenterX ), FRound( fCenterY ) );
nStart = 1;
nEnd = nPoints + 1;
mpImplPolygon = new ImplPolygon( nPoints + 2 );
mpImplPolygon->mpPointAry[ 0 ] = aCenter2;
mpImplPolygon->mpPointAry[ nEnd ] = aCenter2;
}
else
{
mpImplPolygon = new ImplPolygon( ( POLY_CHORD == eStyle ) ? ( nPoints + 1 ) : nPoints );
nStart = 0;
nEnd = nPoints;
}
for(; nStart < nEnd; nStart++, fStart += fStep )
{
Point& rPt = mpImplPolygon->mpPointAry[ nStart ];
rPt.X() = FRound( fCenterX + fRadX * cos( fStart ) );
rPt.Y() = FRound( fCenterY - fRadY * sin( fStart ) );
}
if( POLY_CHORD == eStyle )
mpImplPolygon->mpPointAry[ nPoints ] = mpImplPolygon->mpPointAry[ 0 ];
}
else
mpImplPolygon = static_cast<ImplPolygon*>( &aStaticImplPolygon );
}
Polygon::Polygon( 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();
mpImplPolygon = new ImplPolygon( nPoints );
for( sal_uInt16 i = 0; i < nPoints; i++, fK_1 += fInc, fK1_1 -= fInc )
{
Point& rPt = mpImplPolygon->mpPointAry[ i ];
fK_2 = fK_1;
fK_3 = ( fK_2 *= fK_1 );
fK_3 *= fK_1;
fK1_2 = fK1_1;
fK1_3 = ( fK1_2 *= fK1_1 );
fK1_3 *= fK1_1;
double fK12 = fK_1 * fK1_2;
double fK21 = fK_2 * fK1_1;
rPt.X() = FRound( fK1_3 * fX0 + fK12 * fX1 + fK21 * fX2 + fK_3 * fX3 );
rPt.Y() = FRound( fK1_3 * fY0 + fK12 * fY1 + fK21 * fY2 + fK_3 * fY3 );
}
}
Polygon::~Polygon()
{
// Remove if refcount == 0, otherwise decrement refcount
if ( mpImplPolygon->mnRefCount )
{
if ( mpImplPolygon->mnRefCount > 1 )
mpImplPolygon->mnRefCount--;
else
delete mpImplPolygon;
}
}
const Point* Polygon::GetConstPointAry() const
{
return mpImplPolygon->mpPointAry;
}
const sal_uInt8* Polygon::GetConstFlagAry() const
{
return mpImplPolygon->mpFlagAry;
}
void Polygon::SetPoint( const Point& rPt, sal_uInt16 nPos )
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::SetPoint(): nPos >= nPoints" );
ImplMakeUnique();
mpImplPolygon->mpPointAry[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 POLY_NORMAL
if ( eFlags != POLY_NORMAL )
{
ImplMakeUnique();
mpImplPolygon->ImplCreateFlagArray();
mpImplPolygon->mpFlagAry[ nPos ] = (sal_uInt8) eFlags;
}
}
const Point& Polygon::GetPoint( sal_uInt16 nPos ) const
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::GetPoint(): nPos >= nPoints" );
return mpImplPolygon->mpPointAry[nPos];
}
PolyFlags Polygon::GetFlags( sal_uInt16 nPos ) const
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints,
"Polygon::GetFlags(): nPos >= nPoints" );
return( mpImplPolygon->mpFlagAry ?
(PolyFlags) mpImplPolygon->mpFlagAry[ nPos ] :
POLY_NORMAL );
}
bool Polygon::HasFlags() const
{
return mpImplPolygon->mpFlagAry != nullptr;
}
bool Polygon::IsRect() const
{
bool bIsRect = false;
if ( mpImplPolygon->mpFlagAry == nullptr )
{
if ( ( ( mpImplPolygon->mnPoints == 5 ) && ( mpImplPolygon->mpPointAry[ 0 ] == mpImplPolygon->mpPointAry[ 4 ] ) ) ||
( mpImplPolygon->mnPoints == 4 ) )
{
if ( ( mpImplPolygon->mpPointAry[ 0 ].X() == mpImplPolygon->mpPointAry[ 3 ].X() ) &&
( mpImplPolygon->mpPointAry[ 0 ].Y() == mpImplPolygon->mpPointAry[ 1 ].Y() ) &&
( mpImplPolygon->mpPointAry[ 1 ].X() == mpImplPolygon->mpPointAry[ 2 ].X() ) &&
( mpImplPolygon->mpPointAry[ 2 ].Y() == mpImplPolygon->mpPointAry[ 3 ].Y() ) )
bIsRect = true;
}
}
return bIsRect;
}
void Polygon::SetSize( sal_uInt16 nNewSize )
{
if( nNewSize != mpImplPolygon->mnPoints )
{
ImplMakeUnique();
mpImplPolygon->ImplSetSize( nNewSize );
}
}
sal_uInt16 Polygon::GetSize() const
{
return mpImplPolygon->mnPoints;
}
void Polygon::Clear()
{
if ( mpImplPolygon->mnRefCount )
{
if ( mpImplPolygon->mnRefCount > 1 )
mpImplPolygon->mnRefCount--;
else
delete mpImplPolygon;
}
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
double Polygon::CalcDistance( sal_uInt16 nP1, sal_uInt16 nP2 )
{
DBG_ASSERT( nP1 < mpImplPolygon->mnPoints,
"Polygon::CalcDistance(): nPos1 >= nPoints" );
DBG_ASSERT( nP2 < mpImplPolygon->mnPoints,
"Polygon::CalcDistance(): nPos2 >= nPoints" );
const Point& rP1 = mpImplPolygon->mpPointAry[ nP1 ];
const Point& rP2 = mpImplPolygon->mpPointAry[ nP2 ];
const double fDx = rP2.X() - rP1.X();
const double fDy = rP2.Y() - rP1.Y();
return sqrt( fDx * fDx + fDy * fDy );
}
void Polygon::Optimize( PolyOptimizeFlags nOptimizeFlags )
{
DBG_ASSERT( !mpImplPolygon->mpFlagAry, "Optimizing could fail with beziers!" );
sal_uInt16 nSize = mpImplPolygon->mnPoints;
if( bool(nOptimizeFlags) && nSize )
{
if( nOptimizeFlags & PolyOptimizeFlags::EDGES )
{
const 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::REDUCE | PolyOptimizeFlags::NO_SAME ) )
{
tools::Polygon aNewPoly;
const Point& rFirst = mpImplPolygon->mpPointAry[ 0 ];
const long nReduce = ( nOptimizeFlags & PolyOptimizeFlags::REDUCE ) ? 4 : 0;
while( nSize && ( mpImplPolygon->mpPointAry[ 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->mpPointAry[ i ] != mpImplPolygon->mpPointAry[ nLast ] ) &&
( !nReduce || ( nReduce < FRound( CalcDistance( nLast, i ) ) ) ) )
{
aNewPoly[ nNewCount++ ] = mpImplPolygon->mpPointAry[ nLast = i ];
}
}
if( nNewCount == 1 )
aNewPoly.Clear();
else
aNewPoly.SetSize( nNewCount );
}
*this = aNewPoly;
}
nSize = mpImplPolygon->mnPoints;
if( nSize > 1 )
{
if( ( nOptimizeFlags & PolyOptimizeFlags::CLOSE ) &&
( mpImplPolygon->mpPointAry[ 0 ] != mpImplPolygon->mpPointAry[ nSize - 1 ] ) )
{
SetSize( mpImplPolygon->mnPoints + 1 );
mpImplPolygon->mpPointAry[ mpImplPolygon->mnPoints - 1 ] = mpImplPolygon->mpPointAry[ 0 ];
}
else if( ( nOptimizeFlags & PolyOptimizeFlags::OPEN ) &&
( mpImplPolygon->mpPointAry[ 0 ] == mpImplPolygon->mpPointAry[ nSize - 1 ] ) )
{
const Point& rFirst = mpImplPolygon->mpPointAry[ 0 ];
while( nSize && ( mpImplPolygon->mpPointAry[ nSize - 1 ] == rFirst ) )
nSize--;
SetSize( nSize );
}
}
}
}
/** Recursively subdivide cubic bezier curve via deCasteljau.
@param rPointIter
Output iterator, 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::back_insert_iterator< ::std::vector< Point > >& rPointIter,
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 )
{
// 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(rPointIter, distance2, recursionDepth, d2, L1x, L1y, L2x, L2y, L3x, L3y, L4x, L4y);
ImplAdaptiveSubdivide(rPointIter, 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.
*rPointIter++ = Point( FRound(P1x), FRound(P1y) );
}
}
void Polygon::AdaptiveSubdivide( Polygon& rResult, const double d ) const
{
if( !mpImplPolygon->mpFlagAry )
{
rResult = *this;
}
else
{
sal_uInt16 i;
sal_uInt16 nPts( GetSize() );
::std::vector< Point > aPoints;
aPoints.reserve( nPts );
::std::back_insert_iterator< ::std::vector< Point > > aPointIter( aPoints );
for(i=0; i<nPts;)
{
if( ( i + 3 ) < nPts )
{
sal_uInt8 P1( mpImplPolygon->mpFlagAry[ i ] );
sal_uInt8 P4( mpImplPolygon->mpFlagAry[ i + 3 ] );
if( ( POLY_NORMAL == P1 || POLY_SMOOTH == P1 || POLY_SYMMTR == P1 ) &&
( POLY_CONTROL == mpImplPolygon->mpFlagAry[ i + 1 ] ) &&
( POLY_CONTROL == mpImplPolygon->mpFlagAry[ i + 2 ] ) &&
( POLY_NORMAL == P4 || POLY_SMOOTH == P4 || POLY_SYMMTR == P4 ) )
{
ImplAdaptiveSubdivide( aPointIter, d*d+1.0, 0, d*d,
mpImplPolygon->mpPointAry[ i ].X(), mpImplPolygon->mpPointAry[ i ].Y(),
mpImplPolygon->mpPointAry[ i+1 ].X(), mpImplPolygon->mpPointAry[ i+1 ].Y(),
mpImplPolygon->mpPointAry[ i+2 ].X(), mpImplPolygon->mpPointAry[ i+2 ].Y(),
mpImplPolygon->mpPointAry[ i+3 ].X(), mpImplPolygon->mpPointAry[ i+3 ].Y() );
i += 3;
continue;
}
}
*aPointIter++ = mpImplPolygon->mpPointAry[ i++ ];
if (aPoints.size() >= SAL_MAX_UINT16)
{
OSL_ENSURE(aPoints.size() < SAL_MAX_UINT16,
"Polygon::AdapativeSubdivision 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( (sal_uInt16)aPoints.size() ); // ensure sufficient size for copy
::std::copy(aPoints.begin(), aPoints.end(), rResult.mpImplPolygon->mpPointAry);
}
}
class Vector2D
{
private:
double mfX;
double mfY;
public:
explicit Vector2D( const Point& rPair ) : mfX( rPair.A() ), mfY( rPair.B() ) {};
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 ) ) != 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 ] );
double fDistB = ( aVecB -= Vector2D(rPoly[ nIndPrev ] )).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 = acos( fTurnPrev ) / ( aVec1.IsNegative( aVec2 ) ? -F_PI180 : F_PI180 );
fGradB = acos( fTurnB ) / ( aVec2.IsNegative( aVec3 ) ? -F_PI180 : F_PI180 );
if( fabs( fTurnNext ) < ( 1.0 + SMALL_DVALUE ) && fabs( fTurnNext ) > ( 1.0 - SMALL_DVALUE ) )
fGradNext = 0.0;
else
fGradNext = acos( fTurnNext ) / ( aVec3.IsNegative( aVec4 ) ? -F_PI180 : F_PI180 );
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( ( (sal_uInt32) ( ( ( fLenFact - 1.0 ) * 1000000.0 ) + 0.5 ) < 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( long nHorzMove, long nVertMove )
{
// This check is required for DrawEngine
if ( !nHorzMove && !nVertMove )
return;
ImplMakeUnique();
// Move points
sal_uInt16 nCount = mpImplPolygon->mnPoints;
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
Point* pPt = &(mpImplPolygon->mpPointAry[i]);
pPt->X() += nHorzMove;
pPt->Y() += nVertMove;
}
}
void Polygon::Translate(const Point& rTrans)
{
ImplMakeUnique();
for ( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
mpImplPolygon->mpPointAry[ i ] += rTrans;
}
void Polygon::Scale( double fScaleX, double fScaleY )
{
ImplMakeUnique();
for ( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
{
Point& rPnt = mpImplPolygon->mpPointAry[i];
rPnt.X() = (long) ( fScaleX * rPnt.X() );
rPnt.Y() = (long) ( fScaleY * rPnt.Y() );
}
}
void Polygon::Rotate( const Point& rCenter, sal_uInt16 nAngle10 )
{
nAngle10 %= 3600;
if( nAngle10 )
{
const double fAngle = F_PI1800 * nAngle10;
Rotate( rCenter, sin( fAngle ), cos( fAngle ) );
}
}
void Polygon::Rotate( const Point& rCenter, double fSin, double fCos )
{
ImplMakeUnique();
long nCenterX = rCenter.X();
long nCenterY = rCenter.Y();
for( sal_uInt16 i = 0, nCount = mpImplPolygon->mnPoints; i < nCount; i++ )
{
Point& rPt = mpImplPolygon->mpPointAry[ i ];
const long nX = rPt.X() - nCenterX;
const long nY = rPt.Y() - nCenterY;
rPt.X() = (long) FRound( fCos * nX + fSin * nY ) + nCenterX;
rPt.Y() = -(long) FRound( fSin * nX - fCos * nY ) + nCenterY;
}
}
void Polygon::Clip( const Rectangle& rRect )
{
// #105251# Justify rect before edge filtering
Rectangle aJustifiedRect( rRect );
aJustifiedRect.Justify();
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->mpPointAry[i] );
if ( aVertFilter.IsPolygon() )
aVertFilter.LastPoint();
else
aPolygon.LastPoint();
// Delete old ImpPolygon-data and assign from ImpPolygonPointFilter
if ( mpImplPolygon->mnRefCount )
{
if ( mpImplPolygon->mnRefCount > 1 )
mpImplPolygon->mnRefCount--;
else
delete mpImplPolygon;
}
mpImplPolygon = aPolygon.release();
}
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->mpFlagAry, "GetBoundRect could fail with beziers!" );
sal_uInt16 nCount = mpImplPolygon->mnPoints;
if( ! nCount )
return Rectangle();
long nXMin, nXMax, nYMin, nYMax;
const Point* pPt = &(mpImplPolygon->mpPointAry[0]);
nXMin = nXMax = pPt->X();
nYMin = nYMax = pPt->Y();
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
pPt = &(mpImplPolygon->mpPointAry[i]);
if ( pPt->X() < nXMin )
nXMin = pPt->X();
if ( pPt->X() > nXMax )
nXMax = pPt->X();
if ( pPt->Y() < nYMin )
nYMin = pPt->Y();
if ( pPt->Y() > nYMax )
nYMax = pPt->Y();
}
return Rectangle( nXMin, nYMin, nXMax, nYMax );
}
double Polygon::GetSignedArea() const
{
DBG_ASSERT( !mpImplPolygon->mpFlagAry, "GetArea could fail with beziers!" );
double fArea = 0.0;
if( mpImplPolygon->mnPoints > 2 )
{
const sal_uInt16 nCount1 = mpImplPolygon->mnPoints - 1;
for( sal_uInt16 i = 0; i < nCount1; )
{
const Point& rPt = mpImplPolygon->mpPointAry[ i ];
const Point& rPt1 = mpImplPolygon->mpPointAry[ ++i ];
fArea += ( rPt.X() - rPt1.X() ) * ( rPt.Y() + rPt1.Y() );
}
const Point& rPt = mpImplPolygon->mpPointAry[ nCount1 ];
const Point& rPt0 = mpImplPolygon->mpPointAry[ 0 ];
fArea += ( rPt.X() - rPt0.X() ) * ( rPt.Y() + rPt0.Y() );
}
return fArea;
}
bool Polygon::IsInside( const Point& rPoint ) const
{
DBG_ASSERT( !mpImplPolygon->mpFlagAry, "IsInside could fail with beziers!" );
const Rectangle aBound( GetBoundRect() );
const Line aLine( rPoint, Point( aBound.Right() + 100L, rPoint.Y() ) );
sal_uInt16 nCount = mpImplPolygon->mnPoints;
sal_uInt16 nPCounter = 0;
if ( ( nCount > 2 ) && aBound.IsInside( rPoint ) )
{
Point aPt1( mpImplPolygon->mpPointAry[ 0 ] );
Point aIntersection;
Point aLastIntersection;
while ( ( aPt1 == mpImplPolygon->mpPointAry[ nCount - 1 ] ) && ( nCount > 3 ) )
nCount--;
for ( sal_uInt16 i = 1; i <= nCount; i++ )
{
const Point& rPt2 = mpImplPolygon->mpPointAry[ ( 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 );
}
bool Polygon::IsRightOrientated() const
{
return GetSignedArea() >= 0.0;
}
void Polygon::Insert( sal_uInt16 nPos, const Point& rPt )
{
ImplMakeUnique();
if( nPos >= mpImplPolygon->mnPoints )
nPos = mpImplPolygon->mnPoints;
if (mpImplPolygon->ImplSplit(nPos, 1))
mpImplPolygon->mpPointAry[ nPos ] = rPt;
}
void Polygon::Insert( sal_uInt16 nPos, const tools::Polygon& rPoly )
{
const sal_uInt16 nInsertCount = rPoly.mpImplPolygon->mnPoints;
if( nInsertCount )
{
ImplMakeUnique();
if( nPos >= mpImplPolygon->mnPoints )
nPos = mpImplPolygon->mnPoints;
if( rPoly.mpImplPolygon->mpFlagAry )
mpImplPolygon->ImplCreateFlagArray();
mpImplPolygon->ImplSplit( nPos, nInsertCount, rPoly.mpImplPolygon );
}
}
Point& Polygon::operator[]( sal_uInt16 nPos )
{
DBG_ASSERT( nPos < mpImplPolygon->mnPoints, "Polygon::[]: nPos >= nPoints" );
ImplMakeUnique();
return mpImplPolygon->mpPointAry[nPos];
}
tools::Polygon& Polygon::operator=( const tools::Polygon& rPoly )
{
DBG_ASSERT( rPoly.mpImplPolygon->mnRefCount < 0xFFFFFFFE, "Polygon: RefCount overflow" );
// Increase refcounter before assigning
// Note: RefCount == 0 for static objects
if ( rPoly.mpImplPolygon->mnRefCount )
rPoly.mpImplPolygon->mnRefCount++;
// Delete if recount == 0, otherwise decrement
if ( mpImplPolygon->mnRefCount )
{
if ( mpImplPolygon->mnRefCount > 1 )
mpImplPolygon->mnRefCount--;
else
delete mpImplPolygon;
}
mpImplPolygon = rPoly.mpImplPolygon;
return *this;
}
tools::Polygon& Polygon::operator=( tools::Polygon&& rPoly )
{
std::swap(mpImplPolygon, rPoly.mpImplPolygon);
return *this;
}
bool Polygon::operator==( const tools::Polygon& rPoly ) const
{
if ( (rPoly.mpImplPolygon == mpImplPolygon) )
return true;
else
return false;
}
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 i;
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;
}
if ( rPoly.mpImplPolygon->mnRefCount != 1 )
{
if ( rPoly.mpImplPolygon->mnRefCount )
rPoly.mpImplPolygon->mnRefCount--;
rPoly.mpImplPolygon = new ImplPolygon( nPoints );
}
else
rPoly.mpImplPolygon->ImplSetSize( nPoints, false );
{
// Determine whether we need to write through operators
#if (SAL_TYPES_SIZEOFLONG) == 4
#ifdef OSL_BIGENDIAN
if ( rIStream.GetEndian() == SvStreamEndian::BIG )
#else
if ( rIStream.GetEndian() == SvStreamEndian::LITTLE )
#endif
rIStream.ReadBytes(rPoly.mpImplPolygon->mpPointAry, nPoints*sizeof(Point));
else
#endif
{
for( i = 0; i < nPoints; i++ )
{
sal_Int32 nTmpX(0), nTmpY(0);
rIStream.ReadInt32( nTmpX ).ReadInt32( nTmpY );
rPoly.mpImplPolygon->mpPointAry[i].X() = nTmpX;
rPoly.mpImplPolygon->mpPointAry[i].Y() = nTmpY;
}
}
}
return rIStream;
}
SvStream& WritePolygon( SvStream& rOStream, const tools::Polygon& rPoly )
{
sal_uInt16 i;
sal_uInt16 nPoints = rPoly.GetSize();
// Write number of points
rOStream.WriteUInt16( nPoints );
{
// Determine whether we need to write through operators
#if (SAL_TYPES_SIZEOFLONG) == 4
#ifdef OSL_BIGENDIAN
if ( rOStream.GetEndian() == SvStreamEndian::BIG )
#else
if ( rOStream.GetEndian() == SvStreamEndian::LITTLE )
#endif
{
if ( nPoints )
rOStream.WriteBytes(rPoly.mpImplPolygon->mpPointAry, nPoints*sizeof(Point));
}
else
#endif
{
for( i = 0; i < nPoints; i++ )
{
rOStream.WriteInt32( rPoly.mpImplPolygon->mpPointAry[i].X() )
.WriteInt32( rPoly.mpImplPolygon->mpPointAry[i].Y() );
}
}
}
return rOStream;
}
void Polygon::ImplRead( SvStream& rIStream )
{
sal_uInt8 bHasPolyFlags(0);
ReadPolygon( rIStream, *this );
rIStream.ReadUChar( bHasPolyFlags );
if ( bHasPolyFlags )
{
mpImplPolygon->mpFlagAry = new sal_uInt8[ mpImplPolygon->mnPoints ];
rIStream.ReadBytes(mpImplPolygon->mpFlagAry, mpImplPolygon->mnPoints);
}
}
void Polygon::Read( SvStream& rIStream )
{
VersionCompat aCompat( rIStream, StreamMode::READ );
ImplRead( rIStream );
}
void Polygon::ImplWrite( SvStream& rOStream ) const
{
bool bHasPolyFlags = mpImplPolygon->mpFlagAry != nullptr;
WritePolygon( rOStream, *this );
rOStream.WriteBool(bHasPolyFlags);
if ( bHasPolyFlags )
rOStream.WriteBytes( mpImplPolygon->mpFlagAry, mpImplPolygon->mnPoints );
}
void Polygon::Write( SvStream& rOStream ) const
{
VersionCompat aCompat( rOStream, StreamMode::WRITE, 1 );
ImplWrite( rOStream );
}
// #i74631#/#i115917# numerical correction method for B2DPolygon
void impCorrectContinuity(basegfx::B2DPolygon& roPolygon, sal_uInt32 nIndex, sal_uInt8 nCFlag)
{
const sal_uInt32 nPointCount(roPolygon.count());
OSL_ENSURE(nIndex < nPointCount, "impCorrectContinuity: index access out of range (!)");
if(nIndex < nPointCount && (POLY_SMOOTH == nCFlag || POLY_SYMMTR == nCFlag))
{
if(roPolygon.isPrevControlPointUsed(nIndex) && roPolygon.isNextControlPointUsed(nIndex))
{
// #i115917# Patch from osnola (modified, thanks for showing the porblem)
// 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 mantisses
// 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);
if(POLY_SMOOTH == nCFlag)
{
// C1: apply common direction vector, preserve individual lengths
const double fInvDirectionLen(1.0 / aDirection.getLength());
roPolygon.setNextControlPoint(nIndex, basegfx::B2DPoint(aPoint + (aDirection * (aNext.getLength() * fInvDirectionLen))));
roPolygon.setPrevControlPoint(nIndex, basegfx::B2DPoint(aPoint - (aDirection * (aPrev.getLength() * fInvDirectionLen))));
}
else // POLY_SYMMTR
{
// 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 / aDirection.getLength()));
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->mpFlagAry)
{
// handling for curves. Add start point
const Point aStartPoint(mpImplPolygon->mpPointAry[0]);
sal_uInt8 nPointFlag(mpImplPolygon->mpFlagAry[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(POLY_CONTROL == mpImplPolygon->mpFlagAry[a])
{
aControlA = mpImplPolygon->mpPointAry[a++];
bControlA = true;
}
if(a < nCount && POLY_CONTROL == mpImplPolygon->mpFlagAry[a])
{
aControlB = mpImplPolygon->mpPointAry[a++];
bControlB = true;
}
// assert invalid polygons
OSL_ENSURE(bControlA == bControlB, "Polygon::getB2DPolygon: Invalid source polygon (!)");
(void)bControlB;
if(a < nCount)
{
const Point aEndPoint(mpImplPolygon->mpPointAry[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->mpFlagAry[a++];
}
}
// if exist, remove double first/last points, set closed and correct control points
basegfx::tools::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->mpFlagAry[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->mpPointAry[a]);
aRetval.append(basegfx::B2DPoint(aPoint.X(), aPoint.Y()));
}
// set closed flag
basegfx::tools::checkClosed(aRetval);
}
}
return aRetval;
}
// 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.
Polygon::Polygon(const basegfx::B2DPolygon& rPolygon)
: mpImplPolygon(nullptr)
{
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 / 3L) - 1L))
{
OSL_FAIL("Polygon::Polygon: Too many points in given B2DPolygon, need to reduce hard to maximum of tools Polygon (!)");
nB2DLocalCount = ((0x0000ffff / 3L) - 1L);
}
// calculate target point count
const sal_uInt32 nLoopCount(bClosed ? nB2DLocalCount : (nB2DLocalCount ? nB2DLocalCount - 1L : 0L ));
if(nLoopCount)
{
// calculate maximum array size and allocate; prepare insert index
const sal_uInt32 nMaxTargetCount((nLoopCount * 3) + 1);
mpImplPolygon = new ImplPolygon(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(0L); 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);
mpImplPolygon->mpPointAry[nStartPointIndex] = aStartPoint;
mpImplPolygon->mpFlagAry[nStartPointIndex] = (sal_uInt8)POLY_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
mpImplPolygon->mpPointAry[nArrayInsert] = Point(FRound(aBezier.getControlPointA().getX()), FRound(aBezier.getControlPointA().getY()));
mpImplPolygon->mpFlagAry[nArrayInsert] = (sal_uInt8)POLY_CONTROL;
nArrayInsert++;
mpImplPolygon->mpPointAry[nArrayInsert] = Point(FRound(aBezier.getControlPointB().getX()), FRound(aBezier.getControlPointB().getY()));
mpImplPolygon->mpFlagAry[nArrayInsert] = (sal_uInt8)POLY_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)
{
mpImplPolygon->mpFlagAry[nStartPointIndex] = (sal_uInt8)POLY_SMOOTH;
}
else if(basegfx::B2VectorContinuity::C2 == eCont)
{
mpImplPolygon->mpFlagAry[nStartPointIndex] = (sal_uInt8)POLY_SYMMTR;
}
}
// prepare next polygon step
aBezier.setStartPoint(aBezier.getEndPoint());
}
if(bClosed)
{
// add first point again as closing point due to old definition
mpImplPolygon->mpPointAry[nArrayInsert] = mpImplPolygon->mpPointAry[0];
mpImplPolygon->mpFlagAry[nArrayInsert] = (sal_uInt8)POLY_NORMAL;
nArrayInsert++;
}
else
{
// add last point as closing point
const basegfx::B2DPoint aClosingPoint(rPolygon.getB2DPoint(nB2DLocalCount - 1L));
const Point aEnd(FRound(aClosingPoint.getX()), FRound(aClosingPoint.getY()));
mpImplPolygon->mpPointAry[nArrayInsert] = aEnd;
mpImplPolygon->mpFlagAry[nArrayInsert] = (sal_uInt8)POLY_NORMAL;
nArrayInsert++;
}
DBG_ASSERT(nArrayInsert <= nMaxTargetCount, "Polygon::Polygon from basegfx::B2DPolygon: wrong max point count estimation (!)");
if(nArrayInsert != nMaxTargetCount)
{
mpImplPolygon->ImplSetSize(static_cast< sal_uInt16 >(nArrayInsert));
}
}
}
else
{
// #127979# Reduce source point count hard to the limit of the tools Polygon
if(nB2DLocalCount > (0x0000ffff - 1L))
{
OSL_FAIL("Polygon::Polygon: Too many points in given B2DPolygon, need to reduce hard to maximum of tools Polygon (!)");
nB2DLocalCount = (0x0000ffff - 1L);
}
if(nB2DLocalCount)
{
// point list creation
const sal_uInt32 nTargetCount(nB2DLocalCount + (bClosed ? 1L : 0L));
mpImplPolygon = new ImplPolygon( static_cast< sal_uInt16 >(nTargetCount) );
sal_uInt16 nIndex(0);
for(sal_uInt32 a(0L); a < nB2DLocalCount; a++)
{
basegfx::B2DPoint aB2DPoint(rPolygon.getB2DPoint(a));
Point aPoint(FRound(aB2DPoint.getX()), FRound(aB2DPoint.getY()));
mpImplPolygon->mpPointAry[nIndex++] = aPoint;
}
if(bClosed)
{
// add first point as closing point
mpImplPolygon->mpPointAry[nIndex] = mpImplPolygon->mpPointAry[0];
}
}
}
if(!mpImplPolygon)
{
// no content yet, create empty polygon
mpImplPolygon = static_cast<ImplPolygon*>(&aStaticImplPolygon);
}
}
} // namespace tools
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|