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" |
| 9 | // SkTypes will include Windows.h, which will pull in all of the GDI defines. |
| 10 | // GDI #defines GetGlyphIndices to GetGlyphIndicesA or GetGlyphIndicesW, but |
| 11 | // IDWriteFontFace has a method called GetGlyphIndices. Since this file does |
| 12 | // not use GDI, undefing GetGlyphIndices makes things less confusing. |
| 13 | #undef GetGlyphIndices |
| 14 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 15 | #include "SkDWrite.h" |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 16 | #include "SkDWriteFontFileStream.h" |
| 17 | #include "SkFontDescriptor.h" |
| 18 | #include "SkFontStream.h" |
| 19 | #include "SkOTTable_head.h" |
| 20 | #include "SkOTTable_hhea.h" |
| 21 | #include "SkOTTable_OS_2.h" |
| 22 | #include "SkOTTable_post.h" |
| 23 | #include "SkScalerContext.h" |
| 24 | #include "SkScalerContext_win_dw.h" |
| 25 | #include "SkTypeface_win_dw.h" |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 26 | #include "SkUtils.h" |
| 27 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 28 | void DWriteFontTypeface::onGetFamilyName(SkString* familyName) const { |
| 29 | SkTScopedComPtr<IDWriteLocalizedStrings> familyNames; |
| 30 | HRV(fDWriteFontFamily->GetFamilyNames(&familyNames)); |
| 31 | |
| 32 | sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, familyName); |
| 33 | } |
| 34 | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 35 | void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
| 36 | bool* isLocalStream) const { |
| 37 | // Get the family name. |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 38 | SkTScopedComPtr<IDWriteLocalizedStrings> familyNames; |
| 39 | HRV(fDWriteFontFamily->GetFamilyNames(&familyNames)); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 40 | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 41 | SkString utf8FamilyName; |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 42 | sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, &utf8FamilyName); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 43 | |
| 44 | desc->setFamilyName(utf8FamilyName.c_str()); |
| 45 | *isLocalStream = SkToBool(fDWriteFontFileLoader.get()); |
| 46 | } |
| 47 | |
| 48 | static SkUnichar next_utf8(const void** chars) { |
| 49 | return SkUTF8_NextUnichar((const char**)chars); |
| 50 | } |
| 51 | |
| 52 | static SkUnichar next_utf16(const void** chars) { |
| 53 | return SkUTF16_NextUnichar((const uint16_t**)chars); |
| 54 | } |
| 55 | |
| 56 | static SkUnichar next_utf32(const void** chars) { |
| 57 | const SkUnichar** uniChars = (const SkUnichar**)chars; |
| 58 | SkUnichar uni = **uniChars; |
| 59 | *uniChars += 1; |
| 60 | return uni; |
| 61 | } |
| 62 | |
| 63 | typedef SkUnichar (*EncodingProc)(const void**); |
| 64 | |
| 65 | static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) { |
| 66 | static const EncodingProc gProcs[] = { |
| 67 | next_utf8, next_utf16, next_utf32 |
| 68 | }; |
| 69 | SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs)); |
| 70 | return gProcs[enc]; |
| 71 | } |
| 72 | |
| 73 | int DWriteFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding, |
| 74 | uint16_t glyphs[], int glyphCount) const |
| 75 | { |
| 76 | if (NULL == glyphs) { |
| 77 | EncodingProc next_ucs4_proc = find_encoding_proc(encoding); |
| 78 | for (int i = 0; i < glyphCount; ++i) { |
| 79 | const SkUnichar c = next_ucs4_proc(&chars); |
| 80 | BOOL exists; |
| 81 | fDWriteFont->HasCharacter(c, &exists); |
| 82 | if (!exists) { |
| 83 | return i; |
| 84 | } |
| 85 | } |
| 86 | return glyphCount; |
| 87 | } |
| 88 | |
| 89 | switch (encoding) { |
| 90 | case SkTypeface::kUTF8_Encoding: |
| 91 | case SkTypeface::kUTF16_Encoding: { |
| 92 | static const int scratchCount = 256; |
| 93 | UINT32 scratch[scratchCount]; |
| 94 | EncodingProc next_ucs4_proc = find_encoding_proc(encoding); |
| 95 | for (int baseGlyph = 0; baseGlyph < glyphCount; baseGlyph += scratchCount) { |
| 96 | int glyphsLeft = glyphCount - baseGlyph; |
| 97 | int limit = SkTMin(glyphsLeft, scratchCount); |
| 98 | for (int i = 0; i < limit; ++i) { |
| 99 | scratch[i] = next_ucs4_proc(&chars); |
| 100 | } |
| 101 | fDWriteFontFace->GetGlyphIndices(scratch, limit, &glyphs[baseGlyph]); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | case SkTypeface::kUTF32_Encoding: { |
| 106 | const UINT32* utf32 = reinterpret_cast<const UINT32*>(chars); |
| 107 | fDWriteFontFace->GetGlyphIndices(utf32, glyphCount, glyphs); |
| 108 | break; |
| 109 | } |
| 110 | default: |
| 111 | SK_CRASH(); |
| 112 | } |
| 113 | |
| 114 | for (int i = 0; i < glyphCount; ++i) { |
| 115 | if (0 == glyphs[i]) { |
| 116 | return i; |
| 117 | } |
| 118 | } |
| 119 | return glyphCount; |
| 120 | } |
| 121 | |
| 122 | int DWriteFontTypeface::onCountGlyphs() const { |
| 123 | return fDWriteFontFace->GetGlyphCount(); |
| 124 | } |
| 125 | |
| 126 | int DWriteFontTypeface::onGetUPEM() const { |
| 127 | DWRITE_FONT_METRICS metrics; |
| 128 | fDWriteFontFace->GetMetrics(&metrics); |
| 129 | return metrics.designUnitsPerEm; |
| 130 | } |
| 131 | |
| 132 | class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStrings { |
| 133 | public: |
| 134 | /** Takes ownership of the IDWriteLocalizedStrings. */ |
| 135 | explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* strings) |
| 136 | : fIndex(0), fStrings(strings) |
| 137 | { } |
| 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | bool next(SkTypeface::LocalizedString* localizedString) override { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 140 | if (fIndex >= fStrings->GetCount()) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | // String |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 145 | UINT32 stringLen; |
| 146 | HRBM(fStrings->GetStringLength(fIndex, &stringLen), "Could not get string length."); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 147 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 148 | SkSMallocWCHAR wString(stringLen+1); |
| 149 | HRBM(fStrings->GetString(fIndex, wString.get(), stringLen+1), "Could not get string."); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 150 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 151 | HRB(sk_wchar_to_skstring(wString.get(), stringLen, &localizedString->fString)); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 152 | |
| 153 | // Locale |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 154 | UINT32 localeLen; |
| 155 | HRBM(fStrings->GetLocaleNameLength(fIndex, &localeLen), "Could not get locale length."); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 156 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 157 | SkSMallocWCHAR wLocale(localeLen+1); |
| 158 | HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLen+1), "Could not get locale."); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 159 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 160 | HRB(sk_wchar_to_skstring(wLocale.get(), localeLen, &localizedString->fLanguage)); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 161 | |
| 162 | ++fIndex; |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | UINT32 fIndex; |
| 168 | SkTScopedComPtr<IDWriteLocalizedStrings> fStrings; |
| 169 | }; |
| 170 | |
| 171 | SkTypeface::LocalizedStrings* DWriteFontTypeface::onCreateFamilyNameIterator() const { |
| 172 | SkTScopedComPtr<IDWriteLocalizedStrings> familyNames; |
| 173 | HRNM(fDWriteFontFamily->GetFamilyNames(&familyNames), "Could not obtain family names."); |
| 174 | |
| 175 | return new LocalizedStrings_IDWriteLocalizedStrings(familyNames.release()); |
| 176 | } |
| 177 | |
| 178 | int DWriteFontTypeface::onGetTableTags(SkFontTableTag tags[]) const { |
| 179 | DWRITE_FONT_FACE_TYPE type = fDWriteFontFace->GetType(); |
| 180 | if (type != DWRITE_FONT_FACE_TYPE_CFF && |
| 181 | type != DWRITE_FONT_FACE_TYPE_TRUETYPE && |
| 182 | type != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION) |
| 183 | { |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int ttcIndex; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 188 | SkAutoTDelete<SkStream> stream(this->openStream(&ttcIndex)); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 189 | return stream.get() ? SkFontStream::GetTableTags(stream, ttcIndex, tags) : 0; |
| 190 | } |
| 191 | |
| 192 | size_t DWriteFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
| 193 | size_t length, void* data) const |
| 194 | { |
| 195 | AutoDWriteTable table(fDWriteFontFace.get(), SkEndian_SwapBE32(tag)); |
| 196 | if (!table.fExists) { |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | if (offset > table.fSize) { |
| 201 | return 0; |
| 202 | } |
| 203 | size_t size = SkTMin(length, table.fSize - offset); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 204 | if (data) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 205 | memcpy(data, table.fData + offset, size); |
| 206 | } |
| 207 | |
| 208 | return size; |
| 209 | } |
| 210 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 211 | SkStreamAsset* DWriteFontTypeface::onOpenStream(int* ttcIndex) const { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 212 | *ttcIndex = fDWriteFontFace->GetIndex(); |
| 213 | |
| 214 | UINT32 numFiles; |
| 215 | HRNM(fDWriteFontFace->GetFiles(&numFiles, NULL), |
| 216 | "Could not get number of font files."); |
| 217 | if (numFiles != 1) { |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | SkTScopedComPtr<IDWriteFontFile> fontFile; |
| 222 | HRNM(fDWriteFontFace->GetFiles(&numFiles, &fontFile), "Could not get font files."); |
| 223 | |
| 224 | const void* fontFileKey; |
| 225 | UINT32 fontFileKeySize; |
| 226 | HRNM(fontFile->GetReferenceKey(&fontFileKey, &fontFileKeySize), |
| 227 | "Could not get font file reference key."); |
| 228 | |
| 229 | SkTScopedComPtr<IDWriteFontFileLoader> fontFileLoader; |
| 230 | HRNM(fontFile->GetLoader(&fontFileLoader), "Could not get font file loader."); |
| 231 | |
| 232 | SkTScopedComPtr<IDWriteFontFileStream> fontFileStream; |
| 233 | HRNM(fontFileLoader->CreateStreamFromKey(fontFileKey, fontFileKeySize, |
| 234 | &fontFileStream), |
| 235 | "Could not create font file stream."); |
| 236 | |
| 237 | return SkNEW_ARGS(SkDWriteFontFileStream, (fontFileStream.get())); |
| 238 | } |
| 239 | |
| 240 | SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* desc) const { |
| 241 | return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this), desc)); |
| 242 | } |
| 243 | |
| 244 | void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const { |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 245 | if (rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 246 | rec->fMaskFormat = SkMask::kA8_Format; |
| 247 | } |
| 248 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 249 | unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag | |
| 250 | SkScalerContext::kDevKernText_Flag | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 251 | SkScalerContext::kForceAutohinting_Flag | |
| 252 | SkScalerContext::kEmbolden_Flag | |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 253 | SkScalerContext::kLCD_Vertical_Flag; |
| 254 | rec->fFlags &= ~flagsWeDontSupport; |
| 255 | |
| 256 | SkPaint::Hinting h = rec->getHinting(); |
| 257 | // DirectWrite does not provide for hinting hints. |
| 258 | h = SkPaint::kSlight_Hinting; |
| 259 | rec->setHinting(h); |
| 260 | |
| 261 | #if SK_FONT_HOST_USE_SYSTEM_SETTINGS |
| 262 | IDWriteFactory* factory = get_dwrite_factory(); |
| 263 | if (factory != NULL) { |
| 264 | SkTScopedComPtr<IDWriteRenderingParams> defaultRenderingParams; |
| 265 | if (SUCCEEDED(factory->CreateRenderingParams(&defaultRenderingParams))) { |
| 266 | float gamma = defaultRenderingParams->GetGamma(); |
| 267 | rec->setDeviceGamma(gamma); |
| 268 | rec->setPaintGamma(gamma); |
| 269 | |
| 270 | rec->setContrast(defaultRenderingParams->GetEnhancedContrast()); |
| 271 | } |
| 272 | } |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | /////////////////////////////////////////////////////////////////////////////// |
| 277 | //PDF Support |
| 278 | |
| 279 | using namespace skia_advanced_typeface_metrics_utils; |
| 280 | |
| 281 | // Construct Glyph to Unicode table. |
| 282 | // Unicode code points that require conjugate pairs in utf16 are not |
| 283 | // supported. |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 284 | // TODO(bungeman): This never does what anyone wants. |
| 285 | // What is really wanted is the text to glyphs mapping |
| 286 | static void populate_glyph_to_unicode(IDWriteFontFace* fontFace, |
| 287 | const unsigned glyphCount, |
| 288 | SkTDArray<SkUnichar>* glyphToUnicode) { |
| 289 | HRESULT hr = S_OK; |
| 290 | |
| 291 | //Do this like free type instead |
bungeman | df2ec35 | 2014-08-20 12:21:32 -0700 | [diff] [blame] | 292 | SkAutoTMalloc<SkUnichar> glyphToUni(glyphCount); |
| 293 | int maxGlyph = -1; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 294 | for (UINT32 c = 0; c < 0x10FFFF; ++c) { |
| 295 | UINT16 glyph; |
| 296 | hr = fontFace->GetGlyphIndices(&c, 1, &glyph); |
bungeman | df2ec35 | 2014-08-20 12:21:32 -0700 | [diff] [blame] | 297 | SkASSERT(glyph < glyphCount); |
| 298 | if (0 < glyph) { |
| 299 | maxGlyph = SkTMax(static_cast<int>(glyph), maxGlyph); |
| 300 | glyphToUni[glyph] = c; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
bungeman | aace997 | 2014-08-25 07:14:02 -0700 | [diff] [blame] | 304 | SkTDArray<SkUnichar>(glyphToUni, maxGlyph + 1).swap(*glyphToUnicode); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static bool getWidthAdvance(IDWriteFontFace* fontFace, int gId, int16_t* advance) { |
| 308 | SkASSERT(advance); |
| 309 | |
| 310 | UINT16 glyphId = gId; |
| 311 | DWRITE_GLYPH_METRICS gm; |
| 312 | HRESULT hr = fontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm); |
| 313 | |
| 314 | if (FAILED(hr)) { |
| 315 | *advance = 0; |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | *advance = gm.advanceWidth; |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | SkAdvancedTypefaceMetrics* DWriteFontTypeface::onGetAdvancedTypefaceMetrics( |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 324 | PerGlyphInfo perGlyphInfo, |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 325 | const uint32_t* glyphIDs, |
| 326 | uint32_t glyphIDsCount) const { |
| 327 | |
| 328 | SkAdvancedTypefaceMetrics* info = NULL; |
| 329 | |
| 330 | HRESULT hr = S_OK; |
| 331 | |
| 332 | const unsigned glyphCount = fDWriteFontFace->GetGlyphCount(); |
| 333 | |
| 334 | DWRITE_FONT_METRICS dwfm; |
| 335 | fDWriteFontFace->GetMetrics(&dwfm); |
| 336 | |
| 337 | info = new SkAdvancedTypefaceMetrics; |
| 338 | info->fEmSize = dwfm.designUnitsPerEm; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 339 | info->fLastGlyphID = SkToU16(glyphCount - 1); |
| 340 | info->fStyle = 0; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 341 | info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 342 | |
bungeman | 6d867d4 | 2014-06-17 10:48:04 -0700 | [diff] [blame] | 343 | // SkAdvancedTypefaceMetrics::fFontName is in theory supposed to be |
| 344 | // the PostScript name of the font. However, due to the way it is currently |
| 345 | // used, it must actually be a family name. |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 346 | SkTScopedComPtr<IDWriteLocalizedStrings> familyNames; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 347 | hr = fDWriteFontFamily->GetFamilyNames(&familyNames); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 348 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 349 | UINT32 familyNameLen; |
| 350 | hr = familyNames->GetStringLength(0, &familyNameLen); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 351 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 352 | SkSMallocWCHAR familyName(familyNameLen+1); |
| 353 | hr = familyNames->GetString(0, familyName.get(), familyNameLen+1); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 354 | |
bungeman | 5e7b4f9 | 2014-08-25 10:16:01 -0700 | [diff] [blame] | 355 | hr = sk_wchar_to_skstring(familyName.get(), familyNameLen, &info->fFontName); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 356 | |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 357 | if (perGlyphInfo & kToUnicode_PerGlyphInfo) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 358 | populate_glyph_to_unicode(fDWriteFontFace.get(), glyphCount, &(info->fGlyphToUnicode)); |
| 359 | } |
| 360 | |
| 361 | DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType(); |
| 362 | if (fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE || |
| 363 | fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION) { |
| 364 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 365 | } else { |
| 366 | info->fType = SkAdvancedTypefaceMetrics::kOther_Font; |
| 367 | info->fItalicAngle = 0; |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 368 | info->fAscent = dwfm.ascent; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 369 | info->fDescent = dwfm.descent; |
| 370 | info->fStemV = 0; |
| 371 | info->fCapHeight = dwfm.capHeight; |
| 372 | info->fBBox = SkIRect::MakeEmpty(); |
| 373 | return info; |
| 374 | } |
| 375 | |
| 376 | AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get()); |
| 377 | AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get()); |
| 378 | AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get()); |
| 379 | AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get()); |
| 380 | if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Table.fExists) { |
| 381 | info->fItalicAngle = 0; |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 382 | info->fAscent = dwfm.ascent; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 383 | info->fDescent = dwfm.descent; |
| 384 | info->fStemV = 0; |
| 385 | info->fCapHeight = dwfm.capHeight; |
| 386 | info->fBBox = SkIRect::MakeEmpty(); |
| 387 | return info; |
| 388 | } |
| 389 | |
| 390 | //There exist CJK fonts which set the IsFixedPitch and Monospace bits, |
| 391 | //but have full width, latin half-width, and half-width kana. |
| 392 | bool fixedWidth = (postTable->isFixedPitch && |
| 393 | (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics))); |
| 394 | //Monospace |
| 395 | if (fixedWidth) { |
| 396 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 397 | } |
| 398 | //Italic |
| 399 | if (os2Table->version.v0.fsSelection.field.Italic) { |
| 400 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 401 | } |
| 402 | //Script |
| 403 | if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType.value) { |
| 404 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 405 | //Serif |
| 406 | } else if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.panose.bFamilyType.value && |
| 407 | SkPanose::Data::TextAndDisplay::SerifStyle::Triangle <= os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value && |
| 408 | SkPanose::Data::TextAndDisplay::SerifStyle::NoFit != os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value) { |
| 409 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 410 | } |
| 411 | |
| 412 | info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16; |
| 413 | |
| 414 | info->fAscent = SkToS16(dwfm.ascent); |
| 415 | info->fDescent = SkToS16(dwfm.descent); |
| 416 | info->fCapHeight = SkToS16(dwfm.capHeight); |
| 417 | |
| 418 | info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMin), |
| 419 | (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMax), |
| 420 | (int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMax), |
| 421 | (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMin)); |
| 422 | |
| 423 | //TODO: is this even desired? It seems PDF only wants this value for Type1 |
| 424 | //fonts, and we only get here for TrueType fonts. |
| 425 | info->fStemV = 0; |
| 426 | /* |
| 427 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 428 | // This probably isn't very good with an italic font. |
| 429 | int16_t min_width = SHRT_MAX; |
| 430 | info->fStemV = 0; |
| 431 | char stem_chars[] = {'i', 'I', '!', '1'}; |
| 432 | for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) { |
| 433 | ABC abcWidths; |
| 434 | if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) { |
| 435 | int16_t width = abcWidths.abcB; |
| 436 | if (width > 0 && width < min_width) { |
| 437 | min_width = width; |
| 438 | info->fStemV = min_width; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | */ |
| 443 | |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 444 | if (perGlyphInfo & kHAdvance_PerGlyphInfo) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 445 | if (fixedWidth) { |
| 446 | appendRange(&info->fGlyphWidths, 0); |
| 447 | int16_t advance; |
| 448 | getWidthAdvance(fDWriteFontFace.get(), 1, &advance); |
| 449 | info->fGlyphWidths->fAdvance.append(1, &advance); |
| 450 | finishRange(info->fGlyphWidths.get(), 0, |
| 451 | SkAdvancedTypefaceMetrics::WidthRange::kDefault); |
| 452 | } else { |
| 453 | info->fGlyphWidths.reset( |
| 454 | getAdvanceData(fDWriteFontFace.get(), |
| 455 | glyphCount, |
| 456 | glyphIDs, |
| 457 | glyphIDsCount, |
| 458 | getWidthAdvance)); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return info; |
| 463 | } |