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    virtual bool next(SkTypeface::LocalizedString* localizedString) SK_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    SkAutoTUnref<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
212SkStream* 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_BGROrder_Flag ||
247        rec->fFlags & SkScalerContext::kLCD_Vertical_Flag)
248    {
249        rec->fMaskFormat = SkMask::kA8_Format;
250    }
251
252    unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag |
253                                  SkScalerContext::kDevKernText_Flag |
254                                  SkScalerContext::kForceAutohinting_Flag |
255                                  SkScalerContext::kEmbolden_Flag |
256                                  SkScalerContext::kLCD_BGROrder_Flag |
257                                  SkScalerContext::kLCD_Vertical_Flag;
258    rec->fFlags &= ~flagsWeDontSupport;
259
260    SkPaint::Hinting h = rec->getHinting();
261    // DirectWrite does not provide for hinting hints.
262    h = SkPaint::kSlight_Hinting;
263    rec->setHinting(h);
264
265#if SK_FONT_HOST_USE_SYSTEM_SETTINGS
266    IDWriteFactory* factory = get_dwrite_factory();
267    if (factory != NULL) {
268        SkTScopedComPtr<IDWriteRenderingParams> defaultRenderingParams;
269        if (SUCCEEDED(factory->CreateRenderingParams(&defaultRenderingParams))) {
270            float gamma = defaultRenderingParams->GetGamma();
271            rec->setDeviceGamma(gamma);
272            rec->setPaintGamma(gamma);
273
274            rec->setContrast(defaultRenderingParams->GetEnhancedContrast());
275        }
276    }
277#endif
278}
279
280///////////////////////////////////////////////////////////////////////////////
281//PDF Support
282
283using namespace skia_advanced_typeface_metrics_utils;
284
285// Construct Glyph to Unicode table.
286// Unicode code points that require conjugate pairs in utf16 are not
287// supported.
288// TODO(bungeman): This never does what anyone wants.
289// What is really wanted is the text to glyphs mapping
290static void populate_glyph_to_unicode(IDWriteFontFace* fontFace,
291                                      const unsigned glyphCount,
292                                      SkTDArray<SkUnichar>* glyphToUnicode) {
293    HRESULT hr = S_OK;
294
295    //Do this like free type instead
296    SkAutoTMalloc<SkUnichar> glyphToUni(glyphCount);
297    int maxGlyph = -1;
298    for (UINT32 c = 0; c < 0x10FFFF; ++c) {
299        UINT16 glyph;
300        hr = fontFace->GetGlyphIndices(&c, 1, &glyph);
301        SkASSERT(glyph < glyphCount);
302        if (0 < glyph) {
303            maxGlyph = SkTMax(static_cast<int>(glyph), maxGlyph);
304            glyphToUni[glyph] = c;
305        }
306    }
307
308    SkTDArray<SkUnichar>(glyphToUni, maxGlyph + 1).swap(*glyphToUnicode);
309}
310
311static bool getWidthAdvance(IDWriteFontFace* fontFace, int gId, int16_t* advance) {
312    SkASSERT(advance);
313
314    UINT16 glyphId = gId;
315    DWRITE_GLYPH_METRICS gm;
316    HRESULT hr = fontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm);
317
318    if (FAILED(hr)) {
319        *advance = 0;
320        return false;
321    }
322
323    *advance = gm.advanceWidth;
324    return true;
325}
326
327SkAdvancedTypefaceMetrics* DWriteFontTypeface::onGetAdvancedTypefaceMetrics(
328        SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
329        const uint32_t* glyphIDs,
330        uint32_t glyphIDsCount) const {
331
332    SkAdvancedTypefaceMetrics* info = NULL;
333
334    HRESULT hr = S_OK;
335
336    const unsigned glyphCount = fDWriteFontFace->GetGlyphCount();
337
338    DWRITE_FONT_METRICS dwfm;
339    fDWriteFontFace->GetMetrics(&dwfm);
340
341    info = new SkAdvancedTypefaceMetrics;
342    info->fEmSize = dwfm.designUnitsPerEm;
343    info->fLastGlyphID = SkToU16(glyphCount - 1);
344    info->fStyle = 0;
345    info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
346
347    // SkAdvancedTypefaceMetrics::fFontName is in theory supposed to be
348    // the PostScript name of the font. However, due to the way it is currently
349    // used, it must actually be a family name.
350    SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
351    hr = fDWriteFontFamily->GetFamilyNames(&familyNames);
352
353    UINT32 familyNameLen;
354    hr = familyNames->GetStringLength(0, &familyNameLen);
355
356    SkSMallocWCHAR familyName(familyNameLen+1);
357    hr = familyNames->GetString(0, familyName.get(), familyNameLen+1);
358
359    hr = sk_wchar_to_skstring(familyName.get(), familyNameLen, &info->fFontName);
360
361    if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
362        populate_glyph_to_unicode(fDWriteFontFace.get(), glyphCount, &(info->fGlyphToUnicode));
363    }
364
365    DWRITE_FONT_FACE_TYPE fontType = fDWriteFontFace->GetType();
366    if (fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE ||
367        fontType == DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION) {
368        info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
369    } else {
370        info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
371        info->fItalicAngle = 0;
372        info->fAscent = dwfm.ascent;;
373        info->fDescent = dwfm.descent;
374        info->fStemV = 0;
375        info->fCapHeight = dwfm.capHeight;
376        info->fBBox = SkIRect::MakeEmpty();
377        return info;
378    }
379
380    AutoTDWriteTable<SkOTTableHead> headTable(fDWriteFontFace.get());
381    AutoTDWriteTable<SkOTTablePostScript> postTable(fDWriteFontFace.get());
382    AutoTDWriteTable<SkOTTableHorizontalHeader> hheaTable(fDWriteFontFace.get());
383    AutoTDWriteTable<SkOTTableOS2> os2Table(fDWriteFontFace.get());
384    if (!headTable.fExists || !postTable.fExists || !hheaTable.fExists || !os2Table.fExists) {
385        info->fItalicAngle = 0;
386        info->fAscent = dwfm.ascent;;
387        info->fDescent = dwfm.descent;
388        info->fStemV = 0;
389        info->fCapHeight = dwfm.capHeight;
390        info->fBBox = SkIRect::MakeEmpty();
391        return info;
392    }
393
394    //There exist CJK fonts which set the IsFixedPitch and Monospace bits,
395    //but have full width, latin half-width, and half-width kana.
396    bool fixedWidth = (postTable->isFixedPitch &&
397                      (1 == SkEndian_SwapBE16(hheaTable->numberOfHMetrics)));
398    //Monospace
399    if (fixedWidth) {
400        info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
401    }
402    //Italic
403    if (os2Table->version.v0.fsSelection.field.Italic) {
404        info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
405    }
406    //Script
407    if (SkPanose::FamilyType::Script == os2Table->version.v0.panose.bFamilyType.value) {
408        info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
409    //Serif
410    } else if (SkPanose::FamilyType::TextAndDisplay == os2Table->version.v0.panose.bFamilyType.value &&
411               SkPanose::Data::TextAndDisplay::SerifStyle::Triangle <= os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value &&
412               SkPanose::Data::TextAndDisplay::SerifStyle::NoFit != os2Table->version.v0.panose.data.textAndDisplay.bSerifStyle.value) {
413        info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
414    }
415
416    info->fItalicAngle = SkEndian_SwapBE32(postTable->italicAngle) >> 16;
417
418    info->fAscent = SkToS16(dwfm.ascent);
419    info->fDescent = SkToS16(dwfm.descent);
420    info->fCapHeight = SkToS16(dwfm.capHeight);
421
422    info->fBBox = SkIRect::MakeLTRB((int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMin),
423                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMax),
424                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->xMax),
425                                    (int32_t)SkEndian_SwapBE16((uint16_t)headTable->yMin));
426
427    //TODO: is this even desired? It seems PDF only wants this value for Type1
428    //fonts, and we only get here for TrueType fonts.
429    info->fStemV = 0;
430    /*
431    // Figure out a good guess for StemV - Min width of i, I, !, 1.
432    // This probably isn't very good with an italic font.
433    int16_t min_width = SHRT_MAX;
434    info->fStemV = 0;
435    char stem_chars[] = {'i', 'I', '!', '1'};
436    for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
437        ABC abcWidths;
438        if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
439            int16_t width = abcWidths.abcB;
440            if (width > 0 && width < min_width) {
441                min_width = width;
442                info->fStemV = min_width;
443            }
444        }
445    }
446    */
447
448    if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
449        if (fixedWidth) {
450            appendRange(&info->fGlyphWidths, 0);
451            int16_t advance;
452            getWidthAdvance(fDWriteFontFace.get(), 1, &advance);
453            info->fGlyphWidths->fAdvance.append(1, &advance);
454            finishRange(info->fGlyphWidths.get(), 0,
455                        SkAdvancedTypefaceMetrics::WidthRange::kDefault);
456        } else {
457            info->fGlyphWidths.reset(
458                getAdvanceData(fDWriteFontFace.get(),
459                               glyphCount,
460                               glyphIDs,
461                               glyphIDsCount,
462                               getWidthAdvance));
463        }
464    }
465
466    return info;
467}
468