summaryrefslogtreecommitdiffstats
path: root/gfx/thebes/COLRFonts.cpp
blob: 4a451f22d31c7a688241524d202f11347a77f2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "COLRFonts.h"
#include "gfxFontUtils.h"
#include "gfxUtils.h"
#include "harfbuzz/hb.h"
#include "harfbuzz/hb-ot.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPrefs_gfx.h"
#include "TextDrawTarget.h"

#include <limits>

using namespace mozilla;
using namespace mozilla::gfx;

namespace {  // anonymous namespace for implementation internals

#pragma pack(1)  // ensure no padding is added to the COLR structs

// Alias bigendian-reading types from gfxFontUtils to names used in the spec.
using int16 = AutoSwap_PRInt16;
using uint16 = AutoSwap_PRUint16;
using int32 = AutoSwap_PRInt32;
using uint32 = AutoSwap_PRUint32;
using FWORD = AutoSwap_PRInt16;
using UFWORD = AutoSwap_PRUint16;
using Offset16 = AutoSwap_PRUint16;
using Offset24 = AutoSwap_PRUint24;
using Offset32 = AutoSwap_PRUint32;

struct COLRv1Header;
struct ClipList;
struct LayerRecord;
struct BaseGlyphRecord;
struct DeltaSetIndexMap;
struct ItemVariationStore;

struct COLRHeader {
  uint16 version;
  uint16 numBaseGlyphRecords;
  Offset32 baseGlyphRecordsOffset;
  Offset32 layerRecordsOffset;
  uint16 numLayerRecords;

  const BaseGlyphRecord* GetBaseGlyphRecords() const {
    return reinterpret_cast<const BaseGlyphRecord*>(
        reinterpret_cast<const char*>(this) + baseGlyphRecordsOffset);
  }

  const LayerRecord* GetLayerRecords() const {
    return reinterpret_cast<const LayerRecord*>(
        reinterpret_cast<const char*>(this) + layerRecordsOffset);
  }

  bool Validate(uint64_t aLength) const;
};

struct BaseGlyphPaintRecord {
  uint16 glyphID;
  Offset32 paintOffset;
};

struct BaseGlyphList {
  uint32 numBaseGlyphPaintRecords;
  // BaseGlyphPaintRecord baseGlyphPaintRecords[numBaseGlyphPaintRecords];
  const BaseGlyphPaintRecord* baseGlyphPaintRecords() const {
    return reinterpret_cast<const BaseGlyphPaintRecord*>(this + 1);
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

struct LayerList {
  uint32 numLayers;
  // uint32 paintOffsets[numLayers];
  const uint32* paintOffsets() const {
    return reinterpret_cast<const uint32*>(this + 1);
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

struct COLRv1Header {
  COLRHeader base;
  Offset32 baseGlyphListOffset;
  Offset32 layerListOffset;
  Offset32 clipListOffset;
  Offset32 varIndexMapOffset;
  Offset32 itemVariationStoreOffset;

  bool Validate(uint64_t aLength) const;

  const BaseGlyphList* baseGlyphList() const {
    uint32_t offset = baseGlyphListOffset;
    if (!offset) {
      return nullptr;
    }
    const char* ptr = reinterpret_cast<const char*>(this) + offset;
    return reinterpret_cast<const struct BaseGlyphList*>(ptr);
  }

  const LayerList* layerList() const {
    uint32_t offset = layerListOffset;
    if (!offset) {
      return nullptr;
    }
    const char* ptr = reinterpret_cast<const char*>(this) + offset;
    return reinterpret_cast<const LayerList*>(ptr);
  }

  const struct ClipList* clipList() const {
    uint32_t offset = clipListOffset;
    if (!offset) {
      return nullptr;
    }
    const char* ptr = reinterpret_cast<const char*>(this) + offset;
    return reinterpret_cast<const ClipList*>(ptr);
  }

  const struct DeltaSetIndexMap* varIndexMap() const {
    uint32_t offset = varIndexMapOffset;
    if (!offset) {
      return nullptr;
    }
    const char* ptr = reinterpret_cast<const char*>(this) + offset;
    return reinterpret_cast<const DeltaSetIndexMap*>(ptr);
  }

  const struct ItemVariationStore* itemVariationStore() const {
    uint32_t offset = itemVariationStoreOffset;
    if (!offset) {
      return nullptr;
    }
    const char* ptr = reinterpret_cast<const char*>(this) + offset;
    return reinterpret_cast<const ItemVariationStore*>(ptr);
  }

  const BaseGlyphPaintRecord* GetBaseGlyphPaint(uint32_t aGlyphId) const;
};

struct PaintState {
  union {
    const COLRHeader* v0;
    const COLRv1Header* v1;
  } mHeader;
  const sRGBColor* mPalette;
  DrawTarget* mDrawTarget;
  ScaledFont* mScaledFont;
  const int* mCoords;
  DrawOptions mDrawOptions;
  uint32_t mCOLRLength;
  sRGBColor mCurrentColor;
  float mFontUnitsToPixels;
  uint16_t mNumColors;
  uint16_t mCoordCount;
  nsTArray<uint32_t>* mVisited;

  const char* COLRv1BaseAddr() const {
    return reinterpret_cast<const char*>(mHeader.v1);
  }

  DeviceColor GetColor(uint16_t aPaletteIndex, float aAlpha) const;

  // Convert from FUnits (either integer or Fixed 16.16) to device pixels.
  template <typename T>
  float F2P(T aPixels) const {
    return mFontUnitsToPixels * float(aPixels);
  }
};

DeviceColor PaintState::GetColor(uint16_t aPaletteIndex, float aAlpha) const {
  sRGBColor color;
  if (aPaletteIndex < mNumColors) {
    color = mPalette[uint16_t(aPaletteIndex)];
  } else if (aPaletteIndex == 0xffff) {
    color = mCurrentColor;
  } else {  // Palette index out of range! Return transparent black.
    color = sRGBColor();
  }
  color.a *= aAlpha;
  return ToDeviceColor(color);
}

static bool DispatchPaint(const PaintState& aState, uint32_t aOffset,
                          const Rect* aBounds /* may be nullptr if unknown */);
static UniquePtr<Pattern> DispatchMakePattern(const PaintState& aState,
                                              uint32_t aOffset);
static Rect DispatchGetBounds(const PaintState& aState, uint32_t aOffset);
static Matrix DispatchGetMatrix(const PaintState& aState, uint32_t aOffset);

// Variation-data types
struct Fixed {
  enum { kFractionBits = 16 };
  operator float() const {
    return float(int32_t(value)) / float(1 << kFractionBits);
  }
  int32_t intRepr() const { return int32_t(value); }

 private:
  int32 value;
};

struct F2DOT14 {
  enum { kFractionBits = 14 };
  operator float() const {
    return float(int16_t(value)) / float(1 << kFractionBits);
  }
  int32_t intRepr() const { return int16_t(value); }

 private:
  int16 value;
};

// Saturating addition used for variation indexes to avoid wrap-around.
static uint32_t SatAdd(uint32_t a, uint32_t b) {
  if (a <= std::numeric_limits<uint32_t>::max() - b) {
    return a + b;
  }
  return std::numeric_limits<uint32_t>::max();
}

struct RegionAxisCoordinates {
  F2DOT14 startCoord;
  F2DOT14 peakCoord;
  F2DOT14 endCoord;
};

struct VariationRegion {
  // RegionAxisCoordinates regionAxes[axisCount];
  const RegionAxisCoordinates* regionAxes() const {
    return reinterpret_cast<const RegionAxisCoordinates*>(this);
  }
};

struct VariationRegionList {
  uint16 axisCount;
  uint16 regionCount;
  // VariationRegion variationRegions[regionCount];
  const char* variationRegionsBase() const {
    return reinterpret_cast<const char*>(this + 1);
  }
  size_t regionSize() const {
    return uint16_t(axisCount) * sizeof(RegionAxisCoordinates);
  }
  const VariationRegion* getRegion(uint16_t i) const {
    if (i >= uint16_t(regionCount)) {
      return nullptr;
    }
    return reinterpret_cast<const VariationRegion*>(variationRegionsBase() +
                                                    i * regionSize());
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const {
    if (variationRegionsBase() - reinterpret_cast<const char*>(aHeader) +
            uint16_t(regionCount) * regionSize() >
        aLength) {
      return false;
    }
    return true;
  }
};

struct DeltaSet {
  // (int16 and int8)
  // *or*
  // (int32 and int16) deltaData[regionIndexCount];
};

struct DeltaSetIndexMap {
  enum { INNER_INDEX_BIT_COUNT_MASK = 0x0f, MAP_ENTRY_SIZE_MASK = 0x30 };
  uint8_t format;
  uint8_t entryFormat;
  union {
    struct {
      uint16 mapCount;
      // uint8 mapData[variable];
    } v0;
    struct {
      uint32 mapCount;
      // uint8 mapData[variable];
    } v1;
  };
  uint32_t entrySize() const {
    return (((entryFormat & MAP_ENTRY_SIZE_MASK) >> 4) + 1);
  }
  uint32_t map(uint32_t aIndex) const {
    uint32_t mapCount;
    const uint8_t* mapData;
    switch (format) {
      case 0:
        mapCount = uint32_t(v0.mapCount);
        mapData = reinterpret_cast<const uint8_t*>(&v0.mapCount) +
                  sizeof(v0.mapCount);
        break;
      case 1:
        mapCount = uint32_t(v1.mapCount);
        mapData = reinterpret_cast<const uint8_t*>(&v1.mapCount) +
                  sizeof(v1.mapCount);
        break;
      default:
        // unknown DeltaSetIndexMap format
        return aIndex;
    }
    if (!mapCount) {
      return aIndex;
    }
    if (aIndex >= mapCount) {
      aIndex = mapCount - 1;
    }
    const uint8_t* entryData = mapData + aIndex * entrySize();
    uint32_t entry = 0;
    for (uint32_t i = 0; i < entrySize(); ++i) {
      entry = (entry << 8) + entryData[i];
    }
    uint16_t outerIndex =
        entry >> ((entryFormat & INNER_INDEX_BIT_COUNT_MASK) + 1);
    uint16_t innerIndex =
        entry & ((1 << ((entryFormat & INNER_INDEX_BIT_COUNT_MASK) + 1)) - 1);
    return (uint32_t(outerIndex) << 16) + innerIndex;
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

enum EntryFormatMasks {
  INNER_INDEX_BIT_COUNT_MASK = 0x0f,
  MAP_ENTRY_SIZE_MASK = 0x30
};

struct ItemVariationData {
  enum { WORD_DELTA_COUNT_MASK = 0x7FFF, LONG_WORDS = 0x8000 };
  uint16 itemCount;
  uint16 wordDeltaCount;
  uint16 regionIndexCount;
  // uint16 regionIndexes[regionIndexCount];
  const uint16* regionIndexes() const {
    return reinterpret_cast<const uint16*>(
        reinterpret_cast<const char*>(this + 1));
  }
  // DeltaSet deltaSets[itemCount];
  const DeltaSet* deltaSets() const {
    return reinterpret_cast<const DeltaSet*>(
        reinterpret_cast<const char*>(this + 1) +
        uint16_t(regionIndexCount) * sizeof(uint16));
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

struct ItemVariationStore {
  uint16 format;
  Offset32 variationRegionListOffset;
  uint16 itemVariationDataCount;
  // Offset32 itemVariationDataOffsets[itemVariationDataCount];
  const Offset32* itemVariationDataOffsets() const {
    return reinterpret_cast<const Offset32*>(
        reinterpret_cast<const char*>(this + 1));
  }
  const VariationRegionList* variationRegionList() const {
    return reinterpret_cast<const VariationRegionList*>(
        reinterpret_cast<const char*>(this) + variationRegionListOffset);
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

static int32_t ApplyVariation(const PaintState& aState, int32_t aValue,
                              uint32_t aIndex) {
  if (aIndex == 0xffffffff) {
    return aValue;
  }
  const auto* store = aState.mHeader.v1->itemVariationStore();
  if (!store || uint16_t(store->format) != 1) {
    return aValue;
  }
  const DeltaSetIndexMap* map = aState.mHeader.v1->varIndexMap();
  uint32_t mappedIndex = map ? map->map(aIndex) : aIndex;
  uint16_t outerIndex = mappedIndex >> 16;
  uint16_t innerIndex = mappedIndex & 0xffff;
  const auto* itemVariationDataOffsets = store->itemVariationDataOffsets();
  if (mappedIndex == 0xffffffff ||
      outerIndex >= uint16_t(store->itemVariationDataCount) ||
      !itemVariationDataOffsets[outerIndex]) {
    return aValue;
  }
  const auto* regionList = store->variationRegionList();
  if (outerIndex >= uint16_t(store->itemVariationDataCount)) {
    return aValue;
  }
  const auto* variationData = reinterpret_cast<const ItemVariationData*>(
      reinterpret_cast<const char*>(store) +
      itemVariationDataOffsets[outerIndex]);
  if (innerIndex >= uint16_t(variationData->itemCount)) {
    return aValue;
  }
  const auto* regionIndexes = variationData->regionIndexes();
  uint16_t regionIndexCount = variationData->regionIndexCount;
  const DeltaSet* deltaSets = variationData->deltaSets();
  uint16_t wordDeltaCount = variationData->wordDeltaCount;
  bool longWords = wordDeltaCount & ItemVariationData::LONG_WORDS;
  wordDeltaCount &= ItemVariationData::WORD_DELTA_COUNT_MASK;
  uint32_t deltaSetSize = (regionIndexCount + wordDeltaCount) << longWords;
  const uint8_t* deltaData =
      reinterpret_cast<const uint8_t*>(deltaSets) + deltaSetSize * innerIndex;
  uint16_t deltaSize = longWords ? 4 : 2;
  int32_t result = aValue;
  for (uint16_t i = 0; i < regionIndexCount; ++i, deltaData += deltaSize) {
    if (i == wordDeltaCount) {
      deltaSize >>= 1;
    }
    const auto* region = regionList->getRegion(uint16_t(regionIndexes[i]));
    if (!region) {
      return aValue;
    }
    // XXX Should we do the calculations here in fixed-point? Check spec.
    float scalar = -1.0;
    for (uint16_t axisIndex = 0; axisIndex < uint16_t(regionList->axisCount);
         ++axisIndex) {
      const auto& axis = region->regionAxes()[axisIndex];
      float peak = axis.peakCoord;
      if (peak == 0.0) {
        // This axis cannot contribute to scalar.
        continue;
      }
      float start = axis.startCoord;
      float end = axis.endCoord;
      float value = axisIndex < aState.mCoordCount
                        ? float(aState.mCoords[axisIndex]) / 16384.0f
                        : 0.0;
      if (value < start || value > end) {
        // Out of range: this region is not applicable.
        scalar = -1.0;
        break;
      }
      if (scalar < 0.0) {
        scalar = 1.0;
      }
      if (value == peak) {
        continue;
      }
      if (value < peak && peak > start) {
        scalar *= (value - start) / (peak - start);
      } else if (value > peak && peak < end) {
        scalar *= (end - value) / (end - peak);
      }
    }
    if (scalar <= 0.0) {
      continue;
    }
    int32_t delta = *reinterpret_cast<const int8_t*>(deltaData);  // sign-extend
    for (uint16_t j = 1; j < deltaSize; ++j) {
      delta = (delta << 8) | deltaData[j];
    }
    delta = int32_t(floorf((float(delta) * scalar) + 0.5f));
    result += delta;
  }
  return result;
};

static float ApplyVariation(const PaintState& aState, Fixed aValue,
                            uint32_t aIndex) {
  return float(ApplyVariation(aState, aValue.intRepr(), aIndex)) /
         (1 << Fixed::kFractionBits);
}

static float ApplyVariation(const PaintState& aState, F2DOT14 aValue,
                            uint32_t aIndex) {
  return float(ApplyVariation(aState, aValue.intRepr(), aIndex)) /
         (1 << F2DOT14::kFractionBits);
}

struct ClipBoxFormat1 {
  enum { kFormat = 1 };
  uint8_t format;
  FWORD xMin;
  FWORD yMin;
  FWORD xMax;
  FWORD yMax;

  Rect GetRect(const PaintState& aState) const {
    MOZ_ASSERT(format == kFormat);
    int32_t x0 = int16_t(xMin);
    int32_t y0 = int16_t(yMin);
    int32_t x1 = int16_t(xMax);
    int32_t y1 = int16_t(yMax);
    // Flip the y-coordinates to map from OpenType to Moz2d space.
    return Rect(aState.F2P(x0), -aState.F2P(y1), aState.F2P(x1 - x0),
                aState.F2P(y1 - y0));
  }
};

struct ClipBoxFormat2 : public ClipBoxFormat1 {
  enum { kFormat = 2 };
  uint32 varIndexBase;

  Rect GetRect(const PaintState& aState) const {
    MOZ_ASSERT(format == kFormat);
    int32_t x0 = ApplyVariation(aState, int16_t(xMin), varIndexBase);
    int32_t y0 = ApplyVariation(aState, int16_t(yMin), SatAdd(varIndexBase, 1));
    int32_t x1 = ApplyVariation(aState, int16_t(xMax), SatAdd(varIndexBase, 2));
    int32_t y1 = ApplyVariation(aState, int16_t(yMax), SatAdd(varIndexBase, 3));
    return Rect(aState.F2P(x0), -aState.F2P(y1), aState.F2P(x1 - x0),
                aState.F2P(y1 - y0));
  }
};

struct Clip {
  uint16 startGlyphID;
  uint16 endGlyphID;
  Offset24 clipBoxOffset;

  Rect GetRect(const PaintState& aState) const {
    uint32_t offset = aState.mHeader.v1->clipListOffset + clipBoxOffset;
    const auto* box = aState.COLRv1BaseAddr() + offset;
    switch (*box) {
      case 1:
        return reinterpret_cast<const ClipBoxFormat1*>(box)->GetRect(aState);
      case 2:
        return reinterpret_cast<const ClipBoxFormat2*>(box)->GetRect(aState);
      default:
        // unknown ClipBoxFormat
        break;
    }
    return Rect();
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

struct ClipList {
  uint8_t format;
  uint32 numClips;
  // Clip clips[numClips]
  const Clip* clips() const { return reinterpret_cast<const Clip*>(this + 1); }
  const Clip* GetClip(uint32_t aGlyphId) const {
    auto compare = [](const void* key, const void* data) -> int {
      uint32_t glyphId = (uint32_t)(uintptr_t)key;
      const auto* clip = reinterpret_cast<const Clip*>(data);
      uint32_t start = uint16_t(clip->startGlyphID);
      uint32_t end = uint16_t(clip->endGlyphID);
      if (start <= glyphId && end >= glyphId) {
        return 0;
      }
      return start > glyphId ? -1 : 1;
    };
    return reinterpret_cast<const Clip*>(bsearch((void*)(uintptr_t)aGlyphId,
                                                 clips(), uint32_t(numClips),
                                                 sizeof(Clip), compare));
  }
  bool Validate(const COLRv1Header* aHeader, uint64_t aLength) const;
};

struct LayerRecord {
  uint16 glyphId;
  uint16 paletteEntryIndex;

  bool Paint(const PaintState& aState, float aAlpha,
             const Point& aPoint) const {
    Glyph glyph{uint16_t(glyphId), aPoint};
    GlyphBuffer buffer{&glyph, 1};
    aState.mDrawTarget->FillGlyphs(
        aState.mScaledFont, buffer,
        ColorPattern(aState.GetColor(paletteEntryIndex, aAlpha)),
        aState.mDrawOptions);
    return true;
  }
};

struct BaseGlyphRecord {
  uint16 glyphId;
  uint16 firstLayerIndex;
  uint16 numLayers;

  bool Paint(const PaintState& aState, float aAlpha,
             const Point& aPoint) const {
    uint32_t layerIndex = uint16_t(firstLayerIndex);
    uint32_t end = layerIndex + uint16_t(numLayers);
    if (end > uint16_t(aState.mHeader.v0->numLayerRecords)) {
      MOZ_ASSERT_UNREACHABLE("bad COLRv0 table");
      return false;
    }
    const auto* layers = aState.mHeader.v0->GetLayerRecords();
    while (layerIndex < end) {
      if (!layers[layerIndex].Paint(aState, aAlpha, aPoint)) {
        return false;
      }
      ++layerIndex;
    }
    return true;
  }
};

struct ColorStop {
  F2DOT14 stopOffset;
  uint16 paletteIndex;
  F2DOT14 alpha;

  float GetStopOffset(const PaintState& aState) const { return stopOffset; }
  uint16_t GetPaletteIndex() const { return paletteIndex; }
  float GetAlpha(const PaintState& aState) const { return alpha; }
};

struct VarColorStop : public ColorStop {
  uint32 varIndexBase;

  float GetStopOffset(const PaintState& aState) const {
    return ApplyVariation(aState, stopOffset, varIndexBase);
  }
  float GetAlpha(const PaintState& aState) const {
    return ApplyVariation(aState, alpha, SatAdd(varIndexBase, 1));
  }
};

template <typename T>
struct ColorLineT {
  enum { EXTEND_PAD = 0, EXTEND_REPEAT = 1, EXTEND_REFLECT = 2 };
  uint8_t extend;
  uint16 numStops;
  const T* colorStops() const { return reinterpret_cast<const T*>(this + 1); }

  // If the color line has only one stop, return it as a simple ColorPattern.
  UniquePtr<Pattern> AsSolidColor(const PaintState& aState) const {
    if (uint16_t(numStops) != 1) {
      return nullptr;
    }
    const auto* stop = colorStops();
    return MakeUnique<ColorPattern>(
        aState.GetColor(stop->GetPaletteIndex(), stop->GetAlpha(aState)));
  }

  // Retrieve the color stops into an array of GradientStop records. The stops
  // are normalized to the range [0 .. 1], and the original offsets of the
  // first and last stops are returned.
  // If aReverse is true, the color line is reversed.
  void CollectGradientStops(const PaintState& aState,
                            nsTArray<GradientStop>& aStops, float* aFirstStop,
                            float* aLastStop, bool aReverse = false) const {
    MOZ_ASSERT(aStops.IsEmpty());
    uint16_t count = numStops;
    if (!count) {
      return;
    }
    const auto* stop = colorStops();
    if (reinterpret_cast<const char*>(stop) + count * sizeof(T) >
        aState.COLRv1BaseAddr() + aState.mCOLRLength) {
      return;
    }
    aStops.SetCapacity(count);
    for (uint16_t i = 0; i < count; ++i, ++stop) {
      DeviceColor color =
          aState.GetColor(stop->GetPaletteIndex(), stop->GetAlpha(aState));
      aStops.AppendElement(GradientStop{stop->GetStopOffset(aState), color});
    }
    if (count == 1) {
      *aFirstStop = *aLastStop = aStops[0].offset;
      return;
    }
    aStops.StableSort();
    if (aReverse) {
      float a = aStops[0].offset;
      float b = aStops.LastElement().offset;
      aStops.Reverse();
      for (auto& gs : aStops) {
        gs.offset = a + b - gs.offset;
      }
    }
    // Normalize stops to the range 0.0 .. 1.0, and return the original
    // start & end.
    // Note that if all stops are at the same offset, no normalization
    // will be done.
    *aFirstStop = aStops[0].offset;
    *aLastStop = aStops.LastElement().offset;
    if ((*aLastStop > *aFirstStop) &&
        (*aLastStop != 1.0f || *aFirstStop != 0.0f)) {
      float f = 1.0f / (*aLastStop - *aFirstStop);
      for (auto& gs : aStops) {
        gs.offset = (gs.offset - *aFirstStop) * f;
      }
    }
  }

  // Create a gfx::GradientStops representing the given color line stops,
  // applying our extend mode.
  already_AddRefed<GradientStops> MakeGradientStops(
      const PaintState& aState, nsTArray<GradientStop>& aStops) const {
    auto mapExtendMode = [](uint8_t aExtend) -> ExtendMode {
      switch (aExtend) {
        case EXTEND_REPEAT:
          return ExtendMode::REPEAT;
        case EXTEND_REFLECT:
          return ExtendMode::REFLECT;
        case EXTEND_PAD:
        default:
          return ExtendMode::CLAMP;
      }
    };
    return aState.mDrawTarget->CreateGradientStops(
        aStops.Elements(), aStops.Length(), mapExtendMode(extend));
  }

  already_AddRefed<GradientStops> MakeGradientStops(
      const PaintState& aState, float* aFirstStop, float* aLastStop,
      bool aReverse = false) const {
    AutoTArray<GradientStop, 8> stops;
    CollectGradientStops(aState, stops, aFirstStop, aLastStop, aReverse);
    if (stops.IsEmpty()) {
      return nullptr;
    }
    return MakeGradientStops(aState, stops);
  }
};

using ColorLine = ColorLineT<ColorStop>;
using VarColorLine = ColorLineT<VarColorStop>;

// Used to check for cycles in the paint graph, and bail out to avoid infinite
// recursion when traversing the graph in Paint() or GetBoundingRect(). (Only
// PaintColrLayers and PaintColrGlyph can cause cycles; all other paint types
// have only forward references within the table.)
#define IF_CYCLE_RETURN(retval)             \
  if (aState.mVisited->Contains(aOffset)) { \
    return retval;                          \
  }                                         \
  aState.mVisited->AppendElement(aOffset);  \
  ScopeExit e([aState]() { aState.mVisited->RemoveLastElement(); })

struct PaintColrLayers {
  enum { kFormat = 1 };
  uint8_t format;
  uint8_t numLayers;
  uint32 firstLayerIndex;

  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    MOZ_ASSERT(format == kFormat);
    IF_CYCLE_RETURN(true);
    const auto* layerList = aState.mHeader.v1->layerList();
    if (!layerList) {
      return false;
    }
    if (uint64_t(firstLayerIndex) + numLayers > layerList->numLayers) {
      return false;
    }
    const auto* paintOffsets = layerList->paintOffsets() + firstLayerIndex;
    for (uint32_t i = 0; i < numLayers; i++) {
      if (!DispatchPaint(aState,
                         aState.mHeader.v1->layerListOffset + paintOffsets[i],
                         aBounds)) {
        return false;
      }
    }
    return true;
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    IF_CYCLE_RETURN(Rect());
    const auto* layerList = aState.mHeader.v1->layerList();
    if (!layerList) {
      return Rect();
    }
    if (uint64_t(firstLayerIndex) + numLayers > layerList->numLayers) {
      return Rect();
    }
    Rect result;
    const auto* paintOffsets = layerList->paintOffsets() + firstLayerIndex;
    for (uint32_t i = 0; i < numLayers; i++) {
      result = result.Union(DispatchGetBounds(
          aState, aState.mHeader.v1->layerListOffset + paintOffsets[i]));
    }
    return result;
  }
};

struct PaintPatternBase {
  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    Matrix m = aState.mDrawTarget->GetTransform();
    if (m.Invert()) {
      if (auto pattern = DispatchMakePattern(aState, aOffset)) {
        aState.mDrawTarget->FillRect(
            m.TransformBounds(IntRectToRect(aState.mDrawTarget->GetRect())),
            *pattern, aState.mDrawOptions);
        return true;
      }
    }
    return false;
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    return Rect();
  }
};

struct PaintSolid : public PaintPatternBase {
  enum { kFormat = 2 };
  uint8_t format;
  uint16 paletteIndex;
  F2DOT14 alpha;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return MakeUnique<ColorPattern>(aState.GetColor(paletteIndex, alpha));
  }
};

struct PaintVarSolid : public PaintSolid {
  enum { kFormat = 3 };
  uint32 varIndexBase;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return MakeUnique<ColorPattern>(aState.GetColor(
        paletteIndex, ApplyVariation(aState, alpha, varIndexBase)));
  }
};

struct PaintLinearGradient : public PaintPatternBase {
  enum { kFormat = 4 };
  uint8_t format;
  Offset24 colorLineOffset;
  FWORD x0;
  FWORD y0;
  FWORD x1;
  FWORD y1;
  FWORD x2;
  FWORD y2;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(ColorLine) + sizeof(ColorStop) > aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine =
        reinterpret_cast<const ColorLine*>(aState.COLRv1BaseAddr() + clOffset);
    Point p0(aState.F2P(int16_t(x0)), aState.F2P(int16_t(y0)));
    Point p1(aState.F2P(int16_t(x1)), aState.F2P(int16_t(y1)));
    Point p2(aState.F2P(int16_t(x2)), aState.F2P(int16_t(y2)));
    return NormalizeAndMakeGradient(aState, colorLine, p0, p1, p2);
  }

  template <typename T>
  UniquePtr<Pattern> NormalizeAndMakeGradient(const PaintState& aState,
                                              const T* aColorLine, Point p0,
                                              Point p1, Point p2) const {
    // Ill-formed gradient should not be rendered.
    if (p1 == p0 || p2 == p0) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    UniquePtr<Pattern> solidColor = aColorLine->AsSolidColor(aState);
    if (solidColor) {
      return solidColor;
    }
    float firstStop, lastStop;
    AutoTArray<GradientStop, 8> stopArray;
    aColorLine->CollectGradientStops(aState, stopArray, &firstStop, &lastStop);
    if (stopArray.IsEmpty()) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    if (firstStop != 0.0 || lastStop != 1.0) {
      if (firstStop == lastStop) {
        if (aColorLine->extend != T::EXTEND_PAD) {
          return MakeUnique<ColorPattern>(DeviceColor());
        }
        // For extend-pad, when the color line is zero-length, we add a "fake"
        // color stop to create a [0.0..1.0]-normalized color line, so that the
        // projection of points below works as expected.
        for (auto& gs : stopArray) {
          gs.offset = 0.0f;
        }
        stopArray.AppendElement(
            GradientStop{1.0f, stopArray.LastElement().color});
        lastStop += 1.0f;
      }
      // Adjust positions of the points to account for normalization of the
      // color line stop offsets.
      Point v = p1 - p0;
      p0 += v * firstStop;
      p1 -= v * (1.0f - lastStop);
      // Move the rotation vector to maintain the same direction from p0.
      p2 += v * firstStop;
    }
    Point p3;
    if (FuzzyEqualsMultiplicative(p2.y, p0.y)) {
      // rotation vector is horizontal
      p3 = Point(p0.x, p1.y);
    } else if (FuzzyEqualsMultiplicative(p2.x, p0.x)) {
      // rotation vector is vertical
      p3 = Point(p1.x, p0.y);
    } else {
      float m = (p2.y - p0.y) / (p2.x - p0.x);  // slope of line p0->p2
      float mInv = -1.0f / m;         // slope of desired perpendicular p0->p3
      float c1 = p0.y - mInv * p0.x;  // line p0->p3 is m * x - y + c1 = 0
      float c2 = p1.y - m * p1.x;     // line p1->p3 is mInv * x - y + c2 = 0
      float x3 = (c1 - c2) / (m - mInv);
      float y3 = (c1 * m - c2 * mInv) / (m - mInv);
      p3 = Point(x3, y3);
    }
    RefPtr stops = aColorLine->MakeGradientStops(aState, stopArray);
    return MakeUnique<LinearGradientPattern>(p0, p3, std::move(stops),
                                             Matrix::Scaling(1.0, -1.0));
  }
};

struct PaintVarLinearGradient : public PaintLinearGradient {
  enum { kFormat = 5 };
  uint32 varIndexBase;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(VarColorLine) + sizeof(VarColorStop) >
        aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine = reinterpret_cast<const VarColorLine*>(
        aState.COLRv1BaseAddr() + clOffset);
    Point p0(aState.F2P(ApplyVariation(aState, int16_t(x0), varIndexBase)),
             aState.F2P(
                 ApplyVariation(aState, int16_t(y0), SatAdd(varIndexBase, 1))));
    Point p1(aState.F2P(
                 ApplyVariation(aState, int16_t(x1), SatAdd(varIndexBase, 2))),
             aState.F2P(
                 ApplyVariation(aState, int16_t(y1), SatAdd(varIndexBase, 3))));
    Point p2(aState.F2P(
                 ApplyVariation(aState, int16_t(x2), SatAdd(varIndexBase, 4))),
             aState.F2P(
                 ApplyVariation(aState, int16_t(y2), SatAdd(varIndexBase, 5))));
    return NormalizeAndMakeGradient(aState, colorLine, p0, p1, p2);
  }
};

struct PaintRadialGradient : public PaintPatternBase {
  enum { kFormat = 6 };
  uint8_t format;
  Offset24 colorLineOffset;
  FWORD x0;
  FWORD y0;
  UFWORD radius0;
  FWORD x1;
  FWORD y1;
  UFWORD radius1;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(ColorLine) + sizeof(ColorStop) > aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine =
        reinterpret_cast<const ColorLine*>(aState.COLRv1BaseAddr() + clOffset);
    Point c1(aState.F2P(int16_t(x0)), aState.F2P(int16_t(y0)));
    Point c2(aState.F2P(int16_t(x1)), aState.F2P(int16_t(y1)));
    float r1 = aState.F2P(uint16_t(radius0));
    float r2 = aState.F2P(uint16_t(radius1));
    return NormalizeAndMakeGradient(aState, colorLine, c1, c2, r1, r2);
  }

  // Helper function to trim the gradient stops array at the start or end.
  void TruncateGradientStops(nsTArray<GradientStop>& aStops, float aStart,
                             float aEnd) const {
    // For pad mode, we may need a sub-range of the line: figure out which
    // stops to trim, and interpolate as needed at truncation point(s).
    // (Currently this is only ever used to trim one end of the color line,
    // so edge cases that may occur when trimming both ends are untested.)
    MOZ_ASSERT(aStart == 0.0f || aEnd == 1.0f,
               "Trimming both ends of color-line is untested!");

    // Create a color that is |r| of the way from c1 to c2.
    auto interpolateColor = [](DeviceColor c1, DeviceColor c2, float r) {
      return DeviceColor(
          c2.r * r + c1.r * (1.0f - r), c2.g * r + c1.g * (1.0f - r),
          c2.b * r + c1.b * (1.0f - r), c2.a * r + c1.a * (1.0f - r));
    };

    size_t count = aStops.Length();
    MOZ_ASSERT(count > 1);

    // Truncate at the start of the color line?
    if (aStart > 0.0f) {
      // Skip forward past any stops that can be dropped.
      size_t i = 0;
      while (i < count - 1 && aStops[i].offset < aStart) {
        ++i;
      }
      // If we're not truncating exactly at a color-stop offset, shift the
      // preceding stop to the truncation offset and interpolate its color.
      if (i && aStops[i].offset > aStart) {
        auto& prev = aStops[i - 1];
        auto& curr = aStops[i];
        float ratio = (aStart - prev.offset) / (curr.offset - prev.offset);
        prev.color = interpolateColor(prev.color, curr.color, ratio);
        prev.offset = aStart;
        --i;  // We don't want to remove this stop, as we adjusted it.
      }
      aStops.RemoveElementsAt(0, i);
      // Re-normalize the remaining stops to the [0, 1] range.
      if (aStart < 1.0f) {
        float r = 1.0f / (1.0f - aStart);
        for (auto& gs : aStops) {
          gs.offset = r * (gs.offset - aStart);
        }
      }
    }

    // Truncate at the end of the color line?
    if (aEnd < 1.0f) {
      // Skip back over any stops that can be dropped.
      size_t i = count - 1;
      while (i && aStops[i].offset > aEnd) {
        --i;
      }
      // If we're not truncating exactly at a color-stop offset, shift the
      // following stop to the truncation offset and interpolate its color.
      if (i + 1 < count && aStops[i].offset < aEnd) {
        auto& next = aStops[i + 1];
        auto& curr = aStops[i];
        float ratio = (aEnd - curr.offset) / (next.offset - curr.offset);
        next.color = interpolateColor(curr.color, next.color, ratio);
        next.offset = aEnd;
        ++i;
      }
      aStops.RemoveElementsAt(i + 1, count - i - 1);
      // Re-normalize the remaining stops to the [0, 1] range.
      if (aEnd > 0.0f) {
        float r = 1.0f / aEnd;
        for (auto& gs : aStops) {
          gs.offset = r * gs.offset;
        }
      }
    }
  }

  template <typename T>
  UniquePtr<Pattern> NormalizeAndMakeGradient(const PaintState& aState,
                                              const T* aColorLine, Point c1,
                                              Point c2, float r1,
                                              float r2) const {
    if ((c1 == c2 && r1 == r2) || (r1 == 0.0 && r2 == 0.0)) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    UniquePtr<Pattern> solidColor = aColorLine->AsSolidColor(aState);
    if (solidColor) {
      return solidColor;
    }
    float firstStop, lastStop;
    AutoTArray<GradientStop, 8> stopArray;
    aColorLine->CollectGradientStops(aState, stopArray, &firstStop, &lastStop);
    if (stopArray.IsEmpty()) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    // If the color stop offsets had to be normalized to the [0, 1] range,
    // adjust the circle positions and radii to match.
    if (firstStop != 0.0f || lastStop != 1.0f) {
      if (firstStop == lastStop) {
        if (aColorLine->extend != T::EXTEND_PAD) {
          return MakeUnique<ColorPattern>(DeviceColor());
        }
        // For extend-pad, when the color line is zero-length, we add a "fake"
        // color stop to ensure we'll maintain the orientation of the cone,
        // otherwise when we adjust circles to account for the normalized color
        // stops, the centers will coalesce and the cone or cylinder collapses.
        for (auto& gs : stopArray) {
          gs.offset = 0.0f;
        }
        stopArray.AppendElement(
            GradientStop{1.0f, stopArray.LastElement().color});
        lastStop += 1.0f;
      }
      // Adjust centers along the vector between them, and scale radii for
      // gradient line defined from 0.0 to 1.0.
      Point vec = c2 - c1;
      c1 += vec * firstStop;
      c2 -= vec * (1.0f - lastStop);
      float deltaR = r2 - r1;
      r1 = r1 + deltaR * firstStop;
      r2 = r2 - deltaR * (1.0f - lastStop);
    }
    if ((r1 < 0.0f || r2 < 0.0f) && aColorLine->extend == T::EXTEND_PAD) {
      // For EXTEND_PAD, we can restrict the gradient definition to just its
      // visible portion because the shader doesn't need to see any part of the
      // color line that extends into the negative-radius "virtual cone".
      if (r1 < 0.0f && r2 < 0.0f) {
        // If both radii are negative, then only the color at the closer circle
        // will appear in the projected positive cone (or if they're equal,
        // nothing will be visible at all).
        if (r1 == r2) {
          return MakeUnique<ColorPattern>(DeviceColor());
        }
        // The defined range of the color line is entirely in the invisible
        // cone; all that will project into visible space is a single color.
        if (r1 < r2) {
          // Keep only the last color stop.
          stopArray.RemoveElementsAt(0, stopArray.Length() - 1);
        } else {
          // Keep only the first color stop.
          stopArray.RemoveElementsAt(1, stopArray.Length() - 1);
        }
      } else {
        // Truncate the gradient at the tip of the visible cone: find the color
        // stops closest to that point and interpolate between them.
        if (r1 < r2) {
          float start = r1 / (r1 - r2);
          TruncateGradientStops(stopArray, start, 1.0f);
          r1 = 0.0f;
          c1 = c1 * (1.0f - start) + c2 * start;
        } else if (r2 < r1) {
          float end = 1.0f - r2 / (r2 - r1);
          TruncateGradientStops(stopArray, 0.0f, end);
          r2 = 0.0f;
          c2 = c1 * (1.0f - end) + c2 * end;
        }
      }
    }
    // Handle negative radii, which the shader won't understand directly, by
    // projecting the circles along the cones such that both radii are positive.
    if (r1 < 0.0f || r2 < 0.0f) {
      float deltaR = r2 - r1;
      // If deltaR is zero, then nothing is visible because the cone has
      // degenerated into a negative-radius cylinder, and does not project
      // into visible space at all.
      if (deltaR == 0.0f) {
        return MakeUnique<ColorPattern>(DeviceColor());
      }
      Point vec = c2 - c1;
      if (aColorLine->extend == T::EXTEND_REFLECT) {
        deltaR *= 2.0f;
        vec = vec * 2.0f;
      }
      if (r2 < r1) {
        vec = -vec;
        deltaR = -deltaR;
      }
      // Number of repeats by which we need to shift.
      float n = std::ceil(std::max(-r1, -r2) / deltaR);
      deltaR *= n;
      r1 += deltaR;
      r2 += deltaR;
      vec = vec * n;
      c1 += vec;
      c2 += vec;
    }
    RefPtr stops = aColorLine->MakeGradientStops(aState, stopArray);
    if (!stops) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    return MakeUnique<RadialGradientPattern>(c1, c2, r1, r2, std::move(stops),
                                             Matrix::Scaling(1.0, -1.0));
  }
};

struct PaintVarRadialGradient : public PaintRadialGradient {
  enum { kFormat = 7 };
  uint32 varIndexBase;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(VarColorLine) + sizeof(VarColorStop) >
        aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine = reinterpret_cast<const VarColorLine*>(
        aState.COLRv1BaseAddr() + clOffset);
    Point c1(aState.F2P(ApplyVariation(aState, int16_t(x0), varIndexBase)),
             aState.F2P(
                 ApplyVariation(aState, int16_t(y0), SatAdd(varIndexBase, 1))));
    float r1 = aState.F2P(
        ApplyVariation(aState, uint16_t(radius0), SatAdd(varIndexBase, 2)));
    Point c2(aState.F2P(
                 ApplyVariation(aState, int16_t(x1), SatAdd(varIndexBase, 3))),
             aState.F2P(
                 ApplyVariation(aState, int16_t(y1), SatAdd(varIndexBase, 4))));
    float r2 = aState.F2P(
        ApplyVariation(aState, uint16_t(radius1), SatAdd(varIndexBase, 5)));
    return NormalizeAndMakeGradient(aState, colorLine, c1, c2, r1, r2);
  }
};

struct PaintSweepGradient : public PaintPatternBase {
  enum { kFormat = 8 };
  uint8_t format;
  Offset24 colorLineOffset;
  FWORD centerX;
  FWORD centerY;
  F2DOT14 startAngle;
  F2DOT14 endAngle;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(ColorLine) + sizeof(ColorStop) > aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine =
        reinterpret_cast<const ColorLine*>(aState.COLRv1BaseAddr() + clOffset);
    float start = float(startAngle) + 1.0f;
    float end = float(endAngle) + 1.0f;
    Point center(aState.F2P(int16_t(centerX)), aState.F2P(int16_t(centerY)));
    return NormalizeAndMakeGradient(aState, colorLine, center, start, end);
  }

  template <typename T>
  UniquePtr<Pattern> NormalizeAndMakeGradient(const PaintState& aState,
                                              const T* aColorLine,
                                              Point aCenter, float aStart,
                                              float aEnd) const {
    if (aStart == aEnd && aColorLine->extend != T::EXTEND_PAD) {
      return MakeUnique<ColorPattern>(DeviceColor());
    }
    UniquePtr<Pattern> solidColor = aColorLine->AsSolidColor(aState);
    if (solidColor) {
      return solidColor;
    }
    // ConicGradientPattern works counterclockwise. If the gradient is defined
    // clockwise (with aStart greater than aEnd), we'll reverse the color line
    // and swap the start and end angles.
    bool reverse = aEnd < aStart;
    float firstStop, lastStop;
    RefPtr stops =
        aColorLine->MakeGradientStops(aState, &firstStop, &lastStop, reverse);
    if (!stops) {
      return nullptr;
    }
    if (firstStop != 0.0 || lastStop != 1.0) {
      if (firstStop == lastStop) {
        if (aColorLine->extend != T::EXTEND_PAD) {
          return MakeUnique<ColorPattern>(DeviceColor());
        }
      } else {
        float sweep = aEnd - aStart;
        aStart = aStart + sweep * firstStop;
        aEnd = aStart + sweep * (lastStop - firstStop);
      }
    }
    if (reverse) {
      std::swap(aStart, aEnd);
    }
    return MakeUnique<ConicGradientPattern>(aCenter, M_PI / 2.0, aStart / 2.0,
                                            aEnd / 2.0, std::move(stops),
                                            Matrix::Scaling(1.0, -1.0));
  }
};

struct PaintVarSweepGradient : public PaintSweepGradient {
  enum { kFormat = 9 };
  uint32 varIndexBase;

  UniquePtr<Pattern> MakePattern(const PaintState& aState,
                                 uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    uint32_t clOffset = aOffset + colorLineOffset;
    if (clOffset + sizeof(VarColorLine) + sizeof(VarColorStop) >
        aState.mCOLRLength) {
      return nullptr;
    }
    const auto* colorLine = reinterpret_cast<const VarColorLine*>(
        aState.COLRv1BaseAddr() + clOffset);
    float start =
        ApplyVariation(aState, startAngle, SatAdd(varIndexBase, 2)) + 1.0f;
    float end =
        ApplyVariation(aState, endAngle, SatAdd(varIndexBase, 3)) + 1.0f;
    Point center(
        aState.F2P(ApplyVariation(aState, int16_t(centerX), varIndexBase)),
        aState.F2P(
            ApplyVariation(aState, int16_t(centerY), SatAdd(varIndexBase, 1))));
    return NormalizeAndMakeGradient(aState, colorLine, center, start, end);
  }
};

struct PaintGlyph {
  enum { kFormat = 10 };
  uint8_t format;
  Offset24 paintOffset;
  uint16 glyphID;

  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    MOZ_ASSERT(format == kFormat);
    if (!paintOffset) {
      return true;
    }
    Glyph g{uint16_t(glyphID), Point()};
    GlyphBuffer buffer{&g, 1};
    // If the paint is a simple fill (rather than a sub-graph of further paint
    // records), we can just use FillGlyphs to render it instead of setting up
    // a clip.
    UniquePtr<Pattern> fillPattern =
        DispatchMakePattern(aState, aOffset + paintOffset);
    if (fillPattern) {
      // On macOS we can't use FillGlyphs because when we render the glyph,
      // Core Text's own color font support may step in and ignore the
      // pattern. So to avoid this, fill the glyph as a path instead.
#if XP_MACOSX
      RefPtr<Path> path = GetPathForGlyphs(aState, buffer);
      aState.mDrawTarget->Fill(path, *fillPattern, aState.mDrawOptions);
#else
      aState.mDrawTarget->FillGlyphs(aState.mScaledFont, buffer, *fillPattern,
                                     aState.mDrawOptions);
#endif
      return true;
    }
    RefPtr<Path> path = GetPathForGlyphs(aState, buffer);
    aState.mDrawTarget->PushClip(path);
    bool ok = DispatchPaint(aState, aOffset + paintOffset, aBounds);
    aState.mDrawTarget->PopClip();
    return ok;
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Glyph g{uint16_t(glyphID), Point()};
    GlyphBuffer buffer{&g, 1};
    RefPtr<Path> path = GetPathForGlyphs(aState, buffer);
    return path->GetFastBounds();
  }

 private:
  RefPtr<Path> GetPathForGlyphs(const PaintState& aState,
                                const GlyphBuffer& buffer) const {
    if (aState.mDrawTarget->GetBackendType() == BackendType::WEBRENDER_TEXT) {
      RefPtr dt = gfxPlatform::ThreadLocalScreenReferenceDrawTarget();
      return aState.mScaledFont->GetPathForGlyphs(buffer, dt);
    }
    return aState.mScaledFont->GetPathForGlyphs(buffer, aState.mDrawTarget);
  }
};

struct PaintColrGlyph {
  enum { kFormat = 11 };
  uint8_t format;
  uint16 glyphID;

  // Factored out as a helper because this is also used by the top-level
  // PaintGlyphGraph function.
  static bool DoPaint(const PaintState& aState,
                      const BaseGlyphPaintRecord* aBaseGlyphPaint,
                      uint32_t aGlyphId, const Rect* aBounds) {
    AutoPopClips clips(aState.mDrawTarget);
    Rect clipRect;
    if (const auto* clipList = aState.mHeader.v1->clipList()) {
      if (const auto* clip = clipList->GetClip(aGlyphId)) {
        clipRect = clip->GetRect(aState);
        clips.PushClipRect(clipRect);
        if (!aBounds) {
          aBounds = &clipRect;
        }
      }
    }
    return DispatchPaint(
        aState,
        aState.mHeader.v1->baseGlyphListOffset + aBaseGlyphPaint->paintOffset,
        aBounds);
  }

  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    MOZ_ASSERT(format == kFormat);
    IF_CYCLE_RETURN(true);
    const auto* base = aState.mHeader.v1->GetBaseGlyphPaint(glyphID);
    return base ? DoPaint(aState, base, uint16_t(glyphID), aBounds) : false;
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    IF_CYCLE_RETURN(Rect());
    if (const auto* clipList = aState.mHeader.v1->clipList()) {
      if (const auto* clip = clipList->GetClip(uint16_t(glyphID))) {
        return clip->GetRect(aState);
      }
    }
    if (const auto* base =
            aState.mHeader.v1->GetBaseGlyphPaint(uint16_t(glyphID))) {
      return DispatchGetBounds(
          aState, aState.mHeader.v1->baseGlyphListOffset + base->paintOffset);
    }
    return Rect();
  }
};

#undef IF_CYCLE_RETURN

struct Affine2x3 {
  Fixed xx;
  Fixed yx;
  Fixed xy;
  Fixed yy;
  Fixed dx;
  Fixed dy;

  Matrix AsMatrix(const PaintState& aState) const {
    // Flip signs because of opposite y-axis direction in moz2d vs opentype.
    return Matrix(float(xx), -float(yx), -float(xy), float(yy),
                  aState.F2P(float(dx)), -aState.F2P(float(dy)));
  }
};

struct VarAffine2x3 : public Affine2x3 {
  uint32 varIndexBase;

  Matrix AsMatrix(const PaintState& aState) const {
    return Matrix(
        ApplyVariation(aState, xx, varIndexBase),
        -ApplyVariation(aState, yx, SatAdd(varIndexBase, 1)),
        -ApplyVariation(aState, xy, SatAdd(varIndexBase, 2)),
        ApplyVariation(aState, yy, SatAdd(varIndexBase, 3)),
        aState.F2P(ApplyVariation(aState, dx, SatAdd(varIndexBase, 4))),
        -aState.F2P(ApplyVariation(aState, dy, SatAdd(varIndexBase, 5))));
  };
};

struct PaintTransformBase {
  uint8_t format;
  Offset24 paintOffset;

  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    if (!paintOffset) {
      return true;
    }
    AutoRestoreTransform saveTransform(aState.mDrawTarget);
    aState.mDrawTarget->ConcatTransform(DispatchGetMatrix(aState, aOffset));
    return DispatchPaint(aState, aOffset + paintOffset, aBounds);
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    if (!paintOffset) {
      return Rect();
    }
    Rect bounds = DispatchGetBounds(aState, aOffset + paintOffset);
    bounds = DispatchGetMatrix(aState, aOffset).TransformBounds(bounds);
    return bounds;
  }
};

struct PaintTransform : public PaintTransformBase {
  enum { kFormat = 12 };
  Offset24 transformOffset;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    if (aOffset + transformOffset + sizeof(Affine2x3) > aState.mCOLRLength) {
      return Matrix();
    }
    const auto* t = reinterpret_cast<const Affine2x3*>(
        aState.COLRv1BaseAddr() + aOffset + transformOffset);
    return t->AsMatrix(aState);
  }
};

struct PaintVarTransform : public PaintTransformBase {
  enum { kFormat = 13 };
  Offset24 transformOffset;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    if (aOffset + transformOffset + sizeof(VarAffine2x3) > aState.mCOLRLength) {
      return Matrix();
    }
    const auto* t = reinterpret_cast<const VarAffine2x3*>(
        aState.COLRv1BaseAddr() + aOffset + transformOffset);
    return t->AsMatrix(aState);
  }
};

struct PaintTranslate : public PaintTransformBase {
  enum { kFormat = 14 };
  FWORD dx;
  FWORD dy;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Translation(aState.F2P(int16_t(dx)),
                               -aState.F2P(int16_t(dy)));
  }
};

struct PaintVarTranslate : public PaintTranslate {
  enum { kFormat = 15 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Translation(
        aState.F2P(ApplyVariation(aState, int16_t(dx), varIndexBase)),
        -aState.F2P(
            ApplyVariation(aState, int16_t(dy), SatAdd(varIndexBase, 1))));
  }
};

struct PaintScale : public PaintTransformBase {
  enum { kFormat = 16 };
  F2DOT14 scaleX;
  F2DOT14 scaleY;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Scaling(float(scaleX), float(scaleY));
  }
};

struct PaintVarScale : public PaintScale {
  enum { kFormat = 17 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Scaling(
        ApplyVariation(aState, scaleX, varIndexBase),
        ApplyVariation(aState, scaleY, SatAdd(varIndexBase, 1)));
  }
};

struct PaintScaleAroundCenter : public PaintTransformBase {
  enum { kFormat = 18 };
  F2DOT14 scaleX;
  F2DOT14 scaleY;
  FWORD centerX;
  FWORD centerY;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(int16_t(centerX)), -aState.F2P(int16_t(centerY)));
    return Matrix::Translation(center)
        .PreScale(float(scaleX), float(scaleY))
        .PreTranslate(-center);
  }
};

struct PaintVarScaleAroundCenter : public PaintScaleAroundCenter {
  enum { kFormat = 19 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(ApplyVariation(aState, int16_t(centerX),
                                           SatAdd(varIndexBase, 2))),
                 -aState.F2P(ApplyVariation(aState, int16_t(centerY),
                                            SatAdd(varIndexBase, 3))));
    return Matrix::Translation(center)
        .PreScale(ApplyVariation(aState, scaleX, varIndexBase),
                  ApplyVariation(aState, scaleY, SatAdd(varIndexBase, 1)))
        .PreTranslate(-center);
  }
};

struct PaintScaleUniform : public PaintTransformBase {
  enum { kFormat = 20 };
  F2DOT14 scale;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Scaling(float(scale), float(scale));
  }
};

struct PaintVarScaleUniform : public PaintScaleUniform {
  enum { kFormat = 21 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    float sc = ApplyVariation(aState, scale, varIndexBase);
    return Matrix::Scaling(sc, sc);
  }
};

struct PaintScaleUniformAroundCenter : public PaintTransformBase {
  enum { kFormat = 22 };
  F2DOT14 scale;
  FWORD centerX;
  FWORD centerY;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(int16_t(centerX)), -aState.F2P(int16_t(centerY)));
    return Matrix::Translation(center)
        .PreScale(float(scale), float(scale))
        .PreTranslate(-center);
  }
};

struct PaintVarScaleUniformAroundCenter : public PaintScaleUniformAroundCenter {
  enum { kFormat = 23 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    float sc = ApplyVariation(aState, scale, varIndexBase);
    Point center(aState.F2P(ApplyVariation(aState, int16_t(centerX),
                                           SatAdd(varIndexBase, 1))),
                 -aState.F2P(ApplyVariation(aState, int16_t(centerY),
                                            SatAdd(varIndexBase, 2))));
    return Matrix::Translation(center).PreScale(sc, sc).PreTranslate(-center);
  }
};

struct PaintRotate : public PaintTransformBase {
  enum { kFormat = 24 };
  F2DOT14 angle;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return Matrix::Rotation(-float(angle) * float(M_PI));
  }
};

struct PaintVarRotate : public PaintRotate {
  enum { kFormat = 25 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    float ang = ApplyVariation(aState, angle, varIndexBase);
    return Matrix::Rotation(-ang * float(M_PI));
  }
};

struct PaintRotateAroundCenter : public PaintTransformBase {
  enum { kFormat = 26 };
  F2DOT14 angle;
  FWORD centerX;
  FWORD centerY;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(int16_t(centerX)), -aState.F2P(int16_t(centerY)));
    return Matrix::Translation(center)
        .PreRotate(-float(angle) * float(M_PI))
        .PreTranslate(-center);
  }
};

struct PaintVarRotateAroundCenter : public PaintRotateAroundCenter {
  enum { kFormat = 27 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    float ang = ApplyVariation(aState, angle, varIndexBase);
    Point center(aState.F2P(ApplyVariation(aState, int16_t(centerX),
                                           SatAdd(varIndexBase, 1))),
                 -aState.F2P(ApplyVariation(aState, int16_t(centerY),
                                            SatAdd(varIndexBase, 2))));
    return Matrix::Translation(center)
        .PreRotate(-ang * float(M_PI))
        .PreTranslate(-center);
  }
};

static inline Matrix SkewMatrix(float aSkewX, float aSkewY) {
  float xy = tanf(aSkewX * float(M_PI));
  float yx = tanf(aSkewY * float(M_PI));
  return std::isnan(xy) || std::isnan(yx) ? Matrix()
                                          : Matrix(1.0, -yx, xy, 1.0, 0.0, 0.0);
}

struct PaintSkew : public PaintTransformBase {
  enum { kFormat = 28 };
  F2DOT14 xSkewAngle;
  F2DOT14 ySkewAngle;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return SkewMatrix(float(xSkewAngle), float(ySkewAngle));
  }
};

struct PaintVarSkew : public PaintSkew {
  enum { kFormat = 29 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    return SkewMatrix(
        float(ApplyVariation(aState, xSkewAngle, varIndexBase)),
        float(ApplyVariation(aState, ySkewAngle, SatAdd(varIndexBase, 1))));
  }
};

struct PaintSkewAroundCenter : public PaintTransformBase {
  enum { kFormat = 30 };
  F2DOT14 xSkewAngle;
  F2DOT14 ySkewAngle;
  FWORD centerX;
  FWORD centerY;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(int16_t(centerX)), -aState.F2P(int16_t(centerY)));
    return Matrix::Translation(center)
        .PreMultiply(SkewMatrix(float(xSkewAngle), float(ySkewAngle)))
        .PreTranslate(-center);
  }
};

struct PaintVarSkewAroundCenter : public PaintSkewAroundCenter {
  enum { kFormat = 31 };
  uint32 varIndexBase;

  Matrix GetMatrix(const PaintState& aState, uint32_t aOffset) const {
    MOZ_ASSERT(format == kFormat);
    Point center(aState.F2P(ApplyVariation(aState, int16_t(centerX),
                                           SatAdd(varIndexBase, 2))),
                 -aState.F2P(ApplyVariation(aState, int16_t(centerY),
                                            SatAdd(varIndexBase, 3))));
    return Matrix::Translation(center)
        .PreMultiply(SkewMatrix(
            ApplyVariation(aState, xSkewAngle, varIndexBase),
            ApplyVariation(aState, ySkewAngle, SatAdd(varIndexBase, 1))))
        .PreTranslate(-center);
  }
};

struct PaintComposite {
  enum { kFormat = 32 };
  uint8_t format;
  Offset24 sourcePaintOffset;
  uint8_t compositeMode;
  Offset24 backdropPaintOffset;

  enum {
    COMPOSITE_CLEAR = 0,
    COMPOSITE_SRC = 1,
    COMPOSITE_DEST = 2,
    COMPOSITE_SRC_OVER = 3,
    COMPOSITE_DEST_OVER = 4,
    COMPOSITE_SRC_IN = 5,
    COMPOSITE_DEST_IN = 6,
    COMPOSITE_SRC_OUT = 7,
    COMPOSITE_DEST_OUT = 8,
    COMPOSITE_SRC_ATOP = 9,
    COMPOSITE_DEST_ATOP = 10,
    COMPOSITE_XOR = 11,
    COMPOSITE_PLUS = 12,
    COMPOSITE_SCREEN = 13,
    COMPOSITE_OVERLAY = 14,
    COMPOSITE_DARKEN = 15,
    COMPOSITE_LIGHTEN = 16,
    COMPOSITE_COLOR_DODGE = 17,
    COMPOSITE_COLOR_BURN = 18,
    COMPOSITE_HARD_LIGHT = 19,
    COMPOSITE_SOFT_LIGHT = 20,
    COMPOSITE_DIFFERENCE = 21,
    COMPOSITE_EXCLUSION = 22,
    COMPOSITE_MULTIPLY = 23,
    COMPOSITE_HSL_HUE = 24,
    COMPOSITE_HSL_SATURATION = 25,
    COMPOSITE_HSL_COLOR = 26,
    COMPOSITE_HSL_LUMINOSITY = 27
  };

  bool Paint(const PaintState& aState, uint32_t aOffset,
             const Rect* aBounds) const {
    MOZ_ASSERT(format == kFormat);
    if (!backdropPaintOffset || !sourcePaintOffset) {
      return true;
    }
    auto mapCompositionMode = [](uint8_t aMode) -> CompositionOp {
      switch (aMode) {
        default:
          return CompositionOp::OP_SOURCE;
        case COMPOSITE_CLEAR:
        case COMPOSITE_SRC:
        case COMPOSITE_DEST:
          MOZ_ASSERT_UNREACHABLE("should have short-circuited");
          return CompositionOp::OP_SOURCE;
        case COMPOSITE_SRC_OVER:
          return CompositionOp::OP_OVER;
        case COMPOSITE_DEST_OVER:
          return CompositionOp::OP_DEST_OVER;
        case COMPOSITE_SRC_IN:
          return CompositionOp::OP_IN;
        case COMPOSITE_DEST_IN:
          return CompositionOp::OP_DEST_IN;
        case COMPOSITE_SRC_OUT:
          return CompositionOp::OP_OUT;
        case COMPOSITE_DEST_OUT:
          return CompositionOp::OP_DEST_OUT;
        case COMPOSITE_SRC_ATOP:
          return CompositionOp::OP_ATOP;
        case COMPOSITE_DEST_ATOP:
          return CompositionOp::OP_DEST_ATOP;
        case COMPOSITE_XOR:
          return CompositionOp::OP_XOR;
        case COMPOSITE_PLUS:
          return CompositionOp::OP_ADD;
        case COMPOSITE_SCREEN:
          return CompositionOp::OP_SCREEN;
        case COMPOSITE_OVERLAY:
          return CompositionOp::OP_OVERLAY;
        case COMPOSITE_DARKEN:
          return CompositionOp::OP_DARKEN;
        case COMPOSITE_LIGHTEN:
          return CompositionOp::OP_LIGHTEN;
        case COMPOSITE_COLOR_DODGE:
          return CompositionOp::OP_COLOR_DODGE;
        case COMPOSITE_COLOR_BURN:
          return CompositionOp::OP_COLOR_BURN;
        case COMPOSITE_HARD_LIGHT:
          return CompositionOp::OP_HARD_LIGHT;
        case COMPOSITE_SOFT_LIGHT:
          return CompositionOp::OP_SOFT_LIGHT;
        case COMPOSITE_DIFFERENCE:
          return CompositionOp::OP_DIFFERENCE;
        case COMPOSITE_EXCLUSION:
          return CompositionOp::OP_EXCLUSION;
        case COMPOSITE_MULTIPLY:
          return CompositionOp::OP_MULTIPLY;
        case COMPOSITE_HSL_HUE:
          return CompositionOp::OP_HUE;
        case COMPOSITE_HSL_SATURATION:
          return CompositionOp::OP_SATURATION;
        case COMPOSITE_HSL_COLOR:
          return CompositionOp::OP_COLOR;
        case COMPOSITE_HSL_LUMINOSITY:
          return CompositionOp::OP_LUMINOSITY;
      }
    };
    // Short-circuit cases:
    if (compositeMode == COMPOSITE_CLEAR) {
      return true;
    }
    if (compositeMode == COMPOSITE_SRC) {
      return DispatchPaint(aState, aOffset + sourcePaintOffset, aBounds);
    }
    if (compositeMode == COMPOSITE_DEST) {
      return DispatchPaint(aState, aOffset + backdropPaintOffset, aBounds);
    }

    // We need bounds for the temporary surfaces; so if we didn't have
    // explicitly-provided bounds from a clipList entry for the top-level
    // glyph, then we need to determine the bounding rect here.
    Rect bounds = aBounds ? *aBounds : GetBoundingRect(aState, aOffset);
    if (bounds.IsEmpty()) {
      return true;
    }
    bounds.RoundOut();

    PaintState state = aState;
    state.mDrawOptions.mCompositionOp = CompositionOp::OP_OVER;
    IntSize intSize(int(bounds.width), int(bounds.height));

    if (!aState.mDrawTarget->CanCreateSimilarDrawTarget(
            intSize, SurfaceFormat::B8G8R8A8)) {
      // We're not going to be able to render this, so just bail out.
      // (Returning true rather than false means we'll just not paint this
      // part of the glyph, but won't return an error and likely fall back
      // to an ugly black blob.)
      return true;
    }

    // Draw the backdrop paint graph to a temporary surface.
    RefPtr backdrop = aState.mDrawTarget->CreateSimilarDrawTarget(
        intSize, SurfaceFormat::B8G8R8A8);
    if (!backdrop) {
      return true;
    }
    backdrop->SetTransform(Matrix::Translation(-bounds.TopLeft()));
    state.mDrawTarget = backdrop;
    if (!DispatchPaint(state, aOffset + backdropPaintOffset, &bounds)) {
      return false;
    }

    // Draw the source paint graph to another temp surface.
    RefPtr source = aState.mDrawTarget->CreateSimilarDrawTarget(
        intSize, SurfaceFormat::B8G8R8A8);
    if (!source) {
      return true;
    }
    source->SetTransform(Matrix::Translation(-bounds.TopLeft()));
    state.mDrawTarget = source;
    if (!DispatchPaint(state, aOffset + sourcePaintOffset, &bounds)) {
      return false;
    }

    // Composite the source onto the backdrop using the specified operation.
    Rect localBounds(Point(), bounds.Size());
    RefPtr snapshot = source->Snapshot();
    backdrop->SetTransform(Matrix());
    backdrop->DrawSurface(snapshot, localBounds, localBounds,
                          DrawSurfaceOptions(),
                          DrawOptions(1.0, mapCompositionMode(compositeMode)));

    // And copy the composited result to our final destination.
    snapshot = backdrop->Snapshot();
    aState.mDrawTarget->DrawSurface(snapshot, bounds, localBounds);

    return true;
  }

  Rect GetBoundingRect(const PaintState& aState, uint32_t aOffset) const {
    if (!backdropPaintOffset || !sourcePaintOffset) {
      return Rect();
    }
    // For now, just return the maximal bounds that could result; this could be
    // smarter, returning just one of the rects or their intersection when
    // appropriate for the composite mode in effect.
    return DispatchGetBounds(aState, aOffset + backdropPaintOffset)
        .Union(DispatchGetBounds(aState, aOffset + sourcePaintOffset));
  }
};

#pragma pack()

const BaseGlyphPaintRecord* COLRv1Header::GetBaseGlyphPaint(
    uint32_t aGlyphId) const {
  const auto* list = baseGlyphList();
  if (!list) {
    return nullptr;
  }
  auto compare = [](const void* key, const void* data) -> int {
    uint32_t glyphId = (uint32_t)(uintptr_t)key;
    const auto* paintRecord =
        reinterpret_cast<const BaseGlyphPaintRecord*>(data);
    uint32_t baseGlyphId = uint16_t(paintRecord->glyphID);
    if (baseGlyphId == glyphId) {
      return 0;
    }
    return baseGlyphId > glyphId ? -1 : 1;
  };
  return reinterpret_cast<const BaseGlyphPaintRecord*>(
      bsearch((void*)(uintptr_t)aGlyphId, list + 1,
              uint32_t(list->numBaseGlyphPaintRecords),
              sizeof(BaseGlyphPaintRecord), compare));
}

#define DO_CASE_VAR(T) \
  DO_CASE(Paint##T);   \
  DO_CASE(PaintVar##T)

// Process paint table at aOffset from start of COLRv1 table.
static bool DispatchPaint(const PaintState& aState, uint32_t aOffset,
                          const Rect* aBounds) {
  if (aOffset >= aState.mCOLRLength) {
    return false;
  }

  const char* paint = aState.COLRv1BaseAddr() + aOffset;
  // All paint table formats start with an 8-bit 'format' field.
  uint8_t format = uint8_t(*paint);

#define DO_CASE(T)                                                         \
  case T::kFormat:                                                         \
    return aOffset + sizeof(T) <= aState.mCOLRLength                       \
               ? reinterpret_cast<const T*>(paint)->Paint(aState, aOffset, \
                                                          aBounds)         \
               : false

  switch (format) {
    DO_CASE(PaintColrLayers);
    DO_CASE_VAR(Solid);
    DO_CASE_VAR(LinearGradient);
    DO_CASE_VAR(RadialGradient);
    DO_CASE_VAR(SweepGradient);
    DO_CASE(PaintGlyph);
    DO_CASE(PaintColrGlyph);
    DO_CASE_VAR(Transform);
    DO_CASE_VAR(Translate);
    DO_CASE_VAR(Scale);
    DO_CASE_VAR(ScaleAroundCenter);
    DO_CASE_VAR(ScaleUniform);
    DO_CASE_VAR(ScaleUniformAroundCenter);
    DO_CASE_VAR(Rotate);
    DO_CASE_VAR(RotateAroundCenter);
    DO_CASE_VAR(Skew);
    DO_CASE_VAR(SkewAroundCenter);
    DO_CASE(PaintComposite);
    default:
      break;
  }

#undef DO_CASE

  return false;
}

// Get a gfx::Pattern corresponding to the given paint table, if it is a
// simple format that can be used as a fill (not a sub-graph).
static UniquePtr<Pattern> DispatchMakePattern(const PaintState& aState,
                                              uint32_t aOffset) {
  if (aOffset >= aState.mCOLRLength) {
    return nullptr;
  }

  const char* paint = aState.COLRv1BaseAddr() + aOffset;
  // All paint table formats start with an 8-bit 'format' field.
  uint8_t format = uint8_t(*paint);

#define DO_CASE(T)                                                       \
  case T::kFormat:                                                       \
    return aOffset + sizeof(T) <= aState.mCOLRLength                     \
               ? reinterpret_cast<const T*>(paint)->MakePattern(aState,  \
                                                                aOffset) \
               : nullptr;

  switch (format) {
    DO_CASE_VAR(Solid);
    DO_CASE_VAR(LinearGradient);
    DO_CASE_VAR(RadialGradient);
    DO_CASE_VAR(SweepGradient);
    default:
      break;
  }

#undef DO_CASE

  return nullptr;
}

static Matrix DispatchGetMatrix(const PaintState& aState, uint32_t aOffset) {
  if (aOffset >= aState.mCOLRLength) {
    return Matrix();
  }

  const char* paint = aState.COLRv1BaseAddr() + aOffset;
  // All paint table formats start with an 8-bit 'format' field.
  uint8_t format = uint8_t(*paint);

#define DO_CASE(T)                                                             \
  case T::kFormat:                                                             \
    return aOffset + sizeof(T) <= aState.mCOLRLength                           \
               ? reinterpret_cast<const T*>(paint)->GetMatrix(aState, aOffset) \
               : Matrix();

  switch (format) {
    DO_CASE_VAR(Transform);
    DO_CASE_VAR(Translate);
    DO_CASE_VAR(Scale);
    DO_CASE_VAR(ScaleAroundCenter);
    DO_CASE_VAR(ScaleUniform);
    DO_CASE_VAR(ScaleUniformAroundCenter);
    DO_CASE_VAR(Rotate);
    DO_CASE_VAR(RotateAroundCenter);
    DO_CASE_VAR(Skew);
    DO_CASE_VAR(SkewAroundCenter);
    default:
      break;
  }

#undef DO_CASE

  return Matrix();
}

static Rect DispatchGetBounds(const PaintState& aState, uint32_t aOffset) {
  if (aOffset >= aState.mCOLRLength) {
    return Rect();
  }

  const char* paint = aState.COLRv1BaseAddr() + aOffset;
  // All paint table formats start with an 8-bit 'format' field.
  uint8_t format = uint8_t(*paint);

#define DO_CASE(T)                                                           \
  case T::kFormat:                                                           \
    return aOffset + sizeof(T) <= aState.mCOLRLength                         \
               ? reinterpret_cast<const T*>(paint)->GetBoundingRect(aState,  \
                                                                    aOffset) \
               : Rect();

  switch (format) {
    DO_CASE(PaintColrLayers);
    DO_CASE_VAR(Solid);
    DO_CASE_VAR(LinearGradient);
    DO_CASE_VAR(RadialGradient);
    DO_CASE_VAR(SweepGradient);
    DO_CASE(PaintGlyph);
    DO_CASE(PaintColrGlyph);
    DO_CASE_VAR(Transform);
    DO_CASE_VAR(Translate);
    DO_CASE_VAR(Scale);
    DO_CASE_VAR(ScaleAroundCenter);
    DO_CASE_VAR(ScaleUniform);
    DO_CASE_VAR(ScaleUniformAroundCenter);
    DO_CASE_VAR(Rotate);
    DO_CASE_VAR(RotateAroundCenter);
    DO_CASE_VAR(Skew);
    DO_CASE_VAR(SkewAroundCenter);
    DO_CASE(PaintComposite);
    default:
      break;
  }

#undef DO_CASE

  return Rect();
}

#undef DO_CASE_VAR

bool COLRHeader::Validate(uint64_t aLength) const {
  uint64_t count;
  if ((count = numBaseGlyphRecords)) {
    if (baseGlyphRecordsOffset + count * sizeof(BaseGlyphRecord) > aLength) {
      return false;
    }
  }
  if ((count = numLayerRecords)) {
    if (layerRecordsOffset + count * sizeof(LayerRecord) > aLength) {
      return false;
    }
  }
  // Check ordering of baseGlyphRecords, and that layer indices are in bounds.
  int32_t lastGlyphId = -1;
  const auto* baseGlyph = reinterpret_cast<const BaseGlyphRecord*>(
      reinterpret_cast<const char*>(this) + baseGlyphRecordsOffset);
  for (uint16_t i = 0; i < uint16_t(numBaseGlyphRecords); i++, baseGlyph++) {
    uint16_t glyphId = baseGlyph->glyphId;
    if (lastGlyphId >= int32_t(glyphId)) {
      return false;
    }
    if (uint32_t(baseGlyph->firstLayerIndex) + uint32_t(baseGlyph->numLayers) >
        uint32_t(numLayerRecords)) {
      return false;
    }
    lastGlyphId = glyphId;
  }
  // We don't need to validate all the layer paletteEntryIndex fields here,
  // because PaintState.GetColor will range-check them at paint time.
  return true;
}

bool COLRv1Header::Validate(uint64_t aLength) const {
  if (!base.Validate(aLength)) {
    return false;
  }
  if (baseGlyphListOffset + sizeof(BaseGlyphList) > aLength ||
      layerListOffset + sizeof(LayerList) > aLength ||
      clipListOffset + sizeof(ClipList) > aLength ||
      varIndexMapOffset + sizeof(DeltaSetIndexMap) > aLength ||
      itemVariationStoreOffset + sizeof(ItemVariationStore) > aLength) {
    return false;
  }
  const auto* b = baseGlyphList();
  if (b && !b->Validate(this, aLength)) {
    return false;
  }
  const auto* l = layerList();
  if (l && !l->Validate(this, aLength)) {
    return false;
  }
  const auto* c = clipList();
  if (c && !c->Validate(this, aLength)) {
    return false;
  }
  const auto* v = varIndexMap();
  if (v && !v->Validate(this, aLength)) {
    return false;
  }
  const auto* i = itemVariationStore();
  if (i && !i->Validate(this, aLength)) {
    return false;
  }
  return true;
}

bool BaseGlyphList::Validate(const COLRv1Header* aHeader,
                             uint64_t aLength) const {
  uint64_t count = numBaseGlyphPaintRecords;
  if (aHeader->baseGlyphListOffset + sizeof(BaseGlyphList) +
          count * sizeof(BaseGlyphPaintRecord) >
      aLength) {
    return false;
  }
  // Check ordering of baseGlyphPaint records.
  const auto* records = baseGlyphPaintRecords();
  int32_t prevGlyphID = -1;
  for (uint32_t i = 0; i < numBaseGlyphPaintRecords; ++i) {
    const auto& base = records[i];
    if (int32_t(uint16_t(base.glyphID)) <= prevGlyphID) {
      return false;
    }
    prevGlyphID = base.glyphID;
  }
  return true;
}

bool LayerList::Validate(const COLRv1Header* aHeader, uint64_t aLength) const {
  // Check that paintOffsets array fits.
  uint64_t count = numLayers;
  uint32_t listOffset = aHeader->layerListOffset;
  if (listOffset + sizeof(LayerList) + count * sizeof(uint32) > aLength) {
    return false;
  }
  // Check that values in paintOffsets are within bounds.
  const auto* offsets = paintOffsets();
  for (uint32_t i = 0; i < count; i++) {
    if (listOffset + offsets[i] >= aLength) {
      return false;
    }
  }
  return true;
}

bool Clip::Validate(const COLRv1Header* aHeader, uint64_t aLength) const {
  uint32_t offset = aHeader->clipListOffset + clipBoxOffset;
  if (offset >= aLength) {
    return false;
  }
  // ClipBox format begins with a 1-byte format field.
  const uint8_t* box = reinterpret_cast<const uint8_t*>(aHeader) + offset;
  switch (*box) {
    case 1:
      if (offset <= aLength - sizeof(ClipBoxFormat1)) {
        return true;
      }
      break;
    case 2:
      if (offset <= aLength - sizeof(ClipBoxFormat2)) {
        return true;
      }
      break;
    default:
      // unknown ClipBoxFormat
      break;
  }
  return false;
}

bool ClipList::Validate(const COLRv1Header* aHeader, uint64_t aLength) const {
  uint64_t count = numClips;
  if (aHeader->clipListOffset + sizeof(ClipList) + count * sizeof(Clip) >
      aLength) {
    return false;
  }
  // Check ordering of clip records, and that they are within bounds.
  const auto* clipArray = clips();
  int32_t prevEnd = -1;
  for (uint32_t i = 0; i < count; ++i) {
    const auto& clip = clipArray[i];
    if (int32_t(uint16_t(clip.startGlyphID)) <= prevEnd) {
      return false;
    }
    if (!clip.Validate(aHeader, aLength)) {
      return false;
    }
    prevEnd = uint16_t(clip.endGlyphID);
  }
  return true;
}

bool DeltaSetIndexMap::Validate(const COLRv1Header* aHeader,
                                uint64_t aLength) const {
  uint64_t entrySize = ((entryFormat & MAP_ENTRY_SIZE_MASK) >> 4) + 1;
  uint64_t mapCount;
  uint64_t baseSize;
  switch (format) {
    case 0:
      mapCount = uint32_t(v0.mapCount);
      baseSize = 4;
      break;
    case 1:
      mapCount = uint32_t(v1.mapCount);
      baseSize = 6;
      break;
    default:
      return false;
  }
  if (aHeader->varIndexMapOffset + baseSize + mapCount * entrySize > aLength) {
    return false;
  }
  return true;
}

bool ItemVariationStore::Validate(const COLRv1Header* aHeader,
                                  uint64_t aLength) const {
  uint64_t offset = reinterpret_cast<const char*>(this) -
                    reinterpret_cast<const char*>(aHeader);
  if (offset + variationRegionListOffset + sizeof(VariationRegionList) >
      aLength) {
    return false;
  }
  if (!variationRegionList()->Validate(aHeader, aLength)) {
    return false;
  }
  uint16_t count = itemVariationDataCount;
  if (offset + sizeof(ItemVariationStore) + count * sizeof(Offset32) >
      aLength) {
    return false;
  }
  const auto* ivdOffsets = itemVariationDataOffsets();
  for (uint16_t i = 0; i < count; ++i) {
    uint32_t o = ivdOffsets[i];
    if (offset + o + sizeof(ItemVariationData) > aLength) {
      return false;
    }
    const auto* variationData = reinterpret_cast<const ItemVariationData*>(
        reinterpret_cast<const char*>(this) + ivdOffsets[i]);
    if (!variationData->Validate(aHeader, aLength)) {
      return false;
    }
  }
  return true;
}

bool ItemVariationData::Validate(const COLRv1Header* aHeader,
                                 uint64_t aLength) const {
  if (reinterpret_cast<const char*>(regionIndexes() +
                                    uint16_t(regionIndexCount)) >
      reinterpret_cast<const char*>(aHeader) + aLength) {
    return false;
  }
  uint16_t wordDeltaCount = this->wordDeltaCount;
  bool longWords = wordDeltaCount & LONG_WORDS;
  wordDeltaCount &= WORD_DELTA_COUNT_MASK;
  uint32_t deltaSetSize =
      (uint16_t(regionIndexCount) + uint16_t(wordDeltaCount)) << longWords;
  if (reinterpret_cast<const char*>(deltaSets()) +
          uint16_t(itemCount) * deltaSetSize >
      reinterpret_cast<const char*>(aHeader) + aLength) {
    return false;
  }
  return true;
}

}  // end anonymous namespace

namespace mozilla::gfx {

bool COLRFonts::ValidateColorGlyphs(hb_blob_t* aCOLR, hb_blob_t* aCPAL) {
  struct ColorRecord {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
    uint8_t alpha;
  };

  struct CPALHeaderVersion0 {
    uint16 version;
    uint16 numPaletteEntries;
    uint16 numPalettes;
    uint16 numColorRecords;
    Offset32 colorRecordsArrayOffset;
    // uint16 	colorRecordIndices[numPalettes];
    const uint16* colorRecordIndices() const {
      return reinterpret_cast<const uint16*>(this + 1);
    }
  };

  unsigned int cpalLength;
  const CPALHeaderVersion0* cpal = reinterpret_cast<const CPALHeaderVersion0*>(
      hb_blob_get_data(aCPAL, &cpalLength));
  if (!cpal || cpalLength < sizeof(CPALHeaderVersion0)) {
    return false;
  }

  // We can handle either version 0 or 1.
  if (uint16_t(cpal->version) > 1) {
    return false;
  }

  uint16_t numPaletteEntries = cpal->numPaletteEntries;
  uint16_t numPalettes = cpal->numPalettes;
  uint16_t numColorRecords = cpal->numColorRecords;
  uint32_t colorRecordsArrayOffset = cpal->colorRecordsArrayOffset;
  const auto* indices = cpal->colorRecordIndices();
  if (colorRecordsArrayOffset >= cpalLength) {
    return false;
  }
  if (!numPaletteEntries || !numPalettes ||
      numColorRecords < numPaletteEntries) {
    return false;
  }
  if (sizeof(ColorRecord) * numColorRecords >
      cpalLength - colorRecordsArrayOffset) {
    return false;
  }
  if (sizeof(uint16) * numPalettes > cpalLength - sizeof(CPALHeaderVersion0)) {
    return false;
  }
  for (uint16_t i = 0; i < numPalettes; ++i) {
    uint32_t index = indices[i];
    if (index + numPaletteEntries > numColorRecords) {
      return false;
    }
  }

  // The additional fields in CPALv1 are not checked here; the harfbuzz code
  // handles reading them safely.

  unsigned int colrLength;
  const COLRHeader* colr =
      reinterpret_cast<const COLRHeader*>(hb_blob_get_data(aCOLR, &colrLength));
  if (!colr || colrLength < sizeof(COLRHeader)) {
    return false;
  }

  if (uint16_t(colr->version) == 1) {
    return StaticPrefs::gfx_font_rendering_colr_v1_enabled() &&
           colrLength >= sizeof(COLRv1Header) &&
           reinterpret_cast<const COLRv1Header*>(colr)->Validate(colrLength);
  }

  if (uint16_t(colr->version) != 0) {
    // We only support version 1 (above) or version 0 headers.
    return false;
  }

  return colr->Validate(colrLength);
}

const COLRFonts::GlyphLayers* COLRFonts::GetGlyphLayers(hb_blob_t* aCOLR,
                                                        uint32_t aGlyphId) {
  unsigned int length;
  const COLRHeader* colr =
      reinterpret_cast<const COLRHeader*>(hb_blob_get_data(aCOLR, &length));
  // This should never be called unless we have checked that the COLR table is
  // structurally valid, so it will be safe to read the header fields.
  MOZ_RELEASE_ASSERT(colr && length >= sizeof(COLRHeader), "bad COLR table!");
  auto compareBaseGlyph = [](const void* key, const void* data) -> int {
    uint32_t glyphId = (uint32_t)(uintptr_t)key;
    const auto* baseGlyph = reinterpret_cast<const BaseGlyphRecord*>(data);
    uint32_t baseGlyphId = uint16_t(baseGlyph->glyphId);
    if (baseGlyphId == glyphId) {
      return 0;
    }
    return baseGlyphId > glyphId ? -1 : 1;
  };
  return reinterpret_cast<const GlyphLayers*>(
      bsearch((void*)(uintptr_t)aGlyphId, colr->GetBaseGlyphRecords(),
              uint16_t(colr->numBaseGlyphRecords), sizeof(BaseGlyphRecord),
              compareBaseGlyph));
}

bool COLRFonts::PaintGlyphLayers(
    hb_blob_t* aCOLR, hb_face_t* aFace, const GlyphLayers* aLayers,
    DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
    ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
    const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors) {
  const auto* glyphRecord = reinterpret_cast<const BaseGlyphRecord*>(aLayers);
  // Default to opaque rendering (non-webrender applies alpha with a layer)
  float alpha = 1.0;
  if (aTextDrawer) {
    // defaultColor is the one that comes from CSS, so it has transparency info.
    bool hasComplexTransparency =
        0.0 < aCurrentColor.a && aCurrentColor.a < 1.0;
    if (hasComplexTransparency && uint16_t(glyphRecord->numLayers) > 1) {
      // WebRender doesn't support drawing multi-layer transparent color-glyphs,
      // as it requires compositing all the layers before applying transparency.
      // (pretend to succeed, output doesn't matter, we will emit a blob)
      aTextDrawer->FoundUnsupportedFeature();
      return true;
    }

    // If we get here, then either alpha is 0 or 1, or there's only one layer
    // which shouldn't have composition issues. In all of these cases, applying
    // transparency directly to the glyph should work perfectly fine.
    //
    // Note that we must still emit completely transparent emoji, because they
    // might be wrapped in a shadow that uses the text run's glyphs.
    alpha = aCurrentColor.a;
  }

  unsigned int length;
  const COLRHeader* colr =
      reinterpret_cast<const COLRHeader*>(hb_blob_get_data(aCOLR, &length));
  PaintState state{{colr},
                   aColors->Elements(),
                   aDrawTarget,
                   aScaledFont,
                   nullptr,  // variations not needed
                   aDrawOptions,
                   length,
                   aCurrentColor,
                   0.0,  // fontUnitsToPixels not needed
                   uint16_t(aColors->Length()),
                   0,
                   nullptr};
  return glyphRecord->Paint(state, alpha, aPoint);
}

const COLRFonts::GlyphPaintGraph* COLRFonts::GetGlyphPaintGraph(
    hb_blob_t* aCOLR, uint32_t aGlyphId) {
  if (!StaticPrefs::gfx_font_rendering_colr_v1_enabled()) {
    return nullptr;
  }
  unsigned int blobLength;
  const auto* colr =
      reinterpret_cast<const COLRHeader*>(hb_blob_get_data(aCOLR, &blobLength));
  MOZ_ASSERT(colr, "Cannot get COLR raw data");
  MOZ_ASSERT(blobLength >= sizeof(COLRHeader), "COLR data too small");

  uint16_t version = colr->version;
  if (version == 1) {
    MOZ_ASSERT(blobLength >= sizeof(COLRv1Header), "COLRv1 data too small");
    const auto* colrv1 = reinterpret_cast<const COLRv1Header*>(colr);
    return reinterpret_cast<const GlyphPaintGraph*>(
        colrv1->GetBaseGlyphPaint(aGlyphId));
  }

  return nullptr;
}

bool COLRFonts::PaintGlyphGraph(
    hb_blob_t* aCOLR, hb_font_t* aFont, const GlyphPaintGraph* aPaintGraph,
    DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
    ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
    const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors,
    uint32_t aGlyphId, float aFontUnitsToPixels) {
  if (aTextDrawer) {
    // Currently we always punt to a blob for COLRv1 glyphs.
    aTextDrawer->FoundUnsupportedFeature();
    return true;
  }

  unsigned int coordCount;
  const int* coords = hb_font_get_var_coords_normalized(aFont, &coordCount);

  AutoTArray<uint32_t, 32> visitedOffsets;
  PaintState state{{nullptr},
                   aColors->Elements(),
                   aDrawTarget,
                   aScaledFont,
                   coords,
                   aDrawOptions,
                   hb_blob_get_length(aCOLR),
                   aCurrentColor,
                   aFontUnitsToPixels,
                   uint16_t(aColors->Length()),
                   uint16_t(coordCount),
                   &visitedOffsets};
  state.mHeader.v1 =
      reinterpret_cast<const COLRv1Header*>(hb_blob_get_data(aCOLR, nullptr));
  AutoRestoreTransform saveTransform(aDrawTarget);
  aDrawTarget->ConcatTransform(Matrix::Translation(aPoint));
  return PaintColrGlyph::DoPaint(
      state, reinterpret_cast<const BaseGlyphPaintRecord*>(aPaintGraph),
      aGlyphId, nullptr);
}

Rect COLRFonts::GetColorGlyphBounds(hb_blob_t* aCOLR, hb_font_t* aFont,
                                    uint32_t aGlyphId, DrawTarget* aDrawTarget,
                                    ScaledFont* aScaledFont,
                                    float aFontUnitsToPixels) {
  unsigned int coordCount;
  const int* coords = hb_font_get_var_coords_normalized(aFont, &coordCount);

  AutoTArray<uint32_t, 32> visitedOffsets;
  PaintState state{{nullptr},
                   nullptr,  // palette is not needed
                   aDrawTarget,
                   aScaledFont,
                   coords,
                   DrawOptions(),
                   hb_blob_get_length(aCOLR),
                   sRGBColor(),
                   aFontUnitsToPixels,
                   0,  // numPaletteEntries
                   uint16_t(coordCount),
                   &visitedOffsets};
  state.mHeader.v1 =
      reinterpret_cast<const COLRv1Header*>(hb_blob_get_data(aCOLR, nullptr));
  MOZ_ASSERT(uint16_t(state.mHeader.v1->base.version) == 1);
  // If a clip rect is provided, return this as the glyph bounds.
  const auto* clipList = state.mHeader.v1->clipList();
  if (clipList) {
    const auto* clip = clipList->GetClip(aGlyphId);
    if (clip) {
      return clip->GetRect(state);
    }
  }
  // Otherwise, compute bounds by walking the paint graph.
  const auto* base = state.mHeader.v1->GetBaseGlyphPaint(aGlyphId);
  if (base) {
    return DispatchGetBounds(
        state, state.mHeader.v1->baseGlyphListOffset + base->paintOffset);
  }
  return Rect();
}

uint16_t COLRFonts::GetColrTableVersion(hb_blob_t* aCOLR) {
  unsigned int blobLength;
  const auto* colr =
      reinterpret_cast<const COLRHeader*>(hb_blob_get_data(aCOLR, &blobLength));
  MOZ_ASSERT(colr, "Cannot get COLR raw data");
  MOZ_ASSERT(blobLength >= sizeof(COLRHeader), "COLR data too small");
  return colr->version;
}

nsTArray<sRGBColor> COLRFonts::CreateColorPalette(
    hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet,
    nsAtom* aFontPalette, const nsACString& aFamilyName) {
  // Find the base color palette to use, if there are multiple available;
  // default to first in the font, if nothing matches what is requested.
  unsigned int paletteIndex = 0;
  unsigned int count = hb_ot_color_palette_get_count(aFace);
  MOZ_ASSERT(count > 0, "No palettes? Font should have been rejected!");

  const FontPaletteValueSet::PaletteValues* fpv = nullptr;
  if (aFontPalette && aFontPalette != nsGkAtoms::normal &&
      (count > 1 || aPaletteValueSet)) {
    auto findPalette = [&](hb_ot_color_palette_flags_t flag) -> unsigned int {
      MOZ_ASSERT(flag != HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
      for (unsigned int i = 0; i < count; ++i) {
        if (hb_ot_color_palette_get_flags(aFace, i) & flag) {
          return i;
        }
      }
      return 0;
    };

    if (aFontPalette == nsGkAtoms::light) {
      paletteIndex =
          findPalette(HB_OT_COLOR_PALETTE_FLAG_USABLE_WITH_LIGHT_BACKGROUND);
    } else if (aFontPalette == nsGkAtoms::dark) {
      paletteIndex =
          findPalette(HB_OT_COLOR_PALETTE_FLAG_USABLE_WITH_DARK_BACKGROUND);
    } else {
      if (aPaletteValueSet) {
        if ((fpv = aPaletteValueSet->Lookup(aFontPalette, aFamilyName))) {
          if (fpv->mBasePalette >= 0 && fpv->mBasePalette < int32_t(count)) {
            paletteIndex = fpv->mBasePalette;
          } else if (fpv->mBasePalette ==
                     FontPaletteValueSet::PaletteValues::kLight) {
            paletteIndex = findPalette(
                HB_OT_COLOR_PALETTE_FLAG_USABLE_WITH_LIGHT_BACKGROUND);
          } else if (fpv->mBasePalette ==
                     FontPaletteValueSet::PaletteValues::kDark) {
            paletteIndex = findPalette(
                HB_OT_COLOR_PALETTE_FLAG_USABLE_WITH_DARK_BACKGROUND);
          }
        }
      }
    }
  }

  // Collect the palette colors and convert them to sRGBColor values.
  count =
      hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, nullptr, nullptr);
  nsTArray<hb_color_t> colors;
  colors.SetLength(count);
  hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, &count,
                                 colors.Elements());

  nsTArray<sRGBColor> palette;
  palette.SetCapacity(count);
  for (const auto c : colors) {
    palette.AppendElement(
        sRGBColor(hb_color_get_red(c) / 255.0, hb_color_get_green(c) / 255.0,
                  hb_color_get_blue(c) / 255.0, hb_color_get_alpha(c) / 255.0));
  }

  // Apply @font-palette-values overrides, if present.
  if (fpv) {
    for (const auto overrideColor : fpv->mOverrides) {
      if (overrideColor.mIndex < palette.Length()) {
        palette[overrideColor.mIndex] = overrideColor.mColor;
      }
    }
  }

  return palette;
}

const FontPaletteValueSet::PaletteValues* FontPaletteValueSet::Lookup(
    nsAtom* aName, const nsACString& aFamily) const {
  nsAutoCString family(aFamily);
  ToLowerCase(family);
  if (const HashEntry* entry =
          mValues.GetEntry(PaletteHashKey(aName, family))) {
    return &entry->mValue;
  }
  return nullptr;
}

FontPaletteValueSet::PaletteValues* FontPaletteValueSet::Insert(
    nsAtom* aName, const nsACString& aFamily) {
  nsAutoCString family(aFamily);
  ToLowerCase(family);
  HashEntry* entry = mValues.PutEntry(PaletteHashKey(aName, family));
  return &entry->mValue;
}

}  // end namespace mozilla::gfx