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