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
21void 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
40static SkUnichar next_utf8(const void** chars) {
41    return SkUTF8_NextUnichar((const char**)chars);
42}
43
44static SkUnichar next_utf16(const void** chars) {
45    return SkUTF16_NextUnichar((const uint16_t**)chars);
46}
47
48static 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
55typedef SkUnichar (*EncodingProc)(const void**);
56
57static 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
65int 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 += scratchCount) {
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
114int DWriteFontTypeface::onCountGlyphs() const {
115    return fDWriteFontFace->GetGlyphCount();
116}
117
118int DWriteFontTypeface::onGetUPEM() const {
119    DWRITE_FONT_METRICS metrics;
120    fDWriteFontFace->GetMetrics(&metrics);
121    return metrics.designUnitsPerEm;
122}
123
124class LocalizedStrings_IDWriteLocalizedStrings : public SkTypeface::LocalizedStrings {
125public:
126    /** Takes ownership of the IDWriteLocalizedStrings. */
127    explicit LocalizedStrings_IDWriteLocalizedStrings(IDWriteLocalizedStrings* strings)
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 string length.");
139        stringLength += 1;
140
141        SkSMallocWCHAR wString(stringLength);
142        HRBM(fStrings->GetString(fIndex, wString.get(), stringLength), "Could not 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 get locale length.");
149        localeLength += 1;
150
151        SkSMallocWCHAR wLocale(localeLength);
152        HRBM(fStrings->GetLocaleName(fIndex, wLocale.get(), localeLength), "Could not get locale.");
153
154        HRB(sk_wchar_to_skstring(wLocale.get(), &localizedString->fLanguage));
155
156        ++fIndex;
157        return true;
158    }
159
160private:
161    UINT32 fIndex;
162    SkTScopedComPtr<IDWriteLocalizedStrings> fStrings;
163};
164
165SkTypeface::LocalizedStrings* DWriteFontTypeface::onCreateFamilyNameIterator() const {
166    SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
167    HRNM(fDWriteFontFamily->GetFamilyNames(&familyNames), "Could not obtain family names.");
168
169    return new LocalizedStrings_IDWriteLocalizedStrings(familyNames.release());
170}
171
172int 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
186size_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
205SkStream* 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 files.");
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
234SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* desc) const {
235    return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this), desc));
236}
237
238void 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
275using 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
285static 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
332static 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
348SkAdvancedTypefaceMetrics* 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->fLastGlyphID = SkToU16(glyphCount - 1);
365    info->fStyle = 0;
366    info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
367
368    // SkAdvancedTypefaceMetrics::fFontName is in theory supposed to be
369    // the PostScript name of the font. However, due to the way it is currently
370    // used, it must actually be a family name.
371    SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
372    hr = fDWriteFontFamily->GetFamilyNames(&familyNames);
373
374    UINT32 familyNameLength;
375    hr = familyNames->GetStringLength(0, &familyNameLength);
376
377    UINT32 size = familyNameLength+1;
378    SkSMallocWCHAR wFamilyName(size);
379    hr = familyNames->GetString(0, wFamilyName.get(), size);
380
381    hr = sk_wchar_to_skstring(wFamilyName.get(), &info->fFontName);
382
383    if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
384        populate_glyph_to_unicode(fDWriteFontFace.get(), glyphCount, &(info->fGlyphToUnicode));
385    }
386
387    DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType();
388    if (fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE ||
389        fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION) {
390        info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
391    } else {
392        info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
393        info->fItalicAngle = 0;
394        info->fAscent = dwfm.ascent;;
395        info->fDescent = dwfm.descent;
396        info->fStemV = 0;
397        info->fCapHeight = dwfm.capHeight;
398        info->fBBox = SkIRect::MakeEmpty();
399        return info;
400    }
401
402    AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get());
403    AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get());
404    AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get());
405    AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get());
406    if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Table.fExists) {
407        info->fItalicAngle = 0;
408        info->fAscent = dwfm.ascent;;
409        info->fDescent = dwfm.descent;
410        info->fStemV = 0;
411        info->fCapHeight = dwfm.capHeight;
412        info->fBBox = SkIRect::MakeEmpty();
413        return info;
414    }
415
416    //There exist CJK fonts which set the IsFixedPitch and Monospace bits,
417    //but have full width, latin half-width, and half-width kana.
418    bool fixedWidth = (postTable->isFixedPitch &&
419                      (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics)));
420    //Monospace
421    if (fixedWidth) {
422        info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
423    }
424    //Italic
425    if (os2Table->version.v0.fsSelection.field.Italic) {
426        info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
427    }
428    //Script
429    if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType.value) {
430        info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
431    //Serif
432    } else if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.panose.bFamilyType.value &&
433               SkPanose::Data::TextAndDisplay::SerifStyle::Triangle <= os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value &&
434               SkPanose::Data::TextAndDisplay::SerifStyle::NoFit != os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value) {
435        info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
436    }
437
438    info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16;
439
440    info->fAscent = SkToS16(dwfm.ascent);
441    info->fDescent = SkToS16(dwfm.descent);
442    info->fCapHeight = SkToS16(dwfm.capHeight);
443
444    info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMin),
445                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMax),
446                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMax),
447                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMin));
448
449    //TODO: is this even desired? It seems PDF only wants this value for Type1
450    //fonts, and we only get here for TrueType fonts.
451    info->fStemV = 0;
452    /*
453    // Figure out a good guess for StemV - Min width of i, I, !, 1.
454    // This probably isn't very good with an italic font.
455    int16_t min_width = SHRT_MAX;
456    info->fStemV = 0;
457    char stem_chars[] = {'i', 'I', '!', '1'};
458    for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
459        ABC abcWidths;
460        if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
461            int16_t width = abcWidths.abcB;
462            if (width > 0 && width < min_width) {
463                min_width = width;
464                info->fStemV = min_width;
465            }
466        }
467    }
468    */
469
470    if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
471        if (fixedWidth) {
472            appendRange(&info->fGlyphWidths, 0);
473            int16_t advance;
474            getWidthAdvance(fDWriteFontFace.get(), 1, &advance);
475            info->fGlyphWidths->fAdvance.append(1, &advance);
476            finishRange(info->fGlyphWidths.get(), 0,
477                        SkAdvancedTypefaceMetrics::WidthRange::kDefault);
478        } else {
479            info->fGlyphWidths.reset(
480                getAdvanceData(fDWriteFontFace.get(),
481                               glyphCount,
482                               glyphIDs,
483                               glyphIDsCount,
484                               getWidthAdvance));
485        }
486    }
487
488    return info;
489}
490