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
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkTypes.h"
#if defined(SK_BUILD_FOR_WIN32)
#undef GetGlyphIndices
#include "SkDraw.h"
#include "SkDWrite.h"
#include "SkDWriteGeometrySink.h"
#include "SkEndian.h"
#include "SkGlyph.h"
#include "SkHRESULT.h"
#include "SkMaskGamma.h"
#include "SkMatrix22.h"
#include "SkMutex.h"
#include "SkOTTable_EBLC.h"
#include "SkOTTable_EBSC.h"
#include "SkOTTable_gasp.h"
#include "SkOTTable_maxp.h"
#include "SkPath.h"
#include "SkRasterClip.h"
#include "SkScalerContext.h"
#include "SkScalerContext_win_dw.h"
#include "SkSharedMutex.h"
#include "SkTScopedComPtr.h"
#include "SkTypeface_win_dw.h"
#include <dwrite.h>
#include <dwrite_1.h>
/* Note:
* In versions 8 and 8.1 of Windows, some calls in DWrite are not thread safe.
* The DWriteFactoryMutex protects the calls that are problematic.
*/
static SkSharedMutex DWriteFactoryMutex;
typedef SkAutoSharedMutexShared Shared;
static bool isLCD(const SkScalerContext::Rec& rec) {
return SkMask::kLCD16_Format == rec.fMaskFormat;
}
static bool is_hinted(DWriteFontTypeface* typeface) {
SkAutoExclusive l(DWriteFactoryMutex);
AutoTDWriteTable<SkOTTableMaximumProfile> maxp(typeface->fDWriteFontFace.get());
if (!maxp.fExists) {
return false;
}
if (maxp.fSize < sizeof(SkOTTableMaximumProfile::Version::TT)) {
return false;
}
if (maxp->version.version != SkOTTableMaximumProfile::Version::TT::VERSION) {
return false;
}
return (0 != maxp->version.tt.maxSizeOfInstructions);
}
/** A GaspRange is inclusive, [min, max]. */
struct GaspRange {
using Behavior = SkOTTableGridAndScanProcedure::GaspRange::behavior;
GaspRange(int min, int max, int version, Behavior flags)
: fMin(min), fMax(max), fVersion(version), fFlags(flags) { }
int fMin;
int fMax;
int fVersion;
Behavior fFlags;
};
bool get_gasp_range(DWriteFontTypeface* typeface, int size, GaspRange* range) {
AutoTDWriteTable<SkOTTableGridAndScanProcedure> gasp(typeface->fDWriteFontFace.get());
if (!gasp.fExists) {
return false;
}
if (gasp.fSize < sizeof(SkOTTableGridAndScanProcedure)) {
return false;
}
if (gasp->version != SkOTTableGridAndScanProcedure::version0 &&
gasp->version != SkOTTableGridAndScanProcedure::version1)
{
return false;
}
uint16_t numRanges = SkEndianSwap16(gasp->numRanges);
if (numRanges > 1024 ||
gasp.fSize < sizeof(SkOTTableGridAndScanProcedure) +
sizeof(SkOTTableGridAndScanProcedure::GaspRange) * numRanges)
{
return false;
}
const SkOTTableGridAndScanProcedure::GaspRange* rangeTable =
SkTAfter<const SkOTTableGridAndScanProcedure::GaspRange>(gasp.get());
int minPPEM = -1;
for (uint16_t i = 0; i < numRanges; ++i, ++rangeTable) {
int maxPPEM = SkEndianSwap16(rangeTable->maxPPEM);
if (minPPEM < size && size <= maxPPEM) {
range->fMin = minPPEM + 1;
range->fMax = maxPPEM;
range->fVersion = SkEndian_SwapBE16(gasp->version);
range->fFlags = rangeTable->flags;
return true;
}
minPPEM = maxPPEM;
}
return false;
}
/** If the rendering mode for the specified 'size' is gridfit, then place
* the gridfit range into 'range'. Otherwise, leave 'range' alone.
*/
static bool is_gridfit_only(GaspRange::Behavior flags) {
return flags.raw.value == GaspRange::Behavior::Raw::GridfitMask;
}
static bool has_bitmap_strike(DWriteFontTypeface* typeface, GaspRange range) {
SkAutoExclusive l(DWriteFactoryMutex);
{
AutoTDWriteTable<SkOTTableEmbeddedBitmapLocation> eblc(typeface->fDWriteFontFace.get());
if (!eblc.fExists) {
return false;
}
if (eblc.fSize < sizeof(SkOTTableEmbeddedBitmapLocation)) {
return false;
}
if (eblc->version != SkOTTableEmbeddedBitmapLocation::version_initial) {
return false;
}
uint32_t numSizes = SkEndianSwap32(eblc->numSizes);
if (numSizes > 1024 ||
eblc.fSize < sizeof(SkOTTableEmbeddedBitmapLocation) +
sizeof(SkOTTableEmbeddedBitmapLocation::BitmapSizeTable) * numSizes)
{
return false;
}
const SkOTTableEmbeddedBitmapLocation::BitmapSizeTable* sizeTable =
SkTAfter<const SkOTTableEmbeddedBitmapLocation::BitmapSizeTable>(eblc.get());
for (uint32_t i = 0; i < numSizes; ++i, ++sizeTable) {
if (sizeTable->ppemX == sizeTable->ppemY &&
range.fMin <= sizeTable->ppemX && sizeTable->ppemX <= range.fMax)
{
// TODO: determine if we should dig through IndexSubTableArray/IndexSubTable
// to determine the actual number of glyphs with bitmaps.
// TODO: Ensure that the bitmaps actually cover a significant portion of the strike.
// TODO: Ensure that the bitmaps are bi-level?
if (sizeTable->endGlyphIndex >= sizeTable->startGlyphIndex + 3) {
return true;
}
}
}
}
{
AutoTDWriteTable<SkOTTableEmbeddedBitmapScaling> ebsc(typeface->fDWriteFontFace.get());
if (!ebsc.fExists) {
return false;
}
if (ebsc.fSize < sizeof(SkOTTableEmbeddedBitmapScaling)) {
return false;
}
if (ebsc->version != SkOTTableEmbeddedBitmapScaling::version_initial) {
return false;
}
uint32_t numSizes = SkEndianSwap32(ebsc->numSizes);
if (numSizes > 1024 ||
ebsc.fSize < sizeof(SkOTTableEmbeddedBitmapScaling) +
sizeof(SkOTTableEmbeddedBitmapScaling::BitmapScaleTable) * numSizes)
{
return false;
}
const SkOTTableEmbeddedBitmapScaling::BitmapScaleTable* scaleTable =
SkTAfter<const SkOTTableEmbeddedBitmapScaling::BitmapScaleTable>(ebsc.get());
for (uint32_t i = 0; i < numSizes; ++i, ++scaleTable) {
if (scaleTable->ppemX == scaleTable->ppemY &&
range.fMin <= scaleTable->ppemX && scaleTable->ppemX <= range.fMax) {
// EBSC tables are normally only found in bitmap only fonts.
return true;
}
}
}
return false;
}
static bool both_zero(SkScalar a, SkScalar b) {
return 0 == a && 0 == b;
}
// returns false if there is any non-90-rotation or skew
static bool is_axis_aligned(const SkScalerContext::Rec& rec) {
return 0 == rec.fPreSkewX &&
(both_zero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
both_zero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
}
SkScalerContext_DW::SkScalerContext_DW(sk_sp<DWriteFontTypeface> typefaceRef,
const SkScalerContextEffects& effects,
const SkDescriptor* desc)
: SkScalerContext(std::move(typefaceRef), effects, desc)
, fGlyphCount(-1) {
DWriteFontTypeface* typeface = this->getDWriteTypeface();
fIsColorFont = typeface->fFactory2 &&
typeface->fDWriteFontFace2 &&
typeface->fDWriteFontFace2->IsColorFont();
// In general, all glyphs should use NATURAL_SYMMETRIC
// except when bi-level rendering is requested or there are embedded
// bi-level bitmaps (and the embedded bitmap flag is set and no rotation).
//
// DirectWrite's IDWriteFontFace::GetRecommendedRenderingMode does not do
// this. As a result, determine the actual size of the text and then see if
// there are any embedded bi-level bitmaps of that size. If there are, then
// force bitmaps by requesting bi-level rendering.
//
// FreeType allows for separate ppemX and ppemY, but DirectWrite assumes
// square pixels and only uses ppemY. Therefore the transform must track any
// non-uniform x-scale.
//
// Also, rotated glyphs should have the same absolute advance widths as
// horizontal glyphs and the subpixel flag should not affect glyph shapes.
SkVector scale;
fRec.computeMatrices(SkScalerContextRec::kVertical_PreMatrixScale, &scale, &fSkXform);
fXform.m11 = SkScalarToFloat(fSkXform.getScaleX());
fXform.m12 = SkScalarToFloat(fSkXform.getSkewY());
fXform.m21 = SkScalarToFloat(fSkXform.getSkewX());
fXform.m22 = SkScalarToFloat(fSkXform.getScaleY());
fXform.dx = 0;
fXform.dy = 0;
// realTextSize is the actual device size we want (as opposed to the size the user requested).
// gdiTextSize is the size we request when GDI compatible.
// If the scale is negative, this means the matrix will do the flip anyway.
const SkScalar realTextSize = scale.fY;
// Due to floating point math, the lower bits are suspect. Round carefully.
SkScalar gdiTextSize = SkScalarRoundToScalar(realTextSize * 64.0f) / 64.0f;
if (gdiTextSize == 0) {
gdiTextSize = SK_Scalar1;
}
bool bitmapRequested = SkToBool(fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag);
bool treatLikeBitmap = false;
bool axisAlignedBitmap = false;
if (bitmapRequested) {
// When embedded bitmaps are requested, treat the entire range like
// a bitmap strike if the range is gridfit only and contains a bitmap.
int bitmapPPEM = SkScalarTruncToInt(gdiTextSize);
GaspRange range(bitmapPPEM, bitmapPPEM, 0, GaspRange::Behavior());
if (get_gasp_range(typeface, bitmapPPEM, &range)) {
if (!is_gridfit_only(range.fFlags)) {
range = GaspRange(bitmapPPEM, bitmapPPEM, 0, GaspRange::Behavior());
}
}
treatLikeBitmap = has_bitmap_strike(typeface, range);
axisAlignedBitmap = is_axis_aligned(fRec);
}
GaspRange range(0, 0xFFFF, 0, GaspRange::Behavior());
// If the user requested aliased, do so with aliased compatible metrics.
if (SkMask::kBW_Format == fRec.fMaskFormat) {
fTextSizeRender = gdiTextSize;
fRenderingMode = DWRITE_RENDERING_MODE_ALIASED;
fTextureType = DWRITE_TEXTURE_ALIASED_1x1;
fTextSizeMeasure = gdiTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC;
// If we can use a bitmap, use gdi classic rendering and measurement.
// This will not always provide a bitmap, but matches expected behavior.
} else if (treatLikeBitmap && axisAlignedBitmap) {
fTextSizeRender = gdiTextSize;
fRenderingMode = DWRITE_RENDERING_MODE_GDI_CLASSIC;
fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1;
fTextSizeMeasure = gdiTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC;
// If rotated but the horizontal text could have used a bitmap,
// render high quality rotated glyphs but measure using bitmap metrics.
} else if (treatLikeBitmap) {
fTextSizeRender = gdiTextSize;
fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1;
fTextSizeMeasure = gdiTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC;
// If the font has a gasp table version 1, use it to determine symmetric rendering.
} else if (get_gasp_range(typeface, SkScalarRoundToInt(gdiTextSize), &range) &&
range.fVersion >= 1)
{
fTextSizeRender = realTextSize;
fRenderingMode = range.fFlags.field.SymmetricSmoothing
? DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC
: DWRITE_RENDERING_MODE_NATURAL;
fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1;
fTextSizeMeasure = realTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL;
// If the requested size is above 20px or there are no bytecode hints, use symmetric rendering.
} else if (realTextSize > SkIntToScalar(20) || !is_hinted(typeface)) {
fTextSizeRender = realTextSize;
fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1;
fTextSizeMeasure = realTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL;
// Fonts with hints, no gasp or gasp version 0, and below 20px get non-symmetric rendering.
// Often such fonts have hints which were only tested with GDI ClearType classic.
// Some of these fonts rely on drop out control in the y direction in order to be legible.
// Tenor Sans
// https://fonts.google.com/specimen/Tenor+Sans
// Gill Sans W04
// https://cdn.leagueoflegends.com/lolkit/1.1.9/resources/fonts/gill-sans-w04-book.woff
// https://na.leagueoflegends.com/en/news/game-updates/patch/patch-410-notes
// See https://crbug.com/385897
} else {
fTextSizeRender = gdiTextSize;
fRenderingMode = DWRITE_RENDERING_MODE_NATURAL;
fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1;
fTextSizeMeasure = realTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL;
}
// DirectWrite2 allows for grayscale hinting.
fAntiAliasMode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE;
if (typeface->fFactory2 && typeface->fDWriteFontFace2 &&
SkMask::kA8_Format == fRec.fMaskFormat &&
!(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag))
{
// DWRITE_TEXTURE_ALIASED_1x1 is now misnamed, it must also be used with grayscale.
fTextureType = DWRITE_TEXTURE_ALIASED_1x1;
fAntiAliasMode = DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE;
}
// DirectWrite2 allows hinting to be disabled.
fGridFitMode = DWRITE_GRID_FIT_MODE_ENABLED;
if (fRec.getHinting() == SkPaint::kNo_Hinting) {
fGridFitMode = DWRITE_GRID_FIT_MODE_DISABLED;
if (fRenderingMode != DWRITE_RENDERING_MODE_ALIASED) {
fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
}
}
if (this->isSubpixel()) {
fTextSizeMeasure = realTextSize;
fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL;
}
}
SkScalerContext_DW::~SkScalerContext_DW() {
}
unsigned SkScalerContext_DW::generateGlyphCount() {
if (fGlyphCount < 0) {
fGlyphCount = this->getDWriteTypeface()->fDWriteFontFace->GetGlyphCount();
}
return fGlyphCount;
}
uint16_t SkScalerContext_DW::generateCharToGlyph(SkUnichar uni) {
uint16_t index = 0;
UINT32* uniPtr = reinterpret_cast<UINT32*>(&uni);
this->getDWriteTypeface()->fDWriteFontFace->GetGlyphIndices(uniPtr, 1, &index);
return index;
}
void SkScalerContext_DW::generateAdvance(SkGlyph* glyph) {
//Delta is the difference between the right/left side bearing metric
//and where the right/left side bearing ends up after hinting.
//DirectWrite does not provide this information.
glyph->fRsbDelta = 0;
glyph->fLsbDelta = 0;
glyph->fAdvanceX = 0;
glyph->fAdvanceY = 0;
uint16_t glyphId = glyph->getGlyphID();
DWRITE_GLYPH_METRICS gm;
if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode ||
DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode)
{
SkAutoExclusive l(DWriteFactoryMutex);
HRVM(this->getDWriteTypeface()->fDWriteFontFace->GetGdiCompatibleGlyphMetrics(
fTextSizeMeasure,
1.0f, // pixelsPerDip
// This parameter does not act like the lpmat2 parameter to GetGlyphOutlineW.
// If it did then GsA here and G_inv below to mapVectors.
nullptr,
DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode,
&glyphId, 1,
&gm),
"Could not get gdi compatible glyph metrics.");
} else {
SkAutoExclusive l(DWriteFactoryMutex);
HRVM(this->getDWriteTypeface()->fDWriteFontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm),
"Could not get design metrics.");
}
DWRITE_FONT_METRICS dwfm;
{
Shared l(DWriteFactoryMutex);
this->getDWriteTypeface()->fDWriteFontFace->GetMetrics(&dwfm);
}
SkScalar advanceX = fTextSizeMeasure * gm.advanceWidth / dwfm.designUnitsPerEm;
SkVector advance = { advanceX, 0 };
if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode ||
DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode)
{
// DirectWrite produced 'compatible' metrics, but while close,
// the end result is not always an integer as it would be with GDI.
advance.fX = SkScalarRoundToScalar(advance.fX);
}
fSkXform.mapVectors(&advance, 1);
glyph->fAdvanceX = SkScalarToFloat(advance.fX);
glyph->fAdvanceY = SkScalarToFloat(advance.fY);
}
HRESULT SkScalerContext_DW::getBoundingBox(SkGlyph* glyph,
DWRITE_RENDERING_MODE renderingMode,
DWRITE_TEXTURE_TYPE textureType,
RECT* bbox)
{
//Measure raster size.
fXform.dx = SkFixedToFloat(glyph->getSubXFixed());
fXform.dy = SkFixedToFloat(glyph->getSubYFixed());
FLOAT advance = 0;
UINT16 glyphId = glyph->getGlyphID();
DWRITE_GLYPH_OFFSET offset;
offset.advanceOffset = 0.0f;
offset.ascenderOffset = 0.0f;
DWRITE_GLYPH_RUN run;
run.glyphCount = 1;
run.glyphAdvances = &advance;
run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get();
run.fontEmSize = SkScalarToFloat(fTextSizeRender);
run.bidiLevel = 0;
run.glyphIndices = &glyphId;
run.isSideways = FALSE;
run.glyphOffsets = &offset;
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
SkAutoExclusive l(DWriteFactoryMutex);
// IDWriteFactory2::CreateGlyphRunAnalysis is very bad at aliased glyphs.
if (this->getDWriteTypeface()->fFactory2 &&
(fGridFitMode == DWRITE_GRID_FIT_MODE_DISABLED ||
fAntiAliasMode == DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE))
{
HRM(this->getDWriteTypeface()->fFactory2->CreateGlyphRunAnalysis(
&run,
&fXform,
renderingMode,
fMeasuringMode,
fGridFitMode,
fAntiAliasMode,
0.0f, // baselineOriginX,
0.0f, // baselineOriginY,
&glyphRunAnalysis),
"Could not create DW2 glyph run analysis.");
} else {
HRM(this->getDWriteTypeface()->fFactory->CreateGlyphRunAnalysis(&run,
1.0f, // pixelsPerDip,
&fXform,
renderingMode,
fMeasuringMode,
0.0f, // baselineOriginX,
0.0f, // baselineOriginY,
&glyphRunAnalysis),
"Could not create glyph run analysis.");
}
}
{
Shared l(DWriteFactoryMutex);
HRM(glyphRunAnalysis->GetAlphaTextureBounds(textureType, bbox),
"Could not get texture bounds.");
}
return S_OK;
}
/** GetAlphaTextureBounds succeeds but sometimes returns empty bounds like
* { 0x80000000, 0x80000000, 0x80000000, 0x80000000 }
* for small, but not quite zero, sized glyphs.
* Only set as non-empty if the returned bounds are non-empty.
*/
static bool glyph_check_and_set_bounds(SkGlyph* glyph, const RECT& bbox) {
if (bbox.left >= bbox.right || bbox.top >= bbox.bottom) {
return false;
}
glyph->fWidth = SkToU16(bbox.right - bbox.left);
glyph->fHeight = SkToU16(bbox.bottom - bbox.top);
glyph->fLeft = SkToS16(bbox.left);
glyph->fTop = SkToS16(bbox.top);
return true;
}
bool SkScalerContext_DW::isColorGlyph(const SkGlyph& glyph) {
SkTScopedComPtr<IDWriteColorGlyphRunEnumerator> colorLayer;
return getColorGlyphRun(glyph, &colorLayer);
}
bool SkScalerContext_DW::getColorGlyphRun(const SkGlyph& glyph,
IDWriteColorGlyphRunEnumerator** colorGlyph)
{
FLOAT advance = 0;
UINT16 glyphId = glyph.getGlyphID();
DWRITE_GLYPH_OFFSET offset;
offset.advanceOffset = 0.0f;
offset.ascenderOffset = 0.0f;
DWRITE_GLYPH_RUN run;
run.glyphCount = 1;
run.glyphAdvances = &advance;
run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get();
run.fontEmSize = SkScalarToFloat(fTextSizeRender);
run.bidiLevel = 0;
run.glyphIndices = &glyphId;
run.isSideways = FALSE;
run.glyphOffsets = &offset;
HRESULT hr = this->getDWriteTypeface()->fFactory2->TranslateColorGlyphRun(
0, 0, &run, nullptr, fMeasuringMode, &fXform, 0, colorGlyph);
if (hr == DWRITE_E_NOCOLOR) {
return false;
}
HRBM(hr, "Failed to translate color glyph run");
return true;
}
void SkScalerContext_DW::generateMetrics(SkGlyph* glyph) {
glyph->fWidth = 0;
glyph->fHeight = 0;
glyph->fLeft = 0;
glyph->fTop = 0;
this->generateAdvance(glyph);
if (fIsColorFont && isColorGlyph(*glyph)) {
glyph->fMaskFormat = SkMask::kARGB32_Format;
}
RECT bbox;
HRVM(this->getBoundingBox(glyph, fRenderingMode, fTextureType, &bbox),
"Requested bounding box could not be determined.");
if (glyph_check_and_set_bounds(glyph, bbox)) {
return;
}
// GetAlphaTextureBounds succeeds but returns an empty RECT if there are no
// glyphs of the specified texture type. When this happens, try with the
// alternate texture type.
if (DWRITE_TEXTURE_CLEARTYPE_3x1 == fTextureType) {
HRVM(this->getBoundingBox(glyph,
DWRITE_RENDERING_MODE_ALIASED,
DWRITE_TEXTURE_ALIASED_1x1,
&bbox),
"Fallback bounding box could not be determined.");
if (glyph_check_and_set_bounds(glyph, bbox)) {
glyph->fForceBW = 1;
}
}
// TODO: handle the case where a request for DWRITE_TEXTURE_ALIASED_1x1
// fails, and try DWRITE_TEXTURE_CLEARTYPE_3x1.
}
void SkScalerContext_DW::generateFontMetrics(SkPaint::FontMetrics* metrics) {
if (nullptr == metrics) {
return;
}
sk_bzero(metrics, sizeof(*metrics));
DWRITE_FONT_METRICS dwfm;
if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode ||
DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode)
{
this->getDWriteTypeface()->fDWriteFontFace->GetGdiCompatibleMetrics(
fTextSizeRender,
1.0f, // pixelsPerDip
&fXform,
&dwfm);
} else {
this->getDWriteTypeface()->fDWriteFontFace->GetMetrics(&dwfm);
}
SkScalar upem = SkIntToScalar(dwfm.designUnitsPerEm);
metrics->fAscent = -fTextSizeRender * SkIntToScalar(dwfm.ascent) / upem;
metrics->fDescent = fTextSizeRender * SkIntToScalar(dwfm.descent) / upem;
metrics->fLeading = fTextSizeRender * SkIntToScalar(dwfm.lineGap) / upem;
metrics->fXHeight = fTextSizeRender * SkIntToScalar(dwfm.xHeight) / upem;
metrics->fCapHeight = fTextSizeRender * SkIntToScalar(dwfm.capHeight) / upem;
metrics->fUnderlineThickness = fTextSizeRender * SkIntToScalar(dwfm.underlineThickness) / upem;
metrics->fUnderlinePosition = -(fTextSizeRender * SkIntToScalar(dwfm.underlinePosition) / upem);
metrics->fStrikeoutThickness = fTextSizeRender * SkIntToScalar(dwfm.strikethroughThickness) / upem;
metrics->fStrikeoutPosition = -(fTextSizeRender * SkIntToScalar(dwfm.strikethroughPosition) / upem);
metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThicknessIsValid_Flag;
metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
metrics->fFlags |= SkPaint::FontMetrics::kStrikeoutThicknessIsValid_Flag;
metrics->fFlags |= SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag;
if (this->getDWriteTypeface()->fDWriteFontFace1.get()) {
DWRITE_FONT_METRICS1 dwfm1;
this->getDWriteTypeface()->fDWriteFontFace1->GetMetrics(&dwfm1);
metrics->fTop = -fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxTop) / upem;
metrics->fBottom = -fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxBottom) / upem;
metrics->fXMin = fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxLeft) / upem;
metrics->fXMax = fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxRight) / upem;
metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin;
return;
}
AutoTDWriteTable<SkOTTableHead> head(this->getDWriteTypeface()->fDWriteFontFace.get());
if (head.fExists &&
head.fSize >= sizeof(SkOTTableHead) &&
head->version == SkOTTableHead::version1)
{
metrics->fTop = -fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->yMax) / upem;
metrics->fBottom = -fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->yMin) / upem;
metrics->fXMin = fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->xMin) / upem;
metrics->fXMax = fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->xMax) / upem;
metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin;
return;
}
metrics->fTop = metrics->fAscent;
metrics->fBottom = metrics->fDescent;
}
///////////////////////////////////////////////////////////////////////////////
#include "SkColorPriv.h"
static void bilevel_to_bw(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph) {
const int width = glyph.fWidth;
const size_t dstRB = (width + 7) >> 3;
uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage);
int byteCount = width >> 3;
int bitCount = width & 7;
for (int y = 0; y < glyph.fHeight; ++y) {
if (byteCount > 0) {
for (int i = 0; i < byteCount; ++i) {
unsigned byte = 0;
byte |= src[0] & (1 << 7);
byte |= src[1] & (1 << 6);
byte |= src[2] & (1 << 5);
byte |= src[3] & (1 << 4);
byte |= src[4] & (1 << 3);
byte |= src[5] & (1 << 2);
byte |= src[6] & (1 << 1);
byte |= src[7] & (1 << 0);
dst[i] = byte;
src += 8;
}
}
if (bitCount > 0) {
unsigned byte = 0;
unsigned mask = 0x80;
for (int i = 0; i < bitCount; i++) {
byte |= (src[i]) & mask;
mask >>= 1;
}
dst[byteCount] = byte;
}
src += bitCount;
dst += dstRB;
}
}
template<bool APPLY_PREBLEND>
static void grayscale_to_a8(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph,
const uint8_t* table8) {
const size_t dstRB = glyph.rowBytes();
const U16CPU width = glyph.fWidth;
uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage);
for (U16CPU y = 0; y < glyph.fHeight; y++) {
for (U16CPU i = 0; i < width; i++) {
U8CPU a = *(src++);
dst[i] = sk_apply_lut_if<APPLY_PREBLEND>(a, table8);
}
dst = SkTAddOffset<uint8_t>(dst, dstRB);
}
}
template<bool APPLY_PREBLEND>
static void rgb_to_a8(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph, const uint8_t* table8) {
const size_t dstRB = glyph.rowBytes();
const U16CPU width = glyph.fWidth;
uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage);
for (U16CPU y = 0; y < glyph.fHeight; y++) {
for (U16CPU i = 0; i < width; i++) {
U8CPU r = *(src++);
U8CPU g = *(src++);
U8CPU b = *(src++);
dst[i] = sk_apply_lut_if<APPLY_PREBLEND>((r + g + b) / 3, table8);
}
dst = SkTAddOffset<uint8_t>(dst, dstRB);
}
}
template<bool APPLY_PREBLEND, bool RGB>
static void rgb_to_lcd16(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph,
const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
const size_t dstRB = glyph.rowBytes();
const U16CPU width = glyph.fWidth;
uint16_t* SK_RESTRICT dst = static_cast<uint16_t*>(glyph.fImage);
for (U16CPU y = 0; y < glyph.fHeight; y++) {
for (U16CPU i = 0; i < width; i++) {
U8CPU r, g, b;
if (RGB) {
r = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableR);
g = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableG);
b = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableB);
} else {
b = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableB);
g = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableG);
r = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableR);
}
dst[i] = SkPack888ToRGB16(r, g, b);
}
dst = SkTAddOffset<uint16_t>(dst, dstRB);
}
}
const void* SkScalerContext_DW::drawDWMask(const SkGlyph& glyph,
DWRITE_RENDERING_MODE renderingMode,
DWRITE_TEXTURE_TYPE textureType)
{
int sizeNeeded = glyph.fWidth * glyph.fHeight;
if (DWRITE_TEXTURE_CLEARTYPE_3x1 == textureType) {
sizeNeeded *= 3;
}
if (sizeNeeded > fBits.count()) {
fBits.setCount(sizeNeeded);
}
// erase
memset(fBits.begin(), 0, sizeNeeded);
fXform.dx = SkFixedToFloat(glyph.getSubXFixed());
fXform.dy = SkFixedToFloat(glyph.getSubYFixed());
FLOAT advance = 0.0f;
UINT16 index = glyph.getGlyphID();
DWRITE_GLYPH_OFFSET offset;
offset.advanceOffset = 0.0f;
offset.ascenderOffset = 0.0f;
DWRITE_GLYPH_RUN run;
run.glyphCount = 1;
run.glyphAdvances = &advance;
run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get();
run.fontEmSize = SkScalarToFloat(fTextSizeRender);
run.bidiLevel = 0;
run.glyphIndices = &index;
run.isSideways = FALSE;
run.glyphOffsets = &offset;
{
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
SkAutoExclusive l(DWriteFactoryMutex);
// IDWriteFactory2::CreateGlyphRunAnalysis is very bad at aliased glyphs.
if (this->getDWriteTypeface()->fFactory2 &&
(fGridFitMode == DWRITE_GRID_FIT_MODE_DISABLED ||
fAntiAliasMode == DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE))
{
HRNM(this->getDWriteTypeface()->fFactory2->CreateGlyphRunAnalysis(&run,
&fXform,
renderingMode,
fMeasuringMode,
fGridFitMode,
fAntiAliasMode,
0.0f, // baselineOriginX,
0.0f, // baselineOriginY,
&glyphRunAnalysis),
"Could not create DW2 glyph run analysis.");
} else {
HRNM(this->getDWriteTypeface()->fFactory->CreateGlyphRunAnalysis(&run,
1.0f, // pixelsPerDip,
&fXform,
renderingMode,
fMeasuringMode,
0.0f, // baselineOriginX,
0.0f, // baselineOriginY,
&glyphRunAnalysis),
"Could not create glyph run analysis.");
}
}
//NOTE: this assumes that the glyph has already been measured
//with an exact same glyph run analysis.
RECT bbox;
bbox.left = glyph.fLeft;
bbox.top = glyph.fTop;
bbox.right = glyph.fLeft + glyph.fWidth;
bbox.bottom = glyph.fTop + glyph.fHeight;
{
Shared l(DWriteFactoryMutex);
HRNM(glyphRunAnalysis->CreateAlphaTexture(textureType,
&bbox,
fBits.begin(),
sizeNeeded),
"Could not draw mask.");
}
}
return fBits.begin();
}
void SkScalerContext_DW::generateColorGlyphImage(const SkGlyph& glyph) {
SkASSERT(isColorGlyph(glyph));
SkASSERT(glyph.fMaskFormat == SkMask::Format::kARGB32_Format);
memset(glyph.fImage, 0, glyph.computeImageSize());
SkTScopedComPtr<IDWriteColorGlyphRunEnumerator> colorLayers;
getColorGlyphRun(glyph, &colorLayers);
SkASSERT(colorLayers.get());
SkMatrix matrix = fSkXform;
matrix.postTranslate(-SkIntToScalar(glyph.fLeft), -SkIntToScalar(glyph.fTop));
SkRasterClip rc(SkIRect::MakeWH(glyph.fWidth, glyph.fHeight));
SkDraw draw;
draw.fDst = SkPixmap(SkImageInfo::MakeN32(glyph.fWidth, glyph.fHeight, kPremul_SkAlphaType),
glyph.fImage,
glyph.ComputeRowBytes(glyph.fWidth, SkMask::Format::kARGB32_Format));
draw.fMatrix = &matrix;
draw.fRC = &rc;
SkPaint paint;
if (fRenderingMode != DWRITE_RENDERING_MODE_ALIASED) {
paint.setFlags(SkPaint::Flags::kAntiAlias_Flag);
}
BOOL hasNextRun = FALSE;
while (SUCCEEDED(colorLayers->MoveNext(&hasNextRun)) && hasNextRun) {
const DWRITE_COLOR_GLYPH_RUN* colorGlyph;
HRVM(colorLayers->GetCurrentRun(&colorGlyph), "Could not get current color glyph run");
SkColor color;
if (colorGlyph->paletteIndex != 0xffff) {
color = SkColorSetARGB(sk_float_round2int(colorGlyph->runColor.a * 255),
sk_float_round2int(colorGlyph->runColor.r * 255),
sk_float_round2int(colorGlyph->runColor.g * 255),
sk_float_round2int(colorGlyph->runColor.b * 255));
} else {
// If all components of runColor are 0 or (equivalently) paletteIndex is 0xFFFF then
// the 'current brush' is used. fRec.getLuminanceColor() is kinda sorta what is wanted
// here, but not really, it will often be the wrong value because it wan't designed for
// this.
// TODO: implement this fully, bug.skia.org/5788
color = fRec.getLuminanceColor();
}
paint.setColor(color);
SkPath path;
SkTScopedComPtr<IDWriteGeometrySink> geometryToPath;
HRVM(SkDWriteGeometrySink::Create(&path, &geometryToPath),
"Could not create geometry to path converter.");
{
SkAutoExclusive l(DWriteFactoryMutex);
HRVM(colorGlyph->glyphRun.fontFace->GetGlyphRunOutline(
colorGlyph->glyphRun.fontEmSize,
colorGlyph->glyphRun.glyphIndices,
colorGlyph->glyphRun.glyphAdvances,
colorGlyph->glyphRun.glyphOffsets,
colorGlyph->glyphRun.glyphCount,
colorGlyph->glyphRun.isSideways,
colorGlyph->glyphRun.bidiLevel % 2, //rtl
geometryToPath.get()),
"Could not create glyph outline.");
}
draw.drawPath(path, paint, nullptr, true /* pathIsMutable */);
}
}
void SkScalerContext_DW::generateImage(const SkGlyph& glyph) {
//Create the mask.
DWRITE_RENDERING_MODE renderingMode = fRenderingMode;
DWRITE_TEXTURE_TYPE textureType = fTextureType;
if (glyph.fForceBW) {
renderingMode = DWRITE_RENDERING_MODE_ALIASED;
textureType = DWRITE_TEXTURE_ALIASED_1x1;
}
if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
generateColorGlyphImage(glyph);
return;
}
const void* bits = this->drawDWMask(glyph, renderingMode, textureType);
if (!bits) {
sk_bzero(glyph.fImage, glyph.computeImageSize());
return;
}
//Copy the mask into the glyph.
const uint8_t* src = (const uint8_t*)bits;
if (DWRITE_RENDERING_MODE_ALIASED == renderingMode) {
bilevel_to_bw(src, glyph);
const_cast<SkGlyph&>(glyph).fMaskFormat = SkMask::kBW_Format;
} else if (!isLCD(fRec)) {
if (textureType == DWRITE_TEXTURE_ALIASED_1x1) {
if (fPreBlend.isApplicable()) {
grayscale_to_a8<true>(src, glyph, fPreBlend.fG);
} else {
grayscale_to_a8<false>(src, glyph, fPreBlend.fG);
}
} else {
if (fPreBlend.isApplicable()) {
rgb_to_a8<true>(src, glyph, fPreBlend.fG);
} else {
rgb_to_a8<false>(src, glyph, fPreBlend.fG);
}
}
} else {
SkASSERT(SkMask::kLCD16_Format == glyph.fMaskFormat);
if (fPreBlend.isApplicable()) {
if (fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag) {
rgb_to_lcd16<true, false>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
} else {
rgb_to_lcd16<true, true>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
}
} else {
if (fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag) {
rgb_to_lcd16<false, false>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
} else {
rgb_to_lcd16<false, true>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
}
}
}
}
void SkScalerContext_DW::generatePath(SkGlyphID glyph, SkPath* path) {
SkASSERT(path);
path->reset();
SkTScopedComPtr<IDWriteGeometrySink> geometryToPath;
HRVM(SkDWriteGeometrySink::Create(path, &geometryToPath),
"Could not create geometry to path converter.");
UINT16 glyphId = SkTo<UINT16>(glyph);
{
SkAutoExclusive l(DWriteFactoryMutex);
//TODO: convert to<->from DIUs? This would make a difference if hinting.
//It may not be needed, it appears that DirectWrite only hints at em size.
HRVM(this->getDWriteTypeface()->fDWriteFontFace->GetGlyphRunOutline(
SkScalarToFloat(fTextSizeRender),
&glyphId,
nullptr, //advances
nullptr, //offsets
1, //num glyphs
FALSE, //sideways
FALSE, //rtl
geometryToPath.get()),
"Could not create glyph outline.");
}
path->transform(fSkXform);
}
#endif//defined(SK_BUILD_FOR_WIN32)
|