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