1ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com */
7ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
8ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkAdvancedTypefaceMetrics.h"
9a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkBase64.h"
101bfe01d06b896eca94c80d9af9a6bb30216b5e42bungeman@google.com#include "SkColorPriv.h"
11a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkData.h"
12a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkDescriptor.h"
13e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#include "SkFontDescriptor.h"
14bbe50131413098bffed9186660c41f047d881596bungeman@google.com#include "SkGlyph.h"
1527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com#include "SkHRESULT.h"
1697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com#include "SkMaskGamma.h"
17d3fbd34099a530b5415c95b1f2f8149ac417b9b3bungeman@google.com#include "SkMatrix22.h"
187bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com#include "SkOTTable_maxp.h"
19a980269c2498836101146adc729ef780fb89824ebungeman@google.com#include "SkOTTable_name.h"
20a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkOTUtils.h"
2127889877c0e3c909dd4afd7b5e7b1d58cbf61ae6reed@google.com#include "SkPath.h"
22b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com#include "SkSFNTHeader.h"
23ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkStream.h"
24a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkString.h"
2505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com#include "SkTemplates.h"
26ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkThread.h"
27ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkTypeface_win.h"
2859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com#include "SkTypefaceCache.h"
29ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkUtils.h"
30ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
31a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkTypes.h"
32a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <tchar.h>
33a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <usp10.h>
34a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <objbase.h>
35ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
36f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.comstatic void (*gEnsureLOGFONTAccessibleProc)(const LOGFONT&);
37f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com
38f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.comvoid SkTypeface_SetEnsureLOGFONTAccessibleProc(void (*proc)(const LOGFONT&)) {
39f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com    gEnsureLOGFONTAccessibleProc = proc;
40f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com}
41f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com
42055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.comstatic void call_ensure_accessible(const LOGFONT& lf) {
43055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    if (gEnsureLOGFONTAccessibleProc) {
44055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        gEnsureLOGFONTAccessibleProc(lf);
45055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    }
46055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com}
47055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
48055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com///////////////////////////////////////////////////////////////////////////////
49055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
506f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com// always packed xxRRGGBB
516f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.comtypedef uint32_t SkGdiRGB;
526f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
53a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// define this in your Makefile or .gyp to enforce AA requests
54a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// which GDI ignores at small sizes. This flag guarantees AA
55a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// for rotated text, regardless of GDI's notions.
56a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com//#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
57a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
5882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.comstatic bool isLCD(const SkScalerContext::Rec& rec) {
59d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed    return SkMask::kLCD16_Format == rec.fMaskFormat;
6082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
6182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
62a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool bothZero(SkScalar a, SkScalar b) {
63a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    return 0 == a && 0 == b;
64a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
65a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
66a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// returns false if there is any non-90-rotation or skew
67a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool isAxisAligned(const SkScalerContext::Rec& rec) {
68a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    return 0 == rec.fPreSkewX &&
69a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com           (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
70a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com            bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
71a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
72a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
73a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool needToRenderWithSkia(const SkScalerContext::Rec& rec) {
74a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com#ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
75a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // What we really want to catch is when GDI will ignore the AA request and give
76a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // us BW instead. Smallish rotated text is one heuristic, so this code is just
77a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // an approximation. We shouldn't need to do this for larger sizes, but at those
78a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // sizes, the quality difference gets less and less between our general
79a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // scanconverter and GDI's.
80a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) {
81a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com        return true;
82a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    }
83a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com#endif
840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    return rec.getHinting() == SkPaint::kNo_Hinting || rec.getHinting() == SkPaint::kSlight_Hinting;
85a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
86a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
87ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comusing namespace skia_advanced_typeface_metrics_utils;
88ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
89a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.comstatic void tchar_to_skstring(const TCHAR t[], SkString* s) {
90484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#ifdef UNICODE
91484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    size_t sSize = WideCharToMultiByte(CP_UTF8, 0, t, -1, NULL, 0, NULL, NULL);
92484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    s->resize(sSize);
93484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    WideCharToMultiByte(CP_UTF8, 0, t, -1, s->writable_str(), sSize, NULL, NULL);
94484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#else
95484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    s->set(t);
96484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#endif
97484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com}
98484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
99a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic void dcfontname_to_skstring(HDC deviceContext, const LOGFONT& lf, SkString* familyName) {
100a980269c2498836101146adc729ef780fb89824ebungeman@google.com    int fontNameLen; //length of fontName in TCHARS.
101a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (0 == (fontNameLen = GetTextFace(deviceContext, 0, NULL))) {
102a980269c2498836101146adc729ef780fb89824ebungeman@google.com        call_ensure_accessible(lf);
103a980269c2498836101146adc729ef780fb89824ebungeman@google.com        if (0 == (fontNameLen = GetTextFace(deviceContext, 0, NULL))) {
104a980269c2498836101146adc729ef780fb89824ebungeman@google.com            fontNameLen = 0;
105a980269c2498836101146adc729ef780fb89824ebungeman@google.com        }
106a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
107a980269c2498836101146adc729ef780fb89824ebungeman@google.com
108a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkAutoSTArray<LF_FULLFACESIZE, TCHAR> fontName(fontNameLen+1);
109a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) {
110a980269c2498836101146adc729ef780fb89824ebungeman@google.com        call_ensure_accessible(lf);
111a980269c2498836101146adc729ef780fb89824ebungeman@google.com        if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) {
112a980269c2498836101146adc729ef780fb89824ebungeman@google.com            fontName[0] = 0;
113a980269c2498836101146adc729ef780fb89824ebungeman@google.com        }
114a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
115a980269c2498836101146adc729ef780fb89824ebungeman@google.com
116a980269c2498836101146adc729ef780fb89824ebungeman@google.com    tchar_to_skstring(fontName.get(), familyName);
117a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
118a980269c2498836101146adc729ef780fb89824ebungeman@google.com
119ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic void make_canonical(LOGFONT* lf) {
12053cbb0baac09e60e7633c15301a29a83afb1a8debungeman@google.com    lf->lfHeight = -64;
12159d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY;
12259d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfCharSet = DEFAULT_CHARSET;
12382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com//    lf->lfClipPrecision = 64;
12459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com}
12559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
126a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungemanstatic SkFontStyle get_style(const LOGFONT& lf) {
127a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman    return SkFontStyle(lf.lfWeight,
128a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman                       lf.lfWidth,
129a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman                       lf.lfItalic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
130ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
131ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
132ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic inline FIXED SkFixedToFIXED(SkFixed x) {
133ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return *(FIXED*)(&x);
134ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
135a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.comstatic inline SkFixed SkFIXEDToFixed(FIXED x) {
136a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    return *(SkFixed*)(&x);
137a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com}
138ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
139ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic inline FIXED SkScalarToFIXED(SkScalar x) {
140ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return SkFixedToFIXED(SkScalarToFixed(x));
141ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
142ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1434732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.comstatic inline SkScalar SkFIXEDToScalar(FIXED x) {
1444732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com    return SkFixedToScalar(SkFIXEDToFixed(x));
1454732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com}
1464732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com
1477bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comstatic unsigned calculateGlyphCount(HDC hdc, const LOGFONT& lf) {
1487bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    TEXTMETRIC textMetric;
1497bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == GetTextMetrics(hdc, &textMetric)) {
1507bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        textMetric.tmPitchAndFamily = TMPF_VECTOR;
1517bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
1527bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        GetTextMetrics(hdc, &textMetric);
1537bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1547bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
1557bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) {
1567bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        return textMetric.tmLastChar;
1577bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1587bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
159ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes.
160ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint16_t glyphs;
1617bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (GDI_ERROR != GetFontData(hdc, SkOTTableMaximumProfile::TAG, 4, &glyphs, sizeof(glyphs))) {
162ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        return SkEndian_SwapBE16(glyphs);
163ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1645aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org
165ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Binary search for glyph count.
166ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
167ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int32_t max = SK_MaxU16 + 1;
168ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int32_t min = 0;
169ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
170ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    while (min < max) {
171ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        int32_t mid = min + ((max - min) / 2);
172ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0,
173ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                             NULL, &mat2) == GDI_ERROR) {
174ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            max = mid;
175ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        } else {
176ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            min = mid + 1;
177ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
178ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
179ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(min == max);
180ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return min;
181ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
182ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1837bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comstatic unsigned calculateUPEM(HDC hdc, const LOGFONT& lf) {
1847bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    TEXTMETRIC textMetric;
1857bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == GetTextMetrics(hdc, &textMetric)) {
1867bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        textMetric.tmPitchAndFamily = TMPF_VECTOR;
1877bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
1887bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        GetTextMetrics(hdc, &textMetric);
1897bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1907bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
1917bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) {
1927bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        return textMetric.tmMaxCharWidth;
1937bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1947bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
1957bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    OUTLINETEXTMETRIC otm;
1967bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
1977bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == otmRet) {
1987bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
1997bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
2007bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
2017bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
2027bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return (0 == otmRet) ? 0 : otm.otmEMSquare;
2037bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
2047bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
205ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comclass LogFontTypeface : public SkTypeface {
206ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.compublic:
207a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman    LogFontTypeface(const SkFontStyle& style, const LOGFONT& lf, bool serializeAsStream)
208a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        : SkTypeface(style, SkTypefaceCache::NewFontID(), false)
209a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        , fLogFont(lf)
210a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        , fSerializeAsStream(serializeAsStream)
211a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman    {
212cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
213cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // If the font has cubic outlines, it will not be rendered with ClearType.
214cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HFONT font = CreateFontIndirect(&lf);
215cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
216cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HDC deviceContext = ::CreateCompatibleDC(NULL);
217cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HFONT savefont = (HFONT)SelectObject(deviceContext, font);
218cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
219cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        TEXTMETRIC textMetric;
220cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (0 == GetTextMetrics(deviceContext, &textMetric)) {
221055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com            call_ensure_accessible(lf);
222cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            if (0 == GetTextMetrics(deviceContext, &textMetric)) {
223cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com                textMetric.tmPitchAndFamily = TMPF_TRUETYPE;
224cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            }
225cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
226cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (deviceContext) {
227cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::SelectObject(deviceContext, savefont);
228cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::DeleteDC(deviceContext);
229cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
230cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (font) {
231cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::DeleteObject(font);
232cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
233cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
234fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com        // The fixed pitch bit is set if the font is *not* fixed pitch.
235fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com        this->setIsFixedPitch((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0);
236fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com
237cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // Used a logfont on a memory context, should never get a device font.
238cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // Therefore all TMPF_DEVICE will be PostScript (cubic) fonts.
239cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) &&
240cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com                      (textMetric.tmPitchAndFamily & TMPF_DEVICE));
241cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    }
242ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
24359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    LOGFONT fLogFont;
244e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com    bool fSerializeAsStream;
245cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    bool fCanBeLCD;
246ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
24759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    static LogFontTypeface* Create(const LOGFONT& lf) {
248a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        return new LogFontTypeface(get_style(lf), lf, false);
249ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
2500da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com
251055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    static void EnsureAccessible(const SkTypeface* face) {
252055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFont);
253055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    }
254055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
2550da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comprotected:
25636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkStreamAsset* onOpenStream(int* ttcIndex) const override;
25736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override;
25836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onFilterRec(SkScalerContextRec*) const override;
25939a9a5069b25aa8b9529118cfd0ece5f412fb56breed    SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
26039a9a5069b25aa8b9529118cfd0ece5f412fb56breed                                PerGlyphInfo, const uint32_t*, uint32_t) const override;
26136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
2623c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
26336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                uint16_t glyphs[], int glyphCount) const override;
26436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    int onCountGlyphs() const override;
26536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    int onGetUPEM() const override;
26636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onGetFamilyName(SkString* familyName) const override;
26736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
26836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    int onGetTableTags(SkFontTableTag tags[]) const override;
269b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    virtual size_t onGetTableData(SkFontTableTag, size_t offset,
27036352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                  size_t length, void* data) const override;
271ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com};
272ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
273a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comclass FontMemResourceTypeface : public LogFontTypeface {
274a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.compublic:
275a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    /**
276a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     *  The created FontMemResourceTypeface takes ownership of fontMemResource.
277a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     */
278a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemResource) {
279a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        return new FontMemResourceTypeface(get_style(lf), lf, fontMemResource);
280a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
281a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
282a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comprotected:
28336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void weak_dispose() const override {
284a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        RemoveFontMemResourceEx(fFontMemResource);
285a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        //SkTypefaceCache::Remove(this);
286a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        INHERITED::weak_dispose();
287a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
288a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
289a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comprivate:
29072cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    /**
29172cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     *  Takes ownership of fontMemResource.
29272cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com     */
293a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman    FontMemResourceTypeface(const SkFontStyle& style, const LOGFONT& lf, HANDLE fontMemResource)
294a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        : LogFontTypeface(style, lf, true), fFontMemResource(fontMemResource)
295a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman    { }
29672cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
29772cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com    HANDLE fFontMemResource;
29872cf4fcafa54cfa04c5ec7cb8eaa3acb144712ddbungeman@google.com
299a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    typedef LogFontTypeface INHERITED;
300a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com};
301a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
302ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic const LOGFONT& get_default_font() {
303ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static LOGFONT gDefaultFont;
304ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return gDefaultFont;
305ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
306ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
307a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungemanstatic bool FindByLogFont(SkTypeface* face, const SkFontStyle& requestedStyle, void* ctx) {
308a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LogFontTypeface* lface = static_cast<LogFontTypeface*>(face);
30959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx);
310ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
311a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return lface &&
312a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com           get_style(lface->fLogFont) == requestedStyle &&
31359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com           !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT));
31459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com}
31559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
31659d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com/**
31759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com *  This guy is public. It first searches the cache, and if a match is not found,
31859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com *  it creates a new face.
31959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com */
32059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.comSkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) {
32159d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    LOGFONT lf = origLF;
32259d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    make_canonical(&lf);
323ee51d1a6e43615ae640410013c36d2d9873e6aa5bungeman@google.com    SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByLogFont, &lf);
324ee51d1a6e43615ae640410013c36d2d9873e6aa5bungeman@google.com    if (NULL == face) {
32559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        face = LogFontTypeface::Create(lf);
32690d812b77ab025d5fd01dab904287f5c5bf56228bungeman@google.com        SkTypefaceCache::Add(face, get_style(lf));
32759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    }
32859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    return face;
329ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
330ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
331db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com/**
332a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com *  The created SkTypeface takes ownership of fontMemResource.
333a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
334a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comSkTypeface* SkCreateFontMemResourceTypefaceFromLOGFONT(const LOGFONT& origLF, HANDLE fontMemResource) {
335a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LOGFONT lf = origLF;
336a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    make_canonical(&lf);
33760b6e9dbbc492f987a5b887dff60aec107ab70d0mtklein    // We'll never get a cache hit, so no point in putting this in SkTypefaceCache.
33860b6e9dbbc492f987a5b887dff60aec107ab70d0mtklein    return FontMemResourceTypeface::Create(lf, fontMemResource);
339a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
340a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
341a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
342db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com *  This guy is public
343db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com */
344db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.comvoid SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) {
345db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    if (NULL == face) {
346db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com        *lf = get_default_font();
347db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    } else {
348a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        *lf = static_cast<const LogFontTypeface*>(face)->fLogFont;
349db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    }
350db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com}
351db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com
3526744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// Construct Glyph to Unicode table.
3536744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// Unicode code points that require conjugate pairs in utf16 are not
3546744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// supported.
3556744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// TODO(arthurhsu): Add support for conjugate pairs. It looks like that may
3566744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// require parsing the TTF cmap table (platform 4, encoding 12) directly instead
3576744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// of calling GetFontUnicodeRange().
3586744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.orgstatic void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount,
3596744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                                      SkTDArray<SkUnichar>* glyphToUnicode) {
3606744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, NULL);
3616744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    if (!glyphSetBufferSize) {
3626744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        return;
3636744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
3646744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
3656744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    SkAutoTDeleteArray<BYTE> glyphSetBuffer(new BYTE[glyphSetBufferSize]);
3666744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    GLYPHSET* glyphSet =
3676744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get());
3686744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) {
3696744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        return;
3706744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
3716744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
3726744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    glyphToUnicode->setCount(glyphCount);
3736744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    memset(glyphToUnicode->begin(), 0, glyphCount * sizeof(SkUnichar));
3746744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    for (DWORD i = 0; i < glyphSet->cRanges; ++i) {
3756744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // There is no guarantee that within a Unicode range, the corresponding
3766744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // glyph id in a font file are continuous. So, even if we have ranges,
3776744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // we can't just use the first and last entry of the range to compute
3786744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // result. We need to enumerate them one by one.
3796744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        int count = glyphSet->ranges[i].cGlyphs;
3806744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        SkAutoTArray<WCHAR> chars(count + 1);
3816744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        chars[count] = 0;  // termintate string
3826744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        SkAutoTArray<WORD> glyph(count);
3836744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        for (USHORT j = 0; j < count; ++j) {
3846744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            chars[j] = glyphSet->ranges[i].wcLow + j;
3856744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        }
3866744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(),
3876744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                         GGI_MARK_NONEXISTING_GLYPHS);
3886744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // If the glyph ID is valid, and the glyph is not mapped, then we will
3896744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // fill in the char id into the vector. If the glyph is mapped already,
3906744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // skip it.
3916744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // TODO(arthurhsu): better improve this. e.g. Get all used char ids from
3926744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // font cache, then generate this mapping table from there. It's
3936744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // unlikely to have collisions since glyph reuse happens mostly for
3946744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // different Unicode pages.
3956744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        for (USHORT j = 0; j < count; ++j) {
3966744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            if (glyph[j] != 0xffff && glyph[j] < glyphCount &&
3976744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                (*glyphToUnicode)[glyph[j]] == 0) {
3986744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                (*glyphToUnicode)[glyph[j]] = chars[j];
3996744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            }
4006744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        }
4016744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
4026744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org}
4036744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
40499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com//////////////////////////////////////////////////////////////////////////////////////
40599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
40699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comstatic int alignTo32(int n) {
40799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    return (n + 31) & ~31;
40899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com}
40999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
41099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comstruct MyBitmapInfo : public BITMAPINFO {
41199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    RGBQUAD fMoreSpaceForColors[1];
41299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com};
41399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
41499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comclass HDCOffscreen {
41599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.compublic:
41699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDCOffscreen() {
41799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fFont = 0;
41899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fDC = 0;
41999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = 0;
42099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBits = NULL;
42199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fWidth = fHeight = 0;
42299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fIsBW = false;
42399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
42499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
42599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    ~HDCOffscreen() {
42699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (fDC) {
42799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            DeleteDC(fDC);
42899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
42999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (fBM) {
43099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            DeleteObject(fBM);
43199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
43299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
43399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
43499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    void init(HFONT font, const XFORM& xform) {
43599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fFont = font;
43699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fXform = xform;
43799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
43899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
43997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    const void* draw(const SkGlyph&, bool isBW, size_t* srcRBPtr);
44099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
44199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comprivate:
44299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDC     fDC;
44399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HBITMAP fBM;
44499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HFONT   fFont;
44599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    XFORM   fXform;
44699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    void*   fBits;  // points into fBM
44799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int     fWidth;
44899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int     fHeight;
44999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    bool    fIsBW;
45099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com};
45199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
452754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.comconst void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW,
45397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                               size_t* srcRBPtr) {
45484e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // Can we share the scalercontext's fDDC, so we don't need to create
45584e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // a separate fDC here?
45699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (0 == fDC) {
45799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fDC = CreateCompatibleDC(0);
45899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (0 == fDC) {
45999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            return NULL;
46099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
46199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetGraphicsMode(fDC, GM_ADVANCED);
46299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetBkMode(fDC, TRANSPARENT);
46399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetTextAlign(fDC, TA_LEFT | TA_BASELINE);
46499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SelectObject(fDC, fFont);
46597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com
46697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        COLORREF color = 0x00FFFFFF;
467b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com        SkDEBUGCODE(COLORREF prev =) SetTextColor(fDC, color);
46897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        SkASSERT(prev != CLR_INVALID);
46999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
47099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
47199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (fBM && (fIsBW != isBW || fWidth < glyph.fWidth || fHeight < glyph.fHeight)) {
47299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        DeleteObject(fBM);
47399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = 0;
47499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
475754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    fIsBW = isBW;
47699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
47799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    fWidth = SkMax32(fWidth, glyph.fWidth);
47899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    fHeight = SkMax32(fHeight, glyph.fHeight);
47999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
48099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int biWidth = isBW ? alignTo32(fWidth) : fWidth;
48199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
48299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (0 == fBM) {
48399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        MyBitmapInfo info;
48499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        sk_bzero(&info, sizeof(info));
48599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (isBW) {
48699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            RGBQUAD blackQuad = { 0, 0, 0, 0 };
48799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 };
48899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiColors[0] = blackQuad;
48999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiColors[1] = whiteQuad;
49099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
49199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biSize = sizeof(info.bmiHeader);
49299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biWidth = biWidth;
49399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biHeight = fHeight;
49499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biPlanes = 1;
49599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biBitCount = isBW ? 1 : 32;
49699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biCompression = BI_RGB;
49799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (isBW) {
49899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiHeader.biClrUsed = 2;
49999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
50099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0);
50199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (0 == fBM) {
50299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            return NULL;
50399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
50499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SelectObject(fDC, fBM);
50599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
50699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
50799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    // erase
50899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2);
50999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t size = fHeight * srcRB;
51097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    memset(fBits, 0, size);
51199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
51299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    XFORM xform = fXform;
51399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    xform.eDx = (float)-glyph.fLeft;
51499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    xform.eDy = (float)-glyph.fTop;
51599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    SetWorldTransform(fDC, &xform);
51699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
51799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    uint16_t glyphID = glyph.getGlyphID();
51839698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    BOOL ret = ExtTextOutW(fDC, 0, 0, ETO_GLYPH_INDEX, NULL, reinterpret_cast<LPCWSTR>(&glyphID), 1, NULL);
51999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    GdiFlush();
52039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (0 == ret) {
52139698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        return NULL;
52239698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
52399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    *srcRBPtr = srcRB;
52499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    // offset to the start of the image
52599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    return (const char*)fBits + (fHeight - glyph.fHeight) * srcRB;
52699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com}
52799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
528b8a5c618d2e18d71707ae4dcafbe5153d7ff427freed@google.com//////////////////////////////////////////////////////////////////////////////
5290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#define BUFFERSIZE (1 << 13)
53059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
53130ddd615c447fed73286151b463af20d309c85f1reed@google.comclass SkScalerContext_GDI : public SkScalerContext {
532ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.compublic:
53330ddd615c447fed73286151b463af20d309c85f1reed@google.com    SkScalerContext_GDI(SkTypeface*, const SkDescriptor* desc);
53430ddd615c447fed73286151b463af20d309c85f1reed@google.com    virtual ~SkScalerContext_GDI();
535ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
53684e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // Returns true if the constructor was able to complete all of its
53784e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // initializations (which may include calling GDI).
53884e22d847fc84727bc220947162363ee1fe068fcreed@google.com    bool isValid() const;
53984e22d847fc84727bc220947162363ee1fe068fcreed@google.com
540ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comprotected:
54136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    unsigned generateGlyphCount() override;
54236352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    uint16_t generateCharToGlyph(SkUnichar uni) override;
54336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void generateAdvance(SkGlyph* glyph) override;
54436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void generateMetrics(SkGlyph* glyph) override;
54536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void generateImage(const SkGlyph& glyph) override;
54636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void generatePath(const SkGlyph& glyph, SkPath* path) override;
54736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void generateFontMetrics(SkPaint::FontMetrics*) override;
54899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
549ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comprivate:
5500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    DWORD getGDIGlyphPath(const SkGlyph& glyph, UINT flags,
5510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                          SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf);
5520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
55399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDCOffscreen fOffscreen;
5540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** fGsA is the non-rotational part of total matrix without the text height scale.
5550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     *  Used to find the magnitude of advances.
5560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     */
5570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    MAT2         fGsA;
5586a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** The total matrix without the textSize. */
559ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    MAT2         fMat22;
5606a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** Scales font to EM size. */
5616a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    MAT2         fHighResMat22;
562ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC          fDDC;
563ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT        fSavefont;
564ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT        fFont;
565ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SCRIPT_CACHE fSC;
566ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int          fGlyphCount;
5671dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
5686a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** The total matrix which also removes EM scale. */
5691dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    SkMatrix     fHiResMatrix;
5700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** fG_inv is the inverse of the rotational part of the total matrix.
5710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     *  Used to set the direction of advances.
5720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     */
5730abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix     fG_inv;
574a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    enum Type {
5754732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        kTrueType_Type, kBitmap_Type, kLine_Type
576a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    } fType;
577a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    TEXTMETRIC fTM;
578ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com};
579ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
580ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic FIXED float2FIXED(float x) {
581ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return SkFixedToFIXED(SkFloatToFixed(x));
582ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
583ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
58482a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.comstatic BYTE compute_quality(const SkScalerContext::Rec& rec) {
58582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    switch (rec.fMaskFormat) {
58682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        case SkMask::kBW_Format:
58782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            return NONANTIALIASED_QUALITY;
58882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        case SkMask::kLCD16_Format:
58982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            return CLEARTYPE_QUALITY;
59082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        default:
5918351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            if (rec.fFlags & SkScalerContext::kGenA8FromLCD_Flag) {
5928351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com                return CLEARTYPE_QUALITY;
5938351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            } else {
5948351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com                return ANTIALIASED_QUALITY;
5958351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            }
59682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    }
59782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
59882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
59930ddd615c447fed73286151b463af20d309c85f1reed@google.comSkScalerContext_GDI::SkScalerContext_GDI(SkTypeface* rawTypeface,
6000da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com                                                 const SkDescriptor* desc)
601055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        : SkScalerContext(rawTypeface, desc)
602055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fDDC(0)
603055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fSavefont(0)
60484e22d847fc84727bc220947162363ee1fe068fcreed@google.com        , fFont(0)
605055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fSC(0)
60605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        , fGlyphCount(-1)
60705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com{
608055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    LogFontTypeface* typeface = reinterpret_cast<LogFontTypeface*>(rawTypeface);
609055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
610ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fDDC = ::CreateCompatibleDC(NULL);
61184e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!fDDC) {
61284e22d847fc84727bc220947162363ee1fe068fcreed@google.com        return;
61384e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
6141dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    SetGraphicsMode(fDDC, GM_ADVANCED);
615ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SetBkMode(fDDC, TRANSPARENT);
61627e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com
6176f94076da504a9e292c7f6173b039d2692d47c51bungeman    // When GDI hinting, remove the entire Y scale from sA and GsA. (Prevents 'linear' metrics.)
6186f94076da504a9e292c7f6173b039d2692d47c51bungeman    // When not hinting, remove only the integer Y scale from sA and GsA. (Applied by GDI.)
6195f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    SkScalerContextRec::PreMatrixScale scaleConstraints =
6205f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman        (fRec.getHinting() == SkPaint::kNo_Hinting || fRec.getHinting() == SkPaint::kSlight_Hinting)
6215f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman                   ? SkScalerContextRec::kVerticalInteger_PreMatrixScale
6225f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman                   : SkScalerContextRec::kVertical_PreMatrixScale;
6235f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    SkVector scale;
6245f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    SkMatrix sA;
6255f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    SkMatrix GsA;
6265f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    SkMatrix A;
6275f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    fRec.computeMatrices(scaleConstraints, &scale, &sA, &GsA, &fG_inv, &A);
6280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM11 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleX));
6300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM12 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewY)); // This should be ~0.
6310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM21 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewX));
6320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM22 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleY));
6330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6346f94076da504a9e292c7f6173b039d2692d47c51bungeman    // When not hinting, scale was computed with kVerticalInteger, so is already an integer.
6356f94076da504a9e292c7f6173b039d2692d47c51bungeman    // The sA and GsA transforms will be used to create 'linear' metrics.
6366f94076da504a9e292c7f6173b039d2692d47c51bungeman
6376f94076da504a9e292c7f6173b039d2692d47c51bungeman    // When hinting, scale was computed with kVertical, stating that our port can handle
6386f94076da504a9e292c7f6173b039d2692d47c51bungeman    // non-integer scales. This is done so that sA and GsA are computed without any 'residual'
6396f94076da504a9e292c7f6173b039d2692d47c51bungeman    // scale in them, preventing 'linear' metrics. However, GDI cannot actually handle non-integer
6406f94076da504a9e292c7f6173b039d2692d47c51bungeman    // scales so we need to round in this case. This is fine, since all of the scale has been
6416f94076da504a9e292c7f6173b039d2692d47c51bungeman    // removed from sA and GsA, so GDI will be handling the scale completely.
6426f94076da504a9e292c7f6173b039d2692d47c51bungeman    SkScalar gdiTextSize = SkScalarRoundToScalar(scale.fY);
6436f94076da504a9e292c7f6173b039d2692d47c51bungeman
6446f94076da504a9e292c7f6173b039d2692d47c51bungeman    // GDI will not accept a size of zero, so round the range [0, 1] to 1.
6456f94076da504a9e292c7f6173b039d2692d47c51bungeman    // If the size was non-zero, the scale factors will also be non-zero and 1px tall text is drawn.
6466f94076da504a9e292c7f6173b039d2692d47c51bungeman    // If the size actually was zero, the scale factors will also be zero, so GDI will draw nothing.
6475f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    if (gdiTextSize == 0) {
6485f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman        gdiTextSize = SK_Scalar1;
6495f14c5e038a16c80d50c2f87ae7c8775f977456cbungeman    }
650ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
651055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    LOGFONT lf = typeface->fLogFont;
65211ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    lf.lfHeight = -SkScalarTruncToInt(gdiTextSize);
65382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    lf.lfQuality = compute_quality(fRec);
654ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fFont = CreateFontIndirect(&lf);
65584e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!fFont) {
65684e22d847fc84727bc220947162363ee1fe068fcreed@google.com        return;
65784e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
6581dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
659ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fSavefont = (HFONT)SelectObject(fDDC, fFont);
660a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
661a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    if (0 == GetTextMetrics(fDDC, &fTM)) {
662055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(lf);
663a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (0 == GetTextMetrics(fDDC, &fTM)) {
664a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            fTM.tmPitchAndFamily = TMPF_TRUETYPE;
665a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
666a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
66790b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com
668d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    XFORM xform;
66990b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    if (fTM.tmPitchAndFamily & TMPF_VECTOR) {
6704732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // Used a logfont on a memory context, should never get a device font.
6714732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // Therefore all TMPF_DEVICE will be PostScript fonts.
6724732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com
6734732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // If TMPF_VECTOR is set, one of TMPF_TRUETYPE or TMPF_DEVICE means that
6744732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // we have an outline font. Otherwise we have a vector FON, which is
6754732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // scalable, but not an outline font.
6764732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // This was determined by testing with Type1 PFM/PFB and
6774732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // OpenTypeCFF OTF, as well as looking at Wine bugs and sources.
6784732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        if (fTM.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_DEVICE)) {
6794732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            // Truetype or PostScript.
6804732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            fType = SkScalerContext_GDI::kTrueType_Type;
6814732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        } else {
6824732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            // Stroked FON.
6834732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            fType = SkScalerContext_GDI::kLine_Type;
6844732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        }
685a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
686d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // fPost2x2 is column-major, left handed (y down).
687d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // XFORM 2x2 is row-major, left handed (y down).
6880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM11 = SkScalarToFloat(sA.get(SkMatrix::kMScaleX));
6890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM12 = SkScalarToFloat(sA.get(SkMatrix::kMSkewY));
6900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM21 = SkScalarToFloat(sA.get(SkMatrix::kMSkewX));
6910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM22 = SkScalarToFloat(sA.get(SkMatrix::kMScaleY));
692d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDx = 0;
693d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDy = 0;
694d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
695d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // MAT2 is row major, right handed (y up).
696d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM11 = float2FIXED(xform.eM11);
697d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM12 = float2FIXED(-xform.eM12);
698d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM21 = float2FIXED(-xform.eM21);
699d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM22 = float2FIXED(xform.eM22);
700a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
701a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (needToRenderWithSkia(fRec)) {
702a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            this->forceGenerateImageFromPath();
703a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
704a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
70511ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com        // Create a hires matrix if we need linear metrics.
7060abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (this->isSubpixel()) {
7070abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            OUTLINETEXTMETRIC otm;
7080abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            UINT success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
7090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (0 == success) {
7100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                call_ensure_accessible(lf);
7110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
7120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
7130abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (0 != success) {
71411ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar upem = SkIntToScalar(otm.otmEMSquare);
7156a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com
71611ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar gdiTextSizeToEMScale = upem / gdiTextSize;
71711ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHighResMat22.eM11 = float2FIXED(gdiTextSizeToEMScale);
7186a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHighResMat22.eM12 = float2FIXED(0);
7196a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHighResMat22.eM21 = float2FIXED(0);
72011ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHighResMat22.eM22 = float2FIXED(gdiTextSizeToEMScale);
7216a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com
72211ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar removeEMScale = SkScalarInvert(upem);
7236a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHiResMatrix = A;
72411ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHiResMatrix.preScale(removeEMScale, removeEMScale);
7250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
7260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
7270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
728a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    } else {
729a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        // Assume bitmap
73030ddd615c447fed73286151b463af20d309c85f1reed@google.com        fType = SkScalerContext_GDI::kBitmap_Type;
731a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
732d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM11 = 1.0f;
733d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM12 = 0.0f;
734d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM21 = 0.0f;
735d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM22 = 1.0f;
736d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDx = 0.0f;
737d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDy = 0.0f;
738a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
739d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // fPost2x2 is column-major, left handed (y down).
740d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // MAT2 is row major, right handed (y up).
741a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        fMat22.eM11 = SkScalarToFIXED(fRec.fPost2x2[0][0]);
742d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM12 = SkScalarToFIXED(-fRec.fPost2x2[1][0]);
743a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        fMat22.eM21 = SkScalarToFIXED(-fRec.fPost2x2[0][1]);
744d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM22 = SkScalarToFIXED(fRec.fPost2x2[1][1]);
745a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    }
74699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
747d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    fOffscreen.init(fFont, xform);
748ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
749ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
75030ddd615c447fed73286151b463af20d309c85f1reed@google.comSkScalerContext_GDI::~SkScalerContext_GDI() {
751ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fDDC) {
752ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::SelectObject(fDDC, fSavefont);
753ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::DeleteDC(fDDC);
754ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
755ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fFont) {
756ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::DeleteObject(fFont);
757ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
758ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fSC) {
759ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::ScriptFreeCache(&fSC);
760ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
761ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
762ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
76330ddd615c447fed73286151b463af20d309c85f1reed@google.combool SkScalerContext_GDI::isValid() const {
76484e22d847fc84727bc220947162363ee1fe068fcreed@google.com    return fDDC && fFont;
76584e22d847fc84727bc220947162363ee1fe068fcreed@google.com}
76684e22d847fc84727bc220947162363ee1fe068fcreed@google.com
76730ddd615c447fed73286151b463af20d309c85f1reed@google.comunsigned SkScalerContext_GDI::generateGlyphCount() {
768ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fGlyphCount < 0) {
7697bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        fGlyphCount = calculateGlyphCount(
7707bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com                          fDDC, static_cast<const LogFontTypeface*>(this->getTypeface())->fLogFont);
771ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
772ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return fGlyphCount;
773ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
774ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
77533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.comuint16_t SkScalerContext_GDI::generateCharToGlyph(SkUnichar utf32) {
776ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint16_t index = 0;
77733346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com    WCHAR utf16[2];
778ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // TODO(ctguil): Support characters that generate more than one glyph.
77933346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com    if (SkUTF16_FromUnichar(utf32, (uint16_t*)utf16) == 1) {
780ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        // Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0.
7817bd141dce43ea3405bc60c9c84e6f910b851b079skia.committer@gmail.com
78233346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        /** Real documentation for GetGlyphIndiciesW:
78333346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *
78433346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  When GGI_MARK_NONEXISTING_GLYPHS is not specified and a character does not map to a
78533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  glyph, then the 'default character's glyph is returned instead. The 'default character'
7864732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com         *  is available in fTM.tmDefaultChar. FON fonts have a default character, and there exists
7874732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com         *  a usDefaultChar in the 'OS/2' table, version 2 and later. If there is no
78833346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  'default character' specified by the font, then often the first character found is used.
78933346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *
79033346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  When GGI_MARK_NONEXISTING_GLYPHS is specified and a character does not map to a glyph,
79133346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  then the glyph 0xFFFF is used. In Windows XP and earlier, Bitmap/Vector FON usually use
79233346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  glyph 0x1F instead ('Terminal' appears to be special, returning 0xFFFF).
79333346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  Type1 PFM/PFB, TT, OT TT, OT CFF all appear to use 0xFFFF, even on XP.
79433346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         */
79533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        DWORD result = GetGlyphIndicesW(fDDC, utf16, 1, &index, GGI_MARK_NONEXISTING_GLYPHS);
79633346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        if (result == GDI_ERROR
79733346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com            || 0xFFFF == index
7984732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            || (0x1F == index &&
7994732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com               (fType == SkScalerContext_GDI::kBitmap_Type ||
8004732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com                fType == SkScalerContext_GDI::kLine_Type)
8014732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com               /*&& winVer < Vista */)
8024732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com           )
80333346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        {
80433346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com            index = 0;
80533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        }
806ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
807ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        // Use uniscribe to detemine glyph index for non-BMP characters.
80827f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int numWCHAR = 2;
80927f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int maxItems = 2;
81027f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // MSDN states that this can be NULL, but some things don't work then.
81127f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_CONTROL sc = { 0 };
81227f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // Add extra item to SCRIPT_ITEM to work around a bug (now documented).
81327f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // https://bugzilla.mozilla.org/show_bug.cgi?id=366643
81427f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_ITEM si[maxItems + 1];
81527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        int numItems;
81627f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &sc, NULL, si, &numItems),
81727f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com             "Could not itemize character.");
81827f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com
81927f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs.
82027f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int maxGlyphs = 2;
82127f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_VISATTR vsa[maxGlyphs];
82227f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        WORD outGlyphs[maxGlyphs];
82327f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        WORD logClust[numWCHAR];
82427f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        int numGlyphs;
82527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        HRZM(ScriptShape(fDDC, &fSC, utf16, numWCHAR, maxGlyphs, &si[0].a,
82627f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com                         outGlyphs, logClust, vsa, &numGlyphs),
82727f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com             "Could not shape character.");
82827f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        if (1 == numGlyphs) {
82927f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com            index = outGlyphs[0];
83027f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        }
831ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
832ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return index;
833ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
834ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
83530ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateAdvance(SkGlyph* glyph) {
836ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    this->generateMetrics(glyph);
837ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
838ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
83930ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateMetrics(SkGlyph* glyph) {
840ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
841ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
8424732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com    if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) {
843a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        SIZE size;
8441b27704eba15be4e9d1997faac42038493a30be5djsollen        WORD glyphs = glyph->getGlyphID();
845a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (0 == GetTextExtentPointI(fDDC, &glyphs, 1, &size)) {
846a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            glyph->fWidth = SkToS16(fTM.tmMaxCharWidth);
847a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        } else {
848a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            glyph->fWidth = SkToS16(size.cx);
849a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
850a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fHeight = SkToS16(size.cy);
851a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
852a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fTop = SkToS16(-fTM.tmAscent);
8534732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // Bitmap FON cannot underhang, but vector FON may.
8544732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // There appears no means of determining underhang of vector FON.
855a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fLeft = SkToS16(0);
856a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceX = SkIntToFixed(glyph->fWidth);
857a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceY = 0;
858a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
8594732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        // Vector FON will transform nicely, but bitmap FON do not.
8604732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        if (fType == SkScalerContext_GDI::kLine_Type) {
8614732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            SkRect bounds = SkRect::MakeXYWH(glyph->fLeft, glyph->fTop,
8624732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com                                             glyph->fWidth, glyph->fHeight);
8634732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            SkMatrix m;
8644732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            m.setAll(SkFIXEDToScalar(fMat22.eM11), -SkFIXEDToScalar(fMat22.eM21), 0,
8654732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com                     -SkFIXEDToScalar(fMat22.eM12), SkFIXEDToScalar(fMat22.eM22), 0,
8663f43f8ab9cd3ba0ea83f5ae0659c21f101dfca7areed                     0,  0, 1);
8674732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            m.mapRect(&bounds);
868d02cf2664d0e453a6ec57cddc7f09bfc07fd91dbreed            bounds.roundOut(&bounds);
8694732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            glyph->fLeft = SkScalarTruncToInt(bounds.fLeft);
8704732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            glyph->fTop = SkScalarTruncToInt(bounds.fTop);
8714732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            glyph->fWidth = SkScalarTruncToInt(bounds.width());
8724732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com            glyph->fHeight = SkScalarTruncToInt(bounds.height());
8734732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        }
8744732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com
875d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // Apply matrix to advance.
8764732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com        glyph->fAdvanceY = SkFixedMul(-SkFIXEDToFixed(fMat22.eM12), glyph->fAdvanceX);
877a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceX = SkFixedMul(SkFIXEDToFixed(fMat22.eM11), glyph->fAdvanceX);
878a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
879a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
880a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
881a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
8821b27704eba15be4e9d1997faac42038493a30be5djsollen    UINT glyphId = glyph->getGlyphID();
883d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
884ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
8851dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    sk_bzero(&gm, sizeof(gm));
886ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
887d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    DWORD status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
888d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (GDI_ERROR == status) {
889055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
890d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
891d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        if (GDI_ERROR == status) {
892d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->zeroMetrics();
893d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            return;
894d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
89539698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
896ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
897d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    bool empty = false;
898d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // The black box is either the embedded bitmap size or the outline extent.
899d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // It is 1x1 if nothing is to be drawn, but will also be 1x1 if something very small
900d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // is to be drawn, like a '.'. We need to outset '.' but do not wish to outset ' '.
901d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (1 == gm.gmBlackBoxX && 1 == gm.gmBlackBoxY) {
902d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // If GetGlyphOutline with GGO_NATIVE returns 0, we know there was no outline.
903d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        DWORD bufferSize = GetGlyphOutlineW(fDDC, glyphId, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
904d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        empty = (0 == bufferSize);
905d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    }
9061dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
907d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fTop = SkToS16(-gm.gmptGlyphOrigin.y);
908d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fLeft = SkToS16(gm.gmptGlyphOrigin.x);
909d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (empty) {
910d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fWidth = 0;
911d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fHeight = 0;
912ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
913d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // Outset, since the image may bleed out of the black box.
914d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For embedded bitmaps the black box should be exact.
915d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For outlines we need to outset by 1 in all directions for bleed.
916d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For ClearType we need to outset by 2 for bleed.
917d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fWidth = gm.gmBlackBoxX + 4;
918d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fHeight = gm.gmBlackBoxY + 4;
919d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fTop -= 2;
920d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fLeft -= 2;
921d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    }
922d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fAdvanceX = SkIntToFixed(gm.gmCellIncX);
923d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fAdvanceY = SkIntToFixed(gm.gmCellIncY);
924d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fRsbDelta = 0;
925d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fLsbDelta = 0;
926d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
9276a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    if (this->isSubpixel()) {
928d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        sk_bzero(&gm, sizeof(gm));
9296a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fHighResMat22);
930d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        if (GDI_ERROR != status) {
931d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            SkPoint advance;
932d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            fHiResMatrix.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance);
933d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->fAdvanceX = SkScalarToFixed(advance.fX);
934d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->fAdvanceY = SkScalarToFixed(advance.fY);
935d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
9360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else if (!isAxisAligned(this->fRec)) {
9370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fGsA);
9380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR != status) {
9390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            SkPoint advance;
9400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fG_inv.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance);
9410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            glyph->fAdvanceX = SkScalarToFixed(advance.fX);
9420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            glyph->fAdvanceY = SkScalarToFixed(advance.fY);
9430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
944ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
945ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
946ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
9476a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.comstatic const MAT2 gMat2Identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
948410780677af260e32948b02c0725ef6ad761260cbungemanvoid SkScalerContext_GDI::generateFontMetrics(SkPaint::FontMetrics* metrics) {
949410780677af260e32948b02c0725ef6ad761260cbungeman    if (NULL == metrics) {
950410780677af260e32948b02c0725ef6ad761260cbungeman        return;
95160af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    }
952410780677af260e32948b02c0725ef6ad761260cbungeman    sk_bzero(metrics, sizeof(*metrics));
953ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
954ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
955ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
956e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
9574732df69415fbece99b3d4c033dc3bebe73d0117bungeman@google.com    if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) {
958e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#endif
959410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fTop = SkIntToScalar(-fTM.tmAscent);
960410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fAscent = SkIntToScalar(-fTM.tmAscent);
961410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fDescent = SkIntToScalar(fTM.tmDescent);
962410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fBottom = SkIntToScalar(fTM.tmDescent);
963410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fLeading = SkIntToScalar(fTM.tmExternalLeading);
964410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fAvgCharWidth = SkIntToScalar(fTM.tmAveCharWidth);
965410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fMaxCharWidth = SkIntToScalar(fTM.tmMaxCharWidth);
966410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fXMin = 0;
967410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fXMax = metrics->fMaxCharWidth;
968410780677af260e32948b02c0725ef6ad761260cbungeman        //metrics->fXHeight = 0;
969e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
970a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
971a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
972e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#endif
973a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
974ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    OUTLINETEXTMETRIC otm;
975ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
976ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
9770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == ret) {
978055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
97939698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
98039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
9810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == ret) {
982a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
983ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
984ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
985e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
986410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fTop = SkIntToScalar(-otm.otmrcFontBox.top);
987410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fAscent = SkIntToScalar(-otm.otmAscent);
988410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fDescent = SkIntToScalar(-otm.otmDescent);
989410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fBottom = SkIntToScalar(-otm.otmrcFontBox.bottom);
990410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fLeading = SkIntToScalar(otm.otmLineGap);
991410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fAvgCharWidth = SkIntToScalar(otm.otmTextMetrics.tmAveCharWidth);
992410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fMaxCharWidth = SkIntToScalar(otm.otmTextMetrics.tmMaxCharWidth);
993410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fXMin = SkIntToScalar(otm.otmrcFontBox.left);
994410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fXMax = SkIntToScalar(otm.otmrcFontBox.right);
995d3031aa5ae90b796593a04c0da062024198e4769commit-bot@chromium.org#endif
996410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fUnderlineThickness = SkIntToScalar(otm.otmsUnderscoreSize);
997410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fUnderlinePosition = -SkIntToScalar(otm.otmsUnderscorePosition);
9984a867a6356e05a09186a16c1c8ffc3f2cafbbe59bungeman@google.com
999410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1000410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
10010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1002410780677af260e32948b02c0725ef6ad761260cbungeman    metrics->fXHeight = SkIntToScalar(otm.otmsXHeight);
1003410780677af260e32948b02c0725ef6ad761260cbungeman    GLYPHMETRICS gm;
1004410780677af260e32948b02c0725ef6ad761260cbungeman    sk_bzero(&gm, sizeof(gm));
1005410780677af260e32948b02c0725ef6ad761260cbungeman    DWORD len = GetGlyphOutlineW(fDDC, 'x', GGO_METRICS, &gm, 0, 0, &gMat2Identity);
1006410780677af260e32948b02c0725ef6ad761260cbungeman    if (len != GDI_ERROR && gm.gmBlackBoxY > 0) {
1007410780677af260e32948b02c0725ef6ad761260cbungeman        metrics->fXHeight = SkIntToScalar(gm.gmBlackBoxY);
1008ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1009ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1010ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
10117430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com////////////////////////////////////////////////////////////////////////////////////////
10127430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
10130abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#define SK_SHOW_TEXT_BLIT_COVERAGE 0
10140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
10157430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.comstatic void build_power_table(uint8_t table[], float ee) {
10167430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    for (int i = 0; i < 256; i++) {
10177430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        float x = i / 255.f;
101897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        x = sk_float_pow(x, ee);
1019e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com        int xx = SkScalarRoundToInt(x * 255);
10207430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        table[i] = SkToU8(xx);
10217430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    }
10227430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com}
10237430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
102497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com/**
102597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  This will invert the gamma applied by GDI (gray-scale antialiased), so we
102697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  can get linear values.
102797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
102897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI grayscale appears to use a hard-coded gamma of 2.3.
102997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
103097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI grayscale appears to draw using the black and white rasterizer at four
103197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  times the size and then downsamples to compute the coverage mask. As a
103297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  result there are only seventeen total grays. This lack of fidelity means
103397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  that shifting into other color spaces is imprecise.
103497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com */
103597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic const uint8_t* getInverseGammaTableGDI() {
103605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Since build_power_table is idempotent, many threads can build gTableGdi
103705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // simultaneously.
103805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
103905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Microsoft Specific:
104005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Making gInited volatile provides read-aquire and write-release in vc++.
104105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // In VS2012, see compiler option /volatile:(ms|iso).
104205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Replace with C++11 atomics when possible.
104305a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    static volatile bool gInited;
104497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    static uint8_t gTableGdi[256];
104505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    if (gInited) {
104605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a L/L (read) barrier (full acquire not needed). If gInited is observed
104705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // true then gTableGdi is observable, but it must be requested.
104805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    } else {
104997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        build_power_table(gTableGdi, 2.3f);
105005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a S/S (write) barrier (full release not needed) here so that this
105105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // write to gInited becomes observable after gTableGdi.
105297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        gInited = true;
105397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    }
105497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return gTableGdi;
105597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com}
105697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com
105797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com/**
105897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  This will invert the gamma applied by GDI ClearType, so we can get linear
105997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  values.
106097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
106197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI ClearType uses SPI_GETFONTSMOOTHINGCONTRAST / 1000 as the gamma value.
106297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  If this value is not specified, the default is a gamma of 1.4.
106397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com */
106497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic const uint8_t* getInverseGammaTableClearType() {
106505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // We don't expect SPI_GETFONTSMOOTHINGCONTRAST to ever change, so building
106605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // gTableClearType with build_power_table is effectively idempotent.
106705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
106805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Microsoft Specific:
106905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Making gInited volatile provides read-aquire and write-release in vc++.
107005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // In VS2012, see compiler option /volatile:(ms|iso).
107105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Replace with C++11 atomics when possible.
107205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    static volatile bool gInited;
107397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    static uint8_t gTableClearType[256];
107405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    if (gInited) {
107505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a L/L (read) barrier (acquire not needed). If gInited is observed
107605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // true then gTableClearType is observable, but it must be requested.
107705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    } else {
10787430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        UINT level = 0;
10797430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &level, 0) || !level) {
10807430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com            // can't get the data, so use a default
10817430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com            level = 1400;
10827430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        }
108397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        build_power_table(gTableClearType, level / 1000.0f);
108405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a S/S (write) barrier (release not needed) here so that this
108505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // write to gInited becomes observable after gTableClearType.
10867430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        gInited = true;
10877430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    }
108897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return gTableClearType;
10897430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com}
10907430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
1091ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkColorPriv.h"
1092ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
10936385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com//Cannot assume that the input rgb is gray due to possible setting of kGenA8FromLCD_Flag.
109497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
109597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic inline uint8_t rgb_to_a8(SkGdiRGB rgb, const uint8_t* table8) {
10966385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU r = (rgb >> 16) & 0xFF;
10976385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU g = (rgb >>  8) & 0xFF;
10986385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU b = (rgb >>  0) & 0xFF;
10991bfe01d06b896eca94c80d9af9a6bb30216b5e42bungeman@google.com    return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8);
110082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
110182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
110297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
110397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic inline uint16_t rgb_to_lcd16(SkGdiRGB rgb, const uint8_t* tableR,
110497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                  const uint8_t* tableG,
110597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                  const uint8_t* tableB) {
110697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR);
110797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  8) & 0xFF, tableG);
110897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  0) & 0xFF, tableB);
11090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
11100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
11110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
111297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return SkPack888ToRGB16(r, g, b);
1113ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1114ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
111582cff020b7e055f2d01686f060a884df842306edreed@google.com// Is this GDI color neither black nor white? If so, we have to keep this
111682cff020b7e055f2d01686f060a884df842306edreed@google.com// image as is, rather than smashing it down to a BW mask.
111782cff020b7e055f2d01686f060a884df842306edreed@google.com//
111882cff020b7e055f2d01686f060a884df842306edreed@google.com// returns int instead of bool, since we don't want/have to pay to convert
111982cff020b7e055f2d01686f060a884df842306edreed@google.com// the zero/non-zero value into a bool
112082cff020b7e055f2d01686f060a884df842306edreed@google.comstatic int is_not_black_or_white(SkGdiRGB c) {
112182cff020b7e055f2d01686f060a884df842306edreed@google.com    // same as (but faster than)
112282cff020b7e055f2d01686f060a884df842306edreed@google.com    //      c &= 0x00FFFFFF;
112382cff020b7e055f2d01686f060a884df842306edreed@google.com    //      return 0 == c || 0x00FFFFFF == c;
112482cff020b7e055f2d01686f060a884df842306edreed@google.com    return (c + (c & 1)) & 0x00FFFFFF;
11255e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
11265e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11274b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.comstatic bool is_rgb_really_bw(const SkGdiRGB* src, int width, int height, size_t srcRB) {
11285e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < height; ++y) {
11295e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int x = 0; x < width; ++x) {
113082cff020b7e055f2d01686f060a884df842306edreed@google.com            if (is_not_black_or_white(src[x])) {
11315e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                return false;
11325e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
11335e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
113405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
11355e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
11365e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    return true;
11375e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
11385e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
113997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com// gdi's bitmap is upside-down, so we reverse dst walking in Y
114097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com// whenever we copy it into skia's buffer
11415e2df64215f12660ab26b96816f40ad8e32bf16freed@google.comstatic void rgb_to_bw(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
114297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                      const SkGlyph& glyph) {
11435e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
11445e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = (width + 7) >> 3;
11455e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
11465e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11475e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    int byteCount = width >> 3;
11485e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    int bitCount = width & 7;
11495e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11505e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    // adjust srcRB to skip the values in our byteCount loop,
11515e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    // since we increment src locally there
11525e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    srcRB -= byteCount * 8 * sizeof(SkGdiRGB);
11535e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11545e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; ++y) {
11555e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (byteCount > 0) {
11565e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            for (int i = 0; i < byteCount; ++i) {
11577a230142e01c5a8cf955be12a5f287589b551634reed@google.com                unsigned byte = 0;
115897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[0] & (1 << 7);
115997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[1] & (1 << 6);
116097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[2] & (1 << 5);
116197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[3] & (1 << 4);
116297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[4] & (1 << 3);
116397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[5] & (1 << 2);
116497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[6] & (1 << 1);
116597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[7] & (1 << 0);
11666a8f14d9633b330fb8051581828a0aaf4403d1a9reed@google.com                dst[i] = byte;
11675e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                src += 8;
11685e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
11695e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
11705e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (bitCount > 0) {
11715e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            unsigned byte = 0;
11725e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            unsigned mask = 0x80;
11735e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            for (int i = 0; i < bitCount; i++) {
117497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[i] & mask;
11755e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                mask >>= 1;
11765e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
11775e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            dst[byteCount] = byte;
11785e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
117905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
11805e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst -= dstRB;
11815e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
11820abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
11830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (glyph.fWidth > 0 && glyph.fHeight > 0) {
11840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        uint8_t* first = (uint8_t*)glyph.fImage;
11850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1);
11860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        *first |= 1 << 7;
11870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount);
11880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
11890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
11905e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
11915e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
119297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
11935e2df64215f12660ab26b96816f40ad8e32bf16freed@google.comstatic void rgb_to_a8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
119497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                      const SkGlyph& glyph, const uint8_t* table8) {
11955e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = glyph.rowBytes();
11965e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
11975e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
11985e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11995e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; y++) {
12005e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int i = 0; i < width; i++) {
120197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            dst[i] = rgb_to_a8<APPLY_PREBLEND>(src[i], table8);
12020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
12030abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            dst[i] = SkMax32(dst[i], 10);
12040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
12055e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
120605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
12075e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst -= dstRB;
12085e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
12095e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12105e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
121197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
121297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic void rgb_to_lcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph,
121397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                         const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
12145e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = glyph.rowBytes();
12155e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
12165e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
12175e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12185e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; y++) {
12195e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int i = 0; i < width; i++) {
122097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(src[i], tableR, tableG, tableB);
12215e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
122205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
12235e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst = (uint16_t*)((char*)dst - dstRB);
12245e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
12255e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12265e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12276f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.comstatic inline unsigned clamp255(unsigned x) {
12286f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    SkASSERT(x <= 256);
12296f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    return x - (x >> 8);
12306f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com}
12316f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
123230ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateImage(const SkGlyph& glyph) {
1233ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
1234ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
12356271117d826098c1399cf7442f8eea2d665cb78areed@google.com    const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat;
123682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    const bool isAA = !isLCD(fRec);
123782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
123899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t srcRB;
123997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    const void* bits = fOffscreen.draw(glyph, isBW, &srcRB);
124039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (NULL == bits) {
1241055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
124297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        bits = fOffscreen.draw(glyph, isBW, &srcRB);
124339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        if (NULL == bits) {
124439698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            sk_bzero(glyph.fImage, glyph.computeImageSize());
124539698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            return;
124639698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        }
124782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    }
124882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
124997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    if (!isBW) {
12501bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        const uint8_t* table;
12511bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        //The offscreen contains a GDI blit if isAA and kGenA8FromLCD_Flag is not set.
12521bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        //Otherwise the offscreen contains a ClearType blit.
12531bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        if (isAA && !(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag)) {
12541bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com            table = getInverseGammaTableGDI();
12551bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        } else {
12561bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com            table = getInverseGammaTableClearType();
125797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        }
125897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //Note that the following cannot really be integrated into the
125997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //pre-blend, since we may not be applying the pre-blend; when we aren't
126097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //applying the pre-blend it means that a filter wants linear anyway.
126197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //Other code may also be applying the pre-blend, so we'd need another
126297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //one with this and one without.
12636f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        SkGdiRGB* addr = (SkGdiRGB*)bits;
12646f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        for (int y = 0; y < glyph.fHeight; ++y) {
12656f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com            for (int x = 0; x < glyph.fWidth; ++x) {
12666f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int r = (addr[x] >> 16) & 0xFF;
12676f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int g = (addr[x] >>  8) & 0xFF;
12686f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int b = (addr[x] >>  0) & 0xFF;
12697430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com                addr[x] = (table[r] << 16) | (table[g] << 8) | table[b];
12706f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com            }
127105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            addr = SkTAddOffset<SkGdiRGB>(addr, srcRB);
12726f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        }
12736f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    }
12746f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
127582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    int width = glyph.fWidth;
127682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    size_t dstRB = glyph.rowBytes();
127782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    if (isBW) {
127882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        const uint8_t* src = (const uint8_t*)bits;
127982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        uint8_t* dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
128082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        for (int y = 0; y < glyph.fHeight; y++) {
128182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            memcpy(dst, src, dstRB);
128282a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            src += srcRB;
128382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            dst -= dstRB;
1284ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
12850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
12860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (glyph.fWidth > 0 && glyph.fHeight > 0) {
12870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                int bitCount = width & 7;
12880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                uint8_t* first = (uint8_t*)glyph.fImage;
12890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1);
12900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                *first |= 1 << 7;
12910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount);
12920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
12930abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
129482a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    } else if (isAA) {
12956f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        // since the caller may require A8 for maskfilters, we can't check for BW
12966f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        // ... until we have the caller tell us that explicitly
12975e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        const SkGdiRGB* src = (const SkGdiRGB*)bits;
1298a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com        if (fPreBlend.isApplicable()) {
1299a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com            rgb_to_a8<true>(src, srcRB, glyph, fPreBlend.fG);
130097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        } else {
1301a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com            rgb_to_a8<false>(src, srcRB, glyph, fPreBlend.fG);
130297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        }
130382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    } else {    // LCD16
13045e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        const SkGdiRGB* src = (const SkGdiRGB*)bits;
13055e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) {
130697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            rgb_to_bw(src, srcRB, glyph);
13075e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            ((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format;
13085e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        } else {
1309d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed            SkASSERT(SkMask::kLCD16_Format == glyph.fMaskFormat);
1310d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed            if (fPreBlend.isApplicable()) {
1311d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed                rgb_to_lcd16<true>(src, srcRB, glyph,
1312d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed                                   fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1313754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com            } else {
1314d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed                rgb_to_lcd16<false>(src, srcRB, glyph,
1315d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed                                    fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
1316754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com            }
1317ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1318ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1319ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1320ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
13210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comclass GDIGlyphbufferPointIter {
13220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.compublic:
132327e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com    GDIGlyphbufferPointIter(const uint8_t* glyphbuf, DWORD total_size)
13240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        : fHeaderIter(glyphbuf, total_size), fCurveIter(), fPointIter()
13250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    { }
13260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1327dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    POINTFX const * next() {
13280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comnextHeader:
13290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (!fCurveIter.isSet()) {
13300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYGONHEADER* header = fHeaderIter.next();
1331dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            if (NULL == header) {
1332dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                return NULL;
1333dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            }
13340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurveIter.set(header);
13350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* curve = fCurveIter.next();
1336dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            if (NULL == curve) {
1337dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                return NULL;
1338dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            }
13390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fPointIter.set(curve);
1340dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            return &header->pfxStart;
13410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
1342ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
13430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* nextPoint = fPointIter.next();
13440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (NULL == nextPoint) {
13450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* curve = fCurveIter.next();
13460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (NULL == curve) {
13470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                fCurveIter.set();
13480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                goto nextHeader;
13490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            } else {
13500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                fPointIter.set(curve);
13510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
13520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            nextPoint = fPointIter.next();
13530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
1354dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        return nextPoint;
13550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
1356ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
13570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    WORD currentCurveType() {
13580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        return fPointIter.fCurveType;
13590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
136005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
13610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comprivate:
13620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon headers in a glyphbuf. */
13630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonHeaderIter {
13640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
136527e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com        GDIPolygonHeaderIter(const uint8_t* glyphbuf, DWORD total_size)
13660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurPolygon(reinterpret_cast<const TTPOLYGONHEADER*>(glyphbuf))
13670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndPolygon(SkTAddOffset<const TTPOLYGONHEADER>(glyphbuf, total_size))
13680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
13690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
13700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* next() {
13710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurPolygon >= fEndPolygon) {
13720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
13730abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
13740abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYGONHEADER* thisPolygon = fCurPolygon;
13750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPolygon = SkTAddOffset<const TTPOLYGONHEADER>(fCurPolygon, fCurPolygon->cb);
13760abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisPolygon;
13770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
13780abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
13790abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* fCurPolygon;
13800abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* fEndPolygon;
13810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
138205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
13830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon curves in a polygon header. */
13840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonCurveIter {
13850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
13860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurveIter() : fCurCurve(NULL), fEndCurve(NULL) { }
13870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
13880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurveIter(const TTPOLYGONHEADER* curPolygon)
13890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER)))
13900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb))
13910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
13920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
13930abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        bool isSet() { return fCurCurve != NULL; }
13940abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
13950abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set(const TTPOLYGONHEADER* curPolygon) {
13960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER));
13970abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb);
13980abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
13990abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set() {
14000abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = NULL;
14010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndCurve = NULL;
14020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14030abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* next() {
14050abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurCurve >= fEndCurve) {
14060abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
140705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            }
14080abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* thisCurve = fCurCurve;
14090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = SkTAddOffset<const TTPOLYCURVE>(fCurCurve, size_of_TTPOLYCURVE(*fCurCurve));
14100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisCurve;
14110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
14130abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        size_t size_of_TTPOLYCURVE(const TTPOLYCURVE& curve) {
14140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return 2*sizeof(WORD) + curve.cpfx*sizeof(POINTFX);
141505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        }
14160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* fCurCurve;
14170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* fEndCurve;
14180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
141905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
14200abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon points in a polygon curve. */
14210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonCurvePointIter {
14220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
14230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurvePointIter() : fCurveType(0), fCurPoint(NULL), fEndPoint(NULL) { }
142405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
14250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurvePointIter(const TTPOLYCURVE* curPolygon)
14260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurveType(curPolygon->wType)
14270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fCurPoint(&curPolygon->apfx[0])
14280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndPoint(&curPolygon->apfx[curPolygon->cpfx])
14290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
14300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        bool isSet() { return fCurPoint != NULL; }
14320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set(const TTPOLYCURVE* curPolygon) {
14340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurveType = curPolygon->wType;
14350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPoint = &curPolygon->apfx[0];
14360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndPoint = &curPolygon->apfx[curPolygon->cpfx];
14370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set() {
14390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPoint = NULL;
14400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndPoint = NULL;
14410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* next() {
14440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurPoint >= fEndPoint) {
14450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
144605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            }
14470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const POINTFX* thisPoint = fCurPoint;
14480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            ++fCurPoint;
14490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisPoint;
1450d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
1451ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
14520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        WORD fCurveType;
14530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
14540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* fCurPoint;
14550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* fEndPoint;
14560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
14570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonHeaderIter fHeaderIter;
14590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonCurveIter fCurveIter;
14600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonCurvePointIter fPointIter;
14610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com};
14620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comstatic void sk_path_from_gdi_path(SkPath* path, const uint8_t* glyphbuf, DWORD total_size) {
1464d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    const uint8_t* cur_glyph = glyphbuf;
1465d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    const uint8_t* end_glyph = glyphbuf + total_size;
1466ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1467d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    while (cur_glyph < end_glyph) {
1468d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph;
1469ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1470d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const uint8_t* end_poly = cur_glyph + th->cb;
1471d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER);
1472ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1473d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)),
1474d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                     SkFixedToScalar(-SkFIXEDToFixed(th->pfxStart.y)));
1475ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1476d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        while (cur_poly < end_poly) {
1477d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly;
1478ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1479d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            if (pc->wType == TT_PRIM_LINE) {
1480d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                for (uint16_t i = 0; i < pc->cpfx; i++) {
1481d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)),
1482d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pc->apfx[i].y)));
1483ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                }
1484d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            }
1485ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1486d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            if (pc->wType == TT_PRIM_QSPLINE) {
1487d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline
1488d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    POINTFX pnt_b = pc->apfx[u];    // B is always the current point
1489d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    POINTFX pnt_c = pc->apfx[u+1];
1490ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1491d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    if (u < pc->cpfx - 2) {          // If not on last spline, compute C
1492d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                        pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x),
1493d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                                            SkFIXEDToFixed(pnt_c.x)));
1494d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                        pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y),
1495d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                                            SkFIXEDToFixed(pnt_c.y)));
1496ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                    }
1497d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
1498d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)),
1499d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)),
1500d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)),
1501d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y)));
1502ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                }
1503ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            }
1504d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            // Advance past this TTPOLYCURVE.
1505d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx;
1506ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1507d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        cur_glyph += th->cb;
1508d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        path->close();
1509ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1510ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1511ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1512dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com#define move_next_expected_hinted_point(iter, pElem) do {\
1513dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    pElem = iter.next(); \
1514dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    if (NULL == pElem) return false; \
1515dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com} while(0)
1516dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com
1517dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// It is possible for the hinted and unhinted versions of the same path to have
1518dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// a different number of points due to GDI's handling of flipped points.
1519dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// If this is detected, this will return false.
1520dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.comstatic bool sk_path_from_gdi_paths(SkPath* path, const uint8_t* glyphbuf, DWORD total_size,
15210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                   GDIGlyphbufferPointIter hintedYs) {
15220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const uint8_t* cur_glyph = glyphbuf;
15230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const uint8_t* end_glyph = glyphbuf + total_size;
15240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1525dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    POINTFX const * hintedPoint;
1526dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com
15270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    while (cur_glyph < end_glyph) {
15280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph;
15290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const uint8_t* end_poly = cur_glyph + th->cb;
15310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER);
15320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1533dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        move_next_expected_hinted_point(hintedYs, hintedPoint);
15340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)),
1535dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                     SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y)));
15360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        while (cur_poly < end_poly) {
15380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly;
15390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (pc->wType == TT_PRIM_LINE) {
15410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                for (uint16_t i = 0; i < pc->cpfx; i++) {
1542dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    move_next_expected_hinted_point(hintedYs, hintedPoint);
15430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)),
1544dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y)));
15450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
15460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
15470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (pc->wType == TT_PRIM_QSPLINE) {
15490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                POINTFX currentPoint = pc->apfx[0];
1550dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                move_next_expected_hinted_point(hintedYs, hintedPoint);
15510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                // only take the hinted y if it wasn't flipped
15520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) {
1553dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    currentPoint.y = hintedPoint->y;
15540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
15550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline
15560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    POINTFX pnt_b = currentPoint;//pc->apfx[u]; // B is always the current point
15570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    POINTFX pnt_c = pc->apfx[u+1];
1558dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    move_next_expected_hinted_point(hintedYs, hintedPoint);
15590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    // only take the hinted y if it wasn't flipped
15600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) {
1561dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                        pnt_c.y = hintedPoint->y;
15620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    }
15630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    currentPoint.x = pnt_c.x;
15640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    currentPoint.y = pnt_c.y;
15650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    if (u < pc->cpfx - 2) {          // If not on last spline, compute C
15670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                        pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x),
15680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                                            SkFIXEDToFixed(pnt_c.x)));
15690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                        pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y),
15700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                                            SkFIXEDToFixed(pnt_c.y)));
15710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    }
15720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15730abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)),
15740abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)),
15750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)),
15760abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y)));
15770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
15780abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
15790abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // Advance past this TTPOLYCURVE.
15800abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx;
15810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
15820abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        cur_glyph += th->cb;
15830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        path->close();
15840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
1585dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    return true;
15860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
15870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
158830ddd615c447fed73286151b463af20d309c85f1reed@google.comDWORD SkScalerContext_GDI::getGDIGlyphPath(const SkGlyph& glyph, UINT flags,
15890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                               SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf)
15900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com{
15910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GLYPHMETRICS gm;
15920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1593b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    DWORD total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, BUFFERSIZE, glyphbuf->get(), &fMat22);
15940abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Sometimes GetGlyphOutlineW returns a number larger than BUFFERSIZE even if BUFFERSIZE > 0.
15950abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // It has been verified that this does not involve a buffer overrun.
15960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (GDI_ERROR == total_size || total_size > BUFFERSIZE) {
15970abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // GDI_ERROR because the BUFFERSIZE was too small, or because the data was not accessible.
15980abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // When the data is not accessable GetGlyphOutlineW fails rather quickly,
15990abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // so just try to get the size. If that fails then ensure the data is accessible.
1600b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, 0, NULL, &fMat22);
16010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR == total_size) {
16020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            LogFontTypeface::EnsureAccessible(this->getTypeface());
1603b69d0e0ac45e13f667bc11a937dcb547072bc93dherb            total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, 0, NULL, &fMat22);
16040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (GDI_ERROR == total_size) {
1605c6cb56f36c4aad8ed45486a3bb4de614bb822f1bkkinnunen                // GetGlyphOutlineW is known to fail for some characters, such as spaces.
1606c6cb56f36c4aad8ed45486a3bb4de614bb822f1bkkinnunen                // In these cases, just return that the glyph does not have a shape.
16070abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return 0;
16080abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
16090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
16100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        glyphbuf->reset(total_size);
16120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1613b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        DWORD ret = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, total_size, glyphbuf->get(), &fMat22);
16140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR == ret) {
16150abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            LogFontTypeface::EnsureAccessible(this->getTypeface());
1616b69d0e0ac45e13f667bc11a937dcb547072bc93dherb            ret = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, total_size, glyphbuf->get(), &fMat22);
16170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (GDI_ERROR == ret) {
16180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                SkASSERT(false);
16190abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return 0;
16200abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
16210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
16220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
16230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    return total_size;
16240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
16250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
162630ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generatePath(const SkGlyph& glyph, SkPath* path) {
16270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkASSERT(&glyph && path);
16280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkASSERT(fDDC);
16290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    path->reset();
16310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Out of all the fonts on a typical Windows box,
16330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 25% of glyphs require more than 2KB.
16340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 1% of glyphs require more than 4KB.
16350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 0.01% of glyphs require more than 8KB.
16360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 8KB is less than 1% of the normal 1MB stack on Windows.
16370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Note that some web fonts glyphs require more than 20KB.
16380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //static const DWORD BUFFERSIZE = (1 << 13);
16390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //GDI only uses hinted outlines when axis aligned.
16410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    UINT format = GGO_NATIVE | GGO_GLYPH_INDEX;
16420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (fRec.getHinting() == SkPaint::kNo_Hinting || fRec.getHinting() == SkPaint::kSlight_Hinting){
16430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        format |= GGO_UNHINTED;
16440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
16450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkAutoSTMalloc<BUFFERSIZE, uint8_t> glyphbuf(BUFFERSIZE);
16460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    DWORD total_size = getGDIGlyphPath(glyph, format, &glyphbuf);
16470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == total_size) {
16480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        return;
16490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
16500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (fRec.getHinting() != SkPaint::kSlight_Hinting) {
16520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        sk_path_from_gdi_path(path, glyphbuf, total_size);
16530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else {
16540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        //GDI only uses hinted outlines when axis aligned.
16550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        UINT format = GGO_NATIVE | GGO_GLYPH_INDEX;
16560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkAutoSTMalloc<BUFFERSIZE, uint8_t> hintedGlyphbuf(BUFFERSIZE);
16580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        DWORD hinted_total_size = getGDIGlyphPath(glyph, format, &hintedGlyphbuf);
16590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (0 == hinted_total_size) {
16600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return;
16610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
16620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1663dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        if (!sk_path_from_gdi_paths(path, glyphbuf, total_size,
1664dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                                    GDIGlyphbufferPointIter(hintedGlyphbuf, hinted_total_size)))
1665dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        {
1666dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            path->reset();
1667dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            sk_path_from_gdi_path(path, glyphbuf, total_size);
1668dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        }
16690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
16700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
16710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1672484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comstatic void logfont_for_name(const char* familyName, LOGFONT* lf) {
1673484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    sk_bzero(lf, sizeof(LOGFONT));
1674e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#ifdef UNICODE
1675484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Get the buffer size needed first.
1676484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName,
1677484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                                            -1, NULL, 0);
1678484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Allocate a buffer (str_len already has terminating null
1679484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // accounted for).
1680484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    wchar_t *wideFamilyName = new wchar_t[str_len];
1681484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Now actually convert the string.
1682484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::MultiByteToWideChar(CP_UTF8, 0, familyName, -1,
1683484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                            wideFamilyName, str_len);
1684484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::wcsncpy(lf->lfFaceName, wideFamilyName, LF_FACESIZE - 1);
1685484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    delete [] wideFamilyName;
1686484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    lf->lfFaceName[LF_FACESIZE-1] = L'\0';
1687e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#else
1688484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::strncpy(lf->lfFaceName, familyName, LF_FACESIZE - 1);
1689484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    lf->lfFaceName[LF_FACESIZE - 1] = '\0';
1690e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#endif
1691e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com}
1692e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com
1693b374d6a62c0259387d90cad74753d8bad9ee1beabungemanvoid LogFontTypeface::onGetFamilyName(SkString* familyName) const {
16947103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    // Get the actual name of the typeface. The logfont may not know this.
16955526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    HFONT font = CreateFontIndirect(&fLogFont);
16967103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
16977103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    HDC deviceContext = ::CreateCompatibleDC(NULL);
16987103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    HFONT savefont = (HFONT)SelectObject(deviceContext, font);
16997103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
1700b374d6a62c0259387d90cad74753d8bad9ee1beabungeman    dcfontname_to_skstring(deviceContext, fLogFont, familyName);
17017103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
17027103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    if (deviceContext) {
17037103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::SelectObject(deviceContext, savefont);
17047103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::DeleteDC(deviceContext);
17057103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    }
17067103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    if (font) {
17077103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::DeleteObject(font);
17087103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    }
1709b374d6a62c0259387d90cad74753d8bad9ee1beabungeman}
17107103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
1711b374d6a62c0259387d90cad74753d8bad9ee1beabungemanvoid LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
1712b374d6a62c0259387d90cad74753d8bad9ee1beabungeman                                          bool* isLocalStream) const {
1713b374d6a62c0259387d90cad74753d8bad9ee1beabungeman    SkString familyName;
1714b374d6a62c0259387d90cad74753d8bad9ee1beabungeman    this->onGetFamilyName(&familyName);
17155526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    desc->setFamilyName(familyName.c_str());
17165526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    *isLocalStream = this->fSerializeAsStream;
1717ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1718ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1719ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic bool getWidthAdvance(HDC hdc, int gId, int16_t* advance) {
1720ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Initialize the MAT2 structure to the identify transformation matrix.
1721ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static const MAT2 mat2 = {SkScalarToFIXED(1), SkScalarToFIXED(0),
1722ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                        SkScalarToFIXED(0), SkScalarToFIXED(1)};
1723ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int flags = GGO_METRICS | GGO_GLYPH_INDEX;
1724ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
1725ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (GDI_ERROR == GetGlyphOutline(hdc, gId, flags, &gm, 0, NULL, &mat2)) {
1726ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        return false;
1727ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1728ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(advance);
1729ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    *advance = gm.gmCellIncX;
1730ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return true;
1731ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1732ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
17332689f615e364dc48ad73826564f5b13d2329179dreed@google.comSkAdvancedTypefaceMetrics* LogFontTypeface::onGetAdvancedTypefaceMetrics(
173439a9a5069b25aa8b9529118cfd0ece5f412fb56breed        PerGlyphInfo perGlyphInfo,
173537ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org        const uint32_t* glyphIDs,
17362689f615e364dc48ad73826564f5b13d2329179dreed@google.com        uint32_t glyphIDsCount) const {
17372689f615e364dc48ad73826564f5b13d2329179dreed@google.com    LOGFONT lf = fLogFont;
1738ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkAdvancedTypefaceMetrics* info = NULL;
1739ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1740ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC hdc = CreateCompatibleDC(NULL);
1741ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT font = CreateFontIndirect(&lf);
1742ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
1743ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT designFont = NULL;
1744ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
174505b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    const char stem_chars[] = {'i', 'I', '!', '1'};
174605b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    int16_t min_width;
174705b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    unsigned glyphCount;
174805b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com
1749ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // To request design units, create a logical font whose height is specified
1750ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // as unitsPerEm.
1751ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    OUTLINETEXTMETRIC otm;
175239698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
175339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (0 == otmRet) {
1754055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(lf);
175539698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
175639698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
175739698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (!otmRet || !GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) {
1758ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        goto Error;
1759ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1760ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    lf.lfHeight = -SkToS32(otm.otmEMSquare);
1761ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    designFont = CreateFontIndirect(&lf);
1762ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, designFont);
1763ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) {
1764ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        goto Error;
1765ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
17667bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    glyphCount = calculateGlyphCount(hdc, fLogFont);
1767ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1768ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info = new SkAdvancedTypefaceMetrics;
1769ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fEmSize = otm.otmEMSquare;
1770ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fLastGlyphID = SkToU16(glyphCount - 1);
1771ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fStyle = 0;
17727103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    tchar_to_skstring(lf.lfFaceName, &info->fFontName);
17730f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
17740f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    // If bit 1 is set, the font may not be embedded in a document.
17750f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    // If bit 1 is clear, the font can be embedded.
17760f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    // If bit 2 is set, the embedding is read-only.
17770f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    if (otm.otmfsType & 0x1) {
17780f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo        info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
17790f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo                info->fFlags,
17800f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo                SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
17810f9bad01b0e7ad592ffb342dcf1d238b15329be1vandebo    }
1782ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
178339a9a5069b25aa8b9529118cfd0ece5f412fb56breed    if (perGlyphInfo & kToUnicode_PerGlyphInfo) {
17846744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        populate_glyph_to_unicode(hdc, glyphCount, &(info->fGlyphToUnicode));
17856744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
17866744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
1787d41e70de196e78e21e1001d23a7259785cbd0835vandebo@chromium.org    if (glyphCount > 0 &&
1788d41e70de196e78e21e1001d23a7259785cbd0835vandebo@chromium.org        (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE)) {
1789ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
1790ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
1791ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
1792ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fItalicAngle = 0;
1793ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fAscent = 0;
1794ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fDescent = 0;
1795ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStemV = 0;
1796ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fCapHeight = 0;
1797ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fBBox = SkIRect::MakeEmpty();
1798390c6d7a9018e233a6519397ac6c739fb21a99efedisonn@google.com        goto ReturnInfo;
1799ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
18005aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org
1801ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // If this bit is clear the font is a fixed pitch font.
1802ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1803ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
1804ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1805ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (otm.otmTextMetrics.tmItalic) {
1806ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
1807ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1808ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) {
1809ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
1810ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) {
1811ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
1812ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1813ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1814ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // The main italic angle of the font, in tenths of a degree counterclockwise
1815ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // from vertical.
1816ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fItalicAngle = otm.otmItalicAngle / 10;
1817ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent);
1818ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent);
1819ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // TODO(ctguil): Use alternate cap height calculation.
1820ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // MSDN says otmsCapEmHeight is not support but it is returning a value on
1821ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // my Win7 box.
1822ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fCapHeight = otm.otmsCapEmHeight;
1823ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fBBox =
1824ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top,
1825ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                          otm.otmrcFontBox.right, otm.otmrcFontBox.bottom);
1826ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1827ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Figure out a good guess for StemV - Min width of i, I, !, 1.
1828ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // This probably isn't very good with an italic font.
182905b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    min_width = SHRT_MAX;
1830ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fStemV = 0;
1831ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
1832ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ABC abcWidths;
1833ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
1834ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            int16_t width = abcWidths.abcB;
1835ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            if (width > 0 && width < min_width) {
1836ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                min_width = width;
1837ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                info->fStemV = min_width;
1838ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            }
1839ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1840ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1841ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
184239a9a5069b25aa8b9529118cfd0ece5f412fb56breed    if (perGlyphInfo & kHAdvance_PerGlyphInfo) {
18435aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) {
18445aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            appendRange(&info->fGlyphWidths, 0);
18455aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            info->fGlyphWidths->fAdvance.append(1, &min_width);
18465aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            finishRange(info->fGlyphWidths.get(), 0,
18475aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org                        SkAdvancedTypefaceMetrics::WidthRange::kDefault);
18485aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        } else {
18495aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            info->fGlyphWidths.reset(
185037ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                getAdvanceData(hdc,
185137ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphCount,
185237ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphIDs,
185337ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphIDsCount,
185437ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               &getWidthAdvance));
18555aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        }
1856ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1857ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1858ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comError:
1859390c6d7a9018e233a6519397ac6c739fb21a99efedisonn@google.comReturnInfo:
1860ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, savefont);
1861ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(designFont);
1862ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(font);
1863ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteDC(hdc);
1864ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1865ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return info;
1866ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1867ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1868a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com//Dummy representation of a Base64 encoded GUID from create_unique_font_name.
1869a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#define BASE64_GUID_ID "XXXXXXXXXXXXXXXXXXXXXXXX"
1870a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com//Length of GUID representation from create_id, including NULL terminator.
1871a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#define BASE64_GUID_ID_LEN SK_ARRAY_COUNT(BASE64_GUID_ID)
1872a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1873a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comSK_COMPILE_ASSERT(BASE64_GUID_ID_LEN < LF_FACESIZE, GUID_longer_than_facesize);
1874a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1875a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1876a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   NameID 6 Postscript names cannot have the character '/'.
1877a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   It would be easier to hex encode the GUID, but that is 32 bytes,
1878a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   and many systems have issues with names longer than 28 bytes.
1879a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The following need not be any standard base64 encoding.
1880a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The encoded value is never decoded.
1881a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com*/
1882d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.comstatic const char postscript_safe_base64_encode[] =
1883a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1884a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "abcdefghijklmnopqrstuvwxyz"
1885a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "0123456789-_=";
1886a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1887a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1888a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Formats a GUID into Base64 and places it into buffer.
1889a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   buffer should have space for at least BASE64_GUID_ID_LEN characters.
1890a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The string will always be null terminated.
1891a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   XXXXXXXXXXXXXXXXXXXXXXXX0
1892a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
1893a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic void format_guid_b64(const GUID& guid, char* buffer, size_t bufferSize) {
1894a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    SkASSERT(bufferSize >= BASE64_GUID_ID_LEN);
1895a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    size_t written = SkBase64::Encode(&guid, sizeof(guid), buffer, postscript_safe_base64_encode);
1896a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    SkASSERT(written < LF_FACESIZE);
1897a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    buffer[written] = '\0';
1898a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
1899a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1900a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1901a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Creates a Base64 encoded GUID and places it into buffer.
1902a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   buffer should have space for at least BASE64_GUID_ID_LEN characters.
1903a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The string will always be null terminated.
1904a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   XXXXXXXXXXXXXXXXXXXXXXXX0
1905a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
1906a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic HRESULT create_unique_font_name(char* buffer, size_t bufferSize) {
1907a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    GUID guid = {};
1908a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (FAILED(CoCreateGuid(&guid))) {
1909a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return E_UNEXPECTED;
1910a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1911a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    format_guid_b64(guid, buffer, bufferSize);
1912a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1913a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return S_OK;
1914a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
1915a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1916a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1917a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Introduces a font to GDI. On failure will return NULL. The returned handle
1918a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   should eventually be passed to RemoveFontMemResourceEx.
1919a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com*/
1920a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic HANDLE activate_font(SkData* fontData) {
1921a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    DWORD numFonts = 0;
1922a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    //AddFontMemResourceEx just copies the data, but does not specify const.
1923a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    HANDLE fontHandle = AddFontMemResourceEx(const_cast<void*>(fontData->data()),
19244b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com                                             static_cast<DWORD>(fontData->size()),
1925a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com                                             0,
1926a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com                                             &numFonts);
1927a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1928a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (fontHandle != NULL && numFonts < 1) {
1929a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        RemoveFontMemResourceEx(fontHandle);
1930a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
1931a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1932a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1933a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return fontHandle;
1934a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
1935a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1936a1193e4b0e34a7e4e1bd33e9708d7341679f8321scroggo// Does not affect ownership of stream.
19375f213d9627d2eefa7da81cd97f36754f75eb4ae9bungemanstatic SkTypeface* create_from_stream(SkStreamAsset* stream) {
1938a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Create a unique and unpredictable font name.
1939a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Avoids collisions and access from CSS.
1940a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    char familyName[BASE64_GUID_ID_LEN];
1941a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    const int familyNameSize = SK_ARRAY_COUNT(familyName);
1942a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (FAILED(create_unique_font_name(familyName, familyNameSize))) {
1943a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
1944a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1945d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
1946a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Change the name of the font.
1947e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    SkAutoTUnref<SkData> rewrittenFontData(SkOTUtils::RenameFont(stream, familyName, familyNameSize-1));
1948e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    if (NULL == rewrittenFontData.get()) {
1949a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
1950a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1951ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1952a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Register the font with GDI.
1953e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    HANDLE fontReference = activate_font(rewrittenFontData.get());
1954a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (NULL == fontReference) {
1955a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
1956a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1957a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1958a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Create the typeface.
1959a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LOGFONT lf;
1960484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    logfont_for_name(familyName, &lf);
1961a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1962a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return SkCreateFontMemResourceTypefaceFromLOGFONT(lf, fontReference);
1963ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1964ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
19655f213d9627d2eefa7da81cd97f36754f75eb4ae9bungemanSkStreamAsset* LogFontTypeface::onOpenStream(int* ttcIndex) const {
19660042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com    *ttcIndex = 0;
19670042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com
1968f4c2622a1a3489988f2d85a81fdc132b17a92bddctguil@chromium.org    const DWORD kTTCTag =
1969f4c2622a1a3489988f2d85a81fdc132b17a92bddctguil@chromium.org        SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f'));
19700042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com    LOGFONT lf = fLogFont;
1971ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1972ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
197359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    HFONT font = CreateFontIndirect(&lf);
1974ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
1975ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1976d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    SkMemoryStream* stream = NULL;
1977d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    DWORD tables[2] = {kTTCTag, 0};
1978d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    for (int i = 0; i < SK_ARRAY_COUNT(tables); i++) {
19794b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com        DWORD bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0);
198039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        if (bufferSize == GDI_ERROR) {
1981055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com            call_ensure_accessible(lf);
198239698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0);
198339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        }
1984d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org        if (bufferSize != GDI_ERROR) {
1985d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            stream = new SkMemoryStream(bufferSize);
19864b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com            if (GetFontData(hdc, tables[i], 0, (void*)stream->getMemoryBase(), bufferSize)) {
1987d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                break;
1988d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            } else {
1989d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                delete stream;
1990d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                stream = NULL;
1991d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            }
1992d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org        }
1993ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1994ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1995ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, savefont);
1996ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(font);
1997ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteDC(hdc);
1998ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1999ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return stream;
2000ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
2001ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
20023c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comstatic void bmpCharsToGlyphs(HDC hdc, const WCHAR* bmpChars, int count, uint16_t* glyphs,
20033c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                             bool Ox1FHack)
20043c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com{
20053c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    DWORD result = GetGlyphIndicesW(hdc, bmpChars, count, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
20063c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (GDI_ERROR == result) {
20073c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
20083c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            glyphs[i] = 0;
20093c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
20103c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        return;
20113c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20123c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20133c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (Ox1FHack) {
20143c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
20153c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (0xFFFF == glyphs[i] || 0x1F == glyphs[i]) {
20163c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[i] = 0;
20173c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
20183c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
20193c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    } else {
20203c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
20213c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (0xFFFF == glyphs[i]){
20223c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[i] = 0;
20233c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
20243c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
20253c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20263c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
20273c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20283c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comstatic uint16_t nonBmpCharToGlyph(HDC hdc, SCRIPT_CACHE* scriptCache, const WCHAR utf16[2]) {
20293c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    uint16_t index = 0;
20303c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Use uniscribe to detemine glyph index for non-BMP characters.
20313c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int numWCHAR = 2;
20323c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int maxItems = 2;
20333c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // MSDN states that this can be NULL, but some things don't work then.
20343c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_CONTROL scriptControl = { 0 };
20353c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Add extra item to SCRIPT_ITEM to work around a bug (now documented).
20363c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // https://bugzilla.mozilla.org/show_bug.cgi?id=366643
20373c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_ITEM si[maxItems + 1];
20383c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    int numItems;
20393c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &scriptControl, NULL, si, &numItems),
20403c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com         "Could not itemize character.");
20413c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20423c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs.
20433c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int maxGlyphs = 2;
20443c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_VISATTR vsa[maxGlyphs];
20453c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    WORD outGlyphs[maxGlyphs];
20463c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    WORD logClust[numWCHAR];
20473c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    int numGlyphs;
20483c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HRZM(ScriptShape(hdc, scriptCache, utf16, numWCHAR, maxGlyphs, &si[0].a,
20493c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                     outGlyphs, logClust, vsa, &numGlyphs),
20503c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com         "Could not shape character.");
20513c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (1 == numGlyphs) {
20523c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        index = outGlyphs[0];
20533c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20543c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    return index;
20553c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
20563c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20573c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comclass SkAutoHDC {
20583c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.compublic:
20593c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoHDC(const LOGFONT& lf)
20603c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        : fHdc(::CreateCompatibleDC(NULL))
20613c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        , fFont(::CreateFontIndirect(&lf))
20623c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        , fSavefont((HFONT)SelectObject(fHdc, fFont))
20633c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    { }
20643c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    ~SkAutoHDC() {
20653c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SelectObject(fHdc, fSavefont);
20663c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        DeleteObject(fFont);
20673c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        DeleteDC(fHdc);
20683c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20693c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    operator HDC() { return fHdc; }
20703c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comprivate:
20713c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HDC fHdc;
20723c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HFONT fFont;
20733c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HFONT fSavefont;
20743c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com};
2075e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoHDC(...) SK_REQUIRE_LOCAL_VAR(SkAutoHDC)
20763c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20773c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comint LogFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
20783c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                                     uint16_t userGlyphs[], int glyphCount) const
20793c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com{
20803c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoHDC hdc(fLogFont);
20813c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20823c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    TEXTMETRIC tm;
20833c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (0 == GetTextMetrics(hdc, &tm)) {
20843c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        call_ensure_accessible(fLogFont);
20853c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (0 == GetTextMetrics(hdc, &tm)) {
20863c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            tm.tmPitchAndFamily = TMPF_TRUETYPE;
20873c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
20883c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20893c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    bool Ox1FHack = !(tm.tmPitchAndFamily & TMPF_VECTOR) /*&& winVer < Vista */;
20903c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20913c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoSTMalloc<256, uint16_t> scratchGlyphs;
20923c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    uint16_t* glyphs;
20933c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (userGlyphs != NULL) {
20943c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        glyphs = userGlyphs;
20953c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    } else {
20963c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        glyphs = scratchGlyphs.reset(glyphCount);
20973c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20983c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20993c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_CACHE sc = 0;
21003c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    switch (encoding) {
21013c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF8_Encoding: {
21023c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        static const int scratchCount = 256;
21033c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        WCHAR scratch[scratchCount];
21043c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
21053c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const char* currentUtf8 = reinterpret_cast<const char*>(chars);
21063c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SkUnichar currentChar;
21073c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (glyphCount) {
21083c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            currentChar = SkUTF8_NextUnichar(&currentUtf8);
21093c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21103c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
21113c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
21123c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount);
21133c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
21143c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && currentChar <= 0xFFFF) {
21153c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                scratch[runLength] = static_cast<WCHAR>(currentChar);
21163c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
21173c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                if (runLength < glyphsLeft) {
21183c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                    currentChar = SkUTF8_NextUnichar(&currentUtf8);
21193c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                }
21203c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21213c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
21223c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack);
21233c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
21243c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21253c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21263c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
21273c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && currentChar > 0xFFFF) {
21283c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                SkUTF16_FromUnichar(currentChar, reinterpret_cast<uint16_t*>(scratch));
21293c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch);
21303c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
21313c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                if (glyphIndex < glyphCount) {
21323c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                    currentChar = SkUTF8_NextUnichar(&currentUtf8);
21333c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                }
21343c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21353c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21363c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
21373c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21383c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF16_Encoding: {
21393c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
21403c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const WCHAR* currentUtf16 = reinterpret_cast<const WCHAR*>(chars);
21413c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
21423c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
21433c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = glyphCount - glyphIndex;
21443c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
21453c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && !SkUTF16_IsHighSurrogate(currentUtf16[runLength])) {
21463c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
21473c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21483c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
21493c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, currentUtf16, runLength, &glyphs[glyphIndex], Ox1FHack);
21503c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
21513c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                currentUtf16 += runLength;
21523c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21533c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21543c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
21553c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && SkUTF16_IsHighSurrogate(*currentUtf16)) {
21563c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, currentUtf16);
21573c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
21583c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                currentUtf16 += 2;
21593c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21603c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21613c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
21623c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21633c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF32_Encoding: {
21643c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        static const int scratchCount = 256;
21653c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        WCHAR scratch[scratchCount];
21663c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
21673c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const uint32_t* utf32 = reinterpret_cast<const uint32_t*>(chars);
21683c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
21693c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
21703c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount);
21713c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
21723c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && utf32[glyphIndex + runLength] <= 0xFFFF) {
21733c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                scratch[runLength] = static_cast<WCHAR>(utf32[glyphIndex + runLength]);
21743c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
21753c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21763c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
21773c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack);
21783c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
21793c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21803c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21813c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
21823c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && utf32[glyphIndex] > 0xFFFF) {
21833c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                SkUTF16_FromUnichar(utf32[glyphIndex], reinterpret_cast<uint16_t*>(scratch));
21843c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch);
21853c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
21863c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21873c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21883c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
21893c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21903c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    default:
21913c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SK_CRASH();
21923c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21933c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21943c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (sc) {
21953c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        ::ScriptFreeCache(&sc);
21963c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21973c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21983c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    for (int i = 0; i < glyphCount; ++i) {
21993c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (0 == glyphs[i]) {
22003c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            return i;
22013c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
22023c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22033c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    return glyphCount;
22043c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
22053c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22067bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comint LogFontTypeface::onCountGlyphs() const {
22077bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
22087bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT font = CreateFontIndirect(&fLogFont);
22097bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
22107bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22117bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int glyphCount = calculateGlyphCount(hdc, fLogFont);
22127bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22137bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    SelectObject(hdc, savefont);
22147bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteObject(font);
22157bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteDC(hdc);
22167bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22177bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return glyphCount;
22187bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
22197bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22207bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comint LogFontTypeface::onGetUPEM() const {
22217bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
22227bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT font = CreateFontIndirect(&fLogFont);
22237bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
22247bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22257bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int upem = calculateUPEM(hdc, fLogFont);
22267bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22277bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    SelectObject(hdc, savefont);
22287bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteObject(font);
22297bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteDC(hdc);
22307bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22317bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return upem;
22327bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
22337bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
2234839702b61934914118ec557dd641be322eba3b5fbungeman@google.comSkTypeface::LocalizedStrings* LogFontTypeface::onCreateFamilyNameIterator() const {
2235a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkTypeface::LocalizedStrings* nameIter =
2236a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
2237a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (NULL == nameIter) {
2238a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString familyName;
2239a980269c2498836101146adc729ef780fb89824ebungeman@google.com        this->getFamilyName(&familyName);
2240a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString language("und"); //undetermined
2241a980269c2498836101146adc729ef780fb89824ebungeman@google.com        nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
2242a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
2243a980269c2498836101146adc729ef780fb89824ebungeman@google.com    return nameIter;
2244a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
2245a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2246b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.comint LogFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
2247b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    SkSFNTHeader header;
2248b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (sizeof(header) != this->onGetTableData(0, 0, sizeof(header), &header)) {
2249b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        return 0;
2250b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2251b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2252b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    int numTables = SkEndian_SwapBE16(header.numTables);
2253b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2254b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (tags) {
2255b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        size_t size = numTables * sizeof(SkSFNTHeader::TableDirectoryEntry);
2256b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        SkAutoSTMalloc<0x20, SkSFNTHeader::TableDirectoryEntry> dir(numTables);
2257b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        if (size != this->onGetTableData(0, sizeof(header), size, dir.get())) {
2258b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com            return 0;
2259b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        }
2260b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2261b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        for (int i = 0; i < numTables; ++i) {
2262b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com            tags[i] = SkEndian_SwapBE32(dir[i].tag);
2263b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        }
2264b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2265b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    return numTables;
2266b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com}
2267b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2268b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.comsize_t LogFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
2269b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com                                       size_t length, void* data) const
2270b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com{
2271b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    LOGFONT lf = fLogFont;
2272b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2273b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
2274b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HFONT font = CreateFontIndirect(&lf);
2275b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
2276b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2277b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    tag = SkEndian_SwapBE32(tag);
2278b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (NULL == data) {
2279b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        length = 0;
2280b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
22818b169311b59ab84e8ca6f3630a1e960cc1be751erobertphillips@google.com    DWORD bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length);
2282b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (bufferSize == GDI_ERROR) {
2283b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        call_ensure_accessible(lf);
22848b169311b59ab84e8ca6f3630a1e960cc1be751erobertphillips@google.com        bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length);
2285b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2286b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2287b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    SelectObject(hdc, savefont);
2288b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    DeleteObject(font);
2289b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    DeleteDC(hdc);
2290b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2291b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    return bufferSize == GDI_ERROR ? 0 : bufferSize;
2292b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com}
2293b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
22940da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comSkScalerContext* LogFontTypeface::onCreateScalerContext(const SkDescriptor* desc) const {
229530ddd615c447fed73286151b463af20d309c85f1reed@google.com    SkScalerContext_GDI* ctx = SkNEW_ARGS(SkScalerContext_GDI,
229684e22d847fc84727bc220947162363ee1fe068fcreed@google.com                                                (const_cast<LogFontTypeface*>(this), desc));
229784e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!ctx->isValid()) {
229884e22d847fc84727bc220947162363ee1fe068fcreed@google.com        SkDELETE(ctx);
229984e22d847fc84727bc220947162363ee1fe068fcreed@google.com        ctx = NULL;
230084e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
230184e22d847fc84727bc220947162363ee1fe068fcreed@google.com    return ctx;
2302ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
2303ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
23040da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comvoid LogFontTypeface::onFilterRec(SkScalerContextRec* rec) const {
2305c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag ||
2306c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fFlags & SkScalerContext::kLCD_Vertical_Flag)
2307c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    {
2308c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fMaskFormat = SkMask::kA8_Format;
2309c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag;
2310c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    }
2311c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org
2312410780677af260e32948b02c0725ef6ad761260cbungeman    unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag |
2313410780677af260e32948b02c0725ef6ad761260cbungeman                                  SkScalerContext::kDevKernText_Flag |
2314f6f56878bc96386882721b15ba610984185d3dc9bungeman@google.com                                  SkScalerContext::kForceAutohinting_Flag |
2315e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kEmbeddedBitmapText_Flag |
2316e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kEmbolden_Flag |
2317e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kLCD_BGROrder_Flag |
2318e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kLCD_Vertical_Flag;
2319e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    rec->fFlags &= ~flagsWeDontSupport;
2320e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com
2321e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    SkPaint::Hinting h = rec->getHinting();
2322e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    switch (h) {
2323e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kNo_Hinting:
23240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            break;
2325e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kSlight_Hinting:
23260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // Only do slight hinting when axis aligned.
23277c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            // TODO: re-enable slight hinting when FontHostTest can pass.
23287c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            //if (!isAxisAligned(*rec)) {
23290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                h = SkPaint::kNo_Hinting;
23307c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            //}
2331e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            break;
2332e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kNormal_Hinting:
2333e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kFull_Hinting:
23340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // TODO: need to be able to distinguish subpixel positioned glyphs
23350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // and linear metrics.
23360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            //rec->fFlags &= ~SkScalerContext::kSubpixelPositioning_Flag;
2337e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            h = SkPaint::kNormal_Hinting;
2338e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            break;
2339e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        default:
23400c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("unknown hinting");
2341e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    }
23420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //TODO: if this is a bitmap font, squash hinting and subpixel.
2343e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    rec->setHinting(h);
2344da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com
23459181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com// turn this off since GDI might turn A8 into BW! Need a bigger fix.
23469181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com#if 0
2347da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    // Disable LCD when rotated, since GDI's output is ugly
2348da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    if (isLCD(*rec) && !isAxisAligned(*rec)) {
2349da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com        rec->fMaskFormat = SkMask::kA8_Format;
2350da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    }
23519181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com#endif
2352754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com
23530da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    if (!fCanBeLCD && isLCD(*rec)) {
2354cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        rec->fMaskFormat = SkMask::kA8_Format;
2355cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        rec->fFlags &= ~SkScalerContext::kGenA8FromLCD_Flag;
2356754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    }
2357754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com}
2358070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
2359070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com///////////////////////////////////////////////////////////////////////////////
2360070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
2361070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com#include "SkFontMgr.h"
2362484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#include "SkDataTable.h"
2363484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2364a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic bool valid_logfont_for_enum(const LOGFONT& lf) {
2365a980269c2498836101146adc729ef780fb89824ebungeman@google.com    // TODO: Vector FON is unsupported and should not be listed.
2366a980269c2498836101146adc729ef780fb89824ebungeman@google.com    return
2367a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // Ignore implicit vertical variants.
2368a980269c2498836101146adc729ef780fb89824ebungeman@google.com        lf.lfFaceName[0] && lf.lfFaceName[0] != '@'
2369a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2370a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // DEFAULT_CHARSET is used to get all fonts, but also implies all
2371a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // character sets. Filter assuming all fonts support ANSI_CHARSET.
2372a980269c2498836101146adc729ef780fb89824ebungeman@google.com        && ANSI_CHARSET == lf.lfCharSet
2373a980269c2498836101146adc729ef780fb89824ebungeman@google.com    ;
2374a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
2375a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2376a980269c2498836101146adc729ef780fb89824ebungeman@google.com/** An EnumFontFamExProc implementation which interprets builderParam as
2377a980269c2498836101146adc729ef780fb89824ebungeman@google.com *  an SkTDArray<ENUMLOGFONTEX>* and appends logfonts which
2378a980269c2498836101146adc729ef780fb89824ebungeman@google.com *  pass the valid_logfont_for_enum predicate.
2379a980269c2498836101146adc729ef780fb89824ebungeman@google.com */
2380a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*,
2381a980269c2498836101146adc729ef780fb89824ebungeman@google.com                                     DWORD fontType, LPARAM builderParam) {
2382a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (valid_logfont_for_enum(*lf)) {
2383a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderParam;
2384a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        *array->append() = *(ENUMLOGFONTEX*)lf;
2385484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2386484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    return 1; // non-zero means continue
2387484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com}
2388484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2389484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comclass SkFontStyleSetGDI : public SkFontStyleSet {
2390484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.compublic:
2391a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkFontStyleSetGDI(const TCHAR familyName[]) {
2392a980269c2498836101146adc729ef780fb89824ebungeman@google.com        LOGFONT lf;
2393a980269c2498836101146adc729ef780fb89824ebungeman@google.com        sk_bzero(&lf, sizeof(lf));
2394a980269c2498836101146adc729ef780fb89824ebungeman@google.com        lf.lfCharSet = DEFAULT_CHARSET;
2395a980269c2498836101146adc729ef780fb89824ebungeman@google.com        _tcscpy_s(lf.lfFaceName, familyName);
2396a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2397484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        HDC hdc = ::CreateCompatibleDC(NULL);
2398a980269c2498836101146adc729ef780fb89824ebungeman@google.com        ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0);
2399484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::DeleteDC(hdc);
2400484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2401484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
240236352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    int count() override {
2403484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return fArray.count();
2404484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2405484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
240636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void getStyle(int index, SkFontStyle* fs, SkString* styleName) override {
2407484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        if (fs) {
2408a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman            *fs = get_style(fArray[index].elfLogFont);
2409a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        }
2410a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        if (styleName) {
2411a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            const ENUMLOGFONTEX& ref = fArray[index];
2412a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type in the
2413a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // non-unicode version.
2414a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            //      ENUMLOGFONTEX uses BYTE
2415a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            //      LOGFONT uses CHAR
2416a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // Here we assert they that the style name is logically the same (size) as
2417a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // a TCHAR, so we can use the same converter function.
2418a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0]));
2419a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            tchar_to_skstring((const TCHAR*)ref.elfStyle, styleName);
2420484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        }
2421484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2422484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
242336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface* createTypeface(int index) override {
2424a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkCreateTypefaceFromLOGFONT(fArray[index].elfLogFont);
2425484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2426484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
242736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface* matchStyle(const SkFontStyle& pattern) override {
2428484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        // todo:
2429a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkCreateTypefaceFromLOGFONT(fArray[0].elfLogFont);
2430484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2431484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2432484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprivate:
2433a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkTDArray<ENUMLOGFONTEX> fArray;
2434484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com};
2435484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2436484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comclass SkFontMgrGDI : public SkFontMgr {
2437a980269c2498836101146adc729ef780fb89824ebungeman@google.compublic:
2438a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkFontMgrGDI() {
2439484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        LOGFONT lf;
2440484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        sk_bzero(&lf, sizeof(lf));
2441484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        lf.lfCharSet = DEFAULT_CHARSET;
2442484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2443484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        HDC hdc = ::CreateCompatibleDC(NULL);
2444484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fLogFontArray, 0);
2445484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::DeleteDC(hdc);
2446484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2447484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2448484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprotected:
244936352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    int onCountFamilies() const override {
2450484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return fLogFontArray.count();
2451484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2452484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
245336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onGetFamilyName(int index, SkString* familyName) const override {
2454484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkASSERT((unsigned)index < (unsigned)fLogFontArray.count());
2455a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        tchar_to_skstring(fLogFontArray[index].elfLogFont.lfFaceName, familyName);
2456484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2457484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
245836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkFontStyleSet* onCreateStyleSet(int index) const override {
2459484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkASSERT((unsigned)index < (unsigned)fLogFontArray.count());
2460a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkNEW_ARGS(SkFontStyleSetGDI, (fLogFontArray[index].elfLogFont.lfFaceName));
2461484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2462484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
246336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
2464484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        if (NULL == familyName) {
2465484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com            familyName = "";    // do we need this check???
2466484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        }
2467484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        LOGFONT lf;
2468484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        logfont_for_name(familyName, &lf);
2469a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkNEW_ARGS(SkFontStyleSetGDI, (lf.lfFaceName));
2470484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2471484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2472484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
247336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                           const SkFontStyle& fontstyle) const override {
2474437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2475484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
2476484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return sset->matchStyle(fontstyle);
2477484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2478484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
247933068c19f1b8c18f000c18935ad11f1082534b5adjsollen    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
248033068c19f1b8c18f000c18935ad11f1082534b5adjsollen                                                    const char* bcp47[], int bcp47Count,
248136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                                    SkUnichar character) const override {
248233068c19f1b8c18f000c18935ad11f1082534b5adjsollen        return NULL;
248333068c19f1b8c18f000c18935ad11f1082534b5adjsollen    }
248433068c19f1b8c18f000c18935ad11f1082534b5adjsollen
2485484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
248636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                         const SkFontStyle& fontstyle) const override {
2487437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2488484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkString familyName;
2489484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ((LogFontTypeface*)familyMember)->getFamilyName(&familyName);
2490484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return this->matchFamilyStyle(familyName.c_str(), fontstyle);
2491484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2492484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
249336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
24945f213d9627d2eefa7da81cd97f36754f75eb4ae9bungeman        SkAutoTDelete<SkStreamAsset> stream(bareStream);
2495437eea160b2f605d9074106891288e85baaecfefreed@google.com        return create_from_stream(stream);
2496484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2497437eea160b2f605d9074106891288e85baaecfefreed@google.com
249836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
2499437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2500a1193e4b0e34a7e4e1bd33e9708d7341679f8321scroggo        return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)));
2501484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2502437eea160b2f605d9074106891288e85baaecfefreed@google.com
250336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
2504437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2505a1193e4b0e34a7e4e1bd33e9708d7341679f8321scroggo        return this->createFromStream(SkStream::NewFromFile(path));
2506484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2507484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
250830ddd615c447fed73286151b463af20d309c85f1reed@google.com    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
250936352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                                               unsigned styleBits) const override {
2510f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        LOGFONT lf;
2511f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        if (NULL == familyName) {
2512f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com            lf = get_default_font();
2513f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        } else {
2514f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com            logfont_for_name(familyName, &lf);
2515f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        }
2516a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman
2517a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        SkTypeface::Style style = (SkTypeface::Style)styleBits;
2518a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        lf.lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL;
2519a4c4a2d8cd65abb1e5ac20813831cdb9ace6c7eebungeman        lf.lfItalic = ((style & SkTypeface::kItalic) != 0);
2520f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        return SkCreateTypefaceFromLOGFONT(lf);
252130ddd615c447fed73286151b463af20d309c85f1reed@google.com    }
252230ddd615c447fed73286151b463af20d309c85f1reed@google.com
2523484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprivate:
2524a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkTDArray<ENUMLOGFONTEX> fLogFontArray;
2525484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com};
2526070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
252730ddd615c447fed73286151b463af20d309c85f1reed@google.com///////////////////////////////////////////////////////////////////////////////
252830ddd615c447fed73286151b463af20d309c85f1reed@google.com
2529bfc6cc4b6c41e99d3887d268fbf798aa3abc9a27bungeman@google.comSkFontMgr* SkFontMgr_New_GDI() {
2530484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    return SkNEW(SkFontMgrGDI);
2531070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com}
2532