Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: src/ports/SkTypeface_win_dw.cpp

Issue 314193002: Split SkFontHost_win_dw. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Remove unneeded includes. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ports/SkTypeface_win_dw.h ('k') | src/utils/win/SkDWrite.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkDWriteFontFileStream.h"
9 #include "SkFontDescriptor.h"
10 #include "SkFontStream.h"
11 #include "SkOTTable_head.h"
12 #include "SkOTTable_hhea.h"
13 #include "SkOTTable_OS_2.h"
14 #include "SkOTTable_post.h"
15 #include "SkScalerContext.h"
16 #include "SkScalerContext_win_dw.h"
17 #include "SkTypeface_win_dw.h"
18 #include "SkTypes.h"
19 #include "SkUtils.h"
20
21 void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
22 bool* isLocalStream) const {
23 // Get the family name.
24 SkTScopedComPtr<IDWriteLocalizedStrings> dwFamilyNames;
25 HRV(fDWriteFontFamily->GetFamilyNames(&dwFamilyNames));
26
27 UINT32 dwFamilyNamesLength;
28 HRV(dwFamilyNames->GetStringLength(0, &dwFamilyNamesLength));
29
30 SkSMallocWCHAR dwFamilyNameChar(dwFamilyNamesLength+1);
31 HRV(dwFamilyNames->GetString(0, dwFamilyNameChar.get(), dwFamilyNamesLength+ 1));
32
33 SkString utf8FamilyName;
34 HRV(sk_wchar_to_skstring(dwFamilyNameChar.get(), &utf8FamilyName));
35
36 desc->setFamilyName(utf8FamilyName.c_str());
37 *isLocalStream = SkToBool(fDWriteFontFileLoader.get());
38 }
39
40 static SkUnichar next_utf8(const void** chars) {
41 return SkUTF8_NextUnichar((const char**)chars);
42 }
43
44 static SkUnichar next_utf16(const void** chars) {
45 return SkUTF16_NextUnichar((const uint16_t**)chars);
46 }
47
48 static SkUnichar next_utf32(const void** chars) {
49 const SkUnichar** uniChars = (const SkUnichar**)chars;
50 SkUnichar uni = **uniChars;
51 *uniChars += 1;
52 return uni;
53 }
54
55 typedef SkUnichar (*EncodingProc)(const void**);
56
57 static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
58 static const EncodingProc gProcs[] = {
59 next_utf8, next_utf16, next_utf32
60 };
61 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
62 return gProcs[enc];
63 }
64
65 int DWriteFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
66 uint16_t glyphs[], int glyphCount) const
67 {
68 if (NULL == glyphs) {
69 EncodingProc next_ucs4_proc = find_encoding_proc(encoding);
70 for (int i = 0; i < glyphCount; ++i) {
71 const SkUnichar c = next_ucs4_proc(&chars);
72 BOOL exists;
73 fDWriteFont->HasCharacter(c, &exists);
74 if (!exists) {
75 return i;
76 }
77 }
78 return glyphCount;
79 }
80
81 switch (encoding) {
82 case SkTypeface::kUTF8_Encoding:
83 case SkTypeface::kUTF16_Encoding: {
84 static const int scratchCount = 256;
85 UINT32 scratch[scratchCount];
86 EncodingProc next_ucs4_proc = find_encoding_proc(encoding);
87 for (int baseGlyph = 0; baseGlyph < glyphCount; baseGlyph += scratchCoun t) {
88 int glyphsLeft = glyphCount - baseGlyph;
89 int limit = SkTMin(glyphsLeft, scratchCount);
90 for (int i = 0; i < limit; ++i) {
91 scratch[i] = next_ucs4_proc(&chars);
92 }
93 fDWriteFontFace->GetGlyphIndices(scratch, limit, &glyphs[baseGlyph]) ;
94 }
95 break;
96 }
97 case SkTypeface::kUTF32_Encoding: {
98 const UINT32* utf32 = reinterpret_cast<const UINT32*>(chars);
99 fDWriteFontFace->GetGlyphIndices(utf32, glyphCount, glyphs);
100 break;
101 }
102 default:
103 SK_CRASH();
104 }
105
106 for (int i = 0; i < glyphCount; ++i) {
107 if (0 == glyphs[i]) {
108 return i;
109 }
110 }
111 return glyphCount;
112 }
113
114 int DWriteFontTypeface::onCountGlyphs() const {
115 return fDWriteFontFace->GetGlyphCount();
116 }
117
118 int DWriteFontTypeface::onGetUPEM() const {
119 DWRITE_FONT_METRICS metrics;
120 fDWriteFontFace->GetMetrics(&metrics);
121 return metrics.designUnitsPerEm;
122 }
123
124 class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStr ings {
125 public:
126 /** Takes ownership of the IDWriteLocalizedStrings. */
127 explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* s trings)
128 : fIndex(0), fStrings(strings)
129 { }
130
131 virtual bool next(SkTypeface::LocalizedString* localizedString) SK_OVERRIDE {
132 if (fIndex >= fStrings->GetCount()) {
133 return false;
134 }
135
136 // String
137 UINT32 stringLength;
138 HRBM(fStrings->GetStringLength(fIndex, &stringLength), "Could not get st ring length.");
139 stringLength += 1;
140
141 SkSMallocWCHAR wString(stringLength);
142 HRBM(fStrings->GetString(fIndex, wString.get(), stringLength), "Could no t get string.");
143
144 HRB(sk_wchar_to_skstring(wString.get(), &localizedString->fString));
145
146 // Locale
147 UINT32 localeLength;
148 HRBM(fStrings->GetLocaleNameLength(fIndex, &localeLength), "Could not ge t locale length.");
149 localeLength += 1;
150
151 SkSMallocWCHAR wLocale(localeLength);
152 HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLength), "Coul d not get locale.");
153
154 HRB(sk_wchar_to_skstring(wLocale.get(), &localizedString->fLanguage));
155
156 ++fIndex;
157 return true;
158 }
159
160 private:
161 UINT32 fIndex;
162 SkTScopedComPtr<IDWriteLocalizedStrings> fStrings;
163 };
164
165 SkTypeface::LocalizedStrings* DWriteFontTypeface::onCreateFamilyNameIterator() c onst {
166 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
167 HRNM(fDWriteFontFamily->GetFamilyNames(&familyNames), "Could not obtain fami ly names.");
168
169 return new LocalizedStrings_IDWriteLocalizedStrings(familyNames.release());
170 }
171
172 int DWriteFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
173 DWRITE_FONT_FACE_TYPE type = fDWriteFontFace->GetType();
174 if (type != DWRITE_FONT_FACE_TYPE_CFF &&
175 type != DWRITE_FONT_FACE_TYPE_TRUETYPE &&
176 type != DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION)
177 {
178 return 0;
179 }
180
181 int ttcIndex;
182 SkAutoTUnref<SkStream> stream(this->openStream(&ttcIndex));
183 return stream.get() ? SkFontStream::GetTableTags(stream, ttcIndex, tags) : 0 ;
184 }
185
186 size_t DWriteFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
187 size_t length, void* data) const
188 {
189 AutoDWriteTable table(fDWriteFontFace.get(), SkEndian_SwapBE32(tag));
190 if (!table.fExists) {
191 return 0;
192 }
193
194 if (offset > table.fSize) {
195 return 0;
196 }
197 size_t size = SkTMin(length, table.fSize - offset);
198 if (NULL != data) {
199 memcpy(data, table.fData + offset, size);
200 }
201
202 return size;
203 }
204
205 SkStream* DWriteFontTypeface::onOpenStream(int* ttcIndex) const {
206 *ttcIndex = fDWriteFontFace->GetIndex();
207
208 UINT32 numFiles;
209 HRNM(fDWriteFontFace->GetFiles(&numFiles, NULL),
210 "Could not get number of font files.");
211 if (numFiles != 1) {
212 return NULL;
213 }
214
215 SkTScopedComPtr<IDWriteFontFile> fontFile;
216 HRNM(fDWriteFontFace->GetFiles(&numFiles, &fontFile), "Could not get font fi les.");
217
218 const void* fontFileKey;
219 UINT32 fontFileKeySize;
220 HRNM(fontFile->GetReferenceKey(&fontFileKey, &fontFileKeySize),
221 "Could not get font file reference key.");
222
223 SkTScopedComPtr<IDWriteFontFileLoader> fontFileLoader;
224 HRNM(fontFile->GetLoader(&fontFileLoader), "Could not get font file loader." );
225
226 SkTScopedComPtr<IDWriteFontFileStream> fontFileStream;
227 HRNM(fontFileLoader->CreateStreamFromKey(fontFileKey, fontFileKeySize,
228 &fontFileStream),
229 "Could not create font file stream.");
230
231 return SkNEW_ARGS(SkDWriteFontFileStream, (fontFileStream.get()));
232 }
233
234 SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* d esc) const {
235 return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this) , desc));
236 }
237
238 void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const {
239 if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag ||
240 rec->fFlags & SkScalerContext::kLCD_Vertical_Flag)
241 {
242 rec->fMaskFormat = SkMask::kA8_Format;
243 }
244
245 unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag |
246 SkScalerContext::kForceAutohinting_Flag |
247 SkScalerContext::kEmbolden_Flag |
248 SkScalerContext::kLCD_BGROrder_Flag |
249 SkScalerContext::kLCD_Vertical_Flag;
250 rec->fFlags &= ~flagsWeDontSupport;
251
252 SkPaint::Hinting h = rec->getHinting();
253 // DirectWrite does not provide for hinting hints.
254 h = SkPaint::kSlight_Hinting;
255 rec->setHinting(h);
256
257 #if SK_FONT_HOST_USE_SYSTEM_SETTINGS
258 IDWriteFactory* factory = get_dwrite_factory();
259 if (factory != NULL) {
260 SkTScopedComPtr<IDWriteRenderingParams> defaultRenderingParams;
261 if (SUCCEEDED(factory->CreateRenderingParams(&defaultRenderingParams))) {
262 float gamma = defaultRenderingParams->GetGamma();
263 rec->setDeviceGamma(gamma);
264 rec->setPaintGamma(gamma);
265
266 rec->setContrast(defaultRenderingParams->GetEnhancedContrast());
267 }
268 }
269 #endif
270 }
271
272 ///////////////////////////////////////////////////////////////////////////////
273 //PDF Support
274
275 using namespace skia_advanced_typeface_metrics_utils;
276
277 // Construct Glyph to Unicode table.
278 // Unicode code points that require conjugate pairs in utf16 are not
279 // supported.
280 // TODO(arthurhsu): Add support for conjugate pairs. It looks like that may
281 // require parsing the TTF cmap table (platform 4, encoding 12) directly instead
282 // of calling GetFontUnicodeRange().
283 // TODO(bungeman): This never does what anyone wants.
284 // What is really wanted is the text to glyphs mapping
285 static void populate_glyph_to_unicode(IDWriteFontFace* fontFace,
286 const unsigned glyphCount,
287 SkTDArray<SkUnichar>* glyphToUnicode) {
288 HRESULT hr = S_OK;
289
290 //Do this like free type instead
291 UINT32 count = 0;
292 for (UINT32 c = 0; c < 0x10FFFF; ++c) {
293 UINT16 glyph;
294 hr = fontFace->GetGlyphIndices(&c, 1, &glyph);
295 if (glyph > 0) {
296 ++count;
297 }
298 }
299
300 SkAutoTArray<UINT32> chars(count);
301 count = 0;
302 for (UINT32 c = 0; c < 0x10FFFF; ++c) {
303 UINT16 glyph;
304 hr = fontFace->GetGlyphIndices(&c, 1, &glyph);
305 if (glyph > 0) {
306 chars[count] = c;
307 ++count;
308 }
309 }
310
311 SkAutoTArray<UINT16> glyph(count);
312 fontFace->GetGlyphIndices(chars.get(), count, glyph.get());
313
314 USHORT maxGlyph = 0;
315 for (USHORT j = 0; j < count; ++j) {
316 if (glyph[j] > maxGlyph) maxGlyph = glyph[j];
317 }
318
319 glyphToUnicode->setCount(maxGlyph+1);
320 for (USHORT j = 0; j < maxGlyph+1u; ++j) {
321 (*glyphToUnicode)[j] = 0;
322 }
323
324 //'invert'
325 for (USHORT j = 0; j < count; ++j) {
326 if (glyph[j] < glyphCount && (*glyphToUnicode)[glyph[j]] == 0) {
327 (*glyphToUnicode)[glyph[j]] = chars[j];
328 }
329 }
330 }
331
332 static bool getWidthAdvance(IDWriteFontFace* fontFace, int gId, int16_t* advance ) {
333 SkASSERT(advance);
334
335 UINT16 glyphId = gId;
336 DWRITE_GLYPH_METRICS gm;
337 HRESULT hr = fontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm);
338
339 if (FAILED(hr)) {
340 *advance = 0;
341 return false;
342 }
343
344 *advance = gm.advanceWidth;
345 return true;
346 }
347
348 SkAdvancedTypefaceMetrics* DWriteFontTypeface::onGetAdvancedTypefaceMetrics(
349 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
350 const uint32_t* glyphIDs,
351 uint32_t glyphIDsCount) const {
352
353 SkAdvancedTypefaceMetrics* info = NULL;
354
355 HRESULT hr = S_OK;
356
357 const unsigned glyphCount = fDWriteFontFace->GetGlyphCount();
358
359 DWRITE_FONT_METRICS dwfm;
360 fDWriteFontFace->GetMetrics(&dwfm);
361
362 info = new SkAdvancedTypefaceMetrics;
363 info->fEmSize = dwfm.designUnitsPerEm;
364 info->fMultiMaster = false;
365 info->fLastGlyphID = SkToU16(glyphCount - 1);
366 info->fStyle = 0;
367
368
369 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
370 SkTScopedComPtr<IDWriteLocalizedStrings> faceNames;
371 hr = fDWriteFontFamily->GetFamilyNames(&familyNames);
372 hr = fDWriteFont->GetFaceNames(&faceNames);
373
374 UINT32 familyNameLength;
375 hr = familyNames->GetStringLength(0, &familyNameLength);
376
377 UINT32 faceNameLength;
378 hr = faceNames->GetStringLength(0, &faceNameLength);
379
380 UINT32 size = familyNameLength+1+faceNameLength+1;
381 SkSMallocWCHAR wFamilyName(size);
382 hr = familyNames->GetString(0, wFamilyName.get(), size);
383 wFamilyName[familyNameLength] = L' ';
384 hr = faceNames->GetString(0, &wFamilyName[familyNameLength+1], size - faceNa meLength + 1);
385
386 hr = sk_wchar_to_skstring(wFamilyName.get(), &info->fFontName);
387
388 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
389 populate_glyph_to_unicode(fDWriteFontFace.get(), glyphCount, &(info->fGl yphToUnicode));
390 }
391
392 DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType();
393 if (fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE ||
394 fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION) {
395 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
396 } else {
397 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
398 info->fItalicAngle = 0;
399 info->fAscent = dwfm.ascent;;
400 info->fDescent = dwfm.descent;
401 info->fStemV = 0;
402 info->fCapHeight = dwfm.capHeight;
403 info->fBBox = SkIRect::MakeEmpty();
404 return info;
405 }
406
407 AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get());
408 AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get());
409 AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get()) ;
410 AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get());
411 if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Ta ble.fExists) {
412 info->fItalicAngle = 0;
413 info->fAscent = dwfm.ascent;;
414 info->fDescent = dwfm.descent;
415 info->fStemV = 0;
416 info->fCapHeight = dwfm.capHeight;
417 info->fBBox = SkIRect::MakeEmpty();
418 return info;
419 }
420
421 //There exist CJK fonts which set the IsFixedPitch and Monospace bits,
422 //but have full width, latin half-width, and half-width kana.
423 bool fixedWidth = (postTable->isFixedPitch &&
424 (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics)));
425 //Monospace
426 if (fixedWidth) {
427 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
428 }
429 //Italic
430 if (os2Table->version.v0.fsSelection.field.Italic) {
431 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
432 }
433 //Script
434 if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType. value) {
435 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
436 //Serif
437 } else if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.pano se.bFamilyType.value &&
438 SkPanose::Data::TextAndDisplay::SerifStyle::Triangle <= os2Table- >version.v0.panose.data.textAndDisplay.bSerifStyle.value &&
439 SkPanose::Data::TextAndDisplay::SerifStyle::NoFit != os2Table->ve rsion.v0.panose.data.textAndDisplay.bSerifStyle.value) {
440 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
441 }
442
443 info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16;
444
445 info->fAscent = SkToS16(dwfm.ascent);
446 info->fDescent = SkToS16(dwfm.descent);
447 info->fCapHeight = SkToS16(dwfm.capHeight);
448
449 info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTab le->xMin),
450 (int32_t)SkEndian_SwapBE16((uint16_t)headTab le->yMax),
451 (int32_t)SkEndian_SwapBE16((uint16_t)headTab le->xMax),
452 (int32_t)SkEndian_SwapBE16((uint16_t)headTab le->yMin));
453
454 //TODO: is this even desired? It seems PDF only wants this value for Type1
455 //fonts, and we only get here for TrueType fonts.
456 info->fStemV = 0;
457 /*
458 // Figure out a good guess for StemV - Min width of i, I, !, 1.
459 // This probably isn't very good with an italic font.
460 int16_t min_width = SHRT_MAX;
461 info->fStemV = 0;
462 char stem_chars[] = {'i', 'I', '!', '1'};
463 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
464 ABC abcWidths;
465 if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
466 int16_t width = abcWidths.abcB;
467 if (width > 0 && width < min_width) {
468 min_width = width;
469 info->fStemV = min_width;
470 }
471 }
472 }
473 */
474
475 // If Restricted, the font may not be embedded in a document.
476 // If not Restricted, the font can be embedded.
477 // If PreviewPrint, the embedding is read-only.
478 if (os2Table->version.v0.fsType.field.Restricted) {
479 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
480 } else if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
481 if (fixedWidth) {
482 appendRange(&info->fGlyphWidths, 0);
483 int16_t advance;
484 getWidthAdvance(fDWriteFontFace.get(), 1, &advance);
485 info->fGlyphWidths->fAdvance.append(1, &advance);
486 finishRange(info->fGlyphWidths.get(), 0,
487 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
488 } else {
489 info->fGlyphWidths.reset(
490 getAdvanceData(fDWriteFontFace.get(),
491 glyphCount,
492 glyphIDs,
493 glyphIDsCount,
494 getWidthAdvance));
495 }
496 }
497
498 return info;
499 }
OLDNEW
« no previous file with comments | « src/ports/SkTypeface_win_dw.h ('k') | src/utils/win/SkDWrite.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698