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
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
|
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2004 David Reveman
* Copyright © 2005 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of David
* Reveman not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. David Reveman makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: David Reveman <davidr@novell.com>
* Keith Packard <keithp@keithp.com>
* Carl Worth <cworth@cworth.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-freed-pool-private.h"
#include "cairo-path-private.h"
#include <float.h>
#define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
/**
* SECTION:cairo-pattern
* @Title: cairo_pattern_t
* @Short_Description: Sources for drawing
* @See_Also: #cairo_t, #cairo_surface_t
*
* #cairo_pattern_t is the paint with which cairo draws.
* The primary use of patterns is as the source for all cairo drawing
* operations, although they can also be used as masks, that is, as the
* brush too.
*
* A cairo pattern is created by using one of the many constructors,
* of the form
* <function>cairo_pattern_create_<emphasis>type</emphasis>()</function>
* or implicitly through
* <function>cairo_set_source_<emphasis>type</emphasis>()</function>
* functions.
*/
#if HAS_FREED_POOL
static freed_pool_t freed_pattern_pool[5];
#endif
static const cairo_solid_pattern_t _cairo_pattern_nil = {
{ CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_NO_MEMORY, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT }, /* extend */
};
static const cairo_solid_pattern_t _cairo_pattern_nil_null_pointer = {
{ CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_NULL_POINTER, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT }, /* extend */
};
const cairo_solid_pattern_t _cairo_pattern_black = {
{ CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT}, /* extend */
{ 0., 0., 0., 1., 0, 0, 0, 0xffff },/* color (double rgba, short rgba) */
};
const cairo_solid_pattern_t _cairo_pattern_clear = {
{ CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT}, /* extend */
{ 0., 0., 0., 0., 0, 0, 0, 0 },/* color (double rgba, short rgba) */
};
const cairo_solid_pattern_t _cairo_pattern_white = {
{ CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT}, /* extend */
{ 1., 1., 1., 1., 0xffff, 0xffff, 0xffff, 0xffff },/* color (double rgba, short rgba) */
};
/**
* _cairo_pattern_set_error:
* @pattern: a pattern
* @status: a status value indicating an error
*
* Atomically sets pattern->status to @status and calls _cairo_error;
* Does nothing if status is %CAIRO_STATUS_SUCCESS.
*
* All assignments of an error status to pattern->status should happen
* through _cairo_pattern_set_error(). Note that due to the nature of
* the atomic operation, it is not safe to call this function on the nil
* objects.
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
**/
static cairo_status_t
_cairo_pattern_set_error (cairo_pattern_t *pattern,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return status;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (&pattern->status, status);
return _cairo_error (status);
}
static void
_cairo_pattern_init (cairo_pattern_t *pattern, cairo_pattern_type_t type)
{
#if HAVE_VALGRIND
switch (type) {
case CAIRO_PATTERN_TYPE_SOLID:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_mesh_pattern_t));
break;
}
#endif
pattern->type = type;
pattern->status = CAIRO_STATUS_SUCCESS;
/* Set the reference count to zero for on-stack patterns.
* Callers needs to explicitly increment the count for heap allocations. */
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
if (type == CAIRO_PATTERN_TYPE_SURFACE)
pattern->extend = CAIRO_EXTEND_SURFACE_DEFAULT;
else
pattern->extend = CAIRO_EXTEND_GRADIENT_DEFAULT;
pattern->filter = CAIRO_FILTER_DEFAULT;
pattern->has_component_alpha = FALSE;
cairo_matrix_init_identity (&pattern->matrix);
}
static cairo_status_t
_cairo_gradient_pattern_init_copy (cairo_gradient_pattern_t *pattern,
const cairo_gradient_pattern_t *other)
{
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (other->base.type == CAIRO_PATTERN_TYPE_LINEAR)
{
cairo_linear_pattern_t *dst = (cairo_linear_pattern_t *) pattern;
cairo_linear_pattern_t *src = (cairo_linear_pattern_t *) other;
*dst = *src;
}
else
{
cairo_radial_pattern_t *dst = (cairo_radial_pattern_t *) pattern;
cairo_radial_pattern_t *src = (cairo_radial_pattern_t *) other;
*dst = *src;
}
if (other->stops == other->stops_embedded)
pattern->stops = pattern->stops_embedded;
else if (other->stops)
{
pattern->stops = _cairo_malloc_ab (other->stops_size,
sizeof (cairo_gradient_stop_t));
if (unlikely (pattern->stops == NULL)) {
pattern->stops_size = 0;
pattern->n_stops = 0;
return _cairo_pattern_set_error (&pattern->base, CAIRO_STATUS_NO_MEMORY);
}
memcpy (pattern->stops, other->stops,
other->n_stops * sizeof (cairo_gradient_stop_t));
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_mesh_pattern_init_copy (cairo_mesh_pattern_t *pattern,
const cairo_mesh_pattern_t *other)
{
*pattern = *other;
_cairo_array_init (&pattern->patches, sizeof (cairo_mesh_patch_t));
return _cairo_array_append_multiple (&pattern->patches,
_cairo_array_index_const (&other->patches, 0),
_cairo_array_num_elements (&other->patches));
}
cairo_status_t
_cairo_pattern_init_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
if (other->status)
return _cairo_pattern_set_error (pattern, other->status);
switch (other->type) {
case CAIRO_PATTERN_TYPE_SOLID: {
cairo_solid_pattern_t *dst = (cairo_solid_pattern_t *) pattern;
cairo_solid_pattern_t *src = (cairo_solid_pattern_t *) other;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_solid_pattern_t)));
*dst = *src;
} break;
case CAIRO_PATTERN_TYPE_SURFACE: {
cairo_surface_pattern_t *dst = (cairo_surface_pattern_t *) pattern;
cairo_surface_pattern_t *src = (cairo_surface_pattern_t *) other;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_surface_pattern_t)));
*dst = *src;
cairo_surface_reference (dst->surface);
} break;
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
cairo_gradient_pattern_t *dst = (cairo_gradient_pattern_t *) pattern;
cairo_gradient_pattern_t *src = (cairo_gradient_pattern_t *) other;
cairo_status_t status;
if (other->type == CAIRO_PATTERN_TYPE_LINEAR) {
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_linear_pattern_t)));
} else {
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_radial_pattern_t)));
}
status = _cairo_gradient_pattern_init_copy (dst, src);
if (unlikely (status))
return status;
} break;
case CAIRO_PATTERN_TYPE_MESH: {
cairo_mesh_pattern_t *dst = (cairo_mesh_pattern_t *) pattern;
cairo_mesh_pattern_t *src = (cairo_mesh_pattern_t *) other;
cairo_status_t status;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_mesh_pattern_t)));
status = _cairo_mesh_pattern_init_copy (dst, src);
if (unlikely (status))
return status;
} break;
}
/* The reference count and user_data array are unique to the copy. */
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_init_static_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
int size;
assert (other->status == CAIRO_STATUS_SUCCESS);
switch (other->type) {
default:
ASSERT_NOT_REACHED;
case CAIRO_PATTERN_TYPE_SOLID:
size = sizeof (cairo_solid_pattern_t);
break;
case CAIRO_PATTERN_TYPE_SURFACE:
size = sizeof (cairo_surface_pattern_t);
break;
case CAIRO_PATTERN_TYPE_LINEAR:
size = sizeof (cairo_linear_pattern_t);
break;
case CAIRO_PATTERN_TYPE_RADIAL:
size = sizeof (cairo_radial_pattern_t);
break;
case CAIRO_PATTERN_TYPE_MESH:
size = sizeof (cairo_mesh_pattern_t);
break;
}
memcpy (pattern, other, size);
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
}
cairo_status_t
_cairo_pattern_init_snapshot (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
cairo_status_t status;
/* We don't bother doing any fancy copy-on-write implementation
* for the pattern's data. It's generally quite tiny. */
status = _cairo_pattern_init_copy (pattern, other);
if (unlikely (status))
return status;
/* But we do let the surface snapshot stuff be as fancy as it
* would like to be. */
if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_pattern_t *surface_pattern =
(cairo_surface_pattern_t *) pattern;
cairo_surface_t *surface = surface_pattern->surface;
surface_pattern->surface = _cairo_surface_snapshot (surface);
cairo_surface_destroy (surface);
if (surface_pattern->surface->status)
return surface_pattern->surface->status;
}
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_fini (cairo_pattern_t *pattern)
{
_cairo_user_data_array_fini (&pattern->user_data);
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
break;
case CAIRO_PATTERN_TYPE_SURFACE: {
cairo_surface_pattern_t *surface_pattern =
(cairo_surface_pattern_t *) pattern;
cairo_surface_destroy (surface_pattern->surface);
} break;
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
cairo_gradient_pattern_t *gradient =
(cairo_gradient_pattern_t *) pattern;
if (gradient->stops && gradient->stops != gradient->stops_embedded)
free (gradient->stops);
} break;
case CAIRO_PATTERN_TYPE_MESH: {
cairo_mesh_pattern_t *mesh =
(cairo_mesh_pattern_t *) pattern;
_cairo_array_fini (&mesh->patches);
} break;
}
#if HAVE_VALGRIND
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
VALGRIND_MAKE_MEM_NOACCESS (pattern, sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
VALGRIND_MAKE_MEM_NOACCESS (pattern, sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
VALGRIND_MAKE_MEM_NOACCESS (pattern, sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
VALGRIND_MAKE_MEM_NOACCESS (pattern, sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
VALGRIND_MAKE_MEM_NOACCESS (pattern, sizeof (cairo_mesh_pattern_t));
break;
}
#endif
}
cairo_status_t
_cairo_pattern_create_copy (cairo_pattern_t **pattern_out,
const cairo_pattern_t *other)
{
cairo_pattern_t *pattern;
cairo_status_t status;
if (other->status)
return other->status;
switch (other->type) {
case CAIRO_PATTERN_TYPE_SOLID:
pattern = malloc (sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
pattern = malloc (sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
pattern = malloc (sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
pattern = malloc (sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
pattern = malloc (sizeof (cairo_mesh_pattern_t));
break;
default:
ASSERT_NOT_REACHED;
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
}
if (unlikely (pattern == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
status = _cairo_pattern_init_copy (pattern, other);
if (unlikely (status)) {
free (pattern);
return status;
}
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 1);
*pattern_out = pattern;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_init_solid (cairo_solid_pattern_t *pattern,
const cairo_color_t *color)
{
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SOLID);
pattern->color = *color;
}
void
_cairo_pattern_init_for_surface (cairo_surface_pattern_t *pattern,
cairo_surface_t *surface)
{
if (surface->status) {
/* Force to solid to simplify the pattern_fini process. */
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SOLID);
_cairo_pattern_set_error (&pattern->base, surface->status);
return;
}
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SURFACE);
pattern->surface = cairo_surface_reference (surface);
}
static void
_cairo_pattern_init_gradient (cairo_gradient_pattern_t *pattern,
cairo_pattern_type_t type)
{
_cairo_pattern_init (&pattern->base, type);
pattern->n_stops = 0;
pattern->stops_size = 0;
pattern->stops = NULL;
}
static void
_cairo_pattern_init_linear (cairo_linear_pattern_t *pattern,
double x0, double y0, double x1, double y1)
{
_cairo_pattern_init_gradient (&pattern->base, CAIRO_PATTERN_TYPE_LINEAR);
pattern->pd1.x = x0;
pattern->pd1.y = y0;
pattern->pd2.x = x1;
pattern->pd2.y = y1;
}
static void
_cairo_pattern_init_radial (cairo_radial_pattern_t *pattern,
double cx0, double cy0, double radius0,
double cx1, double cy1, double radius1)
{
_cairo_pattern_init_gradient (&pattern->base, CAIRO_PATTERN_TYPE_RADIAL);
pattern->cd1.center.x = cx0;
pattern->cd1.center.y = cy0;
pattern->cd1.radius = fabs (radius0);
pattern->cd2.center.x = cx1;
pattern->cd2.center.y = cy1;
pattern->cd2.radius = fabs (radius1);
}
cairo_pattern_t *
_cairo_pattern_create_solid (const cairo_color_t *color)
{
cairo_solid_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_SOLID]);
if (unlikely (pattern == NULL)) {
/* None cached, need to create a new pattern. */
pattern = malloc (sizeof (cairo_solid_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil;
}
}
_cairo_pattern_init_solid (pattern, color);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
cairo_pattern_t *
_cairo_pattern_create_in_error (cairo_status_t status)
{
cairo_pattern_t *pattern;
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_pattern_t *)&_cairo_pattern_nil.base;
CAIRO_MUTEX_INITIALIZE ();
pattern = _cairo_pattern_create_solid (CAIRO_COLOR_BLACK);
if (pattern->status == CAIRO_STATUS_SUCCESS)
status = _cairo_pattern_set_error (pattern, status);
return pattern;
}
/**
* cairo_pattern_create_rgb:
* @red: red component of the color
* @green: green component of the color
* @blue: blue component of the color
*
* Creates a new #cairo_pattern_t corresponding to an opaque color. The
* color components are floating point numbers in the range 0 to 1.
* If the values passed in are outside that range, they will be
* clamped.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
**/
cairo_pattern_t *
cairo_pattern_create_rgb (double red, double green, double blue)
{
cairo_color_t color;
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
_cairo_color_init_rgb (&color, red, green, blue);
CAIRO_MUTEX_INITIALIZE ();
return _cairo_pattern_create_solid (&color);
}
slim_hidden_def (cairo_pattern_create_rgb);
/**
* cairo_pattern_create_rgba:
* @red: red component of the color
* @green: green component of the color
* @blue: blue component of the color
* @alpha: alpha component of the color
*
* Creates a new #cairo_pattern_t corresponding to a translucent color.
* The color components are floating point numbers in the range 0 to
* 1. If the values passed in are outside that range, they will be
* clamped.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
**/
cairo_pattern_t *
cairo_pattern_create_rgba (double red, double green, double blue,
double alpha)
{
cairo_color_t color;
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_color_init_rgba (&color, red, green, blue, alpha);
CAIRO_MUTEX_INITIALIZE ();
return _cairo_pattern_create_solid (&color);
}
slim_hidden_def (cairo_pattern_create_rgba);
/**
* cairo_pattern_create_for_surface:
* @surface: the surface
*
* Create a new #cairo_pattern_t for the given surface.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
**/
cairo_pattern_t *
cairo_pattern_create_for_surface (cairo_surface_t *surface)
{
cairo_surface_pattern_t *pattern;
if (surface == NULL) {
_cairo_error_throw (CAIRO_STATUS_NULL_POINTER);
return (cairo_pattern_t*) &_cairo_pattern_nil_null_pointer;
}
if (surface->status)
return _cairo_pattern_create_in_error (surface->status);
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_SURFACE]);
if (unlikely (pattern == NULL)) {
pattern = malloc (sizeof (cairo_surface_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *)&_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_for_surface (pattern, surface);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
slim_hidden_def (cairo_pattern_create_for_surface);
/**
* cairo_pattern_create_linear:
* @x0: x coordinate of the start point
* @y0: y coordinate of the start point
* @x1: x coordinate of the end point
* @y1: y coordinate of the end point
*
* Create a new linear gradient #cairo_pattern_t along the line defined
* by (x0, y0) and (x1, y1). Before using the gradient pattern, a
* number of color stops should be defined using
* cairo_pattern_add_color_stop_rgb() or
* cairo_pattern_add_color_stop_rgba().
*
* Note: The coordinates here are in pattern space. For a new pattern,
* pattern space is identical to user space, but the relationship
* between the spaces can be changed with cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
**/
cairo_pattern_t *
cairo_pattern_create_linear (double x0, double y0, double x1, double y1)
{
cairo_linear_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_LINEAR]);
if (unlikely (pattern == NULL)) {
pattern = malloc (sizeof (cairo_linear_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_linear (pattern, x0, y0, x1, y1);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.base.ref_count, 1);
return &pattern->base.base;
}
/**
* cairo_pattern_create_radial:
* @cx0: x coordinate for the center of the start circle
* @cy0: y coordinate for the center of the start circle
* @radius0: radius of the start circle
* @cx1: x coordinate for the center of the end circle
* @cy1: y coordinate for the center of the end circle
* @radius1: radius of the end circle
*
* Creates a new radial gradient #cairo_pattern_t between the two
* circles defined by (cx0, cy0, radius0) and (cx1, cy1, radius1). Before using the
* gradient pattern, a number of color stops should be defined using
* cairo_pattern_add_color_stop_rgb() or
* cairo_pattern_add_color_stop_rgba().
*
* Note: The coordinates here are in pattern space. For a new pattern,
* pattern space is identical to user space, but the relationship
* between the spaces can be changed with cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
**/
cairo_pattern_t *
cairo_pattern_create_radial (double cx0, double cy0, double radius0,
double cx1, double cy1, double radius1)
{
cairo_radial_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_RADIAL]);
if (unlikely (pattern == NULL)) {
pattern = malloc (sizeof (cairo_radial_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_radial (pattern, cx0, cy0, radius0, cx1, cy1, radius1);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.base.ref_count, 1);
return &pattern->base.base;
}
/* This order is specified in the diagram in the documentation for
* cairo_pattern_create_mesh() */
static const int mesh_path_point_i[12] = { 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 2, 1 };
static const int mesh_path_point_j[12] = { 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0 };
static const int mesh_control_point_i[4] = { 1, 1, 2, 2 };
static const int mesh_control_point_j[4] = { 1, 2, 2, 1 };
/**
* cairo_pattern_create_mesh:
*
* Create a new mesh pattern.
*
* Mesh patterns are tensor-product patch meshes (type 7 shadings in
* PDF). Mesh patterns may also be used to create other types of
* shadings that are special cases of tensor-product patch meshes such
* as Coons patch meshes (type 6 shading in PDF) and Gouraud-shaded
* triangle meshes (type 4 and 5 shadings in PDF).
*
* Mesh patterns consist of one or more tensor-product patches, which
* should be defined before using the mesh pattern. Using a mesh
* pattern with a partially defined patch as source or mask will put
* the context in an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* A tensor-product patch is defined by 4 Bézier curves (side 0, 1, 2,
* 3) and by 4 additional control points (P0, P1, P2, P3) that provide
* further control over the patch and complete the definition of the
* tensor-product patch. The corner C0 is the first point of the
* patch.
*
* Degenerate sides are permitted so straight lines may be used. A
* zero length line on one side may be used to create 3 sided patches.
*
* <informalexample><programlisting>
* C1 Side 1 C2
* +---------------+
* | |
* | P1 P2 |
* | |
* Side 0 | | Side 2
* | |
* | |
* | P0 P3 |
* | |
* +---------------+
* C0 Side 3 C3
* </programlisting></informalexample>
*
* Each patch is constructed by first calling
* cairo_pattern_mesh_begin_patch(), then cairo_pattern_mesh_move_to()
* to specify the first point in the patch (C0). Then the sides are
* specified with calls to cairo_pattern_mesh_curve_to() and
* cairo_pattern_mesh_line_to().
*
* The four additional control points (P0, P1, P2, P3) in a patch can
* be specified with cairo_pattern_mesh_set_control_point().
*
* At each corner of the patch (C0, C1, C2, C3) a color may be
* specified with cairo_pattern_mesh_set_corner_color_rgb() or
* cairo_pattern_mesh_set_corner_color_rgba(). Any corner whose color
* is not explicitly specified defaults to transparent black.
*
* A Coons patch is a special case of the tensor-product patch where
* the control points are implicitly defined by the sides of the
* patch. The default value for any control point not specified is the
* implicit value for a Coons patch, i.e. if no control points are
* specified the patch is a Coons patch.
*
* A triangle is a special case of the tensor-product patch where the
* control points are implicitly defined by the sides of the patch,
* all the sides are lines and one of them has length 0, i.e. if the
* patch is specified using just 3 lines, it is a triangle. If the
* corners connected by the 0-length side have the same color, the
* patch is a Gouraud-shaded triangle.
*
* Patches may be oriented differently to the above diagram. For
* example the first point could be at the top left. The diagram only
* shows the relationship between the sides, corners and control
* points. Regardless of where the first point is located, when
* specifying colors, corner 0 will always be the first point, corner
* 1 the point between side 0 and side 1 etc.
*
* Calling cairo_pattern_mesh_end_patch() completes the current
* patch. If less than 4 sides have been defined, the first missing
* side is defined as a line from the current point to the first point
* of the patch (C0) and the other sides are degenerate lines from C0
* to C0. The corners between the added sides will all be coincident
* with C0 of the patch and their color will be set to be the same as
* the color of C0.
*
* Additional patches may be added with additional calls to
* cairo_pattern_mesh_begin_patch()/cairo_pattern_mesh_end_patch().
*
* <informalexample><programlisting>
* cairo_pattern_t *mesh = cairo_pattern_mesh_create_mesh ();
*
* /* Add a Coons patch */
* cairo_pattern_mesh_begin_patch (mesh);
* cairo_pattern_mesh_move_to (pattern, 0, 0);
* cairo_pattern_mesh_curve_to (pattern, 30, -30, 60, 30, 100, 0);
* cairo_pattern_mesh_curve_to (pattern, 60, 30, 130, 60, 100, 100);
* cairo_pattern_mesh_curve_to (pattern, 60, 70, 30, 130, 0, 100);
* cairo_pattern_mesh_curve_to (pattern, 30, 70, -30, 30, 0, 0);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 0, 1, 0, 0);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 1, 0, 1, 0);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 2, 0, 0, 1);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 3, 1, 1, 0);
* cairo_pattern_mesh_end_patch (mesh);
*
* /* Add a Gouraud-shaded triangle */
* cairo_pattern_mesh_begin_patch (mesh)
* cairo_pattern_mesh_move_to (pattern, 100, 100);
* cairo_pattern_mesh_line_to (pattern, 130, 130);
* cairo_pattern_mesh_line_to (pattern, 130, 70);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 0, 1, 0, 0);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 1, 0, 1, 0);
* cairo_pattern_mesh_set_corner_color_rgb (pattern, 2, 0, 0, 1);
* cairo_pattern_mesh_end_patch (mesh)
* </programlisting></informalexample>
*
* When two patches overlap, the last one that has been added is drawn
* over the first one.
*
* When a patch folds over itself, points are sorted depending on
* their parameter coordinates inside the patch. The v coordinate
* ranges from 0 to 1 when moving from side 3 to side 1; the u
* coordinate ranges from 0 to 1 when going from side 0 to side
* 2. Points with higher v coordinate hide points with lower v
* coordinate. When two points have the same v coordinate, the one
* with higher u coordinate is above. This means that points nearer to
* side 1 are above points nearer to side 3; when this is not
* sufficient to decide which point is above (for example when both
* points belong to side 1 or side 3) points nearer to side 2 are
* above points nearer to side 0.
*
* For a complete definition of tensor-product patches, see the PDF
* specification (ISO32000), which describes the parametrization in
* detail.
*
* Note: The coordinates are always in pattern space. For a new
* pattern, pattern space is identical to user space, but the
* relationship between the spaces can be changed with
* cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the returned
* object and should call cairo_pattern_destroy() when finished with
* it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect the
* status of a pattern use cairo_pattern_status().
*
* Since: 1.12
**/
cairo_pattern_t *
cairo_pattern_create_mesh (void)
{
cairo_mesh_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_MESH]);
if (unlikely (pattern == NULL)) {
pattern = malloc (sizeof (cairo_mesh_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_MESH);
_cairo_array_init (&pattern->patches, sizeof (cairo_mesh_patch_t));
pattern->current_patch = NULL;
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
/**
* cairo_pattern_reference:
* @pattern: a #cairo_pattern_t
*
* Increases the reference count on @pattern by one. This prevents
* @pattern from being destroyed until a matching call to
* cairo_pattern_destroy() is made.
*
* The number of references to a #cairo_pattern_t can be get using
* cairo_pattern_get_reference_count().
*
* Return value: the referenced #cairo_pattern_t.
**/
cairo_pattern_t *
cairo_pattern_reference (cairo_pattern_t *pattern)
{
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return pattern;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
_cairo_reference_count_inc (&pattern->ref_count);
return pattern;
}
slim_hidden_def (cairo_pattern_reference);
/**
* cairo_pattern_get_type:
* @pattern: a #cairo_pattern_t
*
* This function returns the type a pattern.
* See #cairo_pattern_type_t for available types.
*
* Return value: The type of @pattern.
*
* Since: 1.2
**/
cairo_pattern_type_t
cairo_pattern_get_type (cairo_pattern_t *pattern)
{
return pattern->type;
}
/**
* cairo_pattern_status:
* @pattern: a #cairo_pattern_t
*
* Checks whether an error has previously occurred for this
* pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_STATUS_NO_MEMORY, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH.
**/
cairo_status_t
cairo_pattern_status (cairo_pattern_t *pattern)
{
return pattern->status;
}
/**
* cairo_pattern_destroy:
* @pattern: a #cairo_pattern_t
*
* Decreases the reference count on @pattern by one. If the result is
* zero, then @pattern and all associated resources are freed. See
* cairo_pattern_reference().
**/
void
cairo_pattern_destroy (cairo_pattern_t *pattern)
{
cairo_pattern_type_t type;
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
if (! _cairo_reference_count_dec_and_test (&pattern->ref_count))
return;
type = pattern->type;
_cairo_pattern_fini (pattern);
/* maintain a small cache of freed patterns */
_freed_pool_put (&freed_pattern_pool[type], pattern);
}
slim_hidden_def (cairo_pattern_destroy);
/**
* cairo_pattern_get_reference_count:
* @pattern: a #cairo_pattern_t
*
* Returns the current reference count of @pattern.
*
* Return value: the current reference count of @pattern. If the
* object is a nil object, 0 will be returned.
*
* Since: 1.4
**/
unsigned int
cairo_pattern_get_reference_count (cairo_pattern_t *pattern)
{
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return 0;
return CAIRO_REFERENCE_COUNT_GET_VALUE (&pattern->ref_count);
}
/**
* cairo_pattern_get_user_data:
* @pattern: a #cairo_pattern_t
* @key: the address of the #cairo_user_data_key_t the user data was
* attached to
*
* Return user data previously attached to @pattern using the
* specified key. If no user data has been attached with the given
* key this function returns %NULL.
*
* Return value: the user data previously attached or %NULL.
*
* Since: 1.4
**/
void *
cairo_pattern_get_user_data (cairo_pattern_t *pattern,
const cairo_user_data_key_t *key)
{
return _cairo_user_data_array_get_data (&pattern->user_data,
key);
}
/**
* cairo_pattern_set_user_data:
* @pattern: a #cairo_pattern_t
* @key: the address of a #cairo_user_data_key_t to attach the user data to
* @user_data: the user data to attach to the #cairo_pattern_t
* @destroy: a #cairo_destroy_func_t which will be called when the
* #cairo_t is destroyed or when new user data is attached using the
* same key.
*
* Attach user data to @pattern. To remove user data from a surface,
* call this function with the key that was used to set it and %NULL
* for @data.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
* slot could not be allocated for the user data.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_set_user_data (cairo_pattern_t *pattern,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return pattern->status;
return _cairo_user_data_array_set_data (&pattern->user_data,
key, user_data, destroy);
}
/**
* cairo_pattern_mesh_begin_patch:
* @pattern: a #cairo_pattern_t
*
* Begin a patch in a mesh pattern.
*
* After calling this function, the patch shape should be defined with
* cairo_pattern_mesh_move_to(), cairo_pattern_mesh_line_to() and
* cairo_pattern_mesh_curve_to().
*
* After defining the patch, cairo_pattern_mesh_end_patch() must be
* called before using @pattern as a source or mask.
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern already has a
* current patch, it will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_begin_patch (cairo_pattern_t *pattern)
{
cairo_mesh_pattern_t *mesh;
cairo_status_t status;
cairo_mesh_patch_t *current_patch;
int i;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
status = _cairo_array_allocate (&mesh->patches, 1, (void **) ¤t_patch);
if (unlikely (status)) {
_cairo_pattern_set_error (pattern, status);
return;
}
mesh->current_patch = current_patch;
mesh->current_side = -2; /* no current point */
for (i = 0; i < 4; i++)
mesh->has_control_point[i] = FALSE;
for (i = 0; i < 4; i++)
mesh->has_color[i] = FALSE;
}
static void
_calc_control_point (cairo_mesh_patch_t *patch, int control_point)
{
/* The Coons patch is a special case of the Tensor Product patch
* where the four control points are:
*
* P11 = S(1/3, 1/3)
* P12 = S(1/3, 2/3)
* P21 = S(2/3, 1/3)
* P22 = S(2/3, 2/3)
*
* where S is the gradient surface.
*
* When one or more control points has not been specified
* calculated the Coons patch control points are substituted. If
* no control points are specified the gradient will be a Coons
* patch.
*
* The equations below are defined in the ISO32000 standard.
*/
cairo_point_double_t *p[3][3];
int cp_i, cp_j, i, j;
cp_i = mesh_control_point_i[control_point];
cp_j = mesh_control_point_j[control_point];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
p[i][j] = &patch->points[cp_i ^ i][cp_j ^ j];
p[0][0]->x = (- 4 * p[1][1]->x
+ 6 * (p[1][0]->x + p[0][1]->x)
- 2 * (p[1][2]->x + p[2][1]->x)
+ 3 * (p[2][0]->x + p[0][2]->x)
- 1 * p[2][2]->x) * (1. / 9);
p[0][0]->y = (- 4 * p[1][1]->y
+ 6 * (p[1][0]->y + p[0][1]->y)
- 2 * (p[1][2]->y + p[2][1]->y)
+ 3 * (p[2][0]->y + p[0][2]->y)
- 1 * p[2][2]->y) * (1. / 9);
}
/**
* cairo_pattern_mesh_end_patch:
* @pattern: a #cairo_pattern_t
*
* Indicates the end of the current patch in a mesh pattern.
*
* If the current patch has less than 4 sides, it is closed with a
* straight line from the current point to the first point of the
* patch as if cairo_pattern_mesh_line_to() was used.
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch has no current point, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_end_patch (cairo_pattern_t *pattern)
{
cairo_mesh_pattern_t *mesh;
cairo_mesh_patch_t *current_patch;
int i;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
current_patch = mesh->current_patch;
if (unlikely (!current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == -2)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
while (mesh->current_side < 3) {
int corner_num;
cairo_pattern_mesh_line_to (pattern,
current_patch->points[0][0].x,
current_patch->points[0][0].y);
corner_num = mesh->current_side + 1;
if (corner_num < 4 && ! mesh->has_color[corner_num]) {
current_patch->colors[corner_num] = current_patch->colors[0];
mesh->has_color[corner_num] = TRUE;
}
}
for (i = 0; i < 4; i++) {
if (! mesh->has_control_point[i])
_calc_control_point (current_patch, i);
}
for (i = 0; i < 4; i++) {
if (! mesh->has_color[i])
current_patch->colors[i] = *CAIRO_COLOR_TRANSPARENT;
}
mesh->current_patch = NULL;
}
/**
* cairo_pattern_mesh_curve_to:
* @pattern: a #cairo_pattern_t
* @x1: the X coordinate of the first control point
* @y1: the Y coordinate of the first control point
* @x2: the X coordinate of the second control point
* @y2: the Y coordinate of the second control point
* @x3: the X coordinate of the end of the curve
* @y3: the Y coordinate of the end of the curve
*
* Adds a cubic Bézier spline to the current patch from the current
* point to position (@x3, @y3) in pattern-space coordinates, using
* (@x1, @y1) and (@x2, @y2) as the control points.
*
* If the current patch has no current point before the call to
* cairo_pattern_mesh_curve_to(), this function will behave as if
* preceded by a call to cairo_pattern_mesh_move_to(@pattern, @x1,
* @y1).
*
* After this call the current point will be (@x3, @y3).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has 4 sides, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_curve_to (cairo_pattern_t *pattern,
double x1, double y1,
double x2, double y2,
double x3, double y3)
{
cairo_mesh_pattern_t *mesh;
int current_point, i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (mesh->current_side == -2)
cairo_pattern_mesh_move_to (pattern, x1, y1);
assert (mesh->current_side >= -1);
assert (pattern->status == CAIRO_STATUS_SUCCESS);
mesh->current_side++;
current_point = 3 * mesh->current_side;
current_point++;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x1;
mesh->current_patch->points[i][j].y = y1;
current_point++;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x2;
mesh->current_patch->points[i][j].y = y2;
current_point++;
if (current_point < 12) {
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x3;
mesh->current_patch->points[i][j].y = y3;
}
}
slim_hidden_def (cairo_pattern_mesh_curve_to);
/**
* cairo_pattern_mesh_line_to:
* @pattern: a #cairo_pattern_t
* @x: the X coordinate of the end of the new line
* @y: the Y coordinate of the end of the new line
*
* Adds a line to the current patch from the current point to position
* (@x, @y) in pattern-space coordinates.
*
* If there is no current point before the call to
* cairo_pattern_mesh_line_to() this function will behave as
* cairo_pattern_mesh_move_to(@pattern, @x, @y).
*
* After this call the current point will be (@x, @y).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has 4 sides, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_line_to (cairo_pattern_t *pattern,
double x, double y)
{
cairo_mesh_pattern_t *mesh;
cairo_point_double_t last_point;
int last_point_idx, i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (mesh->current_side == -2) {
cairo_pattern_mesh_move_to (pattern, x, y);
return;
}
last_point_idx = 3 * (mesh->current_side + 1);
i = mesh_path_point_i[last_point_idx];
j = mesh_path_point_j[last_point_idx];
last_point = mesh->current_patch->points[i][j];
cairo_pattern_mesh_curve_to (pattern,
(2 * last_point.x + x) * (1. / 3),
(2 * last_point.y + y) * (1. / 3),
(last_point.x + 2 * x) * (1. / 3),
(last_point.y + 2 * y) * (1. / 3),
x, y);
}
slim_hidden_def (cairo_pattern_mesh_line_to);
/**
* cairo_pattern_mesh_move_to:
* @pattern: a #cairo_pattern_t
* @x: the X coordinate of the new position
* @y: the Y coordinate of the new position
*
* Define the first point of the current patch in a mesh pattern.
*
* After this call the current point will be (@x, @y).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has at leas one side, @pattern
* will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_move_to (cairo_pattern_t *pattern,
double x, double y)
{
cairo_mesh_pattern_t *mesh;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side >= 0)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
mesh->current_side = -1;
mesh->current_patch->points[0][0].x = x;
mesh->current_patch->points[0][0].y = y;
}
slim_hidden_def (cairo_pattern_mesh_move_to);
/**
* cairo_pattern_mesh_set_control_point:
* @pattern: a #cairo_pattern_t
* @point_num: the control point to set the position for
* @x: the X coordinate of the control point
* @y: the Y coordinate of the control point
*
* Set an internal control point of the current patch.
*
* Valid values for @point_num are from 0 to 3 and identify the
* control points as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @point_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_set_control_point (cairo_pattern_t *pattern,
unsigned int point_num,
double x,
double y)
{
cairo_mesh_pattern_t *mesh;
int i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
if (unlikely (point_num > 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_INDEX);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
i = mesh_control_point_i[point_num];
j = mesh_control_point_j[point_num];
mesh->current_patch->points[i][j].x = x;
mesh->current_patch->points[i][j].y = y;
mesh->has_control_point[point_num] = TRUE;
}
/* make room for at least one more color stop */
static cairo_status_t
_cairo_pattern_gradient_grow (cairo_gradient_pattern_t *pattern)
{
cairo_gradient_stop_t *new_stops;
int old_size = pattern->stops_size;
int embedded_size = ARRAY_LENGTH (pattern->stops_embedded);
int new_size = 2 * MAX (old_size, 4);
/* we have a local buffer at pattern->stops_embedded. try to fulfill the request
* from there. */
if (old_size < embedded_size) {
pattern->stops = pattern->stops_embedded;
pattern->stops_size = embedded_size;
return CAIRO_STATUS_SUCCESS;
}
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
assert (pattern->n_stops <= pattern->stops_size);
if (pattern->stops == pattern->stops_embedded) {
new_stops = _cairo_malloc_ab (new_size, sizeof (cairo_gradient_stop_t));
if (new_stops)
memcpy (new_stops, pattern->stops, old_size * sizeof (cairo_gradient_stop_t));
} else {
new_stops = _cairo_realloc_ab (pattern->stops,
new_size,
sizeof (cairo_gradient_stop_t));
}
if (unlikely (new_stops == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
pattern->stops = new_stops;
pattern->stops_size = new_size;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_pattern_mesh_set_corner_color (cairo_mesh_pattern_t *mesh,
unsigned int corner_num,
double red, double green, double blue,
double alpha)
{
cairo_color_t *color;
assert (mesh->current_patch);
assert (corner_num <= 3);
color = &mesh->current_patch->colors[corner_num];
color->red = red;
color->green = green;
color->blue = blue;
color->alpha = alpha;
color->red_short = _cairo_color_double_to_short (red);
color->green_short = _cairo_color_double_to_short (green);
color->blue_short = _cairo_color_double_to_short (blue);
color->alpha_short = _cairo_color_double_to_short (alpha);
mesh->has_color[corner_num] = TRUE;
}
/**
* cairo_pattern_mesh_set_corner_color_rgb:
* @pattern: a #cairo_pattern_t
* @corner_num: the corner to set the color for
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
*
* Sets the color of a corner of the current patch in a mesh pattern.
*
* The color is specified in the same way as in cairo_set_source_rgb().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @corner_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_set_corner_color_rgb (cairo_pattern_t *pattern,
unsigned int corner_num,
double red, double green, double blue)
{
cairo_pattern_mesh_set_corner_color_rgba (pattern, corner_num, red, green, blue, 1.0);
}
/**
* cairo_pattern_mesh_set_corner_color_rgba:
* @pattern: a #cairo_pattern_t
* @corner_num: the corner to set the color for
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
* @alpha: alpha component of color
*
* Sets the color of a corner of the current patch in a mesh pattern.
*
* The color is specified in the same way as in cairo_set_source_rgba().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @corner_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_pattern_mesh_set_corner_color_rgba (cairo_pattern_t *pattern,
unsigned int corner_num,
double red, double green, double blue,
double alpha)
{
cairo_mesh_pattern_t *mesh;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
if (unlikely (corner_num > 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_INDEX);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_pattern_mesh_set_corner_color (mesh, corner_num, red, green, blue, alpha);
}
slim_hidden_def (cairo_pattern_mesh_set_corner_color_rgba);
static void
_cairo_pattern_add_color_stop (cairo_gradient_pattern_t *pattern,
double offset,
double red,
double green,
double blue,
double alpha)
{
cairo_gradient_stop_t *stops;
unsigned int i;
if (pattern->n_stops >= pattern->stops_size) {
cairo_status_t status = _cairo_pattern_gradient_grow (pattern);
if (unlikely (status)) {
status = _cairo_pattern_set_error (&pattern->base, status);
return;
}
}
stops = pattern->stops;
for (i = 0; i < pattern->n_stops; i++)
{
if (offset < stops[i].offset)
{
memmove (&stops[i + 1], &stops[i],
sizeof (cairo_gradient_stop_t) * (pattern->n_stops - i));
break;
}
}
stops[i].offset = offset;
stops[i].color.red = red;
stops[i].color.green = green;
stops[i].color.blue = blue;
stops[i].color.alpha = alpha;
stops[i].color.red_short = _cairo_color_double_to_short (red);
stops[i].color.green_short = _cairo_color_double_to_short (green);
stops[i].color.blue_short = _cairo_color_double_to_short (blue);
stops[i].color.alpha_short = _cairo_color_double_to_short (alpha);
pattern->n_stops++;
}
/**
* cairo_pattern_add_color_stop_rgb:
* @pattern: a #cairo_pattern_t
* @offset: an offset in the range [0.0 .. 1.0]
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
*
* Adds an opaque color stop to a gradient pattern. The offset
* specifies the location along the gradient's control vector. For
* example, a linear gradient's control vector is from (x0,y0) to
* (x1,y1) while a radial gradient's control vector is from any point
* on the start circle to the corresponding point on the end circle.
*
* The color is specified in the same way as in cairo_set_source_rgb().
*
* If two (or more) stops are specified with identical offset values,
* they will be sorted according to the order in which the stops are
* added, (stops added earlier will compare less than stops added
* later). This can be useful for reliably making sharp color
* transitions instead of the typical blend.
*
*
* Note: If the pattern is not a gradient pattern, (eg. a linear or
* radial pattern), then the pattern will be put into an error status
* with a status of %CAIRO_STATUS_PATTERN_TYPE_MISMATCH.
**/
void
cairo_pattern_add_color_stop_rgb (cairo_pattern_t *pattern,
double offset,
double red,
double green,
double blue)
{
if (pattern->status)
return;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
{
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
offset = _cairo_restrict_value (offset, 0.0, 1.0);
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
_cairo_pattern_add_color_stop ((cairo_gradient_pattern_t *) pattern,
offset, red, green, blue, 1.0);
}
/**
* cairo_pattern_add_color_stop_rgba:
* @pattern: a #cairo_pattern_t
* @offset: an offset in the range [0.0 .. 1.0]
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
* @alpha: alpha component of color
*
* Adds a translucent color stop to a gradient pattern. The offset
* specifies the location along the gradient's control vector. For
* example, a linear gradient's control vector is from (x0,y0) to
* (x1,y1) while a radial gradient's control vector is from any point
* on the start circle to the corresponding point on the end circle.
*
* The color is specified in the same way as in cairo_set_source_rgba().
*
* If two (or more) stops are specified with identical offset values,
* they will be sorted according to the order in which the stops are
* added, (stops added earlier will compare less than stops added
* later). This can be useful for reliably making sharp color
* transitions instead of the typical blend.
*
* Note: If the pattern is not a gradient pattern, (eg. a linear or
* radial pattern), then the pattern will be put into an error status
* with a status of %CAIRO_STATUS_PATTERN_TYPE_MISMATCH.
*/
void
cairo_pattern_add_color_stop_rgba (cairo_pattern_t *pattern,
double offset,
double red,
double green,
double blue,
double alpha)
{
if (pattern->status)
return;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
{
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
offset = _cairo_restrict_value (offset, 0.0, 1.0);
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_pattern_add_color_stop ((cairo_gradient_pattern_t *) pattern,
offset, red, green, blue, alpha);
}
/**
* cairo_pattern_set_matrix:
* @pattern: a #cairo_pattern_t
* @matrix: a #cairo_matrix_t
*
* Sets the pattern's transformation matrix to @matrix. This matrix is
* a transformation from user space to pattern space.
*
* When a pattern is first created it always has the identity matrix
* for its transformation matrix, which means that pattern space is
* initially identical to user space.
*
* Important: Please note that the direction of this transformation
* matrix is from user space to pattern space. This means that if you
* imagine the flow from a pattern to user space (and on to device
* space), then coordinates in that flow will be transformed by the
* inverse of the pattern matrix.
*
* For example, if you want to make a pattern appear twice as large as
* it does by default the correct code to use is:
*
* <informalexample><programlisting>
* cairo_matrix_init_scale (&matrix, 0.5, 0.5);
* cairo_pattern_set_matrix (pattern, &matrix);
* </programlisting></informalexample>
*
* Meanwhile, using values of 2.0 rather than 0.5 in the code above
* would cause the pattern to appear at half of its default size.
*
* Also, please note the discussion of the user-space locking
* semantics of cairo_set_source().
**/
void
cairo_pattern_set_matrix (cairo_pattern_t *pattern,
const cairo_matrix_t *matrix)
{
cairo_matrix_t inverse;
cairo_status_t status;
if (pattern->status)
return;
if (memcmp (&pattern->matrix, matrix, sizeof (cairo_matrix_t)) == 0)
return;
pattern->matrix = *matrix;
inverse = *matrix;
status = cairo_matrix_invert (&inverse);
if (unlikely (status))
status = _cairo_pattern_set_error (pattern, status);
}
slim_hidden_def (cairo_pattern_set_matrix);
/**
* cairo_pattern_get_matrix:
* @pattern: a #cairo_pattern_t
* @matrix: return value for the matrix
*
* Stores the pattern's transformation matrix into @matrix.
**/
void
cairo_pattern_get_matrix (cairo_pattern_t *pattern, cairo_matrix_t *matrix)
{
*matrix = pattern->matrix;
}
/**
* cairo_pattern_set_filter:
* @pattern: a #cairo_pattern_t
* @filter: a #cairo_filter_t describing the filter to use for resizing
* the pattern
*
* Sets the filter to be used for resizing when using this pattern.
* See #cairo_filter_t for details on each filter.
*
* * Note that you might want to control filtering even when you do not
* have an explicit #cairo_pattern_t object, (for example when using
* cairo_set_source_surface()). In these cases, it is convenient to
* use cairo_get_source() to get access to the pattern that cairo
* creates implicitly. For example:
*
* <informalexample><programlisting>
* cairo_set_source_surface (cr, image, x, y);
* cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
* </programlisting></informalexample>
**/
void
cairo_pattern_set_filter (cairo_pattern_t *pattern, cairo_filter_t filter)
{
if (pattern->status)
return;
pattern->filter = filter;
}
/**
* cairo_pattern_get_filter:
* @pattern: a #cairo_pattern_t
*
* Gets the current filter for a pattern. See #cairo_filter_t
* for details on each filter.
*
* Return value: the current filter used for resizing the pattern.
**/
cairo_filter_t
cairo_pattern_get_filter (cairo_pattern_t *pattern)
{
return pattern->filter;
}
/**
* cairo_pattern_set_extend:
* @pattern: a #cairo_pattern_t
* @extend: a #cairo_extend_t describing how the area outside of the
* pattern will be drawn
*
* Sets the mode to be used for drawing outside the area of a pattern.
* See #cairo_extend_t for details on the semantics of each extend
* strategy.
*
* The default extend mode is %CAIRO_EXTEND_NONE for surface patterns
* and %CAIRO_EXTEND_PAD for gradient patterns.
**/
void
cairo_pattern_set_extend (cairo_pattern_t *pattern, cairo_extend_t extend)
{
if (pattern->status)
return;
pattern->extend = extend;
}
/**
* cairo_pattern_get_extend:
* @pattern: a #cairo_pattern_t
*
* Gets the current extend mode for a pattern. See #cairo_extend_t
* for details on the semantics of each extend strategy.
*
* Return value: the current extend strategy used for drawing the
* pattern.
**/
cairo_extend_t
cairo_pattern_get_extend (cairo_pattern_t *pattern)
{
return pattern->extend;
}
slim_hidden_def (cairo_pattern_get_extend);
void
_cairo_pattern_transform (cairo_pattern_t *pattern,
const cairo_matrix_t *ctm_inverse)
{
if (pattern->status)
return;
cairo_matrix_multiply (&pattern->matrix, ctm_inverse, &pattern->matrix);
}
static void
_cairo_linear_pattern_classify (cairo_linear_pattern_t *pattern,
double offset_x,
double offset_y,
int width,
int height,
cairo_bool_t *is_horizontal,
cairo_bool_t *is_vertical)
{
cairo_point_double_t point0, point1;
double a, b, c, d, tx, ty;
double scale, start, dx, dy;
cairo_fixed_t factors[3];
int i;
/* To classify a pattern as horizontal or vertical, we first
* compute the (fixed point) factors at the corners of the
* pattern. We actually only need 3/4 corners, so we skip the
* fourth.
*/
point0 = pattern->pd1;
point1 = pattern->pd2;
_cairo_matrix_get_affine (&pattern->base.base.matrix,
&a, &b, &c, &d, &tx, &ty);
dx = point1.x - point0.x;
dy = point1.y - point0.y;
scale = dx * dx + dy * dy;
scale = (scale) ? 1.0 / scale : 1.0;
start = dx * point0.x + dy * point0.y;
for (i = 0; i < 3; i++) {
double qx_device = (i % 2) * (width - 1) + offset_x;
double qy_device = (i / 2) * (height - 1) + offset_y;
/* transform fragment into pattern space */
double qx = a * qx_device + c * qy_device + tx;
double qy = b * qx_device + d * qy_device + ty;
factors[i] = _cairo_fixed_from_double (((dx * qx + dy * qy) - start) * scale);
}
/* We consider a pattern to be vertical if the fixed point factor
* at the two upper corners is the same. We could accept a small
* change, but determining what change is acceptable would require
* sorting the stops in the pattern and looking at the differences.
*
* Horizontal works the same way with the two left corners.
*/
*is_vertical = factors[1] == factors[0];
*is_horizontal = factors[2] == factors[0];
}
static cairo_int_status_t
_cairo_pattern_acquire_surface_for_gradient (const cairo_gradient_pattern_t *pattern,
cairo_surface_t *dst,
int x,
int y,
unsigned int width,
unsigned int height,
cairo_surface_t **out,
cairo_surface_attributes_t *attr)
{
cairo_image_surface_t *image;
pixman_image_t *pixman_image;
pixman_transform_t pixman_transform;
cairo_circle_double_t extremes[2];
pixman_point_fixed_t p1, p2;
cairo_status_t status;
cairo_bool_t repeat = FALSE;
int ix, iy;
pixman_gradient_stop_t pixman_stops_static[2];
pixman_gradient_stop_t *pixman_stops = pixman_stops_static;
unsigned int i;
int clone_offset_x, clone_offset_y;
cairo_matrix_t matrix = pattern->base.matrix;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (pattern->n_stops > ARRAY_LENGTH(pixman_stops_static)) {
pixman_stops = _cairo_malloc_ab (pattern->n_stops,
sizeof(pixman_gradient_stop_t));
if (unlikely (pixman_stops == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
for (i = 0; i < pattern->n_stops; i++) {
pixman_stops[i].x = _cairo_fixed_16_16_from_double (pattern->stops[i].offset);
pixman_stops[i].color.red = pattern->stops[i].color.red_short;
pixman_stops[i].color.green = pattern->stops[i].color.green_short;
pixman_stops[i].color.blue = pattern->stops[i].color.blue_short;
pixman_stops[i].color.alpha = pattern->stops[i].color.alpha_short;
}
_cairo_gradient_pattern_fit_to_range (pattern, PIXMAN_MAX_INT >> 1, &matrix, extremes);
p1.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
p1.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
p2.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
p2.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
if (pattern->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
pixman_image = pixman_image_create_linear_gradient (&p1, &p2,
pixman_stops,
pattern->n_stops);
} else {
pixman_fixed_t r1, r2;
r1 = _cairo_fixed_16_16_from_double (extremes[0].radius);
r2 = _cairo_fixed_16_16_from_double (extremes[1].radius);
pixman_image = pixman_image_create_radial_gradient (&p1, &p2, r1, r2,
pixman_stops,
pattern->n_stops);
}
if (pixman_stops != pixman_stops_static)
free (pixman_stops);
if (unlikely (pixman_image == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (_cairo_surface_is_image (dst))
{
image = (cairo_image_surface_t *)
_cairo_image_surface_create_for_pixman_image (pixman_image,
PIXMAN_a8r8g8b8);
if (image->base.status)
{
pixman_image_unref (pixman_image);
return image->base.status;
}
attr->x_offset = attr->y_offset = 0;
attr->matrix = matrix;
attr->extend = pattern->base.extend;
attr->filter = CAIRO_FILTER_NEAREST;
attr->has_component_alpha = pattern->base.has_component_alpha;
*out = &image->base;
return CAIRO_STATUS_SUCCESS;
}
if (pattern->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_bool_t is_horizontal;
cairo_bool_t is_vertical;
_cairo_linear_pattern_classify ((cairo_linear_pattern_t *)pattern,
x, y, width, height,
&is_horizontal, &is_vertical);
if (is_horizontal) {
height = 1;
repeat = TRUE;
}
/* width-1 repeating patterns are quite slow with scan-line based
* compositing code, so we use a wider strip and spend some extra
* expense in computing the gradient. It's possible that for narrow
* gradients we'd be better off using a 2 or 4 pixel strip; the
* wider the gradient, the more it's worth spending extra time
* computing a sample.
*/
if (is_vertical && width > 8) {
width = 8;
repeat = TRUE;
}
}
if (! pixman_image_set_filter (pixman_image, PIXMAN_FILTER_BILINEAR,
NULL, 0))
{
pixman_image_unref (pixman_image);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
image = (cairo_image_surface_t *)
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
if (image->base.status) {
pixman_image_unref (pixman_image);
return image->base.status;
}
ix = x;
iy = y;
status = _cairo_matrix_to_pixman_matrix_offset (&matrix,
pattern->base.filter,
width/2., height/2.,
&pixman_transform,
&ix, &iy);
if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
if (unlikely (status != CAIRO_STATUS_SUCCESS) ||
! pixman_image_set_transform (pixman_image, &pixman_transform))
{
cairo_surface_destroy (&image->base);
pixman_image_unref (pixman_image);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
}
switch (pattern->base.extend) {
case CAIRO_EXTEND_NONE:
pixman_image_set_repeat (pixman_image, PIXMAN_REPEAT_NONE);
break;
case CAIRO_EXTEND_REPEAT:
pixman_image_set_repeat (pixman_image, PIXMAN_REPEAT_NORMAL);
break;
case CAIRO_EXTEND_REFLECT:
pixman_image_set_repeat (pixman_image, PIXMAN_REPEAT_REFLECT);
break;
case CAIRO_EXTEND_PAD:
pixman_image_set_repeat (pixman_image, PIXMAN_REPEAT_PAD);
break;
}
pixman_image_composite32 (PIXMAN_OP_SRC,
pixman_image,
NULL,
image->pixman_image,
ix, iy,
0, 0,
0, 0,
width, height);
pixman_image_unref (pixman_image);
_cairo_debug_check_image_surface_is_defined (&image->base);
status = _cairo_surface_clone_similar (dst, &image->base,
0, 0, width, height,
&clone_offset_x,
&clone_offset_y,
out);
cairo_surface_destroy (&image->base);
attr->x_offset = -x;
attr->y_offset = -y;
cairo_matrix_init_identity (&attr->matrix);
attr->extend = repeat ? CAIRO_EXTEND_REPEAT : CAIRO_EXTEND_NONE;
attr->filter = CAIRO_FILTER_NEAREST;
attr->has_component_alpha = pattern->base.has_component_alpha;
return status;
}
static cairo_int_status_t
_cairo_pattern_acquire_surface_for_mesh (const cairo_mesh_pattern_t *pattern,
cairo_surface_t *dst,
int x,
int y,
unsigned int width,
unsigned int height,
cairo_surface_t **out,
cairo_surface_attributes_t *attr)
{
cairo_surface_t *image;
void *data;
cairo_status_t status;
int clone_offset_x, clone_offset_y;
int stride;
image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
if (unlikely (image->status))
return image->status;
stride = cairo_image_surface_get_stride (image);
data = cairo_image_surface_get_data (image);
_cairo_mesh_pattern_rasterize (pattern, data, width, height, stride, -x, -y);
attr->x_offset = -x;
attr->y_offset = -y;
attr->filter = CAIRO_FILTER_NEAREST;
attr->extend = pattern->base.extend;
cairo_matrix_init_identity (&attr->matrix);
attr->has_component_alpha = pattern->base.has_component_alpha;
if (_cairo_surface_is_image (dst)) {
*out = image;
return CAIRO_STATUS_SUCCESS;
}
status = _cairo_surface_clone_similar (dst, image,
0, 0, width, height,
&clone_offset_x,
&clone_offset_y, out);
cairo_surface_destroy (image);
return status;
}
/* We maintain a small cache here, because we don't want to constantly
* recreate surfaces for simple solid colors. */
#define MAX_SURFACE_CACHE_SIZE 16
static struct {
struct _cairo_pattern_solid_surface_cache{
cairo_color_t color;
cairo_surface_t *surface;
} cache[MAX_SURFACE_CACHE_SIZE];
int size;
} solid_surface_cache;
static cairo_bool_t
_cairo_pattern_solid_surface_matches (
const struct _cairo_pattern_solid_surface_cache *cache,
const cairo_solid_pattern_t *pattern,
cairo_surface_t *dst)
{
if (cairo_surface_get_content (cache->surface) != _cairo_color_get_content (&pattern->color))
return FALSE;
if (CAIRO_REFERENCE_COUNT_GET_VALUE (&cache->surface->ref_count) != 1)
return FALSE;
if (! _cairo_surface_is_similar (cache->surface, dst))
return FALSE;
return TRUE;
}
static cairo_bool_t
_cairo_pattern_solid_surface_matches_color (
const struct _cairo_pattern_solid_surface_cache *cache,
const cairo_solid_pattern_t *pattern,
cairo_surface_t *dst)
{
if (! _cairo_color_equal (&cache->color, &pattern->color))
return FALSE;
return _cairo_pattern_solid_surface_matches (cache, pattern, dst);
}
static cairo_int_status_t
_cairo_pattern_acquire_surface_for_solid (const cairo_solid_pattern_t *pattern,
cairo_surface_t *dst,
int x,
int y,
unsigned int width,
unsigned int height,
cairo_surface_t **out,
cairo_surface_attributes_t *attribs)
{
static int i;
cairo_surface_t *surface, *to_destroy = NULL;
cairo_status_t status;
CAIRO_MUTEX_LOCK (_cairo_pattern_solid_surface_cache_lock);
/* Check cache first */
if (i < solid_surface_cache.size &&
_cairo_pattern_solid_surface_matches_color (&solid_surface_cache.cache[i],
pattern,
dst))
{
goto DONE;
}
for (i = 0 ; i < solid_surface_cache.size; i++) {
if (_cairo_pattern_solid_surface_matches_color (&solid_surface_cache.cache[i],
pattern,
dst))
{
goto DONE;
}
}
/* Choose a surface to repaint/evict */
surface = NULL;
if (solid_surface_cache.size == MAX_SURFACE_CACHE_SIZE) {
i = rand () % MAX_SURFACE_CACHE_SIZE;
surface = solid_surface_cache.cache[i].surface;
if (_cairo_pattern_solid_surface_matches (&solid_surface_cache.cache[i],
pattern,
dst))
{
/* Reuse the surface instead of evicting */
status = _cairo_surface_repaint_solid_pattern_surface (dst, surface, pattern);
if (unlikely (status))
goto EVICT;
cairo_surface_reference (surface);
}
else
{
EVICT:
surface = NULL;
}
}
if (surface == NULL) {
/* Not cached, need to create new */
surface = _cairo_surface_create_solid_pattern_surface (dst, pattern);
if (surface == NULL) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto UNLOCK;
}
if (unlikely (surface->status)) {
status = surface->status;
goto UNLOCK;
}
if (unlikely (! _cairo_surface_is_similar (surface, dst)))
{
/* In the rare event of a substitute surface being returned,
* don't cache the fallback.
*/
*out = surface;
goto NOCACHE;
}
}
if (i == solid_surface_cache.size)
solid_surface_cache.size++;
to_destroy = solid_surface_cache.cache[i].surface;
solid_surface_cache.cache[i].surface = surface;
solid_surface_cache.cache[i].color = pattern->color;
DONE:
*out = cairo_surface_reference (solid_surface_cache.cache[i].surface);
NOCACHE:
attribs->x_offset = attribs->y_offset = 0;
cairo_matrix_init_identity (&attribs->matrix);
attribs->extend = CAIRO_EXTEND_REPEAT;
attribs->filter = CAIRO_FILTER_NEAREST;
attribs->has_component_alpha = pattern->base.has_component_alpha;
status = CAIRO_STATUS_SUCCESS;
UNLOCK:
CAIRO_MUTEX_UNLOCK (_cairo_pattern_solid_surface_cache_lock);
if (to_destroy)
cairo_surface_destroy (to_destroy);
return status;
}
static void
_cairo_pattern_reset_solid_surface_cache (void)
{
CAIRO_MUTEX_LOCK (_cairo_pattern_solid_surface_cache_lock);
/* remove surfaces starting from the end so that solid_surface_cache.cache
* is always in a consistent state when we release the mutex. */
while (solid_surface_cache.size) {
cairo_surface_t *surface;
solid_surface_cache.size--;
surface = solid_surface_cache.cache[solid_surface_cache.size].surface;
solid_surface_cache.cache[solid_surface_cache.size].surface = NULL;
/* release the lock to avoid the possibility of a recursive
* deadlock when the surface destroy closure gets called */
CAIRO_MUTEX_UNLOCK (_cairo_pattern_solid_surface_cache_lock);
cairo_surface_destroy (surface);
CAIRO_MUTEX_LOCK (_cairo_pattern_solid_surface_cache_lock);
}
CAIRO_MUTEX_UNLOCK (_cairo_pattern_solid_surface_cache_lock);
}
static cairo_bool_t
_linear_pattern_is_degenerate (const cairo_linear_pattern_t *linear)
{
return fabs (linear->pd1.x - linear->pd2.x) < DBL_EPSILON &&
fabs (linear->pd1.y - linear->pd2.y) < DBL_EPSILON;
}
static cairo_bool_t
_radial_pattern_is_degenerate (const cairo_radial_pattern_t *radial)
{
/* A radial pattern is considered degenerate if it can be
* represented as a solid or clear pattern. This corresponds to
* one of the two cases:
*
* 1) The radii are both very small:
* |dr| < DBL_EPSILON && min (r0, r1) < DBL_EPSILON
*
* 2) The two circles have about the same radius and are very
* close to each other (approximately a cylinder gradient that
* doesn't move with the parameter):
* |dr| < DBL_EPSILON && max (|dx|, |dy|) < 2 * DBL_EPSILON
*
* These checks are consistent with the assumptions used in
* _cairo_radial_pattern_box_to_parameter ().
*/
return fabs (radial->cd1.radius - radial->cd2.radius) < DBL_EPSILON &&
(MIN (radial->cd1.radius, radial->cd2.radius) < DBL_EPSILON ||
MAX (fabs (radial->cd1.center.x - radial->cd2.center.x),
fabs (radial->cd1.center.y - radial->cd2.center.y)) < 2 * DBL_EPSILON);
}
static void
_cairo_linear_pattern_box_to_parameter (const cairo_linear_pattern_t *linear,
double x0, double y0,
double x1, double y1,
double range[2])
{
double t0, tdx, tdy;
double p1x, p1y, pdx, pdy, invsqnorm;
assert (! _linear_pattern_is_degenerate (linear));
/*
* Linear gradients are othrogonal to the line passing through
* their extremes. Because of convexity, the parameter range can
* be computed as the convex hull (one the real line) of the
* parameter values of the 4 corners of the box.
*
* The parameter value t for a point (x,y) can be computed as:
*
* t = (p2 - p1) . (x,y) / |p2 - p1|^2
*
* t0 is the t value for the top left corner
* tdx is the difference between left and right corners
* tdy is the difference between top and bottom corners
*/
p1x = linear->pd1.x;
p1y = linear->pd1.y;
pdx = linear->pd2.x - p1x;
pdy = linear->pd2.y - p1y;
invsqnorm = 1.0 / (pdx * pdx + pdy * pdy);
pdx *= invsqnorm;
pdy *= invsqnorm;
t0 = (x0 - p1x) * pdx + (y0 - p1y) * pdy;
tdx = (x1 - x0) * pdx;
tdy = (y1 - y0) * pdy;
/*
* Because of the linearity of the t value, tdx can simply be
* added the t0 to move along the top edge. After this, range[0]
* and range[1] represent the parameter range for the top edge, so
* extending it to include the whole box simply requires adding
* tdy to the correct extreme.
*/
range[0] = range[1] = t0;
if (tdx < 0)
range[0] += tdx;
else
range[1] += tdx;
if (tdy < 0)
range[0] += tdy;
else
range[1] += tdy;
}
static cairo_bool_t
_extend_range (double range[2], double value, cairo_bool_t valid)
{
if (!valid)
range[0] = range[1] = value;
else if (value < range[0])
range[0] = value;
else if (value > range[1])
range[1] = value;
return TRUE;
}
static void
_cairo_radial_pattern_box_to_parameter (const cairo_radial_pattern_t *radial,
double x0, double y0,
double x1, double y1,
double tolerance,
double range[2])
{
double cx, cy, cr, dx, dy, dr;
double a, x_focus, y_focus;
double mindr, minx, miny, maxx, maxy;
cairo_bool_t valid;
assert (! _radial_pattern_is_degenerate (radial));
assert (x0 < x1);
assert (y0 < y1);
tolerance = MAX (tolerance, DBL_EPSILON);
range[0] = range[1] = 0;
valid = FALSE;
x_focus = y_focus = 0; /* silence gcc */
cx = radial->cd1.center.x;
cy = radial->cd1.center.y;
cr = radial->cd1.radius;
dx = radial->cd2.center.x - cx;
dy = radial->cd2.center.y - cy;
dr = radial->cd2.radius - cr;
/* translate by -(cx, cy) to simplify computations */
x0 -= cx;
y0 -= cy;
x1 -= cx;
y1 -= cy;
/* enlarge boundaries slightly to avoid rounding problems in the
* parameter range computation */
x0 -= DBL_EPSILON;
y0 -= DBL_EPSILON;
x1 += DBL_EPSILON;
y1 += DBL_EPSILON;
/* enlarge boundaries even more to avoid rounding problems when
* testing if a point belongs to the box */
minx = x0 - DBL_EPSILON;
miny = y0 - DBL_EPSILON;
maxx = x1 + DBL_EPSILON;
maxy = y1 + DBL_EPSILON;
/* we dont' allow negative radiuses, so we will be checking that
* t*dr >= mindr to consider t valid */
mindr = -(cr + DBL_EPSILON);
/*
* After the previous transformations, the start circle is
* centered in the origin and has radius cr. A 1-unit change in
* the t parameter corresponds to dx,dy,dr changes in the x,y,r of
* the circle (center coordinates, radius).
*
* To compute the minimum range needed to correctly draw the
* pattern, we start with an empty range and extend it to include
* the circles touching the bounding box or within it.
*/
/*
* Focus, the point where the circle has radius == 0.
*
* r = cr + t * dr = 0
* t = -cr / dr
*
* If the radius is constant (dr == 0) there is no focus (the
* gradient represents a cylinder instead of a cone).
*/
if (fabs (dr) >= DBL_EPSILON) {
double t_focus;
t_focus = -cr / dr;
x_focus = t_focus * dx;
y_focus = t_focus * dy;
if (minx <= x_focus && x_focus <= maxx &&
miny <= y_focus && y_focus <= maxy)
{
valid = _extend_range (range, t_focus, valid);
}
}
/*
* Circles externally tangent to box edges.
*
* All circles have center in (dx, dy) * t
*
* If the circle is tangent to the line defined by the edge of the
* box, then at least one of the following holds true:
*
* (dx*t) + (cr + dr*t) == x0 (left edge)
* (dx*t) - (cr + dr*t) == x1 (right edge)
* (dy*t) + (cr + dr*t) == y0 (top edge)
* (dy*t) - (cr + dr*t) == y1 (bottom edge)
*
* The solution is only valid if the tangent point is actually on
* the edge, i.e. if its y coordinate is in [y0,y1] for left/right
* edges and if its x coordinate is in [x0,x1] for top/bottom
* edges.
*
* For the first equation:
*
* (dx + dr) * t = x0 - cr
* t = (x0 - cr) / (dx + dr)
* y = dy * t
*
* in the code this becomes:
*
* t_edge = (num) / (den)
* v = (delta) * t_edge
*
* If the denominator in t is 0, the pattern is tangent to a line
* parallel to the edge under examination. The corner-case where
* the boundary line is the same as the edge is handled by the
* focus point case and/or by the a==0 case.
*/
#define T_EDGE(num,den,delta,lower,upper) \
if (fabs (den) >= DBL_EPSILON) { \
double t_edge, v; \
\
t_edge = (num) / (den); \
v = t_edge * (delta); \
if (t_edge * dr >= mindr && (lower) <= v && v <= (upper)) \
valid = _extend_range (range, t_edge, valid); \
}
/* circles tangent (externally) to left/right/top/bottom edge */
T_EDGE (x0 - cr, dx + dr, dy, miny, maxy);
T_EDGE (x1 + cr, dx - dr, dy, miny, maxy);
T_EDGE (y0 - cr, dy + dr, dx, minx, maxx);
T_EDGE (y1 + cr, dy - dr, dx, minx, maxx);
#undef T_EDGE
/*
* Circles passing through a corner.
*
* A circle passing through the point (x,y) satisfies:
*
* (x-t*dx)^2 + (y-t*dy)^2 == (cr + t*dr)^2
*
* If we set:
* a = dx^2 + dy^2 - dr^2
* b = x*dx + y*dy + cr*dr
* c = x^2 + y^2 - cr^2
* we have:
* a*t^2 - 2*b*t + c == 0
*/
a = dx * dx + dy * dy - dr * dr;
if (fabs (a) < DBL_EPSILON * DBL_EPSILON) {
double b, maxd2;
/* Ensure that gradients with both a and dr small are
* considered degenerate.
* The floating point version of the degeneracy test implemented
* in _radial_pattern_is_degenerate() is:
*
* 1) The circles are practically the same size:
* |dr| < DBL_EPSILON
* AND
* 2a) The circles are both very small:
* min (r0, r1) < DBL_EPSILON
* OR
* 2b) The circles are very close to each other:
* max (|dx|, |dy|) < 2 * DBL_EPSILON
*
* Assuming that the gradient is not degenerate, we want to
* show that |a| < DBL_EPSILON^2 implies |dr| >= DBL_EPSILON.
*
* If the gradient is not degenerate yet it has |dr| <
* DBL_EPSILON, (2b) is false, thus:
*
* max (|dx|, |dy|) >= 2*DBL_EPSILON
* which implies:
* 4*DBL_EPSILON^2 <= max (|dx|, |dy|)^2 <= dx^2 + dy^2
*
* From the definition of a, we get:
* a = dx^2 + dy^2 - dr^2 < DBL_EPSILON^2
* dx^2 + dy^2 - DBL_EPSILON^2 < dr^2
* 3*DBL_EPSILON^2 < dr^2
*
* which is inconsistent with the hypotheses, thus |dr| <
* DBL_EPSILON is false or the gradient is degenerate.
*/
assert (fabs (dr) >= DBL_EPSILON);
/*
* If a == 0, all the circles are tangent to a line in the
* focus point. If this line is within the box extents, we
* should add the circle with infinite radius, but this would
* make the range unbounded, so we add the smallest circle whose
* distance to the desired (degenerate) circle within the
* bounding box does not exceed tolerance.
*
* The equation of the line is b==0, i.e.:
* x*dx + y*dy + cr*dr == 0
*
* We compute the intersection of the line with the box and
* keep the intersection with maximum square distance (maxd2)
* from the focus point.
*
* In the code the intersection is represented in another
* coordinate system, whose origin is the focus point and
* which has a u,v axes, which are respectively orthogonal and
* parallel to the edge being intersected.
*
* The intersection is valid only if it belongs to the box,
* otherwise it is ignored.
*
* For example:
*
* y = y0
* x*dx + y0*dy + cr*dr == 0
* x = -(y0*dy + cr*dr) / dx
*
* which in (u,v) is:
* u = y0 - y_focus
* v = -(y0*dy + cr*dr) / dx - x_focus
*
* In the code:
* u = (edge) - (u_origin)
* v = -((edge) * (delta) + cr*dr) / (den) - v_focus
*/
#define T_EDGE(edge,delta,den,lower,upper,u_origin,v_origin) \
if (fabs (den) >= DBL_EPSILON) { \
double v; \
\
v = -((edge) * (delta) + cr * dr) / (den); \
if ((lower) <= v && v <= (upper)) { \
double u, d2; \
\
u = (edge) - (u_origin); \
v -= (v_origin); \
d2 = u*u + v*v; \
if (maxd2 < d2) \
maxd2 = d2; \
} \
}
maxd2 = 0;
/* degenerate circles (lines) passing through each edge */
T_EDGE (y0, dy, dx, minx, maxx, y_focus, x_focus);
T_EDGE (y1, dy, dx, minx, maxx, y_focus, x_focus);
T_EDGE (x0, dx, dy, miny, maxy, x_focus, y_focus);
T_EDGE (x1, dx, dy, miny, maxy, x_focus, y_focus);
#undef T_EDGE
/*
* The limit circle can be transformed rigidly to the y=0 line
* and the circles tangent to it in (0,0) are:
*
* x^2 + (y-r)^2 = r^2 <=> x^2 + y^2 - 2*y*r = 0
*
* y is the distance from the line, in our case tolerance;
* x is the distance along the line, i.e. sqrt(maxd2),
* so:
*
* r = cr + dr * t = (maxd2 + tolerance^2) / (2*tolerance)
* t = (r - cr) / dr =
* (maxd2 + tolerance^2 - 2*tolerance*cr) / (2*tolerance*dr)
*/
if (maxd2 > 0) {
double t_limit = maxd2 + tolerance*tolerance - 2*tolerance*cr;
t_limit /= 2 * tolerance * dr;
valid = _extend_range (range, t_limit, valid);
}
/*
* Nondegenerate, nonlimit circles passing through the corners.
*
* a == 0 && a*t^2 - 2*b*t + c == 0
*
* t = c / (2*b)
*
* The b == 0 case has just been handled, so we only have to
* compute this if b != 0.
*/
#define T_CORNER(x,y) \
b = (x) * dx + (y) * dy + cr * dr; \
if (fabs (b) >= DBL_EPSILON) { \
double t_corner; \
double x2 = (x) * (x); \
double y2 = (y) * (y); \
double cr2 = (cr) * (cr); \
double c = x2 + y2 - cr2; \
\
t_corner = 0.5 * c / b; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
}
/* circles touching each corner */
T_CORNER (x0, y0);
T_CORNER (x0, y1);
T_CORNER (x1, y0);
T_CORNER (x1, y1);
#undef T_CORNER
} else {
double inva, b, c, d;
inva = 1 / a;
/*
* Nondegenerate, nonlimit circles passing through the corners.
*
* a != 0 && a*t^2 - 2*b*t + c == 0
*
* t = (b +- sqrt (b*b - a*c)) / a
*
* If the argument of sqrt() is negative, then no circle
* passes through the corner.
*/
#define T_CORNER(x,y) \
b = (x) * dx + (y) * dy + cr * dr; \
c = (x) * (x) + (y) * (y) - cr * cr; \
d = b * b - a * c; \
if (d >= 0) { \
double t_corner; \
\
d = sqrt (d); \
t_corner = (b + d) * inva; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
t_corner = (b - d) * inva; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
}
/* circles touching each corner */
T_CORNER (x0, y0);
T_CORNER (x0, y1);
T_CORNER (x1, y0);
T_CORNER (x1, y1);
#undef T_CORNER
}
}
/**
* _cairo_gradient_pattern_box_to_parameter
*
* Compute a interpolation range sufficient to draw (within the given
* tolerance) the gradient in the given box getting the same result as
* using the (-inf, +inf) range.
*
* Assumes that the pattern is not degenerate. This can be guaranteed
* by simplifying it to a solid clear if _cairo_pattern_is_clear or to
* a solid color if _cairo_gradient_pattern_is_solid.
*
* The range isn't guaranteed to be minimal, but it tries to.
**/
void
_cairo_gradient_pattern_box_to_parameter (const cairo_gradient_pattern_t *gradient,
double x0, double y0,
double x1, double y1,
double tolerance,
double out_range[2])
{
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
_cairo_linear_pattern_box_to_parameter ((cairo_linear_pattern_t *) gradient,
x0, y0, x1, y1, out_range);
} else {
_cairo_radial_pattern_box_to_parameter ((cairo_radial_pattern_t *) gradient,
x0, y0, x1, y1, tolerance, out_range);
}
}
/**
* _cairo_gradient_pattern_interpolate
*
* Interpolate between the start and end objects of linear or radial
* gradients. The interpolated object is stored in out_circle, with
* the radius being zero in the linear gradient case.
**/
void
_cairo_gradient_pattern_interpolate (const cairo_gradient_pattern_t *gradient,
double t,
cairo_circle_double_t *out_circle)
{
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
#define lerp(a,b) (a)*(1-t) + (b)*t
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
out_circle->center.x = lerp (linear->pd1.x, linear->pd2.x);
out_circle->center.y = lerp (linear->pd1.y, linear->pd2.y);
out_circle->radius = 0;
} else {
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t *) gradient;
out_circle->center.x = lerp (radial->cd1.center.x, radial->cd2.center.x);
out_circle->center.y = lerp (radial->cd1.center.y, radial->cd2.center.y);
out_circle->radius = lerp (radial->cd1.radius , radial->cd2.radius);
}
#undef lerp
}
/**
* _cairo_gradient_pattern_fit_to_range
*
* Scale the extremes of a gradient to guarantee that the coordinates
* and their deltas are within the range (-max_value, max_value). The
* new extremes are stored in out_circle.
*
* The pattern matrix is scaled to guarantee that the aspect of the
* gradient is the same and the result is stored in out_matrix.
*
**/
void
_cairo_gradient_pattern_fit_to_range (const cairo_gradient_pattern_t *gradient,
double max_value,
cairo_matrix_t *out_matrix,
cairo_circle_double_t out_circle[2])
{
double dim;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
out_circle[0].center = linear->pd1;
out_circle[0].radius = 0;
out_circle[1].center = linear->pd2;
out_circle[1].radius = 0;
dim = fabs (linear->pd1.x);
dim = MAX (dim, fabs (linear->pd1.y));
dim = MAX (dim, fabs (linear->pd2.x));
dim = MAX (dim, fabs (linear->pd2.y));
dim = MAX (dim, fabs (linear->pd1.x - linear->pd2.x));
dim = MAX (dim, fabs (linear->pd1.y - linear->pd2.y));
} else {
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t *) gradient;
out_circle[0] = radial->cd1;
out_circle[1] = radial->cd2;
dim = fabs (radial->cd1.center.x);
dim = MAX (dim, fabs (radial->cd1.center.y));
dim = MAX (dim, fabs (radial->cd1.radius));
dim = MAX (dim, fabs (radial->cd2.center.x));
dim = MAX (dim, fabs (radial->cd2.center.y));
dim = MAX (dim, fabs (radial->cd2.radius));
dim = MAX (dim, fabs (radial->cd1.center.x - radial->cd2.center.x));
dim = MAX (dim, fabs (radial->cd1.center.y - radial->cd2.center.y));
dim = MAX (dim, fabs (radial->cd1.radius - radial->cd2.radius));
}
if (unlikely (dim > max_value)) {
cairo_matrix_t scale;
dim = max_value / dim;
out_circle[0].center.x *= dim;
out_circle[0].center.y *= dim;
out_circle[0].radius *= dim;
out_circle[1].center.x *= dim;
out_circle[1].center.y *= dim;
out_circle[1].radius *= dim;
cairo_matrix_init_scale (&scale, dim, dim);
cairo_matrix_multiply (out_matrix, &gradient->base.matrix, &scale);
} else {
*out_matrix = gradient->base.matrix;
}
}
static cairo_bool_t
_gradient_is_clear (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->n_stops == 0 ||
(gradient->base.extend == CAIRO_EXTEND_NONE &&
gradient->stops[0].offset == gradient->stops[gradient->n_stops - 1].offset))
return TRUE;
if (gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL) {
/* degenerate radial gradients are clear */
if (_radial_pattern_is_degenerate ((cairo_radial_pattern_t *) gradient))
return TRUE;
} else if (gradient->base.extend == CAIRO_EXTEND_NONE) {
/* EXTEND_NONE degenerate linear gradients are clear */
if (_linear_pattern_is_degenerate ((cairo_linear_pattern_t *) gradient))
return TRUE;
}
/* Check if the extents intersect the drawn part of the pattern. */
if (extents != NULL &&
(gradient->base.extend == CAIRO_EXTEND_NONE ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL))
{
double t[2];
_cairo_gradient_pattern_box_to_parameter (gradient,
extents->x,
extents->y,
extents->x + extents->width,
extents->y + extents->height,
DBL_EPSILON,
t);
if (gradient->base.extend == CAIRO_EXTEND_NONE &&
(t[0] >= gradient->stops[gradient->n_stops - 1].offset ||
t[1] <= gradient->stops[0].offset))
{
return TRUE;
}
if (t[0] == t[1])
return TRUE;
}
for (i = 0; i < gradient->n_stops; i++)
if (! CAIRO_COLOR_IS_CLEAR (&gradient->stops[i].color))
return FALSE;
return TRUE;
}
static void
_gradient_color_average (const cairo_gradient_pattern_t *gradient,
cairo_color_t *color)
{
double delta0, delta1;
double r, g, b, a;
unsigned int i, start = 1, end;
assert (gradient->n_stops > 0);
assert (gradient->base.extend != CAIRO_EXTEND_NONE);
if (gradient->n_stops == 1) {
_cairo_color_init_rgba (color,
gradient->stops[0].color.red,
gradient->stops[0].color.green,
gradient->stops[0].color.blue,
gradient->stops[0].color.alpha);
return;
}
end = gradient->n_stops - 1;
switch (gradient->base.extend) {
case CAIRO_EXTEND_REPEAT:
/*
* Sa, Sb and Sy, Sz are the first two and last two stops respectively.
* The weight of the first and last stop can be computed as the area of
* the following triangles (taken with height 1, since the whole [0-1]
* will have total weight 1 this way): b*h/2
*
* + +
* / |\ / | \
* / | \ / | \
* / | \ / | \
* ~~~~~+---+---+---+~~~~~~~+-------+---+---+~~~~~
* -1+Sz 0 Sa Sb Sy Sz 1 1+Sa
*
* For the first stop: (Sb-(-1+Sz)/2 = (1+Sb-Sz)/2
* For the last stop: ((1+Sa)-Sy)/2 = (1+Sa-Sy)/2
* Halving the result is done after summing up all the areas.
*/
delta0 = 1.0 + gradient->stops[1].offset - gradient->stops[end].offset;
delta1 = 1.0 + gradient->stops[0].offset - gradient->stops[end-1].offset;
break;
case CAIRO_EXTEND_REFLECT:
/*
* Sa, Sb and Sy, Sz are the first two and last two stops respectively.
* The weight of the first and last stop can be computed as the area of
* the following trapezoids (taken with height 1, since the whole [0-1]
* will have total weight 1 this way): (b+B)*h/2
*
* +-------+ +---+
* | |\ / | |
* | | \ / | |
* | | \ / | |
* +-------+---+~~~~~~~+-------+---+
* 0 Sa Sb Sy Sz 1
*
* For the first stop: (Sa+Sb)/2
* For the last stop: ((1-Sz) + (1-Sy))/2 = (2-Sy-Sz)/2
* Halving the result is done after summing up all the areas.
*/
delta0 = gradient->stops[0].offset + gradient->stops[1].offset;
delta1 = 2.0 - gradient->stops[end-1].offset - gradient->stops[end].offset;
break;
case CAIRO_EXTEND_PAD:
/* PAD is computed as the average of the first and last stop:
* - take both of them with weight 1 (they will be halved
* after the whole sum has been computed).
* - avoid summing any of the inner stops.
*/
delta0 = delta1 = 1.0;
start = end;
break;
case CAIRO_EXTEND_NONE:
default:
ASSERT_NOT_REACHED;
_cairo_color_init_rgba (color, 0, 0, 0, 0);
return;
}
r = delta0 * gradient->stops[0].color.red;
g = delta0 * gradient->stops[0].color.green;
b = delta0 * gradient->stops[0].color.blue;
a = delta0 * gradient->stops[0].color.alpha;
for (i = start; i < end; ++i) {
/* Inner stops weight is the same as the area of the triangle they influence
* (which goes from the stop before to the stop after), again with height 1
* since the whole must sum up to 1: b*h/2
* Halving is done after the whole sum has been computed.
*/
double delta = gradient->stops[i+1].offset - gradient->stops[i-1].offset;
r += delta * gradient->stops[i].color.red;
g += delta * gradient->stops[i].color.green;
b += delta * gradient->stops[i].color.blue;
a += delta * gradient->stops[i].color.alpha;
}
r += delta1 * gradient->stops[end].color.red;
g += delta1 * gradient->stops[end].color.green;
b += delta1 * gradient->stops[end].color.blue;
a += delta1 * gradient->stops[end].color.alpha;
_cairo_color_init_rgba (color, r * .5, g * .5, b * .5, a * .5);
}
/**
* _cairo_pattern_alpha_range
*
* Convenience function to determine the minimum and maximum alpha in
* the drawn part of a pattern (i.e. ignoring clear parts caused by
* extend modes and/or pattern shape).
*
* If not NULL, out_min and out_max will be set respectively to the
* minimum and maximum alpha value of the pattern.
**/
void
_cairo_pattern_alpha_range (const cairo_pattern_t *pattern,
double *out_min,
double *out_max)
{
double alpha_min, alpha_max;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID: {
const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
alpha_min = alpha_max = solid->color.alpha;
break;
}
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
const cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
unsigned int i;
assert (gradient->n_stops >= 1);
alpha_min = alpha_max = gradient->stops[0].color.alpha;
for (i = 1; i < gradient->n_stops; i++) {
if (alpha_min > gradient->stops[i].color.alpha)
alpha_min = gradient->stops[i].color.alpha;
else if (alpha_max < gradient->stops[i].color.alpha)
alpha_max = gradient->stops[i].color.alpha;
}
break;
}
case CAIRO_PATTERN_TYPE_MESH: {
const cairo_mesh_pattern_t *mesh = (const cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch = _cairo_array_index_const (&mesh->patches, 0);
unsigned int i, j, n = _cairo_array_num_elements (&mesh->patches);
assert (n >= 1);
alpha_min = alpha_max = patch[0].colors[0].alpha;
for (i = 0; i < n; i++) {
for (j = 0; j < 4; j++) {
if (patch[i].colors[j].alpha < alpha_min)
alpha_min = patch[i].colors[j].alpha;
else if (patch[i].colors[j].alpha > alpha_max)
alpha_max = patch[i].colors[j].alpha;
}
}
break;
}
default:
ASSERT_NOT_REACHED;
/* fall through */
case CAIRO_PATTERN_TYPE_SURFACE:
alpha_min = 0;
alpha_max = 1;
break;
}
if (out_min)
*out_min = alpha_min;
if (out_max)
*out_max = alpha_max;
}
/**
* _cairo_mesh_pattern_coord_box
*
* Convenience function to determine the range of the coordinates of
* the points used to define the patches of the mesh.
*
* This is guaranteed to contain the pattern extents, but might not be
* tight, just like a Bezier curve is always inside the convex hull of
* the control points.
*
* This function cannot be used while the mesh is being constructed.
*
* The function returns TRUE and sets the output parametes to define
* the coodrinate range if the mesh pattern contains at least one
* patch, otherwise it returns FALSE.
**/
cairo_bool_t
_cairo_mesh_pattern_coord_box (const cairo_mesh_pattern_t *mesh,
double *out_xmin,
double *out_ymin,
double *out_xmax,
double *out_ymax)
{
const cairo_mesh_patch_t *patch;
unsigned int num_patches, i, j, k;
double x0, y0, x1, y1;
assert (mesh->current_patch == NULL);
num_patches = _cairo_array_num_elements (&mesh->patches);
if (num_patches == 0)
return FALSE;
patch = _cairo_array_index_const (&mesh->patches, 0);
x0 = x1 = patch->points[0][0].x;
y0 = y1 = patch->points[0][0].y;
for (i = 0; i < num_patches; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
x0 = MIN (x0, patch[i].points[j][k].x);
y0 = MIN (y0, patch[i].points[j][k].y);
x1 = MAX (x1, patch[i].points[j][k].x);
y1 = MAX (y1, patch[i].points[j][k].y);
}
}
}
*out_xmin = x0;
*out_ymin = y0;
*out_xmax = x1;
*out_ymax = y1;
return TRUE;
}
/**
* _cairo_gradient_pattern_is_solid
*
* Convenience function to determine whether a gradient pattern is
* a solid color within the given extents. In this case the color
* argument is initialized to the color the pattern represents.
* This functions doesn't handle completely transparent gradients,
* thus it should be called only after _cairo_pattern_is_clear has
* returned FALSE.
*
* Return value: %TRUE if the pattern is a solid color.
**/
cairo_bool_t
_cairo_gradient_pattern_is_solid (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents,
cairo_color_t *color)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
/* TODO: radial */
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
if (_linear_pattern_is_degenerate (linear)) {
_gradient_color_average (gradient, color);
return TRUE;
}
if (gradient->base.extend == CAIRO_EXTEND_NONE) {
double t[2];
/* We already know that the pattern is not clear, thus if some
* part of it is clear, the whole is not solid.
*/
if (extents == NULL)
return FALSE;
_cairo_linear_pattern_box_to_parameter (linear,
extents->x,
extents->y,
extents->x + extents->width,
extents->y + extents->height,
t);
if (t[0] < 0.0 || t[1] > 1.0)
return FALSE;
}
} else
return FALSE;
for (i = 1; i < gradient->n_stops; i++)
if (! _cairo_color_stop_equal (&gradient->stops[0].color,
&gradient->stops[i].color))
return FALSE;
_cairo_color_init_rgba (color,
gradient->stops[0].color.red,
gradient->stops[0].color.green,
gradient->stops[0].color.blue,
gradient->stops[0].color.alpha);
return TRUE;
}
static cairo_bool_t
_mesh_is_clear (const cairo_mesh_pattern_t *mesh)
{
double x1, y1, x2, y2;
cairo_bool_t is_valid;
is_valid = _cairo_mesh_pattern_coord_box (mesh, &x1, &y1, &x2, &y2);
if (!is_valid)
return TRUE;
if (x2 - x1 < DBL_EPSILON || y2 - y1 < DBL_EPSILON)
return TRUE;
return FALSE;
}
/**
* _cairo_pattern_is_opaque_solid
*
* Convenience function to determine whether a pattern is an opaque
* (alpha==1.0) solid color pattern. This is done by testing whether
* the pattern's alpha value when converted to a byte is 255, so if a
* backend actually supported deep alpha channels this function might
* not do the right thing.
*
* Return value: %TRUE if the pattern is an opaque, solid color.
**/
cairo_bool_t
_cairo_pattern_is_opaque_solid (const cairo_pattern_t *pattern)
{
cairo_solid_pattern_t *solid;
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
return FALSE;
solid = (cairo_solid_pattern_t *) pattern;
return CAIRO_COLOR_IS_OPAQUE (&solid->color);
}
static cairo_bool_t
_surface_is_opaque (const cairo_surface_pattern_t *pattern,
const cairo_rectangle_int_t *r)
{
if (pattern->surface->content & CAIRO_CONTENT_ALPHA)
return FALSE;
if (pattern->base.extend != CAIRO_EXTEND_NONE)
return TRUE;
if (r != NULL) {
cairo_rectangle_int_t extents;
if (! _cairo_surface_get_extents (pattern->surface, &extents))
return TRUE;
if (r->x >= extents.x &&
r->y >= extents.y &&
r->x + r->width <= extents.x + extents.width &&
r->y + r->height <= extents.y + extents.height)
{
return TRUE;
}
}
return FALSE;
}
static cairo_bool_t
_surface_is_clear (const cairo_surface_pattern_t *pattern)
{
cairo_rectangle_int_t extents;
if (_cairo_surface_get_extents (pattern->surface, &extents) &&
(extents.width == 0 || extents.height == 0))
return TRUE;
return pattern->surface->is_clear &&
pattern->surface->content & CAIRO_CONTENT_ALPHA;
}
static cairo_bool_t
_gradient_is_opaque (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->n_stops == 0 ||
(gradient->base.extend == CAIRO_EXTEND_NONE &&
gradient->stops[0].offset == gradient->stops[gradient->n_stops - 1].offset))
return FALSE;
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
if (gradient->base.extend == CAIRO_EXTEND_NONE) {
double t[2];
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
/* EXTEND_NONE degenerate radial gradients are clear */
if (_linear_pattern_is_degenerate (linear))
return FALSE;
if (extents == NULL)
return FALSE;
_cairo_linear_pattern_box_to_parameter (linear,
extents->x,
extents->y,
extents->x + extents->width,
extents->y + extents->height,
t);
if (t[0] < 0.0 || t[1] > 1.0)
return FALSE;
}
} else
return FALSE; /* TODO: check actual intersection */
for (i = 0; i < gradient->n_stops; i++)
if (! CAIRO_COLOR_IS_OPAQUE (&gradient->stops[i].color))
return FALSE;
return TRUE;
}
/**
* _cairo_pattern_is_opaque
*
* Convenience function to determine whether a pattern is an opaque
* pattern (of any type). The same caveats that apply to
* _cairo_pattern_is_opaque_solid apply here as well.
*
* Return value: %TRUE if the pattern is a opaque.
**/
cairo_bool_t
_cairo_pattern_is_opaque (const cairo_pattern_t *abstract_pattern,
const cairo_rectangle_int_t *extents)
{
const cairo_pattern_union_t *pattern;
if (abstract_pattern->has_component_alpha)
return FALSE;
pattern = (cairo_pattern_union_t *) abstract_pattern;
switch (pattern->base.type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_pattern_is_opaque_solid (abstract_pattern);
case CAIRO_PATTERN_TYPE_SURFACE:
return _surface_is_opaque (&pattern->surface, extents);
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
return _gradient_is_opaque (&pattern->gradient.base, extents);
case CAIRO_PATTERN_TYPE_MESH:
return FALSE;
}
ASSERT_NOT_REACHED;
return FALSE;
}
cairo_bool_t
_cairo_pattern_is_clear (const cairo_pattern_t *abstract_pattern)
{
const cairo_pattern_union_t *pattern;
if (abstract_pattern->has_component_alpha)
return FALSE;
pattern = (cairo_pattern_union_t *) abstract_pattern;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return CAIRO_COLOR_IS_CLEAR (&pattern->solid.color);
case CAIRO_PATTERN_TYPE_SURFACE:
return _surface_is_clear (&pattern->surface);
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
return _gradient_is_clear (&pattern->gradient.base, NULL);
case CAIRO_PATTERN_TYPE_MESH:
return _mesh_is_clear (&pattern->mesh);
}
ASSERT_NOT_REACHED;
return FALSE;
}
/**
* _cairo_pattern_analyze_filter:
* @pattern: surface pattern
* @pad_out: location to store necessary padding in the source image, or %NULL
* Returns: the optimized #cairo_filter_t to use with @pattern.
*
* Analyze the filter to determine how much extra needs to be sampled
* from the source image to account for the filter radius and whether
* we can optimize the filter to a simpler value.
*
* XXX: We don't actually have any way of querying the backend for
* the filter radius, so we just guess base on what we know that
* backends do currently (see bug #10508)
*/
cairo_filter_t
_cairo_pattern_analyze_filter (const cairo_pattern_t *pattern,
double *pad_out)
{
double pad;
cairo_filter_t optimized_filter;
switch (pattern->filter) {
case CAIRO_FILTER_GOOD:
case CAIRO_FILTER_BEST:
case CAIRO_FILTER_BILINEAR:
/* If source pixels map 1:1 onto destination pixels, we do
* not need to filter (and do not want to filter, since it
* will cause blurriness)
*/
if (_cairo_matrix_is_pixel_exact (&pattern->matrix)) {
pad = 0.;
optimized_filter = CAIRO_FILTER_NEAREST;
} else {
/* 0.5 is enough for a bilinear filter. It's possible we
* should defensively use more for CAIRO_FILTER_BEST, but
* without a single example, it's hard to know how much
* more would be defensive...
*/
pad = 0.5;
optimized_filter = pattern->filter;
}
break;
case CAIRO_FILTER_FAST:
case CAIRO_FILTER_NEAREST:
case CAIRO_FILTER_GAUSSIAN:
default:
pad = 0.;
optimized_filter = pattern->filter;
break;
}
if (pad_out)
*pad_out = pad;
return optimized_filter;
}
static double
_pixman_nearest_sample (double d)
{
return ceil (d - .5);
}
static cairo_int_status_t
_cairo_pattern_acquire_surface_for_surface (const cairo_surface_pattern_t *pattern,
cairo_surface_t *dst,
int x,
int y,
unsigned int width,
unsigned int height,
unsigned int flags,
cairo_surface_t **out,
cairo_surface_attributes_t *attr)
{
cairo_surface_t *surface;
cairo_rectangle_int_t extents;
cairo_rectangle_int_t sampled_area;
double x1, y1, x2, y2;
int tx, ty;
double pad;
cairo_bool_t is_identity;
cairo_bool_t is_empty;
cairo_bool_t is_bounded;
cairo_int_status_t status;
surface = cairo_surface_reference (pattern->surface);
is_identity = FALSE;
attr->matrix = pattern->base.matrix;
attr->extend = pattern->base.extend;
attr->filter = _cairo_pattern_analyze_filter (&pattern->base, &pad);
attr->has_component_alpha = pattern->base.has_component_alpha;
attr->x_offset = attr->y_offset = tx = ty = 0;
if (_cairo_matrix_is_integer_translation (&attr->matrix, &tx, &ty)) {
cairo_matrix_init_identity (&attr->matrix);
attr->x_offset = tx;
attr->y_offset = ty;
is_identity = TRUE;
} else if (attr->filter == CAIRO_FILTER_NEAREST) {
/*
* For NEAREST, we can remove the fractional translation component
* from the transformation - this ensures that the pattern will always
* hit fast-paths in the backends for simple transformations that
* become (almost) identity, without loss of quality.
*/
attr->matrix.x0 = 0;
attr->matrix.y0 = 0;
if (_cairo_matrix_is_pixel_exact (&attr->matrix)) {
/* The rounding here is rather peculiar as it needs to match the
* rounding performed on the sample coordinate used by pixman.
*/
attr->matrix.x0 = _pixman_nearest_sample (pattern->base.matrix.x0);
attr->matrix.y0 = _pixman_nearest_sample (pattern->base.matrix.y0);
} else {
attr->matrix.x0 = pattern->base.matrix.x0;
attr->matrix.y0 = pattern->base.matrix.y0;
}
if (_cairo_matrix_is_integer_translation (&attr->matrix, &tx, &ty)) {
cairo_matrix_init_identity (&attr->matrix);
attr->x_offset = tx;
attr->y_offset = ty;
is_identity = TRUE;
}
}
/* XXX: Hack:
*
* The way we currently support CAIRO_EXTEND_REFLECT is to create
* an image twice bigger on each side, and create a pattern of four
* images such that the new image, when repeated, has the same effect
* of reflecting the original pattern.
*/
if (flags & CAIRO_PATTERN_ACQUIRE_NO_REFLECT &&
attr->extend == CAIRO_EXTEND_REFLECT)
{
cairo_t *cr;
cairo_surface_t *src;
int w, h;
is_bounded = _cairo_surface_get_extents (surface, &extents);
assert (is_bounded);
status = _cairo_surface_clone_similar (dst, surface,
extents.x, extents.y,
extents.width, extents.height,
&extents.x, &extents.y, &src);
if (unlikely (status))
goto BAIL;
w = 2 * extents.width;
h = 2 * extents.height;
if (is_identity) {
attr->x_offset = -x;
x += tx;
while (x <= -w)
x += w;
while (x >= w)
x -= w;
extents.x += x;
tx = x = 0;
attr->y_offset = -y;
y += ty;
while (y <= -h)
y += h;
while (y >= h)
y -= h;
extents.y += y;
ty = y = 0;
}
cairo_surface_destroy (surface);
surface = _cairo_surface_create_similar_solid (dst,
dst->content, w, h,
CAIRO_COLOR_TRANSPARENT,
FALSE);
if (surface == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
if (unlikely (surface->status)) {
cairo_surface_destroy (src);
return surface->status;
}
surface->device_transform = pattern->surface->device_transform;
surface->device_transform_inverse = pattern->surface->device_transform_inverse;
cr = cairo_create (surface);
cairo_set_source_surface (cr, src, -extents.x, -extents.y);
cairo_paint (cr);
cairo_scale (cr, -1, +1);
cairo_set_source_surface (cr, src, extents.x-w, -extents.y);
cairo_paint (cr);
cairo_set_source_surface (cr, src, extents.x, -extents.y);
cairo_paint (cr);
cairo_scale (cr, +1, -1);
cairo_set_source_surface (cr, src, extents.x-w, extents.y-h);
cairo_paint (cr);
cairo_set_source_surface (cr, src, extents.x, extents.y-h);
cairo_paint (cr);
cairo_set_source_surface (cr, src, extents.x-w, extents.y);
cairo_paint (cr);
cairo_set_source_surface (cr, src, extents.x, extents.y);
cairo_paint (cr);
cairo_scale (cr, -1, +1);
cairo_set_source_surface (cr, src, -extents.x, extents.y-h);
cairo_paint (cr);
cairo_set_source_surface (cr, src, -extents.x, extents.y);
cairo_paint (cr);
status = cairo_status (cr);
cairo_destroy (cr);
cairo_surface_destroy (src);
if (unlikely (status))
goto BAIL;
attr->extend = CAIRO_EXTEND_REPEAT;
}
/* We first transform the rectangle to the coordinate space of the
* source surface so that we only need to clone that portion of the
* surface that will be read.
*/
x1 = x;
y1 = y;
x2 = x + (int) width;
y2 = y + (int) height;
if (! is_identity) {
_cairo_matrix_transform_bounding_box (&attr->matrix,
&x1, &y1, &x2, &y2,
NULL);
}
sampled_area.x = floor (x1 - pad);
sampled_area.y = floor (y1 - pad);
sampled_area.width = ceil (x2 + pad) - sampled_area.x;
sampled_area.height = ceil (y2 + pad) - sampled_area.y;
sampled_area.x += tx;
sampled_area.y += ty;
if ( _cairo_surface_get_extents (surface, &extents)) {
if (attr->extend == CAIRO_EXTEND_NONE) {
/* Never acquire a larger area than the source itself */
is_empty = _cairo_rectangle_intersect (&extents, &sampled_area);
} else {
int trim = 0;
if (sampled_area.x >= extents.x &&
sampled_area.x + (int) sampled_area.width <= extents.x + (int) extents.width)
{
/* source is horizontally contained within extents, trim */
extents.x = sampled_area.x;
extents.width = sampled_area.width;
trim |= 0x1;
}
if (sampled_area.y >= extents.y &&
sampled_area.y + (int) sampled_area.height <= extents.y + (int) extents.height)
{
/* source is vertically contained within extents, trim */
extents.y = sampled_area.y;
extents.height = sampled_area.height;
trim |= 0x2;
}
if (trim == 0x3) {
/* source is wholly contained within extents, drop the REPEAT */
attr->extend = CAIRO_EXTEND_NONE;
}
is_empty = extents.width == 0 || extents.height == 0;
}
}
/* XXX can we use is_empty? */
status = _cairo_surface_clone_similar (dst, surface,
extents.x, extents.y,
extents.width, extents.height,
&x, &y, out);
if (unlikely (status))
goto BAIL;
if (x != 0 || y != 0) {
if (is_identity) {
attr->x_offset -= x;
attr->y_offset -= y;
} else {
cairo_matrix_t m;
x -= attr->x_offset;
y -= attr->y_offset;
attr->x_offset = 0;
attr->y_offset = 0;
cairo_matrix_init_translate (&m, -x, -y);
cairo_matrix_multiply (&attr->matrix, &attr->matrix, &m);
}
}
/* reduce likelihood of range overflow with large downscaling */
if (! is_identity) {
cairo_matrix_t m;
cairo_status_t invert_status;
m = attr->matrix;
invert_status = cairo_matrix_invert (&m);
assert (invert_status == CAIRO_STATUS_SUCCESS);
if (m.x0 != 0. || m.y0 != 0.) {
/* pixman also limits the [xy]_offset to 16 bits so evenly
* spread the bits between the two.
*/
x = floor (m.x0 / 2);
y = floor (m.y0 / 2);
attr->x_offset -= x;
attr->y_offset -= y;
cairo_matrix_init_translate (&m, x, y);
cairo_matrix_multiply (&attr->matrix, &m, &attr->matrix);
}
}
BAIL:
cairo_surface_destroy (surface);
return status;
}
/**
* _cairo_pattern_acquire_surface:
* @pattern: a #cairo_pattern_t
* @dst: destination surface
* @x: X coordinate in source corresponding to left side of destination area
* @y: Y coordinate in source corresponding to top side of destination area
* @width: width of destination area
* @height: height of destination area
* @surface_out: location to store a pointer to a surface
* @attributes: surface attributes that destination backend should apply to
* the returned surface
*
* A convenience function to obtain a surface to use as the source for
* drawing on @dst.
*
* Note that this function is only suitable for use when the destination
* surface is pixel based and 1 device unit maps to one pixel.
*
* Return value: %CAIRO_STATUS_SUCCESS if a surface was stored in @surface_out.
**/
cairo_int_status_t
_cairo_pattern_acquire_surface (const cairo_pattern_t *pattern,
cairo_surface_t *dst,
int x,
int y,
unsigned int width,
unsigned int height,
unsigned int flags,
cairo_surface_t **surface_out,
cairo_surface_attributes_t *attributes)
{
if (unlikely (pattern->status)) {
*surface_out = NULL;
return pattern->status;
}
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_pattern_acquire_surface_for_solid ((cairo_solid_pattern_t *) pattern,
dst, x, y, width, height,
surface_out,
attributes);
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
return _cairo_pattern_acquire_surface_for_gradient ((cairo_gradient_pattern_t *) pattern,
dst, x, y, width, height,
surface_out,
attributes);
case CAIRO_PATTERN_TYPE_SURFACE:
return _cairo_pattern_acquire_surface_for_surface ((cairo_surface_pattern_t *) pattern,
dst, x, y, width, height,
flags,
surface_out,
attributes);
case CAIRO_PATTERN_TYPE_MESH:
return _cairo_pattern_acquire_surface_for_mesh ((cairo_mesh_pattern_t *) pattern,
dst, x, y, width, height,
surface_out,
attributes);
default:
ASSERT_NOT_REACHED;
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
}
}
/**
* _cairo_pattern_release_surface:
* @pattern: a #cairo_pattern_t
* @surface: a surface obtained by _cairo_pattern_acquire_surface
* @attributes: attributes obtained by _cairo_pattern_acquire_surface
*
* Releases resources obtained by _cairo_pattern_acquire_surface.
**/
void
_cairo_pattern_release_surface (const cairo_pattern_t *pattern,
cairo_surface_t *surface,
cairo_surface_attributes_t *attributes)
{
cairo_surface_destroy (surface);
}
cairo_int_status_t
_cairo_pattern_acquire_surfaces (const cairo_pattern_t *src,
const cairo_pattern_t *mask,
cairo_surface_t *dst,
int src_x,
int src_y,
int mask_x,
int mask_y,
unsigned int width,
unsigned int height,
unsigned int flags,
cairo_surface_t **src_out,
cairo_surface_t **mask_out,
cairo_surface_attributes_t *src_attributes,
cairo_surface_attributes_t *mask_attributes)
{
cairo_int_status_t status;
cairo_pattern_union_t src_tmp;
if (unlikely (src->status))
return src->status;
if (unlikely (mask != NULL && mask->status))
return mask->status;
/* If src and mask are both solid, then the mask alpha can be
* combined into src and mask can be ignored. */
if (src->type == CAIRO_PATTERN_TYPE_SOLID &&
mask &&
! mask->has_component_alpha &&
mask->type == CAIRO_PATTERN_TYPE_SOLID)
{
cairo_color_t combined;
cairo_solid_pattern_t *src_solid = (cairo_solid_pattern_t *) src;
cairo_solid_pattern_t *mask_solid = (cairo_solid_pattern_t *) mask;
combined = src_solid->color;
_cairo_color_multiply_alpha (&combined, mask_solid->color.alpha);
_cairo_pattern_init_solid (&src_tmp.solid, &combined);
src = &src_tmp.base;
mask = NULL;
}
status = _cairo_pattern_acquire_surface (src, dst,
src_x, src_y,
width, height,
flags,
src_out, src_attributes);
if (unlikely (status))
goto BAIL;
if (mask == NULL) {
*mask_out = NULL;
goto BAIL;
}
status = _cairo_pattern_acquire_surface (mask, dst,
mask_x, mask_y,
width, height,
flags,
mask_out, mask_attributes);
if (unlikely (status))
_cairo_pattern_release_surface (src, *src_out, src_attributes);
BAIL:
if (src == &src_tmp.base)
_cairo_pattern_fini (&src_tmp.base);
return status;
}
/**
* _cairo_pattern_get_extents:
*
* Return the "target-space" extents of @pattern in @extents.
*
* For unbounded patterns, the @extents will be initialized with
* "infinite" extents, (minimum and maximum fixed-point values).
*
* XXX: Currently, bounded gradient patterns will also return
* "infinite" extents, though it would be possible to optimize these
* with a little more work.
**/
void
_cairo_pattern_get_extents (const cairo_pattern_t *pattern,
cairo_rectangle_int_t *extents)
{
double x1, y1, x2, y2;
cairo_status_t status;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
goto UNBOUNDED;
case CAIRO_PATTERN_TYPE_SURFACE:
{
cairo_rectangle_int_t surface_extents;
const cairo_surface_pattern_t *surface_pattern =
(const cairo_surface_pattern_t *) pattern;
cairo_surface_t *surface = surface_pattern->surface;
double pad;
if (! _cairo_surface_get_extents (surface, &surface_extents))
goto UNBOUNDED;
if (surface_extents.width == 0 || surface_extents.height == 0)
goto EMPTY;
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
/* The filter can effectively enlarge the extents of the
* pattern, so extend as necessary.
*/
_cairo_pattern_analyze_filter (&surface_pattern->base, &pad);
x1 = surface_extents.x - pad;
y1 = surface_extents.y - pad;
x2 = surface_extents.x + (int) surface_extents.width + pad;
y2 = surface_extents.y + (int) surface_extents.height + pad;
}
break;
case CAIRO_PATTERN_TYPE_RADIAL:
{
const cairo_radial_pattern_t *radial =
(const cairo_radial_pattern_t *) pattern;
double cx1, cy1;
double cx2, cy2;
double r1, r2;
if (_radial_pattern_is_degenerate (radial)) {
/* cairo-gstate should have optimised degenerate
* patterns to solid clear patterns, so we can ignore
* them here. */
goto EMPTY;
}
/* TODO: in some cases (focus outside/on the circle) it is
* half-bounded. */
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
cx1 = radial->cd1.center.x;
cy1 = radial->cd1.center.y;
r1 = radial->cd1.radius;
cx2 = radial->cd2.center.x;
cy2 = radial->cd2.center.y;
r2 = radial->cd2.radius;
x1 = MIN (cx1 - r1, cx2 - r2);
y1 = MIN (cy1 - r1, cy2 - r2);
x2 = MAX (cx1 + r1, cx2 + r2);
y2 = MAX (cy1 + r1, cy2 + r2);
}
break;
case CAIRO_PATTERN_TYPE_LINEAR:
{
const cairo_linear_pattern_t *linear =
(const cairo_linear_pattern_t *) pattern;
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
if (_linear_pattern_is_degenerate (linear)) {
/* cairo-gstate should have optimised degenerate
* patterns to solid ones, so we can again ignore
* them here. */
goto EMPTY;
}
/* TODO: to get tight extents, use the matrix to transform
* the pattern instead of transforming the extents later. */
if (pattern->matrix.xy != 0. || pattern->matrix.yx != 0.)
goto UNBOUNDED;
if (linear->pd1.x == linear->pd2.x) {
x1 = -HUGE_VAL;
x2 = HUGE_VAL;
y1 = MIN (linear->pd1.y, linear->pd2.y);
y2 = MAX (linear->pd1.y, linear->pd2.y);
} else if (linear->pd1.y == linear->pd2.y) {
x1 = MIN (linear->pd1.x, linear->pd2.x);
x2 = MAX (linear->pd1.x, linear->pd2.x);
y1 = -HUGE_VAL;
y2 = HUGE_VAL;
} else {
goto UNBOUNDED;
}
}
break;
case CAIRO_PATTERN_TYPE_MESH:
{
const cairo_mesh_pattern_t *mesh =
(const cairo_mesh_pattern_t *) pattern;
double padx, pady;
cairo_bool_t is_valid;
is_valid = _cairo_mesh_pattern_coord_box (mesh, &x1, &y1, &x2, &y2);
if (!is_valid)
goto EMPTY;
padx = pady = 1.;
cairo_matrix_transform_distance (&pattern->matrix, &padx, &pady);
padx = fabs (padx);
pady = fabs (pady);
x1 -= padx;
y1 -= pady;
x2 += padx;
y2 += pady;
}
break;
default:
ASSERT_NOT_REACHED;
}
if (_cairo_matrix_is_translation (&pattern->matrix)) {
x1 -= pattern->matrix.x0; x2 -= pattern->matrix.x0;
y1 -= pattern->matrix.y0; y2 -= pattern->matrix.y0;
} else {
cairo_matrix_t imatrix;
imatrix = pattern->matrix;
status = cairo_matrix_invert (&imatrix);
/* cairo_pattern_set_matrix ensures the matrix is invertible */
assert (status == CAIRO_STATUS_SUCCESS);
_cairo_matrix_transform_bounding_box (&imatrix,
&x1, &y1, &x2, &y2,
NULL);
}
x1 = floor (x1);
if (x1 < CAIRO_RECT_INT_MIN)
x1 = CAIRO_RECT_INT_MIN;
y1 = floor (y1);
if (y1 < CAIRO_RECT_INT_MIN)
y1 = CAIRO_RECT_INT_MIN;
x2 = ceil (x2);
if (x2 > CAIRO_RECT_INT_MAX)
x2 = CAIRO_RECT_INT_MAX;
y2 = ceil (y2);
if (y2 > CAIRO_RECT_INT_MAX)
y2 = CAIRO_RECT_INT_MAX;
extents->x = x1; extents->width = x2 - x1;
extents->y = y1; extents->height = y2 - y1;
return;
UNBOUNDED:
/* unbounded patterns -> 'infinite' extents */
_cairo_unbounded_rectangle_init (extents);
return;
EMPTY:
extents->x = extents->y = 0;
extents->width = extents->height = 0;
return;
}
static unsigned long
_cairo_solid_pattern_hash (unsigned long hash,
const cairo_solid_pattern_t *solid)
{
hash = _cairo_hash_bytes (hash, &solid->color, sizeof (solid->color));
return hash;
}
static unsigned long
_cairo_gradient_color_stops_hash (unsigned long hash,
const cairo_gradient_pattern_t *gradient)
{
unsigned int n;
hash = _cairo_hash_bytes (hash,
&gradient->n_stops,
sizeof (gradient->n_stops));
for (n = 0; n < gradient->n_stops; n++) {
hash = _cairo_hash_bytes (hash,
&gradient->stops[n].offset,
sizeof (double));
hash = _cairo_hash_bytes (hash,
&gradient->stops[n].color,
sizeof (cairo_color_stop_t));
}
return hash;
}
unsigned long
_cairo_linear_pattern_hash (unsigned long hash,
const cairo_linear_pattern_t *linear)
{
hash = _cairo_hash_bytes (hash, &linear->pd1, sizeof (linear->pd1));
hash = _cairo_hash_bytes (hash, &linear->pd2, sizeof (linear->pd2));
return _cairo_gradient_color_stops_hash (hash, &linear->base);
}
unsigned long
_cairo_radial_pattern_hash (unsigned long hash,
const cairo_radial_pattern_t *radial)
{
hash = _cairo_hash_bytes (hash, &radial->cd1.center, sizeof (radial->cd1.center));
hash = _cairo_hash_bytes (hash, &radial->cd1.radius, sizeof (radial->cd1.radius));
hash = _cairo_hash_bytes (hash, &radial->cd2.center, sizeof (radial->cd2.center));
hash = _cairo_hash_bytes (hash, &radial->cd2.radius, sizeof (radial->cd2.radius));
return _cairo_gradient_color_stops_hash (hash, &radial->base);
}
static unsigned long
_cairo_mesh_pattern_hash (unsigned long hash, const cairo_mesh_pattern_t *mesh)
{
const cairo_mesh_patch_t *patch = _cairo_array_index_const (&mesh->patches, 0);
unsigned int i, n = _cairo_array_num_elements (&mesh->patches);
for (i = 0; i < n; i++)
hash = _cairo_hash_bytes (hash, patch + i, sizeof (cairo_mesh_patch_t));
return hash;
}
static unsigned long
_cairo_surface_pattern_hash (unsigned long hash,
const cairo_surface_pattern_t *surface)
{
hash ^= surface->surface->unique_id;
return hash;
}
unsigned long
_cairo_pattern_hash (const cairo_pattern_t *pattern)
{
unsigned long hash = _CAIRO_HASH_INIT_VALUE;
if (pattern->status)
return 0;
hash = _cairo_hash_bytes (hash, &pattern->type, sizeof (pattern->type));
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID) {
hash = _cairo_hash_bytes (hash,
&pattern->matrix, sizeof (pattern->matrix));
hash = _cairo_hash_bytes (hash,
&pattern->filter, sizeof (pattern->filter));
hash = _cairo_hash_bytes (hash,
&pattern->extend, sizeof (pattern->extend));
hash = _cairo_hash_bytes (hash,
&pattern->has_component_alpha,
sizeof (pattern->has_component_alpha));
}
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_solid_pattern_hash (hash, (cairo_solid_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_LINEAR:
return _cairo_linear_pattern_hash (hash, (cairo_linear_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_RADIAL:
return _cairo_radial_pattern_hash (hash, (cairo_radial_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_MESH:
return _cairo_mesh_pattern_hash (hash, (cairo_mesh_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_SURFACE:
return _cairo_surface_pattern_hash (hash, (cairo_surface_pattern_t *) pattern);
default:
ASSERT_NOT_REACHED;
return FALSE;
}
}
static cairo_bool_t
_cairo_solid_pattern_equal (const cairo_solid_pattern_t *a,
const cairo_solid_pattern_t *b)
{
return _cairo_color_equal (&a->color, &b->color);
}
static cairo_bool_t
_cairo_gradient_color_stops_equal (const cairo_gradient_pattern_t *a,
const cairo_gradient_pattern_t *b)
{
unsigned int n;
if (a->n_stops != b->n_stops)
return FALSE;
for (n = 0; n < a->n_stops; n++) {
if (a->stops[n].offset != b->stops[n].offset)
return FALSE;
if (! _cairo_color_stop_equal (&a->stops[n].color, &b->stops[n].color))
return FALSE;
}
return TRUE;
}
cairo_bool_t
_cairo_linear_pattern_equal (const cairo_linear_pattern_t *a,
const cairo_linear_pattern_t *b)
{
if (a->pd1.x != b->pd1.x)
return FALSE;
if (a->pd1.y != b->pd1.y)
return FALSE;
if (a->pd2.x != b->pd2.x)
return FALSE;
if (a->pd2.y != b->pd2.y)
return FALSE;
return _cairo_gradient_color_stops_equal (&a->base, &b->base);
}
cairo_bool_t
_cairo_radial_pattern_equal (const cairo_radial_pattern_t *a,
const cairo_radial_pattern_t *b)
{
if (a->cd1.center.x != b->cd1.center.x)
return FALSE;
if (a->cd1.center.y != b->cd1.center.y)
return FALSE;
if (a->cd1.radius != b->cd1.radius)
return FALSE;
if (a->cd2.center.x != b->cd2.center.x)
return FALSE;
if (a->cd2.center.y != b->cd2.center.y)
return FALSE;
if (a->cd2.radius != b->cd2.radius)
return FALSE;
return _cairo_gradient_color_stops_equal (&a->base, &b->base);
}
static cairo_bool_t
_cairo_mesh_pattern_equal (const cairo_mesh_pattern_t *a,
const cairo_mesh_pattern_t *b)
{
const cairo_mesh_patch_t *patch_a, *patch_b;
unsigned int i, num_patches_a, num_patches_b;
num_patches_a = _cairo_array_num_elements (&a->patches);
num_patches_b = _cairo_array_num_elements (&b->patches);
if (num_patches_a != num_patches_b)
return FALSE;
for (i = 0; i < num_patches_a; i++) {
patch_a = _cairo_array_index_const (&a->patches, i);
patch_b = _cairo_array_index_const (&a->patches, i);
if (memcmp (patch_a, patch_b, sizeof(cairo_mesh_patch_t)) != 0)
return FALSE;
}
return TRUE;
}
static cairo_bool_t
_cairo_surface_pattern_equal (const cairo_surface_pattern_t *a,
const cairo_surface_pattern_t *b)
{
return a->surface->unique_id == b->surface->unique_id;
}
cairo_bool_t
_cairo_pattern_equal (const cairo_pattern_t *a, const cairo_pattern_t *b)
{
if (a->status || b->status)
return FALSE;
if (a == b)
return TRUE;
if (a->type != b->type)
return FALSE;
if (a->has_component_alpha != b->has_component_alpha)
return FALSE;
if (a->type != CAIRO_PATTERN_TYPE_SOLID) {
if (memcmp (&a->matrix, &b->matrix, sizeof (cairo_matrix_t)))
return FALSE;
if (a->filter != b->filter)
return FALSE;
if (a->extend != b->extend)
return FALSE;
}
switch (a->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_solid_pattern_equal ((cairo_solid_pattern_t *) a,
(cairo_solid_pattern_t *) b);
case CAIRO_PATTERN_TYPE_LINEAR:
return _cairo_linear_pattern_equal ((cairo_linear_pattern_t *) a,
(cairo_linear_pattern_t *) b);
case CAIRO_PATTERN_TYPE_RADIAL:
return _cairo_radial_pattern_equal ((cairo_radial_pattern_t *) a,
(cairo_radial_pattern_t *) b);
case CAIRO_PATTERN_TYPE_MESH:
return _cairo_mesh_pattern_equal ((cairo_mesh_pattern_t *) a,
(cairo_mesh_pattern_t *) b);
case CAIRO_PATTERN_TYPE_SURFACE:
return _cairo_surface_pattern_equal ((cairo_surface_pattern_t *) a,
(cairo_surface_pattern_t *) b);
default:
ASSERT_NOT_REACHED;
return FALSE;
}
}
/**
* cairo_pattern_get_rgba
* @pattern: a #cairo_pattern_t
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the solid color for a solid color pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a solid
* color pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_rgba (cairo_pattern_t *pattern,
double *red, double *green,
double *blue, double *alpha)
{
cairo_solid_pattern_t *solid = (cairo_solid_pattern_t*) pattern;
double r0, g0, b0, a0;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
_cairo_color_get_rgba (&solid->color, &r0, &g0, &b0, &a0);
if (red)
*red = r0;
if (green)
*green = g0;
if (blue)
*blue = b0;
if (alpha)
*alpha = a0;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_surface
* @pattern: a #cairo_pattern_t
* @surface: return value for surface of pattern, or %NULL
*
* Gets the surface of a surface pattern. The reference returned in
* @surface is owned by the pattern; the caller should call
* cairo_surface_reference() if the surface is to be retained.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a surface
* pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_surface (cairo_pattern_t *pattern,
cairo_surface_t **surface)
{
cairo_surface_pattern_t *spat = (cairo_surface_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (surface)
*surface = spat->surface;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_color_stop_rgba
* @pattern: a #cairo_pattern_t
* @index: index of the stop to return data for
* @offset: return value for the offset of the stop, or %NULL
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the color and offset information at the given @index for a
* gradient pattern. Values of @index are 0 to 1 less than the number
* returned by cairo_pattern_get_color_stop_count().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @index is not valid for the given pattern. If the pattern is
* not a gradient pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is
* returned.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_color_stop_rgba (cairo_pattern_t *pattern,
int index, double *offset,
double *red, double *green,
double *blue, double *alpha)
{
cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (index < 0 || (unsigned int) index >= gradient->n_stops)
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
if (offset)
*offset = gradient->stops[index].offset;
if (red)
*red = gradient->stops[index].color.red;
if (green)
*green = gradient->stops[index].color.green;
if (blue)
*blue = gradient->stops[index].color.blue;
if (alpha)
*alpha = gradient->stops[index].color.alpha;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_color_stop_count
* @pattern: a #cairo_pattern_t
* @count: return value for the number of color stops, or %NULL
*
* Gets the number of color stops specified in the given gradient
* pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a gradient
* pattern.
*
* Since: 1.4
*/
cairo_status_t
cairo_pattern_get_color_stop_count (cairo_pattern_t *pattern,
int *count)
{
cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (count)
*count = gradient->n_stops;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_linear_points
* @pattern: a #cairo_pattern_t
* @x0: return value for the x coordinate of the first point, or %NULL
* @y0: return value for the y coordinate of the first point, or %NULL
* @x1: return value for the x coordinate of the second point, or %NULL
* @y1: return value for the y coordinate of the second point, or %NULL
*
* Gets the gradient endpoints for a linear gradient.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a linear
* gradient pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_linear_points (cairo_pattern_t *pattern,
double *x0, double *y0,
double *x1, double *y1)
{
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (x0)
*x0 = linear->pd1.x;
if (y0)
*y0 = linear->pd1.y;
if (x1)
*x1 = linear->pd2.x;
if (y1)
*y1 = linear->pd2.y;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_radial_circles
* @pattern: a #cairo_pattern_t
* @x0: return value for the x coordinate of the center of the first circle, or %NULL
* @y0: return value for the y coordinate of the center of the first circle, or %NULL
* @r0: return value for the radius of the first circle, or %NULL
* @x1: return value for the x coordinate of the center of the second circle, or %NULL
* @y1: return value for the y coordinate of the center of the second circle, or %NULL
* @r1: return value for the radius of the second circle, or %NULL
*
* Gets the gradient endpoint circles for a radial gradient, each
* specified as a center coordinate and a radius.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a radial
* gradient pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_radial_circles (cairo_pattern_t *pattern,
double *x0, double *y0, double *r0,
double *x1, double *y1, double *r1)
{
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (x0)
*x0 = radial->cd1.center.x;
if (y0)
*y0 = radial->cd1.center.y;
if (r0)
*r0 = radial->cd1.radius;
if (x1)
*x1 = radial->cd2.center.x;
if (y1)
*y1 = radial->cd2.center.y;
if (r1)
*r1 = radial->cd2.radius;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_mesh_get_patch_count
* @pattern: a #cairo_pattern_t
* @count: return value for the number patches, or %NULL
*
* Gets the number of patches specified in the given mesh pattern.
*
* The number only includes patches which have been finished by
* calling cairo_pattern_mesh_end_patch(). For example it will be 0
* during the definition of the first patch.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a mesh
* pattern.
*
* Since: 1.12
*/
cairo_status_t
cairo_pattern_mesh_get_patch_count (cairo_pattern_t *pattern,
unsigned int *count)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (pattern->status))
return pattern->status;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (count) {
*count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
*count -= 1;
}
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_mesh_get_path
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
*
* Gets path defining the patch @patch_num for a mesh
* pattern.
*
* @patch_num can range 0 to 1 less than the number returned by
* cairo_pattern_mesh_get_patch_count().
*
* Return value: the path defining the patch, or a path with status
* %CAIRO_STATUS_INVALID_INDEX if @patch_num or @point_num is not
* valid for @pattern. If @pattern is not a mesh pattern, a path with
* status %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is returned.
*
* Since: 1.12
*/
cairo_path_t *
cairo_pattern_mesh_get_path (cairo_pattern_t *pattern,
unsigned int patch_num)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch;
cairo_path_t *path;
cairo_path_data_t *data;
unsigned int patch_count;
int l, current_point;
if (unlikely (pattern->status))
return _cairo_path_create_in_error (pattern->status);
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH));
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_INDEX));
patch = _cairo_array_index_const (&mesh->patches, patch_num);
path = malloc (sizeof (cairo_path_t));
if (path == NULL)
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
path->num_data = 18;
path->data = _cairo_malloc_ab (path->num_data,
sizeof (cairo_path_data_t));
if (path->data == NULL) {
free (path);
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
data = path->data;
data[0].header.type = CAIRO_PATH_MOVE_TO;
data[0].header.length = 2;
data[1].point.x = patch->points[0][0].x;
data[1].point.y = patch->points[0][0].y;
data += data[0].header.length;
current_point = 0;
for (l = 0; l < 4; l++) {
int i, j, k;
data[0].header.type = CAIRO_PATH_CURVE_TO;
data[0].header.length = 4;
for (k = 1; k < 4; k++) {
current_point = (current_point + 1) % 12;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
data[k].point.x = patch->points[i][j].x;
data[k].point.y = patch->points[i][j].y;
}
data += data[0].header.length;
}
path->status = CAIRO_STATUS_SUCCESS;
return path;
}
/**
* cairo_pattern_mesh_get_corner_color_rgba
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
* @corner_num: the corner number to return data for
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the color information in corner @corner_num of patch
* @patch_num for a mesh pattern.
*
* @patch_num can range 0 to 1 less than the number returned by
* cairo_pattern_mesh_get_patch_count().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @patch_num or @corner_num is not valid for @pattern. If
* @pattern is not a mesh pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH
* is returned.
*
* Since: 1.12
**/
cairo_status_t
cairo_pattern_mesh_get_corner_color_rgba (cairo_pattern_t *pattern,
unsigned int patch_num,
unsigned int corner_num,
double *red, double *green,
double *blue, double *alpha)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
unsigned int patch_count;
const cairo_mesh_patch_t *patch;
if (unlikely (pattern->status))
return pattern->status;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (unlikely (corner_num > 3))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch = _cairo_array_index_const (&mesh->patches, patch_num);
if (red)
*red = patch->colors[corner_num].red;
if (green)
*green = patch->colors[corner_num].green;
if (blue)
*blue = patch->colors[corner_num].blue;
if (alpha)
*alpha = patch->colors[corner_num].alpha;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_mesh_get_control_point
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
* @point_num: the control point number to return data for
* @x: return value for the x coordinate of the control point, or %NULL
* @y: return value for the y coordinate of the control point, or %NULL
*
* Gets the control point @point_num of patch @patch_num for a mesh
* pattern.
*
* @patch_num can range 0 to 1 less than the number returned by
* cairo_pattern_mesh_get_patch_count().
*
* Valid values for @point_num are from 0 to 3 and identify the
* control points as explained in cairo_pattern_create_mesh().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @patch_num or @point_num is not valid for @pattern. If @pattern
* is not a mesh pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is
* returned.
*
* Since: 1.12
**/
cairo_status_t
cairo_pattern_mesh_get_control_point (cairo_pattern_t *pattern,
unsigned int patch_num,
unsigned int point_num,
double *x, double *y)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch;
unsigned int patch_count;
int i, j;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_MESH)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (point_num > 3)
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch = _cairo_array_index_const (&mesh->patches, patch_num);
i = mesh_control_point_i[point_num];
j = mesh_control_point_j[point_num];
if (x)
*x = patch->points[i][j].x;
if (y)
*y = patch->points[i][j].y;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_reset_static_data (void)
{
#if HAS_FREED_POOL
int i;
for (i = 0; i < ARRAY_LENGTH (freed_pattern_pool); i++)
_freed_pool_reset (&freed_pattern_pool[i]);
#endif
_cairo_pattern_reset_solid_surface_cache ();
}
|