blob: 25de1b6578e34e96c14968896837c3f299db66f9 [file] [log] [blame]
bungeman51daa252014-06-05 13:38:45 -07001/*
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
bungemandf2ec352014-08-20 12:21:32 -07008#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
bungemanb374d6a2014-09-17 07:48:59 -070015#include "SkDWrite.h"
bungeman51daa252014-06-05 13:38:45 -070016#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"
bungeman51daa252014-06-05 13:38:45 -070026#include "SkUtils.h"
27
bungemanb374d6a2014-09-17 07:48:59 -070028void 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
bungeman51daa252014-06-05 13:38:45 -070035void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
36 bool* isLocalStream) const {
37 // Get the family name.
bungeman5e7b4f92014-08-25 10:16:01 -070038 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
39 HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
bungeman51daa252014-06-05 13:38:45 -070040
bungeman51daa252014-06-05 13:38:45 -070041 SkString utf8FamilyName;
bungemanb374d6a2014-09-17 07:48:59 -070042 sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, &utf8FamilyName);
bungeman51daa252014-06-05 13:38:45 -070043
44 desc->setFamilyName(utf8FamilyName.c_str());
45 *isLocalStream = SkToBool(fDWriteFontFileLoader.get());
46}
47
48static SkUnichar next_utf8(const void** chars) {
49 return SkUTF8_NextUnichar((const char**)chars);
50}
51
52static SkUnichar next_utf16(const void** chars) {
53 return SkUTF16_NextUnichar((const uint16_t**)chars);
54}
55
56static 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
63typedef SkUnichar (*EncodingProc)(const void**);
64
65static 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
73int 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
122int DWriteFontTypeface::onCountGlyphs() const {
123 return fDWriteFontFace->GetGlyphCount();
124}
125
126int DWriteFontTypeface::onGetUPEM() const {
127 DWRITE_FONT_METRICS metrics;
128 fDWriteFontFace->GetMetrics(&metrics);
129 return metrics.designUnitsPerEm;
130}
131
132class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStrings {
133public:
134 /** Takes ownership of the IDWriteLocalizedStrings. */
135 explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* strings)
136 : fIndex(0), fStrings(strings)
137 { }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 bool next(SkTypeface::LocalizedString* localizedString) override {
bungeman51daa252014-06-05 13:38:45 -0700140 if (fIndex >= fStrings->GetCount()) {
141 return false;
142 }
143
144 // String
bungeman5e7b4f92014-08-25 10:16:01 -0700145 UINT32 stringLen;
146 HRBM(fStrings->GetStringLength(fIndex, &stringLen), "Could not get string length.");
bungeman51daa252014-06-05 13:38:45 -0700147
bungeman5e7b4f92014-08-25 10:16:01 -0700148 SkSMallocWCHAR wString(stringLen+1);
149 HRBM(fStrings->GetString(fIndex, wString.get(), stringLen+1), "Could not get string.");
bungeman51daa252014-06-05 13:38:45 -0700150
bungeman5e7b4f92014-08-25 10:16:01 -0700151 HRB(sk_wchar_to_skstring(wString.get(), stringLen, &localizedString->fString));
bungeman51daa252014-06-05 13:38:45 -0700152
153 // Locale
bungeman5e7b4f92014-08-25 10:16:01 -0700154 UINT32 localeLen;
155 HRBM(fStrings->GetLocaleNameLength(fIndex, &localeLen), "Could not get locale length.");
bungeman51daa252014-06-05 13:38:45 -0700156
bungeman5e7b4f92014-08-25 10:16:01 -0700157 SkSMallocWCHAR wLocale(localeLen+1);
158 HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLen+1), "Could not get locale.");
bungeman51daa252014-06-05 13:38:45 -0700159
bungeman5e7b4f92014-08-25 10:16:01 -0700160 HRB(sk_wchar_to_skstring(wLocale.get(), localeLen, &localizedString->fLanguage));
bungeman51daa252014-06-05 13:38:45 -0700161
162 ++fIndex;
163 return true;
164 }
165
166private:
167 UINT32 fIndex;
168 SkTScopedComPtr<IDWriteLocalizedStrings> fStrings;
169};
170
171SkTypeface::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
178int 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;
scroggoa1193e42015-01-21 12:09:53 -0800188 SkAutoTDelete<SkStream> stream(this->openStream(&ttcIndex));
bungeman51daa252014-06-05 13:38:45 -0700189 return stream.get() ? SkFontStream::GetTableTags(stream, ttcIndex, tags) : 0;
190}
191
192size_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);
bsalomon49f085d2014-09-05 13:34:00 -0700204 if (data) {
bungeman51daa252014-06-05 13:38:45 -0700205 memcpy(data, table.fData + offset, size);
206 }
207
208 return size;
209}
210
bungeman5f213d92015-01-27 05:39:10 -0800211SkStreamAsset* DWriteFontTypeface::onOpenStream(int* ttcIndex) const {
bungeman51daa252014-06-05 13:38:45 -0700212 *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
240SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* desc) const {
241 return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this), desc));
242}
243
244void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const {
bungeman12f03122015-03-19 13:57:36 -0700245 if (rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) {
bungeman51daa252014-06-05 13:38:45 -0700246 rec->fMaskFormat = SkMask::kA8_Format;
247 }
248
bungeman41078062014-07-07 08:16:37 -0700249 unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag |
250 SkScalerContext::kDevKernText_Flag |
bungeman51daa252014-06-05 13:38:45 -0700251 SkScalerContext::kForceAutohinting_Flag |
252 SkScalerContext::kEmbolden_Flag |
bungeman51daa252014-06-05 13:38:45 -0700253 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
279using 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.
bungeman51daa252014-06-05 13:38:45 -0700284// TODO(bungeman): This never does what anyone wants.
285// What is really wanted is the text to glyphs mapping
286static 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
bungemandf2ec352014-08-20 12:21:32 -0700292 SkAutoTMalloc<SkUnichar> glyphToUni(glyphCount);
293 int maxGlyph = -1;
bungeman51daa252014-06-05 13:38:45 -0700294 for (UINT32 c = 0; c < 0x10FFFF; ++c) {
295 UINT16 glyph;
296 hr = fontFace->GetGlyphIndices(&c, 1, &glyph);
bungemandf2ec352014-08-20 12:21:32 -0700297 SkASSERT(glyph < glyphCount);
298 if (0 < glyph) {
299 maxGlyph = SkTMax(static_cast<int>(glyph), maxGlyph);
300 glyphToUni[glyph] = c;
bungeman51daa252014-06-05 13:38:45 -0700301 }
302 }
303
bungemanaace9972014-08-25 07:14:02 -0700304 SkTDArray<SkUnichar>(glyphToUni, maxGlyph + 1).swap(*glyphToUnicode);
bungeman51daa252014-06-05 13:38:45 -0700305}
306
307static 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
323SkAdvancedTypefaceMetrics* DWriteFontTypeface::onGetAdvancedTypefaceMetrics(
reed39a9a502015-05-12 09:50:04 -0700324 PerGlyphInfo perGlyphInfo,
bungeman51daa252014-06-05 13:38:45 -0700325 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;
bungeman51daa252014-06-05 13:38:45 -0700339 info->fLastGlyphID = SkToU16(glyphCount - 1);
340 info->fStyle = 0;
vandebo0f9bad02014-06-19 11:05:39 -0700341 info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
bungeman51daa252014-06-05 13:38:45 -0700342
bungeman6d867d42014-06-17 10:48:04 -0700343 // 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.
bungeman51daa252014-06-05 13:38:45 -0700346 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
bungeman51daa252014-06-05 13:38:45 -0700347 hr = fDWriteFontFamily->GetFamilyNames(&familyNames);
bungeman51daa252014-06-05 13:38:45 -0700348
bungeman5e7b4f92014-08-25 10:16:01 -0700349 UINT32 familyNameLen;
350 hr = familyNames->GetStringLength(0, &familyNameLen);
bungeman51daa252014-06-05 13:38:45 -0700351
bungeman5e7b4f92014-08-25 10:16:01 -0700352 SkSMallocWCHAR familyName(familyNameLen+1);
353 hr = familyNames->GetString(0, familyName.get(), familyNameLen+1);
bungeman51daa252014-06-05 13:38:45 -0700354
bungeman5e7b4f92014-08-25 10:16:01 -0700355 hr = sk_wchar_to_skstring(familyName.get(), familyNameLen, &info->fFontName);
bungeman51daa252014-06-05 13:38:45 -0700356
reed39a9a502015-05-12 09:50:04 -0700357 if (perGlyphInfo & kToUnicode_PerGlyphInfo) {
bungeman51daa252014-06-05 13:38:45 -0700358 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;
tfarina567ff2f2015-04-27 07:01:44 -0700368 info->fAscent = dwfm.ascent;
bungeman51daa252014-06-05 13:38:45 -0700369 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;
tfarina567ff2f2015-04-27 07:01:44 -0700382 info->fAscent = dwfm.ascent;
bungeman51daa252014-06-05 13:38:45 -0700383 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
reed39a9a502015-05-12 09:50:04 -0700444 if (perGlyphInfo & kHAdvance_PerGlyphInfo) {
bungeman51daa252014-06-05 13:38:45 -0700445 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}