epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 11 | #include "SkString.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 12 | #include "SkEndian.h" |
| 13 | #include "SkFontHost.h" |
| 14 | #include "SkDescriptor.h" |
| 15 | #include "SkAdvancedTypefaceMetrics.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkThread.h" |
| 18 | #include "SkTypeface_win.h" |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 19 | #include "SkTypefaceCache.h" |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 20 | #include "SkUtils.h" |
| 21 | |
| 22 | #ifdef WIN32 |
| 23 | #include "windows.h" |
| 24 | #include "tchar.h" |
| 25 | #include "Usp10.h" |
| 26 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 27 | // always packed xxRRGGBB |
| 28 | typedef uint32_t SkGdiRGB; |
| 29 | |
| 30 | template <typename T> T* SkTAddByteOffset(T* ptr, size_t byteOffset) { |
| 31 | return (T*)((char*)ptr + byteOffset); |
| 32 | } |
| 33 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 34 | // define this in your Makefile or .gyp to enforce AA requests |
| 35 | // which GDI ignores at small sizes. This flag guarantees AA |
| 36 | // for rotated text, regardless of GDI's notions. |
| 37 | //#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 38 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 39 | // client3d has to undefine this for now |
| 40 | #define CAN_USE_LOGFONT_NAME |
| 41 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 42 | static bool isLCD(const SkScalerContext::Rec& rec) { |
| 43 | return SkMask::kLCD16_Format == rec.fMaskFormat || |
| 44 | SkMask::kLCD32_Format == rec.fMaskFormat; |
| 45 | } |
| 46 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 47 | static bool bothZero(SkScalar a, SkScalar b) { |
| 48 | return 0 == a && 0 == b; |
| 49 | } |
| 50 | |
| 51 | // returns false if there is any non-90-rotation or skew |
| 52 | static bool isAxisAligned(const SkScalerContext::Rec& rec) { |
| 53 | return 0 == rec.fPreSkewX && |
| 54 | (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 55 | bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
| 56 | } |
| 57 | |
| 58 | static bool needToRenderWithSkia(const SkScalerContext::Rec& rec) { |
| 59 | #ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 60 | // What we really want to catch is when GDI will ignore the AA request and give |
| 61 | // us BW instead. Smallish rotated text is one heuristic, so this code is just |
| 62 | // an approximation. We shouldn't need to do this for larger sizes, but at those |
| 63 | // sizes, the quality difference gets less and less between our general |
| 64 | // scanconverter and GDI's. |
| 65 | if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) { |
| 66 | return true; |
| 67 | } |
| 68 | #endif |
| 69 | // false means allow GDI to generate the bits |
| 70 | return false; |
| 71 | } |
| 72 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 73 | using namespace skia_advanced_typeface_metrics_utils; |
| 74 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 75 | static const uint16_t BUFFERSIZE = (16384 - 32); |
| 76 | static uint8_t glyphbuf[BUFFERSIZE]; |
| 77 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 78 | /** |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 79 | * Since LOGFONT wants its textsize as an int, and we support fractional sizes, |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 80 | * and since we have a cache of LOGFONTs for our tyepfaces, we always set the |
| 81 | * lfHeight to a canonical size, and then we use the 2x2 matrix to achieve the |
| 82 | * actual requested size. |
| 83 | */ |
| 84 | static const int gCanonicalTextSize = 64; |
| 85 | |
| 86 | static void make_canonical(LOGFONT* lf) { |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 87 | lf->lfHeight = -gCanonicalTextSize; |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 88 | lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY; |
| 89 | lf->lfCharSet = DEFAULT_CHARSET; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 90 | // lf->lfClipPrecision = 64; |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 91 | } |
| 92 | |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 93 | static SkTypeface::Style get_style(const LOGFONT& lf) { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 94 | unsigned style = 0; |
| 95 | if (lf.lfWeight >= FW_BOLD) { |
| 96 | style |= SkTypeface::kBold; |
| 97 | } |
| 98 | if (lf.lfItalic) { |
| 99 | style |= SkTypeface::kItalic; |
| 100 | } |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 101 | return static_cast<SkTypeface::Style>(style); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void setStyle(LOGFONT* lf, SkTypeface::Style style) { |
| 105 | lf->lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL ; |
| 106 | lf->lfItalic = ((style & SkTypeface::kItalic) != 0); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline FIXED SkFixedToFIXED(SkFixed x) { |
| 110 | return *(FIXED*)(&x); |
| 111 | } |
| 112 | |
| 113 | static inline FIXED SkScalarToFIXED(SkScalar x) { |
| 114 | return SkFixedToFIXED(SkScalarToFixed(x)); |
| 115 | } |
| 116 | |
| 117 | static unsigned calculateGlyphCount(HDC hdc) { |
| 118 | // The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes. |
ctguil@chromium.org | f4c2622 | 2011-05-16 22:00:05 +0000 | [diff] [blame] | 119 | const DWORD maxpTag = |
| 120 | SkEndian_SwapBE32(SkSetFourByteTag('m', 'a', 'x', 'p')); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 121 | uint16_t glyphs; |
| 122 | if (GetFontData(hdc, maxpTag, 4, &glyphs, sizeof(glyphs)) != GDI_ERROR) { |
| 123 | return SkEndian_SwapBE16(glyphs); |
| 124 | } |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 125 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 126 | // Binary search for glyph count. |
| 127 | static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; |
| 128 | int32_t max = SK_MaxU16 + 1; |
| 129 | int32_t min = 0; |
| 130 | GLYPHMETRICS gm; |
| 131 | while (min < max) { |
| 132 | int32_t mid = min + ((max - min) / 2); |
| 133 | if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, |
| 134 | NULL, &mat2) == GDI_ERROR) { |
| 135 | max = mid; |
| 136 | } else { |
| 137 | min = mid + 1; |
| 138 | } |
| 139 | } |
| 140 | SkASSERT(min == max); |
| 141 | return min; |
| 142 | } |
| 143 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 144 | class LogFontTypeface : public SkTypeface { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 145 | public: |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 146 | LogFontTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf) : |
| 147 | SkTypeface(style, fontID, false), fLogFont(lf) {} |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 148 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 149 | LOGFONT fLogFont; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 150 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 151 | static LogFontTypeface* Create(const LOGFONT& lf) { |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 152 | SkTypeface::Style style = get_style(lf); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 153 | SkFontID fontID = SkTypefaceCache::NewFontID(); |
| 154 | return new LogFontTypeface(style, fontID, lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 158 | static const LOGFONT& get_default_font() { |
| 159 | static LOGFONT gDefaultFont; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 160 | return gDefaultFont; |
| 161 | } |
| 162 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 163 | static bool FindByLogFont(SkTypeface* face, SkTypeface::Style requestedStyle, void* ctx) { |
| 164 | LogFontTypeface* lface = reinterpret_cast<LogFontTypeface*>(face); |
| 165 | const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 166 | |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 167 | return get_style(lface->fLogFont) == requestedStyle && |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 168 | !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT)); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This guy is public. It first searches the cache, and if a match is not found, |
| 173 | * it creates a new face. |
| 174 | */ |
| 175 | SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) { |
| 176 | LOGFONT lf = origLF; |
| 177 | make_canonical(&lf); |
| 178 | SkTypeface* face = SkTypefaceCache::FindByProc(FindByLogFont, &lf); |
| 179 | if (face) { |
| 180 | face->ref(); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 181 | } else { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 182 | face = LogFontTypeface::Create(lf); |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 183 | SkTypefaceCache::Add(face, get_style(lf)); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 184 | } |
| 185 | return face; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 186 | } |
| 187 | |
reed@google.com | db77a6a | 2011-07-19 19:08:33 +0000 | [diff] [blame] | 188 | /** |
| 189 | * This guy is public |
| 190 | */ |
| 191 | void SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) { |
| 192 | if (NULL == face) { |
| 193 | *lf = get_default_font(); |
| 194 | } else { |
| 195 | *lf = ((const LogFontTypeface*)face)->fLogFont; |
| 196 | } |
| 197 | } |
| 198 | |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 199 | SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 200 | // Zero means that we don't have any fallback fonts for this fontID. |
| 201 | // This function is implemented on Android, but doesn't have much |
| 202 | // meaning here. |
| 203 | return 0; |
| 204 | } |
| 205 | |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 206 | static void ensure_typeface_accessible(SkFontID fontID) { |
| 207 | LogFontTypeface* face = (LogFontTypeface*)SkTypefaceCache::FindByID(fontID); |
| 208 | SkFontHost::EnsureTypefaceAccessible(*face); |
| 209 | } |
| 210 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 211 | static void GetLogFontByID(SkFontID fontID, LOGFONT* lf) { |
| 212 | LogFontTypeface* face = (LogFontTypeface*)SkTypefaceCache::FindByID(fontID); |
| 213 | if (face) { |
| 214 | *lf = face->fLogFont; |
| 215 | } else { |
| 216 | sk_bzero(lf, sizeof(LOGFONT)); |
| 217 | } |
| 218 | } |
| 219 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 220 | // Construct Glyph to Unicode table. |
| 221 | // Unicode code points that require conjugate pairs in utf16 are not |
| 222 | // supported. |
| 223 | // TODO(arthurhsu): Add support for conjugate pairs. It looks like that may |
| 224 | // require parsing the TTF cmap table (platform 4, encoding 12) directly instead |
| 225 | // of calling GetFontUnicodeRange(). |
| 226 | static void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount, |
| 227 | SkTDArray<SkUnichar>* glyphToUnicode) { |
| 228 | DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, NULL); |
| 229 | if (!glyphSetBufferSize) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | SkAutoTDeleteArray<BYTE> glyphSetBuffer(new BYTE[glyphSetBufferSize]); |
| 234 | GLYPHSET* glyphSet = |
| 235 | reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get()); |
| 236 | if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | glyphToUnicode->setCount(glyphCount); |
| 241 | memset(glyphToUnicode->begin(), 0, glyphCount * sizeof(SkUnichar)); |
| 242 | for (DWORD i = 0; i < glyphSet->cRanges; ++i) { |
| 243 | // There is no guarantee that within a Unicode range, the corresponding |
| 244 | // glyph id in a font file are continuous. So, even if we have ranges, |
| 245 | // we can't just use the first and last entry of the range to compute |
| 246 | // result. We need to enumerate them one by one. |
| 247 | int count = glyphSet->ranges[i].cGlyphs; |
| 248 | SkAutoTArray<WCHAR> chars(count + 1); |
| 249 | chars[count] = 0; // termintate string |
| 250 | SkAutoTArray<WORD> glyph(count); |
| 251 | for (USHORT j = 0; j < count; ++j) { |
| 252 | chars[j] = glyphSet->ranges[i].wcLow + j; |
| 253 | } |
| 254 | GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(), |
| 255 | GGI_MARK_NONEXISTING_GLYPHS); |
| 256 | // If the glyph ID is valid, and the glyph is not mapped, then we will |
| 257 | // fill in the char id into the vector. If the glyph is mapped already, |
| 258 | // skip it. |
| 259 | // TODO(arthurhsu): better improve this. e.g. Get all used char ids from |
| 260 | // font cache, then generate this mapping table from there. It's |
| 261 | // unlikely to have collisions since glyph reuse happens mostly for |
| 262 | // different Unicode pages. |
| 263 | for (USHORT j = 0; j < count; ++j) { |
| 264 | if (glyph[j] != 0xffff && glyph[j] < glyphCount && |
| 265 | (*glyphToUnicode)[glyph[j]] == 0) { |
| 266 | (*glyphToUnicode)[glyph[j]] = chars[j]; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 272 | ////////////////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | static int alignTo32(int n) { |
| 275 | return (n + 31) & ~31; |
| 276 | } |
| 277 | |
| 278 | struct MyBitmapInfo : public BITMAPINFO { |
| 279 | RGBQUAD fMoreSpaceForColors[1]; |
| 280 | }; |
| 281 | |
| 282 | class HDCOffscreen { |
| 283 | public: |
| 284 | HDCOffscreen() { |
| 285 | fFont = 0; |
| 286 | fDC = 0; |
| 287 | fBM = 0; |
| 288 | fBits = NULL; |
| 289 | fWidth = fHeight = 0; |
| 290 | fIsBW = false; |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 291 | fColor = kInvalid_Color; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | ~HDCOffscreen() { |
| 295 | if (fDC) { |
| 296 | DeleteDC(fDC); |
| 297 | } |
| 298 | if (fBM) { |
| 299 | DeleteObject(fBM); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void init(HFONT font, const XFORM& xform) { |
| 304 | fFont = font; |
| 305 | fXform = xform; |
| 306 | } |
| 307 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 308 | const void* draw(const SkGlyph&, bool isBW, SkGdiRGB fgColor, |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 309 | size_t* srcRBPtr); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 310 | |
| 311 | private: |
| 312 | HDC fDC; |
| 313 | HBITMAP fBM; |
| 314 | HFONT fFont; |
| 315 | XFORM fXform; |
| 316 | void* fBits; // points into fBM |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 317 | COLORREF fColor; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 318 | int fWidth; |
| 319 | int fHeight; |
| 320 | bool fIsBW; |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 321 | |
| 322 | enum { |
| 323 | // will always trigger us to reset the color, since we |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 324 | // should only store 0 or 0x00FFFFFF or gray (0x007F7F7F) |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 325 | kInvalid_Color = 12345 |
| 326 | }; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 329 | const void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW, |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 330 | SkGdiRGB fgColor, size_t* srcRBPtr) { |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 331 | if (0 == fDC) { |
| 332 | fDC = CreateCompatibleDC(0); |
| 333 | if (0 == fDC) { |
| 334 | return NULL; |
| 335 | } |
| 336 | SetGraphicsMode(fDC, GM_ADVANCED); |
| 337 | SetBkMode(fDC, TRANSPARENT); |
| 338 | SetTextAlign(fDC, TA_LEFT | TA_BASELINE); |
| 339 | SelectObject(fDC, fFont); |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 340 | fColor = kInvalid_Color; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (fBM && (fIsBW != isBW || fWidth < glyph.fWidth || fHeight < glyph.fHeight)) { |
| 344 | DeleteObject(fBM); |
| 345 | fBM = 0; |
| 346 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 347 | fIsBW = isBW; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 348 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 349 | COLORREF color = fgColor; |
| 350 | if (fIsBW) { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 351 | color = 0xFFFFFF; |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 352 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 353 | if (fColor != color) { |
| 354 | fColor = color; |
| 355 | COLORREF prev = SetTextColor(fDC, color); |
| 356 | SkASSERT(prev != CLR_INVALID); |
| 357 | } |
| 358 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 359 | fWidth = SkMax32(fWidth, glyph.fWidth); |
| 360 | fHeight = SkMax32(fHeight, glyph.fHeight); |
| 361 | |
| 362 | int biWidth = isBW ? alignTo32(fWidth) : fWidth; |
| 363 | |
| 364 | if (0 == fBM) { |
| 365 | MyBitmapInfo info; |
| 366 | sk_bzero(&info, sizeof(info)); |
| 367 | if (isBW) { |
| 368 | RGBQUAD blackQuad = { 0, 0, 0, 0 }; |
| 369 | RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 }; |
| 370 | info.bmiColors[0] = blackQuad; |
| 371 | info.bmiColors[1] = whiteQuad; |
| 372 | } |
| 373 | info.bmiHeader.biSize = sizeof(info.bmiHeader); |
| 374 | info.bmiHeader.biWidth = biWidth; |
| 375 | info.bmiHeader.biHeight = fHeight; |
| 376 | info.bmiHeader.biPlanes = 1; |
| 377 | info.bmiHeader.biBitCount = isBW ? 1 : 32; |
| 378 | info.bmiHeader.biCompression = BI_RGB; |
| 379 | if (isBW) { |
| 380 | info.bmiHeader.biClrUsed = 2; |
| 381 | } |
| 382 | fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0); |
| 383 | if (0 == fBM) { |
| 384 | return NULL; |
| 385 | } |
| 386 | SelectObject(fDC, fBM); |
| 387 | } |
| 388 | |
| 389 | // erase |
| 390 | size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2); |
| 391 | size_t size = fHeight * srcRB; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 392 | unsigned bg = (0 == color) ? 0xFF : 0; |
| 393 | memset(fBits, bg, size); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 394 | |
| 395 | XFORM xform = fXform; |
| 396 | xform.eDx = (float)-glyph.fLeft; |
| 397 | xform.eDy = (float)-glyph.fTop; |
| 398 | SetWorldTransform(fDC, &xform); |
| 399 | |
| 400 | uint16_t glyphID = glyph.getGlyphID(); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 401 | BOOL ret = ExtTextOutW(fDC, 0, 0, ETO_GLYPH_INDEX, NULL, reinterpret_cast<LPCWSTR>(&glyphID), 1, NULL); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 402 | GdiFlush(); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 403 | if (0 == ret) { |
| 404 | return NULL; |
| 405 | } |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 406 | *srcRBPtr = srcRB; |
| 407 | // offset to the start of the image |
| 408 | return (const char*)fBits + (fHeight - glyph.fHeight) * srcRB; |
| 409 | } |
| 410 | |
| 411 | ////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 412 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 413 | class SkScalerContext_Windows : public SkScalerContext { |
| 414 | public: |
| 415 | SkScalerContext_Windows(const SkDescriptor* desc); |
| 416 | virtual ~SkScalerContext_Windows(); |
| 417 | |
| 418 | protected: |
| 419 | virtual unsigned generateGlyphCount(); |
| 420 | virtual uint16_t generateCharToGlyph(SkUnichar uni); |
| 421 | virtual void generateAdvance(SkGlyph* glyph); |
| 422 | virtual void generateMetrics(SkGlyph* glyph); |
| 423 | virtual void generateImage(const SkGlyph& glyph); |
| 424 | virtual void generatePath(const SkGlyph& glyph, SkPath* path); |
| 425 | virtual void generateFontMetrics(SkPaint::FontMetrics* mX, SkPaint::FontMetrics* mY); |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 426 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 427 | private: |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 428 | HDCOffscreen fOffscreen; |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 429 | SkScalar fScale; // to get from canonical size to real size |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 430 | MAT2 fMat22; |
| 431 | XFORM fXform; |
| 432 | HDC fDDC; |
| 433 | HFONT fSavefont; |
| 434 | HFONT fFont; |
| 435 | SCRIPT_CACHE fSC; |
| 436 | int fGlyphCount; |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 437 | |
| 438 | HFONT fHiResFont; |
| 439 | MAT2 fMat22Identity; |
| 440 | SkMatrix fHiResMatrix; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | static float mul2float(SkScalar a, SkScalar b) { |
| 444 | return SkScalarToFloat(SkScalarMul(a, b)); |
| 445 | } |
| 446 | |
| 447 | static FIXED float2FIXED(float x) { |
| 448 | return SkFixedToFIXED(SkFloatToFixed(x)); |
| 449 | } |
| 450 | |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 451 | static SkMutex gFTMutex; |
| 452 | |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 453 | #define HIRES_TEXTSIZE 2048 |
| 454 | #define HIRES_SHIFT 11 |
| 455 | static inline SkFixed HiResToFixed(int value) { |
| 456 | return value << (16 - HIRES_SHIFT); |
| 457 | } |
| 458 | |
| 459 | static bool needHiResMetrics(const SkScalar mat[2][2]) { |
| 460 | return mat[1][0] || mat[0][1]; |
| 461 | } |
| 462 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 463 | static BYTE compute_quality(const SkScalerContext::Rec& rec) { |
| 464 | switch (rec.fMaskFormat) { |
| 465 | case SkMask::kBW_Format: |
| 466 | return NONANTIALIASED_QUALITY; |
| 467 | case SkMask::kLCD16_Format: |
| 468 | case SkMask::kLCD32_Format: |
| 469 | return CLEARTYPE_QUALITY; |
| 470 | default: |
reed@google.com | 6fc3c1f | 2011-09-30 20:31:25 +0000 | [diff] [blame] | 471 | // here we just want AA, but we may have to force the issue |
| 472 | // since sometimes GDI will instead really give us BW |
| 473 | // (for some fonts and some sizes) |
| 474 | if (rec.fFlags & SkScalerContext::kForceAA_Flag) { |
| 475 | return CLEARTYPE_QUALITY; |
| 476 | } else { |
| 477 | return ANTIALIASED_QUALITY; |
| 478 | } |
| 479 | break; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 483 | SkScalerContext_Windows::SkScalerContext_Windows(const SkDescriptor* desc) |
| 484 | : SkScalerContext(desc), fDDC(0), fFont(0), fSavefont(0), fSC(0) |
| 485 | , fGlyphCount(-1) { |
| 486 | SkAutoMutexAcquire ac(gFTMutex); |
| 487 | |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 488 | fScale = fRec.fTextSize / gCanonicalTextSize; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 489 | |
| 490 | fXform.eM11 = mul2float(fScale, fRec.fPost2x2[0][0]); |
| 491 | fXform.eM12 = mul2float(fScale, fRec.fPost2x2[1][0]); |
| 492 | fXform.eM21 = mul2float(fScale, fRec.fPost2x2[0][1]); |
| 493 | fXform.eM22 = mul2float(fScale, fRec.fPost2x2[1][1]); |
| 494 | fXform.eDx = 0; |
| 495 | fXform.eDy = 0; |
| 496 | |
| 497 | fMat22.eM11 = float2FIXED(fXform.eM11); |
| 498 | fMat22.eM12 = float2FIXED(fXform.eM12); |
| 499 | fMat22.eM21 = float2FIXED(-fXform.eM21); |
| 500 | fMat22.eM22 = float2FIXED(-fXform.eM22); |
| 501 | |
| 502 | fDDC = ::CreateCompatibleDC(NULL); |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 503 | SetGraphicsMode(fDDC, GM_ADVANCED); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 504 | SetBkMode(fDDC, TRANSPARENT); |
| 505 | |
| 506 | // Scaling by the DPI is inconsistent with how Skia draws elsewhere |
| 507 | //SkScalar height = -(fRec.fTextSize * GetDeviceCaps(ddc, LOGPIXELSY) / 72); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 508 | LOGFONT lf; |
| 509 | GetLogFontByID(fRec.fFontID, &lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 510 | lf.lfHeight = -gCanonicalTextSize; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 511 | lf.lfQuality = compute_quality(fRec); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 512 | fFont = CreateFontIndirect(&lf); |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 513 | |
| 514 | // if we're rotated, or want fractional widths, create a hires font |
| 515 | fHiResFont = 0; |
| 516 | if (needHiResMetrics(fRec.fPost2x2) || (fRec.fFlags & kSubpixelPositioning_Flag)) { |
| 517 | lf.lfHeight = -HIRES_TEXTSIZE; |
| 518 | fHiResFont = CreateFontIndirect(&lf); |
| 519 | |
| 520 | fMat22Identity.eM11 = fMat22Identity.eM22 = SkFixedToFIXED(SK_Fixed1); |
| 521 | fMat22Identity.eM12 = fMat22Identity.eM21 = SkFixedToFIXED(0); |
| 522 | |
| 523 | // construct a matrix to go from HIRES logical units to our device units |
| 524 | fRec.getSingleMatrix(&fHiResMatrix); |
| 525 | SkScalar scale = SkScalarInvert(SkIntToScalar(HIRES_TEXTSIZE)); |
| 526 | fHiResMatrix.preScale(scale, scale); |
| 527 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 528 | fSavefont = (HFONT)SelectObject(fDDC, fFont); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 529 | |
| 530 | if (needToRenderWithSkia(fRec)) { |
| 531 | this->forceGenerateImageFromPath(); |
| 532 | } |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 533 | |
| 534 | fOffscreen.init(fFont, fXform); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | SkScalerContext_Windows::~SkScalerContext_Windows() { |
| 538 | if (fDDC) { |
| 539 | ::SelectObject(fDDC, fSavefont); |
| 540 | ::DeleteDC(fDDC); |
| 541 | } |
| 542 | if (fFont) { |
| 543 | ::DeleteObject(fFont); |
| 544 | } |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 545 | if (fHiResFont) { |
| 546 | ::DeleteObject(fHiResFont); |
| 547 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 548 | if (fSC) { |
| 549 | ::ScriptFreeCache(&fSC); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | unsigned SkScalerContext_Windows::generateGlyphCount() { |
| 554 | if (fGlyphCount < 0) { |
| 555 | fGlyphCount = calculateGlyphCount(fDDC); |
| 556 | } |
| 557 | return fGlyphCount; |
| 558 | } |
| 559 | |
| 560 | uint16_t SkScalerContext_Windows::generateCharToGlyph(SkUnichar uni) { |
| 561 | uint16_t index = 0; |
| 562 | WCHAR c[2]; |
| 563 | // TODO(ctguil): Support characters that generate more than one glyph. |
| 564 | if (SkUTF16_FromUnichar(uni, (uint16_t*)c) == 1) { |
| 565 | // Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0. |
| 566 | SkAssertResult(GetGlyphIndicesW(fDDC, c, 1, &index, 0)); |
| 567 | } else { |
| 568 | // Use uniscribe to detemine glyph index for non-BMP characters. |
| 569 | // Need to add extra item to SCRIPT_ITEM to work around a bug in older |
| 570 | // windows versions. https://bugzilla.mozilla.org/show_bug.cgi?id=366643 |
| 571 | SCRIPT_ITEM si[2 + 1]; |
| 572 | int items; |
| 573 | SkAssertResult( |
| 574 | SUCCEEDED(ScriptItemize(c, 2, 2, NULL, NULL, si, &items))); |
| 575 | |
| 576 | WORD log[2]; |
| 577 | SCRIPT_VISATTR vsa; |
| 578 | int glyphs; |
| 579 | SkAssertResult(SUCCEEDED(ScriptShape( |
| 580 | fDDC, &fSC, c, 2, 1, &si[0].a, &index, log, &vsa, &glyphs))); |
| 581 | } |
| 582 | return index; |
| 583 | } |
| 584 | |
| 585 | void SkScalerContext_Windows::generateAdvance(SkGlyph* glyph) { |
| 586 | this->generateMetrics(glyph); |
| 587 | } |
| 588 | |
| 589 | void SkScalerContext_Windows::generateMetrics(SkGlyph* glyph) { |
| 590 | |
| 591 | SkASSERT(fDDC); |
| 592 | |
| 593 | GLYPHMETRICS gm; |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 594 | sk_bzero(&gm, sizeof(gm)); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 595 | |
| 596 | glyph->fRsbDelta = 0; |
| 597 | glyph->fLsbDelta = 0; |
| 598 | |
| 599 | // Note: need to use GGO_GRAY8_BITMAP instead of GGO_METRICS because GGO_METRICS returns a smaller |
| 600 | // BlackBlox; we need the bigger one in case we need the image. fAdvance is the same. |
| 601 | uint32_t ret = GetGlyphOutlineW(fDDC, glyph->getGlyphID(0), GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 602 | if (GDI_ERROR == ret) { |
| 603 | ensure_typeface_accessible(fRec.fFontID); |
| 604 | ret = GetGlyphOutlineW(fDDC, glyph->getGlyphID(0), GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22); |
| 605 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 606 | |
| 607 | if (GDI_ERROR != ret) { |
| 608 | if (ret == 0) { |
| 609 | // for white space, ret is zero and gmBlackBoxX, gmBlackBoxY are 1 incorrectly! |
| 610 | gm.gmBlackBoxX = gm.gmBlackBoxY = 0; |
| 611 | } |
| 612 | glyph->fWidth = gm.gmBlackBoxX; |
| 613 | glyph->fHeight = gm.gmBlackBoxY; |
| 614 | glyph->fTop = SkToS16(gm.gmptGlyphOrigin.y - gm.gmBlackBoxY); |
| 615 | glyph->fLeft = SkToS16(gm.gmptGlyphOrigin.x); |
| 616 | glyph->fAdvanceX = SkIntToFixed(gm.gmCellIncX); |
| 617 | glyph->fAdvanceY = -SkIntToFixed(gm.gmCellIncY); |
| 618 | |
reed@google.com | f8cead5 | 2011-09-02 20:08:11 +0000 | [diff] [blame] | 619 | // we outset in all dimensions, since the image may bleed outside |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 620 | // of the computed bounds returned by GetGlyphOutline. |
| 621 | // This was deduced by trial and error for small text (e.g. 8pt), so there |
| 622 | // maybe a more precise way to make this adjustment... |
reed@google.com | f8cead5 | 2011-09-02 20:08:11 +0000 | [diff] [blame] | 623 | // |
| 624 | // This test shows us clipping the tops of some of the CJK fonts unless we |
| 625 | // increase the top of the box by 2, hence the height by 4. This seems to |
| 626 | // correspond to an embedded bitmap font, but not sure. |
| 627 | // LayoutTests/fast/text/backslash-to-yen-sign-euc.html |
| 628 | // |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 629 | if (glyph->fWidth) { // don't outset an empty glyph |
| 630 | glyph->fWidth += 4; |
| 631 | glyph->fHeight += 4; |
| 632 | glyph->fTop -= 2; |
| 633 | glyph->fLeft -= 2; |
| 634 | } |
reed@google.com | 1dd17a1 | 2011-05-17 14:04:41 +0000 | [diff] [blame] | 635 | |
| 636 | if (fHiResFont) { |
| 637 | SelectObject(fDDC, fHiResFont); |
| 638 | sk_bzero(&gm, sizeof(gm)); |
| 639 | ret = GetGlyphOutlineW(fDDC, glyph->getGlyphID(0), GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, NULL, &fMat22Identity); |
| 640 | if (GDI_ERROR != ret) { |
| 641 | SkPoint advance; |
| 642 | fHiResMatrix.mapXY(SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY), &advance); |
| 643 | glyph->fAdvanceX = SkScalarToFixed(advance.fX); |
| 644 | glyph->fAdvanceY = SkScalarToFixed(advance.fY); |
| 645 | } |
| 646 | SelectObject(fDDC, fFont); |
| 647 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 648 | } else { |
| 649 | glyph->fWidth = 0; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void SkScalerContext_Windows::generateFontMetrics(SkPaint::FontMetrics* mx, SkPaint::FontMetrics* my) { |
| 654 | // Note: This code was borrowed from generateLineHeight, which has a note |
| 655 | // stating that it may be incorrect. |
| 656 | if (!(mx || my)) |
| 657 | return; |
| 658 | |
| 659 | SkASSERT(fDDC); |
| 660 | |
| 661 | OUTLINETEXTMETRIC otm; |
| 662 | |
| 663 | uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 664 | if (GDI_ERROR == ret) { |
| 665 | ensure_typeface_accessible(fRec.fFontID); |
| 666 | ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 667 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 668 | if (sizeof(otm) != ret) { |
| 669 | return; |
| 670 | } |
| 671 | |
| 672 | if (mx) { |
| 673 | mx->fTop = -fScale * otm.otmTextMetrics.tmAscent; |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 674 | mx->fAscent = -fScale * otm.otmAscent; |
| 675 | mx->fDescent = -fScale * otm.otmDescent; |
| 676 | mx->fBottom = fScale * otm.otmTextMetrics.tmDescent; |
| 677 | mx->fLeading = fScale * (otm.otmTextMetrics.tmInternalLeading |
| 678 | + otm.otmTextMetrics.tmExternalLeading); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (my) { |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 682 | my->fTop = -fScale * otm.otmTextMetrics.tmAscent; |
| 683 | my->fAscent = -fScale * otm.otmAscent; |
| 684 | my->fDescent = -fScale * otm.otmDescent; |
| 685 | my->fBottom = fScale * otm.otmTextMetrics.tmDescent; |
| 686 | my->fLeading = fScale * (otm.otmTextMetrics.tmInternalLeading |
| 687 | + otm.otmTextMetrics.tmExternalLeading); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 691 | //////////////////////////////////////////////////////////////////////////////////////// |
| 692 | |
| 693 | static void build_power_table(uint8_t table[], float ee) { |
| 694 | for (int i = 0; i < 256; i++) { |
| 695 | float x = i / 255.f; |
| 696 | x = powf(x, ee); |
| 697 | int xx = SkScalarRound(SkFloatToScalar(x * 255)); |
| 698 | table[i] = SkToU8(xx); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // This will invert the gamma applied by GDI, so we can sort-of get linear values. |
| 703 | // Needed when we draw non-black, non-white text, and don't know how to bias it. |
| 704 | static const uint8_t* getInverseGammaTable() { |
| 705 | static bool gInited; |
| 706 | static uint8_t gTable[256]; |
| 707 | if (!gInited) { |
| 708 | UINT level = 0; |
| 709 | if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &level, 0) || !level) { |
| 710 | // can't get the data, so use a default |
| 711 | level = 1400; |
| 712 | } |
| 713 | build_power_table(gTable, level / 1000.0f); |
| 714 | gInited = true; |
| 715 | } |
| 716 | return gTable; |
| 717 | } |
| 718 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 719 | #include "SkColorPriv.h" |
| 720 | |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 721 | // gdi's bitmap is upside-down, so we reverse dst walking in Y |
| 722 | // whenever we copy it into skia's buffer |
| 723 | |
| 724 | static inline uint8_t rgb_to_a8(SkGdiRGB rgb) { |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 725 | int r = (rgb >> 16) & 0xFF; |
| 726 | int g = (rgb >> 8) & 0xFF; |
| 727 | int b = (rgb >> 0) & 0xFF; |
| 728 | |
reed@google.com | 6fc3c1f | 2011-09-30 20:31:25 +0000 | [diff] [blame] | 729 | return (r * 2 + g * 5 + b) >> 3; // luminance |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 730 | } |
| 731 | |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 732 | static inline uint16_t rgb_to_lcd16(SkGdiRGB rgb) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 733 | int r = (rgb >> 16) & 0xFF; |
| 734 | int g = (rgb >> 8) & 0xFF; |
| 735 | int b = (rgb >> 0) & 0xFF; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 736 | return SkPackRGB16(SkR32ToR16(r), SkG32ToG16(g), SkB32ToB16(b)); |
| 737 | } |
| 738 | |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 739 | static inline SkPMColor rgb_to_lcd32(SkGdiRGB rgb) { |
| 740 | int r = (rgb >> 16) & 0xFF; |
| 741 | int g = (rgb >> 8) & 0xFF; |
| 742 | int b = (rgb >> 0) & 0xFF; |
| 743 | int a = SkMax32(r, SkMax32(g, b)); |
| 744 | return SkPackARGB32(a, r, g, b); |
| 745 | } |
| 746 | |
reed@google.com | 82cff02 | 2011-09-22 14:33:40 +0000 | [diff] [blame] | 747 | // Is this GDI color neither black nor white? If so, we have to keep this |
| 748 | // image as is, rather than smashing it down to a BW mask. |
| 749 | // |
| 750 | // returns int instead of bool, since we don't want/have to pay to convert |
| 751 | // the zero/non-zero value into a bool |
| 752 | static int is_not_black_or_white(SkGdiRGB c) { |
| 753 | // same as (but faster than) |
| 754 | // c &= 0x00FFFFFF; |
| 755 | // return 0 == c || 0x00FFFFFF == c; |
| 756 | return (c + (c & 1)) & 0x00FFFFFF; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static bool is_rgb_really_bw(const SkGdiRGB* src, int width, int height, int srcRB) { |
| 760 | for (int y = 0; y < height; ++y) { |
| 761 | for (int x = 0; x < width; ++x) { |
reed@google.com | 82cff02 | 2011-09-22 14:33:40 +0000 | [diff] [blame] | 762 | if (is_not_black_or_white(src[x])) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | } |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 766 | src = SkTAddByteOffset(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 767 | } |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | static void rgb_to_bw(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 772 | const SkGlyph& glyph, int32_t xorMask) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 773 | const int width = glyph.fWidth; |
| 774 | const size_t dstRB = (width + 7) >> 3; |
| 775 | uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 776 | |
| 777 | int byteCount = width >> 3; |
| 778 | int bitCount = width & 7; |
| 779 | |
| 780 | // adjust srcRB to skip the values in our byteCount loop, |
| 781 | // since we increment src locally there |
| 782 | srcRB -= byteCount * 8 * sizeof(SkGdiRGB); |
| 783 | |
| 784 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 785 | if (byteCount > 0) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 786 | for (int i = 0; i < byteCount; ++i) { |
reed@google.com | 7a23014 | 2011-09-27 14:07:21 +0000 | [diff] [blame] | 787 | unsigned byte = 0; |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 788 | byte |= (src[0] ^ xorMask) & (1 << 7); |
| 789 | byte |= (src[1] ^ xorMask) & (1 << 6); |
| 790 | byte |= (src[2] ^ xorMask) & (1 << 5); |
| 791 | byte |= (src[3] ^ xorMask) & (1 << 4); |
| 792 | byte |= (src[4] ^ xorMask) & (1 << 3); |
| 793 | byte |= (src[5] ^ xorMask) & (1 << 2); |
| 794 | byte |= (src[6] ^ xorMask) & (1 << 1); |
| 795 | byte |= (src[7] ^ xorMask) & (1 << 0); |
reed@google.com | 6a8f14d | 2011-09-27 12:54:24 +0000 | [diff] [blame] | 796 | dst[i] = byte; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 797 | src += 8; |
| 798 | } |
| 799 | } |
| 800 | if (bitCount > 0) { |
| 801 | unsigned byte = 0; |
| 802 | unsigned mask = 0x80; |
| 803 | for (int i = 0; i < bitCount; i++) { |
reed@google.com | 6a8f14d | 2011-09-27 12:54:24 +0000 | [diff] [blame] | 804 | byte |= (src[i] ^ xorMask) & mask; |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 805 | mask >>= 1; |
| 806 | } |
| 807 | dst[byteCount] = byte; |
| 808 | } |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 809 | src = SkTAddByteOffset(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 810 | dst -= dstRB; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | static void rgb_to_a8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 815 | const SkGlyph& glyph, int32_t xorMask) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 816 | const size_t dstRB = glyph.rowBytes(); |
| 817 | const int width = glyph.fWidth; |
| 818 | uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 819 | |
| 820 | for (int y = 0; y < glyph.fHeight; y++) { |
| 821 | for (int i = 0; i < width; i++) { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 822 | dst[i] = rgb_to_a8(src[i] ^ xorMask); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 823 | } |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 824 | src = SkTAddByteOffset(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 825 | dst -= dstRB; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | static void rgb_to_lcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 830 | const SkGlyph& glyph, int32_t xorMask) { |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 831 | const size_t dstRB = glyph.rowBytes(); |
| 832 | const int width = glyph.fWidth; |
| 833 | uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 834 | |
| 835 | for (int y = 0; y < glyph.fHeight; y++) { |
| 836 | for (int i = 0; i < width; i++) { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 837 | dst[i] = rgb_to_lcd16(src[i] ^ xorMask); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 838 | } |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 839 | src = SkTAddByteOffset(src, srcRB); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 840 | dst = (uint16_t*)((char*)dst - dstRB); |
| 841 | } |
| 842 | } |
| 843 | |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 844 | static void rgb_to_lcd32(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
| 845 | const SkGlyph& glyph, int32_t xorMask) { |
| 846 | const size_t dstRB = glyph.rowBytes(); |
| 847 | const int width = glyph.fWidth; |
| 848 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 849 | |
| 850 | for (int y = 0; y < glyph.fHeight; y++) { |
| 851 | for (int i = 0; i < width; i++) { |
| 852 | dst[i] = rgb_to_lcd32(src[i] ^ xorMask); |
| 853 | } |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 854 | src = SkTAddByteOffset(src, srcRB); |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 855 | dst = (SkPMColor*)((char*)dst - dstRB); |
| 856 | } |
| 857 | } |
| 858 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 859 | static inline unsigned clamp255(unsigned x) { |
| 860 | SkASSERT(x <= 256); |
| 861 | return x - (x >> 8); |
| 862 | } |
| 863 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 864 | void SkScalerContext_Windows::generateImage(const SkGlyph& glyph) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 865 | SkAutoMutexAcquire ac(gFTMutex); |
| 866 | |
| 867 | SkASSERT(fDDC); |
| 868 | |
reed@google.com | 6271117 | 2011-05-18 15:08:10 +0000 | [diff] [blame] | 869 | const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 870 | const bool isAA = !isLCD(fRec); |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 871 | |
| 872 | bool isWhite = SkToBool(fRec.fFlags & SkScalerContext::kGammaForWhite_Flag); |
| 873 | bool isBlack = SkToBool(fRec.fFlags & SkScalerContext::kGammaForBlack_Flag); |
| 874 | SkASSERT(!(isWhite && isBlack)); |
| 875 | SkASSERT(!isBW || (!isWhite && !isBlack)); |
| 876 | |
| 877 | SkGdiRGB fgColor; |
| 878 | uint32_t rgbXOR; |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 879 | const uint8_t* table = NULL; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 880 | if (isBW || isWhite) { |
| 881 | fgColor = 0x00FFFFFF; |
| 882 | rgbXOR = 0; |
| 883 | } else if (isBlack) { |
| 884 | fgColor = 0; |
| 885 | rgbXOR = ~0; |
| 886 | } else { |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 887 | table = getInverseGammaTable(); |
| 888 | fgColor = 0x00FFFFFF; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 889 | rgbXOR = 0; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 890 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 891 | |
reed@google.com | 99edd43 | 2011-09-09 14:59:59 +0000 | [diff] [blame] | 892 | size_t srcRB; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 893 | const void* bits = fOffscreen.draw(glyph, isBW, fgColor, &srcRB); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 894 | if (NULL == bits) { |
| 895 | ensure_typeface_accessible(fRec.fFontID); |
| 896 | bits = fOffscreen.draw(glyph, isBW, fgColor, &srcRB); |
| 897 | if (NULL == bits) { |
| 898 | sk_bzero(glyph.fImage, glyph.computeImageSize()); |
| 899 | return; |
| 900 | } |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 901 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 902 | |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 903 | if (table) { |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 904 | SkGdiRGB* addr = (SkGdiRGB*)bits; |
| 905 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 906 | for (int x = 0; x < glyph.fWidth; ++x) { |
| 907 | int r = (addr[x] >> 16) & 0xFF; |
| 908 | int g = (addr[x] >> 8) & 0xFF; |
| 909 | int b = (addr[x] >> 0) & 0xFF; |
reed@google.com | 7430a33 | 2011-10-03 14:37:38 +0000 | [diff] [blame] | 910 | addr[x] = (table[r] << 16) | (table[g] << 8) | table[b]; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 911 | } |
| 912 | addr = SkTAddByteOffset(addr, srcRB); |
| 913 | } |
| 914 | } |
| 915 | |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 916 | int width = glyph.fWidth; |
| 917 | size_t dstRB = glyph.rowBytes(); |
| 918 | if (isBW) { |
| 919 | const uint8_t* src = (const uint8_t*)bits; |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 920 | uint8_t* dst = (uint8_t*)((char*)glyph.fImage + (glyph.fHeight - 1) * dstRB); |
| 921 | for (int y = 0; y < glyph.fHeight; y++) { |
| 922 | memcpy(dst, src, dstRB); |
| 923 | src += srcRB; |
| 924 | dst -= dstRB; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 925 | } |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 926 | } else if (isAA) { |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 927 | // since the caller may require A8 for maskfilters, we can't check for BW |
| 928 | // ... until we have the caller tell us that explicitly |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 929 | const SkGdiRGB* src = (const SkGdiRGB*)bits; |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 930 | rgb_to_a8(src, srcRB, glyph, rgbXOR); |
reed@google.com | 82a34d8 | 2011-07-26 19:33:08 +0000 | [diff] [blame] | 931 | } else { // LCD16 |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 932 | const SkGdiRGB* src = (const SkGdiRGB*)bits; |
| 933 | if (is_rgb_really_bw(src, width, glyph.fHeight, srcRB)) { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 934 | rgb_to_bw(src, srcRB, glyph, rgbXOR); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 935 | ((SkGlyph*)&glyph)->fMaskFormat = SkMask::kBW_Format; |
| 936 | } else { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 937 | if (SkMask::kLCD16_Format == glyph.fMaskFormat) { |
| 938 | rgb_to_lcd16(src, srcRB, glyph, rgbXOR); |
| 939 | } else { |
| 940 | SkASSERT(SkMask::kLCD32_Format == glyph.fMaskFormat); |
| 941 | rgb_to_lcd32(src, srcRB, glyph, rgbXOR); |
| 942 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | void SkScalerContext_Windows::generatePath(const SkGlyph& glyph, SkPath* path) { |
| 948 | |
| 949 | SkAutoMutexAcquire ac(gFTMutex); |
| 950 | |
| 951 | SkASSERT(&glyph && path); |
| 952 | SkASSERT(fDDC); |
| 953 | |
| 954 | path->reset(); |
| 955 | |
| 956 | #if 0 |
| 957 | char buf[1024]; |
| 958 | sprintf(buf, "generatePath: id:%d, w=%d, h=%d, font:%s,fh:%d\n", glyph.fID, glyph.fWidth, glyph.fHeight, lf.lfFaceName, lf.lfHeight); |
| 959 | OutputDebugString(buf); |
| 960 | #endif |
| 961 | |
| 962 | GLYPHMETRICS gm; |
| 963 | uint32_t total_size = GetGlyphOutlineW(fDDC, glyph.fID, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, BUFFERSIZE, glyphbuf, &fMat22); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 964 | if (GDI_ERROR == total_size) { |
| 965 | ensure_typeface_accessible(fRec.fFontID); |
| 966 | total_size = GetGlyphOutlineW(fDDC, glyph.fID, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, BUFFERSIZE, glyphbuf, &fMat22); |
| 967 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 968 | |
| 969 | if (GDI_ERROR != total_size) { |
| 970 | |
| 971 | const uint8_t* cur_glyph = glyphbuf; |
| 972 | const uint8_t* end_glyph = glyphbuf + total_size; |
| 973 | |
| 974 | while(cur_glyph < end_glyph) { |
| 975 | const TTPOLYGONHEADER* th = (TTPOLYGONHEADER*)cur_glyph; |
| 976 | |
| 977 | const uint8_t* end_poly = cur_glyph + th->cb; |
| 978 | const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER); |
| 979 | |
| 980 | path->moveTo(SkFixedToScalar(*(SkFixed*)(&th->pfxStart.x)), SkFixedToScalar(*(SkFixed*)(&th->pfxStart.y))); |
| 981 | |
| 982 | while(cur_poly < end_poly) { |
| 983 | const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly; |
| 984 | |
| 985 | if (pc->wType == TT_PRIM_LINE) { |
| 986 | for (uint16_t i = 0; i < pc->cpfx; i++) { |
| 987 | path->lineTo(SkFixedToScalar(*(SkFixed*)(&pc->apfx[i].x)), SkFixedToScalar(*(SkFixed*)(&pc->apfx[i].y))); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | if (pc->wType == TT_PRIM_QSPLINE) { |
| 992 | for (uint16_t u = 0; u < pc->cpfx - 1; u++) { // Walk through points in spline |
| 993 | POINTFX pnt_b = pc->apfx[u]; // B is always the current point |
| 994 | POINTFX pnt_c = pc->apfx[u+1]; |
| 995 | |
| 996 | if (u < pc->cpfx - 2) { // If not on last spline, compute C |
| 997 | pnt_c.x = SkFixedToFIXED(SkFixedAve(*(SkFixed*)(&pnt_b.x), *(SkFixed*)(&pnt_c.x))); |
| 998 | pnt_c.y = SkFixedToFIXED(SkFixedAve(*(SkFixed*)(&pnt_b.y), *(SkFixed*)(&pnt_c.y))); |
| 999 | } |
| 1000 | |
| 1001 | path->quadTo(SkFixedToScalar(*(SkFixed*)(&pnt_b.x)), SkFixedToScalar(*(SkFixed*)(&pnt_b.y)), SkFixedToScalar(*(SkFixed*)(&pnt_c.x)), SkFixedToScalar(*(SkFixed*)(&pnt_c.y))); |
| 1002 | } |
| 1003 | } |
| 1004 | cur_poly += sizeof(uint16_t) * 2 + sizeof(POINTFX) * pc->cpfx; |
| 1005 | } |
| 1006 | cur_glyph += th->cb; |
| 1007 | path->close(); |
| 1008 | } |
| 1009 | } |
| 1010 | else { |
| 1011 | SkASSERT(false); |
| 1012 | } |
| 1013 | //char buf[1024]; |
| 1014 | //sprintf(buf, "generatePath: count:%d\n", count); |
| 1015 | //OutputDebugString(buf); |
| 1016 | } |
| 1017 | |
| 1018 | void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { |
| 1019 | SkASSERT(!"SkFontHost::Serialize unimplemented"); |
| 1020 | } |
| 1021 | |
| 1022 | SkTypeface* SkFontHost::Deserialize(SkStream* stream) { |
| 1023 | SkASSERT(!"SkFontHost::Deserialize unimplemented"); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | static bool getWidthAdvance(HDC hdc, int gId, int16_t* advance) { |
| 1028 | // Initialize the MAT2 structure to the identify transformation matrix. |
| 1029 | static const MAT2 mat2 = {SkScalarToFIXED(1), SkScalarToFIXED(0), |
| 1030 | SkScalarToFIXED(0), SkScalarToFIXED(1)}; |
| 1031 | int flags = GGO_METRICS | GGO_GLYPH_INDEX; |
| 1032 | GLYPHMETRICS gm; |
| 1033 | if (GDI_ERROR == GetGlyphOutline(hdc, gId, flags, &gm, 0, NULL, &mat2)) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | SkASSERT(advance); |
| 1037 | *advance = gm.gmCellIncX; |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
| 1041 | // static |
| 1042 | SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics( |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 1043 | uint32_t fontID, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1044 | SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, |
| 1045 | const uint32_t* glyphIDs, |
| 1046 | uint32_t glyphIDsCount) { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1047 | LOGFONT lf; |
| 1048 | GetLogFontByID(fontID, &lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1049 | SkAdvancedTypefaceMetrics* info = NULL; |
| 1050 | |
| 1051 | HDC hdc = CreateCompatibleDC(NULL); |
| 1052 | HFONT font = CreateFontIndirect(&lf); |
| 1053 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 1054 | HFONT designFont = NULL; |
| 1055 | |
| 1056 | // To request design units, create a logical font whose height is specified |
| 1057 | // as unitsPerEm. |
| 1058 | OUTLINETEXTMETRIC otm; |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1059 | unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1060 | if (0 == otmRet) { |
| 1061 | ensure_typeface_accessible(fontID); |
| 1062 | otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1063 | } |
| 1064 | if (!otmRet || !GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) { |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1065 | goto Error; |
| 1066 | } |
| 1067 | lf.lfHeight = -SkToS32(otm.otmEMSquare); |
| 1068 | designFont = CreateFontIndirect(&lf); |
| 1069 | SelectObject(hdc, designFont); |
| 1070 | if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) { |
| 1071 | goto Error; |
| 1072 | } |
| 1073 | const unsigned glyphCount = calculateGlyphCount(hdc); |
| 1074 | |
| 1075 | info = new SkAdvancedTypefaceMetrics; |
| 1076 | info->fEmSize = otm.otmEMSquare; |
| 1077 | info->fMultiMaster = false; |
| 1078 | info->fLastGlyphID = SkToU16(glyphCount - 1); |
| 1079 | info->fStyle = 0; |
| 1080 | #ifdef UNICODE |
| 1081 | // Get the buffer size needed first. |
| 1082 | size_t str_len = WideCharToMultiByte(CP_UTF8, 0, lf.lfFaceName, -1, NULL, |
| 1083 | 0, NULL, NULL); |
| 1084 | // Allocate a buffer (str_len already has terminating null accounted for). |
| 1085 | char *familyName = new char[str_len]; |
| 1086 | // Now actually convert the string. |
| 1087 | WideCharToMultiByte(CP_UTF8, 0, lf.lfFaceName, -1, familyName, str_len, |
| 1088 | NULL, NULL); |
| 1089 | info->fFontName.set(familyName); |
| 1090 | delete [] familyName; |
| 1091 | #else |
| 1092 | info->fFontName.set(lf.lfFaceName); |
| 1093 | #endif |
| 1094 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 1095 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo) { |
| 1096 | populate_glyph_to_unicode(hdc, glyphCount, &(info->fGlyphToUnicode)); |
| 1097 | } |
| 1098 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1099 | if (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE) { |
| 1100 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 1101 | } else { |
| 1102 | info->fType = SkAdvancedTypefaceMetrics::kOther_Font; |
| 1103 | info->fItalicAngle = 0; |
| 1104 | info->fAscent = 0; |
| 1105 | info->fDescent = 0; |
| 1106 | info->fStemV = 0; |
| 1107 | info->fCapHeight = 0; |
| 1108 | info->fBBox = SkIRect::MakeEmpty(); |
| 1109 | return info; |
| 1110 | } |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 1111 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1112 | // If this bit is clear the font is a fixed pitch font. |
| 1113 | if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) { |
| 1114 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 1115 | } |
| 1116 | if (otm.otmTextMetrics.tmItalic) { |
| 1117 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 1118 | } |
| 1119 | // Setting symbolic style by default for now. |
| 1120 | info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style; |
| 1121 | if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) { |
| 1122 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 1123 | } else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) { |
| 1124 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 1125 | } |
| 1126 | |
| 1127 | // The main italic angle of the font, in tenths of a degree counterclockwise |
| 1128 | // from vertical. |
| 1129 | info->fItalicAngle = otm.otmItalicAngle / 10; |
| 1130 | info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent); |
| 1131 | info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent); |
| 1132 | // TODO(ctguil): Use alternate cap height calculation. |
| 1133 | // MSDN says otmsCapEmHeight is not support but it is returning a value on |
| 1134 | // my Win7 box. |
| 1135 | info->fCapHeight = otm.otmsCapEmHeight; |
| 1136 | info->fBBox = |
| 1137 | SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top, |
| 1138 | otm.otmrcFontBox.right, otm.otmrcFontBox.bottom); |
| 1139 | |
| 1140 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 1141 | // This probably isn't very good with an italic font. |
| 1142 | int16_t min_width = SHRT_MAX; |
| 1143 | info->fStemV = 0; |
| 1144 | char stem_chars[] = {'i', 'I', '!', '1'}; |
| 1145 | for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) { |
| 1146 | ABC abcWidths; |
| 1147 | if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) { |
| 1148 | int16_t width = abcWidths.abcB; |
| 1149 | if (width > 0 && width < min_width) { |
| 1150 | min_width = width; |
| 1151 | info->fStemV = min_width; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // If bit 1 is set, the font may not be embedded in a document. |
| 1157 | // If bit 1 is clear, the font can be embedded. |
| 1158 | // If bit 2 is set, the embedding is read-only. |
| 1159 | if (otm.otmfsType & 0x1) { |
| 1160 | info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
ctguil@chromium.org | 0e6dc0a | 2011-03-30 20:41:16 +0000 | [diff] [blame] | 1161 | } else if (perGlyphInfo & |
| 1162 | SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) { |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 1163 | if (info->fStyle & SkAdvancedTypefaceMetrics::kFixedPitch_Style) { |
| 1164 | appendRange(&info->fGlyphWidths, 0); |
| 1165 | info->fGlyphWidths->fAdvance.append(1, &min_width); |
| 1166 | finishRange(info->fGlyphWidths.get(), 0, |
| 1167 | SkAdvancedTypefaceMetrics::WidthRange::kDefault); |
| 1168 | } else { |
| 1169 | info->fGlyphWidths.reset( |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 1170 | getAdvanceData(hdc, |
| 1171 | glyphCount, |
| 1172 | glyphIDs, |
| 1173 | glyphIDsCount, |
| 1174 | &getWidthAdvance)); |
ctguil@chromium.org | 5aa937b | 2011-08-04 01:01:24 +0000 | [diff] [blame] | 1175 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | Error: |
| 1179 | SelectObject(hdc, savefont); |
| 1180 | DeleteObject(designFont); |
| 1181 | DeleteObject(font); |
| 1182 | DeleteDC(hdc); |
| 1183 | |
| 1184 | return info; |
| 1185 | } |
| 1186 | |
| 1187 | SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
| 1188 | |
| 1189 | //Should not be used on Windows, keep linker happy |
| 1190 | SkASSERT(false); |
| 1191 | return SkCreateTypefaceFromLOGFONT(get_default_font()); |
| 1192 | } |
| 1193 | |
| 1194 | SkStream* SkFontHost::OpenStream(SkFontID uniqueID) { |
ctguil@chromium.org | f4c2622 | 2011-05-16 22:00:05 +0000 | [diff] [blame] | 1195 | const DWORD kTTCTag = |
| 1196 | SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f')); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1197 | LOGFONT lf; |
| 1198 | GetLogFontByID(uniqueID, &lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1199 | |
| 1200 | HDC hdc = ::CreateCompatibleDC(NULL); |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1201 | HFONT font = CreateFontIndirect(&lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1202 | HFONT savefont = (HFONT)SelectObject(hdc, font); |
| 1203 | |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1204 | SkMemoryStream* stream = NULL; |
| 1205 | DWORD tables[2] = {kTTCTag, 0}; |
| 1206 | for (int i = 0; i < SK_ARRAY_COUNT(tables); i++) { |
| 1207 | size_t bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0); |
bungeman@google.com | 39698b1 | 2011-11-15 22:26:41 +0000 | [diff] [blame] | 1208 | if (bufferSize == GDI_ERROR) { |
| 1209 | ensure_typeface_accessible(uniqueID); |
| 1210 | bufferSize = GetFontData(hdc, tables[i], 0, NULL, 0); |
| 1211 | } |
vandebo@chromium.org | d604481 | 2011-05-13 03:41:29 +0000 | [diff] [blame] | 1212 | if (bufferSize != GDI_ERROR) { |
| 1213 | stream = new SkMemoryStream(bufferSize); |
| 1214 | if (GetFontData(hdc, tables[i], 0, (void*)stream->getMemoryBase(), |
| 1215 | bufferSize)) { |
| 1216 | break; |
| 1217 | } else { |
| 1218 | delete stream; |
| 1219 | stream = NULL; |
| 1220 | } |
| 1221 | } |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | SelectObject(hdc, savefont); |
| 1225 | DeleteObject(font); |
| 1226 | DeleteDC(hdc); |
| 1227 | |
| 1228 | return stream; |
| 1229 | } |
| 1230 | |
| 1231 | SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) { |
| 1232 | return SkNEW_ARGS(SkScalerContext_Windows, (desc)); |
| 1233 | } |
| 1234 | |
| 1235 | /** Return the closest matching typeface given either an existing family |
| 1236 | (specified by a typeface in that family) or by a familyName, and a |
| 1237 | requested style. |
bungeman@google.com | 90d812b | 2011-10-24 21:25:01 +0000 | [diff] [blame] | 1238 | 1) If familyFace is null, use familyName. |
| 1239 | 2) If familyName is null, use familyFace. |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1240 | 3) If both are null, return the default font that best matches style |
| 1241 | This MUST not return NULL. |
| 1242 | */ |
| 1243 | |
| 1244 | SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, |
| 1245 | const char familyName[], |
| 1246 | const void* data, size_t bytelength, |
| 1247 | SkTypeface::Style style) { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1248 | LOGFONT lf; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1249 | if (NULL == familyFace && NULL == familyName) { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1250 | lf = get_default_font(); |
| 1251 | } else if (familyFace) { |
| 1252 | LogFontTypeface* face = (LogFontTypeface*)familyFace; |
| 1253 | lf = face->fLogFont; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1254 | } else { |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1255 | memset(&lf, 0, sizeof(LOGFONT)); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1256 | #ifdef UNICODE |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1257 | // Get the buffer size needed first. |
| 1258 | size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName, |
| 1259 | -1, NULL, 0); |
| 1260 | // Allocate a buffer (str_len already has terminating null |
| 1261 | // accounted for). |
| 1262 | wchar_t *wideFamilyName = new wchar_t[str_len]; |
| 1263 | // Now actually convert the string. |
| 1264 | ::MultiByteToWideChar(CP_UTF8, 0, familyName, -1, |
| 1265 | wideFamilyName, str_len); |
| 1266 | ::wcsncpy(lf.lfFaceName, wideFamilyName, LF_FACESIZE); |
| 1267 | delete [] wideFamilyName; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1268 | #else |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1269 | ::strncpy(lf.lfFaceName, familyName, LF_FACESIZE); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1270 | #endif |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1271 | lf.lfFaceName[LF_FACESIZE-1] = '\0'; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1272 | } |
reed@google.com | 59d2f63 | 2011-05-02 19:36:59 +0000 | [diff] [blame] | 1273 | setStyle(&lf, style); |
| 1274 | return SkCreateTypefaceFromLOGFONT(lf); |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1277 | SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
| 1278 | printf("SkFontHost::CreateTypefaceFromFile unimplemented"); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | void SkFontHost::FilterRec(SkScalerContext::Rec* rec) { |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 1283 | unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag | |
| 1284 | SkScalerContext::kAutohinting_Flag | |
| 1285 | SkScalerContext::kEmbeddedBitmapText_Flag | |
| 1286 | SkScalerContext::kEmbolden_Flag | |
| 1287 | SkScalerContext::kLCD_BGROrder_Flag | |
| 1288 | SkScalerContext::kLCD_Vertical_Flag; |
| 1289 | rec->fFlags &= ~flagsWeDontSupport; |
| 1290 | |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 1291 | SkPaint::Hinting h = rec->getHinting(); |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 1292 | |
| 1293 | // I think we can support no-hinting, if we get hires outlines and just |
| 1294 | // use skia to rasterize into a gray-scale mask... |
| 1295 | #if 0 |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 1296 | switch (h) { |
| 1297 | case SkPaint::kNo_Hinting: |
| 1298 | case SkPaint::kSlight_Hinting: |
| 1299 | h = SkPaint::kNo_Hinting; |
| 1300 | break; |
| 1301 | case SkPaint::kNormal_Hinting: |
| 1302 | case SkPaint::kFull_Hinting: |
| 1303 | h = SkPaint::kNormal_Hinting; |
| 1304 | break; |
| 1305 | default: |
| 1306 | SkASSERT(!"unknown hinting"); |
| 1307 | } |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 1308 | #else |
| 1309 | h = SkPaint::kNormal_Hinting; |
| 1310 | #endif |
reed@google.com | e8fab01 | 2011-07-13 15:25:33 +0000 | [diff] [blame] | 1311 | rec->setHinting(h); |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 1312 | |
reed@google.com | 9181aa8 | 2011-08-05 14:28:31 +0000 | [diff] [blame] | 1313 | // turn this off since GDI might turn A8 into BW! Need a bigger fix. |
| 1314 | #if 0 |
reed@google.com | da44067 | 2011-07-13 18:02:28 +0000 | [diff] [blame] | 1315 | // Disable LCD when rotated, since GDI's output is ugly |
| 1316 | if (isLCD(*rec) && !isAxisAligned(*rec)) { |
| 1317 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1318 | } |
reed@google.com | 9181aa8 | 2011-08-05 14:28:31 +0000 | [diff] [blame] | 1319 | #endif |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1320 | |
| 1321 | #if 0 |
| 1322 | if (SkMask::kLCD16_Format == rec->fMaskFormat) { |
| 1323 | rec->fMaskFormat = SkMask::kLCD32_Format; |
| 1324 | } |
| 1325 | #endif |
reed@google.com | 6fc3c1f | 2011-09-30 20:31:25 +0000 | [diff] [blame] | 1326 | // don't specify gamma if we BW (perhaps caller should do this check) |
| 1327 | if (SkMask::kBW_Format == rec->fMaskFormat) { |
| 1328 | rec->fFlags &= ~(SkScalerContext::kGammaForBlack_Flag | |
| 1329 | SkScalerContext::kGammaForWhite_Flag); |
| 1330 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | ////////////////////////////////////////////////////////////////////////// |
| 1334 | |
| 1335 | void SkFontHost::GetGammaTables(const uint8_t* tables[2]) { |
| 1336 | tables[0] = NULL; |
| 1337 | tables[1] = NULL; |
| 1338 | } |
| 1339 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1340 | static bool justAColor(const SkPaint& paint, SkColor* color) { |
| 1341 | if (paint.getShader()) { |
| 1342 | return false; |
| 1343 | } |
| 1344 | SkColor c = paint.getColor(); |
| 1345 | if (paint.getColorFilter()) { |
| 1346 | c = paint.getColorFilter()->filterColor(c); |
| 1347 | } |
| 1348 | if (color) { |
| 1349 | *color = c; |
| 1350 | } |
| 1351 | return true; |
| 1352 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1353 | |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1354 | #define BLACK_GAMMA_THRESHOLD 0x40 |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1355 | #define WHITE_GAMMA_THRESHOLD 0xA0 |
| 1356 | |
| 1357 | int SkFontHost::ComputeGammaFlag(const SkPaint& paint) { |
reed@google.com | 6f5df48 | 2011-09-28 20:33:24 +0000 | [diff] [blame] | 1358 | SkColor c; |
| 1359 | if (justAColor(paint, &c)) { |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1360 | int r = SkColorGetR(c); |
| 1361 | int g = SkColorGetG(c); |
| 1362 | int b = SkColorGetB(c); |
| 1363 | int luminance = (r * 2 + g * 5 + b) >> 3; |
| 1364 | |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1365 | if (luminance <= BLACK_GAMMA_THRESHOLD) { |
| 1366 | return SkScalerContext::kGammaForBlack_Flag; |
| 1367 | } |
reed@google.com | 754e4eb | 2011-09-26 13:21:39 +0000 | [diff] [blame] | 1368 | if (luminance >= WHITE_GAMMA_THRESHOLD) { |
| 1369 | return SkScalerContext::kGammaForWhite_Flag; |
| 1370 | } |
| 1371 | } |
| 1372 | return 0; |
reed@google.com | ac6b979 | 2011-03-11 15:42:51 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | #endif // WIN32 |