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
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkString.h"
#include "SkEndian.h"
#include "SkFontHost.h"
#include "SkDescriptor.h"
#include "SkAdvancedTypefaceMetrics.h"
#include "SkStream.h"
#include "SkThread.h"
#include "SkTypeface_win.h"
#include "SkTypefaceCache.h"
#include "SkUtils.h"
#ifdef WIN32
#include "windows.h"
#include "tchar.h"
#include "Usp10.h"
// define this in your Makefile or .gyp to enforce AA requests
// which GDI ignores at small sizes. This flag guarantees AA
// for rotated text, regardless of GDI's notions.
//#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
// client3d has to undefine this for now
#define CAN_USE_LOGFONT_NAME
static bool isLCD(const SkScalerContext::Rec& rec) {
return SkMask::kLCD16_Format == rec.fMaskFormat ||
SkMask::kLCD32_Format == rec.fMaskFormat;
}
static bool bothZero(SkScalar a, SkScalar b) {
return 0 == a && 0 == b;
}
// returns false if there is any non-90-rotation or skew
static bool isAxisAligned(const SkScalerContext::Rec& rec) {
return 0 == rec.fPreSkewX &&
(bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
}
static bool needToRenderWithSkia(const SkScalerContext::Rec& rec) {
#ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
// What we really want to catch is when GDI will ignore the AA request and give
// us BW instead. Smallish rotated text is one heuristic, so this code is just
// an approximation. We shouldn't need to do this for larger sizes, but at those
// sizes, the quality difference gets less and less between our general
// scanconverter and GDI's.
if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) {
return true;
}
#endif
// false means allow GDI to generate the bits
return false;
}
using namespace skia_advanced_typeface_metrics_utils;
static const uint16_t BUFFERSIZE = (16384 - 32);
static uint8_t glyphbuf[BUFFERSIZE];
// Give 1MB font cache budget
#define FONT_CACHE_MEMORY_BUDGET (1024 * 1024)
/**
* Since LOGFONT wants its textsize as an int, and we support fractional sizes,
* and since we have a cache of LOGFONTs for our tyepfaces, we always set the
* lfHeight to a canonical size, and then we use the 2x2 matrix to achieve the
* actual requested size.
*/
static const int gCanonicalTextSize = 64;
static void make_canonical(LOGFONT* lf) {
lf->lfHeight = -gCanonicalTextSize;
lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY;
lf->lfCharSet = DEFAULT_CHARSET;
// lf->lfClipPrecision = 64;
}
static SkTypeface::Style getStyle(const LOGFONT& lf) {
unsigned style = 0;
if (lf.lfWeight >= FW_BOLD) {
style |= SkTypeface::kBold;
}
if (lf.lfItalic) {
style |= SkTypeface::kItalic;
}
return (SkTypeface::Style)style;
}
static void setStyle(LOGFONT* lf, SkTypeface::Style style) {
lf->lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL ;
lf->lfItalic = ((style & SkTypeface::kItalic) != 0);
}
static inline FIXED SkFixedToFIXED(SkFixed x) {
return *(FIXED*)(&x);
}
static inline FIXED SkScalarToFIXED(SkScalar x) {
return SkFixedToFIXED(SkScalarToFixed(x));
}
static unsigned calculateGlyphCount(HDC hdc) {
// The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes.
const DWORD maxpTag =
SkEndian_SwapBE32(SkSetFourByteTag('m', 'a', 'x', 'p'));
uint16_t glyphs;
if (GetFontData(hdc, maxpTag, 4, &glyphs, sizeof(glyphs)) != GDI_ERROR) {
return SkEndian_SwapBE16(glyphs);
}
// Binary search for glyph count.
static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
int32_t max = SK_MaxU16 + 1;
int32_t min = 0;
GLYPHMETRICS gm;
while (min < max) {
int32_t mid = min + ((max - min) / 2);
if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0,
NULL, &mat2) == GDI_ERROR) {
max = mid;
} else {
min = mid + 1;
}
}
SkASSERT(min == max);
return min;
}
static SkTypeface::Style GetFontStyle(const LOGFONT& lf) {
int style = SkTypeface::kNormal;
if (lf.lfWeight == FW_SEMIBOLD || lf.lfWeight == FW_DEMIBOLD || lf.lfWeight == FW_BOLD)
style |= SkTypeface::kBold;
if (lf.lfItalic)
style |= SkTypeface::kItalic;
return (SkTypeface::Style)style;
}
class LogFontTypeface : public SkTypeface {
public:
LogFontTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf) :
SkTypeface(style, fontID, false), fLogFont(lf) {}
LOGFONT fLogFont;
static LogFontTypeface* Create(const LOGFONT& lf) {
SkTypeface::Style style = GetFontStyle(lf);
SkFontID fontID = SkTypefaceCache::NewFontID();
return new LogFontTypeface(style, fontID, lf);
}
};
static const LOGFONT& get_default_font() {
static LOGFONT gDefaultFont;
return gDefaultFont;
}
static bool FindByLogFont(SkTypeface* face, SkTypeface::Style requestedStyle, void* ctx) {
LogFontTypeface* lface = reinterpret_cast<LogFontTypeface*>(face);
const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx);
return getStyle(lface->fLogFont) == requestedStyle &&
!memcmp(&lface->fLogFont, lf, sizeof(LOGFONT));
}
/**
* This guy is public. It first searches the cache, and if a match is not found,
* it creates a new face.
*/
SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) {
LOGFONT lf = origLF;
make_canonical(&lf);
SkTypeface* face = SkTypefaceCache::FindByProc(FindByLogFont, &lf);
if (face) {
face->ref();
} else {
face = LogFontTypeface::Create(lf);
SkTypefaceCache::Add(face, getStyle(lf));
}
return face;
}
/**
* This guy is public
*/
void SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) {
if (NULL == face) {
*lf = get_default_font();
} else {
*lf = ((const LogFontTypeface*)face)->fLogFont;
}
}
SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
// Zero means that we don't have any fallback fonts for this fontID.
// This function is implemented on Android, but doesn't have much
// meaning here.
return 0;
}
static void GetLogFontByID(SkFontID fontID, LOGFONT* lf) {
LogFontTypeface* face = (LogFontTypeface*)SkTypefaceCache::FindByID(fontID);
if (face) {
*lf = face->fLogFont;
} else {
sk_bzero(lf, sizeof(LOGFONT));
}
}
// Construct Glyph to Unicode table.
// Unicode code points that require conjugate pairs in utf16 are not
// supported.
// TODO(arthurhsu): Add support for conjugate pairs. It looks like that may
// require parsing the TTF cmap table (platform 4, encoding 12) directly instead
// of calling GetFontUnicodeRange().
static void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount,
SkTDArray<SkUnichar>* glyphToUnicode) {
DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, NULL);
if (!glyphSetBufferSize) {
return;
}
SkAutoTDeleteArray<BYTE> glyphSetBuffer(new BYTE[glyphSetBufferSize]);
GLYPHSET* glyphSet =
reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get());
if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) {
return;
}
glyphToUnicode->setCount(glyphCount);
memset(glyphToUnicode->begin(), 0, glyphCount * sizeof(SkUnichar));
for (DWORD i = 0; i < glyphSet->cRanges; ++i) {
// There is no guarantee that within a Unicode range, the corresponding
// glyph id in a font file are continuous. So, even if we have ranges,
// we can't just use the first and last entry of the range to compute
// result. We need to enumerate them one by one.
int count = glyphSet->ranges[i].cGlyphs;
SkAutoTArray<WCHAR> chars(count + 1);
chars[count] = 0; // termintate string
SkAutoTArray<WORD> glyph(count);
for (USHORT j = 0; j < count; ++j) {
chars[j] = glyphSet->ranges[i].wcLow + j;
}
GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(),
GGI_MARK_NONEXISTING_GLYPHS);
// If the glyph ID is valid, and the glyph is not mapped, then we will
// fill in the char id into the vector. If the glyph is mapped already,
// skip it.
// TODO(arthurhsu): better improve this. e.g. Get all used char ids from
// font cache, then generate this mapping table from there. It's
// unlikely to have collisions since glyph reuse happens mostly for
// different Unicode pages.
for (USHORT j = 0; j < count; ++j) {
if (glyph[j] != 0xffff && glyph[j] < glyphCount &&
(*glyphToUnicode)[glyph[j]] == 0) {
(*glyphToUnicode)[glyph[j]] = chars[j];
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
static int alignTo32(int n) {
return (n + 31) & ~31;
}
struct MyBitmapInfo : public BITMAPINFO {
RGBQUAD fMoreSpaceForColors[1];
};
class HDCOffscreen {
public:
HDCOffscreen() {
fFont = 0;
fDC = 0;
fBM = 0;
fBits = NULL;
fWidth = fHeight = 0;
fIsBW = false;
fColor = kInvalid_Color;
}
~HDCOffscreen() {
if (fDC) {
DeleteDC(fDC);
}
if (fBM) {
DeleteObject(fBM);
}
}
void init(HFONT font, const XFORM& xform) {
fFont = font;
fXform = xform;
}
const void* draw(const SkGlyph&, bool isBW, bool whiteFG,
size_t* srcRBPtr);
private:
HDC fDC;
HBITMAP fBM;
HFONT fFont;
XFORM fXform;
void* fBits; // points into fBM
COLORREF fColor;
int fWidth;
int fHeight;
bool fIsBW;
enum {
// will always trigger us to reset the color, since we
// should only store 0 or 0xFFFFFF
kInvalid_Color = 12345
};
};
const void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW,
bool whiteFG, size_t* srcRBPtr) {
if (0 == fDC) {
fDC = CreateCompatibleDC(0);
if (0 == fDC) {
return NULL;
}
SetGraphicsMode(fDC, GM_ADVANCED);
SetBkMode(fDC, TRANSPARENT);
SetTextAlign(fDC, TA_LEFT | TA_BASELINE);
SelectObject(fDC, fFont);
fColor = kInvalid_Color;
}
if (fBM && (fIsBW != isBW || fWidth < glyph.fWidth || fHeight < glyph.fHeight)) {
DeleteObject(fBM);
fBM = 0;
}
fIsBW = isBW;
COLORREF color = 0;
if (fIsBW || whiteFG) {
color = 0xFFFFFF;
}
if (fColor != color) {
fColor = color;
COLORREF prev = SetTextColor(fDC, color);
SkASSERT(prev != CLR_INVALID);
}
fWidth = SkMax32(fWidth, glyph.fWidth);
fHeight = SkMax32(fHeight, glyph.fHeight);
int biWidth = isBW ? alignTo32(fWidth) : fWidth;
if (0 == fBM) {
MyBitmapInfo info;
sk_bzero(&info, sizeof(info));
if (isBW) {
RGBQUAD blackQuad = { 0, 0, 0, 0 };
RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 };
info.bmiColors[0] = blackQuad;
info.bmiColors[1] = whiteQuad;
}
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biWidth = biWidth;
info.bmiHeader.biHeight = fHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = isBW ? 1 : 32;
info.bmiHeader.biCompression = BI_RGB;
if (isBW) {
info.bmiHeader.biClrUsed = 2;
}
fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0);
if (0 == fBM) {
return NULL;
}
SelectObject(fDC, fBM);
}
// erase
size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2);
size_t size = fHeight * srcRB;
memset(fBits, ~color & 0xFF, size);
XFORM xform = fXform;
xform.eDx = (float)-glyph.fLeft;
xform.eDy = (float)-glyph.fTop;
SetWorldTransform(fDC, &xform);
uint16_t glyphID = glyph.getGlyphID();
#if defined(UNICODE)
ExtTextOut(fDC, 0, 0, ETO_GLYPH_INDEX, NULL, (LPCWSTR)&glyphID, 1, NULL);
#else
ExtTextOut(fDC, 0, 0, ETO_GLYPH_INDEX, NULL, (LPCSTR)&glyphID, 1, NULL);
#endif
GdiFlush();
*srcRBPtr = srcRB;
// offset to the start of the image
return (const char*)fBits + (fHeight - glyph.fHeight) * srcRB;
}
//////////////////////////////////////////////////////////////////////////////////////
class SkScalerContext_Windows : public SkScalerContext {
public:
SkScalerContext_Windows(const SkDescriptor* desc);
virtual ~SkScalerContext_Windows();
protected:
virtual unsigned generateGlyphCount();
virtual uint16_t generateCharToGlyph(SkUnichar uni);
virtual void generateAdvance(SkGlyph* glyph);
virtual void generateMetrics(SkGlyph* glyph);
virtual void generateImage(const SkGlyph& glyph);
virtual void generatePath(const SkGlyph& glyph, SkPath* path);
virtual void generateFontMetrics(SkPaint::FontMetrics* mX, SkPaint::FontMetrics* mY);
private:
HDCOffscreen fOffscreen;
SkScalar fScale; // to get from canonical size to real size
MAT2 fMat22;
XFORM fXform;
HDC fDDC;
HFONT fSavefont;
HFONT fFont;
SCRIPT_CACHE fSC;
int fGlyphCount;
HFONT fHiResFont;
MAT2 fMat22Identity;
SkMatrix fHiResMatrix;
};
static float mul2float(SkScalar a, SkScalar b) {
return SkScalarToFloat(SkScalarMul(a, b));
}
static FIXED float2FIXED(float x) {
return SkFixedToFIXED(SkFloatToFixed(x));
}
static SkMutex gFTMutex;
#define HIRES_TEXTSIZE 2048
#define HIRES_SHIFT 11
static inline SkFixed HiResToFixed(int value) {
return value << (16 - HIRES_SHIFT);
}
static bool needHiResMetrics(const SkScalar mat[2][2]) {
return mat[1][0] || mat[0][1];
}
static BYTE compute_quality(const SkScalerContext::Rec& rec) {
switch (rec.fMaskFormat) {
case SkMask::kBW_Format:
return NONANTIALIASED_QUALITY;
case SkMask::kLCD16_Format:
case SkMask::kLCD32_Format:
return CLEARTYPE_QUALITY;
default:
return ANTIALIASED_QUALITY;
}
}
SkScalerContext_Windows::SkScalerContext_Windows(const SkDescriptor* desc)
: SkScalerContext(desc), fDDC(0), fFont(0), fSavefont(0), fSC(0)
, fGlyphCount(-1) {
SkAutoMutexAcquire ac(gFTMutex);
fScale = fRec.fTextSize / gCanonicalTextSize;
fXform.eM11 = mul2float(fScale, fRec.fPost2x2[0][0]);
fXform.eM12 = mul2float(fScale, fRec.fPost2x2[1][0]);
fXform.eM21 = mul2float(fScale, fRec.fPost2x2[0][1]);
fXform.eM22 = mul2float(fScale, fRec.fPost2x2[1][1]);
fXform.eDx = 0;
fXform.eDy = 0;
fMat22.eM11 = float2FIXED(fXform.eM11);
fMat22.eM12 = float2FIXED(fXform.eM12);
fMat22.eM21 = float2FIXED(-fXform.eM21);
fMat22.eM22 = float2FIXED(-fXform.eM22);
fDDC = ::CreateCompatibleDC(NULL);
SetGraphicsMode(fDDC, GM_ADVANCED);
SetBkMode(fDDC, TRANSPARENT);
// Scaling by the DPI is inconsistent with how Skia draws elsewhere
//SkScalar height = -(fRec.fTextSize * GetDeviceCaps(ddc, LOGPIXELSY) / 72);
LOGFONT lf;
GetLogFontByID(fRec.fFontID, &lf);
lf.lfHeight = -gCanonicalTextSize;
lf.lfQuality = compute_quality(fRec);
fFont = CreateFontIndirect(&lf);
// if we're rotated, or want fractional widths, create a hires font
fHiResFont = 0;
if (needHiResMetrics(fRec.fPost2x2) || (fRec.fFlags & kSubpixelPositioning_Flag)) {
lf.lfHeight = -HIRES_TEXTSIZE;
fHiResFont = CreateFontIndirect(&lf);
fMat22Identity.eM11 = fMat22Identity.eM22 = SkFixedToFIXED(SK_Fixed1);
fMat22Identity.eM12 = fMat22Identity.eM21 = SkFixedToFIXED(0);
// construct a matrix to go from HIRES logical units to our device units
fRec.getSingleMatrix(&fHiResMatrix);
SkScalar scale = SkScalarInvert(SkIntToScalar(HIRES_TEXTSIZE));
fHiResMatrix.preScale(scale, scale);
}
fSavefont = (HFONT)SelectObject(fDDC, fFont);
if (needToRenderWithSkia(fRec)) {
this->forceGenerateImageFromPath();
}
fOffscreen.init(fFont, fXform);
}
SkScalerContext_Windows::~SkScalerContext_Windows() {
if (fDDC) {
::SelectObject(fDDC, fSavefont);
::DeleteDC(fDDC);
}
if (fFont) {
::DeleteObject(fFont);
}
if (fHiResFont) {
::DeleteObject(fHiResFont);
}
if (fSC) {
::ScriptFreeCache(&fSC);
}
}
unsigned SkScalerContext_Windows::generateGlyphCount() {
if (fGlyphCount < 0) {
fGlyphCount = calculateGlyphCount(fDDC);
}
return fGlyphCount;
}
uint16_t SkScalerContext_Windows::generateCharToGlyph(SkUnichar uni) {
uint16_t index = 0;
WCHAR c[2];
// TODO(ctguil): Support characters that generate more than one glyph.
if (SkUTF16_FromUnichar(uni, (uint16_t*)c) == 1) {
// Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0.
SkAssertResult(GetGlyphIndicesW(fDDC, c, 1, &index, 0));
} else {
// Use uniscribe to detemine glyph index for non-BMP characters.
// Need to add extra item to SCRIPT_ITEM to work around a bug in older
// windows versions. https://bugzilla.mozilla.org/show_bug.cgi?id=366643
SCRIPT_ITEM si[2 + 1];
int items;
SkAssertResult(
SUCCEEDED(ScriptItemize(c, 2, 2, NULL, NULL, si, &items)));
WORD log[2];
SCRIPT_VISATTR vsa;
int glyphs;
SkAssertResult(SUCCEEDED(ScriptShape(
fDDC, &fSC, c, 2, 1, &si[0].a, &index, log, &vsa, &glyphs)));
}
return index;
}
void SkScalerContext_Windows::generateAdvance(SkGlyph* glyph) {
this->generateMetrics(glyph);
}
void SkScalerContext_Windows::generateMetrics(SkGlyph* glyph) {
SkASSERT(fDDC);
GLYPHMETRICS gm;
sk_bzero(&gm, sizeof(gm));
glyph->fRsbDelta = 0;
glyph->fLsbDelta = 0;
// Note: need to use GGO_GRAY8_BITMAP instead of GGO_METRICS because GGO_METRICS returns a smaller
// BlackBlox; we need the bigger one in case we need the image. fAdvance is the same.
uint32_t ret = GetGlyphOutlineW(fDDC, glyph->getGlyphID(0), GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
if (GDI_ERROR != ret) {
if (ret == 0) {
// for white space, ret is zero and gmBlackBoxX, gmBlackBoxY are 1 incorrectly!
gm.gmBlackBoxX = gm.gmBlackBoxY = 0;
}
glyph->fWidth = gm.gmBlackBoxX;
glyph->fHeight = gm.gmBlackBoxY;
glyph->fTop = SkToS16(gm.gmptGlyphOrigin.y - gm.gmBlackBoxY);
glyph->fLeft = SkToS16(gm.gmptGlyphOrigin.x);
glyph->fAdvanceX = SkIntToFixed(gm.gmCellIncX);
glyph->fAdvanceY = -SkIntToFixed(gm.gmCellIncY);
// we outset in all dimensions, since the image may bleed outside
// of the computed bounds returned by GetGlyphOutline.
// This was deduced by trial and error for small text (e.g. 8pt), so there
// maybe a more precise way to make this adjustment...
//
// This test shows us clipping the tops of some of the CJK fonts unless we
// increase the top of the box by 2, hence the height by 4. This seems to
// correspond to an embedded bitmap font, but not sure.
// LayoutTests/fast/text/backslash-to-yen-sign-euc.html
//
if (glyph->fWidth) { // don't outset an empty glyph
glyph->fWidth += 4;
glyph->fHeight += 4;
glyph->fTop -= 2;
glyph->fLeft -= 2;
}
if (fHiResFont) {
SelectObject(fDDC, fHiResFont);
sk_bzero(&gm, sizeof(gm));
ret = GetGlyphOutlineW(fDDC, glyph->getGlyphID(0), GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22Identity);
if (GDI_ERROR != ret) {
SkPoint advance;
fHiResMatrix.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance);
glyph->fAdvanceX = SkScalarToFixed(advance.fX);
glyph->fAdvanceY = SkScalarToFixed(advance.fY);
}
SelectObject(fDDC, fFont);
}
} else {
glyph->fWidth = 0;
}
}
void SkScalerContext_Windows::generateFontMetrics(SkPaint::FontMetrics* mx, SkPaint::FontMetrics* my) {
// Note: This code was borrowed from generateLineHeight, which has a note
// stating that it may be incorrect.
if (!(mx || my))
return;
SkASSERT(fDDC);
OUTLINETEXTMETRIC otm;
uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
if (sizeof(otm) != ret) {
return;
}
if (mx) {
mx->fTop = -fScale * otm.otmTextMetrics.tmAscent;
mx->fAscent = -fScale * otm.otmAscent;
mx->fDescent = -fScale * otm.otmDescent;
mx->fBottom = fScale * otm.otmTextMetrics.tmDescent;
mx->fLeading = fScale * (otm.otmTextMetrics.tmInternalLeading
+ otm.otmTextMetrics.tmExternalLeading);
}
if (my) {
my->fTop = -fScale * otm.otmTextMetrics.tmAscent;
my->fAscent = -fScale * otm.otmAscent;
my->fDescent = -fScale * otm.otmDescent;
my->fBottom = fScale * otm.otmTextMetrics.tmDescent;
my->fLeading = fScale * (otm.otmTextMetrics.tmInternalLeading
+ otm.otmTextMetrics.tmExternalLeading);
}
}
#include "SkColorPriv.h"
// always packed xxRRGGBB
typedef uint32_t SkGdiRGB;
// gdi's bitmap is upside-down, so we reverse dst walking in Y
// whenever we copy it into skia's buffer
static inline uint8_t rgb_to_a8(SkGdiRGB rgb) {
// can pick any component (low 3 bytes), since we're grayscale
return rgb & 0xFF;
}
static inline uint16_t rgb_to_lcd16(SkGdiRGB rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
return SkPackRGB16(SkR32ToR16(r), SkG32ToG16(g), SkB32ToB16(b));
}
static inline SkPMColor rgb_to_lcd32(SkGdiRGB rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
int a = SkMax32(r, SkMax32(g, b));
return SkPackARGB32(a, r, g, b);
}
// Is this GDI color neither black nor white? If so, we have to keep this
// image as is, rather than smashing it down to a BW mask.
//
// returns int instead of bool, since we don't want/have to pay to convert
// the zero/non-zero value into a bool
static int is_not_black_or_white(SkGdiRGB c) {
// same as (but faster than)
// c &= 0x00FFFFFF;
// return 0 == c || 0x00FFFFFF == c;
return (c + (c & 1)) & 0x00FFFFFF;
}
static bool is_rgb_really_bw(const SkGdiRGB* src, int width, int height, int srcRB) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (is_not_black_or_white(src[x])) {
return false;
}
}
src = (const SkGdiRGB*)((const char*)src + srcRB);
}
return true;
}
static void rgb_to_bw(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
const SkGlyph& glyph, int32_t xorMask) {
const int width = glyph.fWidth;
const size_t dstRB = (width + 7) >> 3;
uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
int byteCount = width >> 3;
int bitCount = width & 7;
// adjust srcRB to skip the values in our byteCount loop,
// since we increment src locally there
srcRB -= byteCount * 8 * sizeof(SkGdiRGB);
for (int y = 0; y < glyph.fHeight; ++y) {
if (byteCount > 0) {
unsigned byte = 0;
for (int i = 0; i < byteCount; ++i) {
byte |= (src[0] ^ xorMask) & (1 << 7);
byte |= (src[1] ^ xorMask) & (1 << 6);
byte |= (src[2] ^ xorMask) & (1 << 5);
byte |= (src[3] ^ xorMask) & (1 << 4);
byte |= (src[4] ^ xorMask) & (1 << 3);
byte |= (src[5] ^ xorMask) & (1 << 2);
byte |= (src[6] ^ xorMask) & (1 << 1);
byte |= (src[7] ^ xorMask) & (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] ^ xorMask) & mask;
mask >>= 1;
}
dst[byteCount] = byte;
}
src = (const SkGdiRGB*)((const char*)src + srcRB);
dst -= dstRB;
}
}
static void rgb_to_a8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
const SkGlyph& glyph, int32_t xorMask) {
const size_t dstRB = glyph.rowBytes();
const int width = glyph.fWidth;
uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
for (int y = 0; y < glyph.fHeight; y++) {
for (int i = 0; i < width; i++) {
dst[i] = rgb_to_a8(src[i] ^ xorMask);
}
src = (const SkGdiRGB*)((const char*)src + srcRB);
dst -= dstRB;
}
}
static void rgb_to_lcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
const SkGlyph& glyph, int32_t xorMask) {
const size_t dstRB = glyph.rowBytes();
const int width = glyph.fWidth;
uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
for (int y = 0; y < glyph.fHeight; y++) {
for (int i = 0; i < width; i++) {
dst[i] = rgb_to_lcd16(src[i] ^ xorMask);
}
src = (const SkGdiRGB*)((const char*)src + srcRB);
dst = (uint16_t*)((char*)dst - dstRB);
}
}
static void rgb_to_lcd32(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
const SkGlyph& glyph, int32_t xorMask) {
const size_t dstRB = glyph.rowBytes();
const int width = glyph.fWidth;
SkPMColor* SK_RESTRICT dst = (SkPMColor*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
for (int y = 0; y < glyph.fHeight; y++) {
for (int i = 0; i < width; i++) {
dst[i] = rgb_to_lcd32(src[i] ^ xorMask);
}
src = (const SkGdiRGB*)((const char*)src + srcRB);
dst = (SkPMColor*)((char*)dst - dstRB);
}
}
void SkScalerContext_Windows::generateImage(const SkGlyph& glyph) {
SkAutoMutexAcquire ac(gFTMutex);
SkASSERT(fDDC);
const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat;
const bool isAA = !isLCD(fRec);
const bool isWhite = SkToBool(fRec.fFlags & SkScalerContext::kGammaForWhite_Flag);
const uint32_t rgbXOR = isWhite ? 0 : ~0;
size_t srcRB;
const void* bits = fOffscreen.draw(glyph, isBW, isWhite, &srcRB);
if (!bits) {
sk_bzero(glyph.fImage, glyph.computeImageSize());
return;
}
int width = glyph.fWidth;
size_t dstRB = glyph.rowBytes();
if (isBW) {
const uint8_t* src = (const uint8_t*)bits;
uint8_t* dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
for (int y = 0; y < glyph.fHeight; y++) {
memcpy(dst, src, dstRB);
src += srcRB;
dst -= dstRB;
}
} else if (isAA) {
const SkGdiRGB* src = (const SkGdiRGB*)bits;
#if 0 // can't do this (yet) since caller may really want gray8 for maskfilters
if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) {
rgb_to_bw(src, srcRB, glyph, rgbXOR);
((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format;
} else
#endif
{
rgb_to_a8(src, srcRB, glyph, rgbXOR);
}
} else { // LCD16
const SkGdiRGB* src = (const SkGdiRGB*)bits;
if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) {
rgb_to_bw(src, srcRB, glyph, rgbXOR);
((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format;
} else {
if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
rgb_to_lcd16(src, srcRB, glyph, rgbXOR);
} else {
SkASSERT(SkMask::kLCD32_Format == glyph.fMaskFormat);
rgb_to_lcd32(src, srcRB, glyph, rgbXOR);
}
}
}
}
void SkScalerContext_Windows::generatePath(const SkGlyph& glyph, SkPath* path) {
SkAutoMutexAcquire ac(gFTMutex);
SkASSERT(&glyph && path);
SkASSERT(fDDC);
path->reset();
#if 0
char buf[1024];
sprintf(buf, "generatePath: id:%d, w=%d, h=%d, font:%s,fh:%d\n", glyph.fID, glyph.fWidth, glyph.fHeight, lf.lfFaceName, lf.lfHeight);
OutputDebugString(buf);
#endif
GLYPHMETRICS gm;
uint32_t total_size = GetGlyphOutlineW(fDDC, glyph.fID, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, BUFFERSIZE, glyphbuf, &fMat22);
if (GDI_ERROR != total_size) {
const uint8_t* cur_glyph = glyphbuf;
const uint8_t* end_glyph = glyphbuf + total_size;
while(cur_glyph < end_glyph) {
const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph;
const uint8_t* end_poly = cur_glyph + th->cb;
const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER);
path->moveTo(SkFixedToScalar(*(SkFixed*)(&th->pfxStart.x)), SkFixedToScalar(*(SkFixed*)(&th->pfxStart.y)));
while(cur_poly < end_poly) {
const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly;
if (pc->wType == TT_PRIM_LINE) {
for (uint16_t i = 0; i < pc->cpfx; i++) {
path->lineTo(SkFixedToScalar(*(SkFixed*)(&pc->apfx[i].x)), SkFixedToScalar(*(SkFixed*)(&pc->apfx[i].y)));
}
}
if (pc->wType == TT_PRIM_QSPLINE) {
for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline
POINTFX pnt_b = pc->apfx[u]; // B is always the current point
POINTFX pnt_c = pc->apfx[u+1];
if (u < pc->cpfx - 2) { // If not on last spline, compute C
pnt_c.x = SkFixedToFIXED(SkFixedAve(*(SkFixed*)(&pnt_b.x), *(SkFixed*)(&pnt_c.x)));
pnt_c.y = SkFixedToFIXED(SkFixedAve(*(SkFixed*)(&pnt_b.y), *(SkFixed*)(&pnt_c.y)));
}
path->quadTo(SkFixedToScalar(*(SkFixed*)(&pnt_b.x)), SkFixedToScalar(*(SkFixed*)(&pnt_b.y)), SkFixedToScalar(*(SkFixed*)(&pnt_c.x)), SkFixedToScalar(*(SkFixed*)(&pnt_c.y)));
}
}
cur_poly += sizeof(uint16_t) * 2 + sizeof(POINTFX) * pc->cpfx;
}
cur_glyph += th->cb;
path->close();
}
}
else {
SkASSERT(false);
}
//char buf[1024];
//sprintf(buf, "generatePath: count:%d\n", count);
//OutputDebugString(buf);
}
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
SkASSERT(!"SkFontHost::Serialize unimplemented");
}
SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
SkASSERT(!"SkFontHost::Deserialize unimplemented");
return NULL;
}
static bool getWidthAdvance(HDC hdc, int gId, int16_t* advance) {
// Initialize the MAT2 structure to the identify transformation matrix.
static const MAT2 mat2 = {SkScalarToFIXED(1), SkScalarToFIXED(0),
SkScalarToFIXED(0), SkScalarToFIXED(1)};
int flags = GGO_METRICS | GGO_GLYPH_INDEX;
GLYPHMETRICS gm;
if (GDI_ERROR == GetGlyphOutline(hdc, gId, flags, &gm, 0, NULL, &mat2)) {
return false;
}
SkASSERT(advance);
*advance = gm.gmCellIncX;
return true;
}
// static
SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
uint32_t fontID,
SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
const uint32_t* glyphIDs,
uint32_t glyphIDsCount) {
LOGFONT lf;
GetLogFontByID(fontID, &lf);
SkAdvancedTypefaceMetrics* info = NULL;
HDC hdc = CreateCompatibleDC(NULL);
HFONT font = CreateFontIndirect(&lf);
HFONT savefont = (HFONT)SelectObject(hdc, font);
HFONT designFont = NULL;
// To request design units, create a logical font whose height is specified
// as unitsPerEm.
OUTLINETEXTMETRIC otm;
if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm) ||
!GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) {
goto Error;
}
lf.lfHeight = -SkToS32(otm.otmEMSquare);
designFont = CreateFontIndirect(&lf);
SelectObject(hdc, designFont);
if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) {
goto Error;
}
const unsigned glyphCount = calculateGlyphCount(hdc);
info = new SkAdvancedTypefaceMetrics;
info->fEmSize = otm.otmEMSquare;
info->fMultiMaster = false;
info->fLastGlyphID = SkToU16(glyphCount - 1);
info->fStyle = 0;
#ifdef UNICODE
// Get the buffer size needed first.
size_t str_len = WideCharToMultiByte(CP_UTF8, 0, lf.lfFaceName, -1, NULL,
0, NULL, NULL);
// Allocate a buffer (str_len already has terminating null accounted for).
char *familyName = new char[str_len];
// Now actually convert the string.
WideCharToMultiByte(CP_UTF8, 0, lf.lfFaceName, -1, familyName, str_len,
NULL, NULL);
info->fFontName.set(familyName);
delete [] familyName;
#else
info->fFontName.set(lf.lfFaceName);
#endif
if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
populate_glyph_to_unicode(hdc, glyphCount, &(info->fGlyphToUnicode));
}
if (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE) {
info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
} else {
info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
info->fItalicAngle = 0;
info->fAscent = 0;
info->fDescent = 0;
info->fStemV = 0;
info->fCapHeight = 0;
info->fBBox = SkIRect::MakeEmpty();
return info;
}
// If this bit is clear the font is a fixed pitch font.
if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
}
if (otm.otmTextMetrics.tmItalic) {
info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
}
// Setting symbolic style by default for now.
info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) {
info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
} else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) {
info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
}
// The main italic angle of the font, in tenths of a degree counterclockwise
// from vertical.
info->fItalicAngle = otm.otmItalicAngle / 10;
info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent);
info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent);
// TODO(ctguil): Use alternate cap height calculation.
// MSDN says otmsCapEmHeight is not support but it is returning a value on
// my Win7 box.
info->fCapHeight = otm.otmsCapEmHeight;
info->fBBox =
SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top,
otm.otmrcFontBox.right, otm.otmrcFontBox.bottom);
// Figure out a good guess for StemV - Min width of i, I, !, 1.
// This probably isn't very good with an italic font.
int16_t min_width = SHRT_MAX;
info->fStemV = 0;
char stem_chars[] = {'i', 'I', '!', '1'};
for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
ABC abcWidths;
if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
int16_t width = abcWidths.abcB;
if (width > 0 && width < min_width) {
min_width = width;
info->fStemV = min_width;
}
}
}
// If bit 1 is set, the font may not be embedded in a document.
// If bit 1 is clear, the font can be embedded.
// If bit 2 is set, the embedding is read-only.
if (otm.otmfsType & 0x1) {
info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
} else if (perGlyphInfo &
SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) {
appendRange(&info->fGlyphWidths, 0);
info->fGlyphWidths->fAdvance.append(1, &min_width);
finishRange(info->fGlyphWidths.get(), 0,
SkAdvancedTypefaceMetrics::WidthRange::kDefault);
} else {
info->fGlyphWidths.reset(
getAdvanceData(hdc,
glyphCount,
glyphIDs,
glyphIDsCount,
&getWidthAdvance));
}
}
Error:
SelectObject(hdc, savefont);
DeleteObject(designFont);
DeleteObject(font);
DeleteDC(hdc);
return info;
}
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
//Should not be used on Windows, keep linker happy
SkASSERT(false);
return SkCreateTypefaceFromLOGFONT(get_default_font());
}
SkStream* SkFontHost::OpenStream(SkFontID uniqueID) {
const DWORD kTTCTag =
SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f'));
LOGFONT lf;
GetLogFontByID(uniqueID, &lf);
HDC hdc = ::CreateCompatibleDC(NULL);
HFONT font = CreateFontIndirect(&lf);
HFONT savefont = (HFONT)SelectObject(hdc, font);
SkMemoryStream* stream = NULL;
DWORD tables[2] = {kTTCTag, 0};
for (int i = 0; i < SK_ARRAY_COUNT(tables); i++) {
size_t bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0);
if (bufferSize != GDI_ERROR) {
stream = new SkMemoryStream(bufferSize);
if (GetFontData(hdc, tables[i], 0, (void*)stream->getMemoryBase(),
bufferSize)) {
break;
} else {
delete stream;
stream = NULL;
}
}
}
SelectObject(hdc, savefont);
DeleteObject(font);
DeleteDC(hdc);
return stream;
}
SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
return SkNEW_ARGS(SkScalerContext_Windows, (desc));
}
/** Return the closest matching typeface given either an existing family
(specified by a typeface in that family) or by a familyName, and a
requested style.
1) If familyFace is null, use famillyName.
2) If famillyName is null, use familyFace.
3) If both are null, return the default font that best matches style
This MUST not return NULL.
*/
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[],
const void* data, size_t bytelength,
SkTypeface::Style style) {
LOGFONT lf;
if (NULL == familyFace && NULL == familyName) {
lf = get_default_font();
} else if (familyFace) {
LogFontTypeface* face = (LogFontTypeface*)familyFace;
lf = face->fLogFont;
} else {
memset(&lf, 0, sizeof(LOGFONT));
#ifdef UNICODE
// Get the buffer size needed first.
size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName,
-1, NULL, 0);
// Allocate a buffer (str_len already has terminating null
// accounted for).
wchar_t *wideFamilyName = new wchar_t[str_len];
// Now actually convert the string.
::MultiByteToWideChar(CP_UTF8, 0, familyName, -1,
wideFamilyName, str_len);
::wcsncpy(lf.lfFaceName, wideFamilyName, LF_FACESIZE);
delete [] wideFamilyName;
#else
::strncpy(lf.lfFaceName, familyName, LF_FACESIZE);
#endif
lf.lfFaceName[LF_FACESIZE-1] = '\0';
}
setStyle(&lf, style);
return SkCreateTypefaceFromLOGFONT(lf);
}
size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) {
if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET)
return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET;
else
return 0; // nothing to do
}
SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
printf("SkFontHost::CreateTypefaceFromFile unimplemented");
return NULL;
}
void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag |
SkScalerContext::kAutohinting_Flag |
SkScalerContext::kEmbeddedBitmapText_Flag |
SkScalerContext::kEmbolden_Flag |
SkScalerContext::kLCD_BGROrder_Flag |
SkScalerContext::kLCD_Vertical_Flag;
rec->fFlags &= ~flagsWeDontSupport;
SkPaint::Hinting h = rec->getHinting();
// I think we can support no-hinting, if we get hires outlines and just
// use skia to rasterize into a gray-scale mask...
#if 0
switch (h) {
case SkPaint::kNo_Hinting:
case SkPaint::kSlight_Hinting:
h = SkPaint::kNo_Hinting;
break;
case SkPaint::kNormal_Hinting:
case SkPaint::kFull_Hinting:
h = SkPaint::kNormal_Hinting;
break;
default:
SkASSERT(!"unknown hinting");
}
#else
h = SkPaint::kNormal_Hinting;
#endif
rec->setHinting(h);
// turn this off since GDI might turn A8 into BW! Need a bigger fix.
#if 0
// Disable LCD when rotated, since GDI's output is ugly
if (isLCD(*rec) && !isAxisAligned(*rec)) {
rec->fMaskFormat = SkMask::kA8_Format;
}
#endif
#if 0
if (SkMask::kLCD16_Format == rec->fMaskFormat) {
rec->fMaskFormat = SkMask::kLCD32_Format;
}
#endif
if (!isLCD(*rec)) {
rec->fFlags &= ~(SkScalerContext::kGammaForBlack_Flag |
SkScalerContext::kGammaForWhite_Flag);
}
}
//////////////////////////////////////////////////////////////////////////
void SkFontHost::GetGammaTables(const uint8_t* tables[2]) {
tables[0] = NULL;
tables[1] = NULL;
}
// If the luminance is <= this value, then apply the black gamma table
#define BLACK_GAMMA_THRESHOLD 0x40
// If the luminance is >= this value, then apply the white gamma table
#define WHITE_GAMMA_THRESHOLD 0xA0
int SkFontHost::ComputeGammaFlag(const SkPaint& paint) {
if (paint.getShader() == NULL && paint.getColorFilter() == NULL) {
SkColor c = paint.getColor();
int r = SkColorGetR(c);
int g = SkColorGetG(c);
int b = SkColorGetB(c);
int luminance = (r * 2 + g * 5 + b) >> 3;
#if 0 // we basically default to black, so don't need to return it
if (luminance <= BLACK_GAMMA_THRESHOLD) {
return SkScalerContext::kGammaForBlack_Flag;
}
#endif
if (luminance >= WHITE_GAMMA_THRESHOLD) {
return SkScalerContext::kGammaForWhite_Flag;
}
}
return 0;
}
#endif // WIN32
|