1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkTypes.h"
9 #if defined(SK_BUILD_FOR_WIN32)
10
11 // SkTypes will include Windows.h, which will pull in all of the GDI defines.
12 // GDI #defines GetGlyphIndices to GetGlyphIndicesA or GetGlyphIndicesW, but
13 // IDWriteFontFace has a method called GetGlyphIndices. Since this file does
14 // not use GDI, undefing GetGlyphIndices makes things less confusing.
15 #undef GetGlyphIndices
16
17 #include "SkDWrite.h"
18 #include "SkDWriteFontFileStream.h"
19 #include "SkFontDescriptor.h"
20 #include "SkFontStream.h"
21 #include "SkOTTable_head.h"
22 #include "SkOTTable_hhea.h"
23 #include "SkOTTable_OS_2.h"
24 #include "SkOTTable_post.h"
25 #include "SkOTUtils.h"
26 #include "SkScalerContext.h"
27 #include "SkScalerContext_win_dw.h"
28 #include "SkTypeface_win_dw.h"
29 #include "SkUtils.h"
30
onGetFamilyName(SkString * familyName) const31 void DWriteFontTypeface::onGetFamilyName(SkString* familyName) const {
32 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
33 HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
34
35 sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, familyName);
36 }
37
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocalStream) const38 void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
39 bool* isLocalStream) const {
40 // Get the family name.
41 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
42 HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
43
44 SkString utf8FamilyName;
45 sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, &utf8FamilyName);
46
47 desc->setFamilyName(utf8FamilyName.c_str());
48 desc->setStyle(this->fontStyle());
49 *isLocalStream = SkToBool(fDWriteFontFileLoader.get());
50 }
51
next_utf8(const void ** chars)52 static SkUnichar next_utf8(const void** chars) {
53 return SkUTF8_NextUnichar((const char**)chars);
54 }
55
next_utf16(const void ** chars)56 static SkUnichar next_utf16(const void** chars) {
57 return SkUTF16_NextUnichar((const uint16_t**)chars);
58 }
59
next_utf32(const void ** chars)60 static SkUnichar next_utf32(const void** chars) {
61 const SkUnichar** uniChars = (const SkUnichar**)chars;
62 SkUnichar uni = **uniChars;
63 *uniChars += 1;
64 return uni;
65 }
66
67 typedef SkUnichar (*EncodingProc)(const void**);
68
find_encoding_proc(SkTypeface::Encoding enc)69 static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
70 static const EncodingProc gProcs[] = {
71 next_utf8, next_utf16, next_utf32
72 };
73 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
74 return gProcs[enc];
75 }
76
onCharsToGlyphs(const void * chars,Encoding encoding,uint16_t glyphs[],int glyphCount) const77 int DWriteFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
78 uint16_t glyphs[], int glyphCount) const
79 {
80 if (nullptr == glyphs) {
81 EncodingProc next_ucs4_proc = find_encoding_proc(encoding);
82 for (int i = 0; i < glyphCount; ++i) {
83 const SkUnichar c = next_ucs4_proc(&chars);
84 BOOL exists;
85 fDWriteFont->HasCharacter(c, &exists);
86 if (!exists) {
87 return i;
88 }
89 }
90 return glyphCount;
91 }
92
93 switch (encoding) {
94 case SkTypeface::kUTF8_Encoding:
95 case SkTypeface::kUTF16_Encoding: {
96 static const int scratchCount = 256;
97 UINT32 scratch[scratchCount];
98 EncodingProc next_ucs4_proc = find_encoding_proc(encoding);
99 for (int baseGlyph = 0; baseGlyph < glyphCount; baseGlyph += scratchCount) {
100 int glyphsLeft = glyphCount - baseGlyph;
101 int limit = SkTMin(glyphsLeft, scratchCount);
102 for (int i = 0; i < limit; ++i) {
103 scratch[i] = next_ucs4_proc(&chars);
104 }
105 fDWriteFontFace->GetGlyphIndices(scratch, limit, &glyphs[baseGlyph]);
106 }
107 break;
108 }
109 case SkTypeface::kUTF32_Encoding: {
110 const UINT32* utf32 = reinterpret_cast<const UINT32*>(chars);
111 fDWriteFontFace->GetGlyphIndices(utf32, glyphCount, glyphs);
112 break;
113 }
114 default:
115 SK_ABORT("Invalid Text Encoding");
116 }
117
118 for (int i = 0; i < glyphCount; ++i) {
119 if (0 == glyphs[i]) {
120 return i;
121 }
122 }
123 return glyphCount;
124 }
125
onCountGlyphs() const126 int DWriteFontTypeface::onCountGlyphs() const {
127 return fDWriteFontFace->GetGlyphCount();
128 }
129
onGetUPEM() const130 int DWriteFontTypeface::onGetUPEM() const {
131 DWRITE_FONT_METRICS metrics;
132 fDWriteFontFace->GetMetrics(&metrics);
133 return metrics.designUnitsPerEm;
134 }
135
136 class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStrings {
137 public:
138 /** Takes ownership of the IDWriteLocalizedStrings. */
LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings * strings)139 explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* strings)
140 : fIndex(0), fStrings(strings)
141 { }
142
next(SkTypeface::LocalizedString * localizedString)143 bool next(SkTypeface::LocalizedString* localizedString) override {
144 if (fIndex >= fStrings->GetCount()) {
145 return false;
146 }
147
148 // String
149 UINT32 stringLen;
150 HRBM(fStrings->GetStringLength(fIndex, &stringLen), "Could not get string length.");
151
152 SkSMallocWCHAR wString(stringLen+1);
153 HRBM(fStrings->GetString(fIndex, wString.get(), stringLen+1), "Could not get string.");
154
155 HRB(sk_wchar_to_skstring(wString.get(), stringLen, &localizedString->fString));
156
157 // Locale
158 UINT32 localeLen;
159 HRBM(fStrings->GetLocaleNameLength(fIndex, &localeLen), "Could not get locale length.");
160
161 SkSMallocWCHAR wLocale(localeLen+1);
162 HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLen+1), "Could not get locale.");
163
164 HRB(sk_wchar_to_skstring(wLocale.get(), localeLen, &localizedString->fLanguage));
165
166 ++fIndex;
167 return true;
168 }
169
170 private:
171 UINT32 fIndex;
172 SkTScopedComPtr<IDWriteLocalizedStrings> fStrings;
173 };
174
onCreateFamilyNameIterator() const175 SkTypeface::LocalizedStrings* DWriteFontTypeface::onCreateFamilyNameIterator() const {
176 SkTypeface::LocalizedStrings* nameIter =
177 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
178 if (nullptr == nameIter) {
179 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
180 HRNM(fDWriteFontFamily->GetFamilyNames(&familyNames), "Could not obtain family names.");
181 nameIter = new LocalizedStrings_IDWriteLocalizedStrings(familyNames.release());
182 }
183 return nameIter;
184 }
185
onGetTableTags(SkFontTableTag tags[]) const186 int DWriteFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
187 DWRITE_FONT_FACE_TYPE type = fDWriteFontFace->GetType();
188 if (type != DWRITE_FONT_FACE_TYPE_CFF &&
189 type != DWRITE_FONT_FACE_TYPE_TRUETYPE &&
190 type != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION)
191 {
192 return 0;
193 }
194
195 int ttcIndex;
196 std::unique_ptr<SkStream> stream(this->openStream(&ttcIndex));
197 return stream.get() ? SkFontStream::GetTableTags(stream.get(), ttcIndex, tags) : 0;
198 }
199
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const200 size_t DWriteFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
201 size_t length, void* data) const
202 {
203 AutoDWriteTable table(fDWriteFontFace.get(), SkEndian_SwapBE32(tag));
204 if (!table.fExists) {
205 return 0;
206 }
207
208 if (offset > table.fSize) {
209 return 0;
210 }
211 size_t size = SkTMin(length, table.fSize - offset);
212 if (data) {
213 memcpy(data, table.fData + offset, size);
214 }
215
216 return size;
217 }
218
onOpenStream(int * ttcIndex) const219 SkStreamAsset* DWriteFontTypeface::onOpenStream(int* ttcIndex) const {
220 *ttcIndex = fDWriteFontFace->GetIndex();
221
222 UINT32 numFiles;
223 HRNM(fDWriteFontFace->GetFiles(&numFiles, nullptr),
224 "Could not get number of font files.");
225 if (numFiles != 1) {
226 return nullptr;
227 }
228
229 SkTScopedComPtr<IDWriteFontFile> fontFile;
230 HRNM(fDWriteFontFace->GetFiles(&numFiles, &fontFile), "Could not get font files.");
231
232 const void* fontFileKey;
233 UINT32 fontFileKeySize;
234 HRNM(fontFile->GetReferenceKey(&fontFileKey, &fontFileKeySize),
235 "Could not get font file reference key.");
236
237 SkTScopedComPtr<IDWriteFontFileLoader> fontFileLoader;
238 HRNM(fontFile->GetLoader(&fontFileLoader), "Could not get font file loader.");
239
240 SkTScopedComPtr<IDWriteFontFileStream> fontFileStream;
241 HRNM(fontFileLoader->CreateStreamFromKey(fontFileKey, fontFileKeySize,
242 &fontFileStream),
243 "Could not create font file stream.");
244
245 return new SkDWriteFontFileStream(fontFileStream.get());
246 }
247
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const248 SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
249 const SkDescriptor* desc) const {
250 return new SkScalerContext_DW(sk_ref_sp(const_cast<DWriteFontTypeface*>(this)), effects, desc);
251 }
252
onFilterRec(SkScalerContext::Rec * rec) const253 void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const {
254 if (rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) {
255 rec->fMaskFormat = SkMask::kA8_Format;
256 rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag;
257 }
258
259 unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag |
260 SkScalerContext::kDevKernText_Flag |
261 SkScalerContext::kForceAutohinting_Flag |
262 SkScalerContext::kEmbolden_Flag |
263 SkScalerContext::kLCD_Vertical_Flag;
264 rec->fFlags &= ~flagsWeDontSupport;
265
266 SkPaint::Hinting h = rec->getHinting();
267 // DirectWrite2 allows for hinting to be turned off. Force everything else to normal.
268 if (h != SkPaint::kNo_Hinting || !fFactory2 || !fDWriteFontFace2) {
269 h = SkPaint::kNormal_Hinting;
270 }
271 rec->setHinting(h);
272
273 #if defined(SK_FONT_HOST_USE_SYSTEM_SETTINGS)
274 IDWriteFactory* factory = sk_get_dwrite_factory();
275 if (factory != nullptr) {
276 SkTScopedComPtr<IDWriteRenderingParams> defaultRenderingParams;
277 if (SUCCEEDED(factory->CreateRenderingParams(&defaultRenderingParams))) {
278 float gamma = defaultRenderingParams->GetGamma();
279 rec->setDeviceGamma(gamma);
280 rec->setPaintGamma(gamma);
281
282 rec->setContrast(defaultRenderingParams->GetEnhancedContrast());
283 }
284 }
285 #endif
286 }
287
288 ///////////////////////////////////////////////////////////////////////////////
289 //PDF Support
290
291 // Construct Glyph to Unicode table.
292 // Unicode code points that require conjugate pairs in utf16 are not
293 // supported.
294 // TODO(bungeman): This never does what anyone wants.
295 // What is really wanted is the text to glyphs mapping
populate_glyph_to_unicode(IDWriteFontFace * fontFace,const unsigned glyphCount,SkTDArray<SkUnichar> * glyphToUnicode)296 static void populate_glyph_to_unicode(IDWriteFontFace* fontFace,
297 const unsigned glyphCount,
298 SkTDArray<SkUnichar>* glyphToUnicode) {
299 //Do this like free type instead
300 SkAutoTMalloc<SkUnichar> glyphToUni(glyphCount);
301 int maxGlyph = -1;
302 for (UINT32 c = 0; c < 0x10FFFF; ++c) {
303 UINT16 glyph = 0;
304 HRVM(fontFace->GetGlyphIndices(&c, 1, &glyph),
305 "Failed to get glyph index.");
306 // Intermittent DW bug on Windows 10. See crbug.com/470146.
307 if (glyph >= glyphCount) {
308 return;
309 }
310 if (0 < glyph) {
311 maxGlyph = SkTMax(static_cast<int>(glyph), maxGlyph);
312 glyphToUni[glyph] = c;
313 }
314 }
315
316 SkTDArray<SkUnichar>(glyphToUni, maxGlyph + 1).swap(*glyphToUnicode);
317 }
318
onGetAdvancedTypefaceMetrics(PerGlyphInfo perGlyphInfo,const uint32_t * glyphIDs,uint32_t glyphIDsCount) const319 SkAdvancedTypefaceMetrics* DWriteFontTypeface::onGetAdvancedTypefaceMetrics(
320 PerGlyphInfo perGlyphInfo,
321 const uint32_t* glyphIDs,
322 uint32_t glyphIDsCount) const {
323
324 SkAdvancedTypefaceMetrics* info = nullptr;
325
326 HRESULT hr = S_OK;
327
328 const unsigned glyphCount = fDWriteFontFace->GetGlyphCount();
329
330 DWRITE_FONT_METRICS dwfm;
331 fDWriteFontFace->GetMetrics(&dwfm);
332
333 info = new SkAdvancedTypefaceMetrics;
334
335 info->fAscent = SkToS16(dwfm.ascent);
336 info->fDescent = SkToS16(dwfm.descent);
337 info->fCapHeight = SkToS16(dwfm.capHeight);
338
339 // SkAdvancedTypefaceMetrics::fFontName is in theory supposed to be
340 // the PostScript name of the font. However, due to the way it is currently
341 // used, it must actually be a family name.
342 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
343 hr = fDWriteFontFamily->GetFamilyNames(&familyNames);
344
345 UINT32 familyNameLen;
346 hr = familyNames->GetStringLength(0, &familyNameLen);
347
348 SkSMallocWCHAR familyName(familyNameLen+1);
349 hr = familyNames->GetString(0, familyName.get(), familyNameLen+1);
350
351 hr = sk_wchar_to_skstring(familyName.get(), familyNameLen, &info->fFontName);
352
353 if (perGlyphInfo & kToUnicode_PerGlyphInfo) {
354 populate_glyph_to_unicode(fDWriteFontFace.get(), glyphCount, &(info->fGlyphToUnicode));
355 }
356
357 DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType();
358 if (fontType != DWRITE_FONT_FACE_TYPE_TRUETYPE &&
359 fontType != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION)
360 {
361 return info;
362 }
363
364 // Simulated fonts aren't really TrueType fonts.
365 if (fDWriteFontFace->GetSimulations() == DWRITE_FONT_SIMULATIONS_NONE) {
366 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
367 }
368
369 AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get());
370 AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get());
371 AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get());
372 AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get());
373 if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Table.fExists) {
374 return info;
375 }
376
377 //There exist CJK fonts which set the IsFixedPitch and Monospace bits,
378 //but have full width, latin half-width, and half-width kana.
379 bool fixedWidth = (postTable->isFixedPitch &&
380 (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics)));
381 //Monospace
382 if (fixedWidth) {
383 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
384 }
385 //Italic
386 if (os2Table->version.v0.fsSelection.field.Italic) {
387 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
388 }
389 //Serif
390 using SerifStyle = SkPanose::Data::TextAndDisplay::SerifStyle;
391 SerifStyle serifStyle = os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle;
392 if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.panose.bFamilyType) {
393 if (SerifStyle::Cove == serifStyle ||
394 SerifStyle::ObtuseCove == serifStyle ||
395 SerifStyle::SquareCove == serifStyle ||
396 SerifStyle::ObtuseSquareCove == serifStyle ||
397 SerifStyle::Square == serifStyle ||
398 SerifStyle::Thin == serifStyle ||
399 SerifStyle::Bone == serifStyle ||
400 SerifStyle::Exaggerated == serifStyle ||
401 SerifStyle::Triangle == serifStyle)
402 {
403 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
404 }
405 //Script
406 } else if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType) {
407 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
408 }
409
410 info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16;
411
412 info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMin),
413 (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMax),
414 (int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMax),
415 (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMin));
416 return info;
417 }
418 #endif//defined(SK_BUILD_FOR_WIN32)
419