SkFontHost_win.cpp revision e1ca705cac4b946993f6cbf798e2a0ba27e739f3
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com */
8ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
9ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkAdvancedTypefaceMetrics.h"
10a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkBase64.h"
111bfe01d06b896eca94c80d9af9a6bb30216b5e42bungeman@google.com#include "SkColorPriv.h"
12a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkData.h"
13a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkDescriptor.h"
14e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#include "SkFontDescriptor.h"
15a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkFontHost.h"
16bbe50131413098bffed9186660c41f047d881596bungeman@google.com#include "SkGlyph.h"
1727f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com#include "SkHRESULT.h"
1897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com#include "SkMaskGamma.h"
197bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com#include "SkOTTable_maxp.h"
20a980269c2498836101146adc729ef780fb89824ebungeman@google.com#include "SkOTTable_name.h"
21a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkOTUtils.h"
2227889877c0e3c909dd4afd7b5e7b1d58cbf61ae6reed@google.com#include "SkPath.h"
23b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com#include "SkSFNTHeader.h"
24ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkStream.h"
25a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkString.h"
2605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com#include "SkTemplates.h"
27ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkThread.h"
28ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkTypeface_win.h"
2959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com#include "SkTypefaceCache.h"
30ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkUtils.h"
31ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
32a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include "SkTypes.h"
33a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <tchar.h>
34a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <usp10.h>
35a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#include <objbase.h>
36ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
37f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.comstatic void (*gEnsureLOGFONTAccessibleProc)(const LOGFONT&);
38f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com
39f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.comvoid SkTypeface_SetEnsureLOGFONTAccessibleProc(void (*proc)(const LOGFONT&)) {
40f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com    gEnsureLOGFONTAccessibleProc = proc;
41f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com}
42f210f505ffffccc15ae5e91f8ccb9763ba7fc152reed@google.com
43055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.comstatic void call_ensure_accessible(const LOGFONT& lf) {
44055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    if (gEnsureLOGFONTAccessibleProc) {
45055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        gEnsureLOGFONTAccessibleProc(lf);
46055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    }
47055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com}
48055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
49055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com///////////////////////////////////////////////////////////////////////////////
50055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
516f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com// always packed xxRRGGBB
526f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.comtypedef uint32_t SkGdiRGB;
536f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
54a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// define this in your Makefile or .gyp to enforce AA requests
55a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// which GDI ignores at small sizes. This flag guarantees AA
56a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// for rotated text, regardless of GDI's notions.
57a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com//#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
58a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
5982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.comstatic bool isLCD(const SkScalerContext::Rec& rec) {
6082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    return SkMask::kLCD16_Format == rec.fMaskFormat ||
6182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com           SkMask::kLCD32_Format == rec.fMaskFormat;
6282a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
6382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
64a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool bothZero(SkScalar a, SkScalar b) {
65a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    return 0 == a && 0 == b;
66a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
67a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
68a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com// returns false if there is any non-90-rotation or skew
69a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool isAxisAligned(const SkScalerContext::Rec& rec) {
70a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    return 0 == rec.fPreSkewX &&
71a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com           (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
72a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com            bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
73a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
74a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
75a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.comstatic bool needToRenderWithSkia(const SkScalerContext::Rec& rec) {
76a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com#ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS
77a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // What we really want to catch is when GDI will ignore the AA request and give
78a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // us BW instead. Smallish rotated text is one heuristic, so this code is just
79a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // an approximation. We shouldn't need to do this for larger sizes, but at those
80a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // sizes, the quality difference gets less and less between our general
81a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    // scanconverter and GDI's.
82a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) {
83a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com        return true;
84a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    }
85a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com#endif
860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    return rec.getHinting() == SkPaint::kNo_Hinting || rec.getHinting() == SkPaint::kSlight_Hinting;
87a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com}
88a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
89ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comusing namespace skia_advanced_typeface_metrics_utils;
90ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
91a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.comstatic void tchar_to_skstring(const TCHAR t[], SkString* s) {
92484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#ifdef UNICODE
93484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    size_t sSize = WideCharToMultiByte(CP_UTF8, 0, t, -1, NULL, 0, NULL, NULL);
94484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    s->resize(sSize);
95484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    WideCharToMultiByte(CP_UTF8, 0, t, -1, s->writable_str(), sSize, NULL, NULL);
96484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#else
97484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    s->set(t);
98484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#endif
99484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com}
100484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
101a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic void dcfontname_to_skstring(HDC deviceContext, const LOGFONT& lf, SkString* familyName) {
102a980269c2498836101146adc729ef780fb89824ebungeman@google.com    int fontNameLen; //length of fontName in TCHARS.
103a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (0 == (fontNameLen = GetTextFace(deviceContext, 0, NULL))) {
104a980269c2498836101146adc729ef780fb89824ebungeman@google.com        call_ensure_accessible(lf);
105a980269c2498836101146adc729ef780fb89824ebungeman@google.com        if (0 == (fontNameLen = GetTextFace(deviceContext, 0, NULL))) {
106a980269c2498836101146adc729ef780fb89824ebungeman@google.com            fontNameLen = 0;
107a980269c2498836101146adc729ef780fb89824ebungeman@google.com        }
108a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
109a980269c2498836101146adc729ef780fb89824ebungeman@google.com
110a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkAutoSTArray<LF_FULLFACESIZE, TCHAR> fontName(fontNameLen+1);
111a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) {
112a980269c2498836101146adc729ef780fb89824ebungeman@google.com        call_ensure_accessible(lf);
113a980269c2498836101146adc729ef780fb89824ebungeman@google.com        if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) {
114a980269c2498836101146adc729ef780fb89824ebungeman@google.com            fontName[0] = 0;
115a980269c2498836101146adc729ef780fb89824ebungeman@google.com        }
116a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
117a980269c2498836101146adc729ef780fb89824ebungeman@google.com
118a980269c2498836101146adc729ef780fb89824ebungeman@google.com    tchar_to_skstring(fontName.get(), familyName);
119a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
120a980269c2498836101146adc729ef780fb89824ebungeman@google.com
121ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic void make_canonical(LOGFONT* lf) {
12253cbb0baac09e60e7633c15301a29a83afb1a8debungeman@google.com    lf->lfHeight = -64;
12359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY;
12459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfCharSet = DEFAULT_CHARSET;
12582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com//    lf->lfClipPrecision = 64;
12659d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com}
12759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
12890d812b77ab025d5fd01dab904287f5c5bf56228bungeman@google.comstatic SkTypeface::Style get_style(const LOGFONT& lf) {
12959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    unsigned style = 0;
13059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    if (lf.lfWeight >= FW_BOLD) {
13159d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        style |= SkTypeface::kBold;
13259d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    }
13359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    if (lf.lfItalic) {
13459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        style |= SkTypeface::kItalic;
13559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    }
13690d812b77ab025d5fd01dab904287f5c5bf56228bungeman@google.com    return static_cast<SkTypeface::Style>(style);
13759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com}
13859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
13959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.comstatic void setStyle(LOGFONT* lf, SkTypeface::Style style) {
14059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL ;
14159d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    lf->lfItalic = ((style & SkTypeface::kItalic) != 0);
142ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
143ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
144ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic inline FIXED SkFixedToFIXED(SkFixed x) {
145ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return *(FIXED*)(&x);
146ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
147a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.comstatic inline SkFixed SkFIXEDToFixed(FIXED x) {
148a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    return *(SkFixed*)(&x);
149a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com}
150ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
151ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic inline FIXED SkScalarToFIXED(SkScalar x) {
152ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return SkFixedToFIXED(SkScalarToFixed(x));
153ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
154ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1557bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comstatic unsigned calculateGlyphCount(HDC hdc, const LOGFONT& lf) {
1567bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    TEXTMETRIC textMetric;
1577bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == GetTextMetrics(hdc, &textMetric)) {
1587bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        textMetric.tmPitchAndFamily = TMPF_VECTOR;
1597bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
1607bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        GetTextMetrics(hdc, &textMetric);
1617bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1627bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
1637bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) {
1647bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        return textMetric.tmLastChar;
1657bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1667bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
167ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes.
168ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint16_t glyphs;
1697bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (GDI_ERROR != GetFontData(hdc, SkOTTableMaximumProfile::TAG, 4, &glyphs, sizeof(glyphs))) {
170ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        return SkEndian_SwapBE16(glyphs);
171ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1725aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org
173ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Binary search for glyph count.
174ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
175ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int32_t max = SK_MaxU16 + 1;
176ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int32_t min = 0;
177ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
178ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    while (min < max) {
179ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        int32_t mid = min + ((max - min) / 2);
180ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0,
181ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                             NULL, &mat2) == GDI_ERROR) {
182ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            max = mid;
183ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        } else {
184ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            min = mid + 1;
185ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
186ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
187ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(min == max);
188ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return min;
189ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
190ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1917bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comstatic unsigned calculateUPEM(HDC hdc, const LOGFONT& lf) {
1927bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    TEXTMETRIC textMetric;
1937bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == GetTextMetrics(hdc, &textMetric)) {
1947bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        textMetric.tmPitchAndFamily = TMPF_VECTOR;
1957bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
1967bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        GetTextMetrics(hdc, &textMetric);
1977bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
1987bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
1997bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) {
2007bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        return textMetric.tmMaxCharWidth;
2017bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
2027bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
2037bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    OUTLINETEXTMETRIC otm;
2047bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
2057bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    if (0 == otmRet) {
2067bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        call_ensure_accessible(lf);
2077bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
2087bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    }
2097bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
2107bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return (0 == otmRet) ? 0 : otm.otmEMSquare;
2117bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
2127bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
213ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comclass LogFontTypeface : public SkTypeface {
214ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.compublic:
215cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    LogFontTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf, bool serializeAsStream = false) :
216cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        SkTypeface(style, fontID, false), fLogFont(lf), fSerializeAsStream(serializeAsStream) {
217cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
218cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // If the font has cubic outlines, it will not be rendered with ClearType.
219cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HFONT font = CreateFontIndirect(&lf);
220cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
221cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HDC deviceContext = ::CreateCompatibleDC(NULL);
222cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        HFONT savefont = (HFONT)SelectObject(deviceContext, font);
223cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
224cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        TEXTMETRIC textMetric;
225cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (0 == GetTextMetrics(deviceContext, &textMetric)) {
226055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com            call_ensure_accessible(lf);
227cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            if (0 == GetTextMetrics(deviceContext, &textMetric)) {
228cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com                textMetric.tmPitchAndFamily = TMPF_TRUETYPE;
229cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            }
230cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
231cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (deviceContext) {
232cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::SelectObject(deviceContext, savefont);
233cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::DeleteDC(deviceContext);
234cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
235cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        if (font) {
236cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com            ::DeleteObject(font);
237cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        }
238cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
239fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com        // The fixed pitch bit is set if the font is *not* fixed pitch.
240fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com        this->setIsFixedPitch((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0);
241fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com
242cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // Used a logfont on a memory context, should never get a device font.
243cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        // Therefore all TMPF_DEVICE will be PostScript (cubic) fonts.
244cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) &&
245cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com                      (textMetric.tmPitchAndFamily & TMPF_DEVICE));
246cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    }
247ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
24859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    LOGFONT fLogFont;
249e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com    bool fSerializeAsStream;
250cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    bool fCanBeLCD;
251ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
25259d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    static LogFontTypeface* Create(const LOGFONT& lf) {
25390d812b77ab025d5fd01dab904287f5c5bf56228bungeman@google.com        SkTypeface::Style style = get_style(lf);
25459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        SkFontID fontID = SkTypefaceCache::NewFontID();
25559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        return new LogFontTypeface(style, fontID, lf);
256ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
2570da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com
258055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    static void EnsureAccessible(const SkTypeface* face) {
259055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFont);
260055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    }
261055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
2620da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comprotected:
2630042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com    virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
2640da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE;
2650da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE;
2662689f615e364dc48ad73826564f5b13d2329179dreed@google.com    virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
2672689f615e364dc48ad73826564f5b13d2329179dreed@google.com                                SkAdvancedTypefaceMetrics::PerGlyphInfo,
2682689f615e364dc48ad73826564f5b13d2329179dreed@google.com                                const uint32_t*, uint32_t) const SK_OVERRIDE;
2695526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
2703c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
2713c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                                uint16_t glyphs[], int glyphCount) const SK_OVERRIDE;
2727bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    virtual int onCountGlyphs() const SK_OVERRIDE;
2737bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    virtual int onGetUPEM() const SK_OVERRIDE;
274839702b61934914118ec557dd641be322eba3b5fbungeman@google.com    virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
275b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
276b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    virtual size_t onGetTableData(SkFontTableTag, size_t offset,
277b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com                                  size_t length, void* data) const SK_OVERRIDE;
278ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com};
279ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
280a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comclass FontMemResourceTypeface : public LogFontTypeface {
281a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.compublic:
282a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    /**
283a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     *  Takes ownership of fontMemResource.
284a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     */
285a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    FontMemResourceTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf, HANDLE fontMemResource) :
286cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        LogFontTypeface(style, fontID, lf, true), fFontMemResource(fontMemResource) {
287e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com    }
288a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
289a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    HANDLE fFontMemResource;
290a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
291a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    /**
292a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     *  The created FontMemResourceTypeface takes ownership of fontMemResource.
293a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com     */
294a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemResource) {
295a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        SkTypeface::Style style = get_style(lf);
296a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        SkFontID fontID = SkTypefaceCache::NewFontID();
297a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return new FontMemResourceTypeface(style, fontID, lf, fontMemResource);
298a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
299a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
300a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comprotected:
301a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    virtual void weak_dispose() const SK_OVERRIDE {
302a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        RemoveFontMemResourceEx(fFontMemResource);
303a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        //SkTypefaceCache::Remove(this);
304a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        INHERITED::weak_dispose();
305a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
306a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
307a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comprivate:
308a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    typedef LogFontTypeface INHERITED;
309a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com};
310a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
311ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic const LOGFONT& get_default_font() {
312ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static LOGFONT gDefaultFont;
313ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return gDefaultFont;
314ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
315ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
31659d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.comstatic bool FindByLogFont(SkTypeface* face, SkTypeface::Style requestedStyle, void* ctx) {
317a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LogFontTypeface* lface = static_cast<LogFontTypeface*>(face);
31859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx);
319ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
320a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return lface &&
321a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com           get_style(lface->fLogFont) == requestedStyle &&
32259d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com           !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT));
32359d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com}
32459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
32559d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com/**
32659d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com *  This guy is public. It first searches the cache, and if a match is not found,
32759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com *  it creates a new face.
32859d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com */
32959d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.comSkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) {
33059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    LOGFONT lf = origLF;
33159d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    make_canonical(&lf);
332ee51d1a6e43615ae640410013c36d2d9873e6aa5bungeman@google.com    SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByLogFont, &lf);
333ee51d1a6e43615ae640410013c36d2d9873e6aa5bungeman@google.com    if (NULL == face) {
33459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com        face = LogFontTypeface::Create(lf);
33590d812b77ab025d5fd01dab904287f5c5bf56228bungeman@google.com        SkTypefaceCache::Add(face, get_style(lf));
33659d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    }
33759d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    return face;
338ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
339ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
340db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com/**
341a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com *  The created SkTypeface takes ownership of fontMemResource.
342a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
343a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comSkTypeface* SkCreateFontMemResourceTypefaceFromLOGFONT(const LOGFONT& origLF, HANDLE fontMemResource) {
344a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LOGFONT lf = origLF;
345a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    make_canonical(&lf);
346a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    FontMemResourceTypeface* face = FontMemResourceTypeface::Create(lf, fontMemResource);
347a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    SkTypefaceCache::Add(face, get_style(lf), false);
348a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return face;
349a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
350a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
351a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
352db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com *  This guy is public
353db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com */
354db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.comvoid SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) {
355db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    if (NULL == face) {
356db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com        *lf = get_default_font();
357db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    } else {
358a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        *lf = static_cast<const LogFontTypeface*>(face)->fLogFont;
359db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com    }
360db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com}
361db77a6a9a9bef7033aca9dffa99d52be1f51f8d9reed@google.com
3626744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// Construct Glyph to Unicode table.
3636744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// Unicode code points that require conjugate pairs in utf16 are not
3646744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// supported.
3656744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// TODO(arthurhsu): Add support for conjugate pairs. It looks like that may
3666744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// require parsing the TTF cmap table (platform 4, encoding 12) directly instead
3676744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org// of calling GetFontUnicodeRange().
3686744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.orgstatic void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount,
3696744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                                      SkTDArray<SkUnichar>* glyphToUnicode) {
3706744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, NULL);
3716744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    if (!glyphSetBufferSize) {
3726744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        return;
3736744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
3746744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
3756744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    SkAutoTDeleteArray<BYTE> glyphSetBuffer(new BYTE[glyphSetBufferSize]);
3766744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    GLYPHSET* glyphSet =
3776744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get());
3786744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) {
3796744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        return;
3806744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
3816744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
3826744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    glyphToUnicode->setCount(glyphCount);
3836744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    memset(glyphToUnicode->begin(), 0, glyphCount * sizeof(SkUnichar));
3846744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    for (DWORD i = 0; i < glyphSet->cRanges; ++i) {
3856744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // There is no guarantee that within a Unicode range, the corresponding
3866744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // glyph id in a font file are continuous. So, even if we have ranges,
3876744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // we can't just use the first and last entry of the range to compute
3886744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // result. We need to enumerate them one by one.
3896744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        int count = glyphSet->ranges[i].cGlyphs;
3906744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        SkAutoTArray<WCHAR> chars(count + 1);
3916744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        chars[count] = 0;  // termintate string
3926744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        SkAutoTArray<WORD> glyph(count);
3936744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        for (USHORT j = 0; j < count; ++j) {
3946744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            chars[j] = glyphSet->ranges[i].wcLow + j;
3956744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        }
3966744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(),
3976744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                         GGI_MARK_NONEXISTING_GLYPHS);
3986744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // If the glyph ID is valid, and the glyph is not mapped, then we will
3996744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // fill in the char id into the vector. If the glyph is mapped already,
4006744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // skip it.
4016744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // TODO(arthurhsu): better improve this. e.g. Get all used char ids from
4026744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // font cache, then generate this mapping table from there. It's
4036744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // unlikely to have collisions since glyph reuse happens mostly for
4046744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        // different Unicode pages.
4056744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        for (USHORT j = 0; j < count; ++j) {
4066744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            if (glyph[j] != 0xffff && glyph[j] < glyphCount &&
4076744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                (*glyphToUnicode)[glyph[j]] == 0) {
4086744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org                (*glyphToUnicode)[glyph[j]] = chars[j];
4096744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org            }
4106744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        }
4116744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
4126744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org}
4136744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
41499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com//////////////////////////////////////////////////////////////////////////////////////
41599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
41699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comstatic int alignTo32(int n) {
41799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    return (n + 31) & ~31;
41899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com}
41999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
42099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comstruct MyBitmapInfo : public BITMAPINFO {
42199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    RGBQUAD fMoreSpaceForColors[1];
42299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com};
42399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
42499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comclass HDCOffscreen {
42599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.compublic:
42699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDCOffscreen() {
42799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fFont = 0;
42899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fDC = 0;
42999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = 0;
43099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBits = NULL;
43199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fWidth = fHeight = 0;
43299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fIsBW = false;
43399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
43499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
43599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    ~HDCOffscreen() {
43699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (fDC) {
43799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            DeleteDC(fDC);
43899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
43999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (fBM) {
44099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            DeleteObject(fBM);
44199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
44299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
44399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
44499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    void init(HFONT font, const XFORM& xform) {
44599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fFont = font;
44699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fXform = xform;
44799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
44899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
44997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    const void* draw(const SkGlyph&, bool isBW, size_t* srcRBPtr);
45099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
45199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.comprivate:
45299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDC     fDC;
45399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HBITMAP fBM;
45499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HFONT   fFont;
45599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    XFORM   fXform;
45699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    void*   fBits;  // points into fBM
45799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int     fWidth;
45899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int     fHeight;
45999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    bool    fIsBW;
46099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com};
46199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
462754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.comconst void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW,
46397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                               size_t* srcRBPtr) {
46484e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // Can we share the scalercontext's fDDC, so we don't need to create
46584e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // a separate fDC here?
46699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (0 == fDC) {
46799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fDC = CreateCompatibleDC(0);
46899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (0 == fDC) {
46999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            return NULL;
47099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
47199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetGraphicsMode(fDC, GM_ADVANCED);
47299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetBkMode(fDC, TRANSPARENT);
47399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SetTextAlign(fDC, TA_LEFT | TA_BASELINE);
47499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SelectObject(fDC, fFont);
47597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com
47697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        COLORREF color = 0x00FFFFFF;
477b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com        SkDEBUGCODE(COLORREF prev =) SetTextColor(fDC, color);
47897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        SkASSERT(prev != CLR_INVALID);
47999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
48099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
48199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (fBM && (fIsBW != isBW || fWidth < glyph.fWidth || fHeight < glyph.fHeight)) {
48299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        DeleteObject(fBM);
48399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = 0;
48499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
485754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    fIsBW = isBW;
48699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
48799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    fWidth = SkMax32(fWidth, glyph.fWidth);
48899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    fHeight = SkMax32(fHeight, glyph.fHeight);
48999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
49099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    int biWidth = isBW ? alignTo32(fWidth) : fWidth;
49199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
49299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    if (0 == fBM) {
49399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        MyBitmapInfo info;
49499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        sk_bzero(&info, sizeof(info));
49599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (isBW) {
49699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            RGBQUAD blackQuad = { 0, 0, 0, 0 };
49799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 };
49899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiColors[0] = blackQuad;
49999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiColors[1] = whiteQuad;
50099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
50199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biSize = sizeof(info.bmiHeader);
50299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biWidth = biWidth;
50399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biHeight = fHeight;
50499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biPlanes = 1;
50599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biBitCount = isBW ? 1 : 32;
50699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        info.bmiHeader.biCompression = BI_RGB;
50799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (isBW) {
50899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            info.bmiHeader.biClrUsed = 2;
50999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
51099edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0);
51199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        if (0 == fBM) {
51299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com            return NULL;
51399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        }
51499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com        SelectObject(fDC, fBM);
51599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    }
51699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
51799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    // erase
51899edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2);
51999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t size = fHeight * srcRB;
52097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    memset(fBits, 0, size);
52199edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
52299edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    XFORM xform = fXform;
52399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    xform.eDx = (float)-glyph.fLeft;
52499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    xform.eDy = (float)-glyph.fTop;
52599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    SetWorldTransform(fDC, &xform);
52699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
52799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    uint16_t glyphID = glyph.getGlyphID();
52839698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    BOOL ret = ExtTextOutW(fDC, 0, 0, ETO_GLYPH_INDEX, NULL, reinterpret_cast<LPCWSTR>(&glyphID), 1, NULL);
52999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    GdiFlush();
53039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (0 == ret) {
53139698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        return NULL;
53239698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
53399edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    *srcRBPtr = srcRB;
53499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    // offset to the start of the image
53599edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    return (const char*)fBits + (fHeight - glyph.fHeight) * srcRB;
53699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com}
53799edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
538b8a5c618d2e18d71707ae4dcafbe5153d7ff427freed@google.com//////////////////////////////////////////////////////////////////////////////
5390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#define BUFFERSIZE (1 << 13)
54059d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com
54130ddd615c447fed73286151b463af20d309c85f1reed@google.comclass SkScalerContext_GDI : public SkScalerContext {
542ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.compublic:
54330ddd615c447fed73286151b463af20d309c85f1reed@google.com    SkScalerContext_GDI(SkTypeface*, const SkDescriptor* desc);
54430ddd615c447fed73286151b463af20d309c85f1reed@google.com    virtual ~SkScalerContext_GDI();
545ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
54684e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // Returns true if the constructor was able to complete all of its
54784e22d847fc84727bc220947162363ee1fe068fcreed@google.com    // initializations (which may include calling GDI).
54884e22d847fc84727bc220947162363ee1fe068fcreed@google.com    bool isValid() const;
54984e22d847fc84727bc220947162363ee1fe068fcreed@google.com
550ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comprotected:
55197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    virtual unsigned generateGlyphCount() SK_OVERRIDE;
55297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE;
55397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE;
55497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE;
555a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com    virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE;
55697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE;
557a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com    virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
558a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                                     SkPaint::FontMetrics* mY) SK_OVERRIDE;
55999edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
560ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comprivate:
5610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    DWORD getGDIGlyphPath(const SkGlyph& glyph, UINT flags,
5620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                          SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf);
5630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
56499edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    HDCOffscreen fOffscreen;
5650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** fGsA is the non-rotational part of total matrix without the text height scale.
5660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     *  Used to find the magnitude of advances.
5670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     */
5680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    MAT2         fGsA;
5696a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** The total matrix without the textSize. */
570ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    MAT2         fMat22;
5716a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** Scales font to EM size. */
5726a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    MAT2         fHighResMat22;
573ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC          fDDC;
574ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT        fSavefont;
575ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT        fFont;
576ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SCRIPT_CACHE fSC;
577ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int          fGlyphCount;
5781dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
5796a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    /** The total matrix which also removes EM scale. */
5801dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    SkMatrix     fHiResMatrix;
5810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** fG_inv is the inverse of the rotational part of the total matrix.
5820abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     *  Used to set the direction of advances.
5830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com     */
5840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix     fG_inv;
585a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    enum Type {
586a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        kTrueType_Type, kBitmap_Type,
587a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    } fType;
588a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    TEXTMETRIC fTM;
589ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com};
590ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
591ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic FIXED float2FIXED(float x) {
592ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return SkFixedToFIXED(SkFloatToFixed(x));
593ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
594ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
59582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.comstatic BYTE compute_quality(const SkScalerContext::Rec& rec) {
59682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    switch (rec.fMaskFormat) {
59782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        case SkMask::kBW_Format:
59882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            return NONANTIALIASED_QUALITY;
59982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        case SkMask::kLCD16_Format:
60082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        case SkMask::kLCD32_Format:
60182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            return CLEARTYPE_QUALITY;
60282a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        default:
6038351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            if (rec.fFlags & SkScalerContext::kGenA8FromLCD_Flag) {
6048351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com                return CLEARTYPE_QUALITY;
6058351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            } else {
6068351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com                return ANTIALIASED_QUALITY;
6078351aabbfe82a76a698fa2bde00d33c1174518cdreed@google.com            }
60882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    }
60982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
61082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
61130ddd615c447fed73286151b463af20d309c85f1reed@google.comSkScalerContext_GDI::SkScalerContext_GDI(SkTypeface* rawTypeface,
6120da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com                                                 const SkDescriptor* desc)
613055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        : SkScalerContext(rawTypeface, desc)
614055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fDDC(0)
615055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fSavefont(0)
61684e22d847fc84727bc220947162363ee1fe068fcreed@google.com        , fFont(0)
617055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        , fSC(0)
61805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        , fGlyphCount(-1)
61905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com{
620055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    LogFontTypeface* typeface = reinterpret_cast<LogFontTypeface*>(rawTypeface);
621055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com
622ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fDDC = ::CreateCompatibleDC(NULL);
62384e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!fDDC) {
62484e22d847fc84727bc220947162363ee1fe068fcreed@google.com        return;
62584e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
6261dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    SetGraphicsMode(fDDC, GM_ADVANCED);
627ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SetBkMode(fDDC, TRANSPARENT);
62827e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com
6290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkPoint h = SkPoint::Make(SK_Scalar1, 0);
6300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // A is the total matrix.
6310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix A;
6320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fRec.getSingleMatrix(&A);
6330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    A.mapPoints(&h, 1);
6340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Find the Given's matrix [[c, -s],[s, c]] which rotates the baseline vector h
6360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // (where the baseline is mapped to) to the positive horizontal axis.
6370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const SkScalar& a = h.fX;
6380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const SkScalar& b = h.fY;
6390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkScalar c, s;
6400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == b) {
6410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        c = SkDoubleToScalar(_copysign(SK_Scalar1, a));
6420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        s = 0;
6430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else if (0 == a) {
6440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        c = 0;
6450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        s = SkDoubleToScalar(-_copysign(SK_Scalar1, b));
6460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else if (SkScalarAbs(b) > SkScalarAbs(a)) {
6470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkScalar t = a / b;
6480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkScalar u = SkDoubleToScalar(_copysign(SkScalarSqrt(SK_Scalar1 + t*t), b));
6490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        s = -1 / u;
6500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        c = -s * t;
6510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else {
6520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkScalar t = b / a;
6530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkScalar u = SkDoubleToScalar(_copysign(SkScalarSqrt(SK_Scalar1 + t*t), a));
6540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        c = 1 / u;
6550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        s = -c * t;
6560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
6570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // G is the Given's Matrix for A (rotational matrix such that GA[0][1] == 0).
6590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix G;
6600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    G.setAll(c, -s, 0,
6610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com             s,  c, 0,
6620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com             0,  0, SkScalarToPersp(SK_Scalar1));
6630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // GA is the matrix A with rotation removed.
6650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix GA(G);
6660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GA.preConcat(A);
6670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
66811ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    // realTextSize is the actual device size we want (as opposed to the size the user requested).
66911ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    // gdiTextSide is the size we request from GDI.
6700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // If the scale is negative, this means the matrix will do the flip anyway.
67111ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    SkScalar realTextSize = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
67211ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    SkScalar gdiTextSize = SkScalarRoundToScalar(realTextSize);
67311ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    if (gdiTextSize == 0) {
67411ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com        gdiTextSize = SK_Scalar1;
6750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
6760abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
67711ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    // When not hinting, remove only the gdiTextSize scale which will be applied by GDI.
67811ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    // When GDI hinting, remove the entire Y scale to prevent 'subpixel' metrics.
67911ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    SkScalar scale = (fRec.getHinting() == SkPaint::kNo_Hinting ||
68011ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                      fRec.getHinting() == SkPaint::kSlight_Hinting)
68111ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                   ? SkScalarInvert(gdiTextSize)
68211ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                   : SkScalarInvert(realTextSize);
68311ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com
6840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // sA is the total matrix A without the textSize (so GDI knows the text size separately).
6850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // When this matrix is used with GetGlyphOutline, no further processing is needed.
6860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix sA(A);
6870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    sA.preScale(scale, scale); //remove text size
6880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // GsA is the non-rotational part of A without the text height scale.
6900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // This is what is used to find the magnitude of advances.
6910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkMatrix GsA(GA);
6920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GsA.preScale(scale, scale); //remove text size, G is rotational so reorders with the scale.
6930abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6940abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM11 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleX));
6950abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM12 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewY)); // This should be ~0.
6960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM21 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewX));
6970abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fGsA.eM22 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleY));
6980abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
6990abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // fG_inv is G inverse, which is fairly simple since G is 2x2 rotational.
7000abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    fG_inv.setAll(G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX),
7010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                  -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY),
7020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                  G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2));
703ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
704055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com    LOGFONT lf = typeface->fLogFont;
70511ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com    lf.lfHeight = -SkScalarTruncToInt(gdiTextSize);
70682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    lf.lfQuality = compute_quality(fRec);
707ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fFont = CreateFontIndirect(&lf);
70884e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!fFont) {
70984e22d847fc84727bc220947162363ee1fe068fcreed@google.com        return;
71084e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
7111dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
712ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    fSavefont = (HFONT)SelectObject(fDDC, fFont);
713a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com
714a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    if (0 == GetTextMetrics(fDDC, &fTM)) {
715055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(lf);
716a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (0 == GetTextMetrics(fDDC, &fTM)) {
717a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            fTM.tmPitchAndFamily = TMPF_TRUETYPE;
718a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
719a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
720a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    // Used a logfont on a memory context, should never get a device font.
72190b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    // Therefore all TMPF_DEVICE will be PostScript fonts.
72290b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com
72390b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    // If TMPF_VECTOR is set, one of TMPF_TRUETYPE or TMPF_DEVICE must be set,
72490b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    // otherwise we have a vector FON, which we don't support.
72590b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    // This was determined by testing with Type1 PFM/PFB and OpenTypeCFF OTF,
72690b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    // as well as looking at Wine bugs and sources.
72790b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    SkASSERT(!(fTM.tmPitchAndFamily & TMPF_VECTOR) ||
72890b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com              (fTM.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_DEVICE)));
72990b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com
730d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    XFORM xform;
73190b7e386cab251db78152f18adfd54e019b8dc10bungeman@google.com    if (fTM.tmPitchAndFamily & TMPF_VECTOR) {
732a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        // Truetype or PostScript.
733a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        // Stroked FON also gets here (TMPF_VECTOR), but we don't handle it.
73430ddd615c447fed73286151b463af20d309c85f1reed@google.com        fType = SkScalerContext_GDI::kTrueType_Type;
735a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
736d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // fPost2x2 is column-major, left handed (y down).
737d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // XFORM 2x2 is row-major, left handed (y down).
7380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM11 = SkScalarToFloat(sA.get(SkMatrix::kMScaleX));
7390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM12 = SkScalarToFloat(sA.get(SkMatrix::kMSkewY));
7400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM21 = SkScalarToFloat(sA.get(SkMatrix::kMSkewX));
7410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        xform.eM22 = SkScalarToFloat(sA.get(SkMatrix::kMScaleY));
742d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDx = 0;
743d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDy = 0;
744d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
745d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // MAT2 is row major, right handed (y up).
746d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM11 = float2FIXED(xform.eM11);
747d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM12 = float2FIXED(-xform.eM12);
748d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM21 = float2FIXED(-xform.eM21);
749d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM22 = float2FIXED(xform.eM22);
750a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
751a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (needToRenderWithSkia(fRec)) {
752a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            this->forceGenerateImageFromPath();
753a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
754a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
75511ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com        // Create a hires matrix if we need linear metrics.
7560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (this->isSubpixel()) {
7570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            OUTLINETEXTMETRIC otm;
7580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            UINT success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
7590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (0 == success) {
7600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                call_ensure_accessible(lf);
7610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
7620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
7630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (0 != success) {
76411ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar upem = SkIntToScalar(otm.otmEMSquare);
7656a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com
76611ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar gdiTextSizeToEMScale = upem / gdiTextSize;
76711ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHighResMat22.eM11 = float2FIXED(gdiTextSizeToEMScale);
7686a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHighResMat22.eM12 = float2FIXED(0);
7696a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHighResMat22.eM21 = float2FIXED(0);
77011ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHighResMat22.eM22 = float2FIXED(gdiTextSizeToEMScale);
7716a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com
77211ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                SkScalar removeEMScale = SkScalarInvert(upem);
7736a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com                fHiResMatrix = A;
77411ba3199f3edc7883ada4fd941d231f8f4883720bungeman@google.com                fHiResMatrix.preScale(removeEMScale, removeEMScale);
7750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
7760abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
7770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
778a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    } else {
779a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        // Assume bitmap
78030ddd615c447fed73286151b463af20d309c85f1reed@google.com        fType = SkScalerContext_GDI::kBitmap_Type;
781a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
782d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM11 = 1.0f;
783d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM12 = 0.0f;
784d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM21 = 0.0f;
785d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eM22 = 1.0f;
786d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDx = 0.0f;
787d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        xform.eDy = 0.0f;
788a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
789d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // fPost2x2 is column-major, left handed (y down).
790d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // MAT2 is row major, right handed (y up).
791a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        fMat22.eM11 = SkScalarToFIXED(fRec.fPost2x2[0][0]);
792d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM12 = SkScalarToFIXED(-fRec.fPost2x2[1][0]);
793a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        fMat22.eM21 = SkScalarToFIXED(-fRec.fPost2x2[0][1]);
794d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        fMat22.eM22 = SkScalarToFIXED(fRec.fPost2x2[1][1]);
795a767fa06ca28be9df1ff6e08a299e0bec839a2dcreed@google.com    }
79699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com
797d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    fOffscreen.init(fFont, xform);
798ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
799ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
80030ddd615c447fed73286151b463af20d309c85f1reed@google.comSkScalerContext_GDI::~SkScalerContext_GDI() {
801ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fDDC) {
802ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::SelectObject(fDDC, fSavefont);
803ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::DeleteDC(fDDC);
804ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
805ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fFont) {
806ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::DeleteObject(fFont);
807ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
808ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fSC) {
809ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ::ScriptFreeCache(&fSC);
810ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
811ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
812ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
81330ddd615c447fed73286151b463af20d309c85f1reed@google.combool SkScalerContext_GDI::isValid() const {
81484e22d847fc84727bc220947162363ee1fe068fcreed@google.com    return fDDC && fFont;
81584e22d847fc84727bc220947162363ee1fe068fcreed@google.com}
81684e22d847fc84727bc220947162363ee1fe068fcreed@google.com
81730ddd615c447fed73286151b463af20d309c85f1reed@google.comunsigned SkScalerContext_GDI::generateGlyphCount() {
818ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (fGlyphCount < 0) {
8197bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com        fGlyphCount = calculateGlyphCount(
8207bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com                          fDDC, static_cast<const LogFontTypeface*>(this->getTypeface())->fLogFont);
821ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
822ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return fGlyphCount;
823ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
824ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
82533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.comuint16_t SkScalerContext_GDI::generateCharToGlyph(SkUnichar utf32) {
826ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint16_t index = 0;
82733346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com    WCHAR utf16[2];
828ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // TODO(ctguil): Support characters that generate more than one glyph.
82933346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com    if (SkUTF16_FromUnichar(utf32, (uint16_t*)utf16) == 1) {
830ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        // Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0.
8317bd141dce43ea3405bc60c9c84e6f910b851b079skia.committer@gmail.com
83233346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        /** Real documentation for GetGlyphIndiciesW:
83333346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *
83433346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  When GGI_MARK_NONEXISTING_GLYPHS is not specified and a character does not map to a
83533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  glyph, then the 'default character's glyph is returned instead. The 'default character'
83633346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  is available in fTM.tmDefaultChar. FON fonts have adefault character, and there exists a
83733346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  usDefaultChar in the 'OS/2' table, version 2 and later. If there is no
83833346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  'default character' specified by the font, then often the first character found is used.
83933346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *
84033346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  When GGI_MARK_NONEXISTING_GLYPHS is specified and a character does not map to a glyph,
84133346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  then the glyph 0xFFFF is used. In Windows XP and earlier, Bitmap/Vector FON usually use
84233346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  glyph 0x1F instead ('Terminal' appears to be special, returning 0xFFFF).
84333346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         *  Type1 PFM/PFB, TT, OT TT, OT CFF all appear to use 0xFFFF, even on XP.
84433346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com         */
84533346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        DWORD result = GetGlyphIndicesW(fDDC, utf16, 1, &index, GGI_MARK_NONEXISTING_GLYPHS);
84633346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        if (result == GDI_ERROR
84733346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com            || 0xFFFF == index
84833346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com            || (0x1F == index && fType == SkScalerContext_GDI::kBitmap_Type /*&& winVer < Vista */))
84933346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        {
85033346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com            index = 0;
85133346487cbb64134d7204abe32d9318c1b511aa8bungeman@google.com        }
852ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
853ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        // Use uniscribe to detemine glyph index for non-BMP characters.
85427f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int numWCHAR = 2;
85527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int maxItems = 2;
85627f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // MSDN states that this can be NULL, but some things don't work then.
85727f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_CONTROL sc = { 0 };
85827f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // Add extra item to SCRIPT_ITEM to work around a bug (now documented).
85927f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // https://bugzilla.mozilla.org/show_bug.cgi?id=366643
86027f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_ITEM si[maxItems + 1];
86127f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        int numItems;
86227f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &sc, NULL, si, &numItems),
86327f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com             "Could not itemize character.");
86427f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com
86527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs.
86627f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        static const int maxGlyphs = 2;
86727f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        SCRIPT_VISATTR vsa[maxGlyphs];
86827f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        WORD outGlyphs[maxGlyphs];
86927f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        WORD logClust[numWCHAR];
87027f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        int numGlyphs;
87127f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        HRZM(ScriptShape(fDDC, &fSC, utf16, numWCHAR, maxGlyphs, &si[0].a,
87227f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com                         outGlyphs, logClust, vsa, &numGlyphs),
87327f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com             "Could not shape character.");
87427f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        if (1 == numGlyphs) {
87527f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com            index = outGlyphs[0];
87627f74aab32123e6f021359061149966fa4ca5a62bungeman@google.com        }
877ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
878ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return index;
879ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
880ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
88130ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateAdvance(SkGlyph* glyph) {
882ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    this->generateMetrics(glyph);
883ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
884ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
88530ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateMetrics(SkGlyph* glyph) {
886ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
887ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
88830ddd615c447fed73286151b463af20d309c85f1reed@google.com    if (fType == SkScalerContext_GDI::kBitmap_Type) {
889a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        SIZE size;
890a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        WORD glyphs = glyph->getGlyphID(0);
891a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (0 == GetTextExtentPointI(fDDC, &glyphs, 1, &size)) {
892a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            glyph->fWidth = SkToS16(fTM.tmMaxCharWidth);
893a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        } else {
894a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            glyph->fWidth = SkToS16(size.cx);
895a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
896a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fHeight = SkToS16(size.cy);
897a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
898a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fTop = SkToS16(-fTM.tmAscent);
899a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fLeft = SkToS16(0);
900a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceX = SkIntToFixed(glyph->fWidth);
901a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceY = 0;
902a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
903d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // Apply matrix to advance.
904a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceY = SkFixedMul(SkFIXEDToFixed(fMat22.eM21), glyph->fAdvanceX);
905a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        glyph->fAdvanceX = SkFixedMul(SkFIXEDToFixed(fMat22.eM11), glyph->fAdvanceX);
906a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
907a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
908a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
909a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
910d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    UINT glyphId = glyph->getGlyphID(0);
911d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
912ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
9131dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com    sk_bzero(&gm, sizeof(gm));
914ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
915d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    DWORD status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
916d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (GDI_ERROR == status) {
917055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
918d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
919d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        if (GDI_ERROR == status) {
920d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->zeroMetrics();
921d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            return;
922d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
92339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
924ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
925d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    bool empty = false;
926d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // The black box is either the embedded bitmap size or the outline extent.
927d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // It is 1x1 if nothing is to be drawn, but will also be 1x1 if something very small
928d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    // is to be drawn, like a '.'. We need to outset '.' but do not wish to outset ' '.
929d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (1 == gm.gmBlackBoxX && 1 == gm.gmBlackBoxY) {
930d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // If GetGlyphOutline with GGO_NATIVE returns 0, we know there was no outline.
931d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        DWORD bufferSize = GetGlyphOutlineW(fDDC, glyphId, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22);
932d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        empty = (0 == bufferSize);
933d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    }
9341dd17a133f4fa5c5a0c752e6b9a6f7af6f329fb8reed@google.com
935d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fTop = SkToS16(-gm.gmptGlyphOrigin.y);
936d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fLeft = SkToS16(gm.gmptGlyphOrigin.x);
937d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    if (empty) {
938d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fWidth = 0;
939d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fHeight = 0;
940ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
941d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // Outset, since the image may bleed out of the black box.
942d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For embedded bitmaps the black box should be exact.
943d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For outlines we need to outset by 1 in all directions for bleed.
944d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        // For ClearType we need to outset by 2 for bleed.
945d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fWidth = gm.gmBlackBoxX + 4;
946d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fHeight = gm.gmBlackBoxY + 4;
947d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fTop -= 2;
948d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        glyph->fLeft -= 2;
949d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    }
950d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fAdvanceX = SkIntToFixed(gm.gmCellIncX);
951d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fAdvanceY = SkIntToFixed(gm.gmCellIncY);
952d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fRsbDelta = 0;
953d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    glyph->fLsbDelta = 0;
954d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
9556a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com    if (this->isSubpixel()) {
956d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        sk_bzero(&gm, sizeof(gm));
9576a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fHighResMat22);
958d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        if (GDI_ERROR != status) {
959d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            SkPoint advance;
960d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            fHiResMatrix.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance);
961d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->fAdvanceX = SkScalarToFixed(advance.fX);
962d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            glyph->fAdvanceY = SkScalarToFixed(advance.fY);
963d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
9640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else if (!isAxisAligned(this->fRec)) {
9650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fGsA);
9660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR != status) {
9670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            SkPoint advance;
9680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fG_inv.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance);
9690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            glyph->fAdvanceX = SkScalarToFixed(advance.fX);
9700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            glyph->fAdvanceY = SkScalarToFixed(advance.fY);
9710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
972ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
973ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
974ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
9756a774a1f266070f304199ee31b8d1f4c0138b6cfbungeman@google.comstatic const MAT2 gMat2Identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
97630ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateFontMetrics(SkPaint::FontMetrics* mx, SkPaint::FontMetrics* my) {
97760af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    if (!(mx || my)) {
978ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com      return;
97960af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    }
98060af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com
98160af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    if (mx) {
98260af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com        sk_bzero(mx, sizeof(*mx));
98360af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    }
98460af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    if (my) {
98560af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com        sk_bzero(my, sizeof(*my));
98660af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com    }
987ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
988ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
989ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
990e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
99130ddd615c447fed73286151b463af20d309c85f1reed@google.com    if (fType == SkScalerContext_GDI::kBitmap_Type) {
992e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#endif
993a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (mx) {
994a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            mx->fTop = SkIntToScalar(-fTM.tmAscent);
995a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            mx->fAscent = SkIntToScalar(-fTM.tmAscent);
9960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            mx->fDescent = SkIntToScalar(fTM.tmDescent);
997a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            mx->fBottom = SkIntToScalar(fTM.tmDescent);
99860af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com            mx->fLeading = SkIntToScalar(fTM.tmExternalLeading);
999a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
1000a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
1001a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        if (my) {
1002a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            my->fTop = SkIntToScalar(-fTM.tmAscent);
1003a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            my->fAscent = SkIntToScalar(-fTM.tmAscent);
10040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            my->fDescent = SkIntToScalar(fTM.tmDescent);
1005a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com            my->fBottom = SkIntToScalar(fTM.tmDescent);
100660af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com            my->fLeading = SkIntToScalar(fTM.tmExternalLeading);
100760af92cb6de814b14ee5d40b71bfbd79227597ecreed@google.com            my->fAvgCharWidth = SkIntToScalar(fTM.tmAveCharWidth);
1008e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com            my->fMaxCharWidth = SkIntToScalar(fTM.tmMaxCharWidth);
1009e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com            my->fXMin = 0;
1010e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com            my->fXMax = my->fMaxCharWidth;
1011e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com            //my->fXHeight = 0;
1012a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        }
1013e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
1014a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
1015a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com    }
1016e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#endif
1017a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com
1018ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    OUTLINETEXTMETRIC otm;
1019ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1020ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
10210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == ret) {
1022055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
102339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm);
102439698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
10250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == ret) {
1026a0319f6d071bb8298e56ace043ea6c623fb358f7bungeman@google.com        return;
1027ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1028ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1029ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (mx) {
10300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        mx->fTop = SkIntToScalar(-otm.otmrcFontBox.left);
10310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        mx->fAscent = SkIntToScalar(-otm.otmAscent);
10320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        mx->fDescent = SkIntToScalar(-otm.otmDescent);
10330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        mx->fBottom = SkIntToScalar(otm.otmrcFontBox.right);
10340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        mx->fLeading = SkIntToScalar(otm.otmLineGap);
1035ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1036ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1037ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (my) {
1038e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS
10390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fTop = SkIntToScalar(-otm.otmrcFontBox.top);
10400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fAscent = SkIntToScalar(-otm.otmAscent);
10410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fDescent = SkIntToScalar(-otm.otmDescent);
10420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fBottom = SkIntToScalar(-otm.otmrcFontBox.bottom);
10430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fLeading = SkIntToScalar(otm.otmLineGap);
10440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fAvgCharWidth = SkIntToScalar(otm.otmTextMetrics.tmAveCharWidth);
10450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fMaxCharWidth = SkIntToScalar(otm.otmTextMetrics.tmMaxCharWidth);
10460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fXMin = SkIntToScalar(otm.otmrcFontBox.left);
10470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fXMax = SkIntToScalar(otm.otmrcFontBox.right);
1048e9d831957602e3fa46fc4b609ae6d2da46654915bungeman@google.com#endif
10490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        my->fXHeight = SkIntToScalar(otm.otmsXHeight);
10500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
10510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GLYPHMETRICS gm;
10520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        sk_bzero(&gm, sizeof(gm));
10530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        DWORD len = GetGlyphOutlineW(fDDC, 'x', GGO_METRICS, &gm, 0, 0, &gMat2Identity);
10540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (len != GDI_ERROR && gm.gmBlackBoxY > 0) {
10550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            my->fXHeight = SkIntToScalar(gm.gmBlackBoxY);
10560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
1057ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1058ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1059ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
10607430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com////////////////////////////////////////////////////////////////////////////////////////
10617430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
10620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#define SK_SHOW_TEXT_BLIT_COVERAGE 0
10630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
10647430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.comstatic void build_power_table(uint8_t table[], float ee) {
10657430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    for (int i = 0; i < 256; i++) {
10667430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        float x = i / 255.f;
106797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        x = sk_float_pow(x, ee);
1068e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com        int xx = SkScalarRoundToInt(x * 255);
10697430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        table[i] = SkToU8(xx);
10707430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    }
10717430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com}
10727430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
107397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com/**
107497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  This will invert the gamma applied by GDI (gray-scale antialiased), so we
107597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  can get linear values.
107697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
107797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI grayscale appears to use a hard-coded gamma of 2.3.
107897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
107997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI grayscale appears to draw using the black and white rasterizer at four
108097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  times the size and then downsamples to compute the coverage mask. As a
108197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  result there are only seventeen total grays. This lack of fidelity means
108297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  that shifting into other color spaces is imprecise.
108397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com */
108497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic const uint8_t* getInverseGammaTableGDI() {
108505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Since build_power_table is idempotent, many threads can build gTableGdi
108605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // simultaneously.
108705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
108805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Microsoft Specific:
108905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Making gInited volatile provides read-aquire and write-release in vc++.
109005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // In VS2012, see compiler option /volatile:(ms|iso).
109105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Replace with C++11 atomics when possible.
109205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    static volatile bool gInited;
109397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    static uint8_t gTableGdi[256];
109405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    if (gInited) {
109505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a L/L (read) barrier (full acquire not needed). If gInited is observed
109605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // true then gTableGdi is observable, but it must be requested.
109705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    } else {
109897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        build_power_table(gTableGdi, 2.3f);
109905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a S/S (write) barrier (full release not needed) here so that this
110005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // write to gInited becomes observable after gTableGdi.
110197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        gInited = true;
110297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    }
110397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return gTableGdi;
110497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com}
110597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com
110697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com/**
110797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  This will invert the gamma applied by GDI ClearType, so we can get linear
110897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  values.
110997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *
111097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  GDI ClearType uses SPI_GETFONTSMOOTHINGCONTRAST / 1000 as the gamma value.
111197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com *  If this value is not specified, the default is a gamma of 1.4.
111297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com */
111397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic const uint8_t* getInverseGammaTableClearType() {
111405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // We don't expect SPI_GETFONTSMOOTHINGCONTRAST to ever change, so building
111505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // gTableClearType with build_power_table is effectively idempotent.
111605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
111705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Microsoft Specific:
111805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Making gInited volatile provides read-aquire and write-release in vc++.
111905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // In VS2012, see compiler option /volatile:(ms|iso).
112005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    // Replace with C++11 atomics when possible.
112105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    static volatile bool gInited;
112297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    static uint8_t gTableClearType[256];
112305a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    if (gInited) {
112405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a L/L (read) barrier (acquire not needed). If gInited is observed
112505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // true then gTableClearType is observable, but it must be requested.
112605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com    } else {
11277430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        UINT level = 0;
11287430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &level, 0) || !level) {
11297430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com            // can't get the data, so use a default
11307430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com            level = 1400;
11317430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        }
113297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        build_power_table(gTableClearType, level / 1000.0f);
113305a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // Need a S/S (write) barrier (release not needed) here so that this
113405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        // write to gInited becomes observable after gTableClearType.
11357430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com        gInited = true;
11367430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com    }
113797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return gTableClearType;
11387430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com}
11397430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com
1140ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com#include "SkColorPriv.h"
1141ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
11426385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com//Cannot assume that the input rgb is gray due to possible setting of kGenA8FromLCD_Flag.
114397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
114497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic inline uint8_t rgb_to_a8(SkGdiRGB rgb, const uint8_t* table8) {
11456385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU r = (rgb >> 16) & 0xFF;
11466385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU g = (rgb >>  8) & 0xFF;
11476385314686875ecb484d95b33fcbf08890b5144dbungeman@google.com    U8CPU b = (rgb >>  0) & 0xFF;
11481bfe01d06b896eca94c80d9af9a6bb30216b5e42bungeman@google.com    return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8);
114982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com}
115082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
115197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
115297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic inline uint16_t rgb_to_lcd16(SkGdiRGB rgb, const uint8_t* tableR,
115397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                  const uint8_t* tableG,
115497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                  const uint8_t* tableB) {
115597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR);
115697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  8) & 0xFF, tableG);
115797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  0) & 0xFF, tableB);
11580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
11590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
11600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
116197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return SkPack888ToRGB16(r, g, b);
1162ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1163ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
116497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
116597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic inline SkPMColor rgb_to_lcd32(SkGdiRGB rgb, const uint8_t* tableR,
116697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                   const uint8_t* tableG,
116797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                                                   const uint8_t* tableB) {
116897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR);
116997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  8) & 0xFF, tableG);
117097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >>  0) & 0xFF, tableB);
11710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
11720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
11730abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
117497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    return SkPackARGB32(0xFF, r, g, b);
1175754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com}
1176754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com
117782cff020b7e055f2d01686f060a884df842306edreed@google.com// Is this GDI color neither black nor white? If so, we have to keep this
117882cff020b7e055f2d01686f060a884df842306edreed@google.com// image as is, rather than smashing it down to a BW mask.
117982cff020b7e055f2d01686f060a884df842306edreed@google.com//
118082cff020b7e055f2d01686f060a884df842306edreed@google.com// returns int instead of bool, since we don't want/have to pay to convert
118182cff020b7e055f2d01686f060a884df842306edreed@google.com// the zero/non-zero value into a bool
118282cff020b7e055f2d01686f060a884df842306edreed@google.comstatic int is_not_black_or_white(SkGdiRGB c) {
118382cff020b7e055f2d01686f060a884df842306edreed@google.com    // same as (but faster than)
118482cff020b7e055f2d01686f060a884df842306edreed@google.com    //      c &= 0x00FFFFFF;
118582cff020b7e055f2d01686f060a884df842306edreed@google.com    //      return 0 == c || 0x00FFFFFF == c;
118682cff020b7e055f2d01686f060a884df842306edreed@google.com    return (c + (c & 1)) & 0x00FFFFFF;
11875e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
11885e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
11894b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.comstatic bool is_rgb_really_bw(const SkGdiRGB* src, int width, int height, size_t srcRB) {
11905e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < height; ++y) {
11915e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int x = 0; x < width; ++x) {
119282cff020b7e055f2d01686f060a884df842306edreed@google.com            if (is_not_black_or_white(src[x])) {
11935e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                return false;
11945e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
11955e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
119605a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
11975e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
11985e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    return true;
11995e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12005e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
120197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com// gdi's bitmap is upside-down, so we reverse dst walking in Y
120297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com// whenever we copy it into skia's buffer
12035e2df64215f12660ab26b96816f40ad8e32bf16freed@google.comstatic void rgb_to_bw(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
120497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                      const SkGlyph& glyph) {
12055e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
12065e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = (width + 7) >> 3;
12075e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
12085e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12095e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    int byteCount = width >> 3;
12105e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    int bitCount = width & 7;
12115e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12125e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    // adjust srcRB to skip the values in our byteCount loop,
12135e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    // since we increment src locally there
12145e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    srcRB -= byteCount * 8 * sizeof(SkGdiRGB);
12155e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12165e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; ++y) {
12175e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (byteCount > 0) {
12185e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            for (int i = 0; i < byteCount; ++i) {
12197a230142e01c5a8cf955be12a5f287589b551634reed@google.com                unsigned byte = 0;
122097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[0] & (1 << 7);
122197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[1] & (1 << 6);
122297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[2] & (1 << 5);
122397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[3] & (1 << 4);
122497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[4] & (1 << 3);
122597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[5] & (1 << 2);
122697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[6] & (1 << 1);
122797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[7] & (1 << 0);
12286a8f14d9633b330fb8051581828a0aaf4403d1a9reed@google.com                dst[i] = byte;
12295e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                src += 8;
12305e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
12315e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
12325e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (bitCount > 0) {
12335e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            unsigned byte = 0;
12345e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            unsigned mask = 0x80;
12355e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            for (int i = 0; i < bitCount; i++) {
123697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                byte |= src[i] & mask;
12375e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com                mask >>= 1;
12385e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            }
12395e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            dst[byteCount] = byte;
12405e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
124105a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
12425e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst -= dstRB;
12435e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
12440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
12450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (glyph.fWidth > 0 && glyph.fHeight > 0) {
12460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        uint8_t* first = (uint8_t*)glyph.fImage;
12470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1);
12480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        *first |= 1 << 7;
12490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount);
12500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
12510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
12525e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12535e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
125497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
12555e2df64215f12660ab26b96816f40ad8e32bf16freed@google.comstatic void rgb_to_a8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB,
125697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                      const SkGlyph& glyph, const uint8_t* table8) {
12575e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = glyph.rowBytes();
12585e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
12595e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
12605e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12615e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; y++) {
12625e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int i = 0; i < width; i++) {
126397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            dst[i] = rgb_to_a8<APPLY_PREBLEND>(src[i], table8);
12640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
12650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            dst[i] = SkMax32(dst[i], 10);
12660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
12675e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
126805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
12695e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst -= dstRB;
12705e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
12715e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12725e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
127397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
127497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic void rgb_to_lcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph,
127597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                         const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
12765e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const size_t dstRB = glyph.rowBytes();
12775e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    const int width = glyph.fWidth;
12785e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
12795e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
12805e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    for (int y = 0; y < glyph.fHeight; y++) {
12815e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        for (int i = 0; i < width; i++) {
128297efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(src[i], tableR, tableG, tableB);
12835e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        }
128405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
12855e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        dst = (uint16_t*)((char*)dst - dstRB);
12865e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com    }
12875e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com}
12885e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com
128997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comtemplate<bool APPLY_PREBLEND>
129097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.comstatic void rgb_to_lcd32(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph,
129197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                         const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) {
1292754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    const size_t dstRB = glyph.rowBytes();
1293754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    const int width = glyph.fWidth;
129497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    uint32_t* SK_RESTRICT dst = (uint32_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
1295754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com
1296754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    for (int y = 0; y < glyph.fHeight; y++) {
1297754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com        for (int i = 0; i < width; i++) {
129897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            dst[i] = rgb_to_lcd32<APPLY_PREBLEND>(src[i], tableR, tableG, tableB);
1299754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com        }
130005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        src = SkTAddOffset<const SkGdiRGB>(src, srcRB);
130197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        dst = (uint32_t*)((char*)dst - dstRB);
1302754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    }
1303754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com}
1304754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com
13056f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.comstatic inline unsigned clamp255(unsigned x) {
13066f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    SkASSERT(x <= 256);
13076f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    return x - (x >> 8);
13086f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com}
13096f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
131030ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generateImage(const SkGlyph& glyph) {
1311ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(fDDC);
1312ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
13136271117d826098c1399cf7442f8eea2d665cb78areed@google.com    const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat;
131482a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    const bool isAA = !isLCD(fRec);
131582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
131699edd43813b7f1a8f02146cbd8c783d3c82be4abreed@google.com    size_t srcRB;
131797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    const void* bits = fOffscreen.draw(glyph, isBW, &srcRB);
131839698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (NULL == bits) {
1319055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        LogFontTypeface::EnsureAccessible(this->getTypeface());
132097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        bits = fOffscreen.draw(glyph, isBW, &srcRB);
132139698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        if (NULL == bits) {
132239698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            sk_bzero(glyph.fImage, glyph.computeImageSize());
132339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            return;
132439698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        }
132582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    }
132682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com
132797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com    if (!isBW) {
13281bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        const uint8_t* table;
13291bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        //The offscreen contains a GDI blit if isAA and kGenA8FromLCD_Flag is not set.
13301bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        //Otherwise the offscreen contains a ClearType blit.
13311bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        if (isAA && !(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag)) {
13321bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com            table = getInverseGammaTableGDI();
13331bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com        } else {
13341bd2d67702a5c14011f02e23b3724965f82c4780bungeman@google.com            table = getInverseGammaTableClearType();
133597efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        }
133697efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //Note that the following cannot really be integrated into the
133797efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //pre-blend, since we may not be applying the pre-blend; when we aren't
133897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //applying the pre-blend it means that a filter wants linear anyway.
133997efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //Other code may also be applying the pre-blend, so we'd need another
134097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        //one with this and one without.
13416f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        SkGdiRGB* addr = (SkGdiRGB*)bits;
13426f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        for (int y = 0; y < glyph.fHeight; ++y) {
13436f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com            for (int x = 0; x < glyph.fWidth; ++x) {
13446f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int r = (addr[x] >> 16) & 0xFF;
13456f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int g = (addr[x] >>  8) & 0xFF;
13466f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com                int b = (addr[x] >>  0) & 0xFF;
13477430a3376c0f4e0c2809fedecd12765034f9e3a8reed@google.com                addr[x] = (table[r] << 16) | (table[g] << 8) | table[b];
13486f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com            }
134905a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            addr = SkTAddOffset<SkGdiRGB>(addr, srcRB);
13506f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        }
13516f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com    }
13526f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com
135382a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    int width = glyph.fWidth;
135482a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    size_t dstRB = glyph.rowBytes();
135582a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    if (isBW) {
135682a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        const uint8_t* src = (const uint8_t*)bits;
135782a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        uint8_t* dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB);
135882a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com        for (int y = 0; y < glyph.fHeight; y++) {
135982a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            memcpy(dst, src, dstRB);
136082a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            src += srcRB;
136182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com            dst -= dstRB;
1362ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
13630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#if SK_SHOW_TEXT_BLIT_COVERAGE
13640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (glyph.fWidth > 0 && glyph.fHeight > 0) {
13650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                int bitCount = width & 7;
13660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                uint8_t* first = (uint8_t*)glyph.fImage;
13670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1);
13680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                *first |= 1 << 7;
13690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount);
13700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
13710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com#endif
137282a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    } else if (isAA) {
13736f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        // since the caller may require A8 for maskfilters, we can't check for BW
13746f5df48c1d07295b75c1955a69142fb5e66b8647reed@google.com        // ... until we have the caller tell us that explicitly
13755e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        const SkGdiRGB* src = (const SkGdiRGB*)bits;
1376a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com        if (fPreBlend.isApplicable()) {
1377a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com            rgb_to_a8<true>(src, srcRB, glyph, fPreBlend.fG);
137897efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        } else {
1379a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com            rgb_to_a8<false>(src, srcRB, glyph, fPreBlend.fG);
138097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com        }
138182a34d83f646d069ef2d1d7b649d0a0b1829d62freed@google.com    } else {    // LCD16
13825e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        const SkGdiRGB* src = (const SkGdiRGB*)bits;
13835e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) {
138497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com            rgb_to_bw(src, srcRB, glyph);
13855e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com            ((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format;
13865e2df64215f12660ab26b96816f40ad8e32bf16freed@google.com        } else {
1387754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com            if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1388a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                if (fPreBlend.isApplicable()) {
1389a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                    rgb_to_lcd16<true>(src, srcRB, glyph,
1390a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                                       fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
139197efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                } else {
1392a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                    rgb_to_lcd16<false>(src, srcRB, glyph,
1393a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                                        fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
139497efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                }
1395754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com            } else {
1396754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com                SkASSERT(SkMask::kLCD32_Format == glyph.fMaskFormat);
1397a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                if (fPreBlend.isApplicable()) {
1398a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                    rgb_to_lcd32<true>(src, srcRB, glyph,
1399a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                                       fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
140097efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                } else {
1401a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                    rgb_to_lcd32<false>(src, srcRB, glyph,
1402a76de72a6036da0a6b051b14411b80941971f881bungeman@google.com                                        fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
140397efada074e4806479f1350ab1508939c2fdcb53bungeman@google.com                }
1404754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com            }
1405ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1406ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1407ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1408ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
14090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comclass GDIGlyphbufferPointIter {
14100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.compublic:
141127e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com    GDIGlyphbufferPointIter(const uint8_t* glyphbuf, DWORD total_size)
14120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        : fHeaderIter(glyphbuf, total_size), fCurveIter(), fPointIter()
14130abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    { }
14140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1415dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    POINTFX const * next() {
14160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comnextHeader:
14170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (!fCurveIter.isSet()) {
14180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYGONHEADER* header = fHeaderIter.next();
1419dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            if (NULL == header) {
1420dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                return NULL;
1421dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            }
14220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurveIter.set(header);
14230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* curve = fCurveIter.next();
1424dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            if (NULL == curve) {
1425dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                return NULL;
1426dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            }
14270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fPointIter.set(curve);
1428dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            return &header->pfxStart;
14290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
1430ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
14310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* nextPoint = fPointIter.next();
14320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (NULL == nextPoint) {
14330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* curve = fCurveIter.next();
14340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (NULL == curve) {
14350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                fCurveIter.set();
14360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                goto nextHeader;
14370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            } else {
14380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                fPointIter.set(curve);
14390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
14400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            nextPoint = fPointIter.next();
14410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
1442dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        return nextPoint;
14430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
1444ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
14450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    WORD currentCurveType() {
14460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        return fPointIter.fCurveType;
14470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
144805a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
14490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comprivate:
14500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon headers in a glyphbuf. */
14510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonHeaderIter {
14520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
145327e21fe577211c1c40cbf40d0385b02c69d04522skia.committer@gmail.com        GDIPolygonHeaderIter(const uint8_t* glyphbuf, DWORD total_size)
14540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurPolygon(reinterpret_cast<const TTPOLYGONHEADER*>(glyphbuf))
14550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndPolygon(SkTAddOffset<const TTPOLYGONHEADER>(glyphbuf, total_size))
14560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
14570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* next() {
14590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurPolygon >= fEndPolygon) {
14600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
14610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
14620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYGONHEADER* thisPolygon = fCurPolygon;
14630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPolygon = SkTAddOffset<const TTPOLYGONHEADER>(fCurPolygon, fCurPolygon->cb);
14640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisPolygon;
14650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
14670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* fCurPolygon;
14680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* fEndPolygon;
14690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
147005a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
14710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon curves in a polygon header. */
14720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonCurveIter {
14730abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
14740abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurveIter() : fCurCurve(NULL), fEndCurve(NULL) { }
14750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14760abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurveIter(const TTPOLYGONHEADER* curPolygon)
14770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER)))
14780abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb))
14790abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
14800abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        bool isSet() { return fCurCurve != NULL; }
14820abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set(const TTPOLYGONHEADER* curPolygon) {
14840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER));
14850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb);
14860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set() {
14880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = NULL;
14890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndCurve = NULL;
14900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
14910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
14920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* next() {
14930abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurCurve >= fEndCurve) {
14940abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
149505a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            }
14960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* thisCurve = fCurCurve;
14970abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurCurve = SkTAddOffset<const TTPOLYCURVE>(fCurCurve, size_of_TTPOLYCURVE(*fCurCurve));
14980abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisCurve;
14990abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
15000abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
15010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        size_t size_of_TTPOLYCURVE(const TTPOLYCURVE& curve) {
15020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return 2*sizeof(WORD) + curve.cpfx*sizeof(POINTFX);
150305a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com        }
15040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* fCurCurve;
15050abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYCURVE* fEndCurve;
15060abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
150705a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
15080abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    /** Iterates over all of the polygon points in a polygon curve. */
15090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    class GDIPolygonCurvePointIter {
15100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    public:
15110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurvePointIter() : fCurveType(0), fCurPoint(NULL), fEndPoint(NULL) { }
151205a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com
15130abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        GDIPolygonCurvePointIter(const TTPOLYCURVE* curPolygon)
15140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            : fCurveType(curPolygon->wType)
15150abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fCurPoint(&curPolygon->apfx[0])
15160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            , fEndPoint(&curPolygon->apfx[curPolygon->cpfx])
15170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        { }
15180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15190abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        bool isSet() { return fCurPoint != NULL; }
15200abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set(const TTPOLYCURVE* curPolygon) {
15220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurveType = curPolygon->wType;
15230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPoint = &curPolygon->apfx[0];
15240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndPoint = &curPolygon->apfx[curPolygon->cpfx];
15250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
15260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        void set() {
15270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fCurPoint = NULL;
15280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            fEndPoint = NULL;
15290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
15300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* next() {
15320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (fCurPoint >= fEndPoint) {
15330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return NULL;
153405a729ff90f75ac013873742ee0e4ae7e5e6e415bungeman@google.com            }
15350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const POINTFX* thisPoint = fCurPoint;
15360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            ++fCurPoint;
15370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return thisPoint;
1538d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        }
1539ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
15400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        WORD fCurveType;
15410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    private:
15420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* fCurPoint;
15430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const POINTFX* fEndPoint;
15440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    };
15450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonHeaderIter fHeaderIter;
15470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonCurveIter fCurveIter;
15480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GDIPolygonCurvePointIter fPointIter;
15490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com};
15500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
15510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.comstatic void sk_path_from_gdi_path(SkPath* path, const uint8_t* glyphbuf, DWORD total_size) {
1552d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    const uint8_t* cur_glyph = glyphbuf;
1553d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    const uint8_t* end_glyph = glyphbuf + total_size;
1554ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1555d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com    while (cur_glyph < end_glyph) {
1556d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph;
1557ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1558d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const uint8_t* end_poly = cur_glyph + th->cb;
1559d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER);
1560ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1561d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)),
1562d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                     SkFixedToScalar(-SkFIXEDToFixed(th->pfxStart.y)));
1563ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1564d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        while (cur_poly < end_poly) {
1565d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly;
1566ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1567d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            if (pc->wType == TT_PRIM_LINE) {
1568d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                for (uint16_t i = 0; i < pc->cpfx; i++) {
1569d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)),
1570d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pc->apfx[i].y)));
1571ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                }
1572d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            }
1573ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1574d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            if (pc->wType == TT_PRIM_QSPLINE) {
1575d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline
1576d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    POINTFX pnt_b = pc->apfx[u];    // B is always the current point
1577d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    POINTFX pnt_c = pc->apfx[u+1];
1578ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1579d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    if (u < pc->cpfx - 2) {          // If not on last spline, compute C
1580d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                        pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x),
1581d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                                            SkFIXEDToFixed(pnt_c.x)));
1582d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                        pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y),
1583d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                                            SkFIXEDToFixed(pnt_c.y)));
1584ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                    }
1585d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com
1586d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                    path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)),
1587d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)),
1588d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)),
1589d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y)));
1590ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                }
1591ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            }
1592d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            // Advance past this TTPOLYCURVE.
1593d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com            cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx;
1594ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1595d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        cur_glyph += th->cb;
1596d1c7f71ee371738a8c7896a4f06d28b32e3c713ebungeman@google.com        path->close();
1597ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1598ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1599ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1600dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com#define move_next_expected_hinted_point(iter, pElem) do {\
1601dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    pElem = iter.next(); \
1602dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    if (NULL == pElem) return false; \
1603dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com} while(0)
1604dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com
1605dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// It is possible for the hinted and unhinted versions of the same path to have
1606dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// a different number of points due to GDI's handling of flipped points.
1607dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com// If this is detected, this will return false.
1608dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.comstatic bool sk_path_from_gdi_paths(SkPath* path, const uint8_t* glyphbuf, DWORD total_size,
16090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                   GDIGlyphbufferPointIter hintedYs) {
16100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const uint8_t* cur_glyph = glyphbuf;
16110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    const uint8_t* end_glyph = glyphbuf + total_size;
16120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1613dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    POINTFX const * hintedPoint;
1614dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com
16150abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    while (cur_glyph < end_glyph) {
16160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph;
16170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const uint8_t* end_poly = cur_glyph + th->cb;
16190abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER);
16200abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1621dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        move_next_expected_hinted_point(hintedYs, hintedPoint);
16220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)),
1623dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                     SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y)));
16240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        while (cur_poly < end_poly) {
16260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly;
16270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (pc->wType == TT_PRIM_LINE) {
16290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                for (uint16_t i = 0; i < pc->cpfx; i++) {
1630dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    move_next_expected_hinted_point(hintedYs, hintedPoint);
16310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)),
1632dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y)));
16330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
16340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
16350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (pc->wType == TT_PRIM_QSPLINE) {
16370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                POINTFX currentPoint = pc->apfx[0];
1638dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                move_next_expected_hinted_point(hintedYs, hintedPoint);
16390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                // only take the hinted y if it wasn't flipped
16400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) {
1641dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    currentPoint.y = hintedPoint->y;
16420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
16430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline
16440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    POINTFX pnt_b = currentPoint;//pc->apfx[u]; // B is always the current point
16450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    POINTFX pnt_c = pc->apfx[u+1];
1646dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                    move_next_expected_hinted_point(hintedYs, hintedPoint);
16470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    // only take the hinted y if it wasn't flipped
16480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) {
1649dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                        pnt_c.y = hintedPoint->y;
16500abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    }
16510abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    currentPoint.x = pnt_c.x;
16520abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    currentPoint.y = pnt_c.y;
16530abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16540abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    if (u < pc->cpfx - 2) {          // If not on last spline, compute C
16550abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                        pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x),
16560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                                            SkFIXEDToFixed(pnt_c.x)));
16570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                        pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y),
16580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                                            SkFIXEDToFixed(pnt_c.y)));
16590abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    }
16600abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16610abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                    path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)),
16620abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)),
16630abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)),
16640abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                 SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y)));
16650abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                }
16660abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
16670abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // Advance past this TTPOLYCURVE.
16680abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx;
16690abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
16700abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        cur_glyph += th->cb;
16710abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        path->close();
16720abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
1673dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com    return true;
16740abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
16750abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
167630ddd615c447fed73286151b463af20d309c85f1reed@google.comDWORD SkScalerContext_GDI::getGDIGlyphPath(const SkGlyph& glyph, UINT flags,
16770abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                                               SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf)
16780abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com{
16790abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    GLYPHMETRICS gm;
16800abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16810abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    DWORD total_size = GetGlyphOutlineW(fDDC, glyph.fID, flags, &gm, BUFFERSIZE, glyphbuf->get(), &fMat22);
16820abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Sometimes GetGlyphOutlineW returns a number larger than BUFFERSIZE even if BUFFERSIZE > 0.
16830abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // It has been verified that this does not involve a buffer overrun.
16840abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (GDI_ERROR == total_size || total_size > BUFFERSIZE) {
16850abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // GDI_ERROR because the BUFFERSIZE was too small, or because the data was not accessible.
16860abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // When the data is not accessable GetGlyphOutlineW fails rather quickly,
16870abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        // so just try to get the size. If that fails then ensure the data is accessible.
16880abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        total_size = GetGlyphOutlineW(fDDC, glyph.fID, flags, &gm, 0, NULL, &fMat22);
16890abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR == total_size) {
16900abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            LogFontTypeface::EnsureAccessible(this->getTypeface());
16910abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            total_size = GetGlyphOutlineW(fDDC, glyph.fID, flags, &gm, 0, NULL, &fMat22);
16920abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (GDI_ERROR == total_size) {
16930abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                SkASSERT(false);
16940abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return 0;
16950abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
16960abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
16970abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
16980abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        glyphbuf->reset(total_size);
16990abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17000abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        DWORD ret = GetGlyphOutlineW(fDDC, glyph.fID, flags, &gm, total_size, glyphbuf->get(), &fMat22);
17010abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (GDI_ERROR == ret) {
17020abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            LogFontTypeface::EnsureAccessible(this->getTypeface());
17030abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            ret = GetGlyphOutlineW(fDDC, glyph.fID, flags, &gm, total_size, glyphbuf->get(), &fMat22);
17040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            if (GDI_ERROR == ret) {
17050abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                SkASSERT(false);
17060abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                return 0;
17070abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            }
17080abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
17090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
17100abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    return total_size;
17110abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
17120abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
171330ddd615c447fed73286151b463af20d309c85f1reed@google.comvoid SkScalerContext_GDI::generatePath(const SkGlyph& glyph, SkPath* path) {
17140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkASSERT(&glyph && path);
17150abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkASSERT(fDDC);
17160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17170abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    path->reset();
17180abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17190abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Out of all the fonts on a typical Windows box,
17200abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 25% of glyphs require more than 2KB.
17210abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 1% of glyphs require more than 4KB.
17220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 0.01% of glyphs require more than 8KB.
17230abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // 8KB is less than 1% of the normal 1MB stack on Windows.
17240abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    // Note that some web fonts glyphs require more than 20KB.
17250abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //static const DWORD BUFFERSIZE = (1 << 13);
17260abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17270abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //GDI only uses hinted outlines when axis aligned.
17280abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    UINT format = GGO_NATIVE | GGO_GLYPH_INDEX;
17290abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (fRec.getHinting() == SkPaint::kNo_Hinting || fRec.getHinting() == SkPaint::kSlight_Hinting){
17300abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        format |= GGO_UNHINTED;
17310abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
17320abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    SkAutoSTMalloc<BUFFERSIZE, uint8_t> glyphbuf(BUFFERSIZE);
17330abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    DWORD total_size = getGDIGlyphPath(glyph, format, &glyphbuf);
17340abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (0 == total_size) {
17350abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        return;
17360abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
17370abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17380abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    if (fRec.getHinting() != SkPaint::kSlight_Hinting) {
17390abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        sk_path_from_gdi_path(path, glyphbuf, total_size);
17400abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    } else {
17410abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        //GDI only uses hinted outlines when axis aligned.
17420abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        UINT format = GGO_NATIVE | GGO_GLYPH_INDEX;
17430abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
17440abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        SkAutoSTMalloc<BUFFERSIZE, uint8_t> hintedGlyphbuf(BUFFERSIZE);
17450abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        DWORD hinted_total_size = getGDIGlyphPath(glyph, format, &hintedGlyphbuf);
17460abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        if (0 == hinted_total_size) {
17470abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            return;
17480abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com        }
17490abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1750dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        if (!sk_path_from_gdi_paths(path, glyphbuf, total_size,
1751dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com                                    GDIGlyphbufferPointIter(hintedGlyphbuf, hinted_total_size)))
1752dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        {
1753dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            path->reset();
1754dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com            sk_path_from_gdi_path(path, glyphbuf, total_size);
1755dd88eccb22bd2cafcf0c0facf674a8aaaacca7febungeman@google.com        }
17560abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    }
17570abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com}
17580abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com
1759484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comstatic void logfont_for_name(const char* familyName, LOGFONT* lf) {
1760484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    sk_bzero(lf, sizeof(LOGFONT));
1761e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#ifdef UNICODE
1762484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Get the buffer size needed first.
1763484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName,
1764484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                                            -1, NULL, 0);
1765484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Allocate a buffer (str_len already has terminating null
1766484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // accounted for).
1767484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    wchar_t *wideFamilyName = new wchar_t[str_len];
1768484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    // Now actually convert the string.
1769484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::MultiByteToWideChar(CP_UTF8, 0, familyName, -1,
1770484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                            wideFamilyName, str_len);
1771484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::wcsncpy(lf->lfFaceName, wideFamilyName, LF_FACESIZE - 1);
1772484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    delete [] wideFamilyName;
1773484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    lf->lfFaceName[LF_FACESIZE-1] = L'\0';
1774e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#else
1775484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    ::strncpy(lf->lfFaceName, familyName, LF_FACESIZE - 1);
1776484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    lf->lfFaceName[LF_FACESIZE - 1] = '\0';
1777e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com#endif
1778e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com}
1779e70f798ebca1a66f0b568fa46065ebbad9a13b2fbungeman@google.com
17805526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.comvoid LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
17815526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com                                          bool* isLocalStream) const {
17827103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    // Get the actual name of the typeface. The logfont may not know this.
17835526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    HFONT font = CreateFontIndirect(&fLogFont);
17847103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
17857103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    HDC deviceContext = ::CreateCompatibleDC(NULL);
17867103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    HFONT savefont = (HFONT)SelectObject(deviceContext, font);
17877103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
1788a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkString familyName;
1789a980269c2498836101146adc729ef780fb89824ebungeman@google.com    dcfontname_to_skstring(deviceContext, fLogFont, &familyName);
17907103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
17917103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    if (deviceContext) {
17927103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::SelectObject(deviceContext, savefont);
17937103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::DeleteDC(deviceContext);
17947103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    }
17957103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    if (font) {
17967103f182ce61280eacb35f1832df350b642a5381bungeman@google.com        ::DeleteObject(font);
17977103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    }
17987103f182ce61280eacb35f1832df350b642a5381bungeman@google.com
17995526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    desc->setFamilyName(familyName.c_str());
18005526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    *isLocalStream = this->fSerializeAsStream;
1801ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1802ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1803ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comstatic bool getWidthAdvance(HDC hdc, int gId, int16_t* advance) {
1804ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Initialize the MAT2 structure to the identify transformation matrix.
1805ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    static const MAT2 mat2 = {SkScalarToFIXED(1), SkScalarToFIXED(0),
1806ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                        SkScalarToFIXED(0), SkScalarToFIXED(1)};
1807ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    int flags = GGO_METRICS | GGO_GLYPH_INDEX;
1808ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    GLYPHMETRICS gm;
1809ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (GDI_ERROR == GetGlyphOutline(hdc, gId, flags, &gm, 0, NULL, &mat2)) {
1810ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        return false;
1811ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1812ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkASSERT(advance);
1813ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    *advance = gm.gmCellIncX;
1814ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return true;
1815ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1816ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
18172689f615e364dc48ad73826564f5b13d2329179dreed@google.comSkAdvancedTypefaceMetrics* LogFontTypeface::onGetAdvancedTypefaceMetrics(
181837ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org        SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
181937ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org        const uint32_t* glyphIDs,
18202689f615e364dc48ad73826564f5b13d2329179dreed@google.com        uint32_t glyphIDsCount) const {
18212689f615e364dc48ad73826564f5b13d2329179dreed@google.com    LOGFONT lf = fLogFont;
1822ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SkAdvancedTypefaceMetrics* info = NULL;
1823ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1824ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC hdc = CreateCompatibleDC(NULL);
1825ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT font = CreateFontIndirect(&lf);
1826ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
1827ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT designFont = NULL;
1828ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
182905b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    const char stem_chars[] = {'i', 'I', '!', '1'};
183005b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    int16_t min_width;
183105b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    unsigned glyphCount;
183205b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com
1833ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // To request design units, create a logical font whose height is specified
1834ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // as unitsPerEm.
1835ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    OUTLINETEXTMETRIC otm;
183639698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
183739698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (0 == otmRet) {
1838055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com        call_ensure_accessible(lf);
183939698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm);
184039698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    }
184139698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com    if (!otmRet || !GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) {
1842ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        goto Error;
1843ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1844ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    lf.lfHeight = -SkToS32(otm.otmEMSquare);
1845ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    designFont = CreateFontIndirect(&lf);
1846ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, designFont);
1847ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) {
1848ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        goto Error;
1849ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
18507bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    glyphCount = calculateGlyphCount(hdc, fLogFont);
1851ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1852ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info = new SkAdvancedTypefaceMetrics;
1853ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fEmSize = otm.otmEMSquare;
18545f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    info->fMultiMaster = false;
1855ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fLastGlyphID = SkToU16(glyphCount - 1);
1856ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fStyle = 0;
18577103f182ce61280eacb35f1832df350b642a5381bungeman@google.com    tchar_to_skstring(lf.lfFaceName, &info->fFontName);
1858ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
18596744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) {
18606744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org        populate_glyph_to_unicode(hdc, glyphCount, &(info->fGlyphToUnicode));
18616744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org    }
18626744d498fcbbbcf503ec80c4d43dd8f118a88597vandebo@chromium.org
1863d41e70de196e78e21e1001d23a7259785cbd0835vandebo@chromium.org    if (glyphCount > 0 &&
1864d41e70de196e78e21e1001d23a7259785cbd0835vandebo@chromium.org        (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE)) {
1865ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
1866ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else {
1867ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
1868ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fItalicAngle = 0;
1869ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fAscent = 0;
1870ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fDescent = 0;
1871ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStemV = 0;
1872ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fCapHeight = 0;
1873ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fBBox = SkIRect::MakeEmpty();
1874390c6d7a9018e233a6519397ac6c739fb21a99efedisonn@google.com        goto ReturnInfo;
1875ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
18765aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org
1877ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // If this bit is clear the font is a fixed pitch font.
1878ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1879ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
1880ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1881ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (otm.otmTextMetrics.tmItalic) {
1882ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
1883ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1884ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) {
1885ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
1886ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    } else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) {
1887ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
1888ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1889ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1890ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // The main italic angle of the font, in tenths of a degree counterclockwise
1891ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // from vertical.
1892ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fItalicAngle = otm.otmItalicAngle / 10;
1893ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent);
1894ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent);
1895ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // TODO(ctguil): Use alternate cap height calculation.
1896ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // MSDN says otmsCapEmHeight is not support but it is returning a value on
1897ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // my Win7 box.
1898ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fCapHeight = otm.otmsCapEmHeight;
1899ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fBBox =
1900ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top,
1901ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                          otm.otmrcFontBox.right, otm.otmrcFontBox.bottom);
1902ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1903ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // Figure out a good guess for StemV - Min width of i, I, !, 1.
1904ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    // This probably isn't very good with an italic font.
190505b6f3a5a94de475d3a9dcfdd1833e24d933f207reed@google.com    min_width = SHRT_MAX;
1906ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    info->fStemV = 0;
1907ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
1908ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        ABC abcWidths;
1909ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) {
1910ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            int16_t width = abcWidths.abcB;
1911ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            if (width > 0 && width < min_width) {
1912ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                min_width = width;
1913ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com                info->fStemV = min_width;
1914ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com            }
1915ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com        }
1916ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1917ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
19185f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    // If bit 1 is set, the font may not be embedded in a document.
19195f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    // If bit 1 is clear, the font can be embedded.
19205f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    // If bit 2 is set, the embedding is read-only.
19215f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    if (otm.otmfsType & 0x1) {
19225f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org        info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
19235f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org    } else if (perGlyphInfo &
19245f209e64057e607e260c9bb4fee02d16c5f35f98vandebo@chromium.org               SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
19255aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) {
19265aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            appendRange(&info->fGlyphWidths, 0);
19275aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            info->fGlyphWidths->fAdvance.append(1, &min_width);
19285aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            finishRange(info->fGlyphWidths.get(), 0,
19295aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org                        SkAdvancedTypefaceMetrics::WidthRange::kDefault);
19305aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        } else {
19315aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org            info->fGlyphWidths.reset(
193237ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                getAdvanceData(hdc,
193337ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphCount,
193437ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphIDs,
193537ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               glyphIDsCount,
193637ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                               &getWidthAdvance));
19375aa937b300475c956bfad0c34a6daa71d166f6factguil@chromium.org        }
1938ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
1939ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1940ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.comError:
1941390c6d7a9018e233a6519397ac6c739fb21a99efedisonn@google.comReturnInfo:
1942ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, savefont);
1943ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(designFont);
1944ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(font);
1945ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteDC(hdc);
1946ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1947ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return info;
1948ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
1949ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
1950a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com//Dummy representation of a Base64 encoded GUID from create_unique_font_name.
1951a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#define BASE64_GUID_ID "XXXXXXXXXXXXXXXXXXXXXXXX"
1952a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com//Length of GUID representation from create_id, including NULL terminator.
1953a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com#define BASE64_GUID_ID_LEN SK_ARRAY_COUNT(BASE64_GUID_ID)
1954a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1955a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comSK_COMPILE_ASSERT(BASE64_GUID_ID_LEN < LF_FACESIZE, GUID_longer_than_facesize);
1956a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1957a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1958a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   NameID 6 Postscript names cannot have the character '/'.
1959a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   It would be easier to hex encode the GUID, but that is 32 bytes,
1960a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   and many systems have issues with names longer than 28 bytes.
1961a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The following need not be any standard base64 encoding.
1962a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The encoded value is never decoded.
1963a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com*/
1964d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.comstatic const char postscript_safe_base64_encode[] =
1965a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1966a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "abcdefghijklmnopqrstuvwxyz"
1967a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    "0123456789-_=";
1968a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1969a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1970a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Formats a GUID into Base64 and places it into buffer.
1971a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   buffer should have space for at least BASE64_GUID_ID_LEN characters.
1972a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The string will always be null terminated.
1973a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   XXXXXXXXXXXXXXXXXXXXXXXX0
1974a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
1975a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic void format_guid_b64(const GUID& guid, char* buffer, size_t bufferSize) {
1976a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    SkASSERT(bufferSize >= BASE64_GUID_ID_LEN);
1977a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    size_t written = SkBase64::Encode(&guid, sizeof(guid), buffer, postscript_safe_base64_encode);
1978a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    SkASSERT(written < LF_FACESIZE);
1979a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    buffer[written] = '\0';
1980a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
1981a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1982a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1983a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Creates a Base64 encoded GUID and places it into buffer.
1984a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   buffer should have space for at least BASE64_GUID_ID_LEN characters.
1985a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   The string will always be null terminated.
1986a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   XXXXXXXXXXXXXXXXXXXXXXXX0
1987a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com */
1988a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic HRESULT create_unique_font_name(char* buffer, size_t bufferSize) {
1989a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    GUID guid = {};
1990a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (FAILED(CoCreateGuid(&guid))) {
1991a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return E_UNEXPECTED;
1992a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
1993a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    format_guid_b64(guid, buffer, bufferSize);
1994a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1995a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return S_OK;
1996a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
1997a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
1998a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com/**
1999a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   Introduces a font to GDI. On failure will return NULL. The returned handle
2000a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com   should eventually be passed to RemoveFontMemResourceEx.
2001a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com*/
2002a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.comstatic HANDLE activate_font(SkData* fontData) {
2003a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    DWORD numFonts = 0;
2004a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    //AddFontMemResourceEx just copies the data, but does not specify const.
2005a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    HANDLE fontHandle = AddFontMemResourceEx(const_cast<void*>(fontData->data()),
20064b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com                                             static_cast<DWORD>(fontData->size()),
2007a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com                                             0,
2008a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com                                             &numFonts);
2009a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
2010a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (fontHandle != NULL && numFonts < 1) {
2011a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        RemoveFontMemResourceEx(fontHandle);
2012a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
2013a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
2014a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
2015a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return fontHandle;
2016a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com}
2017a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
2018437eea160b2f605d9074106891288e85baaecfefreed@google.comstatic SkTypeface* create_from_stream(SkStream* stream) {
2019a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Create a unique and unpredictable font name.
2020a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Avoids collisions and access from CSS.
2021a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    char familyName[BASE64_GUID_ID_LEN];
2022a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    const int familyNameSize = SK_ARRAY_COUNT(familyName);
2023a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (FAILED(create_unique_font_name(familyName, familyNameSize))) {
2024a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
2025a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
2026d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
2027a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Change the name of the font.
2028e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    SkAutoTUnref<SkData> rewrittenFontData(SkOTUtils::RenameFont(stream, familyName, familyNameSize-1));
2029e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    if (NULL == rewrittenFontData.get()) {
2030a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
2031a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
2032ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
2033a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Register the font with GDI.
2034e9bbee397ce96aa6642a42823feb1d7c4a8ffd8bbungeman@google.com    HANDLE fontReference = activate_font(rewrittenFontData.get());
2035a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    if (NULL == fontReference) {
2036a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com        return NULL;
2037a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    }
2038a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
2039a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    // Create the typeface.
2040a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    LOGFONT lf;
2041484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    logfont_for_name(familyName, &lf);
2042a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com
2043a550199c6f37e1b05a386ea57eee4c40cc91d84dbungeman@google.com    return SkCreateFontMemResourceTypefaceFromLOGFONT(lf, fontReference);
2044ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
2045ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
20460042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.comSkStream* LogFontTypeface::onOpenStream(int* ttcIndex) const {
20470042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com    *ttcIndex = 0;
20480042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com
2049f4c2622a1a3489988f2d85a81fdc132b17a92bddctguil@chromium.org    const DWORD kTTCTag =
2050f4c2622a1a3489988f2d85a81fdc132b17a92bddctguil@chromium.org        SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f'));
20510042b9c2a2e6fe954cdfbcd5a5b7449cdf41a4c3reed@google.com    LOGFONT lf = fLogFont;
2052ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
2053ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
205459d2f63583512b47df579e80f1ae39c74fad5f3ereed@google.com    HFONT font = CreateFontIndirect(&lf);
2055ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
2056ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
2057d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    SkMemoryStream* stream = NULL;
2058d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    DWORD tables[2] = {kTTCTag, 0};
2059d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org    for (int i = 0; i < SK_ARRAY_COUNT(tables); i++) {
20604b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com        DWORD bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0);
206139698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        if (bufferSize == GDI_ERROR) {
2062055180cb9c47c87e5ee8c9fa6d3f6a6079ad6f35reed@google.com            call_ensure_accessible(lf);
206339698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com            bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0);
206439698b1bd71fd54c7c44ae8d7224afdaf93c0ddabungeman@google.com        }
2065d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org        if (bufferSize != GDI_ERROR) {
2066d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            stream = new SkMemoryStream(bufferSize);
20674b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com            if (GetFontData(hdc, tables[i], 0, (void*)stream->getMemoryBase(), bufferSize)) {
2068d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                break;
2069d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            } else {
2070d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                delete stream;
2071d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org                stream = NULL;
2072d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org            }
2073d604481f7a2587f5b400d2a0a68a6491a0d584c7vandebo@chromium.org        }
2074ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    }
2075ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
2076ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    SelectObject(hdc, savefont);
2077ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteObject(font);
2078ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    DeleteDC(hdc);
2079ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
2080ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com    return stream;
2081ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
2082ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
20833c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comstatic void bmpCharsToGlyphs(HDC hdc, const WCHAR* bmpChars, int count, uint16_t* glyphs,
20843c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                             bool Ox1FHack)
20853c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com{
20863c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    DWORD result = GetGlyphIndicesW(hdc, bmpChars, count, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
20873c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (GDI_ERROR == result) {
20883c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
20893c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            glyphs[i] = 0;
20903c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
20913c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        return;
20923c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
20933c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
20943c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (Ox1FHack) {
20953c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
20963c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (0xFFFF == glyphs[i] || 0x1F == glyphs[i]) {
20973c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[i] = 0;
20983c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
20993c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21003c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    } else {
21013c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        for (int i = 0; i < count; ++i) {
21023c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (0xFFFF == glyphs[i]){
21033c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[i] = 0;
21043c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
21053c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21063c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21073c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
21083c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21093c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comstatic uint16_t nonBmpCharToGlyph(HDC hdc, SCRIPT_CACHE* scriptCache, const WCHAR utf16[2]) {
21103c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    uint16_t index = 0;
21113c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Use uniscribe to detemine glyph index for non-BMP characters.
21123c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int numWCHAR = 2;
21133c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int maxItems = 2;
21143c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // MSDN states that this can be NULL, but some things don't work then.
21153c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_CONTROL scriptControl = { 0 };
21163c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Add extra item to SCRIPT_ITEM to work around a bug (now documented).
21173c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // https://bugzilla.mozilla.org/show_bug.cgi?id=366643
21183c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_ITEM si[maxItems + 1];
21193c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    int numItems;
21203c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &scriptControl, NULL, si, &numItems),
21213c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com         "Could not itemize character.");
21223c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21233c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs.
21243c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    static const int maxGlyphs = 2;
21253c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_VISATTR vsa[maxGlyphs];
21263c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    WORD outGlyphs[maxGlyphs];
21273c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    WORD logClust[numWCHAR];
21283c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    int numGlyphs;
21293c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HRZM(ScriptShape(hdc, scriptCache, utf16, numWCHAR, maxGlyphs, &si[0].a,
21303c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                     outGlyphs, logClust, vsa, &numGlyphs),
21313c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com         "Could not shape character.");
21323c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (1 == numGlyphs) {
21333c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        index = outGlyphs[0];
21343c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21353c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    return index;
21363c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
21373c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21383c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comclass SkAutoHDC {
21393c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.compublic:
21403c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoHDC(const LOGFONT& lf)
21413c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        : fHdc(::CreateCompatibleDC(NULL))
21423c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        , fFont(::CreateFontIndirect(&lf))
21433c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        , fSavefont((HFONT)SelectObject(fHdc, fFont))
21443c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    { }
21453c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    ~SkAutoHDC() {
21463c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SelectObject(fHdc, fSavefont);
21473c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        DeleteObject(fFont);
21483c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        DeleteDC(fHdc);
21493c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21503c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    operator HDC() { return fHdc; }
21513c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comprivate:
21523c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HDC fHdc;
21533c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HFONT fFont;
21543c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    HFONT fSavefont;
21553c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com};
2156e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoHDC(...) SK_REQUIRE_LOCAL_VAR(SkAutoHDC)
21573c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21583c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.comint LogFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
21593c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                                     uint16_t userGlyphs[], int glyphCount) const
21603c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com{
21613c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoHDC hdc(fLogFont);
21623c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21633c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    TEXTMETRIC tm;
21643c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (0 == GetTextMetrics(hdc, &tm)) {
21653c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        call_ensure_accessible(fLogFont);
21663c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (0 == GetTextMetrics(hdc, &tm)) {
21673c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            tm.tmPitchAndFamily = TMPF_TRUETYPE;
21683c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21693c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21703c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    bool Ox1FHack = !(tm.tmPitchAndFamily & TMPF_VECTOR) /*&& winVer < Vista */;
21713c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21723c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SkAutoSTMalloc<256, uint16_t> scratchGlyphs;
21733c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    uint16_t* glyphs;
21743c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (userGlyphs != NULL) {
21753c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        glyphs = userGlyphs;
21763c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    } else {
21773c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        glyphs = scratchGlyphs.reset(glyphCount);
21783c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
21793c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
21803c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    SCRIPT_CACHE sc = 0;
21813c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    switch (encoding) {
21823c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF8_Encoding: {
21833c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        static const int scratchCount = 256;
21843c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        WCHAR scratch[scratchCount];
21853c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
21863c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const char* currentUtf8 = reinterpret_cast<const char*>(chars);
21873c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SkUnichar currentChar;
21883c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (glyphCount) {
21893c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            currentChar = SkUTF8_NextUnichar(&currentUtf8);
21903c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
21913c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
21923c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
21933c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount);
21943c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
21953c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && currentChar <= 0xFFFF) {
21963c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                scratch[runLength] = static_cast<WCHAR>(currentChar);
21973c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
21983c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                if (runLength < glyphsLeft) {
21993c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                    currentChar = SkUTF8_NextUnichar(&currentUtf8);
22003c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                }
22013c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22023c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
22033c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack);
22043c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
22053c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22063c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22073c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
22083c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && currentChar > 0xFFFF) {
22093c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                SkUTF16_FromUnichar(currentChar, reinterpret_cast<uint16_t*>(scratch));
22103c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch);
22113c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
22123c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                if (glyphIndex < glyphCount) {
22133c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                    currentChar = SkUTF8_NextUnichar(&currentUtf8);
22143c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                }
22153c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22163c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
22173c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
22183c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22193c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF16_Encoding: {
22203c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
22213c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const WCHAR* currentUtf16 = reinterpret_cast<const WCHAR*>(chars);
22223c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
22233c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
22243c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = glyphCount - glyphIndex;
22253c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
22263c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && !SkUTF16_IsHighSurrogate(currentUtf16[runLength])) {
22273c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
22283c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22293c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
22303c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, currentUtf16, runLength, &glyphs[glyphIndex], Ox1FHack);
22313c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
22323c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                currentUtf16 += runLength;
22333c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22343c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22353c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
22363c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && SkUTF16_IsHighSurrogate(*currentUtf16)) {
22373c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, currentUtf16);
22383c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
22393c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                currentUtf16 += 2;
22403c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22413c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
22423c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
22433c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22443c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    case SkTypeface::kUTF32_Encoding: {
22453c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        static const int scratchCount = 256;
22463c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        WCHAR scratch[scratchCount];
22473c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        int glyphIndex = 0;
22483c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        const uint32_t* utf32 = reinterpret_cast<const uint32_t*>(chars);
22493c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        while (glyphIndex < glyphCount) {
22503c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of bmp.
22513c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount);
22523c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            int runLength = 0;
22533c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (runLength < glyphsLeft && utf32[glyphIndex + runLength] <= 0xFFFF) {
22543c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                scratch[runLength] = static_cast<WCHAR>(utf32[glyphIndex + runLength]);
22553c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++runLength;
22563c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22573c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            if (runLength) {
22583c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack);
22593c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphIndex += runLength;
22603c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22613c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22623c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            // Try a run of non-bmp.
22633c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            while (glyphIndex < glyphCount && utf32[glyphIndex] > 0xFFFF) {
22643c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                SkUTF16_FromUnichar(utf32[glyphIndex], reinterpret_cast<uint16_t*>(scratch));
22653c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch);
22663c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                ++glyphIndex;
22673c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            }
22683c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
22693c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        break;
22703c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22713c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    default:
22723c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        SK_CRASH();
22733c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22743c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22753c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    if (sc) {
22763c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        ::ScriptFreeCache(&sc);
22773c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22783c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22793c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    for (int i = 0; i < glyphCount; ++i) {
22803c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        if (0 == glyphs[i]) {
22813c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com            return i;
22823c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com        }
22833c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    }
22843c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com    return glyphCount;
22853c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com}
22863c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com
22877bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comint LogFontTypeface::onCountGlyphs() const {
22887bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
22897bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT font = CreateFontIndirect(&fLogFont);
22907bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
22917bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22927bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int glyphCount = calculateGlyphCount(hdc, fLogFont);
22937bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22947bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    SelectObject(hdc, savefont);
22957bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteObject(font);
22967bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteDC(hdc);
22977bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
22987bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return glyphCount;
22997bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
23007bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
23017bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.comint LogFontTypeface::onGetUPEM() const {
23027bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
23037bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT font = CreateFontIndirect(&fLogFont);
23047bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
23057bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
23067bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    unsigned int upem = calculateUPEM(hdc, fLogFont);
23077bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
23087bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    SelectObject(hdc, savefont);
23097bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteObject(font);
23107bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    DeleteDC(hdc);
23117bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
23127bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    return upem;
23137bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com}
23147bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com
2315839702b61934914118ec557dd641be322eba3b5fbungeman@google.comSkTypeface::LocalizedStrings* LogFontTypeface::onCreateFamilyNameIterator() const {
2316a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkTypeface::LocalizedStrings* nameIter =
2317a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
2318a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (NULL == nameIter) {
2319a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString familyName;
2320a980269c2498836101146adc729ef780fb89824ebungeman@google.com        this->getFamilyName(&familyName);
2321a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString language("und"); //undetermined
2322a980269c2498836101146adc729ef780fb89824ebungeman@google.com        nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
2323a980269c2498836101146adc729ef780fb89824ebungeman@google.com    }
2324a980269c2498836101146adc729ef780fb89824ebungeman@google.com    return nameIter;
2325a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
2326a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2327b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.comint LogFontTypeface::onGetTableTags(SkFontTableTag tags[]) const {
2328b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    SkSFNTHeader header;
2329b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (sizeof(header) != this->onGetTableData(0, 0, sizeof(header), &header)) {
2330b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        return 0;
2331b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2332b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2333b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    int numTables = SkEndian_SwapBE16(header.numTables);
2334b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2335b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (tags) {
2336b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        size_t size = numTables * sizeof(SkSFNTHeader::TableDirectoryEntry);
2337b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        SkAutoSTMalloc<0x20, SkSFNTHeader::TableDirectoryEntry> dir(numTables);
2338b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        if (size != this->onGetTableData(0, sizeof(header), size, dir.get())) {
2339b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com            return 0;
2340b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        }
2341b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2342b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        for (int i = 0; i < numTables; ++i) {
2343b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com            tags[i] = SkEndian_SwapBE32(dir[i].tag);
2344b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        }
2345b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2346b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    return numTables;
2347b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com}
2348b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2349b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.comsize_t LogFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
2350b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com                                       size_t length, void* data) const
2351b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com{
2352b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    LOGFONT lf = fLogFont;
2353b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2354b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HDC hdc = ::CreateCompatibleDC(NULL);
2355b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HFONT font = CreateFontIndirect(&lf);
2356b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    HFONT savefont = (HFONT)SelectObject(hdc, font);
2357b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2358b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    tag = SkEndian_SwapBE32(tag);
2359b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (NULL == data) {
2360b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        length = 0;
2361b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
23628b169311b59ab84e8ca6f3630a1e960cc1be751erobertphillips@google.com    DWORD bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length);
2363b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    if (bufferSize == GDI_ERROR) {
2364b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com        call_ensure_accessible(lf);
23658b169311b59ab84e8ca6f3630a1e960cc1be751erobertphillips@google.com        bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length);
2366b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    }
2367b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2368b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    SelectObject(hdc, savefont);
2369b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    DeleteObject(font);
2370b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    DeleteDC(hdc);
2371b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
2372b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com    return bufferSize == GDI_ERROR ? 0 : bufferSize;
2373b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com}
2374b10b51f64dbd1cf44571a1eedb412378702d8cd4bungeman@google.com
23750da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comSkScalerContext* LogFontTypeface::onCreateScalerContext(const SkDescriptor* desc) const {
237630ddd615c447fed73286151b463af20d309c85f1reed@google.com    SkScalerContext_GDI* ctx = SkNEW_ARGS(SkScalerContext_GDI,
237784e22d847fc84727bc220947162363ee1fe068fcreed@google.com                                                (const_cast<LogFontTypeface*>(this), desc));
237884e22d847fc84727bc220947162363ee1fe068fcreed@google.com    if (!ctx->isValid()) {
237984e22d847fc84727bc220947162363ee1fe068fcreed@google.com        SkDELETE(ctx);
238084e22d847fc84727bc220947162363ee1fe068fcreed@google.com        ctx = NULL;
238184e22d847fc84727bc220947162363ee1fe068fcreed@google.com    }
238284e22d847fc84727bc220947162363ee1fe068fcreed@google.com    return ctx;
2383ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com}
2384ac6b97972d3c5a8e10b79c1409818756310e0f12reed@google.com
23850da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.comvoid LogFontTypeface::onFilterRec(SkScalerContextRec* rec) const {
2386c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag ||
2387c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fFlags & SkScalerContext::kLCD_Vertical_Flag)
2388c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    {
2389c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fMaskFormat = SkMask::kA8_Format;
2390c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org        rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag;
2391c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org    }
2392c5fd46171841711973b9433c726ff8608335940ccommit-bot@chromium.org
2393e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag |
2394e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kAutohinting_Flag |
2395e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kEmbeddedBitmapText_Flag |
2396e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kEmbolden_Flag |
2397e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kLCD_BGROrder_Flag |
2398e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com                                  SkScalerContext::kLCD_Vertical_Flag;
2399e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    rec->fFlags &= ~flagsWeDontSupport;
2400e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com
2401e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    SkPaint::Hinting h = rec->getHinting();
2402e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    switch (h) {
2403e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kNo_Hinting:
24040abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            break;
2405e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kSlight_Hinting:
24060abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // Only do slight hinting when axis aligned.
24077c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            // TODO: re-enable slight hinting when FontHostTest can pass.
24087c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            //if (!isAxisAligned(*rec)) {
24090abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com                h = SkPaint::kNo_Hinting;
24107c86d8eb3a61eae188bc8af2cd0363858eaea07ebungeman@google.com            //}
2411e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            break;
2412e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kNormal_Hinting:
2413e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        case SkPaint::kFull_Hinting:
24140abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // TODO: need to be able to distinguish subpixel positioned glyphs
24150abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            // and linear metrics.
24160abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com            //rec->fFlags &= ~SkScalerContext::kSubpixelPositioning_Flag;
2417e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            h = SkPaint::kNormal_Hinting;
2418e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com            break;
2419e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com        default:
24200c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("unknown hinting");
2421e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    }
24220abbff9987b9452fd30cce198bea34fdb210ac41bungeman@google.com    //TODO: if this is a bitmap font, squash hinting and subpixel.
2423e8fab0111d11316a027bc6ae60c62cc6575cdc05reed@google.com    rec->setHinting(h);
2424da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com
24259181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com// turn this off since GDI might turn A8 into BW! Need a bigger fix.
24269181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com#if 0
2427da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    // Disable LCD when rotated, since GDI's output is ugly
2428da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    if (isLCD(*rec) && !isAxisAligned(*rec)) {
2429da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com        rec->fMaskFormat = SkMask::kA8_Format;
2430da44067ec97fddd3bab52da34e2fc5ddc5241c34reed@google.com    }
24319181aa84aa288d2bc3ab079b6b40a79ac87d8807reed@google.com#endif
2432754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com
24330da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    if (!fCanBeLCD && isLCD(*rec)) {
2434cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        rec->fMaskFormat = SkMask::kA8_Format;
2435cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com        rec->fFlags &= ~SkScalerContext::kGenA8FromLCD_Flag;
2436754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com    }
2437754e4ebd10d7353ce55be7dfe0a417e502ef4047reed@google.com}
2438070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
2439070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com///////////////////////////////////////////////////////////////////////////////
2440070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
2441070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com#include "SkFontMgr.h"
2442484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com#include "SkDataTable.h"
2443484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2444a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic bool valid_logfont_for_enum(const LOGFONT& lf) {
2445a980269c2498836101146adc729ef780fb89824ebungeman@google.com    // TODO: Vector FON is unsupported and should not be listed.
2446a980269c2498836101146adc729ef780fb89824ebungeman@google.com    return
2447a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // Ignore implicit vertical variants.
2448a980269c2498836101146adc729ef780fb89824ebungeman@google.com        lf.lfFaceName[0] && lf.lfFaceName[0] != '@'
2449a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2450a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // DEFAULT_CHARSET is used to get all fonts, but also implies all
2451a980269c2498836101146adc729ef780fb89824ebungeman@google.com        // character sets. Filter assuming all fonts support ANSI_CHARSET.
2452a980269c2498836101146adc729ef780fb89824ebungeman@google.com        && ANSI_CHARSET == lf.lfCharSet
2453a980269c2498836101146adc729ef780fb89824ebungeman@google.com    ;
2454a980269c2498836101146adc729ef780fb89824ebungeman@google.com}
2455a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2456a980269c2498836101146adc729ef780fb89824ebungeman@google.com/** An EnumFontFamExProc implementation which interprets builderParam as
2457a980269c2498836101146adc729ef780fb89824ebungeman@google.com *  an SkTDArray<ENUMLOGFONTEX>* and appends logfonts which
2458a980269c2498836101146adc729ef780fb89824ebungeman@google.com *  pass the valid_logfont_for_enum predicate.
2459a980269c2498836101146adc729ef780fb89824ebungeman@google.com */
2460a980269c2498836101146adc729ef780fb89824ebungeman@google.comstatic int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*,
2461a980269c2498836101146adc729ef780fb89824ebungeman@google.com                                     DWORD fontType, LPARAM builderParam) {
2462a980269c2498836101146adc729ef780fb89824ebungeman@google.com    if (valid_logfont_for_enum(*lf)) {
2463a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderParam;
2464a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        *array->append() = *(ENUMLOGFONTEX*)lf;
2465484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2466484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    return 1; // non-zero means continue
2467484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com}
2468484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2469484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comstatic SkFontStyle compute_fontstyle(const LOGFONT& lf) {
2470484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    return SkFontStyle(lf.lfWeight, SkFontStyle::kNormal_Width,
2471484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                       lf.lfItalic ? SkFontStyle::kItalic_Slant
2472484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                                   : SkFontStyle::kUpright_Slant);
2473484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com}
2474484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2475484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comclass SkFontStyleSetGDI : public SkFontStyleSet {
2476484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.compublic:
2477a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkFontStyleSetGDI(const TCHAR familyName[]) {
2478a980269c2498836101146adc729ef780fb89824ebungeman@google.com        LOGFONT lf;
2479a980269c2498836101146adc729ef780fb89824ebungeman@google.com        sk_bzero(&lf, sizeof(lf));
2480a980269c2498836101146adc729ef780fb89824ebungeman@google.com        lf.lfCharSet = DEFAULT_CHARSET;
2481a980269c2498836101146adc729ef780fb89824ebungeman@google.com        _tcscpy_s(lf.lfFaceName, familyName);
2482a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2483484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        HDC hdc = ::CreateCompatibleDC(NULL);
2484a980269c2498836101146adc729ef780fb89824ebungeman@google.com        ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0);
2485484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::DeleteDC(hdc);
2486484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2487484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2488484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual int count() SK_OVERRIDE {
2489484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return fArray.count();
2490484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2491484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2492484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName) SK_OVERRIDE {
2493484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        if (fs) {
2494a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            *fs = compute_fontstyle(fArray[index].elfLogFont);
2495a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        }
2496a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        if (styleName) {
2497a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            const ENUMLOGFONTEX& ref = fArray[index];
2498a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type in the
2499a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // non-unicode version.
2500a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            //      ENUMLOGFONTEX uses BYTE
2501a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            //      LOGFONT uses CHAR
2502a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // Here we assert they that the style name is logically the same (size) as
2503a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            // a TCHAR, so we can use the same converter function.
2504a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0]));
2505a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com            tchar_to_skstring((const TCHAR*)ref.elfStyle, styleName);
2506484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        }
2507484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2508484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2509484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
2510a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkCreateTypefaceFromLOGFONT(fArray[index].elfLogFont);
2511484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2512484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2513484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
2514484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        // todo:
2515a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkCreateTypefaceFromLOGFONT(fArray[0].elfLogFont);
2516484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2517484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2518484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprivate:
2519a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkTDArray<ENUMLOGFONTEX> fArray;
2520484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com};
2521484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2522484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comclass SkFontMgrGDI : public SkFontMgr {
2523a980269c2498836101146adc729ef780fb89824ebungeman@google.compublic:
2524a980269c2498836101146adc729ef780fb89824ebungeman@google.com    SkFontMgrGDI() {
2525484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        LOGFONT lf;
2526484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        sk_bzero(&lf, sizeof(lf));
2527484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        lf.lfCharSet = DEFAULT_CHARSET;
2528484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2529484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        HDC hdc = ::CreateCompatibleDC(NULL);
2530484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fLogFontArray, 0);
2531484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ::DeleteDC(hdc);
2532484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2533484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2534484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprotected:
2535484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual int onCountFamilies() SK_OVERRIDE {
2536484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return fLogFontArray.count();
2537484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2538484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2539484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
2540484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkASSERT((unsigned)index < (unsigned)fLogFontArray.count());
2541a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        tchar_to_skstring(fLogFontArray[index].elfLogFont.lfFaceName, familyName);
2542484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2543484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2544484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE {
2545484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkASSERT((unsigned)index < (unsigned)fLogFontArray.count());
2546a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkNEW_ARGS(SkFontStyleSetGDI, (fLogFontArray[index].elfLogFont.lfFaceName));
2547484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2548484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2549484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkFontStyleSet* onMatchFamily(const char familyName[]) SK_OVERRIDE {
2550484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        if (NULL == familyName) {
2551484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com            familyName = "";    // do we need this check???
2552484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        }
2553484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        LOGFONT lf;
2554484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        logfont_for_name(familyName, &lf);
2555a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com        return SkNEW_ARGS(SkFontStyleSetGDI, (lf.lfFaceName));
2556484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2557484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2558484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
2559484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                                           const SkFontStyle& fontstyle) SK_OVERRIDE {
2560437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2561484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
2562484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return sset->matchStyle(fontstyle);
2563484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2564484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2565484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
2566484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com                                         const SkFontStyle& fontstyle) SK_OVERRIDE {
2567437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2568484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        SkString familyName;
2569484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        ((LogFontTypeface*)familyMember)->getFamilyName(&familyName);
2570484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com        return this->matchFamilyStyle(familyName.c_str(), fontstyle);
2571484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2572484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
2573437eea160b2f605d9074106891288e85baaecfefreed@google.com    virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OVERRIDE {
2574437eea160b2f605d9074106891288e85baaecfefreed@google.com        return create_from_stream(stream);
2575484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2576437eea160b2f605d9074106891288e85baaecfefreed@google.com
2577437eea160b2f605d9074106891288e85baaecfefreed@google.com    virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE {
2578437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2579437eea160b2f605d9074106891288e85baaecfefreed@google.com        SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
2580437eea160b2f605d9074106891288e85baaecfefreed@google.com        return this->createFromStream(stream);
2581484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2582437eea160b2f605d9074106891288e85baaecfefreed@google.com
2583484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVERRIDE {
2584437eea160b2f605d9074106891288e85baaecfefreed@google.com        // could be in base impl
2585437eea160b2f605d9074106891288e85baaecfefreed@google.com        SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
2586437eea160b2f605d9074106891288e85baaecfefreed@google.com        return this->createFromStream(stream);
2587484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    }
2588484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com
258930ddd615c447fed73286151b463af20d309c85f1reed@google.com    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
259030ddd615c447fed73286151b463af20d309c85f1reed@google.com                                               unsigned styleBits) SK_OVERRIDE {
2591f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        LOGFONT lf;
2592f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        if (NULL == familyName) {
2593f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com            lf = get_default_font();
2594f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        } else {
2595f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com            logfont_for_name(familyName, &lf);
2596f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        }
2597f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        setStyle(&lf, (SkTypeface::Style)styleBits);
2598f7159bba8efc13f6b3a9a86e92b48451b6aa8073bungeman@google.com        return SkCreateTypefaceFromLOGFONT(lf);
259930ddd615c447fed73286151b463af20d309c85f1reed@google.com    }
260030ddd615c447fed73286151b463af20d309c85f1reed@google.com
2601484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.comprivate:
2602a65a681d43a8e9f5d8726a389832e957b07ee16breed@google.com    SkTDArray<ENUMLOGFONTEX> fLogFontArray;
2603484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com};
2604070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com
260530ddd615c447fed73286151b463af20d309c85f1reed@google.com///////////////////////////////////////////////////////////////////////////////
260630ddd615c447fed73286151b463af20d309c85f1reed@google.com
2607bfc6cc4b6c41e99d3887d268fbf798aa3abc9a27bungeman@google.comSkFontMgr* SkFontMgr_New_GDI() {
2608484f5bcf07c965e84cf021a6272aff73e2d7c1ecreed@google.com    return SkNEW(SkFontMgrGDI);
2609070da5edcb1c6e9b9507f06a5e2c270e1c1fd796reed@google.com}
2610