• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/utils/win/SkDWriteNTDDI_VERSION.h"
8 
9 #include "include/core/SkTypes.h"
10 #if defined(SK_BUILD_FOR_WIN)
11 
12 #include "src/core/SkLeanWindows.h"
13 
14 // SkLeanWindows will include Windows.h, which will pull in all of the GDI defines.
15 // GDI #defines GetGlyphIndices to GetGlyphIndicesA or GetGlyphIndicesW, but
16 // IDWriteFontFace has a method called GetGlyphIndices. Since this file does
17 // not use GDI, undefing GetGlyphIndices makes things less confusing.
18 #undef GetGlyphIndices
19 
20 #include "include/core/SkData.h"
21 #include "include/private/SkTo.h"
22 #include "src/core/SkFontDescriptor.h"
23 #include "src/core/SkFontStream.h"
24 #include "src/core/SkScalerContext.h"
25 #include "src/ports/SkScalerContext_win_dw.h"
26 #include "src/ports/SkTypeface_win_dw.h"
27 #include "src/sfnt/SkOTTable_OS_2.h"
28 #include "src/sfnt/SkOTTable_fvar.h"
29 #include "src/sfnt/SkOTTable_head.h"
30 #include "src/sfnt/SkOTTable_hhea.h"
31 #include "src/sfnt/SkOTTable_post.h"
32 #include "src/sfnt/SkOTUtils.h"
33 #include "src/utils/win/SkDWrite.h"
34 #include "src/utils/win/SkDWriteFontFileStream.h"
35 
~Loaders()36 DWriteFontTypeface::Loaders::~Loaders() {
37     // Don't return if any fail, just keep going to free up as much as possible.
38     HRESULT hr;
39 
40     hr = fFactory->UnregisterFontCollectionLoader(fDWriteFontCollectionLoader.get());
41     if (FAILED(hr)) {
42         SK_TRACEHR(hr, "FontCollectionLoader");
43     }
44 
45     hr = fFactory->UnregisterFontFileLoader(fDWriteFontFileLoader.get());
46     if (FAILED(hr)) {
47         SK_TRACEHR(hr, "FontFileLoader");
48     }
49 }
50 
onGetFamilyName(SkString * familyName) const51 void DWriteFontTypeface::onGetFamilyName(SkString* familyName) const {
52     SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
53     HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
54 
55     sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, familyName);
56 }
57 
onGetPostScriptName(SkString * skPostScriptName) const58 bool DWriteFontTypeface::onGetPostScriptName(SkString* skPostScriptName) const {
59     SkString localSkPostScriptName;
60     SkTScopedComPtr<IDWriteLocalizedStrings> postScriptNames;
61     BOOL exists = FALSE;
62     if (FAILED(fDWriteFont->GetInformationalStrings(
63                     DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_NAME,
64                     &postScriptNames,
65                     &exists)) ||
66         !exists ||
67         FAILED(sk_get_locale_string(postScriptNames.get(), nullptr, &localSkPostScriptName)))
68     {
69         return false;
70     }
71     if (skPostScriptName) {
72         *skPostScriptName = localSkPostScriptName;
73     }
74     return true;
75 }
76 
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocalStream) const77 void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
78                                              bool* isLocalStream) const {
79     // Get the family name.
80     SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
81     HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
82 
83     SkString utf8FamilyName;
84     sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, &utf8FamilyName);
85 
86     desc->setFamilyName(utf8FamilyName.c_str());
87     desc->setStyle(this->fontStyle());
88     *isLocalStream = SkToBool(fLoaders);
89 }
90 
onCharsToGlyphs(const SkUnichar * uni,int count,SkGlyphID glyphs[]) const91 void DWriteFontTypeface::onCharsToGlyphs(const SkUnichar* uni, int count,
92                                          SkGlyphID glyphs[]) const {
93     fDWriteFontFace->GetGlyphIndices((const UINT32*)uni, count, glyphs);
94 }
95 
onCountGlyphs() const96 int DWriteFontTypeface::onCountGlyphs() const {
97     return fDWriteFontFace->GetGlyphCount();
98 }
99 
getPostScriptGlyphNames(SkString *) const100 void DWriteFontTypeface::getPostScriptGlyphNames(SkString*) const {}
101 
onGetUPEM() const102 int DWriteFontTypeface::onGetUPEM() const {
103     DWRITE_FONT_METRICS metrics;
104     fDWriteFontFace->GetMetrics(&metrics);
105     return metrics.designUnitsPerEm;
106 }
107 
108 class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStrings {
109 public:
110     /** Takes ownership of the IDWriteLocalizedStrings. */
LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings * strings)111     explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* strings)
112         : fIndex(0), fStrings(strings)
113     { }
114 
next(SkTypeface::LocalizedString * localizedString)115     bool next(SkTypeface::LocalizedString* localizedString) override {
116         if (fIndex >= fStrings->GetCount()) {
117             return false;
118         }
119 
120         // String
121         UINT32 stringLen;
122         HRBM(fStrings->GetStringLength(fIndex, &stringLen), "Could not get string length.");
123 
124         SkSMallocWCHAR wString(static_cast<size_t>(stringLen)+1);
125         HRBM(fStrings->GetString(fIndex, wString.get(), stringLen+1), "Could not get string.");
126 
127         HRB(sk_wchar_to_skstring(wString.get(), stringLen, &localizedString->fString));
128 
129         // Locale
130         UINT32 localeLen;
131         HRBM(fStrings->GetLocaleNameLength(fIndex, &localeLen), "Could not get locale length.");
132 
133         SkSMallocWCHAR wLocale(static_cast<size_t>(localeLen)+1);
134         HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLen+1), "Could not get locale.");
135 
136         HRB(sk_wchar_to_skstring(wLocale.get(), localeLen, &localizedString->fLanguage));
137 
138         ++fIndex;
139         return true;
140     }
141 
142 private:
143     UINT32 fIndex;
144     SkTScopedComPtr<IDWriteLocalizedStrings> fStrings;
145 };
146 
onCreateFamilyNameIterator() const147 SkTypeface::LocalizedStrings* DWriteFontTypeface::onCreateFamilyNameIterator() const {
148     sk_sp<SkTypeface::LocalizedStrings> nameIter =
149         SkOTUtils::LocalizedStrings_NameTable::MakeForFamilyNames(*this);
150     if (!nameIter) {
151         SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
152         HRNM(fDWriteFontFamily->GetFamilyNames(&familyNames), "Could not obtain family names.");
153         nameIter = sk_make_sp<LocalizedStrings_IDWriteLocalizedStrings>(familyNames.release());
154     }
155     return nameIter.release();
156 }
157 
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const158 int DWriteFontTypeface::onGetVariationDesignPosition(
159     SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
160 {
161 
162 #if defined(NTDDI_WIN10_RS3) && NTDDI_VERSION >= NTDDI_WIN10_RS3
163 
164     SkTScopedComPtr<IDWriteFontFace5> fontFace5;
165     if (FAILED(fDWriteFontFace->QueryInterface(&fontFace5))) {
166         return -1;
167     }
168 
169     // Return 0 if the font is not variable font.
170     if (!fontFace5->HasVariations()) {
171         return 0;
172     }
173 
174     UINT32 fontAxisCount = fontFace5->GetFontAxisValueCount();
175     SkTScopedComPtr<IDWriteFontResource> fontResource;
176     HR_GENERAL(fontFace5->GetFontResource(&fontResource), nullptr, -1);
177     UINT32 variableAxisCount = 0;
178     for (UINT32 i = 0; i < fontAxisCount; ++i) {
179         if (fontResource->GetFontAxisAttributes(i) & DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE) {
180             ++variableAxisCount;
181         }
182     }
183 
184     if (!coordinates || coordinateCount < 0 || (unsigned)coordinateCount < variableAxisCount) {
185         return SkTo<int>(variableAxisCount);
186     }
187 
188     SkAutoSTMalloc<8, DWRITE_FONT_AXIS_VALUE> fontAxisValue(fontAxisCount);
189     HR_GENERAL(fontFace5->GetFontAxisValues(fontAxisValue.get(), fontAxisCount), nullptr, -1);
190     UINT32 coordIndex = 0;
191     for (UINT32 axisIndex = 0; axisIndex < fontAxisCount; ++axisIndex) {
192         if (fontResource->GetFontAxisAttributes(axisIndex) & DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE) {
193             coordinates[coordIndex].axis = SkEndian_SwapBE32(fontAxisValue[axisIndex].axisTag);
194             coordinates[coordIndex].value = fontAxisValue[axisIndex].value;
195             ++coordIndex;
196         }
197     }
198 
199     SkASSERT(coordIndex == variableAxisCount);
200     return SkTo<int>(variableAxisCount);
201 
202 #endif
203 
204     return -1;
205 }
206 
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const207 int DWriteFontTypeface::onGetVariationDesignParameters(
208     SkFontParameters::Variation::Axis parameters[], int parameterCount) const
209 {
210 
211 #if defined(NTDDI_WIN10_RS3) && NTDDI_VERSION >= NTDDI_WIN10_RS3
212 
213     SkTScopedComPtr<IDWriteFontFace5> fontFace5;
214     if (FAILED(fDWriteFontFace->QueryInterface(&fontFace5))) {
215         return -1;
216     }
217 
218     // Return 0 if the font is not variable font.
219     if (!fontFace5->HasVariations()) {
220         return 0;
221     }
222 
223     UINT32 fontAxisCount = fontFace5->GetFontAxisValueCount();
224     SkTScopedComPtr<IDWriteFontResource> fontResource;
225     HR_GENERAL(fontFace5->GetFontResource(&fontResource), nullptr, -1);
226     int variableAxisCount = 0;
227     for (UINT32 i = 0; i < fontAxisCount; ++i) {
228         if (fontResource->GetFontAxisAttributes(i) & DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE) {
229             variableAxisCount++;
230         }
231     }
232 
233     if (!parameters || parameterCount < variableAxisCount) {
234         return variableAxisCount;
235     }
236 
237     SkAutoSTMalloc<8, DWRITE_FONT_AXIS_RANGE> fontAxisRange(fontAxisCount);
238     HR_GENERAL(fontResource->GetFontAxisRanges(fontAxisRange.get(), fontAxisCount), nullptr, -1);
239     SkAutoSTMalloc<8, DWRITE_FONT_AXIS_VALUE> fontAxisDefaultValue(fontAxisCount);
240     HR_GENERAL(fontResource->GetDefaultFontAxisValues(fontAxisDefaultValue.get(), fontAxisCount),
241                nullptr, -1);
242     UINT32 coordIndex = 0;
243 
244     for (UINT32 axisIndex = 0; axisIndex < fontAxisCount; ++axisIndex) {
245         if (fontResource->GetFontAxisAttributes(axisIndex) & DWRITE_FONT_AXIS_ATTRIBUTES_VARIABLE) {
246             parameters[coordIndex].tag = SkEndian_SwapBE32(fontAxisDefaultValue[axisIndex].axisTag);
247             parameters[coordIndex].min = fontAxisRange[axisIndex].minValue;
248             parameters[coordIndex].def = fontAxisDefaultValue[axisIndex].value;
249             parameters[coordIndex].max = fontAxisRange[axisIndex].maxValue;
250             parameters[coordIndex].setHidden(fontResource->GetFontAxisAttributes(axisIndex) &
251                                              DWRITE_FONT_AXIS_ATTRIBUTES_HIDDEN);
252             ++coordIndex;
253         }
254     }
255 
256     return variableAxisCount;
257 
258 #endif
259 
260     return -1;
261 }
262 
onGetTableTags(SkFontTableTag tags[]) const263 int DWriteFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
264     DWRITE_FONT_FACE_TYPE type = fDWriteFontFace->GetType();
265     if (type != DWRITE_FONT_FACE_TYPE_CFF &&
266         type != DWRITE_FONT_FACE_TYPE_TRUETYPE &&
267         type != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION)
268     {
269         return 0;
270     }
271 
272     int ttcIndex;
273     std::unique_ptr<SkStreamAsset> stream = this->openStream(&ttcIndex);
274     return stream.get() ? SkFontStream::GetTableTags(stream.get(), ttcIndex, tags) : 0;
275 }
276 
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const277 size_t DWriteFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
278                                           size_t length, void* data) const
279 {
280     AutoDWriteTable table(fDWriteFontFace.get(), SkEndian_SwapBE32(tag));
281     if (!table.fExists) {
282         return 0;
283     }
284 
285     if (offset > table.fSize) {
286         return 0;
287     }
288     size_t size = std::min(length, table.fSize - offset);
289     if (data) {
290         memcpy(data, table.fData + offset, size);
291     }
292 
293     return size;
294 }
295 
onCopyTableData(SkFontTableTag tag) const296 sk_sp<SkData> DWriteFontTypeface::onCopyTableData(SkFontTableTag tag) const {
297     const uint8_t* data;
298     UINT32 size;
299     void* lock;
300     BOOL exists;
301     fDWriteFontFace->TryGetFontTable(SkEndian_SwapBE32(tag),
302             reinterpret_cast<const void **>(&data), &size, &lock, &exists);
303     if (!exists) {
304         return nullptr;
305     }
306     struct Context {
307         Context(void* lock, IDWriteFontFace* face) : fLock(lock), fFontFace(SkRefComPtr(face)) {}
308         ~Context() { fFontFace->ReleaseFontTable(fLock); }
309         void* fLock;
310         SkTScopedComPtr<IDWriteFontFace> fFontFace;
311     };
312     return SkData::MakeWithProc(data, size,
313                                 [](const void*, void* ctx) { delete (Context*)ctx; },
314                                 new Context(lock, fDWriteFontFace.get()));
315 }
316 
onMakeClone(const SkFontArguments & args) const317 sk_sp<SkTypeface> DWriteFontTypeface::onMakeClone(const SkFontArguments& args) const {
318     // Skip if the current face index does not match the ttcIndex
319     if (fDWriteFontFace->GetIndex() != SkTo<UINT32>(args.getCollectionIndex())) {
320         return sk_ref_sp(this);
321     }
322 
323 #if defined(NTDDI_WIN10_RS3) && NTDDI_VERSION >= NTDDI_WIN10_RS3
324 
325     SkTScopedComPtr<IDWriteFontFace5> fontFace5;
326 
327     if (SUCCEEDED(fDWriteFontFace->QueryInterface(&fontFace5)) && fontFace5->HasVariations()) {
328         UINT32 fontAxisCount = fontFace5->GetFontAxisValueCount();
329         UINT32 argsCoordCount = args.getVariationDesignPosition().coordinateCount;
330         SkAutoSTMalloc<8, DWRITE_FONT_AXIS_VALUE> fontAxisValue(fontAxisCount);
331         HRN(fontFace5->GetFontAxisValues(fontAxisValue.get(), fontAxisCount));
332 
333         for (UINT32 fontIndex = 0; fontIndex < fontAxisCount; ++fontIndex) {
334             for (UINT32 argsIndex = 0; argsIndex < argsCoordCount; ++argsIndex) {
335                 if (SkEndian_SwapBE32(fontAxisValue[fontIndex].axisTag) ==
336                     args.getVariationDesignPosition().coordinates[argsIndex].axis) {
337                     fontAxisValue[fontIndex].value =
338                         args.getVariationDesignPosition().coordinates[argsIndex].value;
339                 }
340             }
341         }
342         SkTScopedComPtr<IDWriteFontResource> fontResource;
343         HRN(fontFace5->GetFontResource(&fontResource));
344         SkTScopedComPtr<IDWriteFontFace5> newFontFace5;
345         HRN(fontResource->CreateFontFace(fDWriteFont->GetSimulations(),
346                                          fontAxisValue.get(),
347                                          fontAxisCount,
348                                          &newFontFace5));
349 
350         SkTScopedComPtr<IDWriteFontFace> newFontFace;
351         HRN(newFontFace5->QueryInterface(&newFontFace));
352         return DWriteFontTypeface::Make(fFactory.get(),
353                                         newFontFace.get(),
354                                         fDWriteFont.get(),
355                                         fDWriteFontFamily.get(),
356                                         fLoaders);
357     }
358 
359 #endif
360 
361     return sk_ref_sp(this);
362 }
363 
onOpenStream(int * ttcIndex) const364 std::unique_ptr<SkStreamAsset> DWriteFontTypeface::onOpenStream(int* ttcIndex) const {
365     *ttcIndex = fDWriteFontFace->GetIndex();
366 
367     UINT32 numFiles = 0;
368     HRNM(fDWriteFontFace->GetFiles(&numFiles, nullptr),
369          "Could not get number of font files.");
370     if (numFiles != 1) {
371         return nullptr;
372     }
373 
374     SkTScopedComPtr<IDWriteFontFile> fontFile;
375     HRNM(fDWriteFontFace->GetFiles(&numFiles, &fontFile), "Could not get font files.");
376 
377     const void* fontFileKey;
378     UINT32 fontFileKeySize;
379     HRNM(fontFile->GetReferenceKey(&fontFileKey, &fontFileKeySize),
380          "Could not get font file reference key.");
381 
382     SkTScopedComPtr<IDWriteFontFileLoader> fontFileLoader;
383     HRNM(fontFile->GetLoader(&fontFileLoader), "Could not get font file loader.");
384 
385     SkTScopedComPtr<IDWriteFontFileStream> fontFileStream;
386     HRNM(fontFileLoader->CreateStreamFromKey(fontFileKey, fontFileKeySize,
387                                              &fontFileStream),
388          "Could not create font file stream.");
389 
390     return std::unique_ptr<SkStreamAsset>(new SkDWriteFontFileStream(fontFileStream.get()));
391 }
392 
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const393 std::unique_ptr<SkScalerContext> DWriteFontTypeface::onCreateScalerContext(
394     const SkScalerContextEffects& effects, const SkDescriptor* desc) const
395 {
396     return std::make_unique<SkScalerContext_DW>(
397             sk_ref_sp(const_cast<DWriteFontTypeface*>(this)), effects, desc);
398 }
399 
onFilterRec(SkScalerContextRec * rec) const400 void DWriteFontTypeface::onFilterRec(SkScalerContextRec* rec) const {
401     if (rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) {
402         rec->fMaskFormat = SkMask::kA8_Format;
403         rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag;
404     }
405 
406     unsigned flagsWeDontSupport = SkScalerContext::kForceAutohinting_Flag |
407                                   SkScalerContext::kEmbolden_Flag |
408                                   SkScalerContext::kLCD_Vertical_Flag;
409     rec->fFlags &= ~flagsWeDontSupport;
410 
411     SkFontHinting h = rec->getHinting();
412     // DirectWrite2 allows for hinting to be turned off. Force everything else to normal.
413     if (h != SkFontHinting::kNone || !fFactory2 || !fDWriteFontFace2) {
414         h = SkFontHinting::kNormal;
415     }
416     rec->setHinting(h);
417 
418 #if defined(SK_FONT_HOST_USE_SYSTEM_SETTINGS)
419     IDWriteFactory* factory = sk_get_dwrite_factory();
420     if (factory != nullptr) {
421         SkTScopedComPtr<IDWriteRenderingParams> defaultRenderingParams;
422         if (SUCCEEDED(factory->CreateRenderingParams(&defaultRenderingParams))) {
423             float gamma = defaultRenderingParams->GetGamma();
424             rec->setDeviceGamma(gamma);
425             rec->setPaintGamma(gamma);
426 
427             rec->setContrast(defaultRenderingParams->GetEnhancedContrast());
428         }
429     }
430 #endif
431 }
432 
433 ///////////////////////////////////////////////////////////////////////////////
434 //PDF Support
435 
glyph_to_unicode_map(IDWriteFontFace * fontFace,DWRITE_UNICODE_RANGE range,UINT32 * remainingGlyphCount,UINT32 numGlyphs,SkUnichar * glyphToUnicode)436 static void glyph_to_unicode_map(IDWriteFontFace* fontFace, DWRITE_UNICODE_RANGE range,
437                                  UINT32* remainingGlyphCount, UINT32 numGlyphs,
438                                  SkUnichar* glyphToUnicode)
439 {
440     constexpr const int batchSize = 128;
441     UINT32 codepoints[batchSize];
442     UINT16 glyphs[batchSize];
443     for (UINT32 c = range.first; c <= range.last && *remainingGlyphCount != 0; c += batchSize) {
444         UINT32 numBatchedCodePoints = std::min<UINT32>(range.last - c + 1, batchSize);
445         for (UINT32 i = 0; i < numBatchedCodePoints; ++i) {
446             codepoints[i] = c + i;
447         }
448         HRVM(fontFace->GetGlyphIndices(codepoints, numBatchedCodePoints, glyphs),
449              "Failed to get glyph indexes.");
450         for (UINT32 i = 0; i < numBatchedCodePoints; ++i) {
451             UINT16 glyph = glyphs[i];
452             // Intermittent DW bug on Windows 10. See crbug.com/470146.
453             if (glyph >= numGlyphs) {
454                 return;
455             }
456             if (0 < glyph && glyphToUnicode[glyph] == 0) {
457                 glyphToUnicode[glyph] = c + i;  // Always use lowest-index unichar.
458                 --*remainingGlyphCount;
459             }
460         }
461     }
462 }
463 
getGlyphToUnicodeMap(SkUnichar * glyphToUnicode) const464 void DWriteFontTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
465     IDWriteFontFace* face = fDWriteFontFace.get();
466     UINT32 numGlyphs = face->GetGlyphCount();
467     sk_bzero(glyphToUnicode, sizeof(SkUnichar) * numGlyphs);
468     UINT32 remainingGlyphCount = numGlyphs;
469 
470     if (fDWriteFontFace1) {
471         IDWriteFontFace1* face1 = fDWriteFontFace1.get();
472         UINT32 numRanges = 0;
473         HRESULT hr = face1->GetUnicodeRanges(0, nullptr, &numRanges);
474         if (hr != E_NOT_SUFFICIENT_BUFFER && FAILED(hr)) {
475             HRVM(hr, "Failed to get number of ranges.");
476         }
477         std::unique_ptr<DWRITE_UNICODE_RANGE[]> ranges(new DWRITE_UNICODE_RANGE[numRanges]);
478         HRVM(face1->GetUnicodeRanges(numRanges, ranges.get(), &numRanges), "Failed to get ranges.");
479         for (UINT32 i = 0; i < numRanges; ++i) {
480             glyph_to_unicode_map(face1, ranges[i], &remainingGlyphCount, numGlyphs, glyphToUnicode);
481         }
482     } else {
483         glyph_to_unicode_map(face, {0, 0x10FFFF}, &remainingGlyphCount, numGlyphs, glyphToUnicode);
484     }
485 }
486 
onGetAdvancedMetrics() const487 std::unique_ptr<SkAdvancedTypefaceMetrics> DWriteFontTypeface::onGetAdvancedMetrics() const {
488 
489     std::unique_ptr<SkAdvancedTypefaceMetrics> info(nullptr);
490 
491     DWRITE_FONT_METRICS dwfm;
492     fDWriteFontFace->GetMetrics(&dwfm);
493 
494     info.reset(new SkAdvancedTypefaceMetrics);
495 
496     info->fAscent = SkToS16(dwfm.ascent);
497     info->fDescent = SkToS16(dwfm.descent);
498     info->fCapHeight = SkToS16(dwfm.capHeight);
499 
500     {
501         SkTScopedComPtr<IDWriteLocalizedStrings> postScriptNames;
502         BOOL exists = FALSE;
503         if (FAILED(fDWriteFont->GetInformationalStrings(
504                         DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_NAME,
505                         &postScriptNames,
506                         &exists)) ||
507             !exists ||
508             FAILED(sk_get_locale_string(postScriptNames.get(), nullptr, &info->fPostScriptName)))
509         {
510             SkDEBUGF("Unable to get postscript name for typeface %p\n", this);
511         }
512     }
513 
514     // SkAdvancedTypefaceMetrics::fFontName must actually be a family name.
515     SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
516     if (FAILED(fDWriteFontFamily->GetFamilyNames(&familyNames)) ||
517         FAILED(sk_get_locale_string(familyNames.get(), nullptr, &info->fFontName)))
518     {
519         SkDEBUGF("Unable to get family name for typeface 0x%p\n", this);
520     }
521     if (info->fPostScriptName.isEmpty()) {
522         info->fPostScriptName = info->fFontName;
523     }
524 
525     DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType();
526     if (fontType != DWRITE_FONT_FACE_TYPE_TRUETYPE &&
527         fontType != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION)
528     {
529         return info;
530     }
531 
532     // Simulated fonts aren't really TrueType fonts.
533     if (fDWriteFontFace->GetSimulations() == DWRITE_FONT_SIMULATIONS_NONE) {
534         info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
535     }
536 
537     AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get());
538     AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get());
539     AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get());
540     AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get());
541     if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Table.fExists) {
542         return info;
543     }
544 
545     SkOTUtils::SetAdvancedTypefaceFlags(os2Table->version.v4.fsType, info.get());
546 
547     // There are versions of DirectWrite which support named instances for system variation fonts,
548     // but no means to indicate that such a typeface is a variation.
549     AutoTDWriteTable<SkOTTableFontVariations> fvarTable(fDWriteFontFace.get());
550     if (fvarTable.fExists) {
551         info->fFlags |= SkAdvancedTypefaceMetrics::kVariable_FontFlag;
552     }
553 
554     //There exist CJK fonts which set the IsFixedPitch and Monospace bits,
555     //but have full width, latin half-width, and half-width kana.
556     bool fixedWidth = (postTable->isFixedPitch &&
557                       (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics)));
558     //Monospace
559     if (fixedWidth) {
560         info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
561     }
562     //Italic
563     if (os2Table->version.v0.fsSelection.field.Italic) {
564         info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
565     }
566     //Serif
567     using SerifStyle = SkPanose::Data::TextAndDisplay::SerifStyle;
568     SerifStyle serifStyle = os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle;
569     if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.panose.bFamilyType) {
570         if (SerifStyle::Cove == serifStyle ||
571             SerifStyle::ObtuseCove == serifStyle ||
572             SerifStyle::SquareCove == serifStyle ||
573             SerifStyle::ObtuseSquareCove == serifStyle ||
574             SerifStyle::Square == serifStyle ||
575             SerifStyle::Thin == serifStyle ||
576             SerifStyle::Bone == serifStyle ||
577             SerifStyle::Exaggerated == serifStyle ||
578             SerifStyle::Triangle == serifStyle)
579         {
580             info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
581         }
582     //Script
583     } else if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType) {
584         info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
585     }
586 
587     info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16;
588 
589     info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMin),
590                                     (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMax),
591                                     (int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMax),
592                                     (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMin));
593     return info;
594 }
595 #endif//defined(SK_BUILD_FOR_WIN)
596