summaryrefslogtreecommitdiff
path: root/drawinglayer/source/processor2d/cairopixelprocessor2d.cxx
blob: 3f2df4822b23054c5bea952b3d42d50a4dd0a80c (plain)
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
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#include <sal/config.h>

#include <drawinglayer/processor2d/cairopixelprocessor2d.hxx>
#include <drawinglayer/processor2d/SDPRProcessor2dTools.hxx>
#include <sal/log.hxx>
#include <vcl/BitmapTools.hxx>
#include <vcl/cairo.hxx>
#include <vcl/outdev.hxx>
#include <vcl/svapp.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
#include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
#include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
#include <drawinglayer/primitive2d/backgroundcolorprimitive2d.hxx>
#include <drawinglayer/primitive2d/baseprimitive2d.hxx>
#include <drawinglayer/primitive2d/markerarrayprimitive2d.hxx>
#include <drawinglayer/primitive2d/maskprimitive2d.hxx>
#include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
#include <drawinglayer/primitive2d/pointarrayprimitive2d.hxx>
#include <drawinglayer/primitive2d/PolygonStrokePrimitive2D.hxx>
#include <drawinglayer/primitive2d/Tools.hxx>
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
#include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
#include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
#include <drawinglayer/primitive2d/fillgradientprimitive2d.hxx>
#include <drawinglayer/primitive2d/invertprimitive2d.hxx>
#include <drawinglayer/primitive2d/PolyPolygonGradientPrimitive2D.hxx>
#include <drawinglayer/primitive2d/PolyPolygonRGBAPrimitive2D.hxx>
#include <drawinglayer/primitive2d/PolyPolygonAlphaGradientPrimitive2D.hxx>
#include <drawinglayer/primitive2d/BitmapAlphaPrimitive2D.hxx>
#include <drawinglayer/primitive2d/textprimitive2d.hxx>
#include <drawinglayer/primitive2d/textdecoratedprimitive2d.hxx>
#include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
#include <drawinglayer/converters.hxx>
#include <drawinglayer/primitive2d/textlayoutdevice.hxx>
#include <basegfx/curve/b2dcubicbezier.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <basegfx/utils/systemdependentdata.hxx>
#include <basegfx/utils/bgradient.hxx>
#include <vcl/BitmapReadAccess.hxx>
#include <officecfg/Office/Common.hxx>
#include <vcl/vcllayout.hxx>
#include <unordered_map>
#include <dlfcn.h>

using namespace com::sun::star;

namespace
{
void impl_cairo_set_hairline(cairo_t* pRT,
                             const drawinglayer::geometry::ViewInformation2D& rViewInformation)
{
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 18, 0)
    void* addr(dlsym(nullptr, "cairo_set_hairline"));
    if (nullptr != addr)
    {
        cairo_set_hairline(pRT, true);
        return;
    }
#endif
    // avoid cairo_device_to_user_distance, see note on that below
    const double fPx(
        (rViewInformation.getInverseObjectToViewTransformation() * basegfx::B2DVector(1.0, 0.0))
            .getLength());
    cairo_set_line_width(pRT, fPx);
}

void addB2DPolygonToPathGeometry(cairo_t* pRT, const basegfx::B2DPolygon& rPolygon)
{
    // short circuit if there is nothing to do
    const sal_uInt32 nPointCount(rPolygon.count());

    const bool bHasCurves(rPolygon.areControlPointsUsed());
    const bool bClosePath(rPolygon.isClosed());
    const basegfx::B2DPoint* pLast(nullptr);

    for (sal_uInt32 nPointIdx = 0, nPrevIdx = 0;; nPrevIdx = nPointIdx++)
    {
        int nClosedIdx = nPointIdx;
        if (nPointIdx >= nPointCount)
        {
            // prepare to close last curve segment if needed
            if (bClosePath && (nPointIdx == nPointCount))
            {
                nClosedIdx = 0;
            }
            else
            {
                break;
            }
        }

        const basegfx::B2DPoint& rPoint(rPolygon.getB2DPoint(nClosedIdx));

        if (!nPointIdx)
        {
            // first point => just move there
            cairo_move_to(pRT, rPoint.getX(), rPoint.getY());
            pLast = &rPoint;
            continue;
        }

        bool bPendingCurve(false);

        if (bHasCurves)
        {
            bPendingCurve = rPolygon.isNextControlPointUsed(nPrevIdx);
            bPendingCurve |= rPolygon.isPrevControlPointUsed(nClosedIdx);
        }

        if (!bPendingCurve) // line segment
        {
            cairo_line_to(pRT, rPoint.getX(), rPoint.getY());
        }
        else // cubic bezier segment
        {
            basegfx::B2DPoint aCP1 = rPolygon.getNextControlPoint(nPrevIdx);
            basegfx::B2DPoint aCP2 = rPolygon.getPrevControlPoint(nClosedIdx);

            // tdf#99165 if the control points are 'empty', create the mathematical
            // correct replacement ones to avoid problems with the graphical sub-system
            // tdf#101026 The 1st attempt to create a mathematically correct replacement control
            // vector was wrong. Best alternative is one as close as possible which means short.
            if (aCP1.equal(*pLast))
            {
                aCP1 = *pLast + ((aCP2 - *pLast) * 0.0005);
            }

            if (aCP2.equal(rPoint))
            {
                aCP2 = rPoint + ((aCP1 - rPoint) * 0.0005);
            }

            cairo_curve_to(pRT, aCP1.getX(), aCP1.getY(), aCP2.getX(), aCP2.getY(), rPoint.getX(),
                           rPoint.getY());
        }

        pLast = &rPoint;
    }

    if (bClosePath)
    {
        cairo_close_path(pRT);
    }
}

// needed as helper, see below. It guarantees clean
// construction/cleanup using destructor
// NOTE: maybe mpSurface can be constructed even simpler,
// not sure about that. It is only used to construct
// and hold path data
struct CairoContextHolder
{
    cairo_surface_t* mpSurface;
    cairo_t* mpRenderContext;

    CairoContextHolder()
        : mpSurface(cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1))
        , mpRenderContext(cairo_create(mpSurface))
    {
    }

    ~CairoContextHolder()
    {
        cairo_destroy(mpRenderContext);
        cairo_surface_destroy(mpSurface);
    }

    cairo_t* getContext() const { return mpRenderContext; }
};

// global static helper instance
CairoContextHolder globalStaticCairoContext;

// it shows that re-using and buffering path geometry data using
// cairo is more complicated than initially thought: when adding
// a path to a cairo_t render context it already *uses* the set
// transformation, also usually consumes the path when painting.
// The (only available) method cairo_copy_path to preserve that
// data *also* transforms the path - if not already created in
// transformed form - using the current transformation set at the
// cairo context.
// This is not what we want to have a re-usable path that is
// buffered at the Poly(poly)gon: we explicitly want *exactly*
// the coordinates in the polygon preserved *at* the polygon to
// be able to re-use that data independent from any set
// transformation at any cairo context.
// Thus, create paths using a helper (CairoPathHelper) using a
// helper cairo context (CairoContextHolder) that never gets
// transformed. This removes the need to feed it the cairo context,
// but also does not immediately add the path data to the target
// context, that needs to be done using cairo_append_path at the
// target cairo context. That works since all geometry is designed
// to use exactly that coordinate system the polygon is already
// designed for anyways, and it transforms as needed inside the
// target cairo context as needed (if transform is set)
class CairoPathHelper
{
    // the created CairoPath
    cairo_path_t* mpCairoPath;

public:
    CairoPathHelper(const basegfx::B2DPolygon& rPolygon)
        : mpCairoPath(nullptr)
    {
        cairo_new_path(globalStaticCairoContext.getContext());
        addB2DPolygonToPathGeometry(globalStaticCairoContext.getContext(), rPolygon);
        mpCairoPath = cairo_copy_path(globalStaticCairoContext.getContext());
        cairo_new_path(globalStaticCairoContext.getContext());
    }

    CairoPathHelper(const basegfx::B2DPolyPolygon& rPolyPolygon)
        : mpCairoPath(nullptr)
    {
        cairo_new_path(globalStaticCairoContext.getContext());
        for (const auto& rPolygon : rPolyPolygon)
            addB2DPolygonToPathGeometry(globalStaticCairoContext.getContext(), rPolygon);
        mpCairoPath = cairo_copy_path(globalStaticCairoContext.getContext());
        cairo_new_path(globalStaticCairoContext.getContext());
    }

    ~CairoPathHelper()
    {
        // need to cleanup instance
        cairo_path_destroy(mpCairoPath);
    }

    // read access
    cairo_path_t* getCairoPath() const { return mpCairoPath; }

    sal_Int64 getEstimatedSize() const
    {
        if (nullptr == mpCairoPath)
            return 0;

        // per node:
        // - num_data incarnations of
        // - sizeof(cairo_path_data_t) which is a union of defines and point data
        //   thus may 2 x sizeof(double)
        return mpCairoPath->num_data * sizeof(cairo_path_data_t);
    }
};

class SystemDependentData_CairoPathGeometry : public basegfx::SystemDependentData
{
    // the CairoPath holder
    std::shared_ptr<CairoPathHelper> mpCairoPathHelper;

public:
    SystemDependentData_CairoPathGeometry(const std::shared_ptr<CairoPathHelper>& pCairoPathHelper)
        : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(),
                                       basegfx::SDD_Type::SDDType_CairoPathGeometry)
        , mpCairoPathHelper(pCairoPathHelper)
    {
    }

    // read access
    const std::shared_ptr<CairoPathHelper>& getCairoPathHelper() const { return mpCairoPathHelper; }

    virtual sal_Int64 estimateUsageInBytes() const override
    {
        return (nullptr != mpCairoPathHelper) ? mpCairoPathHelper->getEstimatedSize() : 0;
    }
};

constexpr unsigned long nMinimalPointsPath(4);
constexpr unsigned long nMinimalPointsFill(12);

void checkAndDoPixelSnap(cairo_t* pRT,
                         const drawinglayer::geometry::ViewInformation2D& rViewInformation)
{
    const bool bPixelSnap(rViewInformation.getPixelSnapHairline()
                          && rViewInformation.getUseAntiAliasing());

    if (!bPixelSnap)
    {
        // no pixel snap, done
        return;
    }

    // with the comments above at CairoPathHelper we cannot do PixelSnap
    // at path construction time, so it needs to be done *after* the path
    // data is added to the cairo context. Advantage is that all general
    // path data can be buffered, though, but needs view-dependent manipulation
    // here after being added.
    // For now, just snap all points - no real need to identify hor/ver lines
    // when you think about it

    // get helper path
    cairo_path_t* path(cairo_copy_path(pRT));

    if (0 == path->num_data)
    {
        // path is empty, done
        cairo_path_destroy(path);
        return;
    }

    for (int a(0); a < path->num_data; a += path->data[a].header.length)
    {
        cairo_path_data_t* data(&path->data[a]);

        switch (data->header.type)
        {
            case CAIRO_PATH_MOVE_TO:
            case CAIRO_PATH_LINE_TO:
            case CAIRO_PATH_CURVE_TO:
            {
                // NOTE: for CAIRO_PATH_CURVE_TO we would also have the control
                // points, but these do not really need correction. If that may
                // change a correction using the deltas in x and y could be added

                // get pointers to double data
                double* pX(&data[1].point.x);
                double* pY(&data[1].point.y);

                // transform to discrete pixels
                cairo_user_to_device(pRT, pX, pY);

                // round them, also subtract 0.5 which will be as transform in
                // the paint method to move to 'inside' pixels when AA used.
                // remember: this is only done when AA is active (see bPixelSnap
                // above)
                *pX = basegfx::fround(*pX) - 0.5;
                *pY = basegfx::fround(*pY) - 0.5;

                // transform back to former transformed state
                cairo_device_to_user(pRT, pX, pY);
                break;
            }
            case CAIRO_PATH_CLOSE_PATH:
            {
                break;
            }
        }
    }

    // set changed path back at cairo context
    cairo_new_path(pRT);
    cairo_append_path(pRT, path);

    // destroy helper path
    cairo_path_destroy(path);
}

void getOrCreatePathGeometry(cairo_t* pRT, const basegfx::B2DPolygon& rPolygon,
                             const drawinglayer::geometry::ViewInformation2D& rViewInformation)
{
    // try to access buffered data
    std::shared_ptr<SystemDependentData_CairoPathGeometry> pSystemDependentData_CairoPathGeometry(
        rPolygon.getSystemDependentData<SystemDependentData_CairoPathGeometry>(
            basegfx::SDD_Type::SDDType_CairoPathGeometry));

    if (pSystemDependentData_CairoPathGeometry)
    {
        // re-use data and do evtl. needed pixel snap after adding on cairo path data
        cairo_append_path(
            pRT, pSystemDependentData_CairoPathGeometry->getCairoPathHelper()->getCairoPath());
        checkAndDoPixelSnap(pRT, rViewInformation);
        return;
    }

    // create new data and add path data to pRT and do evtl. needed pixel snap after adding on cairo path data
    std::shared_ptr<CairoPathHelper> pCairoPathHelper(std::make_shared<CairoPathHelper>(rPolygon));
    cairo_append_path(pRT, pCairoPathHelper->getCairoPath());
    checkAndDoPixelSnap(pRT, rViewInformation);

    // add to buffering mechanism if not trivial
    if (rPolygon.count() > nMinimalPointsPath)
        rPolygon.addOrReplaceSystemDependentData<SystemDependentData_CairoPathGeometry>(
            pCairoPathHelper);
}

void getOrCreateFillGeometry(cairo_t* pRT, const basegfx::B2DPolyPolygon& rPolyPolygon)
{
    // try to access buffered data
    std::shared_ptr<SystemDependentData_CairoPathGeometry> pSystemDependentData_CairoPathGeometry(
        rPolyPolygon.getSystemDependentData<SystemDependentData_CairoPathGeometry>(
            basegfx::SDD_Type::SDDType_CairoPathGeometry));

    if (pSystemDependentData_CairoPathGeometry)
    {
        // re-use data
        cairo_append_path(
            pRT, pSystemDependentData_CairoPathGeometry->getCairoPathHelper()->getCairoPath());
        return;
    }

    // create new data and add path data to pRT
    std::shared_ptr<CairoPathHelper> pCairoPathHelper(
        std::make_shared<CairoPathHelper>(rPolyPolygon));
    cairo_append_path(pRT, pCairoPathHelper->getCairoPath());

    // get all PointCount to detect non-trivial
    sal_uInt32 nAllPointCount(0);
    for (const auto& rPolygon : rPolyPolygon)
        nAllPointCount += rPolygon.count();

    // add to buffering mechanism when no PixelSnapHairline (see above) and not trivial
    if (nAllPointCount > nMinimalPointsFill)
        rPolyPolygon.addOrReplaceSystemDependentData<SystemDependentData_CairoPathGeometry>(
            pCairoPathHelper);
}

// check for env var that decides for using downscale pattern
const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
const bool bDisableDownScale(nullptr != pDisableDownScale);
constexpr unsigned long nMinimalDiscreteSize(15);
constexpr unsigned long nHalfMDSize((nMinimalDiscreteSize + 1) / 2);
constexpr unsigned long
nMinimalDiscreteSquareSizeToBuffer(nMinimalDiscreteSize* nMinimalDiscreteSize);

class CairoSurfaceHelper
{
    // the buffered CairoSurface (bitmap data)
    cairo_surface_t* mpCairoSurface;

    // evtl. MipMapped data (pre-scale to reduce data processing load)
    mutable std::unordered_map<sal_uInt64, cairo_surface_t*> maDownscaled;

    // create 32bit RGBA data for given BitmapEx
    void createRGBA(const BitmapEx& rBitmapEx)
    {
        Bitmap aSrcAlpha(rBitmapEx.GetAlphaMask().GetBitmap());
        BitmapScopedReadAccess pReadAccess(rBitmapEx.GetBitmap());
        BitmapScopedReadAccess pAlphaReadAccess(aSrcAlpha);
        const tools::Long nHeight(pReadAccess->Height());
        const tools::Long nWidth(pReadAccess->Width());
        mpCairoSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, nWidth, nHeight);
        const sal_uInt32 nStride(cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, nWidth));
        unsigned char* surfaceData(cairo_image_surface_get_data(mpCairoSurface));

        for (tools::Long y(0); y < nHeight; ++y)
        {
            unsigned char* pPixelData(surfaceData + (nStride * y));

            for (tools::Long x(0); x < nWidth; ++x)
            {
                const BitmapColor aColor(pReadAccess->GetColor(y, x));
                const BitmapColor aAlpha(pAlphaReadAccess->GetColor(y, x));
                const sal_uInt16 nAlpha(aAlpha.GetRed());

                pPixelData[SVP_CAIRO_RED] = vcl::bitmap::premultiply(nAlpha, aColor.GetRed());
                pPixelData[SVP_CAIRO_GREEN] = vcl::bitmap::premultiply(nAlpha, aColor.GetGreen());
                pPixelData[SVP_CAIRO_BLUE] = vcl::bitmap::premultiply(nAlpha, aColor.GetBlue());
                pPixelData[SVP_CAIRO_ALPHA] = nAlpha;
                pPixelData += 4;
            }
        }

        cairo_surface_mark_dirty(mpCairoSurface);
    }

    // create 32bit RGB data for given BitmapEx
    void createRGB(const BitmapEx& rBitmapEx)
    {
        BitmapScopedReadAccess pReadAccess(rBitmapEx.GetBitmap());
        const tools::Long nHeight(pReadAccess->Height());
        const tools::Long nWidth(pReadAccess->Width());
        mpCairoSurface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, nWidth, nHeight);
        sal_uInt32 nStride(cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, nWidth));
        unsigned char* surfaceData(cairo_image_surface_get_data(mpCairoSurface));

        for (tools::Long y(0); y < nHeight; ++y)
        {
            unsigned char* pPixelData(surfaceData + (nStride * y));

            for (tools::Long x(0); x < nWidth; ++x)
            {
                const BitmapColor aColor(pReadAccess->GetColor(y, x));

                pPixelData[SVP_CAIRO_RED] = aColor.GetRed();
                pPixelData[SVP_CAIRO_GREEN] = aColor.GetGreen();
                pPixelData[SVP_CAIRO_BLUE] = aColor.GetBlue();
                pPixelData[SVP_CAIRO_ALPHA] = 255; // not really needed
                pPixelData += 4;
            }
        }

        cairo_surface_mark_dirty(mpCairoSurface);
    }

// #define TEST_RGB16
#ifdef TEST_RGB16
    // experimental: create 16bit RGB data for given BitmapEx
    void createRGB16(const BitmapEx& rBitmapEx)
    {
        BitmapScopedReadAccess pReadAccess(rBitmapEx.GetBitmap());
        const tools::Long nHeight(pReadAccess->Height());
        const tools::Long nWidth(pReadAccess->Width());
        mpCairoSurface = cairo_image_surface_create(CAIRO_FORMAT_RGB16_565, nWidth, nHeight);
        sal_uInt32 nStride(cairo_format_stride_for_width(CAIRO_FORMAT_RGB16_565, nWidth));
        unsigned char* surfaceData(cairo_image_surface_get_data(mpCairoSurface));

        for (tools::Long y(0); y < nHeight; ++y)
        {
            unsigned char* pPixelData(surfaceData + (nStride * y));

            for (tools::Long x(0); x < nWidth; ++x)
            {
                const BitmapColor aColor(pReadAccess->GetColor(y, x));
                const sal_uInt8 aLeft((aColor.GetBlue() >> 3) | ((aColor.GetGreen() << 3) & 0xe0));
                const sal_uInt8 aRight((aColor.GetRed() & 0xf8) | (aColor.GetGreen() >> 5));
#ifdef OSL_BIGENDIAN
                pPixelData[1] = aRight;
                pPixelData[0] = aLeft;
#else
                pPixelData[0] = aLeft;
                pPixelData[1] = aRight;
#endif
                pPixelData += 2;
            }
        }

        cairo_surface_mark_dirty(mpCairoSurface);
    }
#endif

public:
    CairoSurfaceHelper(const BitmapEx& rBitmapEx)
        : mpCairoSurface(nullptr)
        , maDownscaled()
    {
        if (rBitmapEx.IsAlpha())
            createRGBA(rBitmapEx);
        else
#ifdef TEST_RGB16
            createRGB16(rBitmapEx);
#else
            createRGB(rBitmapEx);
#endif
    }

    ~CairoSurfaceHelper()
    {
        // cleanup surface
        cairo_surface_destroy(mpCairoSurface);

        // cleanup MipMap surfaces
        for (auto& candidate : maDownscaled)
            cairo_surface_destroy(candidate.second);
    }

    cairo_surface_t* getCairoSurface(sal_uInt32 nTargetWidth = 0,
                                     sal_uInt32 nTargetHeight = 0) const
    {
        // in simple cases just return the single created surface
        if (bDisableDownScale || nullptr == mpCairoSurface || 0 == nTargetWidth
            || 0 == nTargetHeight)
            return mpCairoSurface;

        // get width/height of original surface
        const sal_uInt32 nSourceWidth(cairo_image_surface_get_width(mpCairoSurface));
        const sal_uInt32 nSourceHeight(cairo_image_surface_get_height(mpCairoSurface));

        // zoomed in, need to stretch at paint, no pre-scale useful
        if (nTargetWidth >= nSourceWidth || nTargetHeight >= nSourceHeight)
            return mpCairoSurface;

        // calculate downscale factor. Only use ONE factor to get the diagonal
        // MipMap, NOT the full MipMap field in X/Y for uneven factors in both dimensions
        sal_uInt32 nFactor(1);
        sal_uInt32 nW((nSourceWidth + 1) / 2);
        sal_uInt32 nH((nSourceHeight + 1) / 2);

        while (nW > nTargetWidth && nW > nHalfMDSize && nH > nTargetHeight && nH > nHalfMDSize)
        {
            nW = (nW + 1) / 2;
            nH = (nH + 1) / 2;
            nFactor *= 2;
        }

        if (1 == nFactor)
        {
            // original size *is* best binary size, use it
            return mpCairoSurface;
        }

        // go up one scale again
        nW *= 2;
        nH *= 2;

        // bail out if the multiplication for the key would overflow
        if (nW >= SAL_MAX_UINT32 || nH >= SAL_MAX_UINT32)
            return mpCairoSurface;

        // check if we have a downscaled version of required size
        const sal_uInt64 key((nW * static_cast<sal_uInt64>(SAL_MAX_UINT32)) + nH);
        auto isHit(maDownscaled.find(key));

        // found -> return it
        if (isHit != maDownscaled.end())
            return isHit->second;

        // create new surface in the targeted size
        cairo_surface_t* pSurfaceTarget(cairo_surface_create_similar(
            mpCairoSurface, cairo_surface_get_content(mpCairoSurface), nW, nH));

        // made a version to scale self first with direct memory access.
        // That worked well, but would've been hard to support
        // CAIRO_FORMAT_A1 and similar (including bit shifting), so
        // I decided to go with cairo itself - use CAIRO_FILTER_FAST or
        // CAIRO_FILTER_GOOD though. Please modify as needed for
        // performance/quality
        cairo_t* cr = cairo_create(pSurfaceTarget);
        const double fScaleX(static_cast<double>(nW) / static_cast<double>(nSourceWidth));
        const double fScaleY(static_cast<double>(nH) / static_cast<double>(nSourceHeight));

        cairo_scale(cr, fScaleX, fScaleY);
        cairo_set_source_surface(cr, mpCairoSurface, 0.0, 0.0);
        cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_GOOD);
        cairo_paint(cr);
        cairo_destroy(cr);

        // NOTE: Took out, until now not really needed
        // need to set device_scale for downscale surfaces to get
        // them handled correctly
        // cairo_surface_set_device_scale(pSurfaceTarget, fScaleX, fScaleY);

        // add entry to cached entries
        maDownscaled[key] = pSurfaceTarget;

        return pSurfaceTarget;
    }

    bool isTrivial() const
    {
        if (nullptr == mpCairoSurface)
            return true;

        const sal_uInt32 nSourceWidth(cairo_image_surface_get_width(mpCairoSurface));
        const sal_uInt32 nSourceHeight(cairo_image_surface_get_height(mpCairoSurface));

        return nSourceWidth * nSourceHeight < nMinimalDiscreteSquareSizeToBuffer;
    }
};

class SystemDependentData_CairoSurface : public basegfx::SystemDependentData
{
    // the CairoSurface holder
    std::shared_ptr<CairoSurfaceHelper> mpCairoSurfaceHelper;

    // need to remember alpha source for combined BitmapEx to detect/
    // react on that changing
    std::shared_ptr<SalBitmap> maAssociatedAlpha;

public:
    SystemDependentData_CairoSurface(const BitmapEx& rBitmapEx)
        : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(),
                                       basegfx::SDD_Type::SDDType_CairoSurface)
        , mpCairoSurfaceHelper(std::make_shared<CairoSurfaceHelper>(rBitmapEx))
        , maAssociatedAlpha()
    {
        if (rBitmapEx.IsAlpha())
            maAssociatedAlpha = rBitmapEx.GetAlphaMask().GetBitmap().ImplGetSalBitmap();
    }

    // read access
    const std::shared_ptr<CairoSurfaceHelper>& getCairoSurfaceHelper() const
    {
        return mpCairoSurfaceHelper;
    }
    const std::shared_ptr<SalBitmap>& getAssociatedAlpha() const { return maAssociatedAlpha; }

    virtual sal_Int64 estimateUsageInBytes() const override;
};

sal_Int64 SystemDependentData_CairoSurface::estimateUsageInBytes() const
{
    sal_Int64 nRetval(0);

    if (mpCairoSurfaceHelper)
    {
        cairo_surface_t* pSurface(mpCairoSurfaceHelper->getCairoSurface());
        const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
        const tools::Long nHeight(cairo_image_surface_get_height(pSurface));

        nRetval = nStride * nHeight;

        // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
        // rough estimation just multiplies by 1.25 .. 1.33, should be good enough
        // for estimation of buffer survival time
        if (!bDisableDownScale)
        {
            nRetval = (nRetval * 5) / 4;
        }
    }

    return nRetval;
}

std::shared_ptr<CairoSurfaceHelper> getOrCreateCairoSurfaceHelper(const BitmapEx& rBitmapEx)
{
    const basegfx::SystemDependentDataHolder* pHolder(
        rBitmapEx.GetBitmap().accessSystemDependentDataHolder());
    std::shared_ptr<SystemDependentData_CairoSurface> pSystemDependentData_CairoSurface;

    if (nullptr != pHolder)
    {
        // try to access SystemDependentDataHolder and buffered data
        pSystemDependentData_CairoSurface
            = std::static_pointer_cast<SystemDependentData_CairoSurface>(
                pHolder->getSystemDependentData(basegfx::SDD_Type::SDDType_CairoSurface));

        // check data validity for associated Alpha
        if (pSystemDependentData_CairoSurface && rBitmapEx.IsAlpha()
            && pSystemDependentData_CairoSurface->getAssociatedAlpha()
                   != rBitmapEx.GetAlphaMask().GetBitmap().ImplGetSalBitmap())
        {
            // AssociatedAlpha did change, data invalid
            pSystemDependentData_CairoSurface.reset();
        }
    }

    if (!pSystemDependentData_CairoSurface)
    {
        // create new SystemDependentData_CairoSurface
        pSystemDependentData_CairoSurface
            = std::make_shared<SystemDependentData_CairoSurface>(rBitmapEx);

        // only add if feasible
        if (nullptr != pHolder
            && !pSystemDependentData_CairoSurface->getCairoSurfaceHelper()->isTrivial()
            && pSystemDependentData_CairoSurface->calculateCombinedHoldCyclesInSeconds() > 0)
        {
            basegfx::SystemDependentData_SharedPtr r2(pSystemDependentData_CairoSurface);
            const_cast<basegfx::SystemDependentDataHolder*>(pHolder)
                ->addOrReplaceSystemDependentData(r2);
        }
    }

    return pSystemDependentData_CairoSurface->getCairoSurfaceHelper();
}

// This bit-tweaking looping is unpleasant and unfortunate
void LuminanceToAlpha(cairo_surface_t* pMask)
{
    cairo_surface_flush(pMask);

    const sal_uInt32 nWidth(cairo_image_surface_get_width(pMask));
    const sal_uInt32 nHeight(cairo_image_surface_get_height(pMask));
    const sal_uInt32 nStride(cairo_image_surface_get_stride(pMask));

    if (0 == nWidth || 0 == nHeight)
        return;

    unsigned char* mask_surface_data(cairo_image_surface_get_data(pMask));

    // include/basegfx/color/bcolormodifier.hxx
    constexpr double nRedMul(0.2125 / 255.0);
    constexpr double nGreenMul(0.7154 / 255.0);
    constexpr double nBlueMul(0.0721 / 255.0);

    // Only this alpha channel is taken into account by cairo_mask_surface
    // so reuse this surface for the alpha result
    for (sal_uInt32 y(0); y < nHeight; ++y)
    {
        unsigned char* pMaskPixelData = mask_surface_data + (nStride * y);

        for (sal_uInt32 x(0); x < nWidth; ++x)
        {
            // do not forget that we have pre-multiplied alpha
            sal_uInt8 nAlpha(pMaskPixelData[SVP_CAIRO_ALPHA]);

            if (0 != nAlpha)
            {
                const double fLuminance = pMaskPixelData[SVP_CAIRO_RED] * nRedMul
                                          + pMaskPixelData[SVP_CAIRO_GREEN] * nGreenMul
                                          + pMaskPixelData[SVP_CAIRO_BLUE] * nBlueMul;

                if (255 != nAlpha)
                    nAlpha = fLuminance / nAlpha;
                else
                    nAlpha = 255.0 * fLuminance;
            }

            pMaskPixelData[SVP_CAIRO_ALPHA] = 255 - nAlpha;
            pMaskPixelData += 4;
        }
    }

    cairo_surface_mark_dirty(pMask);
}

basegfx::B2DRange getDiscreteViewRange(cairo_t* pRT)
{
    double clip_x1, clip_x2, clip_y1, clip_y2;
    cairo_save(pRT);
    cairo_identity_matrix(pRT);
    cairo_clip_extents(pRT, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
    cairo_restore(pRT);

    return basegfx::B2DRange(basegfx::B2DPoint(clip_x1, clip_y1),
                             basegfx::B2DPoint(clip_x2, clip_y2));
}
}

namespace drawinglayer::processor2d
{
CairoPixelProcessor2D::CairoPixelProcessor2D(const geometry::ViewInformation2D& rViewInformation,
                                             cairo_surface_t* pTarget)
    : BaseProcessor2D(rViewInformation)
    , maBColorModifierStack()
    , mpRT(nullptr)
    , mbRenderSimpleTextDirect(
          officecfg::Office::Common::Drawinglayer::RenderSimpleTextDirect::get())
    , mbRenderDecoratedTextDirect(
          officecfg::Office::Common::Drawinglayer::RenderDecoratedTextDirect::get())
{
    if (pTarget)
    {
        cairo_t* pRT = cairo_create(pTarget);
        cairo_set_antialias(pRT, rViewInformation.getUseAntiAliasing() ? CAIRO_ANTIALIAS_DEFAULT
                                                                       : CAIRO_ANTIALIAS_NONE);
        cairo_set_fill_rule(pRT, CAIRO_FILL_RULE_EVEN_ODD);
        setRenderTarget(pRT);
    }
}

CairoPixelProcessor2D::~CairoPixelProcessor2D()
{
    if (mpRT)
        cairo_destroy(mpRT);
}

void CairoPixelProcessor2D::processBitmapPrimitive2D(
    const primitive2d::BitmapPrimitive2D& rBitmapCandidate)
{
    paintBitmapAlpha(rBitmapCandidate.getBitmap(), rBitmapCandidate.getTransform());
}

void CairoPixelProcessor2D::paintBitmapAlpha(const BitmapEx& rBitmapEx,
                                             const basegfx::B2DHomMatrix& rTransform,
                                             double fTransparency)
{
    // transparency invalid or completely transparent, done
    if (fTransparency < 0.0 || fTransparency >= 1.0)
    {
        return;
    }

    // check if graphic content is inside discrete local ViewPort
    const basegfx::B2DRange& rDiscreteViewPort(getViewInformation2D().getDiscreteViewport());
    const basegfx::B2DHomMatrix aLocalTransform(
        getViewInformation2D().getObjectToViewTransformation() * rTransform);

    if (!rDiscreteViewPort.isEmpty())
    {
        basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);

        aUnitRange.transform(aLocalTransform);

        if (!aUnitRange.overlaps(rDiscreteViewPort))
        {
            // content is outside discrete local ViewPort
            return;
        }
    }

    BitmapEx aBitmapEx(rBitmapEx);

    if (aBitmapEx.IsEmpty() || aBitmapEx.GetSizePixel().IsEmpty())
    {
        // no pixel data, done
        return;
    }

    if (maBColorModifierStack.count())
    {
        // need to apply ColorModifier to Bitmap data
        aBitmapEx = aBitmapEx.ModifyBitmapEx(maBColorModifierStack);

        if (aBitmapEx.IsEmpty())
        {
            // color gets completely replaced, get it
            const basegfx::BColor aModifiedColor(
                maBColorModifierStack.getModifiedColor(basegfx::BColor()));

            // use unit geometry as fallback object geometry. Do *not*
            // transform, the below used method will use the already
            // correctly initialized local ViewInformation
            basegfx::B2DPolygon aPolygon(basegfx::utils::createUnitPolygon());

            rtl::Reference<primitive2d::PolyPolygonColorPrimitive2D> xTemp(
                new primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aPolygon),
                                                             aModifiedColor));

            // draw as Polygon, done
            processPolyPolygonColorPrimitive2D(*xTemp);
            return;
        }
    }

    // access or create cairo bitmap data
    std::shared_ptr<CairoSurfaceHelper> aCairoSurfaceHelper(
        getOrCreateCairoSurfaceHelper(aBitmapEx));
    if (!aCairoSurfaceHelper)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No SurfaceHelper from BitmapEx (!)");
        return;
    }

    // work with dimensions in discrete target pixels to use evtl. MipMap pre-scale
    const tools::Long nDestWidth((aLocalTransform * basegfx::B2DVector(1.0, 0.0)).getLength());
    const tools::Long nDestHeight((aLocalTransform * basegfx::B2DVector(0.0, 1.0)).getLength());

    cairo_surface_t* pTarget(aCairoSurfaceHelper->getCairoSurface(nDestWidth, nDestHeight));
    if (nullptr == pTarget)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No CairoSurface from BitmapEx SurfaceHelper (!)");
        return;
    }

    cairo_save(mpRT);

    // set linear transformation - no fAAOffset for bitmap data
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aLocalTransform.a(), aLocalTransform.b(), aLocalTransform.c(),
                      aLocalTransform.d(), aLocalTransform.e(), aLocalTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);

    static bool bRenderTransformationBounds(false);
    if (bRenderTransformationBounds)
    {
        cairo_set_source_rgba(mpRT, 1, 0, 0, 0.8);
        impl_cairo_set_hairline(mpRT, getViewInformation2D());
        cairo_rectangle(mpRT, 0, 0, 1, 1);
        cairo_stroke(mpRT);
    }

    const sal_uInt32 nWidth(cairo_image_surface_get_width(pTarget));
    const sal_uInt32 nHeight(cairo_image_surface_get_height(pTarget));

    cairo_set_source_surface(mpRT, pTarget, 0, 0);

    // get the pattern created by cairo_set_source_surface and
    // it's transformation
    cairo_pattern_t* sourcepattern = cairo_get_source(mpRT);
    cairo_pattern_get_matrix(sourcepattern, &aMatrix);

    // RGBA sources overlap the unit geometry range, slightly,
    // to see that activate bRenderTransformationBounds and
    // insert a ARGB image, zoom to the borders. Seems to be half
    // a pixel. Very good to demonstrate: 8x1 pixel, some
    // transparent.
    // This effect is also visible in the left/right/bottom/top
    // page shadows, these DO use 8x1/1x8 images which led me to
    // that problem. I see two solutions:
    static bool bRenderMasked(true);
    if (bRenderMasked)
    {
        // Consequence is that these need clipping. That again is
        // simple (we are in unit coordinates). Only do for RGBA,
        // for RGB this effect does not happen
        if (CAIRO_FORMAT_ARGB32 == cairo_image_surface_get_format(pTarget))
        {
            cairo_rectangle(mpRT, 0, 0, 1, 1);
            cairo_clip(mpRT);
        }

        cairo_matrix_scale(&aMatrix, nWidth, nHeight);
    }
    else
    {
        // Alternative: for RGBA, resize/scale it SLIGHTLY to force
        // that half pixel overlap to be inside the unit range.
        // That makes the error disappear, so no clip needed, but
        // SLIGHTLY smaller.
        if (CAIRO_FORMAT_ARGB32 == cairo_image_surface_get_format(pTarget))
        {
            cairo_matrix_init_scale(&aMatrix, nWidth + 1, nHeight + 1);
            cairo_matrix_translate(&aMatrix, -0.5 / (nWidth + 1), -0.5 / (nHeight + 1));
        }
        else
        {
            cairo_matrix_scale(&aMatrix, nWidth, nHeight);
        }
    }

    cairo_pattern_set_matrix(sourcepattern, &aMatrix);

    // paint bitmap data, evtl. with additional alpha channel
    if (!basegfx::fTools::equalZero(fTransparency))
        cairo_paint_with_alpha(mpRT, 1.0 - fTransparency);
    else
        cairo_paint(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processPointArrayPrimitive2D(
    const primitive2d::PointArrayPrimitive2D& rPointArrayCandidate)
{
    const std::vector<basegfx::B2DPoint>& rPositions(rPointArrayCandidate.getPositions());

    if (rPositions.empty())
    {
        // no geometry, done
        return;
    }

    cairo_save(mpRT);

    // determine & set color
    const basegfx::BColor aPointColor(
        maBColorModifierStack.getModifiedColor(rPointArrayCandidate.getRGBColor()));
    cairo_set_source_rgb(mpRT, aPointColor.getRed(), aPointColor.getGreen(), aPointColor.getBlue());

    // To really paint a single pixel I found nothing better than
    // switch off AA and draw a pixel-aligned rectangle
    const cairo_antialias_t eOldAAMode(cairo_get_antialias(mpRT));
    cairo_set_antialias(mpRT, CAIRO_ANTIALIAS_NONE);

    for (auto const& pos : rPositions)
    {
        const basegfx::B2DPoint aDiscretePos(getViewInformation2D().getObjectToViewTransformation()
                                             * pos);
        const double fX(ceil(aDiscretePos.getX()));
        const double fY(ceil(aDiscretePos.getY()));

        cairo_rectangle(mpRT, fX, fY, 1, 1);
        cairo_fill(mpRT);
    }

    cairo_set_antialias(mpRT, eOldAAMode);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processPolygonHairlinePrimitive2D(
    const primitive2d::PolygonHairlinePrimitive2D& rPolygonHairlinePrimitive2D)
{
    const basegfx::B2DPolygon& rPolygon(rPolygonHairlinePrimitive2D.getB2DPolygon());

    if (!rPolygon.count())
    {
        // no geometry, done
        return;
    }

    cairo_save(mpRT);

    // set linear transformation
    cairo_matrix_t aMatrix;
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);
    cairo_set_matrix(mpRT, &aMatrix);

    // determine & set color
    const basegfx::BColor aHairlineColor(
        maBColorModifierStack.getModifiedColor(rPolygonHairlinePrimitive2D.getBColor()));
    cairo_set_source_rgb(mpRT, aHairlineColor.getRed(), aHairlineColor.getGreen(),
                         aHairlineColor.getBlue());

    // set LineWidth, use Cairo's special cairo_set_hairline
    impl_cairo_set_hairline(mpRT, getViewInformation2D());

    // get PathGeometry & paint it
    cairo_new_path(mpRT);
    getOrCreatePathGeometry(mpRT, rPolygon, getViewInformation2D());
    cairo_stroke(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processPolyPolygonColorPrimitive2D(
    const primitive2d::PolyPolygonColorPrimitive2D& rPolyPolygonColorPrimitive2D)
{
    paintPolyPoylgonRGBA(rPolyPolygonColorPrimitive2D.getB2DPolyPolygon(),
                         rPolyPolygonColorPrimitive2D.getBColor());
}

void CairoPixelProcessor2D::paintPolyPoylgonRGBA(const basegfx::B2DPolyPolygon& rPolyPolygon,
                                                 const basegfx::BColor& rColor,
                                                 double fTransparency)
{
    // transparency invalid or completely transparent, done
    if (fTransparency < 0.0 || fTransparency >= 1.0)
    {
        return;
    }

    const sal_uInt32 nCount(rPolyPolygon.count());

    if (!nCount)
    {
        // no geometry, done
        return;
    }

    cairo_save(mpRT);

    // set linear transformation
    cairo_matrix_t aMatrix;
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);
    cairo_set_matrix(mpRT, &aMatrix);

    // determine & set color
    const basegfx::BColor aFillColor(maBColorModifierStack.getModifiedColor(rColor));

    if (!basegfx::fTools::equalZero(fTransparency))
        cairo_set_source_rgba(mpRT, aFillColor.getRed(), aFillColor.getGreen(),
                              aFillColor.getBlue(), 1.0 - fTransparency);
    else
        cairo_set_source_rgb(mpRT, aFillColor.getRed(), aFillColor.getGreen(),
                             aFillColor.getBlue());

    // get PathGeometry & paint it
    cairo_new_path(mpRT);
    getOrCreateFillGeometry(mpRT, rPolyPolygon);
    cairo_fill(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processTransparencePrimitive2D(
    const primitive2d::TransparencePrimitive2D& rTransCandidate)
{
    if (rTransCandidate.getChildren().empty())
    {
        // no content, done
        return;
    }

    if (rTransCandidate.getTransparence().empty())
    {
        // no mask (so nothing visible), done
        return;
    }

    // calculate visible range, create only for that range
    basegfx::B2DRange aDiscreteRange(
        rTransCandidate.getChildren().getB2DRange(getViewInformation2D()));
    aDiscreteRange.transform(getViewInformation2D().getObjectToViewTransformation());
    basegfx::B2DRange aVisibleRange(aDiscreteRange);
    aVisibleRange.intersect(getDiscreteViewRange(mpRT));

    if (aVisibleRange.isEmpty())
    {
        // not visible, done
        return;
    }

    cairo_save(mpRT);

    // create embedding transformation for sub-surface
    const basegfx::B2DHomMatrix aEmbedTransform(basegfx::utils::createTranslateB2DHomMatrix(
        -aVisibleRange.getMinX(), -aVisibleRange.getMinY()));
    geometry::ViewInformation2D aViewInformation2D(getViewInformation2D());
    aViewInformation2D.setViewTransformation(aEmbedTransform
                                             * getViewInformation2D().getViewTransformation());

    // draw mask to temporary surface
    cairo_surface_t* pTarget(cairo_get_target(mpRT));
    const double fContainedWidth(ceil(aVisibleRange.getWidth()));
    const double fContainedHeight(ceil(aVisibleRange.getHeight()));
    cairo_surface_t* pMask(cairo_surface_create_similar_image(pTarget, CAIRO_FORMAT_ARGB32,
                                                              fContainedWidth, fContainedHeight));
    CairoPixelProcessor2D aMaskRenderer(aViewInformation2D, pMask);
    aMaskRenderer.process(rTransCandidate.getTransparence());

    // convert mask to something cairo can use
    LuminanceToAlpha(pMask);

    // draw content to temporary surface
    cairo_surface_t* pContent(cairo_surface_create_similar(
        pTarget, cairo_surface_get_content(pTarget), fContainedWidth, fContainedHeight));
    CairoPixelProcessor2D aContent(aViewInformation2D, pContent);
    aContent.process(rTransCandidate.getChildren());

    // munge the temporary surfaces to our target surface
    cairo_set_source_surface(mpRT, pContent, aVisibleRange.getMinX(), aVisibleRange.getMinY());
    cairo_mask_surface(mpRT, pMask, aVisibleRange.getMinX(), aVisibleRange.getMinY());

    // cleanup temporary surfaces
    cairo_surface_destroy(pContent);
    cairo_surface_destroy(pMask);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processInvertPrimitive2D(
    const primitive2d::InvertPrimitive2D& rInvertCandidate)
{
    if (rInvertCandidate.getChildren().empty())
    {
        // no content, done
        return;
    }

    // calculate visible range, create only for that range
    basegfx::B2DRange aDiscreteRange(
        rInvertCandidate.getChildren().getB2DRange(getViewInformation2D()));
    aDiscreteRange.transform(getViewInformation2D().getObjectToViewTransformation());
    basegfx::B2DRange aVisibleRange(aDiscreteRange);
    aVisibleRange.intersect(getDiscreteViewRange(mpRT));

    if (aVisibleRange.isEmpty())
    {
        // not visible, done
        return;
    }

    cairo_save(mpRT);

    // create embedding transformation for sub-surface
    const basegfx::B2DHomMatrix aEmbedTransform(basegfx::utils::createTranslateB2DHomMatrix(
        -aVisibleRange.getMinX(), -aVisibleRange.getMinY()));
    geometry::ViewInformation2D aViewInformation2D(getViewInformation2D());
    aViewInformation2D.setViewTransformation(aEmbedTransform
                                             * getViewInformation2D().getViewTransformation());

    // draw sub-content to temporary surface
    cairo_surface_t* pTarget(cairo_get_target(mpRT));
    const double fContainedWidth(ceil(aVisibleRange.getWidth()));
    const double fContainedHeight(ceil(aVisibleRange.getHeight()));
    cairo_surface_t* pContent(cairo_surface_create_similar_image(
        pTarget, CAIRO_FORMAT_ARGB32, fContainedWidth, fContainedHeight));
    CairoPixelProcessor2D aContent(aViewInformation2D, pContent);
    aContent.process(rInvertCandidate.getChildren());
    cairo_surface_flush(pContent);

    // decide if to use builtin or create XOR yourself
    // NOTE: not using and doing self is closer to what the
    //       current default does, so keep it
    static bool bUseBuiltinXOR(false);

    if (!bUseBuiltinXOR)
    {
        // get read access to target - XOR unfortunately needs that
        cairo_surface_t* pRenderTarget(pTarget);

        if (CAIRO_SURFACE_TYPE_IMAGE != cairo_surface_get_type(pRenderTarget))
        {
            pRenderTarget = cairo_surface_map_to_image(pRenderTarget, nullptr);
        }

        // iterate over pre-rendered pContent (call it Front)
        const sal_uInt32 nFrontWidth(cairo_image_surface_get_width(pContent));
        const sal_uInt32 nFrontHeight(cairo_image_surface_get_height(pContent));
        const sal_uInt32 nFrontStride(cairo_image_surface_get_stride(pContent));
        unsigned char* pFrontDataRoot(cairo_image_surface_get_data(pContent));

        // in parallel, iterate over original data (call it Back)
        const sal_uInt32 nBackOffX(floor(aVisibleRange.getMinX()));
        const sal_uInt32 nBackOffY(floor(aVisibleRange.getMinY()));
        const sal_uInt32 nBackStride(cairo_image_surface_get_stride(pRenderTarget));
        unsigned char* pBackDataRoot(cairo_image_surface_get_data(pRenderTarget));

        if (nullptr != pFrontDataRoot && nullptr != pBackDataRoot)
        {
            for (sal_uInt32 y(0); y < nFrontHeight; ++y)
            {
                // get mem locations
                unsigned char* pFrontData(pFrontDataRoot + (nFrontStride * y));
                unsigned char* pBackData(pBackDataRoot + (nBackStride * (y + nBackOffY))
                                         + (nBackOffX * 4));

                for (sal_uInt32 x(0); x < nFrontWidth; ++x)
                {
                    // do not forget pre-multiply -> need to get both alphas
                    const sal_uInt8 nBackAlpha(pBackData[SVP_CAIRO_ALPHA]);
                    const sal_uInt8 nFrontAlpha(pFrontData[SVP_CAIRO_ALPHA]);

                    // only something to do if not fully transparent
                    if (0 != nFrontAlpha)
                    {
                        sal_uInt8 nFrontB(pFrontData[SVP_CAIRO_BLUE]);
                        sal_uInt8 nFrontG(pFrontData[SVP_CAIRO_GREEN]);
                        sal_uInt8 nFrontR(pFrontData[SVP_CAIRO_RED]);

                        if (255 != nFrontAlpha)
                        {
                            nFrontB = vcl::bitmap::unpremultiply(nFrontAlpha, nFrontB);
                            nFrontG = vcl::bitmap::unpremultiply(nFrontAlpha, nFrontG);
                            nFrontR = vcl::bitmap::unpremultiply(nFrontAlpha, nFrontR);
                        }

                        sal_uInt8 nBackB(pBackData[SVP_CAIRO_BLUE]);
                        sal_uInt8 nBackG(pBackData[SVP_CAIRO_GREEN]);
                        sal_uInt8 nBackR(pBackData[SVP_CAIRO_RED]);

                        if (255 != nBackAlpha)
                        {
                            nBackB = vcl::bitmap::unpremultiply(nBackAlpha, nBackB);
                            nBackG = vcl::bitmap::unpremultiply(nBackAlpha, nBackG);
                            nBackR = vcl::bitmap::unpremultiply(nBackAlpha, nBackR);
                        }

                        // create XOR r,g,b
                        const sal_uInt8 b(nFrontB ^ nBackB);
                        const sal_uInt8 g(nFrontG ^ nBackG);
                        const sal_uInt8 r(nFrontR ^ nBackR);

                        // write back
                        if (255 == nFrontAlpha)
                        {
                            pFrontData[SVP_CAIRO_BLUE] = b;
                            pFrontData[SVP_CAIRO_GREEN] = g;
                            pFrontData[SVP_CAIRO_RED] = r;
                        }
                        else
                        {
                            pFrontData[SVP_CAIRO_BLUE] = vcl::bitmap::premultiply(nFrontAlpha, b);
                            pFrontData[SVP_CAIRO_GREEN] = vcl::bitmap::premultiply(nFrontAlpha, g);
                            pFrontData[SVP_CAIRO_RED] = vcl::bitmap::premultiply(nFrontAlpha, r);
                        }
                    }

                    // advance memory
                    pBackData += 4;
                    pFrontData += 4;
                }
            }

            cairo_surface_mark_dirty(pContent);
        }

        if (pRenderTarget != pTarget)
        {
            // cleanup mapping for read access to target
            cairo_surface_unmap_image(pTarget, pRenderTarget);
        }
    }

    // draw XOR to target
    cairo_set_source_surface(mpRT, pContent, aVisibleRange.getMinX(), aVisibleRange.getMinY());
    cairo_rectangle(mpRT, aVisibleRange.getMinX(), aVisibleRange.getMinY(),
                    aVisibleRange.getWidth(), aVisibleRange.getHeight());

    if (bUseBuiltinXOR)
    {
        cairo_set_operator(mpRT, CAIRO_OPERATOR_XOR);
    }

    cairo_fill(mpRT);

    // cleanup temporary surface
    cairo_surface_destroy(pContent);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processMaskPrimitive2D(
    const primitive2d::MaskPrimitive2D& rMaskCandidate)
{
    if (rMaskCandidate.getChildren().empty())
    {
        // no content, done
        return;
    }

    basegfx::B2DPolyPolygon aMask(rMaskCandidate.getMask());

    if (!aMask.count())
    {
        // no mask (so nothing inside), done
        return;
    }

    // calculate visible range
    basegfx::B2DRange aMaskRange(aMask.getB2DRange());
    aMaskRange.transform(getViewInformation2D().getObjectToViewTransformation());
    if (!getDiscreteViewRange(mpRT).overlaps(aMaskRange))
    {
        // not visible, done
        return;
    }

    cairo_save(mpRT);

    // set linear transformation
    cairo_matrix_t aMatrix;
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);
    cairo_set_matrix(mpRT, &aMatrix);

    // create path geometry and put mask as path
    cairo_new_path(mpRT);
    getOrCreateFillGeometry(mpRT, aMask);

    // clip to this mask (also reset path, cairo_clip does not consume it)
    cairo_clip(mpRT);
    cairo_new_path(mpRT);

    // reset transformation to not have it set when processing
    // child content below (was only used to set clip path)
    cairo_identity_matrix(mpRT);

    // process sub-content (that shall be masked)
    process(rMaskCandidate.getChildren());

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processModifiedColorPrimitive2D(
    const primitive2d::ModifiedColorPrimitive2D& rModifiedCandidate)
{
    // standard implementation
    if (!rModifiedCandidate.getChildren().empty())
    {
        maBColorModifierStack.push(rModifiedCandidate.getColorModifier());
        process(rModifiedCandidate.getChildren());
        maBColorModifierStack.pop();
    }
}

void CairoPixelProcessor2D::processTransformPrimitive2D(
    const primitive2d::TransformPrimitive2D& rTransformCandidate)
{
    // standard implementation
    // remember current transformation and ViewInformation
    const geometry::ViewInformation2D aLastViewInformation2D(getViewInformation2D());

    // create new transformations for local ViewInformation2D
    geometry::ViewInformation2D aViewInformation2D(getViewInformation2D());
    aViewInformation2D.setObjectTransformation(getViewInformation2D().getObjectTransformation()
                                               * rTransformCandidate.getTransformation());
    updateViewInformation(aViewInformation2D);

    // process content
    process(rTransformCandidate.getChildren());

    // restore transformations
    updateViewInformation(aLastViewInformation2D);
}

void CairoPixelProcessor2D::processUnifiedTransparencePrimitive2D(
    const primitive2d::UnifiedTransparencePrimitive2D& rTransCandidate)
{
    if (rTransCandidate.getChildren().empty())
    {
        // no content, done
        return;
    }

    if (0.0 == rTransCandidate.getTransparence())
    {
        // not transparent at all, use content
        process(rTransCandidate.getChildren());
        return;
    }

    if (rTransCandidate.getTransparence() < 0.0 || rTransCandidate.getTransparence() > 1.0)
    {
        // invalid transparence, done
        return;
    }

    cairo_save(mpRT);

    // calculate visible range, create only for that range
    basegfx::B2DRange aDiscreteRange(
        rTransCandidate.getChildren().getB2DRange(getViewInformation2D()));
    aDiscreteRange.transform(getViewInformation2D().getObjectToViewTransformation());
    basegfx::B2DRange aVisibleRange(aDiscreteRange);
    aVisibleRange.intersect(getDiscreteViewRange(mpRT));

    if (aVisibleRange.isEmpty())
    {
        // not visible, done
        return;
    }

    // create embedding transformation for sub-surface
    const basegfx::B2DHomMatrix aEmbedTransform(basegfx::utils::createTranslateB2DHomMatrix(
        -aVisibleRange.getMinX(), -aVisibleRange.getMinY()));
    geometry::ViewInformation2D aViewInformation2D(getViewInformation2D());
    aViewInformation2D.setViewTransformation(aEmbedTransform
                                             * getViewInformation2D().getViewTransformation());

    // draw content to temporary surface
    cairo_surface_t* pTarget(cairo_get_target(mpRT));
    const double fContainedWidth(ceil(aVisibleRange.getWidth()));
    const double fContainedHeight(ceil(aVisibleRange.getHeight()));
    cairo_surface_t* pContent(cairo_surface_create_similar(
        pTarget, cairo_surface_get_content(pTarget), fContainedWidth, fContainedHeight));
    CairoPixelProcessor2D aContent(aViewInformation2D, pContent);
    aContent.process(rTransCandidate.getChildren());

    // paint temporary surface to target with fixed transparence
    cairo_set_source_surface(mpRT, pContent, aVisibleRange.getMinX(), aVisibleRange.getMinY());
    cairo_paint_with_alpha(mpRT, 1.0 - rTransCandidate.getTransparence());

    // cleanup temporary surface
    cairo_surface_destroy(pContent);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processMarkerArrayPrimitive2D(
    const primitive2d::MarkerArrayPrimitive2D& rMarkerArrayCandidate)
{
    const std::vector<basegfx::B2DPoint>& rPositions(rMarkerArrayCandidate.getPositions());

    if (rPositions.empty())
    {
        // no geometry, done
        return;
    }

    const BitmapEx& rMarker(rMarkerArrayCandidate.getMarker());

    if (rMarker.IsEmpty())
    {
        // no marker defined, done
        return;
    }

    // access or create cairo bitmap data
    const BitmapEx& rBitmapEx(rMarkerArrayCandidate.getMarker());
    std::shared_ptr<CairoSurfaceHelper> aCairoSurfaceHelper(
        getOrCreateCairoSurfaceHelper(rBitmapEx));
    if (!aCairoSurfaceHelper)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No SurfaceHelper from BitmapEx (!)");
        return;
    }

    // do not use dimensions, these are usually small instances
    cairo_surface_t* pTarget(aCairoSurfaceHelper->getCairoSurface());
    if (nullptr == pTarget)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No CairoSurface from BitmapEx SurfaceHelper (!)");
        return;
    }

    const sal_uInt32 nWidth(cairo_image_surface_get_width(pTarget));
    const sal_uInt32 nHeight(cairo_image_surface_get_height(pTarget));
    const tools::Long nMiX((nWidth / 2) + 1);
    const tools::Long nMiY((nHeight / 2) + 1);

    cairo_save(mpRT);
    cairo_identity_matrix(mpRT);
    const cairo_antialias_t eOldAAMode(cairo_get_antialias(mpRT));
    cairo_set_antialias(mpRT, CAIRO_ANTIALIAS_NONE);

    for (auto const& pos : rPositions)
    {
        const basegfx::B2DPoint aDiscretePos(getViewInformation2D().getObjectToViewTransformation()
                                             * pos);
        const double fX(ceil(aDiscretePos.getX()));
        const double fY(ceil(aDiscretePos.getY()));

        cairo_set_source_surface(mpRT, pTarget, fX - nMiX, fY - nMiY);
        cairo_paint(mpRT);
    }

    cairo_set_antialias(mpRT, eOldAAMode);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processBackgroundColorPrimitive2D(
    const primitive2d::BackgroundColorPrimitive2D& rBackgroundColorCandidate)
{
    // check for allowed range [0.0 .. 1.0[
    if (rBackgroundColorCandidate.getTransparency() < 0.0
        || rBackgroundColorCandidate.getTransparency() >= 1.0)
        return;

    cairo_save(mpRT);
    const basegfx::BColor aFillColor(
        maBColorModifierStack.getModifiedColor(rBackgroundColorCandidate.getBColor()));
    cairo_set_source_rgba(mpRT, aFillColor.getRed(), aFillColor.getGreen(), aFillColor.getBlue(),
                          1.0 - rBackgroundColorCandidate.getTransparency());
    // to also copy alpha part of color, see cairo docu. Will be reset by restore below
    cairo_set_operator(mpRT, CAIRO_OPERATOR_SOURCE);
    cairo_paint(mpRT);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processPolygonStrokePrimitive2D(
    const primitive2d::PolygonStrokePrimitive2D& rPolygonStrokeCandidate)
{
    const basegfx::B2DPolygon& rPolygon(rPolygonStrokeCandidate.getB2DPolygon());
    const attribute::LineAttribute& rLineAttribute(rPolygonStrokeCandidate.getLineAttribute());

    if (!rPolygon.count() || rLineAttribute.getWidth() < 0.0)
    {
        // no geometry, done
        return;
    }

    // get some values early that might be used for decisions
    const bool bHairline(0.0 == rLineAttribute.getWidth());
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    const double fDiscreteLineWidth(
        bHairline
            ? 1.0
            : (rObjectToView * basegfx::B2DVector(rLineAttribute.getWidth(), 0.0)).getLength());

    // Here for every combination which the system-specific implementation is not
    // capable of visualizing, use the (for decomposable Primitives always possible)
    // fallback to the decomposition.
    if (basegfx::B2DLineJoin::NONE == rLineAttribute.getLineJoin() && fDiscreteLineWidth > 1.5)
    {
        // basegfx::B2DLineJoin::NONE is special for our office, no other GraphicSystem
        // knows that (so far), so fallback to decomposition. This is only needed if
        // LineJoin will be used, so also check for discrete LineWidth before falling back
        process(rPolygonStrokeCandidate);
        return;
    }

    // This is a method every system-specific implementation of a decomposable Primitive
    // can use to allow simple optical control of paint implementation:
    // Create a copy, e.g. change color to 'red' as here and paint before the system
    // paints it using the decomposition. That way you can - if active - directly
    // optically compare if the system-specific solution is geometrically identical to
    // the decomposition (which defines our interpretation that we need to visualize).
    // Look below in the impl for bRenderDecomposeForCompareInRed to see that in that case
    // we create a half-transparent paint to better support visual control
    static bool bRenderDecomposeForCompareInRed(false);

    if (bRenderDecomposeForCompareInRed)
    {
        const attribute::LineAttribute aRed(
            basegfx::BColor(1.0, 0.0, 0.0), rLineAttribute.getWidth(), rLineAttribute.getLineJoin(),
            rLineAttribute.getLineCap(), rLineAttribute.getMiterMinimumAngle());
        rtl::Reference<primitive2d::PolygonStrokePrimitive2D> xCopy(
            new primitive2d::PolygonStrokePrimitive2D(
                rPolygonStrokeCandidate.getB2DPolygon(), aRed,
                rPolygonStrokeCandidate.getStrokeAttribute()));
        process(*xCopy);
    }

    cairo_save(mpRT);

    // set linear transformation
    cairo_matrix_t aMatrix;
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);
    cairo_set_matrix(mpRT, &aMatrix);

    // setup line attributes
    cairo_line_join_t eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
    switch (rLineAttribute.getLineJoin())
    {
        case basegfx::B2DLineJoin::Bevel:
            eCairoLineJoin = CAIRO_LINE_JOIN_BEVEL;
            break;
        case basegfx::B2DLineJoin::Round:
            eCairoLineJoin = CAIRO_LINE_JOIN_ROUND;
            break;
        case basegfx::B2DLineJoin::NONE:
        case basegfx::B2DLineJoin::Miter:
            eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
            break;
    }
    cairo_set_line_join(mpRT, eCairoLineJoin);

    // convert miter minimum angle to miter limit
    double fMiterLimit
        = 1.0 / sin(std::max(rLineAttribute.getMiterMinimumAngle(), 0.01 * M_PI) / 2.0);
    cairo_set_miter_limit(mpRT, fMiterLimit);

    // setup cap attribute
    cairo_line_cap_t eCairoLineCap(CAIRO_LINE_CAP_BUTT);
    switch (rLineAttribute.getLineCap())
    {
        default: // css::drawing::LineCap_BUTT:
        {
            eCairoLineCap = CAIRO_LINE_CAP_BUTT;
            break;
        }
        case css::drawing::LineCap_ROUND:
        {
            eCairoLineCap = CAIRO_LINE_CAP_ROUND;
            break;
        }
        case css::drawing::LineCap_SQUARE:
        {
            eCairoLineCap = CAIRO_LINE_CAP_SQUARE;
            break;
        }
    }
    cairo_set_line_cap(mpRT, eCairoLineCap);

    // determine & set color
    basegfx::BColor aLineColor(maBColorModifierStack.getModifiedColor(rLineAttribute.getColor()));
    if (bRenderDecomposeForCompareInRed)
        aLineColor.setRed(0.5);
    cairo_set_source_rgb(mpRT, aLineColor.getRed(), aLineColor.getGreen(), aLineColor.getBlue());

    // process/set LineWidth
    const double fObjectLineWidth(
        bHairline ? (getViewInformation2D().getInverseObjectToViewTransformation()
                     * basegfx::B2DVector(1.0, 0.0))
                        .getLength()
                  : rLineAttribute.getWidth());
    cairo_set_line_width(mpRT, fObjectLineWidth);

    // check stroke
    const attribute::StrokeAttribute& rStrokeAttribute(
        rPolygonStrokeCandidate.getStrokeAttribute());
    const bool bDashUsed(!rStrokeAttribute.isDefault()
                         && !rStrokeAttribute.getDotDashArray().empty()
                         && 0.0 < rStrokeAttribute.getFullDotDashLen());
    if (bDashUsed)
    {
        const std::vector<double>& rStroke = rStrokeAttribute.getDotDashArray();
        cairo_set_dash(mpRT, rStroke.data(), rStroke.size(), 0.0);
    }

    // create path geometry and put mask as path
    cairo_new_path(mpRT);
    getOrCreatePathGeometry(mpRT, rPolygon, getViewInformation2D());

    // render
    cairo_stroke(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processLineRectanglePrimitive2D(
    const primitive2d::LineRectanglePrimitive2D& rLineRectanglePrimitive2D)
{
    if (rLineRectanglePrimitive2D.getB2DRange().isEmpty())
    {
        // no geometry, done
        return;
    }

    cairo_save(mpRT);

    cairo_matrix_t aMatrix;
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);

    // set linear transformation
    cairo_set_matrix(mpRT, &aMatrix);

    const basegfx::BColor aHairlineColor(
        maBColorModifierStack.getModifiedColor(rLineRectanglePrimitive2D.getBColor()));
    cairo_set_source_rgb(mpRT, aHairlineColor.getRed(), aHairlineColor.getGreen(),
                         aHairlineColor.getBlue());

    const double fDiscreteLineWidth((getViewInformation2D().getInverseObjectToViewTransformation()
                                     * basegfx::B2DVector(1.0, 0.0))
                                        .getLength());
    cairo_set_line_width(mpRT, fDiscreteLineWidth);

    const basegfx::B2DRange& rRange(rLineRectanglePrimitive2D.getB2DRange());
    cairo_rectangle(mpRT, rRange.getMinX(), rRange.getMinY(), rRange.getWidth(),
                    rRange.getHeight());
    cairo_stroke(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFilledRectanglePrimitive2D(
    const primitive2d::FilledRectanglePrimitive2D& rFilledRectanglePrimitive2D)
{
    if (rFilledRectanglePrimitive2D.getB2DRange().isEmpty())
    {
        // no geometry, done
        return;
    }

    cairo_save(mpRT);

    cairo_matrix_t aMatrix;
    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    cairo_matrix_init(&aMatrix, rObjectToView.a(), rObjectToView.b(), rObjectToView.c(),
                      rObjectToView.d(), rObjectToView.e() + fAAOffset,
                      rObjectToView.f() + fAAOffset);

    // set linear transformation
    cairo_set_matrix(mpRT, &aMatrix);

    const basegfx::BColor aFillColor(
        maBColorModifierStack.getModifiedColor(rFilledRectanglePrimitive2D.getBColor()));
    cairo_set_source_rgb(mpRT, aFillColor.getRed(), aFillColor.getGreen(), aFillColor.getBlue());

    const basegfx::B2DRange& rRange(rFilledRectanglePrimitive2D.getB2DRange());
    cairo_rectangle(mpRT, rRange.getMinX(), rRange.getMinY(), rRange.getWidth(),
                    rRange.getHeight());
    cairo_fill(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processSingleLinePrimitive2D(
    const primitive2d::SingleLinePrimitive2D& rSingleLinePrimitive2D)
{
    cairo_save(mpRT);

    const basegfx::BColor aLineColor(
        maBColorModifierStack.getModifiedColor(rSingleLinePrimitive2D.getBColor()));
    cairo_set_source_rgb(mpRT, aLineColor.getRed(), aLineColor.getGreen(), aLineColor.getBlue());

    const double fAAOffset(getViewInformation2D().getUseAntiAliasing() ? 0.5 : 0.0);
    const basegfx::B2DHomMatrix& rObjectToView(
        getViewInformation2D().getObjectToViewTransformation());
    const basegfx::B2DPoint aStart(rObjectToView * rSingleLinePrimitive2D.getStart());
    const basegfx::B2DPoint aEnd(rObjectToView * rSingleLinePrimitive2D.getEnd());

    cairo_set_line_width(mpRT, 1.0f);

    cairo_move_to(mpRT, aStart.getX() + fAAOffset, aStart.getY() + fAAOffset);
    cairo_line_to(mpRT, aEnd.getX() + fAAOffset, aEnd.getY() + fAAOffset);
    cairo_stroke(mpRT);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGraphicPrimitive2D(
    const primitive2d::FillGraphicPrimitive2D& rFillGraphicPrimitive2D)
{
    if (rFillGraphicPrimitive2D.getTransparency() < 0.0
        || rFillGraphicPrimitive2D.getTransparency() > 1.0)
    {
        // invalid transparence, done
        return;
    }

    BitmapEx aPreparedBitmap;
    basegfx::B2DRange aFillUnitRange(rFillGraphicPrimitive2D.getFillGraphic().getGraphicRange());
    constexpr double fBigDiscreteArea(300.0 * 300.0);

    // use tooling to do various checks and prepare tiled rendering, see
    // description of method, parameters and return value there
    if (!prepareBitmapForDirectRender(rFillGraphicPrimitive2D, getViewInformation2D(),
                                      aPreparedBitmap, aFillUnitRange, fBigDiscreteArea))
    {
        // no output needed, done
        return;
    }

    if (aPreparedBitmap.IsEmpty())
    {
        // output needed and Bitmap data empty, so no bitmap data based
        // tiled rendering is suggested. Use fallback for paint (decomposition)
        process(rFillGraphicPrimitive2D);
        return;
    }

    // render tiled using the prepared Bitmap data
    if (maBColorModifierStack.count())
    {
        // need to apply ColorModifier to Bitmap data
        aPreparedBitmap = aPreparedBitmap.ModifyBitmapEx(maBColorModifierStack);

        if (aPreparedBitmap.IsEmpty())
        {
            // color gets completely replaced, get it (any input works)
            const basegfx::BColor aModifiedColor(
                maBColorModifierStack.getModifiedColor(basegfx::BColor()));

            // use unit geometry as fallback object geometry. Do *not*
            // transform, the below used method will use the already
            // correctly initialized local ViewInformation
            basegfx::B2DPolygon aPolygon(basegfx::utils::createUnitPolygon());

            // what we still need to apply is the object transform from the
            // local primitive, that is not part of DisplayInfo yet
            aPolygon.transform(rFillGraphicPrimitive2D.getTransformation());

            if (rFillGraphicPrimitive2D.hasTransparency())
            {
                rtl::Reference<primitive2d::PolyPolygonRGBAPrimitive2D> aTemp(
                    new primitive2d::PolyPolygonRGBAPrimitive2D(
                        basegfx::B2DPolyPolygon(aPolygon), aModifiedColor,
                        rFillGraphicPrimitive2D.getTransparency()));
                // draw as colored and transparent Polygon, done
                processPolyPolygonRGBAPrimitive2D(*aTemp);
            }
            else
            {
                rtl::Reference<primitive2d::PolyPolygonColorPrimitive2D> aTemp(
                    new primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aPolygon),
                                                                 aModifiedColor));
                // draw as colored Polygon, done
                processPolyPolygonColorPrimitive2D(*aTemp);
            }

            return;
        }
    }

    // access or create cairo bitmap data
    std::shared_ptr<CairoSurfaceHelper> aCairoSurfaceHelper(
        getOrCreateCairoSurfaceHelper(aPreparedBitmap));
    if (!aCairoSurfaceHelper)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No SurfaceHelper from BitmapEx (!)");
        return;
    }

    // work with dimensions in discrete target pixels to use evtl. MipMap pre-scale
    const basegfx::B2DHomMatrix aLocalTransform(
        getViewInformation2D().getObjectToViewTransformation()
        * rFillGraphicPrimitive2D.getTransformation());
    const tools::Long nDestWidth(
        (aLocalTransform * basegfx::B2DVector(aFillUnitRange.getWidth(), 0.0)).getLength());
    const tools::Long nDestHeight(
        (aLocalTransform * basegfx::B2DVector(0.0, aFillUnitRange.getHeight())).getLength());

    cairo_surface_t* pTarget(aCairoSurfaceHelper->getCairoSurface(nDestWidth, nDestHeight));
    if (nullptr == pTarget)
    {
        SAL_WARN("drawinglayer", "SDPRCairo: No CairoSurface from BitmapEx SurfaceHelper (!)");
        return;
    }

    cairo_save(mpRT);

    // set linear transformation - no fAAOffset for bitmap data
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aLocalTransform.a(), aLocalTransform.b(), aLocalTransform.c(),
                      aLocalTransform.d(), aLocalTransform.e(), aLocalTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);

    const sal_uInt32 nWidth(cairo_image_surface_get_width(pTarget));
    const sal_uInt32 nHeight(cairo_image_surface_get_height(pTarget));

    cairo_set_source_surface(mpRT, pTarget, 0, 0);

    // get the pattern created by cairo_set_source_surface and
    // it's transformation
    cairo_pattern_t* sourcepattern = cairo_get_source(mpRT);
    cairo_pattern_get_matrix(sourcepattern, &aMatrix);

    // clip for RGBA (see other places)
    if (CAIRO_FORMAT_ARGB32 == cairo_image_surface_get_format(pTarget))
    {
        cairo_rectangle(mpRT, 0, 0, 1, 1);
        cairo_clip(mpRT);
    }

    // create transformation for source pattern (inverse, see
    // cairo docu: uses user space to pattern space transformation)
    cairo_matrix_init_scale(&aMatrix, nWidth / aFillUnitRange.getWidth(),
                            nHeight / aFillUnitRange.getHeight());
    cairo_matrix_translate(&aMatrix, -aFillUnitRange.getMinX(), -aFillUnitRange.getMinY());

    // set source pattern transform & activate pattern repeat
    cairo_pattern_set_matrix(sourcepattern, &aMatrix);
    cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);

    // paint
    if (rFillGraphicPrimitive2D.hasTransparency())
        cairo_paint_with_alpha(mpRT, 1.0 - rFillGraphicPrimitive2D.getTransparency());
    else
        cairo_paint(mpRT);

    static bool bRenderTransformationBounds(false);
    if (bRenderTransformationBounds)
    {
        cairo_set_source_rgba(mpRT, 0, 1, 0, 0.8);
        impl_cairo_set_hairline(mpRT, getViewInformation2D());
        // full object
        cairo_rectangle(mpRT, 0, 0, 1, 1);
        // outline of pattern root image
        cairo_rectangle(mpRT, aFillUnitRange.getMinX(), aFillUnitRange.getMinY(),
                        aFillUnitRange.getWidth(), aFillUnitRange.getHeight());
        cairo_stroke(mpRT);
    }

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D_drawOutputRange(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    cairo_save(mpRT);

    // fill simple rect with outer color
    const basegfx::BColor aColor(
        maBColorModifierStack.getModifiedColor(rFillGradientPrimitive2D.getOuterColor()));

    if (rFillGradientPrimitive2D.hasAlphaGradient())
    {
        const attribute::FillGradientAttribute& rAlphaGradient(
            rFillGradientPrimitive2D.getAlphaGradient());
        double fLuminance(0.0);

        if (!rAlphaGradient.getColorStops().empty())
        {
            if (css::awt::GradientStyle_AXIAL == rAlphaGradient.getStyle())
                fLuminance = rAlphaGradient.getColorStops().back().getStopColor().luminance();
            else
                fLuminance = rAlphaGradient.getColorStops().front().getStopColor().luminance();
        }

        cairo_set_source_rgba(mpRT, aColor.getRed(), aColor.getGreen(), aColor.getBlue(),
                              1.0 - fLuminance);
    }
    else
    {
        cairo_set_source_rgb(mpRT, aColor.getRed(), aColor.getGreen(), aColor.getBlue());
    }

    const basegfx::B2DHomMatrix aTrans(getViewInformation2D().getObjectToViewTransformation());
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aTrans.a(), aTrans.b(), aTrans.c(), aTrans.d(), aTrans.e(),
                      aTrans.f());
    cairo_set_matrix(mpRT, &aMatrix);

    const basegfx::B2DRange& rRange(rFillGradientPrimitive2D.getOutputRange());
    cairo_rectangle(mpRT, rRange.getMinX(), rRange.getMinY(), rRange.getWidth(),
                    rRange.getHeight());
    cairo_fill(mpRT);

    cairo_restore(mpRT);
}

bool CairoPixelProcessor2D::processFillGradientPrimitive2D_isCompletelyBordered(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    const attribute::FillGradientAttribute& rFillGradient(
        rFillGradientPrimitive2D.getFillGradient());
    const double fBorder(rFillGradient.getBorder());

    // check if completely 'bordered out'. This can be the case for all
    // types of gradients
    if (basegfx::fTools::less(fBorder, 1.0) && fBorder >= 0.0)
    {
        // no, we have visible content besides border
        return false;
    }

    // draw all-covering polygon using getOuterColor and getOutputRange
    processFillGradientPrimitive2D_drawOutputRange(rFillGradientPrimitive2D);
    return true;
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D_linear_axial(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    const attribute::FillGradientAttribute& rFillGradient(
        rFillGradientPrimitive2D.getFillGradient());
    assert(!rFillGradientPrimitive2D.hasAlphaGradient()
           || rFillGradient.sameDefinitionThanAlpha(rFillGradientPrimitive2D.getAlphaGradient()));
    assert(
        (css::awt::GradientStyle_LINEAR == rFillGradientPrimitive2D.getFillGradient().getStyle()
         || css::awt::GradientStyle_AXIAL == rFillGradientPrimitive2D.getFillGradient().getStyle())
        && "SDPRCairo: Helper allows only SPECIFIED types (!)");
    cairo_save(mpRT);

    // need to do 'antique' stuff adaptions for rotate/transitionStart in object coordinates
    // (DefinitionRange) to have the right 'bending' on rotation
    basegfx::B2DRange aAdaptedRange(rFillGradientPrimitive2D.getDefinitionRange());
    const double fAngle(basegfx::normalizeToRange((2 * M_PI) - rFillGradient.getAngle(), 2 * M_PI));
    const bool bAngle(!basegfx::fTools::equalZero(fAngle));
    const basegfx::B2DPoint aCenter(aAdaptedRange.getCenter());

    // pack rotation and offset into a transformation covering that part
    basegfx::B2DHomMatrix aRotation(basegfx::utils::createRotateAroundPoint(aCenter, fAngle));

    // create local transform to work in object coordinates based on OutputRange,
    // combine with rotation - that way we can then just draw into AdaptedRange
    basegfx::B2DHomMatrix aLocalTransform(getViewInformation2D().getObjectToViewTransformation()
                                          * aRotation);
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aLocalTransform.a(), aLocalTransform.b(), aLocalTransform.c(),
                      aLocalTransform.d(), aLocalTransform.e(), aLocalTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);

    if (bAngle)
    {
        // expand Range by rotating
        aAdaptedRange.transform(aRotation);
    }

    // create linear pattern in unit coordinates in y-direction
    cairo_pattern_t* pPattern(
        cairo_pattern_create_linear(aAdaptedRange.getCenterX(), aAdaptedRange.getMinY(),
                                    aAdaptedRange.getCenterX(), aAdaptedRange.getMaxY()));

    // get color stops (make copy, might have to be changed)
    basegfx::BColorStops aBColorStops(rFillGradient.getColorStops());
    basegfx::BColorStops aBColorStopsAlpha;
    const bool bHasAlpha(rFillGradientPrimitive2D.hasAlphaGradient());
    if (bHasAlpha)
        aBColorStopsAlpha = rFillGradientPrimitive2D.getAlphaGradient().getColorStops();
    const bool bAxial(css::awt::GradientStyle_AXIAL == rFillGradient.getStyle());

    // get and apply border - create soace at start in gradient
    const double fBorder(std::max(std::min(rFillGradient.getBorder(), 1.0), 0.0));
    if (!basegfx::fTools::equalZero(fBorder))
    {
        if (bAxial)
        {
            aBColorStops.reverseColorStops();
            if (bHasAlpha)
                aBColorStopsAlpha.reverseColorStops();
        }

        aBColorStops.createSpaceAtStart(fBorder);
        if (bHasAlpha)
            aBColorStopsAlpha.createSpaceAtStart(fBorder);

        if (bAxial)
        {
            aBColorStops.reverseColorStops();
            if (bHasAlpha)
                aBColorStopsAlpha.reverseColorStops();
        }
    }

    if (bAxial)
    {
        // expand with mirrored ColorStops to create axial
        aBColorStops.doApplyAxial();
        if (bHasAlpha)
            aBColorStopsAlpha.doApplyAxial();
    }

    // Apply steps if used to 'emulate' LO's 'discrete step' feature
    if (rFillGradient.getSteps())
    {
        aBColorStops.doApplySteps(rFillGradient.getSteps());
        if (bHasAlpha)
            aBColorStopsAlpha.doApplySteps(rFillGradient.getSteps());
    }

    // add color stops
    for (size_t a(0); a < aBColorStops.size(); a++)
    {
        const basegfx::BColorStop& rStop(aBColorStops[a]);
        const basegfx::BColor aColor(maBColorModifierStack.getModifiedColor(rStop.getStopColor()));

        if (bHasAlpha)
        {
            const basegfx::BColor aAlpha(aBColorStopsAlpha[a].getStopColor());
            cairo_pattern_add_color_stop_rgba(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                              aColor.getGreen(), aColor.getBlue(),
                                              1.0 - aAlpha.luminance());
        }
        else
        {
            if (rFillGradientPrimitive2D.hasTransparency())
            {
                cairo_pattern_add_color_stop_rgba(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                                  aColor.getGreen(), aColor.getBlue(),
                                                  1.0 - rFillGradientPrimitive2D.getTransparency());
            }
            else
            {
                cairo_pattern_add_color_stop_rgb(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                                 aColor.getGreen(), aColor.getBlue());
            }
        }
    }

    // draw OutRange
    basegfx::B2DRange aOutRange(rFillGradientPrimitive2D.getOutputRange());
    if (bAngle)
    {
        // expand backwards to cover all area needed for OutputRange
        aRotation.invert();
        aOutRange.transform(aRotation);
    }
    cairo_rectangle(mpRT, aOutRange.getMinX(), aOutRange.getMinY(), aOutRange.getWidth(),
                    aOutRange.getHeight());
    cairo_set_source(mpRT, pPattern);
    cairo_fill(mpRT);

    // cleanup
    cairo_pattern_destroy(pPattern);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D_square_rect(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    if (rFillGradientPrimitive2D.hasAlphaGradient() || rFillGradientPrimitive2D.hasTransparency())
    {
        // Do not use direct alpha for this: It paints using four trapez that
        // do not add up at edges due to being painted AntiAliased; that means
        // common pixels do not add up, but blend by transparency, so leaving
        // visual traces -> process recursively
        process(rFillGradientPrimitive2D);
        return;
    }

    assert(
        (css::awt::GradientStyle_SQUARE == rFillGradientPrimitive2D.getFillGradient().getStyle()
         || css::awt::GradientStyle_RECT == rFillGradientPrimitive2D.getFillGradient().getStyle())
        && "SDPRCairo: Helper allows only SPECIFIED types (!)");
    cairo_save(mpRT);

    // draw all-covering polygon using getOuterColor and getOutputRange,
    // the partial paints below will not fill areas outside automatically
    // as happens in the other gradient paints
    processFillGradientPrimitive2D_drawOutputRange(rFillGradientPrimitive2D);

    // get DefinitionRange and adapt if needed
    basegfx::B2DRange aAdaptedRange(rFillGradientPrimitive2D.getDefinitionRange());
    const bool bSquare(css::awt::GradientStyle_SQUARE
                       == rFillGradientPrimitive2D.getFillGradient().getStyle());
    const basegfx::B2DPoint aCenter(aAdaptedRange.getCenter());
    bool bLandscape(false);
    double fSmallRadius(1.0);

    // get rotation and offset values
    const attribute::FillGradientAttribute& rFillGradient(
        rFillGradientPrimitive2D.getFillGradient());
    const double fAngle(basegfx::normalizeToRange((2 * M_PI) - rFillGradient.getAngle(), 2 * M_PI));
    const bool bAngle(!basegfx::fTools::equalZero(fAngle));
    const double fOffxsetX(std::max(std::min(rFillGradient.getOffsetX(), 1.0), 0.0));
    const double fOffxsetY(std::max(std::min(rFillGradient.getOffsetY(), 1.0), 0.0));

    if (bSquare)
    {
        // expand to make width == height
        const basegfx::B2DRange& rDefRange(rFillGradientPrimitive2D.getDefinitionRange());

        if (rDefRange.getWidth() > rDefRange.getHeight())
        {
            // landscape -> square
            const double fRadius(0.5 * rDefRange.getWidth());
            aAdaptedRange.expand(basegfx::B2DPoint(rDefRange.getMinX(), aCenter.getY() - fRadius));
            aAdaptedRange.expand(basegfx::B2DPoint(rDefRange.getMaxX(), aCenter.getY() + fRadius));
        }
        else
        {
            // portrait -> square
            const double fRadius(0.5 * rDefRange.getHeight());
            aAdaptedRange.expand(basegfx::B2DPoint(aCenter.getX() - fRadius, rDefRange.getMinY()));
            aAdaptedRange.expand(basegfx::B2DPoint(aCenter.getX() + fRadius, rDefRange.getMaxY()));
        }

        bLandscape = true;
        fSmallRadius = 0.5 * aAdaptedRange.getWidth();
    }
    else
    {
        if (bAngle)
        {
            // expand range using applied rotation
            aAdaptedRange.transform(basegfx::utils::createRotateAroundPoint(aCenter, fAngle));
        }

        // set local params as needed for non-square
        bLandscape = aAdaptedRange.getWidth() > aAdaptedRange.getHeight();
        fSmallRadius = 0.5 * (bLandscape ? aAdaptedRange.getHeight() : aAdaptedRange.getWidth());
    }

    // pack rotation and offset into a combined transformation that covers that parts
    basegfx::B2DHomMatrix aRotAndTranslate;
    aRotAndTranslate.translate(-aCenter.getX(), -aCenter.getY());
    if (bAngle)
        aRotAndTranslate.rotate(fAngle);
    aRotAndTranslate.translate(aAdaptedRange.getMinX() + (fOffxsetX * aAdaptedRange.getWidth()),
                               aAdaptedRange.getMinY() + (fOffxsetY * aAdaptedRange.getHeight()));

    // create local transform to work in object coordinates based on OutputRange,
    // combine with rotation and offset - that way we can then just draw into
    // AdaptedRange
    basegfx::B2DHomMatrix aLocalTransform(getViewInformation2D().getObjectToViewTransformation()
                                          * aRotAndTranslate);
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aLocalTransform.a(), aLocalTransform.b(), aLocalTransform.c(),
                      aLocalTransform.d(), aLocalTransform.e(), aLocalTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);

    // get color stops (make copy, might have to be changed)
    basegfx::BColorStops aBColorStops(rFillGradient.getColorStops());

    // apply BColorModifierStack early - the BColorStops are used multiple
    // times below, so do this only once
    if (0 != maBColorModifierStack.count())
    {
        aBColorStops.tryToApplyBColorModifierStack(maBColorModifierStack);
    }

    // get and apply border - create soace at start in gradient
    const double fBorder(std::max(std::min(rFillGradient.getBorder(), 1.0), 0.0));
    if (!basegfx::fTools::equalZero(fBorder))
    {
        aBColorStops.createSpaceAtStart(fBorder);
    }

    // Apply steps if used to 'emulate' LO's 'discrete step' feature
    if (rFillGradient.getSteps())
    {
        aBColorStops.doApplySteps(rFillGradient.getSteps());
    }

    // get half single pixel size to fill touching 'gaps'
    // NOTE: I formally used cairo_device_to_user_distance, but that
    // can indeed create negative sizes if the transformation e.g.
    // contains rotation(s). could use fabs(), but just rely on
    // linear algebra and use the (always positive) length of a vector
    const double fHalfPx((getViewInformation2D().getInverseObjectToViewTransformation()
                          * basegfx::B2DVector(1.0, 0.0))
                             .getLength());

    // draw top part trapez/triangle
    {
        cairo_move_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMinY());
        cairo_line_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMinY());
        cairo_line_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMinY() + fHalfPx);
        if (!bSquare && bLandscape)
        {
            cairo_line_to(mpRT, aAdaptedRange.getMaxX() - fSmallRadius, aCenter.getY() + fHalfPx);
            cairo_line_to(mpRT, aAdaptedRange.getMinX() + fSmallRadius, aCenter.getY() + fHalfPx);
        }
        else
        {
            cairo_line_to(mpRT, aCenter.getX(), aAdaptedRange.getMinY() + fSmallRadius + fHalfPx);
        }
        cairo_line_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMinY() + fHalfPx);
        cairo_close_path(mpRT);

        // create linear pattern in needed coordinates directly
        // NOTE: I *tried* to create in unit coordinates and adapt modifying and re-using
        // cairo_pattern_set_matrix - that *seems* to work but sometimes runs into
        // numerical problems -> probably cairo implementation. So stay safe and do
        // it the easy way, for the cost of re-creating gradient definitions (still cheap)
        cairo_pattern_t* pPattern(cairo_pattern_create_linear(
            aCenter.getX(), aAdaptedRange.getMinY(), aCenter.getX(),
            aAdaptedRange.getMinY()
                + (bLandscape ? aAdaptedRange.getHeight() * 0.5 : fSmallRadius)));
        for (const auto& aStop : aBColorStops)
        {
            const basegfx::BColor& rColor(aStop.getStopColor());
            cairo_pattern_add_color_stop_rgb(pPattern, aStop.getStopOffset(), rColor.getRed(),
                                             rColor.getGreen(), rColor.getBlue());
        }

        cairo_set_source(mpRT, pPattern);
        cairo_fill(mpRT);
        cairo_pattern_destroy(pPattern);
    }

    {
        // draw right part trapez/triangle
        cairo_move_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMinY());
        cairo_line_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMaxY());
        if (bSquare || bLandscape)
        {
            cairo_line_to(mpRT, aAdaptedRange.getMaxX() - fSmallRadius - fHalfPx, aCenter.getY());
        }
        else
        {
            cairo_line_to(mpRT, aCenter.getX() - fHalfPx, aAdaptedRange.getMaxY() - fSmallRadius);
            cairo_line_to(mpRT, aCenter.getX() - fHalfPx, aAdaptedRange.getMinY() + fSmallRadius);
        }
        cairo_close_path(mpRT);

        // create linear pattern in needed coordinates directly
        cairo_pattern_t* pPattern(cairo_pattern_create_linear(
            aAdaptedRange.getMaxX(), aCenter.getY(),
            aAdaptedRange.getMaxX() - (bLandscape ? fSmallRadius : aAdaptedRange.getWidth() * 0.5),
            aCenter.getY()));
        for (const auto& aStop : aBColorStops)
        {
            const basegfx::BColor& rColor(aStop.getStopColor());
            cairo_pattern_add_color_stop_rgb(pPattern, aStop.getStopOffset(), rColor.getRed(),
                                             rColor.getGreen(), rColor.getBlue());
        }

        cairo_set_source(mpRT, pPattern);
        cairo_fill(mpRT);
        cairo_pattern_destroy(pPattern);
    }

    {
        // draw bottom part trapez/triangle
        cairo_move_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMaxY());
        cairo_line_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMaxY());
        cairo_line_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMaxY() - fHalfPx);
        if (!bSquare && bLandscape)
        {
            cairo_line_to(mpRT, aAdaptedRange.getMinX() + fSmallRadius, aCenter.getY() - fHalfPx);
            cairo_line_to(mpRT, aAdaptedRange.getMaxX() - fSmallRadius, aCenter.getY() - fHalfPx);
        }
        else
        {
            cairo_line_to(mpRT, aCenter.getX(), aAdaptedRange.getMaxY() - fSmallRadius - fHalfPx);
        }
        cairo_line_to(mpRT, aAdaptedRange.getMaxX(), aAdaptedRange.getMaxY() - fHalfPx);
        cairo_close_path(mpRT);

        // create linear pattern in needed coordinates directly
        cairo_pattern_t* pPattern(cairo_pattern_create_linear(
            aCenter.getX(), aAdaptedRange.getMaxY(), aCenter.getX(),
            aAdaptedRange.getMaxY()
                - (bLandscape ? aAdaptedRange.getHeight() * 0.5 : fSmallRadius)));
        for (const auto& aStop : aBColorStops)
        {
            const basegfx::BColor& rColor(aStop.getStopColor());
            cairo_pattern_add_color_stop_rgb(pPattern, aStop.getStopOffset(), rColor.getRed(),
                                             rColor.getGreen(), rColor.getBlue());
        }

        cairo_set_source(mpRT, pPattern);
        cairo_fill(mpRT);
        cairo_pattern_destroy(pPattern);
    }

    {
        // draw left part trapez/triangle
        cairo_move_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMaxY());
        cairo_line_to(mpRT, aAdaptedRange.getMinX(), aAdaptedRange.getMinY());
        if (bSquare || bLandscape)
        {
            cairo_line_to(mpRT, aAdaptedRange.getMinX() + fSmallRadius + fHalfPx, aCenter.getY());
        }
        else
        {
            cairo_line_to(mpRT, aCenter.getX() + fHalfPx, aAdaptedRange.getMinY() + fSmallRadius);
            cairo_line_to(mpRT, aCenter.getX() + fHalfPx, aAdaptedRange.getMaxY() - fSmallRadius);
        }
        cairo_close_path(mpRT);

        // create linear pattern in needed coordinates directly
        cairo_pattern_t* pPattern(cairo_pattern_create_linear(
            aAdaptedRange.getMinX(), aCenter.getY(),
            aAdaptedRange.getMinX() + (bLandscape ? fSmallRadius : aAdaptedRange.getWidth() * 0.5),
            aCenter.getY()));
        for (const auto& aStop : aBColorStops)
        {
            const basegfx::BColor& rColor(aStop.getStopColor());
            cairo_pattern_add_color_stop_rgb(pPattern, aStop.getStopOffset(), rColor.getRed(),
                                             rColor.getGreen(), rColor.getBlue());
        }

        cairo_set_source(mpRT, pPattern);
        cairo_fill(mpRT);
        cairo_pattern_destroy(pPattern);
    }

    // cleanup
    // cairo_pattern_destroy(pPattern);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D_radial_elliptical(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    const attribute::FillGradientAttribute& rFillGradient(
        rFillGradientPrimitive2D.getFillGradient());
    assert(!rFillGradientPrimitive2D.hasAlphaGradient()
           || rFillGradient.sameDefinitionThanAlpha(rFillGradientPrimitive2D.getAlphaGradient()));
    assert((css::awt::GradientStyle_RADIAL == rFillGradientPrimitive2D.getFillGradient().getStyle()
            || css::awt::GradientStyle_ELLIPTICAL
                   == rFillGradientPrimitive2D.getFillGradient().getStyle())
           && "SDPRCairo: Helper allows only SPECIFIED types (!)");
    cairo_save(mpRT);

    // need to do 'antique' stuff adaptions for rotate/transitionStart in object coordinates
    // (DefinitionRange) to have the right 'bending' on rotation
    const basegfx::B2DRange rDefRange(rFillGradientPrimitive2D.getDefinitionRange());
    const basegfx::B2DPoint aCenter(rDefRange.getCenter());
    double fRadius(1.0);
    double fRatioElliptical(1.0);
    const bool bRadial(css::awt::GradientStyle_RADIAL == rFillGradient.getStyle());

    // use what is done in initEllipticalGradientInfo method to get as close as
    // possible to former stuff, expand AdaptedRange as needed
    if (bRadial)
    {
        const double fHalfOriginalDiag(std::hypot(rDefRange.getWidth(), rDefRange.getHeight())
                                       * 0.5);
        fRadius = fHalfOriginalDiag;
    }
    else
    {
        double fTargetSizeX(M_SQRT2 * rDefRange.getWidth());
        double fTargetSizeY(M_SQRT2 * rDefRange.getHeight());
        fRatioElliptical = fTargetSizeX / fTargetSizeY;
        fRadius = std::max(fTargetSizeX, fTargetSizeY) * 0.5;
    }

    // get rotation and offset values
    const double fAngle(basegfx::normalizeToRange((2 * M_PI) - rFillGradient.getAngle(), 2 * M_PI));
    const bool bAngle(!basegfx::fTools::equalZero(fAngle));
    const double fOffxsetX(std::max(std::min(rFillGradient.getOffsetX(), 1.0), 0.0));
    const double fOffxsetY(std::max(std::min(rFillGradient.getOffsetY(), 1.0), 0.0));

    // pack rotation and offset into a combined transformation covering that parts
    basegfx::B2DHomMatrix aRotAndTranslate;
    aRotAndTranslate.translate(-aCenter.getX(), -aCenter.getY());
    if (bAngle)
        aRotAndTranslate.rotate(fAngle);
    aRotAndTranslate.translate(rDefRange.getMinX() + (fOffxsetX * rDefRange.getWidth()),
                               rDefRange.getMinY() + (fOffxsetY * rDefRange.getHeight()));

    // create local transform to work in object coordinates based on OutputRange,
    // combine with rotation and offset - that way we can then just draw into
    // AdaptedRange
    basegfx::B2DHomMatrix aLocalTransform(getViewInformation2D().getObjectToViewTransformation()
                                          * aRotAndTranslate);
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, aLocalTransform.a(), aLocalTransform.b(), aLocalTransform.c(),
                      aLocalTransform.d(), aLocalTransform.e(), aLocalTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);

    // create linear pattern in unit coordinates in y-direction
    cairo_pattern_t* pPattern(cairo_pattern_create_radial(aCenter.getX(), aCenter.getY(), fRadius,
                                                          aCenter.getX(), aCenter.getY(), 0.0));

    // get color stops (make copy, might have to be changed)
    basegfx::BColorStops aBColorStops(rFillGradient.getColorStops());
    basegfx::BColorStops aBColorStopsAlpha;
    const bool bHasAlpha(rFillGradientPrimitive2D.hasAlphaGradient());
    if (bHasAlpha)
        aBColorStopsAlpha = rFillGradientPrimitive2D.getAlphaGradient().getColorStops();

    // get and apply border - create soace at start in gradient
    const double fBorder(std::max(std::min(rFillGradient.getBorder(), 1.0), 0.0));
    if (!basegfx::fTools::equalZero(fBorder))
    {
        aBColorStops.createSpaceAtStart(fBorder);
        if (bHasAlpha)
            aBColorStopsAlpha.createSpaceAtStart(fBorder);
    }

    // Apply steps if used to 'emulate' LO's 'discrete step' feature
    if (rFillGradient.getSteps())
    {
        aBColorStops.doApplySteps(rFillGradient.getSteps());
        if (bHasAlpha)
            aBColorStopsAlpha.doApplySteps(rFillGradient.getSteps());
    }

    // add color stops
    for (size_t a(0); a < aBColorStops.size(); a++)
    {
        const basegfx::BColorStop& rStop(aBColorStops[a]);
        const basegfx::BColor aColor(maBColorModifierStack.getModifiedColor(rStop.getStopColor()));

        if (bHasAlpha)
        {
            const basegfx::BColor aAlpha(aBColorStopsAlpha[a].getStopColor());
            cairo_pattern_add_color_stop_rgba(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                              aColor.getGreen(), aColor.getBlue(),
                                              1.0 - aAlpha.luminance());
        }
        else
        {
            if (rFillGradientPrimitive2D.hasTransparency())
            {
                cairo_pattern_add_color_stop_rgba(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                                  aColor.getGreen(), aColor.getBlue(),
                                                  1.0 - rFillGradientPrimitive2D.getTransparency());
            }
            else
            {
                cairo_pattern_add_color_stop_rgb(pPattern, rStop.getStopOffset(), aColor.getRed(),
                                                 aColor.getGreen(), aColor.getBlue());
            }
        }
    }

    cairo_set_source(mpRT, pPattern);

    if (!bRadial) // css::awt::GradientStyle_ELLIPTICAL
    {
        // set cairo matrix at cairo_pattern_t to get needed ratio scale done.
        // this is necessary since cairo_pattern_create_radial does *not*
        // support ellipse resp. radial gradient with non-equidistant
        // ratio directly
        // this uses the transformation 'from user space to pattern space' as
        // cairo docu states. That is the inverse of the intuitive thought
        // model: describe from coordinates in texture, so use B2DHomMatrix
        // and invert at the end to have better control about what has to happen
        basegfx::B2DHomMatrix aTrans;

        // move center to origin to prepare scale/rotate
        aTrans.translate(-aCenter.getX(), -aCenter.getY());

        // get scale factor and apply as needed
        if (fRatioElliptical > 1.0)
            aTrans.scale(1.0, 1.0 / fRatioElliptical);
        else
            aTrans.scale(fRatioElliptical, 1.0);

        // move transformed stuff back to center
        aTrans.translate(aCenter.getX(), aCenter.getY());

        // invert and set at cairo_pattern_t
        aTrans.invert();
        cairo_matrix_init(&aMatrix, aTrans.a(), aTrans.b(), aTrans.c(), aTrans.d(), aTrans.e(),
                          aTrans.f());
        cairo_pattern_set_matrix(pPattern, &aMatrix);
    }

    // draw OutRange. Due to rot and translate being part of the
    // set transform in cairo we need to back-transform (and expand
    // as needed) the OutputRange to paint at the right place and
    // get all OutputRange covered
    basegfx::B2DRange aOutRange(rFillGradientPrimitive2D.getOutputRange());
    aRotAndTranslate.invert();
    aOutRange.transform(aRotAndTranslate);
    cairo_rectangle(mpRT, aOutRange.getMinX(), aOutRange.getMinY(), aOutRange.getWidth(),
                    aOutRange.getHeight());
    cairo_fill(mpRT);

    // cleanup
    cairo_pattern_destroy(pPattern);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D_fallback_decompose(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    if (rFillGradientPrimitive2D.hasAlphaGradient())
    {
        // process recursively to eliminate alpha, cannot be used in decompose fallback
        process(rFillGradientPrimitive2D);
        return;
    }

    // this helper draws the given gradient using the decompose fallback,
    // maybe needed in some cases an can/will be handy
    cairo_save(mpRT);

    // draw all-covering initial BG polygon 1st using getOuterColor and getOutputRange
    processFillGradientPrimitive2D_drawOutputRange(rFillGradientPrimitive2D);

    // bet basic form in unit coordinates
    CairoPathHelper aForm(rFillGradientPrimitive2D.getUnitPolygon());

    // paint solid fill steps by providing callback as lambda
    auto aCallback([this, &aForm](const basegfx::B2DHomMatrix& rMatrix,
                                  const basegfx::BColor& rColor) {
        const basegfx::B2DHomMatrix aTrans(getViewInformation2D().getObjectToViewTransformation()
                                           * rMatrix);
        cairo_matrix_t aMatrix;
        cairo_matrix_init(&aMatrix, aTrans.a(), aTrans.b(), aTrans.c(), aTrans.d(), aTrans.e(),
                          aTrans.f());
        cairo_set_matrix(mpRT, &aMatrix);

        const basegfx::BColor aColor(maBColorModifierStack.getModifiedColor(rColor));
        cairo_set_source_rgb(mpRT, aColor.getRed(), aColor.getGreen(), aColor.getBlue());

        cairo_append_path(mpRT, aForm.getCairoPath());

        cairo_fill(mpRT);
    });

    // call value generator to trigger callbacks
    rFillGradientPrimitive2D.generateMatricesAndColors(aCallback);

    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::processFillGradientPrimitive2D(
    const primitive2d::FillGradientPrimitive2D& rFillGradientPrimitive2D)
{
    if (rFillGradientPrimitive2D.getDefinitionRange().isEmpty())
    {
        // no definition area, done
        return;
    }

    if (rFillGradientPrimitive2D.getOutputRange().isEmpty())
    {
        // no output area, done
        return;
    }

    const attribute::FillGradientAttribute& rFillGradient(
        rFillGradientPrimitive2D.getFillGradient());

    if (rFillGradient.isDefault())
    {
        // no gradient definition, done
        return;
    }

    // check if completely 'bordered out'
    if (processFillGradientPrimitive2D_isCompletelyBordered(rFillGradientPrimitive2D))
    {
        // yes, done, was processed as single filled rectangle (using getOuterColor())
        return;
    }

    // evtl. prefer fallback: cairo does *not* render hard color transitions
    // in gradients anti-aliased which is most visible in 'step'ed gradients,
    // but may also happen in normal ones -> may need to be checked in
    // basegfx::BColorStops (as tooling, like isSymmetrical() or similar).
    // due to the nature of 'step'ing this also means a low number of
    // filled polygons to be drawn (no 'smooth' parts to be replicated),
    // so this is no runtime burner by definition.
    // Making this configurable using static bool, may be moved to settings
    // somewhere later. Do not forget to deactivate when working on 'step'ping
    // stuff in the other helpers (!)
    static bool bPreferAntiAliasedHardColorTransitions(true);

    if (bPreferAntiAliasedHardColorTransitions && rFillGradient.getSteps())
    {
        processFillGradientPrimitive2D_fallback_decompose(rFillGradientPrimitive2D);
        return;
    }

    switch (rFillGradient.getStyle())
    {
        case css::awt::GradientStyle_LINEAR:
        case css::awt::GradientStyle_AXIAL:
        {
            // use specialized renderer for this cases - linear, axial
            processFillGradientPrimitive2D_linear_axial(rFillGradientPrimitive2D);
            return;
        }
        case css::awt::GradientStyle_RADIAL:
        case css::awt::GradientStyle_ELLIPTICAL:
        {
            // use specialized renderer for this cases - radial, elliptical

            //  NOTE for css::awt::GradientStyle_ELLIPTICAL:
            // The first time ever I will accept slight deviations for the
            // elliptical case here due to it's old chaotic move-two-pixels inside
            // rendering method that cannot be patched into a lineartransformation
            // and is hard/difficult to support in more modern systems. Differences
            // are small and mostly would be visible *if* in steps-mode what is
            // also rare. IF that should make problems reactivation of that case
            // for the default case below is possible. main reason is that speed
            // for direct rendering in cairo is much better.
            processFillGradientPrimitive2D_radial_elliptical(rFillGradientPrimitive2D);
            return;
        }
        case css::awt::GradientStyle_SQUARE:
        case css::awt::GradientStyle_RECT:
        {
            // use specialized renderer for this cases - square, rect
            // NOTE: *NO* support for FillGradientAlpha here. it is anyways
            // hard to map these to direct rendering, but to do so the four
            // trapezoids/sides are 'stitched' together, so painting RGBA
            // directly will make the overlaps look bad and like errors.
            // Anyways, these gradient types are only our internal heritage
            // and rendering them directly is already much faster, will be okay.
            processFillGradientPrimitive2D_square_rect(rFillGradientPrimitive2D);
            return;
        }
        default:
        {
            // NOTE: All cases are covered above, but keep this as fallback,
            // so it is possible anytime to exclude one of the cases above again
            // and go back to decomposed version - just in case...
            processFillGradientPrimitive2D_fallback_decompose(rFillGradientPrimitive2D);
            break;
        }
    }
}

void CairoPixelProcessor2D::processPolyPolygonRGBAPrimitive2D(
    const primitive2d::PolyPolygonRGBAPrimitive2D& rPolyPolygonRGBAPrimitive2D)
{
    if (!rPolyPolygonRGBAPrimitive2D.hasTransparency())
    {
        // do what CairoPixelProcessor2D::processPolyPolygonColorPrimitive2D does
        paintPolyPoylgonRGBA(rPolyPolygonRGBAPrimitive2D.getB2DPolyPolygon(),
                             rPolyPolygonRGBAPrimitive2D.getBColor());
        return;
    }

    // draw with alpha directly
    paintPolyPoylgonRGBA(rPolyPolygonRGBAPrimitive2D.getB2DPolyPolygon(),
                         rPolyPolygonRGBAPrimitive2D.getBColor(),
                         rPolyPolygonRGBAPrimitive2D.getTransparency());
}

void CairoPixelProcessor2D::processPolyPolygonAlphaGradientPrimitive2D(
    const primitive2d::PolyPolygonAlphaGradientPrimitive2D& rPolyPolygonAlphaGradientPrimitive2D)
{
    const basegfx::B2DPolyPolygon& rPolyPolygon(
        rPolyPolygonAlphaGradientPrimitive2D.getB2DPolyPolygon());
    if (0 == rPolyPolygon.count())
    {
        // no geometry, done
        return;
    }

    const basegfx::BColor& rColor(rPolyPolygonAlphaGradientPrimitive2D.getBColor());
    const attribute::FillGradientAttribute& rAlphaGradient(
        rPolyPolygonAlphaGradientPrimitive2D.getAlphaGradient());
    if (rAlphaGradient.isDefault())
    {
        // default is a single ColorStop at 0.0 with black (0, 0, 0). The
        // luminance is then 0.0, too -> not transparent at all
        paintPolyPoylgonRGBA(rPolyPolygon, rColor);
        return;
    }

    basegfx::BColor aSingleColor;
    const basegfx::BColorStops& rAlphaStops(rAlphaGradient.getColorStops());
    if (rAlphaStops.isSingleColor(aSingleColor))
    {
        // draw with alpha directly
        paintPolyPoylgonRGBA(rPolyPolygon, rColor, aSingleColor.luminance());
        return;
    }

    const css::awt::GradientStyle aStyle(rAlphaGradient.getStyle());
    if (css::awt::GradientStyle_SQUARE == aStyle || css::awt::GradientStyle_RECT == aStyle)
    {
        // direct paint cannot be used for these styles since they get 'stitched'
        // by multiple parts, so *need* single alpha for multiple pieces, go
        // with decompose/recursion
        process(rPolyPolygonAlphaGradientPrimitive2D);
        return;
    }

    // render as FillGradientPrimitive2D. The idea is to create BColorStops
    // with the same number of entries, but all the same color, using the
    // polygon's target fill color, so we can directly paint gradients as
    // RGBA in Cairo
    basegfx::BColorStops aColorStops;

    // create ColorStops at same stops but single color
    aColorStops.reserve(rAlphaStops.size());
    for (const auto& entry : rAlphaStops)
        aColorStops.emplace_back(entry.getStopOffset(), rColor);

    // create FillGradient using that single-color ColorStops
    const attribute::FillGradientAttribute aFillGradient(
        rAlphaGradient.getStyle(), rAlphaGradient.getBorder(), rAlphaGradient.getOffsetX(),
        rAlphaGradient.getOffsetY(), rAlphaGradient.getAngle(), aColorStops,
        rAlphaGradient.getSteps());

    // create temporary FillGradientPrimitive2D, but do not forget
    // to embed to MaskPrimitive2D to get the PolyPolygon form
    const basegfx::B2DRange aRange(basegfx::utils::getRange(rPolyPolygon));
    const primitive2d::Primitive2DContainer aContainerMaskedFillGradient{
        rtl::Reference<primitive2d::MaskPrimitive2D>(new primitive2d::MaskPrimitive2D(
            rPolyPolygon,
            primitive2d::Primitive2DContainer{ rtl::Reference<primitive2d::FillGradientPrimitive2D>(
                new primitive2d::FillGradientPrimitive2D(aRange, // OutputRange
                                                         aRange, // DefinitionRange
                                                         aFillGradient, &rAlphaGradient)) }))
    };

    // render this. Use container to not trigger decompose for temporary content
    process(aContainerMaskedFillGradient);
}

void CairoPixelProcessor2D::processBitmapAlphaPrimitive2D(
    const primitive2d::BitmapAlphaPrimitive2D& rBitmapAlphaPrimitive2D)
{
    if (!rBitmapAlphaPrimitive2D.hasTransparency())
    {
        // do what CairoPixelProcessor2D::processPolyPolygonColorPrimitive2D does
        paintBitmapAlpha(rBitmapAlphaPrimitive2D.getBitmap(),
                         rBitmapAlphaPrimitive2D.getTransform());
        return;
    }

    // draw with alpha directly
    paintBitmapAlpha(rBitmapAlphaPrimitive2D.getBitmap(), rBitmapAlphaPrimitive2D.getTransform(),
                     rBitmapAlphaPrimitive2D.getTransparency());
}

void CairoPixelProcessor2D::processTextSimplePortionPrimitive2D(
    const primitive2d::TextSimplePortionPrimitive2D& rCandidate)
{
    if (SAL_LIKELY(mbRenderSimpleTextDirect))
    {
        renderTextSimpleOrDecoratedPortionPrimitive2D(rCandidate, nullptr);
    }
    else
    {
        process(rCandidate);
    }
}

void CairoPixelProcessor2D::processTextDecoratedPortionPrimitive2D(
    const primitive2d::TextDecoratedPortionPrimitive2D& rCandidate)
{
    if (SAL_LIKELY(mbRenderDecoratedTextDirect))
    {
        if (!rCandidate.getOrCreateBrokenUpText().empty())
        {
            // if BrokenUpText/WordLineMode is used, go into recursion
            // with single snippets
            process(rCandidate.getOrCreateBrokenUpText());
            return;
        }

        renderTextSimpleOrDecoratedPortionPrimitive2D(rCandidate, &rCandidate);
    }
    else
    {
        process(rCandidate);
    }
}

void CairoPixelProcessor2D::renderTextBackground(
    const primitive2d::TextSimplePortionPrimitive2D& rTextCandidate, double fAscent,
    double fDescent, const basegfx::B2DHomMatrix& rTransform, double fTextWidth)
{
    cairo_save(mpRT);
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, rTransform.a(), rTransform.b(), rTransform.c(), rTransform.d(),
                      rTransform.e(), rTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);
    const basegfx::BColor aFillColor(
        maBColorModifierStack.getModifiedColor(rTextCandidate.getTextFillColor().getBColor()));
    cairo_set_source_rgb(mpRT, aFillColor.getRed(), aFillColor.getGreen(), aFillColor.getBlue());
    cairo_rectangle(mpRT, 0.0, -fAscent, fTextWidth, fAscent + fDescent);
    cairo_fill(mpRT);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::renderSalLayout(const std::unique_ptr<SalLayout>& rSalLayout,
                                            const basegfx::BColor& rTextColor,
                                            const basegfx::B2DHomMatrix& rTransform,
                                            bool bAntiAliase) const
{
    cairo_save(mpRT);
    cairo_matrix_t aMatrix;
    cairo_matrix_init(&aMatrix, rTransform.a(), rTransform.b(), rTransform.c(), rTransform.d(),
                      rTransform.e(), rTransform.f());
    cairo_set_matrix(mpRT, &aMatrix);
    rSalLayout->drawSalLayout(mpRT, rTextColor, bAntiAliase);
    cairo_restore(mpRT);
}

void CairoPixelProcessor2D::renderTextDecorationWithOptionalTransformAndColor(
    const primitive2d::TextDecoratedPortionPrimitive2D& rDecoratedCandidate,
    const basegfx::utils::B2DHomMatrixBufferedOnDemandDecompose& rDecTrans,
    const basegfx::B2DHomMatrix* pOptionalObjectTransform, const basegfx::BColor* pReplacementColor)
{
    // get decorations from Primitive (using original TextTransform),
    // guaranteed the same visualization as a decomposition would create
    const primitive2d::Primitive2DContainer& rDecorationGeometryContent(
        rDecoratedCandidate.getOrCreateDecorationGeometryContent(
            rDecTrans, rDecoratedCandidate.getText(), rDecoratedCandidate.getTextPosition(),
            rDecoratedCandidate.getTextLength(), rDecoratedCandidate.getDXArray()));

    if (rDecorationGeometryContent.empty())
    {
        // no decoration, done
        return;
    }

    // modify ColorStack as needed - if needed
    if (nullptr != pReplacementColor)
        maBColorModifierStack.push(
            std::make_shared<basegfx::BColorModifier_replace>(*pReplacementColor));

    // modify transformation as needed - if needed
    const geometry::ViewInformation2D aLastViewInformation2D(getViewInformation2D());
    if (nullptr != pOptionalObjectTransform)
    {
        geometry::ViewInformation2D aViewInformation2D(getViewInformation2D());
        aViewInformation2D.setObjectTransformation(*pOptionalObjectTransform);
        updateViewInformation(aViewInformation2D);
    }

    // render primitives
    process(rDecorationGeometryContent);

    // restore mods
    if (nullptr != pOptionalObjectTransform)
        updateViewInformation(aLastViewInformation2D);
    if (nullptr != pReplacementColor)
        maBColorModifierStack.pop();
}

void CairoPixelProcessor2D::renderTextSimpleOrDecoratedPortionPrimitive2D(
    const primitive2d::TextSimplePortionPrimitive2D& rTextCandidate,
    const primitive2d::TextDecoratedPortionPrimitive2D* pDecoratedCandidate)
{
    primitive2d::TextLayouterDevice aTextLayouter;
    rTextCandidate.createTextLayouter(aTextLayouter);
    std::unique_ptr<SalLayout> pSalLayout(rTextCandidate.createSalLayout(aTextLayouter));

    if (!pSalLayout)
    {
        // got no layout, error. use decompose as fallback
        process(rTextCandidate);
        return;
    }

    // prepare local transformations
    basegfx::utils::B2DHomMatrixBufferedOnDemandDecompose aDecTrans(
        rTextCandidate.getTextTransform());
    const basegfx::B2DHomMatrix aObjTransformWithoutScale(
        basegfx::utils::createShearXRotateTranslateB2DHomMatrix(
            aDecTrans.getShearX(), aDecTrans.getRotate(), aDecTrans.getTranslate()));
    const basegfx::B2DHomMatrix aFullTextTransform(
        getViewInformation2D().getObjectToViewTransformation() * aObjTransformWithoutScale);

    if (!rTextCandidate.getTextFillColor().IsTransparent())
    {
        // render TextBackground first -> casts no shadow itself, so do independent of
        // text shadow being activated
        double fAscent(aTextLayouter.getFontAscent());
        double fDescent(aTextLayouter.getFontDescent());

        if (nullptr != pDecoratedCandidate
            && primitive2d::TEXT_FONT_EMPHASIS_MARK_NONE
                   != pDecoratedCandidate->getTextEmphasisMark())
        {
            if (pDecoratedCandidate->getEmphasisMarkAbove())
                fAscent += aTextLayouter.getTextHeight() * (250.0 / 1000.0);
            if (pDecoratedCandidate->getEmphasisMarkBelow())
                fDescent += aTextLayouter.getTextHeight() * (250.0 / 1000.0);
        }

        renderTextBackground(rTextCandidate, fAscent, fDescent, aFullTextTransform,
                             pSalLayout->GetTextWidth());
    }

    if (rTextCandidate.hasShadow())
    {
        // Text shadow is constant, relative to font size, *not* rotated with
        // text (always from top-left!)
        static const double fFactor(1.0 / 24.0);
        const double fTextShadowOffset(aDecTrans.getScale().getY() * fFactor);

        // see ::ImplDrawSpecialText -> no longer simple fixed color
        const basegfx::BColor aBlack(0.0, 0.0, 0.0);
        basegfx::BColor aShadowColor(aBlack);
        if (aBlack == rTextCandidate.getFontColor()
            || rTextCandidate.getFontColor().luminance() < (8.0 / 255.0))
            aShadowColor = COL_LIGHTGRAY.getBColor();
        aShadowColor = maBColorModifierStack.getModifiedColor(aShadowColor);

        // create shadow offset
        const basegfx::B2DHomMatrix aShadowTransform(
            basegfx::utils::createTranslateB2DHomMatrix(fTextShadowOffset, fTextShadowOffset));
        const basegfx::B2DHomMatrix aShadowFullTextTransform(
            // right to left: 1st the ObjTrans, then the shadow offset, last ObjToView. That way
            // the shadow is always from top-left, independent of text rotation. Independent from
            // thinking about if that is wanted (shadow direction *could* rotate with the text)
            // this is what the office currently does -> do *not* change visualization (!)
            getViewInformation2D().getObjectToViewTransformation() * aShadowTransform
            * aObjTransformWithoutScale);

        // render text as shadow
        renderSalLayout(pSalLayout, aShadowColor, aShadowFullTextTransform,
                        getViewInformation2D().getUseAntiAliasing());

        if (rTextCandidate.hasTextDecoration())
        {
            const basegfx::B2DHomMatrix aTransform(getViewInformation2D().getObjectTransformation()
                                                   * aShadowTransform);
            renderTextDecorationWithOptionalTransformAndColor(*pDecoratedCandidate, aDecTrans,
                                                              &aTransform, &aShadowColor);
        }
    }
    // get TextColor early, may have to be modified
    basegfx::BColor aTextColor(rTextCandidate.getFontColor());

    if (rTextCandidate.hasOutline())
    {
        // render as outline
        aTextColor = maBColorModifierStack.getModifiedColor(aTextColor);
        basegfx::B2DHomMatrix aInvViewTransform;

        // discrete offsets defined here to easily allow to change them,
        // e.g. if more 'fat' outline is wanted, it may be increased to 1.5
        constexpr double fZero(0.0);
        constexpr double fPlus(1.0);
        constexpr double fMinus(-1.0);

        constexpr std::array<std::pair<double, double>, 8> offsets{
            std::pair<double, double>{ fMinus, fMinus }, std::pair<double, double>{ fZero, fMinus },
            std::pair<double, double>{ fPlus, fMinus },  std::pair<double, double>{ fMinus, fZero },
            std::pair<double, double>{ fPlus, fZero },   std::pair<double, double>{ fMinus, fPlus },
            std::pair<double, double>{ fZero, fPlus },   std::pair<double, double>{ fPlus, fPlus }
        };

        if (rTextCandidate.hasTextDecoration())
        {
            // to use discrete offset (pixels) we will need the back-transform from
            // discrete view coordinates to 'world' coordinates (logic view coordinates),
            // this is the inverse ViewTransformation.
            // NOTE: Alternatively we could calculate the lengths for fPlus/fMinus in
            // logic view coordinates, but would need to create another B2DHomMatrix and
            // to do it correct would need to handle two vectors holding the directions,
            // else - if ever someone will rotate/shear that transformation - it would
            // break
            aInvViewTransform = getViewInformation2D().getViewTransformation();
            aInvViewTransform.invert();
        }

        for (const auto& offset : offsets)
        {
            const basegfx::B2DHomMatrix aDiscreteOffset(
                basegfx::utils::createTranslateB2DHomMatrix(offset.first, offset.second));
            renderSalLayout(pSalLayout, aTextColor, aDiscreteOffset * aFullTextTransform,
                            getViewInformation2D().getUseAntiAliasing());
            if (rTextCandidate.hasTextDecoration())
            {
                const basegfx::B2DHomMatrix aTransform(
                    aInvViewTransform * aDiscreteOffset
                    * getViewInformation2D().getObjectToViewTransformation());
                renderTextDecorationWithOptionalTransformAndColor(*pDecoratedCandidate, aDecTrans,
                                                                  &aTransform);
            }
        }

        // at (center, center) paint in COL_WHITE
        aTextColor = maBColorModifierStack.getModifiedColor(COL_WHITE.getBColor());
        renderSalLayout(pSalLayout, aTextColor, aFullTextTransform,
                        getViewInformation2D().getUseAntiAliasing());
        if (rTextCandidate.hasTextDecoration())
        {
            renderTextDecorationWithOptionalTransformAndColor(*pDecoratedCandidate, aDecTrans,
                                                              nullptr, &aTextColor);
        }

        // paint is complete, Outline and TextRelief cannot be combined, return
        return;
    }

    if (rTextCandidate.hasTextRelief())
    {
        // manipulate TextColor for final text paint below (see ::ImplDrawSpecialText)
        if (aTextColor == COL_BLACK.getBColor())
            aTextColor = COL_WHITE.getBColor();

        // relief offset defined here to easily allow to change them
        // see ::ImplDrawSpecialText and the comment @ 'nOff += mnDPIX/300'
        const bool bEmboss(primitive2d::TEXT_RELIEF_EMBOSSED
                           == pDecoratedCandidate->getTextRelief());
        constexpr double fReliefOffset(1.1);
        const double fOffset(bEmboss ? fReliefOffset : -fReliefOffset);
        const basegfx::B2DHomMatrix aDiscreteOffset(
            basegfx::utils::createTranslateB2DHomMatrix(fOffset, fOffset));

        // see aReliefColor in ::ImplDrawSpecialText
        basegfx::BColor aReliefColor(COL_LIGHTGRAY.getBColor());
        if (COL_WHITE.getBColor() == aTextColor)
            aReliefColor = COL_BLACK.getBColor();
        aReliefColor = maBColorModifierStack.getModifiedColor(aReliefColor);

        // render relief text with offset
        renderSalLayout(pSalLayout, aReliefColor, aDiscreteOffset * aFullTextTransform,
                        getViewInformation2D().getUseAntiAliasing());

        if (rTextCandidate.hasTextDecoration())
        {
            basegfx::B2DHomMatrix aInvViewTransform(getViewInformation2D().getViewTransformation());
            aInvViewTransform.invert();
            const basegfx::B2DHomMatrix aTransform(
                aInvViewTransform * aDiscreteOffset
                * getViewInformation2D().getObjectToViewTransformation());
            renderTextDecorationWithOptionalTransformAndColor(*pDecoratedCandidate, aDecTrans,
                                                              &aTransform, &aReliefColor);
        }
    }

    // render text
    aTextColor = maBColorModifierStack.getModifiedColor(aTextColor);
    renderSalLayout(pSalLayout, aTextColor, aFullTextTransform,
                    getViewInformation2D().getUseAntiAliasing());

    if (rTextCandidate.hasTextDecoration())
    {
        // render using same geometry/primitives that a decompose would
        // create -> safe to get the same visualization for both
        renderTextDecorationWithOptionalTransformAndColor(*pDecoratedCandidate, aDecTrans);
    }
}

void CairoPixelProcessor2D::processBasePrimitive2D(const primitive2d::BasePrimitive2D& rCandidate)
{
    switch (rCandidate.getPrimitive2DID())
    {
        // geometry that *has* to be processed
        case PRIMITIVE2D_ID_BITMAPPRIMITIVE2D:
        {
            processBitmapPrimitive2D(
                static_cast<const primitive2d::BitmapPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POINTARRAYPRIMITIVE2D:
        {
            processPointArrayPrimitive2D(
                static_cast<const primitive2d::PointArrayPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POLYGONHAIRLINEPRIMITIVE2D:
        {
            processPolygonHairlinePrimitive2D(
                static_cast<const primitive2d::PolygonHairlinePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POLYPOLYGONCOLORPRIMITIVE2D:
        {
            processPolyPolygonColorPrimitive2D(
                static_cast<const primitive2d::PolyPolygonColorPrimitive2D&>(rCandidate));
            break;
        }
        // embedding/groups that *have* to be processed
        case PRIMITIVE2D_ID_TRANSPARENCEPRIMITIVE2D:
        {
            processTransparencePrimitive2D(
                static_cast<const primitive2d::TransparencePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_INVERTPRIMITIVE2D:
        {
            processInvertPrimitive2D(
                static_cast<const primitive2d::InvertPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_MASKPRIMITIVE2D:
        {
            processMaskPrimitive2D(static_cast<const primitive2d::MaskPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_MODIFIEDCOLORPRIMITIVE2D:
        {
            processModifiedColorPrimitive2D(
                static_cast<const primitive2d::ModifiedColorPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_TRANSFORMPRIMITIVE2D:
        {
            processTransformPrimitive2D(
                static_cast<const primitive2d::TransformPrimitive2D&>(rCandidate));
            break;
        }

        // geometry that *may* be processed due to being able to do it better
        // then using the decomposition
        case PRIMITIVE2D_ID_UNIFIEDTRANSPARENCEPRIMITIVE2D:
        {
            processUnifiedTransparencePrimitive2D(
                static_cast<const primitive2d::UnifiedTransparencePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_MARKERARRAYPRIMITIVE2D:
        {
            processMarkerArrayPrimitive2D(
                static_cast<const primitive2d::MarkerArrayPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_BACKGROUNDCOLORPRIMITIVE2D:
        {
            processBackgroundColorPrimitive2D(
                static_cast<const primitive2d::BackgroundColorPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POLYGONSTROKEPRIMITIVE2D:
        {
            processPolygonStrokePrimitive2D(
                static_cast<const primitive2d::PolygonStrokePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_LINERECTANGLEPRIMITIVE2D:
        {
            processLineRectanglePrimitive2D(
                static_cast<const primitive2d::LineRectanglePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_FILLEDRECTANGLEPRIMITIVE2D:
        {
            processFilledRectanglePrimitive2D(
                static_cast<const primitive2d::FilledRectanglePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_SINGLELINEPRIMITIVE2D:
        {
            processSingleLinePrimitive2D(
                static_cast<const primitive2d::SingleLinePrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_FILLGRAPHICPRIMITIVE2D:
        {
            processFillGraphicPrimitive2D(
                static_cast<const primitive2d::FillGraphicPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_FILLGRADIENTPRIMITIVE2D:
        {
            processFillGradientPrimitive2D(
                static_cast<const primitive2d::FillGradientPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POLYPOLYGONRGBAPRIMITIVE2D:
        {
            processPolyPolygonRGBAPrimitive2D(
                static_cast<const primitive2d::PolyPolygonRGBAPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_BITMAPALPHAPRIMITIVE2D:
        {
            processBitmapAlphaPrimitive2D(
                static_cast<const primitive2d::BitmapAlphaPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_POLYPOLYGONALPHAGRADIENTPRIMITIVE2D:
        {
            processPolyPolygonAlphaGradientPrimitive2D(
                static_cast<const primitive2d::PolyPolygonAlphaGradientPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_TEXTSIMPLEPORTIONPRIMITIVE2D:
        {
            processTextSimplePortionPrimitive2D(
                static_cast<const primitive2d::TextSimplePortionPrimitive2D&>(rCandidate));
            break;
        }
        case PRIMITIVE2D_ID_TEXTDECORATEDPORTIONPRIMITIVE2D:
        {
            processTextDecoratedPortionPrimitive2D(
                static_cast<const primitive2d::TextDecoratedPortionPrimitive2D&>(rCandidate));
            break;
        }

        // continue with decompose
        default:
        {
            SAL_INFO("drawinglayer", "default case for " << drawinglayer::primitive2d::idToString(
                                         rCandidate.getPrimitive2DID()));
            // process recursively
            process(rCandidate);
            break;
        }
    }
}

} // end of namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */