forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCOLRFonts.cpp
2626 lines (2399 loc) · 87.2 KB
/
COLRFonts.cpp
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
/* -*- 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);
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(nsDefaultComparator<GradientStop, GradientStop>());
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 {
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])) {
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 {
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) {