reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | #if defined(SK_BUILD_FOR_WIN32) |
| 10 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 11 | #include "SkAdvancedTypefaceMetrics.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 12 | #include "SkBase64.h" |
bungeman@google.com | 1bfe01d | 2012-08-28 16:02:42 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
| 15 | #include "SkDescriptor.h" |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 16 | #include "SkFontDescriptor.h" |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 17 | #include "SkGlyph.h" |
bungeman@google.com | 27f74aa | 2013-10-08 21:32:15 +0000 | [diff] [blame] | 18 | #include "SkHRESULT.h" |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 19 | #include "SkMaskGamma.h" |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 20 | #include "SkMatrix22.h" |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 21 | #include "SkOTTable_maxp.h" |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 22 | #include "SkOTTable_name.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 23 | #include "SkOTUtils.h" |
reed@google.com | 2788987 | 2012-08-07 16:15:13 +0000 | [diff] [blame] | 24 | #include "SkPath.h" |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 25 | #include "SkSFNTHeader.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 26 | #include "SkStream.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 27 | #include "SkString.h" |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 28 | #include "SkTemplates.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 29 | #include "SkTypeface_win.h" |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 30 | #include "SkTypefaceCache.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 31 | #include "SkUtils.h" |
| 32 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 33 | #include "SkTypes.h" |
| 34 | #include <tchar.h> |
| 35 | #include <usp10.h> |
| 36 | #include <objbase.h> |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 37 | |
reed@google.com | f210f50 | 2013-03-20 14:15:52 +0000 | [diff] [blame] | 38 | static void (*gEnsureLOGFONTAccessibleProc)(const LOGFONT&); |
| 39 | |
| 40 | void SkTypeface_SetEnsureLOGFONTAccessibleProc(void (*proc)(const LOGFONT&)) { |
| 41 | gEnsureLOGFONTAccessibleProc = proc; |
| 42 | } |
| 43 | |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 44 | static void call_ensure_accessible(const LOGFONT& lf) { |
| 45 | if (gEnsureLOGFONTAccessibleProc) { |
| 46 | gEnsureLOGFONTAccessibleProc(lf); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 52 | // always packed xxRRGGBB |
| 53 | typedef uint32_t SkGdiRGB; |
| 54 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 55 | // define this in your Makefile or .gyp to enforce AA requests |
| 56 | // which GDI ignores at small sizes. This flag guarantees AA |
| 57 | // for rotated text, regardless of GDI's notions. |
| 58 | //#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 59 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 60 | static bool isLCD(const SkScalerContext::Rec& rec) { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 61 | return SkMask::kLCD16_Format == rec.fMaskFormat; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 62 | } |
| 63 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 64 | static bool bothZero(SkScalar a, SkScalar b) { |
| 65 | return 0 == a && 0 == b; |
| 66 | } |
| 67 | |
| 68 | // returns false if there is any non-90-rotation or skew |
| 69 | static bool isAxisAligned(const SkScalerContext::Rec& rec) { |
| 70 | return 0 == rec.fPreSkewX && |
| 71 | (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 72 | bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
| 73 | } |
| 74 | |
| 75 | static bool needToRenderWithSkia(const SkScalerContext::Rec& rec) { |
| 76 | #ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 77 | // What we really want to catch is when GDI will ignore the AA request and give |
| 78 | // us BW instead. Smallish rotated text is one heuristic, so this code is just |
| 79 | // an approximation. We shouldn't need to do this for larger sizes, but at those |
| 80 | // sizes, the quality difference gets less and less between our general |
| 81 | // scanconverter and GDI's. |
| 82 | if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) { |
| 83 | return true; |
| 84 | } |
| 85 | #endif |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 86 | return rec.getHinting() == SkPaint::kNo_Hinting || rec.getHinting() == SkPaint::kSlight_Hinting; |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 87 | } |
| 88 | |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 89 | static void tchar_to_skstring(const TCHAR t[], SkString* s) { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 90 | #ifdef UNICODE |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | size_t sSize = WideCharToMultiByte(CP_UTF8, 0, t, -1, nullptr, 0, nullptr, nullptr); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 92 | s->resize(sSize); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | WideCharToMultiByte(CP_UTF8, 0, t, -1, s->writable_str(), sSize, nullptr, nullptr); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 94 | #else |
| 95 | s->set(t); |
| 96 | #endif |
| 97 | } |
| 98 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 99 | static void dcfontname_to_skstring(HDC deviceContext, const LOGFONT& lf, SkString* familyName) { |
| 100 | int fontNameLen; //length of fontName in TCHARS. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | if (0 == (fontNameLen = GetTextFace(deviceContext, 0, nullptr))) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 102 | call_ensure_accessible(lf); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | if (0 == (fontNameLen = GetTextFace(deviceContext, 0, nullptr))) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 104 | fontNameLen = 0; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | SkAutoSTArray<LF_FULLFACESIZE, TCHAR> fontName(fontNameLen+1); |
| 109 | if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) { |
| 110 | call_ensure_accessible(lf); |
| 111 | if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) { |
| 112 | fontName[0] = 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | tchar_to_skstring(fontName.get(), familyName); |
| 117 | } |
| 118 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 119 | static void make_canonical(LOGFONT* lf) { |
bungeman@google.com | 53cbb0b | 2013-09-08 19:36:58 +0000 | [diff] [blame] | 120 | lf->lfHeight = -64; |
bungeman | 59f093d | 2016-03-22 10:59:09 -0700 | [diff] [blame] | 121 | lf->lfWidth = 0; // lfWidth is related to lfHeight, not to the OS/2::usWidthClass. |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 122 | lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY; |
| 123 | lf->lfCharSet = DEFAULT_CHARSET; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 124 | // lf->lfClipPrecision = 64; |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 125 | } |
| 126 | |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 127 | static SkFontStyle get_style(const LOGFONT& lf) { |
| 128 | return SkFontStyle(lf.lfWeight, |
bungeman | 59f093d | 2016-03-22 10:59:09 -0700 | [diff] [blame] | 129 | SkFontStyle::kNormal_Width, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 130 | lf.lfItalic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static inline FIXED SkFixedToFIXED(SkFixed x) { |
| 134 | return *(FIXED*)(&x); |
| 135 | } |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 136 | static inline SkFixed SkFIXEDToFixed(FIXED x) { |
| 137 | return *(SkFixed*)(&x); |
| 138 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 139 | |
| 140 | static inline FIXED SkScalarToFIXED(SkScalar x) { |
| 141 | return SkFixedToFIXED(SkScalarToFixed(x)); |
| 142 | } |
| 143 | |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 144 | static inline SkScalar SkFIXEDToScalar(FIXED x) { |
| 145 | return SkFixedToScalar(SkFIXEDToFixed(x)); |
| 146 | } |
| 147 | |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 148 | static unsigned calculateGlyphCount(HDC hdc, const LOGFONT& lf) { |
| 149 | TEXTMETRIC textMetric; |
| 150 | if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 151 | textMetric.tmPitchAndFamily = TMPF_VECTOR; |
| 152 | call_ensure_accessible(lf); |
| 153 | GetTextMetrics(hdc, &textMetric); |
| 154 | } |
| 155 | |
| 156 | if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) { |
| 157 | return textMetric.tmLastChar; |
| 158 | } |
| 159 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 160 | // The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes. |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 161 | uint16_t glyphs; |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 162 | if (GDI_ERROR != GetFontData(hdc, SkOTTableMaximumProfile::TAG, 4, &glyphs, sizeof(glyphs))) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 163 | return SkEndian_SwapBE16(glyphs); |
| 164 | } |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 165 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 166 | // Binary search for glyph count. |
| 167 | static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; |
| 168 | int32_t max = SK_MaxU16 + 1; |
| 169 | int32_t min = 0; |
| 170 | GLYPHMETRICS gm; |
| 171 | while (min < max) { |
| 172 | int32_t mid = min + ((max - min) / 2); |
| 173 | if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | nullptr, &mat2) == GDI_ERROR) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 175 | max = mid; |
| 176 | } else { |
| 177 | min = mid + 1; |
| 178 | } |
| 179 | } |
| 180 | SkASSERT(min == max); |
| 181 | return min; |
| 182 | } |
| 183 | |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 184 | static unsigned calculateUPEM(HDC hdc, const LOGFONT& lf) { |
| 185 | TEXTMETRIC textMetric; |
| 186 | if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 187 | textMetric.tmPitchAndFamily = TMPF_VECTOR; |
| 188 | call_ensure_accessible(lf); |
| 189 | GetTextMetrics(hdc, &textMetric); |
| 190 | } |
| 191 | |
| 192 | if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) { |
| 193 | return textMetric.tmMaxCharWidth; |
| 194 | } |
| 195 | |
| 196 | OUTLINETEXTMETRIC otm; |
| 197 | unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 198 | if (0 == otmRet) { |
| 199 | call_ensure_accessible(lf); |
| 200 | otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 201 | } |
| 202 | |
| 203 | return (0 == otmRet) ? 0 : otm.otmEMSquare; |
| 204 | } |
| 205 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 206 | class LogFontTypeface : public SkTypeface { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 207 | public: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 208 | LogFontTypeface(const SkFontStyle& style, const LOGFONT& lf, bool serializeAsStream) |
bungeman | e3aea10 | 2016-07-13 05:16:58 -0700 | [diff] [blame] | 209 | : SkTypeface(style, false) |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 210 | , fLogFont(lf) |
| 211 | , fSerializeAsStream(serializeAsStream) |
| 212 | { |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 213 | HFONT font = CreateFontIndirect(&lf); |
| 214 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | HDC deviceContext = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 216 | HFONT savefont = (HFONT)SelectObject(deviceContext, font); |
| 217 | |
| 218 | TEXTMETRIC textMetric; |
| 219 | if (0 == GetTextMetrics(deviceContext, &textMetric)) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 220 | call_ensure_accessible(lf); |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 221 | if (0 == GetTextMetrics(deviceContext, &textMetric)) { |
| 222 | textMetric.tmPitchAndFamily = TMPF_TRUETYPE; |
| 223 | } |
| 224 | } |
| 225 | if (deviceContext) { |
| 226 | ::SelectObject(deviceContext, savefont); |
| 227 | ::DeleteDC(deviceContext); |
| 228 | } |
| 229 | if (font) { |
| 230 | ::DeleteObject(font); |
| 231 | } |
| 232 | |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 233 | // The fixed pitch bit is set if the font is *not* fixed pitch. |
| 234 | this->setIsFixedPitch((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0); |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 235 | this->setFontStyle(SkFontStyle(textMetric.tmWeight, style.width(), style.slant())); |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 236 | |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 237 | // Used a logfont on a memory context, should never get a device font. |
| 238 | // Therefore all TMPF_DEVICE will be PostScript (cubic) fonts. |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 239 | // If the font has cubic outlines, it will not be rendered with ClearType. |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 240 | fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) && |
| 241 | (textMetric.tmPitchAndFamily & TMPF_DEVICE)); |
| 242 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 243 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 244 | LOGFONT fLogFont; |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 245 | bool fSerializeAsStream; |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 246 | bool fCanBeLCD; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 247 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 248 | static LogFontTypeface* Create(const LOGFONT& lf) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 249 | return new LogFontTypeface(get_style(lf), lf, false); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 250 | } |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 251 | |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 252 | static void EnsureAccessible(const SkTypeface* face) { |
| 253 | call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFont); |
| 254 | } |
| 255 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 256 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 257 | SkStreamAsset* onOpenStream(int* ttcIndex) const override; |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 258 | SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, |
| 259 | const SkDescriptor*) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 260 | void onFilterRec(SkScalerContextRec*) const override; |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 261 | SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( |
| 262 | PerGlyphInfo, const uint32_t*, uint32_t) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 263 | void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 264 | virtual int onCharsToGlyphs(const void* chars, Encoding encoding, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 265 | uint16_t glyphs[], int glyphCount) const override; |
| 266 | int onCountGlyphs() const override; |
| 267 | int onGetUPEM() const override; |
| 268 | void onGetFamilyName(SkString* familyName) const override; |
| 269 | SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 270 | int onGetTableTags(SkFontTableTag tags[]) const override; |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 271 | virtual size_t onGetTableData(SkFontTableTag, size_t offset, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 272 | size_t length, void* data) const override; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 275 | class FontMemResourceTypeface : public LogFontTypeface { |
| 276 | public: |
| 277 | /** |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 278 | * The created FontMemResourceTypeface takes ownership of fontMemResource. |
| 279 | */ |
| 280 | static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemResource) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 281 | return new FontMemResourceTypeface(get_style(lf), lf, fontMemResource); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 285 | void weak_dispose() const override { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 286 | RemoveFontMemResourceEx(fFontMemResource); |
| 287 | //SkTypefaceCache::Remove(this); |
| 288 | INHERITED::weak_dispose(); |
| 289 | } |
| 290 | |
| 291 | private: |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 292 | /** |
| 293 | * Takes ownership of fontMemResource. |
| 294 | */ |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 295 | FontMemResourceTypeface(const SkFontStyle& style, const LOGFONT& lf, HANDLE fontMemResource) |
| 296 | : LogFontTypeface(style, lf, true), fFontMemResource(fontMemResource) |
| 297 | { } |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 298 | |
| 299 | HANDLE fFontMemResource; |
| 300 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 301 | typedef LogFontTypeface INHERITED; |
| 302 | }; |
| 303 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 304 | static const LOGFONT& get_default_font() { |
| 305 | static LOGFONT gDefaultFont; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 306 | return gDefaultFont; |
| 307 | } |
| 308 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 309 | static bool FindByLogFont(SkTypeface* face, void* ctx) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 310 | LogFontTypeface* lface = static_cast<LogFontTypeface*>(face); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 311 | const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 312 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 313 | return !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT)); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * This guy is public. It first searches the cache, and if a match is not found, |
| 318 | * it creates a new face. |
| 319 | */ |
| 320 | SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) { |
| 321 | LOGFONT lf = origLF; |
| 322 | make_canonical(&lf); |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 323 | SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByLogFont, &lf); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 324 | if (nullptr == face) { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 325 | face = LogFontTypeface::Create(lf); |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 326 | SkTypefaceCache::Add(face); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 327 | } |
| 328 | return face; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 329 | } |
| 330 | |
reed@google.com | db77a6a | 2011-07-19 19:08:33 +0000 | [diff] [blame] | 331 | /** |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 332 | * The created SkTypeface takes ownership of fontMemResource. |
| 333 | */ |
| 334 | SkTypeface* SkCreateFontMemResourceTypefaceFromLOGFONT(const LOGFONT& origLF, HANDLE fontMemResource) { |
| 335 | LOGFONT lf = origLF; |
| 336 | make_canonical(&lf); |
mtklein | 60b6e9d | 2014-10-24 10:43:15 -0700 | [diff] [blame] | 337 | // We'll never get a cache hit, so no point in putting this in SkTypefaceCache. |
| 338 | return FontMemResourceTypeface::Create(lf, fontMemResource); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
reed@google.com | db77a6a | 2011-07-19 19:08:33 +0000 | [diff] [blame] | 342 | * This guy is public |
| 343 | */ |
| 344 | void SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 345 | if (nullptr == face) { |
reed@google.com | db77a6a | 2011-07-19 19:08:33 +0000 | [diff] [blame] | 346 | *lf = get_default_font(); |
| 347 | } else { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 348 | *lf = static_cast<const LogFontTypeface*>(face)->fLogFont; |
reed@google.com | db77a6a | 2011-07-19 19:08:33 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 352 | // Construct Glyph to Unicode table. |
| 353 | // Unicode code points that require conjugate pairs in utf16 are not |
| 354 | // supported. |
| 355 | // TODO(arthurhsu): Add support for conjugate pairs. It looks like that may |
| 356 | // require parsing the TTF cmap table (platform 4, encoding 12) directly instead |
| 357 | // of calling GetFontUnicodeRange(). |
| 358 | static void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount, |
| 359 | SkTDArray<SkUnichar>* glyphToUnicode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 360 | DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, nullptr); |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 361 | if (!glyphSetBufferSize) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | SkAutoTDeleteArray<BYTE> glyphSetBuffer(new BYTE[glyphSetBufferSize]); |
| 366 | GLYPHSET* glyphSet = |
| 367 | reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get()); |
| 368 | if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | glyphToUnicode->setCount(glyphCount); |
| 373 | memset(glyphToUnicode->begin(), 0, glyphCount * sizeof(SkUnichar)); |
| 374 | for (DWORD i = 0; i < glyphSet->cRanges; ++i) { |
| 375 | // There is no guarantee that within a Unicode range, the corresponding |
| 376 | // glyph id in a font file are continuous. So, even if we have ranges, |
| 377 | // we can't just use the first and last entry of the range to compute |
| 378 | // result. We need to enumerate them one by one. |
| 379 | int count = glyphSet->ranges[i].cGlyphs; |
| 380 | SkAutoTArray<WCHAR> chars(count + 1); |
| 381 | chars[count] = 0; // termintate string |
| 382 | SkAutoTArray<WORD> glyph(count); |
| 383 | for (USHORT j = 0; j < count; ++j) { |
| 384 | chars[j] = glyphSet->ranges[i].wcLow + j; |
| 385 | } |
| 386 | GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(), |
| 387 | GGI_MARK_NONEXISTING_GLYPHS); |
| 388 | // If the glyph ID is valid, and the glyph is not mapped, then we will |
| 389 | // fill in the char id into the vector. If the glyph is mapped already, |
| 390 | // skip it. |
| 391 | // TODO(arthurhsu): better improve this. e.g. Get all used char ids from |
| 392 | // font cache, then generate this mapping table from there. It's |
| 393 | // unlikely to have collisions since glyph reuse happens mostly for |
| 394 | // different Unicode pages. |
| 395 | for (USHORT j = 0; j < count; ++j) { |
| 396 | if (glyph[j] != 0xffff && glyph[j] < glyphCount && |
| 397 | (*glyphToUnicode)[glyph[j]] == 0) { |
| 398 | (*glyphToUnicode)[glyph[j]] = chars[j]; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 404 | ////////////////////////////////////////////////////////////////////////////////////// |
| 405 | |
| 406 | static int alignTo32(int n) { |
| 407 | return (n + 31) & ~31; |
| 408 | } |
| 409 | |
| 410 | struct MyBitmapInfo : public BITMAPINFO { |
| 411 | RGBQUAD fMoreSpaceForColors[1]; |
| 412 | }; |
| 413 | |
| 414 | class HDCOffscreen { |
| 415 | public: |
| 416 | HDCOffscreen() { |
| 417 | fFont = 0; |
| 418 | fDC = 0; |
| 419 | fBM = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 420 | fBits = nullptr; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 421 | fWidth = fHeight = 0; |
| 422 | fIsBW = false; |
| 423 | } |
| 424 | |
| 425 | ~HDCOffscreen() { |
| 426 | if (fDC) { |
| 427 | DeleteDC(fDC); |
| 428 | } |
| 429 | if (fBM) { |
| 430 | DeleteObject(fBM); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | void init(HFONT font, const XFORM& xform) { |
| 435 | fFont = font; |
| 436 | fXform = xform; |
| 437 | } |
| 438 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 439 | const void* draw(const SkGlyph&, bool isBW, size_t* srcRBPtr); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 440 | |
| 441 | private: |
| 442 | HDC fDC; |
| 443 | HBITMAP fBM; |
| 444 | HFONT fFont; |
| 445 | XFORM fXform; |
| 446 | void* fBits; // points into fBM |
| 447 | int fWidth; |
| 448 | int fHeight; |
| 449 | bool fIsBW; |
| 450 | }; |
| 451 | |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 452 | const void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW, |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 453 | size_t* srcRBPtr) { |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 454 | // Can we share the scalercontext's fDDC, so we don't need to create |
| 455 | // a separate fDC here? |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 456 | if (0 == fDC) { |
| 457 | fDC = CreateCompatibleDC(0); |
| 458 | if (0 == fDC) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 459 | return nullptr; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 460 | } |
| 461 | SetGraphicsMode(fDC, GM_ADVANCED); |
| 462 | SetBkMode(fDC, TRANSPARENT); |
| 463 | SetTextAlign(fDC, TA_LEFT | TA_BASELINE); |
| 464 | SelectObject(fDC, fFont); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 465 | |
| 466 | COLORREF color = 0x00FFFFFF; |
bsalomon@google.com | b58a639 | 2013-03-21 20:29:05 +0000 | [diff] [blame] | 467 | SkDEBUGCODE(COLORREF prev =) SetTextColor(fDC, color); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 468 | SkASSERT(prev != CLR_INVALID); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (fBM && (fIsBW != isBW || fWidth < glyph.fWidth || fHeight < glyph.fHeight)) { |
| 472 | DeleteObject(fBM); |
| 473 | fBM = 0; |
| 474 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 475 | fIsBW = isBW; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 476 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 477 | fWidth = SkMax32(fWidth, glyph.fWidth); |
| 478 | fHeight = SkMax32(fHeight, glyph.fHeight); |
| 479 | |
| 480 | int biWidth = isBW ? alignTo32(fWidth) : fWidth; |
| 481 | |
| 482 | if (0 == fBM) { |
| 483 | MyBitmapInfo info; |
| 484 | sk_bzero(&info, sizeof(info)); |
| 485 | if (isBW) { |
| 486 | RGBQUAD blackQuad = { 0, 0, 0, 0 }; |
| 487 | RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 }; |
| 488 | info.bmiColors[0] = blackQuad; |
| 489 | info.bmiColors[1] = whiteQuad; |
| 490 | } |
| 491 | info.bmiHeader.biSize = sizeof(info.bmiHeader); |
| 492 | info.bmiHeader.biWidth = biWidth; |
| 493 | info.bmiHeader.biHeight = fHeight; |
| 494 | info.bmiHeader.biPlanes = 1; |
| 495 | info.bmiHeader.biBitCount = isBW ? 1 : 32; |
| 496 | info.bmiHeader.biCompression = BI_RGB; |
| 497 | if (isBW) { |
| 498 | info.bmiHeader.biClrUsed = 2; |
| 499 | } |
| 500 | fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0); |
| 501 | if (0 == fBM) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 502 | return nullptr; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 503 | } |
| 504 | SelectObject(fDC, fBM); |
| 505 | } |
| 506 | |
| 507 | // erase |
| 508 | size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2); |
| 509 | size_t size = fHeight * srcRB; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 510 | memset(fBits, 0, size); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 511 | |
| 512 | XFORM xform = fXform; |
| 513 | xform.eDx = (float)-glyph.fLeft; |
| 514 | xform.eDy = (float)-glyph.fTop; |
| 515 | SetWorldTransform(fDC, &xform); |
| 516 | |
| 517 | uint16_t glyphID = glyph.getGlyphID(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 518 | BOOL ret = ExtTextOutW(fDC, 0, 0, ETO_GLYPH_INDEX, nullptr, reinterpret_cast<LPCWSTR>(&glyphID), 1, nullptr); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 519 | GdiFlush(); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 520 | if (0 == ret) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 521 | return nullptr; |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 522 | } |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 523 | *srcRBPtr = srcRB; |
| 524 | // offset to the start of the image |
| 525 | return (const char*)fBits + (fHeight - glyph.fHeight) * srcRB; |
| 526 | } |
| 527 | |
reed@google.com | b8a5c61 | 2012-06-13 20:01:44 +0000 | [diff] [blame] | 528 | ////////////////////////////////////////////////////////////////////////////// |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 529 | #define BUFFERSIZE (1 << 13) |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 530 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 531 | class SkScalerContext_GDI : public SkScalerContext { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 532 | public: |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 533 | SkScalerContext_GDI(SkTypeface*, const SkScalerContextEffects&, const SkDescriptor* desc); |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 534 | virtual ~SkScalerContext_GDI(); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 535 | |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 536 | // Returns true if the constructor was able to complete all of its |
| 537 | // initializations (which may include calling GDI). |
| 538 | bool isValid() const; |
| 539 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 540 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 541 | unsigned generateGlyphCount() override; |
| 542 | uint16_t generateCharToGlyph(SkUnichar uni) override; |
| 543 | void generateAdvance(SkGlyph* glyph) override; |
| 544 | void generateMetrics(SkGlyph* glyph) override; |
| 545 | void generateImage(const SkGlyph& glyph) override; |
| 546 | void generatePath(const SkGlyph& glyph, SkPath* path) override; |
| 547 | void generateFontMetrics(SkPaint::FontMetrics*) override; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 548 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 549 | private: |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 550 | DWORD getGDIGlyphPath(const SkGlyph& glyph, UINT flags, |
| 551 | SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf); |
| 552 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 553 | HDCOffscreen fOffscreen; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 554 | /** fGsA is the non-rotational part of total matrix without the text height scale. |
| 555 | * Used to find the magnitude of advances. |
| 556 | */ |
| 557 | MAT2 fGsA; |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 558 | /** The total matrix without the textSize. */ |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 559 | MAT2 fMat22; |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 560 | /** Scales font to EM size. */ |
| 561 | MAT2 fHighResMat22; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 562 | HDC fDDC; |
| 563 | HFONT fSavefont; |
| 564 | HFONT fFont; |
| 565 | SCRIPT_CACHE fSC; |
| 566 | int fGlyphCount; |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 567 | |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 568 | /** The total matrix which also removes EM scale. */ |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 569 | SkMatrix fHiResMatrix; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 570 | /** fG_inv is the inverse of the rotational part of the total matrix. |
| 571 | * Used to set the direction of advances. |
| 572 | */ |
| 573 | SkMatrix fG_inv; |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 574 | enum Type { |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 575 | kTrueType_Type, kBitmap_Type, kLine_Type |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 576 | } fType; |
| 577 | TEXTMETRIC fTM; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 578 | }; |
| 579 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 580 | static FIXED float2FIXED(float x) { |
| 581 | return SkFixedToFIXED(SkFloatToFixed(x)); |
| 582 | } |
| 583 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 584 | static inline float FIXED2float(FIXED x) { |
| 585 | return SkFixedToFloat(SkFIXEDToFixed(x)); |
| 586 | } |
| 587 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 588 | static BYTE compute_quality(const SkScalerContext::Rec& rec) { |
| 589 | switch (rec.fMaskFormat) { |
| 590 | case SkMask::kBW_Format: |
| 591 | return NONANTIALIASED_QUALITY; |
| 592 | case SkMask::kLCD16_Format: |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 593 | return CLEARTYPE_QUALITY; |
| 594 | default: |
reed@google.com | 8351aab | 2012-01-18 17:06:35 +0000 | [diff] [blame] | 595 | if (rec.fFlags & SkScalerContext::kGenA8FromLCD_Flag) { |
| 596 | return CLEARTYPE_QUALITY; |
| 597 | } else { |
| 598 | return ANTIALIASED_QUALITY; |
| 599 | } |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 603 | SkScalerContext_GDI::SkScalerContext_GDI(SkTypeface* rawTypeface, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 604 | const SkScalerContextEffects& effects, |
| 605 | const SkDescriptor* desc) |
| 606 | : SkScalerContext(rawTypeface, effects, desc) |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 607 | , fDDC(0) |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 608 | , fSavefont(0) |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 609 | , fFont(0) |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 610 | , fSC(0) |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 611 | , fGlyphCount(-1) |
| 612 | { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 613 | LogFontTypeface* typeface = reinterpret_cast<LogFontTypeface*>(rawTypeface); |
| 614 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 615 | fDDC = ::CreateCompatibleDC(nullptr); |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 616 | if (!fDDC) { |
| 617 | return; |
| 618 | } |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 619 | SetGraphicsMode(fDDC, GM_ADVANCED); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 620 | SetBkMode(fDDC, TRANSPARENT); |
skia.committer@gmail.com | 27e21fe | 2013-07-28 07:01:03 +0000 | [diff] [blame] | 621 | |
bungeman | 6f94076 | 2015-03-18 08:25:43 -0700 | [diff] [blame] | 622 | // When GDI hinting, remove the entire Y scale from sA and GsA. (Prevents 'linear' metrics.) |
| 623 | // When not hinting, remove only the integer Y scale from sA and GsA. (Applied by GDI.) |
bungeman | 5f14c5e | 2014-12-05 12:26:44 -0800 | [diff] [blame] | 624 | SkScalerContextRec::PreMatrixScale scaleConstraints = |
| 625 | (fRec.getHinting() == SkPaint::kNo_Hinting || fRec.getHinting() == SkPaint::kSlight_Hinting) |
| 626 | ? SkScalerContextRec::kVerticalInteger_PreMatrixScale |
| 627 | : SkScalerContextRec::kVertical_PreMatrixScale; |
| 628 | SkVector scale; |
| 629 | SkMatrix sA; |
| 630 | SkMatrix GsA; |
| 631 | SkMatrix A; |
| 632 | fRec.computeMatrices(scaleConstraints, &scale, &sA, &GsA, &fG_inv, &A); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 633 | |
| 634 | fGsA.eM11 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleX)); |
| 635 | fGsA.eM12 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewY)); // This should be ~0. |
| 636 | fGsA.eM21 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewX)); |
| 637 | fGsA.eM22 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleY)); |
| 638 | |
bungeman | 6f94076 | 2015-03-18 08:25:43 -0700 | [diff] [blame] | 639 | // When not hinting, scale was computed with kVerticalInteger, so is already an integer. |
| 640 | // The sA and GsA transforms will be used to create 'linear' metrics. |
| 641 | |
| 642 | // When hinting, scale was computed with kVertical, stating that our port can handle |
| 643 | // non-integer scales. This is done so that sA and GsA are computed without any 'residual' |
| 644 | // scale in them, preventing 'linear' metrics. However, GDI cannot actually handle non-integer |
| 645 | // scales so we need to round in this case. This is fine, since all of the scale has been |
| 646 | // removed from sA and GsA, so GDI will be handling the scale completely. |
| 647 | SkScalar gdiTextSize = SkScalarRoundToScalar(scale.fY); |
| 648 | |
| 649 | // GDI will not accept a size of zero, so round the range [0, 1] to 1. |
| 650 | // If the size was non-zero, the scale factors will also be non-zero and 1px tall text is drawn. |
| 651 | // If the size actually was zero, the scale factors will also be zero, so GDI will draw nothing. |
bungeman | 5f14c5e | 2014-12-05 12:26:44 -0800 | [diff] [blame] | 652 | if (gdiTextSize == 0) { |
| 653 | gdiTextSize = SK_Scalar1; |
| 654 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 655 | |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 656 | LOGFONT lf = typeface->fLogFont; |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 657 | lf.lfHeight = -SkScalarTruncToInt(gdiTextSize); |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 658 | lf.lfQuality = compute_quality(fRec); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 659 | fFont = CreateFontIndirect(&lf); |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 660 | if (!fFont) { |
| 661 | return; |
| 662 | } |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 663 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 664 | fSavefont = (HFONT)SelectObject(fDDC, fFont); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 665 | |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 666 | if (0 == GetTextMetrics(fDDC, &fTM)) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 667 | call_ensure_accessible(lf); |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 668 | if (0 == GetTextMetrics(fDDC, &fTM)) { |
| 669 | fTM.tmPitchAndFamily = TMPF_TRUETYPE; |
| 670 | } |
| 671 | } |
bungeman@google.com | 90b7e38 | 2012-04-20 15:26:28 +0000 | [diff] [blame] | 672 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 673 | XFORM xform; |
bungeman@google.com | 90b7e38 | 2012-04-20 15:26:28 +0000 | [diff] [blame] | 674 | if (fTM.tmPitchAndFamily & TMPF_VECTOR) { |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 675 | // Used a logfont on a memory context, should never get a device font. |
| 676 | // Therefore all TMPF_DEVICE will be PostScript fonts. |
| 677 | |
| 678 | // If TMPF_VECTOR is set, one of TMPF_TRUETYPE or TMPF_DEVICE means that |
| 679 | // we have an outline font. Otherwise we have a vector FON, which is |
| 680 | // scalable, but not an outline font. |
| 681 | // This was determined by testing with Type1 PFM/PFB and |
| 682 | // OpenTypeCFF OTF, as well as looking at Wine bugs and sources. |
| 683 | if (fTM.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_DEVICE)) { |
| 684 | // Truetype or PostScript. |
| 685 | fType = SkScalerContext_GDI::kTrueType_Type; |
| 686 | } else { |
| 687 | // Stroked FON. |
| 688 | fType = SkScalerContext_GDI::kLine_Type; |
| 689 | } |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 690 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 691 | // fPost2x2 is column-major, left handed (y down). |
| 692 | // XFORM 2x2 is row-major, left handed (y down). |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 693 | xform.eM11 = SkScalarToFloat(sA.get(SkMatrix::kMScaleX)); |
| 694 | xform.eM12 = SkScalarToFloat(sA.get(SkMatrix::kMSkewY)); |
| 695 | xform.eM21 = SkScalarToFloat(sA.get(SkMatrix::kMSkewX)); |
| 696 | xform.eM22 = SkScalarToFloat(sA.get(SkMatrix::kMScaleY)); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 697 | xform.eDx = 0; |
| 698 | xform.eDy = 0; |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 699 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 700 | // MAT2 is row major, right handed (y up). |
| 701 | fMat22.eM11 = float2FIXED(xform.eM11); |
| 702 | fMat22.eM12 = float2FIXED(-xform.eM12); |
| 703 | fMat22.eM21 = float2FIXED(-xform.eM21); |
| 704 | fMat22.eM22 = float2FIXED(xform.eM22); |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 705 | |
| 706 | if (needToRenderWithSkia(fRec)) { |
| 707 | this->forceGenerateImageFromPath(); |
| 708 | } |
| 709 | |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 710 | // Create a hires matrix if we need linear metrics. |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 711 | if (this->isSubpixel()) { |
| 712 | OUTLINETEXTMETRIC otm; |
| 713 | UINT success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 714 | if (0 == success) { |
| 715 | call_ensure_accessible(lf); |
| 716 | success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 717 | } |
| 718 | if (0 != success) { |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 719 | SkScalar upem = SkIntToScalar(otm.otmEMSquare); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 720 | |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 721 | SkScalar gdiTextSizeToEMScale = upem / gdiTextSize; |
| 722 | fHighResMat22.eM11 = float2FIXED(gdiTextSizeToEMScale); |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 723 | fHighResMat22.eM12 = float2FIXED(0); |
| 724 | fHighResMat22.eM21 = float2FIXED(0); |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 725 | fHighResMat22.eM22 = float2FIXED(gdiTextSizeToEMScale); |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 726 | |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 727 | SkScalar removeEMScale = SkScalarInvert(upem); |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 728 | fHiResMatrix = A; |
bungeman@google.com | 11ba319 | 2013-10-03 20:17:51 +0000 | [diff] [blame] | 729 | fHiResMatrix.preScale(removeEMScale, removeEMScale); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 733 | } else { |
| 734 | // Assume bitmap |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 735 | fType = SkScalerContext_GDI::kBitmap_Type; |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 736 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 737 | xform.eM11 = 1.0f; |
| 738 | xform.eM12 = 0.0f; |
| 739 | xform.eM21 = 0.0f; |
| 740 | xform.eM22 = 1.0f; |
| 741 | xform.eDx = 0.0f; |
| 742 | xform.eDy = 0.0f; |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 743 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 744 | // fPost2x2 is column-major, left handed (y down). |
| 745 | // MAT2 is row major, right handed (y up). |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 746 | fMat22.eM11 = SkScalarToFIXED(fRec.fPost2x2[0][0]); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 747 | fMat22.eM12 = SkScalarToFIXED(-fRec.fPost2x2[1][0]); |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 748 | fMat22.eM21 = SkScalarToFIXED(-fRec.fPost2x2[0][1]); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 749 | fMat22.eM22 = SkScalarToFIXED(fRec.fPost2x2[1][1]); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 750 | } |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 751 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 752 | fOffscreen.init(fFont, xform); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 753 | } |
| 754 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 755 | SkScalerContext_GDI::~SkScalerContext_GDI() { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 756 | if (fDDC) { |
| 757 | ::SelectObject(fDDC, fSavefont); |
| 758 | ::DeleteDC(fDDC); |
| 759 | } |
| 760 | if (fFont) { |
| 761 | ::DeleteObject(fFont); |
| 762 | } |
| 763 | if (fSC) { |
| 764 | ::ScriptFreeCache(&fSC); |
| 765 | } |
| 766 | } |
| 767 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 768 | bool SkScalerContext_GDI::isValid() const { |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 769 | return fDDC && fFont; |
| 770 | } |
| 771 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 772 | unsigned SkScalerContext_GDI::generateGlyphCount() { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 773 | if (fGlyphCount < 0) { |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 774 | fGlyphCount = calculateGlyphCount( |
| 775 | fDDC, static_cast<const LogFontTypeface*>(this->getTypeface())->fLogFont); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 776 | } |
| 777 | return fGlyphCount; |
| 778 | } |
| 779 | |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 780 | uint16_t SkScalerContext_GDI::generateCharToGlyph(SkUnichar utf32) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 781 | uint16_t index = 0; |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 782 | WCHAR utf16[2]; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 783 | // TODO(ctguil): Support characters that generate more than one glyph. |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 784 | if (SkUTF16_FromUnichar(utf32, (uint16_t*)utf16) == 1) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 785 | // Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0. |
skia.committer@gmail.com | 7bd141d | 2013-08-28 07:01:18 +0000 | [diff] [blame] | 786 | |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 787 | /** Real documentation for GetGlyphIndiciesW: |
| 788 | * |
| 789 | * When GGI_MARK_NONEXISTING_GLYPHS is not specified and a character does not map to a |
| 790 | * glyph, then the 'default character's glyph is returned instead. The 'default character' |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 791 | * is available in fTM.tmDefaultChar. FON fonts have a default character, and there exists |
| 792 | * a usDefaultChar in the 'OS/2' table, version 2 and later. If there is no |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 793 | * 'default character' specified by the font, then often the first character found is used. |
| 794 | * |
| 795 | * When GGI_MARK_NONEXISTING_GLYPHS is specified and a character does not map to a glyph, |
| 796 | * then the glyph 0xFFFF is used. In Windows XP and earlier, Bitmap/Vector FON usually use |
| 797 | * glyph 0x1F instead ('Terminal' appears to be special, returning 0xFFFF). |
| 798 | * Type1 PFM/PFB, TT, OT TT, OT CFF all appear to use 0xFFFF, even on XP. |
| 799 | */ |
| 800 | DWORD result = GetGlyphIndicesW(fDDC, utf16, 1, &index, GGI_MARK_NONEXISTING_GLYPHS); |
| 801 | if (result == GDI_ERROR |
| 802 | || 0xFFFF == index |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 803 | || (0x1F == index && |
| 804 | (fType == SkScalerContext_GDI::kBitmap_Type || |
| 805 | fType == SkScalerContext_GDI::kLine_Type) |
| 806 | /*&& winVer < Vista */) |
| 807 | ) |
bungeman@google.com | 3334648 | 2013-08-27 19:05:32 +0000 | [diff] [blame] | 808 | { |
| 809 | index = 0; |
| 810 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 811 | } else { |
| 812 | // Use uniscribe to detemine glyph index for non-BMP characters. |
bungeman@google.com | 27f74aa | 2013-10-08 21:32:15 +0000 | [diff] [blame] | 813 | static const int numWCHAR = 2; |
| 814 | static const int maxItems = 2; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 815 | // MSDN states that this can be nullptr, but some things don't work then. |
bungeman@google.com | 27f74aa | 2013-10-08 21:32:15 +0000 | [diff] [blame] | 816 | SCRIPT_CONTROL sc = { 0 }; |
| 817 | // Add extra item to SCRIPT_ITEM to work around a bug (now documented). |
| 818 | // https://bugzilla.mozilla.org/show_bug.cgi?id=366643 |
| 819 | SCRIPT_ITEM si[maxItems + 1]; |
| 820 | int numItems; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 821 | HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &sc, nullptr, si, &numItems), |
bungeman@google.com | 27f74aa | 2013-10-08 21:32:15 +0000 | [diff] [blame] | 822 | "Could not itemize character."); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 823 | |
bungeman@google.com | 27f74aa | 2013-10-08 21:32:15 +0000 | [diff] [blame] | 824 | // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs. |
| 825 | static const int maxGlyphs = 2; |
| 826 | SCRIPT_VISATTR vsa[maxGlyphs]; |
| 827 | WORD outGlyphs[maxGlyphs]; |
| 828 | WORD logClust[numWCHAR]; |
| 829 | int numGlyphs; |
| 830 | HRZM(ScriptShape(fDDC, &fSC, utf16, numWCHAR, maxGlyphs, &si[0].a, |
| 831 | outGlyphs, logClust, vsa, &numGlyphs), |
| 832 | "Could not shape character."); |
| 833 | if (1 == numGlyphs) { |
| 834 | index = outGlyphs[0]; |
| 835 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 836 | } |
| 837 | return index; |
| 838 | } |
| 839 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 840 | void SkScalerContext_GDI::generateAdvance(SkGlyph* glyph) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 841 | this->generateMetrics(glyph); |
| 842 | } |
| 843 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 844 | void SkScalerContext_GDI::generateMetrics(SkGlyph* glyph) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 845 | SkASSERT(fDDC); |
| 846 | |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 847 | if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) { |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 848 | SIZE size; |
djsollen | 1b27704 | 2014-08-06 06:58:06 -0700 | [diff] [blame] | 849 | WORD glyphs = glyph->getGlyphID(); |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 850 | if (0 == GetTextExtentPointI(fDDC, &glyphs, 1, &size)) { |
| 851 | glyph->fWidth = SkToS16(fTM.tmMaxCharWidth); |
| 852 | } else { |
| 853 | glyph->fWidth = SkToS16(size.cx); |
| 854 | } |
| 855 | glyph->fHeight = SkToS16(size.cy); |
| 856 | |
| 857 | glyph->fTop = SkToS16(-fTM.tmAscent); |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 858 | // Bitmap FON cannot underhang, but vector FON may. |
| 859 | // There appears no means of determining underhang of vector FON. |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 860 | glyph->fLeft = SkToS16(0); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 861 | glyph->fAdvanceX = glyph->fWidth; |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 862 | glyph->fAdvanceY = 0; |
| 863 | |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 864 | // Vector FON will transform nicely, but bitmap FON do not. |
| 865 | if (fType == SkScalerContext_GDI::kLine_Type) { |
| 866 | SkRect bounds = SkRect::MakeXYWH(glyph->fLeft, glyph->fTop, |
| 867 | glyph->fWidth, glyph->fHeight); |
| 868 | SkMatrix m; |
| 869 | m.setAll(SkFIXEDToScalar(fMat22.eM11), -SkFIXEDToScalar(fMat22.eM21), 0, |
| 870 | -SkFIXEDToScalar(fMat22.eM12), SkFIXEDToScalar(fMat22.eM22), 0, |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 871 | 0, 0, 1); |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 872 | m.mapRect(&bounds); |
reed | d02cf26 | 2014-11-18 18:06:45 -0800 | [diff] [blame] | 873 | bounds.roundOut(&bounds); |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 874 | glyph->fLeft = SkScalarTruncToInt(bounds.fLeft); |
| 875 | glyph->fTop = SkScalarTruncToInt(bounds.fTop); |
| 876 | glyph->fWidth = SkScalarTruncToInt(bounds.width()); |
| 877 | glyph->fHeight = SkScalarTruncToInt(bounds.height()); |
| 878 | } |
| 879 | |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 880 | // Apply matrix to advance. |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 881 | glyph->fAdvanceY = -FIXED2float(fMat22.eM12) * glyph->fAdvanceX; |
| 882 | glyph->fAdvanceX *= FIXED2float(fMat22.eM11); |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 883 | |
| 884 | return; |
| 885 | } |
| 886 | |
djsollen | 1b27704 | 2014-08-06 06:58:06 -0700 | [diff] [blame] | 887 | UINT glyphId = glyph->getGlyphID(); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 888 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 889 | GLYPHMETRICS gm; |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 890 | sk_bzero(&gm, sizeof(gm)); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 891 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 892 | DWORD status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 893 | if (GDI_ERROR == status) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 894 | LogFontTypeface::EnsureAccessible(this->getTypeface()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 895 | status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 896 | if (GDI_ERROR == status) { |
| 897 | glyph->zeroMetrics(); |
| 898 | return; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | bool empty = false; |
| 903 | // The black box is either the embedded bitmap size or the outline extent. |
| 904 | // It is 1x1 if nothing is to be drawn, but will also be 1x1 if something very small |
| 905 | // is to be drawn, like a '.'. We need to outset '.' but do not wish to outset ' '. |
| 906 | if (1 == gm.gmBlackBoxX && 1 == gm.gmBlackBoxY) { |
| 907 | // If GetGlyphOutline with GGO_NATIVE returns 0, we know there was no outline. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 908 | DWORD bufferSize = GetGlyphOutlineW(fDDC, glyphId, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 909 | empty = (0 == bufferSize); |
| 910 | } |
| 911 | |
| 912 | glyph->fTop = SkToS16(-gm.gmptGlyphOrigin.y); |
| 913 | glyph->fLeft = SkToS16(gm.gmptGlyphOrigin.x); |
| 914 | if (empty) { |
| 915 | glyph->fWidth = 0; |
| 916 | glyph->fHeight = 0; |
| 917 | } else { |
| 918 | // Outset, since the image may bleed out of the black box. |
| 919 | // For embedded bitmaps the black box should be exact. |
| 920 | // For outlines we need to outset by 1 in all directions for bleed. |
| 921 | // For ClearType we need to outset by 2 for bleed. |
| 922 | glyph->fWidth = gm.gmBlackBoxX + 4; |
| 923 | glyph->fHeight = gm.gmBlackBoxY + 4; |
| 924 | glyph->fTop -= 2; |
| 925 | glyph->fLeft -= 2; |
| 926 | } |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 927 | // TODO(benjaminwagner): What is the type of gm.gmCellInc[XY]? |
| 928 | glyph->fAdvanceX = (float)((int)gm.gmCellIncX); |
| 929 | glyph->fAdvanceY = (float)((int)gm.gmCellIncY); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 930 | glyph->fRsbDelta = 0; |
| 931 | glyph->fLsbDelta = 0; |
| 932 | |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 933 | if (this->isSubpixel()) { |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 934 | sk_bzero(&gm, sizeof(gm)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 935 | status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fHighResMat22); |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 936 | if (GDI_ERROR != status) { |
| 937 | SkPoint advance; |
| 938 | fHiResMatrix.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 939 | glyph->fAdvanceX = SkScalarToFloat(advance.fX); |
| 940 | glyph->fAdvanceY = SkScalarToFloat(advance.fY); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 941 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 942 | } else if (!isAxisAligned(this->fRec)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 943 | status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fGsA); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 944 | if (GDI_ERROR != status) { |
| 945 | SkPoint advance; |
| 946 | fG_inv.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 947 | glyph->fAdvanceX = SkScalarToFloat(advance.fX); |
| 948 | glyph->fAdvanceY = SkScalarToFloat(advance.fY); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 949 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
bungeman@google.com | 6a774a1 | 2013-07-30 01:07:48 +0000 | [diff] [blame] | 953 | static const MAT2 gMat2Identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 954 | void SkScalerContext_GDI::generateFontMetrics(SkPaint::FontMetrics* metrics) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 955 | if (nullptr == metrics) { |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 956 | return; |
reed@google.com | 60af92c | 2013-05-08 14:11:28 +0000 | [diff] [blame] | 957 | } |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 958 | sk_bzero(metrics, sizeof(*metrics)); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 959 | |
| 960 | SkASSERT(fDDC); |
| 961 | |
bungeman@google.com | e9d8319 | 2013-06-21 05:31:38 +0000 | [diff] [blame] | 962 | #ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
bungeman@google.com | 4732df6 | 2014-01-23 15:22:42 +0000 | [diff] [blame] | 963 | if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) { |
bungeman@google.com | e9d8319 | 2013-06-21 05:31:38 +0000 | [diff] [blame] | 964 | #endif |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 965 | metrics->fTop = SkIntToScalar(-fTM.tmAscent); |
| 966 | metrics->fAscent = SkIntToScalar(-fTM.tmAscent); |
| 967 | metrics->fDescent = SkIntToScalar(fTM.tmDescent); |
| 968 | metrics->fBottom = SkIntToScalar(fTM.tmDescent); |
| 969 | metrics->fLeading = SkIntToScalar(fTM.tmExternalLeading); |
| 970 | metrics->fAvgCharWidth = SkIntToScalar(fTM.tmAveCharWidth); |
| 971 | metrics->fMaxCharWidth = SkIntToScalar(fTM.tmMaxCharWidth); |
| 972 | metrics->fXMin = 0; |
| 973 | metrics->fXMax = metrics->fMaxCharWidth; |
| 974 | //metrics->fXHeight = 0; |
bungeman@google.com | e9d8319 | 2013-06-21 05:31:38 +0000 | [diff] [blame] | 975 | #ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 976 | return; |
| 977 | } |
bungeman@google.com | e9d8319 | 2013-06-21 05:31:38 +0000 | [diff] [blame] | 978 | #endif |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 979 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 980 | OUTLINETEXTMETRIC otm; |
| 981 | |
| 982 | uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 983 | if (0 == ret) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 984 | LogFontTypeface::EnsureAccessible(this->getTypeface()); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 985 | ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 986 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 987 | if (0 == ret) { |
bungeman@google.com | a0319f6 | 2012-04-18 15:40:50 +0000 | [diff] [blame] | 988 | return; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 989 | } |
| 990 | |
bungeman@google.com | e9d8319 | 2013-06-21 05:31:38 +0000 | [diff] [blame] | 991 | #ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 992 | metrics->fTop = SkIntToScalar(-otm.otmrcFontBox.top); |
| 993 | metrics->fAscent = SkIntToScalar(-otm.otmAscent); |
| 994 | metrics->fDescent = SkIntToScalar(-otm.otmDescent); |
| 995 | metrics->fBottom = SkIntToScalar(-otm.otmrcFontBox.bottom); |
| 996 | metrics->fLeading = SkIntToScalar(otm.otmLineGap); |
| 997 | metrics->fAvgCharWidth = SkIntToScalar(otm.otmTextMetrics.tmAveCharWidth); |
| 998 | metrics->fMaxCharWidth = SkIntToScalar(otm.otmTextMetrics.tmMaxCharWidth); |
| 999 | metrics->fXMin = SkIntToScalar(otm.otmrcFontBox.left); |
| 1000 | metrics->fXMax = SkIntToScalar(otm.otmrcFontBox.right); |
commit-bot@chromium.org | d3031aa | 2014-05-14 14:54:51 +0000 | [diff] [blame] | 1001 | #endif |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1002 | metrics->fUnderlineThickness = SkIntToScalar(otm.otmsUnderscoreSize); |
| 1003 | metrics->fUnderlinePosition = -SkIntToScalar(otm.otmsUnderscorePosition); |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1004 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1005 | metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag; |
| 1006 | metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag; |
bungeman@google.com | 4a867a6 | 2014-05-22 17:59:21 +0000 | [diff] [blame] | 1007 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1008 | metrics->fXHeight = SkIntToScalar(otm.otmsXHeight); |
| 1009 | GLYPHMETRICS gm; |
| 1010 | sk_bzero(&gm, sizeof(gm)); |
| 1011 | DWORD len = GetGlyphOutlineW(fDDC, 'x', GGO_METRICS, &gm, 0, 0, &gMat2Identity); |
| 1012 | if (len != GDI_ERROR && gm.gmBlackBoxY > 0) { |
| 1013 | metrics->fXHeight = SkIntToScalar(gm.gmBlackBoxY); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1017 | //////////////////////////////////////////////////////////////////////////////////////// |
| 1018 | |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1019 | #define SK_SHOW_TEXT_BLIT_COVERAGE 0 |
| 1020 | |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1021 | static void build_power_table(uint8_t table[], float ee) { |
| 1022 | for (int i = 0; i < 256; i++) { |
| 1023 | float x = i / 255.f; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1024 | x = sk_float_pow(x, ee); |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 1025 | int xx = SkScalarRoundToInt(x * 255); |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1026 | table[i] = SkToU8(xx); |
| 1027 | } |
| 1028 | } |
| 1029 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1030 | /** |
| 1031 | * This will invert the gamma applied by GDI (gray-scale antialiased), so we |
| 1032 | * can get linear values. |
| 1033 | * |
| 1034 | * GDI grayscale appears to use a hard-coded gamma of 2.3. |
| 1035 | * |
| 1036 | * GDI grayscale appears to draw using the black and white rasterizer at four |
| 1037 | * times the size and then downsamples to compute the coverage mask. As a |
| 1038 | * result there are only seventeen total grays. This lack of fidelity means |
| 1039 | * that shifting into other color spaces is imprecise. |
| 1040 | */ |
| 1041 | static const uint8_t* getInverseGammaTableGDI() { |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1042 | // Since build_power_table is idempotent, many threads can build gTableGdi |
| 1043 | // simultaneously. |
| 1044 | |
| 1045 | // Microsoft Specific: |
| 1046 | // Making gInited volatile provides read-aquire and write-release in vc++. |
| 1047 | // In VS2012, see compiler option /volatile:(ms|iso). |
| 1048 | // Replace with C++11 atomics when possible. |
| 1049 | static volatile bool gInited; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1050 | static uint8_t gTableGdi[256]; |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1051 | if (gInited) { |
| 1052 | // Need a L/L (read) barrier (full acquire not needed). If gInited is observed |
| 1053 | // true then gTableGdi is observable, but it must be requested. |
| 1054 | } else { |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1055 | build_power_table(gTableGdi, 2.3f); |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1056 | // Need a S/S (write) barrier (full release not needed) here so that this |
| 1057 | // write to gInited becomes observable after gTableGdi. |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1058 | gInited = true; |
| 1059 | } |
| 1060 | return gTableGdi; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * This will invert the gamma applied by GDI ClearType, so we can get linear |
| 1065 | * values. |
| 1066 | * |
| 1067 | * GDI ClearType uses SPI_GETFONTSMOOTHINGCONTRAST / 1000 as the gamma value. |
| 1068 | * If this value is not specified, the default is a gamma of 1.4. |
| 1069 | */ |
| 1070 | static const uint8_t* getInverseGammaTableClearType() { |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1071 | // We don't expect SPI_GETFONTSMOOTHINGCONTRAST to ever change, so building |
| 1072 | // gTableClearType with build_power_table is effectively idempotent. |
| 1073 | |
| 1074 | // Microsoft Specific: |
| 1075 | // Making gInited volatile provides read-aquire and write-release in vc++. |
| 1076 | // In VS2012, see compiler option /volatile:(ms|iso). |
| 1077 | // Replace with C++11 atomics when possible. |
| 1078 | static volatile bool gInited; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1079 | static uint8_t gTableClearType[256]; |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1080 | if (gInited) { |
| 1081 | // Need a L/L (read) barrier (acquire not needed). If gInited is observed |
| 1082 | // true then gTableClearType is observable, but it must be requested. |
| 1083 | } else { |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1084 | UINT level = 0; |
| 1085 | if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &level, 0) || !level) { |
| 1086 | // can't get the data, so use a default |
| 1087 | level = 1400; |
| 1088 | } |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1089 | build_power_table(gTableClearType, level / 1000.0f); |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1090 | // Need a S/S (write) barrier (release not needed) here so that this |
| 1091 | // write to gInited becomes observable after gTableClearType. |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1092 | gInited = true; |
| 1093 | } |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1094 | return gTableClearType; |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1097 | #include "SkColorPriv.h" |
| 1098 | |
bungeman@google.com | 6385314 | 2012-08-01 15:36:46 +0000 | [diff] [blame] | 1099 | //Cannot assume that the input rgb is gray due to possible setting of kGenA8FromLCD_Flag. |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1100 | template<bool APPLY_PREBLEND> |
| 1101 | static inline uint8_t rgb_to_a8(SkGdiRGB rgb, const uint8_t* table8) { |
bungeman@google.com | 6385314 | 2012-08-01 15:36:46 +0000 | [diff] [blame] | 1102 | U8CPU r = (rgb >> 16) & 0xFF; |
| 1103 | U8CPU g = (rgb >> 8) & 0xFF; |
| 1104 | U8CPU b = (rgb >> 0) & 0xFF; |
bungeman@google.com | 1bfe01d | 2012-08-28 16:02:42 +0000 | [diff] [blame] | 1105 | return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8); |
reed@google.com | 8351aab | 2012-01-18 17:06:35 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1108 | template<bool APPLY_PREBLEND> |
| 1109 | static inline uint16_t rgb_to_lcd16(SkGdiRGB rgb, const uint8_t* tableR, |
| 1110 | const uint8_t* tableG, |
| 1111 | const uint8_t* tableB) { |
| 1112 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR); |
| 1113 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG); |
| 1114 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1115 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1116 | r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10); |
| 1117 | #endif |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1118 | return SkPack888ToRGB16(r, g, b); |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
reed@google.com | 82cff02 | 2011-09-22 14:33:40 +0000 | [diff] [blame] | 1121 | // Is this GDI color neither black nor white? If so, we have to keep this |
| 1122 | // image as is, rather than smashing it down to a BW mask. |
| 1123 | // |
| 1124 | // returns int instead of bool, since we don't want/have to pay to convert |
| 1125 | // the zero/non-zero value into a bool |
| 1126 | static int is_not_black_or_white(SkGdiRGB c) { |
| 1127 | // same as (but faster than) |
| 1128 | // c &= 0x00FFFFFF; |
| 1129 | // return 0 == c || 0x00FFFFFF == c; |
| 1130 | return (c + (c & 1)) & 0x00FFFFFF; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 1133 | static bool is_rgb_really_bw(const SkGdiRGB* src, int width, int height, size_t srcRB) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1134 | for (int y = 0; y < height; ++y) { |
| 1135 | for (int x = 0; x < width; ++x) { |
reed@google.com | 82cff02 | 2011-09-22 14:33:40 +0000 | [diff] [blame] | 1136 | if (is_not_black_or_white(src[x])) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
| 1139 | } |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1140 | src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1141 | } |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1145 | // gdi's bitmap is upside-down, so we reverse dst walking in Y |
| 1146 | // whenever we copy it into skia's buffer |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1147 | static void rgb_to_bw(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1148 | const SkGlyph& glyph) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1149 | const int width = glyph.fWidth; |
| 1150 | const size_t dstRB = (width + 7) >> 3; |
| 1151 | uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 1152 | |
| 1153 | int byteCount = width >> 3; |
| 1154 | int bitCount = width & 7; |
| 1155 | |
| 1156 | // adjust srcRB to skip the values in our byteCount loop, |
| 1157 | // since we increment src locally there |
| 1158 | srcRB -= byteCount * 8 * sizeof(SkGdiRGB); |
| 1159 | |
| 1160 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1161 | if (byteCount > 0) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1162 | for (int i = 0; i < byteCount; ++i) { |
reed@google.com | 7a23014 | 2011-09-27 14:07:21 +0000 | [diff] [blame] | 1163 | unsigned byte = 0; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1164 | byte |= src[0] & (1 << 7); |
| 1165 | byte |= src[1] & (1 << 6); |
| 1166 | byte |= src[2] & (1 << 5); |
| 1167 | byte |= src[3] & (1 << 4); |
| 1168 | byte |= src[4] & (1 << 3); |
| 1169 | byte |= src[5] & (1 << 2); |
| 1170 | byte |= src[6] & (1 << 1); |
| 1171 | byte |= src[7] & (1 << 0); |
reed@google.com | 6a8f14d | 2011-09-27 12:54:24 +0000 | [diff] [blame] | 1172 | dst[i] = byte; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1173 | src += 8; |
| 1174 | } |
| 1175 | } |
| 1176 | if (bitCount > 0) { |
| 1177 | unsigned byte = 0; |
| 1178 | unsigned mask = 0x80; |
| 1179 | for (int i = 0; i < bitCount; i++) { |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1180 | byte |= src[i] & mask; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1181 | mask >>= 1; |
| 1182 | } |
| 1183 | dst[byteCount] = byte; |
| 1184 | } |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1185 | src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1186 | dst -= dstRB; |
| 1187 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1188 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1189 | if (glyph.fWidth > 0 && glyph.fHeight > 0) { |
| 1190 | uint8_t* first = (uint8_t*)glyph.fImage; |
| 1191 | uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1); |
| 1192 | *first |= 1 << 7; |
| 1193 | *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount); |
| 1194 | } |
| 1195 | #endif |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1198 | template<bool APPLY_PREBLEND> |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1199 | static void rgb_to_a8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1200 | const SkGlyph& glyph, const uint8_t* table8) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1201 | const size_t dstRB = glyph.rowBytes(); |
| 1202 | const int width = glyph.fWidth; |
| 1203 | uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 1204 | |
| 1205 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1206 | for (int i = 0; i < width; i++) { |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1207 | dst[i] = rgb_to_a8<APPLY_PREBLEND>(src[i], table8); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1208 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1209 | dst[i] = SkMax32(dst[i], 10); |
| 1210 | #endif |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1211 | } |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1212 | src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1213 | dst -= dstRB; |
| 1214 | } |
| 1215 | } |
| 1216 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1217 | template<bool APPLY_PREBLEND> |
| 1218 | static void rgb_to_lcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph, |
| 1219 | const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1220 | const size_t dstRB = glyph.rowBytes(); |
| 1221 | const int width = glyph.fWidth; |
| 1222 | uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 1223 | |
| 1224 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1225 | for (int i = 0; i < width; i++) { |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1226 | dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(src[i], tableR, tableG, tableB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1227 | } |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1228 | src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1229 | dst = (uint16_t*)((char*)dst - dstRB); |
| 1230 | } |
| 1231 | } |
| 1232 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 1233 | void SkScalerContext_GDI::generateImage(const SkGlyph& glyph) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1234 | SkASSERT(fDDC); |
| 1235 | |
reed@google.com | 6271117 | 2011-05-18 15:08:10 +0000 | [diff] [blame] | 1236 | const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1237 | const bool isAA = !isLCD(fRec); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1238 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 1239 | size_t srcRB; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1240 | const void* bits = fOffscreen.draw(glyph, isBW, &srcRB); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1241 | if (nullptr == bits) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 1242 | LogFontTypeface::EnsureAccessible(this->getTypeface()); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1243 | bits = fOffscreen.draw(glyph, isBW, &srcRB); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1244 | if (nullptr == bits) { |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1245 | sk_bzero(glyph.fImage, glyph.computeImageSize()); |
| 1246 | return; |
| 1247 | } |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1248 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1249 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1250 | if (!isBW) { |
bungeman@google.com | 1bd2d67 | 2012-08-13 20:01:51 +0000 | [diff] [blame] | 1251 | const uint8_t* table; |
| 1252 | //The offscreen contains a GDI blit if isAA and kGenA8FromLCD_Flag is not set. |
| 1253 | //Otherwise the offscreen contains a ClearType blit. |
| 1254 | if (isAA && !(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag)) { |
| 1255 | table = getInverseGammaTableGDI(); |
| 1256 | } else { |
| 1257 | table = getInverseGammaTableClearType(); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1258 | } |
| 1259 | //Note that the following cannot really be integrated into the |
| 1260 | //pre-blend, since we may not be applying the pre-blend; when we aren't |
| 1261 | //applying the pre-blend it means that a filter wants linear anyway. |
| 1262 | //Other code may also be applying the pre-blend, so we'd need another |
| 1263 | //one with this and one without. |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1264 | SkGdiRGB* addr = (SkGdiRGB*)bits; |
| 1265 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1266 | for (int x = 0; x < glyph.fWidth; ++x) { |
| 1267 | int r = (addr[x] >> 16) & 0xFF; |
| 1268 | int g = (addr[x] >> 8) & 0xFF; |
| 1269 | int b = (addr[x] >> 0) & 0xFF; |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 1270 | addr[x] = (table[r] << 16) | (table[g] << 8) | table[b]; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1271 | } |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1272 | addr = SkTAddOffset<SkGdiRGB>(addr, srcRB); |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1276 | int width = glyph.fWidth; |
| 1277 | size_t dstRB = glyph.rowBytes(); |
| 1278 | if (isBW) { |
| 1279 | const uint8_t* src = (const uint8_t*)bits; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1280 | uint8_t* dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 1281 | for (int y = 0; y < glyph.fHeight; y++) { |
| 1282 | memcpy(dst, src, dstRB); |
| 1283 | src += srcRB; |
| 1284 | dst -= dstRB; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1285 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1286 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 1287 | if (glyph.fWidth > 0 && glyph.fHeight > 0) { |
| 1288 | int bitCount = width & 7; |
| 1289 | uint8_t* first = (uint8_t*)glyph.fImage; |
| 1290 | uint8_t* last = (uint8_t*)((char*)glyph.fImage + glyph.fHeight * dstRB - 1); |
| 1291 | *first |= 1 << 7; |
| 1292 | *last |= bitCount == 0 ? 1 : 1 << (8 - bitCount); |
| 1293 | } |
| 1294 | #endif |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1295 | } else if (isAA) { |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1296 | // since the caller may require A8 for maskfilters, we can't check for BW |
| 1297 | // ... until we have the caller tell us that explicitly |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1298 | const SkGdiRGB* src = (const SkGdiRGB*)bits; |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1299 | if (fPreBlend.isApplicable()) { |
| 1300 | rgb_to_a8<true>(src, srcRB, glyph, fPreBlend.fG); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1301 | } else { |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1302 | rgb_to_a8<false>(src, srcRB, glyph, fPreBlend.fG); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1303 | } |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 1304 | } else { // LCD16 |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1305 | const SkGdiRGB* src = (const SkGdiRGB*)bits; |
| 1306 | if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) { |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1307 | rgb_to_bw(src, srcRB, glyph); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 1308 | ((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format; |
| 1309 | } else { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 1310 | SkASSERT(SkMask::kLCD16_Format == glyph.fMaskFormat); |
| 1311 | if (fPreBlend.isApplicable()) { |
| 1312 | rgb_to_lcd16<true>(src, srcRB, glyph, |
| 1313 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1314 | } else { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 1315 | rgb_to_lcd16<false>(src, srcRB, glyph, |
| 1316 | fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1317 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1322 | class GDIGlyphbufferPointIter { |
| 1323 | public: |
skia.committer@gmail.com | 27e21fe | 2013-07-28 07:01:03 +0000 | [diff] [blame] | 1324 | GDIGlyphbufferPointIter(const uint8_t* glyphbuf, DWORD total_size) |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1325 | : fHeaderIter(glyphbuf, total_size), fCurveIter(), fPointIter() |
| 1326 | { } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1327 | |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1328 | POINTFX const * next() { |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1329 | nextHeader: |
| 1330 | if (!fCurveIter.isSet()) { |
| 1331 | const TTPOLYGONHEADER* header = fHeaderIter.next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1332 | if (nullptr == header) { |
| 1333 | return nullptr; |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1334 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1335 | fCurveIter.set(header); |
| 1336 | const TTPOLYCURVE* curve = fCurveIter.next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1337 | if (nullptr == curve) { |
| 1338 | return nullptr; |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1339 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1340 | fPointIter.set(curve); |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1341 | return &header->pfxStart; |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1344 | const POINTFX* nextPoint = fPointIter.next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1345 | if (nullptr == nextPoint) { |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1346 | const TTPOLYCURVE* curve = fCurveIter.next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1347 | if (nullptr == curve) { |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1348 | fCurveIter.set(); |
| 1349 | goto nextHeader; |
| 1350 | } else { |
| 1351 | fPointIter.set(curve); |
bungeman@google.com | 05a729f | 2013-06-20 15:29:16 +0000 | [diff] [blame] | 1352 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1353 | nextPoint = fPointIter.next(); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1354 | } |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1355 | return nextPoint; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1356 | } |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 1357 | |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1358 | WORD currentCurveType() { |
| 1359 | return fPointIter.fCurveType; |
| 1360 | } |
| 1361 | |
| 1362 | private: |
| 1363 | /** Iterates over all of the polygon headers in a glyphbuf. */ |
| 1364 | class GDIPolygonHeaderIter { |
| 1365 | public: |
skia.committer@gmail.com | 27e21fe | 2013-07-28 07:01:03 +0000 | [diff] [blame] | 1366 | GDIPolygonHeaderIter(const uint8_t* glyphbuf, DWORD total_size) |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1367 | : fCurPolygon(reinterpret_cast<const TTPOLYGONHEADER*>(glyphbuf)) |
| 1368 | , fEndPolygon(SkTAddOffset<const TTPOLYGONHEADER>(glyphbuf, total_size)) |
| 1369 | { } |
| 1370 | |
| 1371 | const TTPOLYGONHEADER* next() { |
| 1372 | if (fCurPolygon >= fEndPolygon) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1373 | return nullptr; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1374 | } |
| 1375 | const TTPOLYGONHEADER* thisPolygon = fCurPolygon; |
| 1376 | fCurPolygon = SkTAddOffset<const TTPOLYGONHEADER>(fCurPolygon, fCurPolygon->cb); |
| 1377 | return thisPolygon; |
| 1378 | } |
| 1379 | private: |
| 1380 | const TTPOLYGONHEADER* fCurPolygon; |
| 1381 | const TTPOLYGONHEADER* fEndPolygon; |
| 1382 | }; |
| 1383 | |
| 1384 | /** Iterates over all of the polygon curves in a polygon header. */ |
| 1385 | class GDIPolygonCurveIter { |
| 1386 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1387 | GDIPolygonCurveIter() : fCurCurve(nullptr), fEndCurve(nullptr) { } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1388 | |
| 1389 | GDIPolygonCurveIter(const TTPOLYGONHEADER* curPolygon) |
| 1390 | : fCurCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER))) |
| 1391 | , fEndCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb)) |
| 1392 | { } |
| 1393 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1394 | bool isSet() { return fCurCurve != nullptr; } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1395 | |
| 1396 | void set(const TTPOLYGONHEADER* curPolygon) { |
| 1397 | fCurCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER)); |
| 1398 | fEndCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb); |
| 1399 | } |
| 1400 | void set() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1401 | fCurCurve = nullptr; |
| 1402 | fEndCurve = nullptr; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | const TTPOLYCURVE* next() { |
| 1406 | if (fCurCurve >= fEndCurve) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1407 | return nullptr; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1408 | } |
| 1409 | const TTPOLYCURVE* thisCurve = fCurCurve; |
| 1410 | fCurCurve = SkTAddOffset<const TTPOLYCURVE>(fCurCurve, size_of_TTPOLYCURVE(*fCurCurve)); |
| 1411 | return thisCurve; |
| 1412 | } |
| 1413 | private: |
| 1414 | size_t size_of_TTPOLYCURVE(const TTPOLYCURVE& curve) { |
| 1415 | return 2*sizeof(WORD) + curve.cpfx*sizeof(POINTFX); |
| 1416 | } |
| 1417 | const TTPOLYCURVE* fCurCurve; |
| 1418 | const TTPOLYCURVE* fEndCurve; |
| 1419 | }; |
| 1420 | |
| 1421 | /** Iterates over all of the polygon points in a polygon curve. */ |
| 1422 | class GDIPolygonCurvePointIter { |
| 1423 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1424 | GDIPolygonCurvePointIter() : fCurveType(0), fCurPoint(nullptr), fEndPoint(nullptr) { } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1425 | |
| 1426 | GDIPolygonCurvePointIter(const TTPOLYCURVE* curPolygon) |
| 1427 | : fCurveType(curPolygon->wType) |
| 1428 | , fCurPoint(&curPolygon->apfx[0]) |
| 1429 | , fEndPoint(&curPolygon->apfx[curPolygon->cpfx]) |
| 1430 | { } |
| 1431 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1432 | bool isSet() { return fCurPoint != nullptr; } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1433 | |
| 1434 | void set(const TTPOLYCURVE* curPolygon) { |
| 1435 | fCurveType = curPolygon->wType; |
| 1436 | fCurPoint = &curPolygon->apfx[0]; |
| 1437 | fEndPoint = &curPolygon->apfx[curPolygon->cpfx]; |
| 1438 | } |
| 1439 | void set() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1440 | fCurPoint = nullptr; |
| 1441 | fEndPoint = nullptr; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | const POINTFX* next() { |
| 1445 | if (fCurPoint >= fEndPoint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1446 | return nullptr; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1447 | } |
| 1448 | const POINTFX* thisPoint = fCurPoint; |
| 1449 | ++fCurPoint; |
| 1450 | return thisPoint; |
| 1451 | } |
| 1452 | |
| 1453 | WORD fCurveType; |
| 1454 | private: |
| 1455 | const POINTFX* fCurPoint; |
| 1456 | const POINTFX* fEndPoint; |
| 1457 | }; |
| 1458 | |
| 1459 | GDIPolygonHeaderIter fHeaderIter; |
| 1460 | GDIPolygonCurveIter fCurveIter; |
| 1461 | GDIPolygonCurvePointIter fPointIter; |
| 1462 | }; |
| 1463 | |
| 1464 | static void sk_path_from_gdi_path(SkPath* path, const uint8_t* glyphbuf, DWORD total_size) { |
bungeman@google.com | d1c7f71 | 2013-03-11 18:51:19 +0000 | [diff] [blame] | 1465 | const uint8_t* cur_glyph = glyphbuf; |
| 1466 | const uint8_t* end_glyph = glyphbuf + total_size; |
| 1467 | |
| 1468 | while (cur_glyph < end_glyph) { |
| 1469 | const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph; |
| 1470 | |
| 1471 | const uint8_t* end_poly = cur_glyph + th->cb; |
| 1472 | const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER); |
| 1473 | |
| 1474 | path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)), |
| 1475 | SkFixedToScalar(-SkFIXEDToFixed(th->pfxStart.y))); |
| 1476 | |
| 1477 | while (cur_poly < end_poly) { |
| 1478 | const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly; |
| 1479 | |
| 1480 | if (pc->wType == TT_PRIM_LINE) { |
| 1481 | for (uint16_t i = 0; i < pc->cpfx; i++) { |
| 1482 | path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)), |
| 1483 | SkFixedToScalar(-SkFIXEDToFixed(pc->apfx[i].y))); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | if (pc->wType == TT_PRIM_QSPLINE) { |
| 1488 | for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline |
| 1489 | POINTFX pnt_b = pc->apfx[u]; // B is always the current point |
| 1490 | POINTFX pnt_c = pc->apfx[u+1]; |
| 1491 | |
| 1492 | if (u < pc->cpfx - 2) { // If not on last spline, compute C |
| 1493 | pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x), |
| 1494 | SkFIXEDToFixed(pnt_c.x))); |
| 1495 | pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y), |
| 1496 | SkFIXEDToFixed(pnt_c.y))); |
| 1497 | } |
| 1498 | |
| 1499 | path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)), |
| 1500 | SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)), |
| 1501 | SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)), |
| 1502 | SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y))); |
| 1503 | } |
| 1504 | } |
| 1505 | // Advance past this TTPOLYCURVE. |
| 1506 | cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx; |
| 1507 | } |
| 1508 | cur_glyph += th->cb; |
| 1509 | path->close(); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1510 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1513 | #define move_next_expected_hinted_point(iter, pElem) do {\ |
| 1514 | pElem = iter.next(); \ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1515 | if (nullptr == pElem) return false; \ |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1516 | } while(0) |
| 1517 | |
| 1518 | // It is possible for the hinted and unhinted versions of the same path to have |
| 1519 | // a different number of points due to GDI's handling of flipped points. |
| 1520 | // If this is detected, this will return false. |
| 1521 | static bool sk_path_from_gdi_paths(SkPath* path, const uint8_t* glyphbuf, DWORD total_size, |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1522 | GDIGlyphbufferPointIter hintedYs) { |
| 1523 | const uint8_t* cur_glyph = glyphbuf; |
| 1524 | const uint8_t* end_glyph = glyphbuf + total_size; |
| 1525 | |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1526 | POINTFX const * hintedPoint; |
| 1527 | |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1528 | while (cur_glyph < end_glyph) { |
| 1529 | const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph; |
| 1530 | |
| 1531 | const uint8_t* end_poly = cur_glyph + th->cb; |
| 1532 | const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER); |
| 1533 | |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1534 | move_next_expected_hinted_point(hintedYs, hintedPoint); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1535 | path->moveTo(SkFixedToScalar( SkFIXEDToFixed(th->pfxStart.x)), |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1536 | SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y))); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1537 | |
| 1538 | while (cur_poly < end_poly) { |
| 1539 | const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly; |
| 1540 | |
| 1541 | if (pc->wType == TT_PRIM_LINE) { |
| 1542 | for (uint16_t i = 0; i < pc->cpfx; i++) { |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1543 | move_next_expected_hinted_point(hintedYs, hintedPoint); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1544 | path->lineTo(SkFixedToScalar( SkFIXEDToFixed(pc->apfx[i].x)), |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1545 | SkFixedToScalar(-SkFIXEDToFixed(hintedPoint->y))); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | if (pc->wType == TT_PRIM_QSPLINE) { |
| 1550 | POINTFX currentPoint = pc->apfx[0]; |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1551 | move_next_expected_hinted_point(hintedYs, hintedPoint); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1552 | // only take the hinted y if it wasn't flipped |
| 1553 | if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) { |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1554 | currentPoint.y = hintedPoint->y; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1555 | } |
| 1556 | for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline |
| 1557 | POINTFX pnt_b = currentPoint;//pc->apfx[u]; // B is always the current point |
| 1558 | POINTFX pnt_c = pc->apfx[u+1]; |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1559 | move_next_expected_hinted_point(hintedYs, hintedPoint); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1560 | // only take the hinted y if it wasn't flipped |
| 1561 | if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) { |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1562 | pnt_c.y = hintedPoint->y; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1563 | } |
| 1564 | currentPoint.x = pnt_c.x; |
| 1565 | currentPoint.y = pnt_c.y; |
| 1566 | |
| 1567 | if (u < pc->cpfx - 2) { // If not on last spline, compute C |
| 1568 | pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x), |
| 1569 | SkFIXEDToFixed(pnt_c.x))); |
| 1570 | pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y), |
| 1571 | SkFIXEDToFixed(pnt_c.y))); |
| 1572 | } |
| 1573 | |
| 1574 | path->quadTo(SkFixedToScalar( SkFIXEDToFixed(pnt_b.x)), |
| 1575 | SkFixedToScalar(-SkFIXEDToFixed(pnt_b.y)), |
| 1576 | SkFixedToScalar( SkFIXEDToFixed(pnt_c.x)), |
| 1577 | SkFixedToScalar(-SkFIXEDToFixed(pnt_c.y))); |
| 1578 | } |
| 1579 | } |
| 1580 | // Advance past this TTPOLYCURVE. |
| 1581 | cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * pc->cpfx; |
| 1582 | } |
| 1583 | cur_glyph += th->cb; |
| 1584 | path->close(); |
| 1585 | } |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1586 | return true; |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 1589 | DWORD SkScalerContext_GDI::getGDIGlyphPath(const SkGlyph& glyph, UINT flags, |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1590 | SkAutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf) |
| 1591 | { |
| 1592 | GLYPHMETRICS gm; |
| 1593 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 1594 | DWORD total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, BUFFERSIZE, glyphbuf->get(), &fMat22); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1595 | // Sometimes GetGlyphOutlineW returns a number larger than BUFFERSIZE even if BUFFERSIZE > 0. |
| 1596 | // It has been verified that this does not involve a buffer overrun. |
| 1597 | if (GDI_ERROR == total_size || total_size > BUFFERSIZE) { |
| 1598 | // GDI_ERROR because the BUFFERSIZE was too small, or because the data was not accessible. |
| 1599 | // When the data is not accessable GetGlyphOutlineW fails rather quickly, |
| 1600 | // so just try to get the size. If that fails then ensure the data is accessible. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1601 | total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, 0, nullptr, &fMat22); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1602 | if (GDI_ERROR == total_size) { |
| 1603 | LogFontTypeface::EnsureAccessible(this->getTypeface()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1604 | total_size = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, 0, nullptr, &fMat22); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1605 | if (GDI_ERROR == total_size) { |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 1606 | // GetGlyphOutlineW is known to fail for some characters, such as spaces. |
| 1607 | // In these cases, just return that the glyph does not have a shape. |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | glyphbuf->reset(total_size); |
| 1613 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 1614 | DWORD ret = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, total_size, glyphbuf->get(), &fMat22); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1615 | if (GDI_ERROR == ret) { |
| 1616 | LogFontTypeface::EnsureAccessible(this->getTypeface()); |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 1617 | ret = GetGlyphOutlineW(fDDC, glyph.getGlyphID(), flags, &gm, total_size, glyphbuf->get(), &fMat22); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1618 | if (GDI_ERROR == ret) { |
| 1619 | SkASSERT(false); |
| 1620 | return 0; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | return total_size; |
| 1625 | } |
| 1626 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 1627 | void SkScalerContext_GDI::generatePath(const SkGlyph& glyph, SkPath* path) { |
dcheng | 3ba043f | 2015-07-08 13:25:23 -0700 | [diff] [blame] | 1628 | SkASSERT(path); |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1629 | SkASSERT(fDDC); |
| 1630 | |
| 1631 | path->reset(); |
| 1632 | |
| 1633 | // Out of all the fonts on a typical Windows box, |
| 1634 | // 25% of glyphs require more than 2KB. |
| 1635 | // 1% of glyphs require more than 4KB. |
| 1636 | // 0.01% of glyphs require more than 8KB. |
| 1637 | // 8KB is less than 1% of the normal 1MB stack on Windows. |
| 1638 | // Note that some web fonts glyphs require more than 20KB. |
| 1639 | //static const DWORD BUFFERSIZE = (1 << 13); |
| 1640 | |
| 1641 | //GDI only uses hinted outlines when axis aligned. |
| 1642 | UINT format = GGO_NATIVE | GGO_GLYPH_INDEX; |
| 1643 | if (fRec.getHinting() == SkPaint::kNo_Hinting || fRec.getHinting() == SkPaint::kSlight_Hinting){ |
| 1644 | format |= GGO_UNHINTED; |
| 1645 | } |
| 1646 | SkAutoSTMalloc<BUFFERSIZE, uint8_t> glyphbuf(BUFFERSIZE); |
| 1647 | DWORD total_size = getGDIGlyphPath(glyph, format, &glyphbuf); |
| 1648 | if (0 == total_size) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | if (fRec.getHinting() != SkPaint::kSlight_Hinting) { |
| 1653 | sk_path_from_gdi_path(path, glyphbuf, total_size); |
| 1654 | } else { |
| 1655 | //GDI only uses hinted outlines when axis aligned. |
| 1656 | UINT format = GGO_NATIVE | GGO_GLYPH_INDEX; |
| 1657 | |
| 1658 | SkAutoSTMalloc<BUFFERSIZE, uint8_t> hintedGlyphbuf(BUFFERSIZE); |
| 1659 | DWORD hinted_total_size = getGDIGlyphPath(glyph, format, &hintedGlyphbuf); |
| 1660 | if (0 == hinted_total_size) { |
| 1661 | return; |
| 1662 | } |
| 1663 | |
bungeman@google.com | dd88ecc | 2013-09-20 17:25:31 +0000 | [diff] [blame] | 1664 | if (!sk_path_from_gdi_paths(path, glyphbuf, total_size, |
| 1665 | GDIGlyphbufferPointIter(hintedGlyphbuf, hinted_total_size))) |
| 1666 | { |
| 1667 | path->reset(); |
| 1668 | sk_path_from_gdi_path(path, glyphbuf, total_size); |
| 1669 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 1673 | static void logfont_for_name(const char* familyName, LOGFONT* lf) { |
| 1674 | sk_bzero(lf, sizeof(LOGFONT)); |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 1675 | #ifdef UNICODE |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 1676 | // Get the buffer size needed first. |
| 1677 | size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1678 | -1, nullptr, 0); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 1679 | // Allocate a buffer (str_len already has terminating null |
| 1680 | // accounted for). |
| 1681 | wchar_t *wideFamilyName = new wchar_t[str_len]; |
| 1682 | // Now actually convert the string. |
| 1683 | ::MultiByteToWideChar(CP_UTF8, 0, familyName, -1, |
| 1684 | wideFamilyName, str_len); |
| 1685 | ::wcsncpy(lf->lfFaceName, wideFamilyName, LF_FACESIZE - 1); |
| 1686 | delete [] wideFamilyName; |
| 1687 | lf->lfFaceName[LF_FACESIZE-1] = L'\0'; |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 1688 | #else |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 1689 | ::strncpy(lf->lfFaceName, familyName, LF_FACESIZE - 1); |
| 1690 | lf->lfFaceName[LF_FACESIZE - 1] = '\0'; |
bungeman@google.com | e70f798 | 2012-06-01 19:38:19 +0000 | [diff] [blame] | 1691 | #endif |
| 1692 | } |
| 1693 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 1694 | void LogFontTypeface::onGetFamilyName(SkString* familyName) const { |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1695 | // Get the actual name of the typeface. The logfont may not know this. |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1696 | HFONT font = CreateFontIndirect(&fLogFont); |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1697 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1698 | HDC deviceContext = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1699 | HFONT savefont = (HFONT)SelectObject(deviceContext, font); |
| 1700 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 1701 | dcfontname_to_skstring(deviceContext, fLogFont, familyName); |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (deviceContext) { |
| 1704 | ::SelectObject(deviceContext, savefont); |
| 1705 | ::DeleteDC(deviceContext); |
| 1706 | } |
| 1707 | if (font) { |
| 1708 | ::DeleteObject(font); |
| 1709 | } |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 1710 | } |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1711 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 1712 | void LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
| 1713 | bool* isLocalStream) const { |
| 1714 | SkString familyName; |
| 1715 | this->onGetFamilyName(&familyName); |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1716 | desc->setFamilyName(familyName.c_str()); |
bungeman | b811378 | 2016-07-25 16:54:59 -0700 | [diff] [blame] | 1717 | desc->setStyle(this->fontStyle()); |
reed@google.com | 5526ede | 2013-03-25 13:03:37 +0000 | [diff] [blame] | 1718 | *isLocalStream = this->fSerializeAsStream; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1721 | SkAdvancedTypefaceMetrics* LogFontTypeface::onGetAdvancedTypefaceMetrics( |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 1722 | PerGlyphInfo perGlyphInfo, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1723 | const uint32_t* glyphIDs, |
reed@google.com | 2689f61 | 2013-03-20 20:01:47 +0000 | [diff] [blame] | 1724 | uint32_t glyphIDsCount) const { |
| 1725 | LOGFONT lf = fLogFont; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1726 | SkAdvancedTypefaceMetrics* info = nullptr; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1727 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1728 | HDC hdc = CreateCompatibleDC(nullptr); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1729 | HFONT font = CreateFontIndirect(&lf); |
| 1730 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1731 | HFONT designFont = nullptr; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1732 | |
reed@google.com | 05b6f3a | 2011-11-28 15:30:28 +0000 | [diff] [blame] | 1733 | const char stem_chars[] = {'i', 'I', '!', '1'}; |
| 1734 | int16_t min_width; |
| 1735 | unsigned glyphCount; |
| 1736 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1737 | // To request design units, create a logical font whose height is specified |
| 1738 | // as unitsPerEm. |
| 1739 | OUTLINETEXTMETRIC otm; |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1740 | unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1741 | if (0 == otmRet) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 1742 | call_ensure_accessible(lf); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1743 | otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1744 | } |
| 1745 | if (!otmRet || !GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1746 | goto Error; |
| 1747 | } |
| 1748 | lf.lfHeight = -SkToS32(otm.otmEMSquare); |
| 1749 | designFont = CreateFontIndirect(&lf); |
| 1750 | SelectObject(hdc, designFont); |
| 1751 | if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) { |
| 1752 | goto Error; |
| 1753 | } |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 1754 | glyphCount = calculateGlyphCount(hdc, fLogFont); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1755 | |
| 1756 | info = new SkAdvancedTypefaceMetrics; |
| 1757 | info->fEmSize = otm.otmEMSquare; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1758 | info->fLastGlyphID = SkToU16(glyphCount - 1); |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 1759 | tchar_to_skstring(lf.lfFaceName, &info->fFontName); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 1760 | // If bit 1 is set, the font may not be embedded in a document. |
| 1761 | // If bit 1 is clear, the font can be embedded. |
| 1762 | // If bit 2 is set, the embedding is read-only. |
| 1763 | if (otm.otmfsType & 0x1) { |
| 1764 | info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( |
| 1765 | info->fFlags, |
| 1766 | SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); |
| 1767 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1768 | |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 1769 | if (perGlyphInfo & kToUnicode_PerGlyphInfo) { |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 1770 | populate_glyph_to_unicode(hdc, glyphCount, &(info->fGlyphToUnicode)); |
| 1771 | } |
| 1772 | |
vandebo@chromium.org | d41e70d | 2012-03-08 19:41:01 +0000 | [diff] [blame] | 1773 | if (glyphCount > 0 && |
| 1774 | (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE)) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1775 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 1776 | } else { |
edisonn@google.com | 390c6d7 | 2013-04-06 20:26:15 +0000 | [diff] [blame] | 1777 | goto ReturnInfo; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1778 | } |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 1779 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1780 | // If this bit is clear the font is a fixed pitch font. |
| 1781 | if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) { |
| 1782 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 1783 | } |
| 1784 | if (otm.otmTextMetrics.tmItalic) { |
| 1785 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 1786 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1787 | if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) { |
| 1788 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 1789 | } else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) { |
| 1790 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 1791 | } |
| 1792 | |
| 1793 | // The main italic angle of the font, in tenths of a degree counterclockwise |
| 1794 | // from vertical. |
| 1795 | info->fItalicAngle = otm.otmItalicAngle / 10; |
| 1796 | info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent); |
| 1797 | info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent); |
| 1798 | // TODO(ctguil): Use alternate cap height calculation. |
| 1799 | // MSDN says otmsCapEmHeight is not support but it is returning a value on |
| 1800 | // my Win7 box. |
| 1801 | info->fCapHeight = otm.otmsCapEmHeight; |
| 1802 | info->fBBox = |
| 1803 | SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top, |
| 1804 | otm.otmrcFontBox.right, otm.otmrcFontBox.bottom); |
| 1805 | |
| 1806 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 1807 | // This probably isn't very good with an italic font. |
reed@google.com | 05b6f3a | 2011-11-28 15:30:28 +0000 | [diff] [blame] | 1808 | min_width = SHRT_MAX; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1809 | info->fStemV = 0; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1810 | for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) { |
| 1811 | ABC abcWidths; |
| 1812 | if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) { |
| 1813 | int16_t width = abcWidths.abcB; |
| 1814 | if (width > 0 && width < min_width) { |
| 1815 | min_width = width; |
| 1816 | info->fStemV = min_width; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 1821 | if (perGlyphInfo & kHAdvance_PerGlyphInfo) { |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 1822 | info->setGlyphWidths( |
| 1823 | glyphCount, |
| 1824 | glyphIDs, |
| 1825 | glyphIDsCount, |
| 1826 | SkAdvancedTypefaceMetrics::GetAdvance([hdc](int gId, int16_t* advance) { |
| 1827 | // Initialize the MAT2 structure to |
| 1828 | // the identify transformation matrix. |
| 1829 | static const MAT2 mat2 = { |
| 1830 | SkScalarToFIXED(1), SkScalarToFIXED(0), |
| 1831 | SkScalarToFIXED(0), SkScalarToFIXED(1)}; |
| 1832 | int flags = GGO_METRICS | GGO_GLYPH_INDEX; |
| 1833 | GLYPHMETRICS gm; |
| 1834 | if (GDI_ERROR == GetGlyphOutline(hdc, gId, flags, &gm, 0, nullptr, &mat2)) { |
| 1835 | return false; |
| 1836 | } |
| 1837 | SkASSERT(advance); |
| 1838 | *advance = gm.gmCellIncX; |
| 1839 | return true; |
| 1840 | }) |
| 1841 | ); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | Error: |
edisonn@google.com | 390c6d7 | 2013-04-06 20:26:15 +0000 | [diff] [blame] | 1845 | ReturnInfo: |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1846 | SelectObject(hdc, savefont); |
| 1847 | DeleteObject(designFont); |
| 1848 | DeleteObject(font); |
| 1849 | DeleteDC(hdc); |
| 1850 | |
| 1851 | return info; |
| 1852 | } |
| 1853 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1854 | //Dummy representation of a Base64 encoded GUID from create_unique_font_name. |
| 1855 | #define BASE64_GUID_ID "XXXXXXXXXXXXXXXXXXXXXXXX" |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1856 | //Length of GUID representation from create_id, including nullptr terminator. |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1857 | #define BASE64_GUID_ID_LEN SK_ARRAY_COUNT(BASE64_GUID_ID) |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1858 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 1859 | static_assert(BASE64_GUID_ID_LEN < LF_FACESIZE, "GUID_longer_than_facesize"); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1860 | |
| 1861 | /** |
| 1862 | NameID 6 Postscript names cannot have the character '/'. |
| 1863 | It would be easier to hex encode the GUID, but that is 32 bytes, |
| 1864 | and many systems have issues with names longer than 28 bytes. |
| 1865 | The following need not be any standard base64 encoding. |
| 1866 | The encoded value is never decoded. |
| 1867 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1868 | static const char postscript_safe_base64_encode[] = |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1869 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 1870 | "abcdefghijklmnopqrstuvwxyz" |
| 1871 | "0123456789-_="; |
| 1872 | |
| 1873 | /** |
| 1874 | Formats a GUID into Base64 and places it into buffer. |
| 1875 | buffer should have space for at least BASE64_GUID_ID_LEN characters. |
| 1876 | The string will always be null terminated. |
| 1877 | XXXXXXXXXXXXXXXXXXXXXXXX0 |
| 1878 | */ |
| 1879 | static void format_guid_b64(const GUID& guid, char* buffer, size_t bufferSize) { |
| 1880 | SkASSERT(bufferSize >= BASE64_GUID_ID_LEN); |
| 1881 | size_t written = SkBase64::Encode(&guid, sizeof(guid), buffer, postscript_safe_base64_encode); |
| 1882 | SkASSERT(written < LF_FACESIZE); |
| 1883 | buffer[written] = '\0'; |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | Creates a Base64 encoded GUID and places it into buffer. |
| 1888 | buffer should have space for at least BASE64_GUID_ID_LEN characters. |
| 1889 | The string will always be null terminated. |
| 1890 | XXXXXXXXXXXXXXXXXXXXXXXX0 |
| 1891 | */ |
| 1892 | static HRESULT create_unique_font_name(char* buffer, size_t bufferSize) { |
| 1893 | GUID guid = {}; |
| 1894 | if (FAILED(CoCreateGuid(&guid))) { |
| 1895 | return E_UNEXPECTED; |
| 1896 | } |
| 1897 | format_guid_b64(guid, buffer, bufferSize); |
| 1898 | |
| 1899 | return S_OK; |
| 1900 | } |
| 1901 | |
| 1902 | /** |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1903 | Introduces a font to GDI. On failure will return nullptr. The returned handle |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1904 | should eventually be passed to RemoveFontMemResourceEx. |
| 1905 | */ |
| 1906 | static HANDLE activate_font(SkData* fontData) { |
| 1907 | DWORD numFonts = 0; |
| 1908 | //AddFontMemResourceEx just copies the data, but does not specify const. |
| 1909 | HANDLE fontHandle = AddFontMemResourceEx(const_cast<void*>(fontData->data()), |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 1910 | static_cast<DWORD>(fontData->size()), |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1911 | 0, |
| 1912 | &numFonts); |
| 1913 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1914 | if (fontHandle != nullptr && numFonts < 1) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1915 | RemoveFontMemResourceEx(fontHandle); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1916 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | return fontHandle; |
| 1920 | } |
| 1921 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 1922 | // Does not affect ownership of stream. |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 1923 | static SkTypeface* create_from_stream(SkStreamAsset* stream) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1924 | // Create a unique and unpredictable font name. |
| 1925 | // Avoids collisions and access from CSS. |
| 1926 | char familyName[BASE64_GUID_ID_LEN]; |
| 1927 | const int familyNameSize = SK_ARRAY_COUNT(familyName); |
| 1928 | if (FAILED(create_unique_font_name(familyName, familyNameSize))) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1929 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1930 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1931 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1932 | // Change the name of the font. |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame^] | 1933 | sk_sp<SkData> rewrittenFontData(SkOTUtils::RenameFont(stream, familyName, familyNameSize-1)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1934 | if (nullptr == rewrittenFontData.get()) { |
| 1935 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1936 | } |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Register the font with GDI. |
bungeman@google.com | e9bbee3 | 2012-05-21 13:46:13 +0000 | [diff] [blame] | 1939 | HANDLE fontReference = activate_font(rewrittenFontData.get()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1940 | if (nullptr == fontReference) { |
| 1941 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | // Create the typeface. |
| 1945 | LOGFONT lf; |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 1946 | logfont_for_name(familyName, &lf); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 1947 | |
| 1948 | return SkCreateFontMemResourceTypefaceFromLOGFONT(lf, fontReference); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 1951 | SkStreamAsset* LogFontTypeface::onOpenStream(int* ttcIndex) const { |
reed@google.com | 0042b9c | 2013-03-21 20:16:04 +0000 | [diff] [blame] | 1952 | *ttcIndex = 0; |
| 1953 | |
ctguil@chromium.org | f4c2622 | 2011-05-16 22:00:05 +0000 | [diff] [blame] | 1954 | const DWORD kTTCTag = |
| 1955 | SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f')); |
reed@google.com | 0042b9c | 2013-03-21 20:16:04 +0000 | [diff] [blame] | 1956 | LOGFONT lf = fLogFont; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1957 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1958 | HDC hdc = ::CreateCompatibleDC(nullptr); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1959 | HFONT font = CreateFontIndirect(&lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1960 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 1961 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1962 | SkMemoryStream* stream = nullptr; |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1963 | DWORD tables[2] = {kTTCTag, 0}; |
| 1964 | for (int i = 0; i < SK_ARRAY_COUNT(tables); i++) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1965 | DWORD bufferSize = GetFontData(hdc, tables[i], 0, nullptr, 0); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1966 | if (bufferSize == GDI_ERROR) { |
reed@google.com | 055180c | 2013-03-21 18:46:35 +0000 | [diff] [blame] | 1967 | call_ensure_accessible(lf); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1968 | bufferSize = GetFontData(hdc, tables[i], 0, nullptr, 0); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1969 | } |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1970 | if (bufferSize != GDI_ERROR) { |
| 1971 | stream = new SkMemoryStream(bufferSize); |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 1972 | if (GetFontData(hdc, tables[i], 0, (void*)stream->getMemoryBase(), bufferSize)) { |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1973 | break; |
| 1974 | } else { |
| 1975 | delete stream; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1976 | stream = nullptr; |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1977 | } |
| 1978 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | SelectObject(hdc, savefont); |
| 1982 | DeleteObject(font); |
| 1983 | DeleteDC(hdc); |
| 1984 | |
| 1985 | return stream; |
| 1986 | } |
| 1987 | |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 1988 | static void bmpCharsToGlyphs(HDC hdc, const WCHAR* bmpChars, int count, uint16_t* glyphs, |
| 1989 | bool Ox1FHack) |
| 1990 | { |
| 1991 | DWORD result = GetGlyphIndicesW(hdc, bmpChars, count, glyphs, GGI_MARK_NONEXISTING_GLYPHS); |
| 1992 | if (GDI_ERROR == result) { |
| 1993 | for (int i = 0; i < count; ++i) { |
| 1994 | glyphs[i] = 0; |
| 1995 | } |
| 1996 | return; |
| 1997 | } |
| 1998 | |
| 1999 | if (Ox1FHack) { |
| 2000 | for (int i = 0; i < count; ++i) { |
| 2001 | if (0xFFFF == glyphs[i] || 0x1F == glyphs[i]) { |
| 2002 | glyphs[i] = 0; |
| 2003 | } |
| 2004 | } |
| 2005 | } else { |
| 2006 | for (int i = 0; i < count; ++i) { |
| 2007 | if (0xFFFF == glyphs[i]){ |
| 2008 | glyphs[i] = 0; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | static uint16_t nonBmpCharToGlyph(HDC hdc, SCRIPT_CACHE* scriptCache, const WCHAR utf16[2]) { |
| 2015 | uint16_t index = 0; |
| 2016 | // Use uniscribe to detemine glyph index for non-BMP characters. |
| 2017 | static const int numWCHAR = 2; |
| 2018 | static const int maxItems = 2; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2019 | // MSDN states that this can be nullptr, but some things don't work then. |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2020 | SCRIPT_CONTROL scriptControl = { 0 }; |
| 2021 | // Add extra item to SCRIPT_ITEM to work around a bug (now documented). |
| 2022 | // https://bugzilla.mozilla.org/show_bug.cgi?id=366643 |
| 2023 | SCRIPT_ITEM si[maxItems + 1]; |
| 2024 | int numItems; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2025 | HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &scriptControl, nullptr, si, &numItems), |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2026 | "Could not itemize character."); |
| 2027 | |
| 2028 | // Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs. |
| 2029 | static const int maxGlyphs = 2; |
| 2030 | SCRIPT_VISATTR vsa[maxGlyphs]; |
| 2031 | WORD outGlyphs[maxGlyphs]; |
| 2032 | WORD logClust[numWCHAR]; |
| 2033 | int numGlyphs; |
| 2034 | HRZM(ScriptShape(hdc, scriptCache, utf16, numWCHAR, maxGlyphs, &si[0].a, |
| 2035 | outGlyphs, logClust, vsa, &numGlyphs), |
| 2036 | "Could not shape character."); |
| 2037 | if (1 == numGlyphs) { |
| 2038 | index = outGlyphs[0]; |
| 2039 | } |
| 2040 | return index; |
| 2041 | } |
| 2042 | |
| 2043 | class SkAutoHDC { |
| 2044 | public: |
| 2045 | SkAutoHDC(const LOGFONT& lf) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2046 | : fHdc(::CreateCompatibleDC(nullptr)) |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2047 | , fFont(::CreateFontIndirect(&lf)) |
| 2048 | , fSavefont((HFONT)SelectObject(fHdc, fFont)) |
| 2049 | { } |
| 2050 | ~SkAutoHDC() { |
| 2051 | SelectObject(fHdc, fSavefont); |
| 2052 | DeleteObject(fFont); |
| 2053 | DeleteDC(fHdc); |
| 2054 | } |
| 2055 | operator HDC() { return fHdc; } |
| 2056 | private: |
| 2057 | HDC fHdc; |
| 2058 | HFONT fFont; |
| 2059 | HFONT fSavefont; |
| 2060 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 2061 | #define SkAutoHDC(...) SK_REQUIRE_LOCAL_VAR(SkAutoHDC) |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2062 | |
| 2063 | int LogFontTypeface::onCharsToGlyphs(const void* chars, Encoding encoding, |
| 2064 | uint16_t userGlyphs[], int glyphCount) const |
| 2065 | { |
| 2066 | SkAutoHDC hdc(fLogFont); |
| 2067 | |
| 2068 | TEXTMETRIC tm; |
| 2069 | if (0 == GetTextMetrics(hdc, &tm)) { |
| 2070 | call_ensure_accessible(fLogFont); |
| 2071 | if (0 == GetTextMetrics(hdc, &tm)) { |
| 2072 | tm.tmPitchAndFamily = TMPF_TRUETYPE; |
| 2073 | } |
| 2074 | } |
| 2075 | bool Ox1FHack = !(tm.tmPitchAndFamily & TMPF_VECTOR) /*&& winVer < Vista */; |
| 2076 | |
| 2077 | SkAutoSTMalloc<256, uint16_t> scratchGlyphs; |
| 2078 | uint16_t* glyphs; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2079 | if (userGlyphs != nullptr) { |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2080 | glyphs = userGlyphs; |
| 2081 | } else { |
| 2082 | glyphs = scratchGlyphs.reset(glyphCount); |
| 2083 | } |
| 2084 | |
| 2085 | SCRIPT_CACHE sc = 0; |
| 2086 | switch (encoding) { |
| 2087 | case SkTypeface::kUTF8_Encoding: { |
| 2088 | static const int scratchCount = 256; |
| 2089 | WCHAR scratch[scratchCount]; |
| 2090 | int glyphIndex = 0; |
| 2091 | const char* currentUtf8 = reinterpret_cast<const char*>(chars); |
| 2092 | SkUnichar currentChar; |
| 2093 | if (glyphCount) { |
| 2094 | currentChar = SkUTF8_NextUnichar(¤tUtf8); |
| 2095 | } |
| 2096 | while (glyphIndex < glyphCount) { |
| 2097 | // Try a run of bmp. |
| 2098 | int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount); |
| 2099 | int runLength = 0; |
| 2100 | while (runLength < glyphsLeft && currentChar <= 0xFFFF) { |
| 2101 | scratch[runLength] = static_cast<WCHAR>(currentChar); |
| 2102 | ++runLength; |
| 2103 | if (runLength < glyphsLeft) { |
| 2104 | currentChar = SkUTF8_NextUnichar(¤tUtf8); |
| 2105 | } |
| 2106 | } |
| 2107 | if (runLength) { |
| 2108 | bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack); |
| 2109 | glyphIndex += runLength; |
| 2110 | } |
| 2111 | |
| 2112 | // Try a run of non-bmp. |
| 2113 | while (glyphIndex < glyphCount && currentChar > 0xFFFF) { |
| 2114 | SkUTF16_FromUnichar(currentChar, reinterpret_cast<uint16_t*>(scratch)); |
| 2115 | glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch); |
| 2116 | ++glyphIndex; |
| 2117 | if (glyphIndex < glyphCount) { |
| 2118 | currentChar = SkUTF8_NextUnichar(¤tUtf8); |
| 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | break; |
| 2123 | } |
| 2124 | case SkTypeface::kUTF16_Encoding: { |
| 2125 | int glyphIndex = 0; |
| 2126 | const WCHAR* currentUtf16 = reinterpret_cast<const WCHAR*>(chars); |
| 2127 | while (glyphIndex < glyphCount) { |
| 2128 | // Try a run of bmp. |
| 2129 | int glyphsLeft = glyphCount - glyphIndex; |
| 2130 | int runLength = 0; |
| 2131 | while (runLength < glyphsLeft && !SkUTF16_IsHighSurrogate(currentUtf16[runLength])) { |
| 2132 | ++runLength; |
| 2133 | } |
| 2134 | if (runLength) { |
| 2135 | bmpCharsToGlyphs(hdc, currentUtf16, runLength, &glyphs[glyphIndex], Ox1FHack); |
| 2136 | glyphIndex += runLength; |
| 2137 | currentUtf16 += runLength; |
| 2138 | } |
| 2139 | |
| 2140 | // Try a run of non-bmp. |
| 2141 | while (glyphIndex < glyphCount && SkUTF16_IsHighSurrogate(*currentUtf16)) { |
| 2142 | glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, currentUtf16); |
| 2143 | ++glyphIndex; |
| 2144 | currentUtf16 += 2; |
| 2145 | } |
| 2146 | } |
| 2147 | break; |
| 2148 | } |
| 2149 | case SkTypeface::kUTF32_Encoding: { |
| 2150 | static const int scratchCount = 256; |
| 2151 | WCHAR scratch[scratchCount]; |
| 2152 | int glyphIndex = 0; |
| 2153 | const uint32_t* utf32 = reinterpret_cast<const uint32_t*>(chars); |
| 2154 | while (glyphIndex < glyphCount) { |
| 2155 | // Try a run of bmp. |
| 2156 | int glyphsLeft = SkTMin(glyphCount - glyphIndex, scratchCount); |
| 2157 | int runLength = 0; |
| 2158 | while (runLength < glyphsLeft && utf32[glyphIndex + runLength] <= 0xFFFF) { |
| 2159 | scratch[runLength] = static_cast<WCHAR>(utf32[glyphIndex + runLength]); |
| 2160 | ++runLength; |
| 2161 | } |
| 2162 | if (runLength) { |
| 2163 | bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack); |
| 2164 | glyphIndex += runLength; |
| 2165 | } |
| 2166 | |
| 2167 | // Try a run of non-bmp. |
| 2168 | while (glyphIndex < glyphCount && utf32[glyphIndex] > 0xFFFF) { |
| 2169 | SkUTF16_FromUnichar(utf32[glyphIndex], reinterpret_cast<uint16_t*>(scratch)); |
| 2170 | glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch); |
| 2171 | ++glyphIndex; |
| 2172 | } |
| 2173 | } |
| 2174 | break; |
| 2175 | } |
| 2176 | default: |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 2177 | SK_ABORT("Invalid Text Encoding"); |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | if (sc) { |
| 2181 | ::ScriptFreeCache(&sc); |
| 2182 | } |
| 2183 | |
| 2184 | for (int i = 0; i < glyphCount; ++i) { |
| 2185 | if (0 == glyphs[i]) { |
| 2186 | return i; |
| 2187 | } |
| 2188 | } |
| 2189 | return glyphCount; |
| 2190 | } |
| 2191 | |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 2192 | int LogFontTypeface::onCountGlyphs() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2193 | HDC hdc = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 2194 | HFONT font = CreateFontIndirect(&fLogFont); |
| 2195 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 2196 | |
| 2197 | unsigned int glyphCount = calculateGlyphCount(hdc, fLogFont); |
| 2198 | |
| 2199 | SelectObject(hdc, savefont); |
| 2200 | DeleteObject(font); |
| 2201 | DeleteDC(hdc); |
| 2202 | |
| 2203 | return glyphCount; |
| 2204 | } |
| 2205 | |
| 2206 | int LogFontTypeface::onGetUPEM() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2207 | HDC hdc = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 2208 | HFONT font = CreateFontIndirect(&fLogFont); |
| 2209 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 2210 | |
| 2211 | unsigned int upem = calculateUPEM(hdc, fLogFont); |
| 2212 | |
| 2213 | SelectObject(hdc, savefont); |
| 2214 | DeleteObject(font); |
| 2215 | DeleteDC(hdc); |
| 2216 | |
| 2217 | return upem; |
| 2218 | } |
| 2219 | |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 2220 | SkTypeface::LocalizedStrings* LogFontTypeface::onCreateFamilyNameIterator() const { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2221 | SkTypeface::LocalizedStrings* nameIter = |
| 2222 | SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2223 | if (nullptr == nameIter) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2224 | SkString familyName; |
| 2225 | this->getFamilyName(&familyName); |
| 2226 | SkString language("und"); //undetermined |
| 2227 | nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language); |
| 2228 | } |
| 2229 | return nameIter; |
| 2230 | } |
| 2231 | |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 2232 | int LogFontTypeface::onGetTableTags(SkFontTableTag tags[]) const { |
| 2233 | SkSFNTHeader header; |
| 2234 | if (sizeof(header) != this->onGetTableData(0, 0, sizeof(header), &header)) { |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
| 2238 | int numTables = SkEndian_SwapBE16(header.numTables); |
| 2239 | |
| 2240 | if (tags) { |
| 2241 | size_t size = numTables * sizeof(SkSFNTHeader::TableDirectoryEntry); |
| 2242 | SkAutoSTMalloc<0x20, SkSFNTHeader::TableDirectoryEntry> dir(numTables); |
| 2243 | if (size != this->onGetTableData(0, sizeof(header), size, dir.get())) { |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | for (int i = 0; i < numTables; ++i) { |
| 2248 | tags[i] = SkEndian_SwapBE32(dir[i].tag); |
| 2249 | } |
| 2250 | } |
| 2251 | return numTables; |
| 2252 | } |
| 2253 | |
| 2254 | size_t LogFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
| 2255 | size_t length, void* data) const |
| 2256 | { |
| 2257 | LOGFONT lf = fLogFont; |
| 2258 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2259 | HDC hdc = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 2260 | HFONT font = CreateFontIndirect(&lf); |
| 2261 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 2262 | |
| 2263 | tag = SkEndian_SwapBE32(tag); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2264 | if (nullptr == data) { |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 2265 | length = 0; |
| 2266 | } |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 2267 | DWORD bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length); |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 2268 | if (bufferSize == GDI_ERROR) { |
| 2269 | call_ensure_accessible(lf); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 2270 | bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length); |
bungeman@google.com | b10b51f | 2013-08-01 20:18:41 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | SelectObject(hdc, savefont); |
| 2274 | DeleteObject(font); |
| 2275 | DeleteDC(hdc); |
| 2276 | |
| 2277 | return bufferSize == GDI_ERROR ? 0 : bufferSize; |
| 2278 | } |
| 2279 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 2280 | SkScalerContext* LogFontTypeface::onCreateScalerContext(const SkScalerContextEffects& effects, |
| 2281 | const SkDescriptor* desc) const { |
| 2282 | SkScalerContext_GDI* ctx = new SkScalerContext_GDI(const_cast<LogFontTypeface*>(this), |
| 2283 | effects, desc); |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 2284 | if (!ctx->isValid()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2285 | delete ctx; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2286 | ctx = nullptr; |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 2287 | } |
| 2288 | return ctx; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 2291 | void LogFontTypeface::onFilterRec(SkScalerContextRec* rec) const { |
commit-bot@chromium.org | c5fd461 | 2013-05-06 22:23:08 +0000 | [diff] [blame] | 2292 | if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag || |
| 2293 | rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) |
| 2294 | { |
| 2295 | rec->fMaskFormat = SkMask::kA8_Format; |
| 2296 | rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag; |
| 2297 | } |
| 2298 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 2299 | unsigned flagsWeDontSupport = SkScalerContext::kVertical_Flag | |
| 2300 | SkScalerContext::kDevKernText_Flag | |
bungeman@google.com | f6f5687 | 2014-01-23 19:01:36 +0000 | [diff] [blame] | 2301 | SkScalerContext::kForceAutohinting_Flag | |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2302 | SkScalerContext::kEmbeddedBitmapText_Flag | |
| 2303 | SkScalerContext::kEmbolden_Flag | |
| 2304 | SkScalerContext::kLCD_BGROrder_Flag | |
| 2305 | SkScalerContext::kLCD_Vertical_Flag; |
| 2306 | rec->fFlags &= ~flagsWeDontSupport; |
| 2307 | |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2308 | SkPaint::Hinting h = rec->getHinting(); |
| 2309 | switch (h) { |
| 2310 | case SkPaint::kNo_Hinting: |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 2311 | break; |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2312 | case SkPaint::kSlight_Hinting: |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 2313 | // Only do slight hinting when axis aligned. |
bungeman@google.com | 7c86d8e | 2013-07-27 21:42:21 +0000 | [diff] [blame] | 2314 | // TODO: re-enable slight hinting when FontHostTest can pass. |
| 2315 | //if (!isAxisAligned(*rec)) { |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 2316 | h = SkPaint::kNo_Hinting; |
bungeman@google.com | 7c86d8e | 2013-07-27 21:42:21 +0000 | [diff] [blame] | 2317 | //} |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2318 | break; |
| 2319 | case SkPaint::kNormal_Hinting: |
| 2320 | case SkPaint::kFull_Hinting: |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 2321 | // TODO: need to be able to distinguish subpixel positioned glyphs |
| 2322 | // and linear metrics. |
| 2323 | //rec->fFlags &= ~SkScalerContext::kSubpixelPositioning_Flag; |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2324 | h = SkPaint::kNormal_Hinting; |
| 2325 | break; |
| 2326 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 2327 | SkDEBUGFAIL("unknown hinting"); |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2328 | } |
bungeman@google.com | 0abbff9 | 2013-07-27 20:37:56 +0000 | [diff] [blame] | 2329 | //TODO: if this is a bitmap font, squash hinting and subpixel. |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 2330 | rec->setHinting(h); |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 2331 | |
reed@google.com | 9181aa8 | 2011-08-05 14:28:31 +0000 | [diff] [blame] | 2332 | // turn this off since GDI might turn A8 into BW! Need a bigger fix. |
| 2333 | #if 0 |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 2334 | // Disable LCD when rotated, since GDI's output is ugly |
| 2335 | if (isLCD(*rec) && !isAxisAligned(*rec)) { |
| 2336 | rec->fMaskFormat = SkMask::kA8_Format; |
| 2337 | } |
reed@google.com | 9181aa8 | 2011-08-05 14:28:31 +0000 | [diff] [blame] | 2338 | #endif |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 2339 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 2340 | if (!fCanBeLCD && isLCD(*rec)) { |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 2341 | rec->fMaskFormat = SkMask::kA8_Format; |
| 2342 | rec->fFlags &= ~SkScalerContext::kGenA8FromLCD_Flag; |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 2343 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 2344 | } |
reed@google.com | 070da5e | 2013-03-27 20:01:49 +0000 | [diff] [blame] | 2345 | |
| 2346 | /////////////////////////////////////////////////////////////////////////////// |
| 2347 | |
| 2348 | #include "SkFontMgr.h" |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2349 | #include "SkDataTable.h" |
| 2350 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2351 | static bool valid_logfont_for_enum(const LOGFONT& lf) { |
| 2352 | // TODO: Vector FON is unsupported and should not be listed. |
| 2353 | return |
| 2354 | // Ignore implicit vertical variants. |
| 2355 | lf.lfFaceName[0] && lf.lfFaceName[0] != '@' |
| 2356 | |
| 2357 | // DEFAULT_CHARSET is used to get all fonts, but also implies all |
| 2358 | // character sets. Filter assuming all fonts support ANSI_CHARSET. |
| 2359 | && ANSI_CHARSET == lf.lfCharSet |
| 2360 | ; |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2363 | /** An EnumFontFamExProc implementation which interprets builderParam as |
| 2364 | * an SkTDArray<ENUMLOGFONTEX>* and appends logfonts which |
| 2365 | * pass the valid_logfont_for_enum predicate. |
| 2366 | */ |
| 2367 | static int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*, |
| 2368 | DWORD fontType, LPARAM builderParam) { |
| 2369 | if (valid_logfont_for_enum(*lf)) { |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2370 | SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderParam; |
| 2371 | *array->append() = *(ENUMLOGFONTEX*)lf; |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2372 | } |
| 2373 | return 1; // non-zero means continue |
| 2374 | } |
| 2375 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2376 | class SkFontStyleSetGDI : public SkFontStyleSet { |
| 2377 | public: |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2378 | SkFontStyleSetGDI(const TCHAR familyName[]) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2379 | LOGFONT lf; |
| 2380 | sk_bzero(&lf, sizeof(lf)); |
| 2381 | lf.lfCharSet = DEFAULT_CHARSET; |
| 2382 | _tcscpy_s(lf.lfFaceName, familyName); |
| 2383 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2384 | HDC hdc = ::CreateCompatibleDC(nullptr); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2385 | ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2386 | ::DeleteDC(hdc); |
| 2387 | } |
| 2388 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2389 | int count() override { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2390 | return fArray.count(); |
| 2391 | } |
| 2392 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2393 | void getStyle(int index, SkFontStyle* fs, SkString* styleName) override { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2394 | if (fs) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 2395 | *fs = get_style(fArray[index].elfLogFont); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2396 | } |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2397 | if (styleName) { |
| 2398 | const ENUMLOGFONTEX& ref = fArray[index]; |
| 2399 | // For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type in the |
| 2400 | // non-unicode version. |
| 2401 | // ENUMLOGFONTEX uses BYTE |
| 2402 | // LOGFONT uses CHAR |
| 2403 | // Here we assert they that the style name is logically the same (size) as |
| 2404 | // a TCHAR, so we can use the same converter function. |
| 2405 | SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0])); |
| 2406 | tchar_to_skstring((const TCHAR*)ref.elfStyle, styleName); |
| 2407 | } |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2410 | SkTypeface* createTypeface(int index) override { |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2411 | return SkCreateTypefaceFromLOGFONT(fArray[index].elfLogFont); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2414 | SkTypeface* matchStyle(const SkFontStyle& pattern) override { |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 2415 | return this->matchStyleCSS3(pattern); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | private: |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2419 | SkTDArray<ENUMLOGFONTEX> fArray; |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2420 | }; |
| 2421 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2422 | class SkFontMgrGDI : public SkFontMgr { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 2423 | public: |
| 2424 | SkFontMgrGDI() { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2425 | LOGFONT lf; |
| 2426 | sk_bzero(&lf, sizeof(lf)); |
| 2427 | lf.lfCharSet = DEFAULT_CHARSET; |
| 2428 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2429 | HDC hdc = ::CreateCompatibleDC(nullptr); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2430 | ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fLogFontArray, 0); |
| 2431 | ::DeleteDC(hdc); |
| 2432 | } |
| 2433 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2434 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2435 | int onCountFamilies() const override { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2436 | return fLogFontArray.count(); |
| 2437 | } |
| 2438 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2439 | void onGetFamilyName(int index, SkString* familyName) const override { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2440 | SkASSERT((unsigned)index < (unsigned)fLogFontArray.count()); |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2441 | tchar_to_skstring(fLogFontArray[index].elfLogFont.lfFaceName, familyName); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2444 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2445 | SkASSERT((unsigned)index < (unsigned)fLogFontArray.count()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2446 | return new SkFontStyleSetGDI(fLogFontArray[index].elfLogFont.lfFaceName); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2449 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2450 | if (nullptr == familyName) { |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2451 | familyName = ""; // do we need this check??? |
| 2452 | } |
| 2453 | LOGFONT lf; |
| 2454 | logfont_for_name(familyName, &lf); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2455 | return new SkFontStyleSetGDI(lf.lfFaceName); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2458 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2459 | const SkFontStyle& fontstyle) const override { |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2460 | // could be in base impl |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2461 | SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
| 2462 | return sset->matchStyle(fontstyle); |
| 2463 | } |
| 2464 | |
djsollen | 33068c1 | 2014-11-14 10:52:53 -0800 | [diff] [blame] | 2465 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
| 2466 | const char* bcp47[], int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2467 | SkUnichar character) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2468 | return nullptr; |
djsollen | 33068c1 | 2014-11-14 10:52:53 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2471 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2472 | const SkFontStyle& fontstyle) const override { |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2473 | // could be in base impl |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2474 | SkString familyName; |
| 2475 | ((LogFontTypeface*)familyMember)->getFamilyName(&familyName); |
| 2476 | return this->matchFamilyStyle(familyName.c_str(), fontstyle); |
| 2477 | } |
| 2478 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2479 | SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override { |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 2480 | SkAutoTDelete<SkStreamAsset> stream(bareStream); |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2481 | return create_from_stream(stream); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2482 | } |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2483 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2484 | SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2485 | // could be in base impl |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2486 | return this->createFromStream(new SkMemoryStream(data)); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2487 | } |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2488 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 2489 | SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { |
reed@google.com | 437eea1 | 2013-04-25 20:40:02 +0000 | [diff] [blame] | 2490 | // could be in base impl |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 2491 | return this->createFromStream(SkStream::NewFromFile(path)); |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 2494 | SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override { |
bungeman@google.com | f7159bb | 2013-11-20 15:11:05 +0000 | [diff] [blame] | 2495 | LOGFONT lf; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2496 | if (nullptr == familyName) { |
bungeman@google.com | f7159bb | 2013-11-20 15:11:05 +0000 | [diff] [blame] | 2497 | lf = get_default_font(); |
| 2498 | } else { |
| 2499 | logfont_for_name(familyName, &lf); |
| 2500 | } |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 2501 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 2502 | lf.lfWeight = style.weight(); |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 2503 | lf.lfItalic = style.slant() == SkFontStyle::kUpright_Slant ? FALSE : TRUE; |
bungeman@google.com | f7159bb | 2013-11-20 15:11:05 +0000 | [diff] [blame] | 2504 | return SkCreateTypefaceFromLOGFONT(lf); |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2507 | private: |
reed@google.com | a65a681 | 2013-05-02 19:47:24 +0000 | [diff] [blame] | 2508 | SkTDArray<ENUMLOGFONTEX> fLogFontArray; |
reed@google.com | 484f5bc | 2013-04-24 19:14:56 +0000 | [diff] [blame] | 2509 | }; |
reed@google.com | 070da5e | 2013-03-27 20:01:49 +0000 | [diff] [blame] | 2510 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 2511 | /////////////////////////////////////////////////////////////////////////////// |
| 2512 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 2513 | SkFontMgr* SkFontMgr_New_GDI() { return new SkFontMgrGDI; } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 2514 | |
| 2515 | #endif//defined(SK_BUILD_FOR_WIN32) |