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