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