epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 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. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkDescriptor.h" |
| 14 | #include "SkFDot6.h" |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 15 | #include "SkFloatingPoint.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkFontHost.h" |
| 17 | #include "SkMask.h" |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 18 | #include "SkAdvancedTypefaceMetrics.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 19 | #include "SkScalerContext.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkStream.h" |
| 21 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkTemplates.h" |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 23 | #include "SkThread.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
| 25 | #include <ft2build.h> |
| 26 | #include FT_FREETYPE_H |
| 27 | #include FT_OUTLINE_H |
| 28 | #include FT_SIZES_H |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 29 | #include FT_TRUETYPE_TABLES_H |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 30 | #include FT_TYPE1_TABLES_H |
agl@chromium.org | e76073b | 2010-06-04 20:31:17 +0000 | [diff] [blame] | 31 | #include FT_BITMAP_H |
agl@chromium.org | 36bb697 | 2010-06-04 20:57:16 +0000 | [diff] [blame] | 32 | // In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file. |
| 33 | #include FT_SYNTHESIS_H |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 34 | #include FT_XFREE86_H |
epoger@google.com | 5070d79 | 2011-06-29 20:43:14 +0000 | [diff] [blame] | 35 | #ifdef FT_LCD_FILTER_H |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 36 | #include FT_LCD_FILTER_H |
epoger@google.com | 5070d79 | 2011-06-29 20:43:14 +0000 | [diff] [blame] | 37 | #endif |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | #ifdef FT_ADVANCES_H |
| 40 | #include FT_ADVANCES_H |
| 41 | #endif |
| 42 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 43 | #if 0 |
| 44 | // Also include the files by name for build tools which require this. |
| 45 | #include <freetype/freetype.h> |
| 46 | #include <freetype/ftoutln.h> |
| 47 | #include <freetype/ftsizes.h> |
| 48 | #include <freetype/tttables.h> |
| 49 | #include <freetype/ftadvanc.h> |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 50 | #include <freetype/ftlcdfil.h> |
agl@chromium.org | e76073b | 2010-06-04 20:31:17 +0000 | [diff] [blame] | 51 | #include <freetype/ftbitmap.h> |
agl@chromium.org | 36bb697 | 2010-06-04 20:57:16 +0000 | [diff] [blame] | 52 | #include <freetype/ftsynth.h> |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | //#define ENABLE_GLYPH_SPEW // for tracing calls |
| 56 | //#define DUMP_STRIKE_CREATION |
| 57 | |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 58 | //#define SK_GAMMA_APPLY_TO_A8 |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 59 | //#define SK_GAMMA_SRGB |
reed@google.com | ec9ed81 | 2012-03-01 19:40:21 +0000 | [diff] [blame] | 60 | |
| 61 | #ifndef SK_GAMMA_CONTRAST |
| 62 | #define SK_GAMMA_CONTRAST 0x66 |
| 63 | #endif |
| 64 | #ifndef SK_GAMMA_EXPONENT |
| 65 | #define SK_GAMMA_EXPONENT 2.2 |
| 66 | #endif |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | #ifdef SK_DEBUG |
| 69 | #define SkASSERT_CONTINUE(pred) \ |
| 70 | do { \ |
| 71 | if (!(pred)) \ |
| 72 | SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \ |
| 73 | } while (false) |
| 74 | #else |
| 75 | #define SkASSERT_CONTINUE(pred) |
| 76 | #endif |
| 77 | |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 78 | using namespace skia_advanced_typeface_metrics_utils; |
| 79 | |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 80 | static bool isLCD(const SkScalerContext::Rec& rec) { |
| 81 | switch (rec.fMaskFormat) { |
| 82 | case SkMask::kLCD16_Format: |
| 83 | case SkMask::kLCD32_Format: |
| 84 | return true; |
| 85 | default: |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | ////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | struct SkFaceRec; |
| 93 | |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 94 | SK_DECLARE_STATIC_MUTEX(gFTMutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | static int gFTCount; |
| 96 | static FT_Library gFTLibrary; |
| 97 | static SkFaceRec* gFaceRecHead; |
agl@chromium.org | f18d876 | 2009-07-28 18:38:08 +0000 | [diff] [blame] | 98 | static bool gLCDSupportValid; // true iff |gLCDSupport| has been set. |
| 99 | static bool gLCDSupport; // true iff LCD is supported by the runtime. |
reed@google.com | a1c3256 | 2012-03-01 19:38:23 +0000 | [diff] [blame] | 100 | static int gLCDExtra; // number of extra pixels for filtering. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 102 | static const uint8_t* gGammaTables[2]; |
| 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | ///////////////////////////////////////////////////////////////////////// |
| 105 | |
agl@chromium.org | e76073b | 2010-06-04 20:31:17 +0000 | [diff] [blame] | 106 | // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden |
| 107 | // This value was chosen by eyeballing the result in Firefox and trying to match it. |
| 108 | static const FT_Pos kBitmapEmboldenStrength = 1 << 6; |
| 109 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 110 | // convert from Skia's fixed (16.16) to FreeType's fixed (26.6) representation |
| 111 | static inline int FixedToDot6(SkFixed x) { return x >> 10; } |
| 112 | // convert from FreeType's fixed (26.6) to Skia's fixed (16.16) representation |
| 113 | static inline SkFixed Dot6ToFixed(int x) { return x << 10; } |
| 114 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 115 | static bool |
| 116 | InitFreetype() { |
| 117 | FT_Error err = FT_Init_FreeType(&gFTLibrary); |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 118 | if (err) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 119 | return false; |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 120 | } |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 121 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 122 | // Setup LCD filtering. This reduces colour fringes for LCD rendered |
| 123 | // glyphs. |
epoger@google.com | b371ed1 | 2011-06-29 21:20:52 +0000 | [diff] [blame] | 124 | #ifdef FT_LCD_FILTER_H |
bungeman@google.com | d2dae96 | 2012-03-09 20:31:17 +0000 | [diff] [blame] | 125 | err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT); |
| 126 | // err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT); |
agl@chromium.org | f18d876 | 2009-07-28 18:38:08 +0000 | [diff] [blame] | 127 | gLCDSupport = err == 0; |
reed@google.com | a1c3256 | 2012-03-01 19:38:23 +0000 | [diff] [blame] | 128 | if (gLCDSupport) { |
| 129 | gLCDExtra = 2; //DEFAULT and LIGHT add one pixel to each side. |
| 130 | } |
epoger@google.com | 5070d79 | 2011-06-29 20:43:14 +0000 | [diff] [blame] | 131 | #else |
| 132 | gLCDSupport = false; |
| 133 | #endif |
reed@android.com | 61608aa | 2009-07-31 14:52:54 +0000 | [diff] [blame] | 134 | gLCDSupportValid = true; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | class SkScalerContext_FreeType : public SkScalerContext { |
| 140 | public: |
| 141 | SkScalerContext_FreeType(const SkDescriptor* desc); |
| 142 | virtual ~SkScalerContext_FreeType(); |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 143 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 144 | bool success() const { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 145 | return fFaceRec != NULL && |
| 146 | fFTSize != NULL && |
| 147 | fFace != NULL; |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 148 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | |
| 150 | protected: |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 151 | virtual unsigned generateGlyphCount(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | virtual uint16_t generateCharToGlyph(SkUnichar uni); |
| 153 | virtual void generateAdvance(SkGlyph* glyph); |
| 154 | virtual void generateMetrics(SkGlyph* glyph); |
| 155 | virtual void generateImage(const SkGlyph& glyph); |
| 156 | virtual void generatePath(const SkGlyph& glyph, SkPath* path); |
| 157 | virtual void generateFontMetrics(SkPaint::FontMetrics* mx, |
| 158 | SkPaint::FontMetrics* my); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 159 | virtual SkUnichar generateGlyphToChar(uint16_t glyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | SkFaceRec* fFaceRec; |
| 163 | FT_Face fFace; // reference to shared face in gFaceRecHead |
| 164 | FT_Size fFTSize; // our own copy |
| 165 | SkFixed fScaleX, fScaleY; |
| 166 | FT_Matrix fMatrix22; |
| 167 | uint32_t fLoadGlyphFlags; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 168 | bool fDoLinearMetrics; |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 169 | bool fLCDIsVert; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | |
| 171 | FT_Error setupSize(); |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 172 | void emboldenOutline(FT_Outline* outline); |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 173 | void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox, |
| 174 | bool snapToPixelBoundary = false); |
| 175 | void updateGlyphIfLCD(SkGlyph* glyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////// |
| 179 | /////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | #include "SkStream.h" |
| 182 | |
| 183 | struct SkFaceRec { |
| 184 | SkFaceRec* fNext; |
| 185 | FT_Face fFace; |
| 186 | FT_StreamRec fFTStream; |
| 187 | SkStream* fSkStream; |
| 188 | uint32_t fRefCnt; |
| 189 | uint32_t fFontID; |
| 190 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 191 | // assumes ownership of the stream, will call unref() when its done |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | SkFaceRec(SkStream* strm, uint32_t fontID); |
| 193 | ~SkFaceRec() { |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 194 | fSkStream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
| 196 | }; |
| 197 | |
| 198 | extern "C" { |
| 199 | static unsigned long sk_stream_read(FT_Stream stream, |
| 200 | unsigned long offset, |
| 201 | unsigned char* buffer, |
| 202 | unsigned long count ) { |
| 203 | SkStream* str = (SkStream*)stream->descriptor.pointer; |
| 204 | |
| 205 | if (count) { |
| 206 | if (!str->rewind()) { |
| 207 | return 0; |
| 208 | } else { |
| 209 | unsigned long ret; |
| 210 | if (offset) { |
| 211 | ret = str->read(NULL, offset); |
| 212 | if (ret != offset) { |
| 213 | return 0; |
| 214 | } |
| 215 | } |
| 216 | ret = str->read(buffer, count); |
| 217 | if (ret != count) { |
| 218 | return 0; |
| 219 | } |
| 220 | count = ret; |
| 221 | } |
| 222 | } |
| 223 | return count; |
| 224 | } |
| 225 | |
| 226 | static void sk_stream_close( FT_Stream stream) {} |
| 227 | } |
| 228 | |
| 229 | SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID) |
vandebo@chromium.org | 9af25f3 | 2012-03-28 21:24:27 +0000 | [diff] [blame] | 230 | : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | // SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm)); |
| 232 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 233 | sk_bzero(&fFTStream, sizeof(fFTStream)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | fFTStream.size = fSkStream->getLength(); |
| 235 | fFTStream.descriptor.pointer = fSkStream; |
| 236 | fFTStream.read = sk_stream_read; |
| 237 | fFTStream.close = sk_stream_close; |
| 238 | } |
| 239 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 240 | // Will return 0 on failure |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | static SkFaceRec* ref_ft_face(uint32_t fontID) { |
| 242 | SkFaceRec* rec = gFaceRecHead; |
| 243 | while (rec) { |
| 244 | if (rec->fFontID == fontID) { |
| 245 | SkASSERT(rec->fFace); |
| 246 | rec->fRefCnt += 1; |
| 247 | return rec; |
| 248 | } |
| 249 | rec = rec->fNext; |
| 250 | } |
| 251 | |
| 252 | SkStream* strm = SkFontHost::OpenStream(fontID); |
| 253 | if (NULL == strm) { |
| 254 | SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | // this passes ownership of strm to the rec |
| 259 | rec = SkNEW_ARGS(SkFaceRec, (strm, fontID)); |
| 260 | |
| 261 | FT_Open_Args args; |
| 262 | memset(&args, 0, sizeof(args)); |
| 263 | const void* memoryBase = strm->getMemoryBase(); |
| 264 | |
| 265 | if (NULL != memoryBase) { |
| 266 | //printf("mmap(%s)\n", keyString.c_str()); |
| 267 | args.flags = FT_OPEN_MEMORY; |
| 268 | args.memory_base = (const FT_Byte*)memoryBase; |
| 269 | args.memory_size = strm->getLength(); |
| 270 | } else { |
| 271 | //printf("fopen(%s)\n", keyString.c_str()); |
| 272 | args.flags = FT_OPEN_STREAM; |
| 273 | args.stream = &rec->fFTStream; |
| 274 | } |
| 275 | |
agl@chromium.org | 61a678a | 2010-08-06 18:08:18 +0000 | [diff] [blame] | 276 | int face_index; |
| 277 | int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index); |
| 278 | FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0, |
| 279 | &rec->fFace); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | |
| 281 | if (err) { // bad filename, try the default font |
| 282 | fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID); |
| 283 | SkDELETE(rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } else { |
| 286 | SkASSERT(rec->fFace); |
| 287 | //fprintf(stderr, "Opened font '%s'\n", filename.c_str()); |
| 288 | rec->fNext = gFaceRecHead; |
| 289 | gFaceRecHead = rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | return rec; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void unref_ft_face(FT_Face face) { |
| 295 | SkFaceRec* rec = gFaceRecHead; |
| 296 | SkFaceRec* prev = NULL; |
| 297 | while (rec) { |
| 298 | SkFaceRec* next = rec->fNext; |
| 299 | if (rec->fFace == face) { |
| 300 | if (--rec->fRefCnt == 0) { |
| 301 | if (prev) { |
| 302 | prev->fNext = next; |
| 303 | } else { |
| 304 | gFaceRecHead = next; |
| 305 | } |
| 306 | FT_Done_Face(face); |
| 307 | SkDELETE(rec); |
| 308 | } |
| 309 | return; |
| 310 | } |
| 311 | prev = rec; |
| 312 | rec = next; |
| 313 | } |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 314 | SkDEBUGFAIL("shouldn't get here, face not in list"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /////////////////////////////////////////////////////////////////////////// |
| 318 | |
vandebo@chromium.org | 16be6b8 | 2011-01-28 21:28:56 +0000 | [diff] [blame] | 319 | // Work around for old versions of freetype. |
| 320 | static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count, |
| 321 | FT_Int32 loadFlags, FT_Fixed* advances) { |
| 322 | #ifdef FT_ADVANCES_H |
| 323 | return FT_Get_Advances(face, start, count, loadFlags, advances); |
| 324 | #else |
| 325 | if (!face || start >= face->num_glyphs || |
| 326 | start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) { |
| 327 | return 6; // "Invalid argument." |
| 328 | } |
| 329 | if (count == 0) |
| 330 | return 0; |
| 331 | |
| 332 | for (int i = 0; i < count; i++) { |
| 333 | FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE); |
| 334 | if (err) |
| 335 | return err; |
| 336 | advances[i] = face->glyph->advance.x; |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | #endif |
| 341 | } |
| 342 | |
| 343 | static bool canEmbed(FT_Face face) { |
djsollen@google.com | fa394d4 | 2012-01-09 20:40:25 +0000 | [diff] [blame] | 344 | #ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
vandebo@chromium.org | 16be6b8 | 2011-01-28 21:28:56 +0000 | [diff] [blame] | 345 | FT_UShort fsType = FT_Get_FSType_Flags(face); |
| 346 | return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING | |
| 347 | FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0; |
| 348 | #else |
| 349 | // No embedding is 0x2 and bitmap embedding only is 0x200. |
| 350 | TT_OS2* os2_table; |
| 351 | if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) { |
| 352 | return (os2_table->fsType & 0x202) == 0; |
| 353 | } |
| 354 | return false; // We tried, fail safe. |
| 355 | #endif |
| 356 | } |
| 357 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 358 | static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) { |
| 359 | const FT_UInt glyph_id = FT_Get_Char_Index(face, letter); |
| 360 | if (!glyph_id) |
| 361 | return false; |
| 362 | FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE); |
| 363 | FT_Outline_Get_CBox(&face->glyph->outline, bbox); |
| 364 | return true; |
| 365 | } |
| 366 | |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 367 | static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 368 | FT_Fixed advance = 0; |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 369 | if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) { |
| 370 | return false; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 371 | } |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 372 | SkASSERT(data); |
| 373 | *data = advance; |
| 374 | return true; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 375 | } |
| 376 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 377 | static void populate_glyph_to_unicode(FT_Face& face, |
| 378 | SkTDArray<SkUnichar>* glyphToUnicode) { |
| 379 | // Check and see if we have Unicode cmaps. |
| 380 | for (int i = 0; i < face->num_charmaps; ++i) { |
| 381 | // CMaps known to support Unicode: |
| 382 | // Platform ID Encoding ID Name |
| 383 | // ----------- ----------- ----------------------------------- |
| 384 | // 0 0,1 Apple Unicode |
| 385 | // 0 3 Apple Unicode 2.0 (preferred) |
| 386 | // 3 1 Microsoft Unicode UCS-2 |
| 387 | // 3 10 Microsoft Unicode UCS-4 (preferred) |
| 388 | // |
| 389 | // See Apple TrueType Reference Manual |
| 390 | // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html |
| 391 | // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID |
| 392 | // Microsoft OpenType Specification |
| 393 | // http://www.microsoft.com/typography/otspec/cmap.htm |
| 394 | |
| 395 | FT_UShort platformId = face->charmaps[i]->platform_id; |
| 396 | FT_UShort encodingId = face->charmaps[i]->encoding_id; |
| 397 | |
| 398 | if (platformId != 0 && platformId != 3) { |
| 399 | continue; |
| 400 | } |
| 401 | if (platformId == 3 && encodingId != 1 && encodingId != 10) { |
| 402 | continue; |
| 403 | } |
| 404 | bool preferredMap = ((platformId == 3 && encodingId == 10) || |
| 405 | (platformId == 0 && encodingId == 3)); |
| 406 | |
| 407 | FT_Set_Charmap(face, face->charmaps[i]); |
| 408 | if (glyphToUnicode->isEmpty()) { |
| 409 | glyphToUnicode->setCount(face->num_glyphs); |
| 410 | memset(glyphToUnicode->begin(), 0, |
| 411 | sizeof(SkUnichar) * face->num_glyphs); |
| 412 | } |
| 413 | |
| 414 | // Iterate through each cmap entry. |
| 415 | FT_UInt glyphIndex; |
| 416 | for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex); |
| 417 | glyphIndex != 0; |
| 418 | charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) { |
| 419 | if (charCode && |
| 420 | ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) { |
| 421 | (*glyphToUnicode)[glyphIndex] = charCode; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 427 | // static |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 428 | SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics( |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 429 | uint32_t fontID, |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 430 | SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, |
| 431 | const uint32_t* glyphIDs, |
| 432 | uint32_t glyphIDsCount) { |
djsollen@google.com | da95772 | 2011-11-16 17:00:46 +0000 | [diff] [blame] | 433 | #if defined(SK_BUILD_FOR_MAC) |
reed@google.com | 8a5d692 | 2011-03-14 15:08:03 +0000 | [diff] [blame] | 434 | return NULL; |
| 435 | #else |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 436 | SkAutoMutexAcquire ac(gFTMutex); |
| 437 | FT_Library libInit = NULL; |
| 438 | if (gFTCount == 0) { |
| 439 | if (!InitFreetype()) |
| 440 | sk_throw(); |
| 441 | libInit = gFTLibrary; |
| 442 | } |
| 443 | SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit); |
| 444 | SkFaceRec* rec = ref_ft_face(fontID); |
| 445 | if (NULL == rec) |
| 446 | return NULL; |
| 447 | FT_Face face = rec->fFace; |
| 448 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 449 | SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics; |
| 450 | info->fFontName.set(FT_Get_Postscript_Name(face)); |
| 451 | info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face); |
| 452 | info->fLastGlyphID = face->num_glyphs - 1; |
| 453 | info->fEmSize = 1000; |
| 454 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 455 | bool cid = false; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 456 | const char* fontType = FT_Get_X11_Font_Format(face); |
vandebo@chromium.org | c3a2ae5 | 2011-02-03 21:48:23 +0000 | [diff] [blame] | 457 | if (strcmp(fontType, "Type 1") == 0) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 458 | info->fType = SkAdvancedTypefaceMetrics::kType1_Font; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 459 | } else if (strcmp(fontType, "CID Type 1") == 0) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 460 | info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 461 | cid = true; |
| 462 | } else if (strcmp(fontType, "CFF") == 0) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 463 | info->fType = SkAdvancedTypefaceMetrics::kCFF_Font; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 464 | } else if (strcmp(fontType, "TrueType") == 0) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 465 | info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 466 | cid = true; |
| 467 | TT_Header* ttHeader; |
| 468 | if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, |
| 469 | ft_sfnt_head)) != NULL) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 470 | info->fEmSize = ttHeader->Units_Per_EM; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 473 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 474 | info->fStyle = 0; |
| 475 | if (FT_IS_FIXED_WIDTH(face)) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 476 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 477 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 478 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 479 | // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's |
| 480 | // character set is a subset of 'Adobe standard Latin.' |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 481 | info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style; |
| 482 | |
| 483 | PS_FontInfoRec ps_info; |
| 484 | TT_Postscript* tt_info; |
| 485 | if (FT_Get_PS_Font_Info(face, &ps_info) == 0) { |
| 486 | info->fItalicAngle = ps_info.italic_angle; |
| 487 | } else if ((tt_info = |
| 488 | (TT_Postscript*)FT_Get_Sfnt_Table(face, |
| 489 | ft_sfnt_post)) != NULL) { |
| 490 | info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle); |
| 491 | } else { |
| 492 | info->fItalicAngle = 0; |
| 493 | } |
| 494 | |
| 495 | info->fAscent = face->ascender; |
| 496 | info->fDescent = face->descender; |
| 497 | |
| 498 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 499 | // This probably isn't very good with an italic font. |
| 500 | int16_t min_width = SHRT_MAX; |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 501 | info->fStemV = 0; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 502 | char stem_chars[] = {'i', 'I', '!', '1'}; |
| 503 | for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) { |
| 504 | FT_BBox bbox; |
| 505 | if (GetLetterCBox(face, stem_chars[i], &bbox)) { |
| 506 | int16_t width = bbox.xMax - bbox.xMin; |
| 507 | if (width > 0 && width < min_width) { |
| 508 | min_width = width; |
| 509 | info->fStemV = min_width; |
| 510 | } |
| 511 | } |
| 512 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 513 | |
| 514 | TT_PCLT* pclt_info; |
| 515 | TT_OS2* os2_table; |
| 516 | if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) { |
| 517 | info->fCapHeight = pclt_info->CapHeight; |
| 518 | uint8_t serif_style = pclt_info->SerifStyle & 0x3F; |
| 519 | if (serif_style >= 2 && serif_style <= 6) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 520 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 521 | else if (serif_style >= 9 && serif_style <= 12) |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 522 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 523 | } else if ((os2_table = |
| 524 | (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) { |
| 525 | info->fCapHeight = os2_table->sCapHeight; |
| 526 | } else { |
| 527 | // Figure out a good guess for CapHeight: average the height of M and X. |
| 528 | FT_BBox m_bbox, x_bbox; |
| 529 | bool got_m, got_x; |
| 530 | got_m = GetLetterCBox(face, 'M', &m_bbox); |
| 531 | got_x = GetLetterCBox(face, 'X', &x_bbox); |
| 532 | if (got_m && got_x) { |
| 533 | info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax - |
| 534 | x_bbox.yMin) / 2; |
| 535 | } else if (got_m && !got_x) { |
| 536 | info->fCapHeight = m_bbox.yMax - m_bbox.yMin; |
| 537 | } else if (!got_m && got_x) { |
| 538 | info->fCapHeight = x_bbox.yMax - x_bbox.yMin; |
| 539 | } |
| 540 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 541 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 542 | info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax, |
| 543 | face->bbox.xMax, face->bbox.yMin); |
| 544 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 545 | if (!canEmbed(face) || !FT_IS_SCALABLE(face) || |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 546 | info->fType == SkAdvancedTypefaceMetrics::kOther_Font) { |
| 547 | perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo; |
| 548 | } |
| 549 | |
| 550 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 551 | if (FT_IS_FIXED_WIDTH(face)) { |
| 552 | appendRange(&info->fGlyphWidths, 0); |
| 553 | int16_t advance = face->max_advance_width; |
| 554 | info->fGlyphWidths->fAdvance.append(1, &advance); |
| 555 | finishRange(info->fGlyphWidths.get(), 0, |
| 556 | SkAdvancedTypefaceMetrics::WidthRange::kDefault); |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 557 | } else if (!cid) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 558 | appendRange(&info->fGlyphWidths, 0); |
| 559 | // So as to not blow out the stack, get advances in batches. |
| 560 | for (int gID = 0; gID < face->num_glyphs; gID += 128) { |
| 561 | FT_Fixed advances[128]; |
| 562 | int advanceCount = 128; |
| 563 | if (gID + advanceCount > face->num_glyphs) |
| 564 | advanceCount = face->num_glyphs - gID + 1; |
| 565 | getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE, |
| 566 | advances); |
| 567 | for (int i = 0; i < advanceCount; i++) { |
| 568 | int16_t advance = advances[gID + i]; |
| 569 | info->fGlyphWidths->fAdvance.append(1, &advance); |
| 570 | } |
| 571 | } |
| 572 | finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1, |
| 573 | SkAdvancedTypefaceMetrics::WidthRange::kRange); |
| 574 | } else { |
vandebo@chromium.org | 6f72d1e | 2011-02-14 23:19:59 +0000 | [diff] [blame] | 575 | info->fGlyphWidths.reset( |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 576 | getAdvanceData(face, |
| 577 | face->num_glyphs, |
| 578 | glyphIDs, |
| 579 | glyphIDsCount, |
| 580 | &getWidthAdvance)); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 581 | } |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 582 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 583 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 584 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo && |
| 585 | FT_HAS_VERTICAL(face)) { |
| 586 | SkASSERT(false); // Not implemented yet. |
| 587 | } |
| 588 | |
| 589 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo && |
| 590 | info->fType == SkAdvancedTypefaceMetrics::kType1_Font) { |
| 591 | // Postscript fonts may contain more than 255 glyphs, so we end up |
| 592 | // using multiple font descriptions with a glyph ordering. Record |
| 593 | // the name of each glyph. |
| 594 | info->fGlyphNames.reset( |
| 595 | new SkAutoTArray<SkString>(face->num_glyphs)); |
| 596 | for (int gID = 0; gID < face->num_glyphs; gID++) { |
| 597 | char glyphName[128]; // PS limit for names is 127 bytes. |
| 598 | FT_Get_Glyph_Name(face, gID, glyphName, 128); |
| 599 | info->fGlyphNames->get()[gID].set(glyphName); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
vandebo@chromium.org | 6744d49 | 2011-05-09 18:13:47 +0000 | [diff] [blame] | 603 | if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo && |
| 604 | info->fType != SkAdvancedTypefaceMetrics::kType1_Font && |
| 605 | face->num_charmaps) { |
| 606 | populate_glyph_to_unicode(face, &(info->fGlyphToUnicode)); |
| 607 | } |
| 608 | |
vandebo@chromium.org | c3a2ae5 | 2011-02-03 21:48:23 +0000 | [diff] [blame] | 609 | if (!canEmbed(face)) |
| 610 | info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 611 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 612 | unref_ft_face(face); |
| 613 | return info; |
reed@google.com | 8a5d692 | 2011-03-14 15:08:03 +0000 | [diff] [blame] | 614 | #endif |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 615 | } |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 616 | |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 617 | /////////////////////////////////////////////////////////////////////////// |
| 618 | |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 619 | #define BLACK_LUMINANCE_LIMIT 0x40 |
| 620 | #define WHITE_LUMINANCE_LIMIT 0xA0 |
| 621 | |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 622 | static bool bothZero(SkScalar a, SkScalar b) { |
| 623 | return 0 == a && 0 == b; |
| 624 | } |
| 625 | |
| 626 | // returns false if there is any non-90-rotation or skew |
| 627 | static bool isAxisAligned(const SkScalerContext::Rec& rec) { |
| 628 | return 0 == rec.fPreSkewX && |
| 629 | (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 630 | bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
| 631 | } |
| 632 | |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 633 | void SkFontHost::FilterRec(SkScalerContext::Rec* rec) { |
| 634 | if (!gLCDSupportValid) { |
| 635 | InitFreetype(); |
| 636 | FT_Done_FreeType(gFTLibrary); |
| 637 | } |
reed@google.com | 5b31b0f | 2011-02-23 14:41:42 +0000 | [diff] [blame] | 638 | |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 639 | if (!gLCDSupport && isLCD(*rec)) { |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 640 | // If the runtime Freetype library doesn't support LCD mode, we disable |
| 641 | // it here. |
| 642 | rec->fMaskFormat = SkMask::kA8_Format; |
| 643 | } |
reed@google.com | 5b31b0f | 2011-02-23 14:41:42 +0000 | [diff] [blame] | 644 | |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 645 | SkPaint::Hinting h = rec->getHinting(); |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 646 | if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) { |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 647 | // collapse full->normal hinting if we're not doing LCD |
| 648 | h = SkPaint::kNormal_Hinting; |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 649 | } |
bungeman@google.com | f4f2b80 | 2012-03-08 19:19:51 +0000 | [diff] [blame] | 650 | if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) { |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 651 | if (SkPaint::kNo_Hinting != h) { |
| 652 | h = SkPaint::kSlight_Hinting; |
| 653 | } |
| 654 | } |
| 655 | |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 656 | #ifndef SK_IGNORE_ROTATED_FREETYPE_FIX |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 657 | // rotated text looks bad with hinting, so we disable it as needed |
| 658 | if (!isAxisAligned(*rec)) { |
| 659 | h = SkPaint::kNo_Hinting; |
| 660 | } |
bsalomon@google.com | 0e35ca8 | 2011-07-22 17:56:19 +0000 | [diff] [blame] | 661 | #endif |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 662 | rec->setHinting(h); |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 663 | |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 664 | #ifndef SK_USE_COLOR_LUMINANCE |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 665 | // for compatibility at the moment, discretize luminance to 3 settings |
| 666 | // black, white, gray. This helps with fontcache utilization, since we |
| 667 | // won't create multiple entries that in the end map to the same results. |
| 668 | { |
| 669 | unsigned lum = rec->getLuminanceByte(); |
| 670 | if (gGammaTables[0] || gGammaTables[1]) { |
| 671 | if (lum <= BLACK_LUMINANCE_LIMIT) { |
| 672 | lum = 0; |
| 673 | } else if (lum >= WHITE_LUMINANCE_LIMIT) { |
| 674 | lum = SkScalerContext::kLuminance_Max; |
| 675 | } else { |
| 676 | lum = SkScalerContext::kLuminance_Max >> 1; |
| 677 | } |
| 678 | } else { |
| 679 | lum = 0; // no gamma correct, so use 0 since SkPaint uses that |
| 680 | // when measuring text w/o regard for luminance |
| 681 | } |
| 682 | rec->setLuminanceBits(lum); |
| 683 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 684 | #endif |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 685 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 686 | |
djsollen@google.com | da95772 | 2011-11-16 17:00:46 +0000 | [diff] [blame] | 687 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 688 | uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) { |
| 689 | SkAutoMutexAcquire ac(gFTMutex); |
| 690 | SkFaceRec *rec = ref_ft_face(fontID); |
| 691 | uint16_t unitsPerEm = 0; |
| 692 | |
| 693 | if (rec != NULL && rec->fFace != NULL) { |
| 694 | unitsPerEm = rec->fFace->units_per_EM; |
| 695 | unref_ft_face(rec->fFace); |
| 696 | } |
| 697 | |
| 698 | return (uint32_t)unitsPerEm; |
| 699 | } |
| 700 | #endif |
| 701 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 702 | SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc) |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 703 | : SkScalerContext(desc) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 704 | SkAutoMutexAcquire ac(gFTMutex); |
| 705 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 706 | if (gFTCount == 0) { |
reed@android.com | 659aaf9 | 2009-07-23 15:20:21 +0000 | [diff] [blame] | 707 | if (!InitFreetype()) { |
| 708 | sk_throw(); |
| 709 | } |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 710 | SkFontHost::GetGammaTables(gGammaTables); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 711 | } |
| 712 | ++gFTCount; |
| 713 | |
| 714 | // load the font file |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 715 | fFTSize = NULL; |
| 716 | fFace = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 717 | fFaceRec = ref_ft_face(fRec.fFontID); |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 718 | if (NULL == fFaceRec) { |
| 719 | return; |
| 720 | } |
| 721 | fFace = fFaceRec->fFace; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 722 | |
| 723 | // compute our factors from the record |
| 724 | |
| 725 | SkMatrix m; |
| 726 | |
| 727 | fRec.getSingleMatrix(&m); |
| 728 | |
| 729 | #ifdef DUMP_STRIKE_CREATION |
| 730 | SkString keyString; |
| 731 | SkFontHost::GetDescriptorKeyString(desc, &keyString); |
| 732 | printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize), |
| 733 | SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX), |
| 734 | SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]), |
| 735 | SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]), |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 736 | fRec.getHinting(), fRec.fMaskFormat, keyString.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 737 | #endif |
| 738 | |
| 739 | // now compute our scale factors |
| 740 | SkScalar sx = m.getScaleX(); |
| 741 | SkScalar sy = m.getScaleY(); |
| 742 | |
| 743 | if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) { |
| 744 | // sort of give up on hinting |
| 745 | sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX())); |
| 746 | sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy)); |
| 747 | sx = sy = SkScalarAve(sx, sy); |
| 748 | |
| 749 | SkScalar inv = SkScalarInvert(sx); |
| 750 | |
| 751 | // flip the skew elements to go from our Y-down system to FreeType's |
| 752 | fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv)); |
| 753 | fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv)); |
| 754 | fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv)); |
| 755 | fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv)); |
| 756 | } else { |
| 757 | fMatrix22.xx = fMatrix22.yy = SK_Fixed1; |
| 758 | fMatrix22.xy = fMatrix22.yx = 0; |
| 759 | } |
| 760 | |
| 761 | fScaleX = SkScalarToFixed(sx); |
| 762 | fScaleY = SkScalarToFixed(sy); |
| 763 | |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 764 | fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); |
| 765 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | // compute the flags we send to Load_Glyph |
| 767 | { |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 768 | FT_Int32 loadFlags = FT_LOAD_DEFAULT; |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 769 | bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 770 | |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 771 | if (SkMask::kBW_Format == fRec.fMaskFormat) { |
| 772 | // See http://code.google.com/p/chromium/issues/detail?id=43252#c24 |
| 773 | loadFlags = FT_LOAD_TARGET_MONO; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 774 | if (fRec.getHinting() == SkPaint::kNo_Hinting) { |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 775 | loadFlags = FT_LOAD_NO_HINTING; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 776 | linearMetrics = true; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 777 | } |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 778 | } else { |
| 779 | switch (fRec.getHinting()) { |
| 780 | case SkPaint::kNo_Hinting: |
| 781 | loadFlags = FT_LOAD_NO_HINTING; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 782 | linearMetrics = true; |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 783 | break; |
| 784 | case SkPaint::kSlight_Hinting: |
| 785 | loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT |
| 786 | break; |
| 787 | case SkPaint::kNormal_Hinting: |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 788 | if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) |
| 789 | loadFlags = FT_LOAD_FORCE_AUTOHINT; |
| 790 | else |
| 791 | loadFlags = FT_LOAD_NO_AUTOHINT; |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 792 | break; |
| 793 | case SkPaint::kFull_Hinting: |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 794 | if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) { |
| 795 | loadFlags = FT_LOAD_FORCE_AUTOHINT; |
| 796 | break; |
| 797 | } |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 798 | loadFlags = FT_LOAD_TARGET_NORMAL; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 799 | if (isLCD(fRec)) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 800 | if (fLCDIsVert) { |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 801 | loadFlags = FT_LOAD_TARGET_LCD_V; |
| 802 | } else { |
| 803 | loadFlags = FT_LOAD_TARGET_LCD; |
| 804 | } |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 805 | } |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 806 | break; |
| 807 | default: |
| 808 | SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting()); |
| 809 | break; |
| 810 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 811 | } |
| 812 | |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 813 | if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) { |
agl@chromium.org | e0d0899 | 2009-08-07 19:19:23 +0000 | [diff] [blame] | 814 | loadFlags |= FT_LOAD_NO_BITMAP; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 815 | } |
agl@chromium.org | e0d0899 | 2009-08-07 19:19:23 +0000 | [diff] [blame] | 816 | |
reed@google.com | 96a9f791 | 2011-05-06 11:49:30 +0000 | [diff] [blame] | 817 | // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct |
| 818 | // advances, as fontconfig and cairo do. |
| 819 | // See http://code.google.com/p/skia/issues/detail?id=222. |
| 820 | loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH; |
| 821 | |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 822 | fLoadGlyphFlags = loadFlags; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 823 | fDoLinearMetrics = linearMetrics; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // now create the FT_Size |
| 827 | |
| 828 | { |
| 829 | FT_Error err; |
| 830 | |
| 831 | err = FT_New_Size(fFace, &fFTSize); |
| 832 | if (err != 0) { |
| 833 | SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n", |
| 834 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 835 | fFace = NULL; |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | err = FT_Activate_Size(fFTSize); |
| 840 | if (err != 0) { |
| 841 | SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 842 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 843 | fFTSize = NULL; |
| 844 | } |
| 845 | |
| 846 | err = FT_Set_Char_Size( fFace, |
| 847 | SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY), |
| 848 | 72, 72); |
| 849 | if (err != 0) { |
| 850 | SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 851 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 852 | fFace = NULL; |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | FT_Set_Transform( fFace, &fMatrix22, NULL); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | SkScalerContext_FreeType::~SkScalerContext_FreeType() { |
| 861 | if (fFTSize != NULL) { |
| 862 | FT_Done_Size(fFTSize); |
| 863 | } |
| 864 | |
| 865 | SkAutoMutexAcquire ac(gFTMutex); |
| 866 | |
| 867 | if (fFace != NULL) { |
| 868 | unref_ft_face(fFace); |
| 869 | } |
| 870 | if (--gFTCount == 0) { |
| 871 | // SkDEBUGF(("FT_Done_FreeType\n")); |
| 872 | FT_Done_FreeType(gFTLibrary); |
| 873 | SkDEBUGCODE(gFTLibrary = NULL;) |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /* We call this before each use of the fFace, since we may be sharing |
| 878 | this face with other context (at different sizes). |
| 879 | */ |
| 880 | FT_Error SkScalerContext_FreeType::setupSize() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 881 | FT_Error err = FT_Activate_Size(fFTSize); |
| 882 | |
| 883 | if (err != 0) { |
| 884 | SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 885 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 886 | fFTSize = NULL; |
| 887 | } else { |
| 888 | // seems we need to reset this every time (not sure why, but without it |
| 889 | // I get random italics from some other fFTSize) |
| 890 | FT_Set_Transform( fFace, &fMatrix22, NULL); |
| 891 | } |
| 892 | return err; |
| 893 | } |
| 894 | |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 895 | void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) { |
| 896 | FT_Pos strength; |
| 897 | strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale) |
| 898 | / 24; |
| 899 | FT_Outline_Embolden(outline, strength); |
| 900 | } |
| 901 | |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 902 | unsigned SkScalerContext_FreeType::generateGlyphCount() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 903 | return fFace->num_glyphs; |
| 904 | } |
| 905 | |
| 906 | uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) { |
| 907 | return SkToU16(FT_Get_Char_Index( fFace, uni )); |
| 908 | } |
| 909 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 910 | SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) { |
| 911 | // iterate through each cmap entry, looking for matching glyph indices |
| 912 | FT_UInt glyphIndex; |
| 913 | SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex ); |
| 914 | |
| 915 | while (glyphIndex != 0) { |
| 916 | if (glyphIndex == glyph) { |
| 917 | return charCode; |
| 918 | } |
| 919 | charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex ); |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 925 | static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) { |
| 926 | switch (format) { |
| 927 | case SkMask::kBW_Format: |
| 928 | return FT_PIXEL_MODE_MONO; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 929 | case SkMask::kA8_Format: |
| 930 | default: |
| 931 | return FT_PIXEL_MODE_GRAY; |
| 932 | } |
| 933 | } |
| 934 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 935 | void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) { |
| 936 | #ifdef FT_ADVANCES_H |
| 937 | /* unhinted and light hinted text have linearly scaled advances |
| 938 | * which are very cheap to compute with some font formats... |
| 939 | */ |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 940 | if (fDoLinearMetrics) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 941 | SkAutoMutexAcquire ac(gFTMutex); |
| 942 | |
| 943 | if (this->setupSize()) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 944 | glyph->zeroMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
| 948 | FT_Error error; |
| 949 | FT_Fixed advance; |
| 950 | |
| 951 | error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount), |
| 952 | fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY, |
| 953 | &advance ); |
| 954 | if (0 == error) { |
| 955 | glyph->fRsbDelta = 0; |
| 956 | glyph->fLsbDelta = 0; |
| 957 | glyph->fAdvanceX = advance; // advance *2/3; //DEBUG |
| 958 | glyph->fAdvanceY = 0; |
| 959 | return; |
| 960 | } |
| 961 | } |
| 962 | #endif /* FT_ADVANCES_H */ |
| 963 | /* otherwise, we need to load/hint the glyph, which is slower */ |
| 964 | this->generateMetrics(glyph); |
| 965 | return; |
| 966 | } |
| 967 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 968 | void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph, |
| 969 | FT_BBox* bbox, |
| 970 | bool snapToPixelBoundary) { |
| 971 | |
| 972 | FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); |
| 973 | |
| 974 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
| 975 | int dx = FixedToDot6(glyph->getSubXFixed()); |
| 976 | int dy = FixedToDot6(glyph->getSubYFixed()); |
| 977 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 978 | bbox->xMin += dx; |
| 979 | bbox->yMin -= dy; |
| 980 | bbox->xMax += dx; |
| 981 | bbox->yMax -= dy; |
| 982 | } |
| 983 | |
| 984 | // outset the box to integral boundaries |
| 985 | if (snapToPixelBoundary) { |
| 986 | bbox->xMin &= ~63; |
| 987 | bbox->yMin &= ~63; |
| 988 | bbox->xMax = (bbox->xMax + 63) & ~63; |
| 989 | bbox->yMax = (bbox->yMax + 63) & ~63; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) { |
| 994 | if (isLCD(fRec)) { |
| 995 | if (fLCDIsVert) { |
| 996 | glyph->fHeight += gLCDExtra; |
| 997 | glyph->fTop -= gLCDExtra >> 1; |
| 998 | } else { |
| 999 | glyph->fWidth += gLCDExtra; |
| 1000 | glyph->fLeft -= gLCDExtra >> 1; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1005 | void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) { |
| 1006 | SkAutoMutexAcquire ac(gFTMutex); |
| 1007 | |
| 1008 | glyph->fRsbDelta = 0; |
| 1009 | glyph->fLsbDelta = 0; |
| 1010 | |
| 1011 | FT_Error err; |
| 1012 | |
| 1013 | if (this->setupSize()) { |
| 1014 | goto ERROR; |
| 1015 | } |
| 1016 | |
| 1017 | err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags ); |
| 1018 | if (err != 0) { |
| 1019 | SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 1020 | fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err)); |
| 1021 | ERROR: |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 1022 | glyph->zeroMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 1026 | SkFixed vLeft = 0, vTop = 0; |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1027 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1028 | switch ( fFace->glyph->format ) { |
thakis@chromium.org | 598b859 | 2011-05-24 05:42:55 +0000 | [diff] [blame] | 1029 | case FT_GLYPH_FORMAT_OUTLINE: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1030 | FT_BBox bbox; |
| 1031 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 1032 | if (0 == fFace->glyph->outline.n_contours) { |
| 1033 | glyph->fWidth = 0; |
| 1034 | glyph->fHeight = 0; |
| 1035 | glyph->fTop = 0; |
| 1036 | glyph->fLeft = 0; |
| 1037 | break; |
| 1038 | } |
| 1039 | |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1040 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1041 | emboldenOutline(&fFace->glyph->outline); |
| 1042 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1043 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1044 | getBBoxForCurrentGlyph(glyph, &bbox, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1045 | |
| 1046 | glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6); |
| 1047 | glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6); |
| 1048 | glyph->fTop = -SkToS16(bbox.yMax >> 6); |
| 1049 | glyph->fLeft = SkToS16(bbox.xMin >> 6); |
reed@google.com | a1c3256 | 2012-03-01 19:38:23 +0000 | [diff] [blame] | 1050 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1051 | if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) { |
| 1052 | vLeft = Dot6ToFixed(bbox.xMin); |
| 1053 | vTop = Dot6ToFixed(bbox.yMax); |
reed@google.com | a1c3256 | 2012-03-01 19:38:23 +0000 | [diff] [blame] | 1054 | } |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1055 | |
| 1056 | updateGlyphIfLCD(glyph); |
| 1057 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1058 | break; |
thakis@chromium.org | 598b859 | 2011-05-24 05:42:55 +0000 | [diff] [blame] | 1059 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1060 | |
| 1061 | case FT_GLYPH_FORMAT_BITMAP: |
agl@chromium.org | e76073b | 2010-06-04 20:31:17 +0000 | [diff] [blame] | 1062 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1063 | FT_GlyphSlot_Own_Bitmap(fFace->glyph); |
| 1064 | FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0); |
| 1065 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1066 | glyph->fWidth = SkToU16(fFace->glyph->bitmap.width); |
| 1067 | glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows); |
| 1068 | glyph->fTop = -SkToS16(fFace->glyph->bitmap_top); |
| 1069 | glyph->fLeft = SkToS16(fFace->glyph->bitmap_left); |
| 1070 | break; |
| 1071 | |
| 1072 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1073 | SkDEBUGFAIL("unknown glyph format"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1074 | goto ERROR; |
| 1075 | } |
| 1076 | |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 1077 | if (fDoLinearMetrics) { |
| 1078 | glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance); |
| 1079 | glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance); |
| 1080 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1081 | glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x); |
| 1082 | glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 1083 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1084 | if (fRec.fFlags & kDevKernText_Flag) { |
| 1085 | glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta); |
| 1086 | glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta); |
| 1087 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1090 | if ((fRec.fFlags & SkScalerContext::kVertical_Flag) |
| 1091 | && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { |
| 1092 | |
| 1093 | //TODO: do we need to specially handle SubpixelPositioning and Kerning? |
| 1094 | |
| 1095 | FT_Matrix identityMatrix; |
| 1096 | identityMatrix.xx = identityMatrix.yy = SK_Fixed1; |
| 1097 | identityMatrix.xy = identityMatrix.yx = 0; |
| 1098 | |
| 1099 | // if the matrix is not the identity matrix then we need to re-load the |
| 1100 | // glyph with the identity matrix to get the necessary bounding box |
| 1101 | if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) { |
| 1102 | |
| 1103 | FT_Set_Transform(fFace, &identityMatrix, NULL); |
| 1104 | |
| 1105 | err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags ); |
| 1106 | if (err != 0) { |
| 1107 | SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 1108 | fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err)); |
| 1109 | goto ERROR; |
| 1110 | } |
| 1111 | |
| 1112 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1113 | emboldenOutline(&fFace->glyph->outline); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | // bounding box of the unskewed and unscaled glyph |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 1118 | FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning. |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1119 | getBBoxForCurrentGlyph(glyph, &bbox); |
| 1120 | |
| 1121 | // compute the vertical gap above and below the glyph if the glyph were |
| 1122 | // centered within the linearVertAdvance |
| 1123 | SkFixed vGap = (fFace->glyph->linearVertAdvance - Dot6ToFixed(bbox.yMax - bbox.yMin)) / 2; |
| 1124 | |
| 1125 | // the origin point of the glyph when rendered vertically |
| 1126 | FT_Vector vOrigin; |
| 1127 | vOrigin.x = fFace->glyph->linearHoriAdvance / 2; |
| 1128 | vOrigin.y = vGap + Dot6ToFixed(bbox.yMax); |
| 1129 | |
| 1130 | // transform the vertical origin based on the matrix of the actual glyph |
| 1131 | FT_Vector_Transform(&vOrigin, &fMatrix22); |
| 1132 | |
| 1133 | // compute a new offset vector for the glyph by subtracting the vertical |
| 1134 | // origin from the original horizontal offset vector |
| 1135 | glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x); |
| 1136 | glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y); |
| 1137 | |
| 1138 | updateGlyphIfLCD(glyph); |
| 1139 | |
| 1140 | // use the vertical advance values computed by freetype |
| 1141 | glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance); |
| 1142 | glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance); |
| 1143 | } |
| 1144 | |
| 1145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1146 | #ifdef ENABLE_GLYPH_SPEW |
| 1147 | SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY)); |
| 1148 | SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth)); |
| 1149 | #endif |
| 1150 | } |
| 1151 | |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1152 | /////////////////////////////////////////////////////////////////////////////// |
| 1153 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1154 | #ifdef SK_USE_COLOR_LUMINANCE |
| 1155 | |
| 1156 | static float apply_contrast(float srca, float contrast) { |
| 1157 | return srca + ((1.0f - srca) * contrast * srca); |
reed@google.com | c518134 | 2011-05-17 20:52:46 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1160 | #ifdef SK_GAMMA_SRGB |
| 1161 | static float lin(float per) { |
| 1162 | if (per <= 0.04045f) { |
| 1163 | return per / 12.92f; |
reed@google.com | c518134 | 2011-05-17 20:52:46 +0000 | [diff] [blame] | 1164 | } |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1165 | return powf((per + 0.055f) / 1.055, 2.4f); |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1166 | } |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1167 | static float per(float lin) { |
| 1168 | if (lin <= 0.0031308f) { |
| 1169 | return lin * 12.92f; |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1170 | } |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1171 | return 1.055f * powf(lin, 1.0f / 2.4f) - 0.055f; |
| 1172 | } |
| 1173 | #else //SK_GAMMA_SRGB |
| 1174 | static float lin(float per) { |
| 1175 | const float g = SK_GAMMA_EXPONENT; |
| 1176 | return powf(per, g); |
| 1177 | } |
| 1178 | static float per(float lin) { |
| 1179 | const float g = SK_GAMMA_EXPONENT; |
| 1180 | return powf(lin, 1.0f / g); |
| 1181 | } |
| 1182 | #endif //SK_GAMMA_SRGB |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1183 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1184 | static void build_gamma_table(uint8_t table[256], int srcI) { |
| 1185 | const float src = (float)srcI / 255.0f; |
| 1186 | const float linSrc = lin(src); |
| 1187 | const float linDst = 1.0f - linSrc; |
| 1188 | const float dst = per(linDst); |
bungeman@google.com | d0b6a2d | 2012-03-05 15:07:25 +0000 | [diff] [blame] | 1189 | |
reed@google.com | 613e9fe | 2012-02-29 15:08:00 +0000 | [diff] [blame] | 1190 | // have our contrast value taper off to 0 as the src luminance becomes white |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1191 | const float contrast = SK_GAMMA_CONTRAST / 255.0f * linDst; |
| 1192 | const float step = 1.0f / 256.0f; |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1193 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1194 | //Remove discontinuity and instability when src is close to dst. |
| 1195 | if (fabs(src - dst) < 0.01f) { |
| 1196 | float rawSrca = 0.0f; |
| 1197 | for (int i = 0; i < 256; ++i, rawSrca += step) { |
| 1198 | float srca = apply_contrast(rawSrca, contrast); |
| 1199 | table[i] = sk_float_round2int(255.0f * srca); |
| 1200 | } |
| 1201 | } else { |
| 1202 | float rawSrca = 0.0f; |
| 1203 | for (int i = 0; i < 256; ++i, rawSrca += step) { |
| 1204 | float srca = apply_contrast(rawSrca, contrast); |
| 1205 | SkASSERT(srca <= 1.0f); |
| 1206 | float dsta = 1 - srca; |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1207 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1208 | //Calculate the output we want. |
| 1209 | float linOut = (linSrc * srca + dsta * linDst); |
| 1210 | SkASSERT(linOut <= 1.0f); |
| 1211 | float out = per(linOut); |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1212 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1213 | //Undo what the blit blend will do. |
| 1214 | float result = (out - dst) / (src - dst); |
| 1215 | SkASSERT(sk_float_round2int(255.0f * result) <= 255); |
| 1216 | |
| 1217 | table[i] = sk_float_round2int(255.0f * result); |
| 1218 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | static const uint8_t* getGammaTable(U8CPU luminance) { |
| 1223 | static uint8_t gGammaTables[4][256]; |
| 1224 | static bool gInited; |
| 1225 | if (!gInited) { |
bungeman@google.com | d0b6a2d | 2012-03-05 15:07:25 +0000 | [diff] [blame] | 1226 | build_gamma_table(gGammaTables[0], 0x00); |
| 1227 | build_gamma_table(gGammaTables[1], 0x55); |
| 1228 | build_gamma_table(gGammaTables[2], 0xAA); |
| 1229 | build_gamma_table(gGammaTables[3], 0xFF); |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1230 | |
| 1231 | gInited = true; |
| 1232 | } |
| 1233 | SkASSERT(0 == (luminance >> 8)); |
| 1234 | return gGammaTables[luminance >> 6]; |
| 1235 | } |
| 1236 | |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1237 | #else //SK_USE_COLOR_LUMINANCE |
reed@google.com | d61b92b | 2012-03-02 16:02:07 +0000 | [diff] [blame] | 1238 | static const uint8_t* getIdentityTable() { |
| 1239 | static bool gOnce; |
| 1240 | static uint8_t gIdentityTable[256]; |
| 1241 | if (!gOnce) { |
| 1242 | for (int i = 0; i < 256; ++i) { |
| 1243 | gIdentityTable[i] = i; |
| 1244 | } |
| 1245 | gOnce = true; |
| 1246 | } |
| 1247 | return gIdentityTable; |
| 1248 | } |
bungeman@google.com | 3aacb41 | 2012-03-13 14:55:12 +0000 | [diff] [blame] | 1249 | #endif //SK_USE_COLOR_LUMINANCE |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1250 | |
| 1251 | static uint16_t packTriple(unsigned r, unsigned g, unsigned b) { |
reed@google.com | c518134 | 2011-05-17 20:52:46 +0000 | [diff] [blame] | 1252 | return SkPackRGB16(r >> 3, g >> 2, b >> 3); |
| 1253 | } |
| 1254 | |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1255 | static uint16_t grayToRGB16(U8CPU gray) { |
| 1256 | SkASSERT(gray <= 255); |
| 1257 | return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3); |
| 1258 | } |
| 1259 | |
| 1260 | static int bittst(const uint8_t data[], int bitOffset) { |
| 1261 | SkASSERT(bitOffset >= 0); |
reed@google.com | c8e0f93 | 2011-06-23 19:39:49 +0000 | [diff] [blame] | 1262 | int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7); |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1263 | return lowBit & 1; |
| 1264 | } |
| 1265 | |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 1266 | static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap, |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1267 | int lcdIsBGR, bool lcdIsVert, const uint8_t* tableR, |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1268 | const uint8_t* tableG, const uint8_t* tableB) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1269 | if (lcdIsVert) { |
| 1270 | SkASSERT(3 * glyph.fHeight == bitmap.rows); |
| 1271 | } else { |
| 1272 | SkASSERT(glyph.fHeight == bitmap.rows); |
| 1273 | } |
| 1274 | |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1275 | uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage); |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1276 | const size_t dstRB = glyph.rowBytes(); |
| 1277 | const int width = glyph.fWidth; |
| 1278 | const uint8_t* src = bitmap.buffer; |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1279 | |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1280 | switch (bitmap.pixel_mode) { |
| 1281 | case FT_PIXEL_MODE_MONO: { |
| 1282 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1283 | for (int x = 0; x < width; ++x) { |
| 1284 | dst[x] = -bittst(src, x); |
| 1285 | } |
| 1286 | dst = (uint16_t*)((char*)dst + dstRB); |
| 1287 | src += bitmap.pitch; |
| 1288 | } |
| 1289 | } break; |
| 1290 | case FT_PIXEL_MODE_GRAY: { |
| 1291 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 1292 | for (int x = 0; x < width; ++x) { |
| 1293 | dst[x] = grayToRGB16(src[x]); |
| 1294 | } |
| 1295 | dst = (uint16_t*)((char*)dst + dstRB); |
| 1296 | src += bitmap.pitch; |
| 1297 | } |
| 1298 | } break; |
| 1299 | default: { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1300 | SkASSERT(lcdIsVert || (glyph.fWidth * 3 == bitmap.width)); |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1301 | for (int y = 0; y < glyph.fHeight; y++) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1302 | if (lcdIsVert) { // vertical stripes |
| 1303 | const uint8_t* srcR = src; |
| 1304 | const uint8_t* srcG = srcR + bitmap.pitch; |
| 1305 | const uint8_t* srcB = srcG + bitmap.pitch; |
| 1306 | if (lcdIsBGR) { |
| 1307 | SkTSwap(srcR, srcB); |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 1308 | } |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 1309 | for (int x = 0; x < width; x++) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1310 | dst[x] = packTriple(tableR[*srcR++], |
| 1311 | tableG[*srcG++], |
| 1312 | tableB[*srcB++]); |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 1313 | } |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1314 | src += 3 * bitmap.pitch; |
| 1315 | } else { // horizontal stripes |
| 1316 | const uint8_t* triple = src; |
| 1317 | if (lcdIsBGR) { |
| 1318 | for (int x = 0; x < width; x++) { |
| 1319 | dst[x] = packTriple(tableR[triple[2]], |
| 1320 | tableG[triple[1]], |
| 1321 | tableB[triple[0]]); |
| 1322 | triple += 3; |
| 1323 | } |
| 1324 | } else { |
| 1325 | for (int x = 0; x < width; x++) { |
| 1326 | dst[x] = packTriple(tableR[triple[0]], |
| 1327 | tableG[triple[1]], |
| 1328 | tableB[triple[2]]); |
| 1329 | triple += 3; |
| 1330 | } |
| 1331 | } |
| 1332 | src += bitmap.pitch; |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1333 | } |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1334 | dst = (uint16_t*)((char*)dst + dstRB); |
| 1335 | } |
| 1336 | } break; |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1340 | void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) { |
| 1341 | SkAutoMutexAcquire ac(gFTMutex); |
| 1342 | |
| 1343 | FT_Error err; |
| 1344 | |
| 1345 | if (this->setupSize()) { |
| 1346 | goto ERROR; |
| 1347 | } |
| 1348 | |
| 1349 | err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags); |
| 1350 | if (err != 0) { |
| 1351 | SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n", |
| 1352 | glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err)); |
| 1353 | ERROR: |
| 1354 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 1355 | return; |
| 1356 | } |
| 1357 | |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1358 | #ifdef SK_USE_COLOR_LUMINANCE |
| 1359 | SkColor lumColor = fRec.getLuminanceColor(); |
| 1360 | const uint8_t* tableR = getGammaTable(SkColorGetR(lumColor)); |
| 1361 | const uint8_t* tableG = getGammaTable(SkColorGetG(lumColor)); |
| 1362 | const uint8_t* tableB = getGammaTable(SkColorGetB(lumColor)); |
| 1363 | #else |
| 1364 | unsigned lum = fRec.getLuminanceByte(); |
reed@google.com | d61b92b | 2012-03-02 16:02:07 +0000 | [diff] [blame] | 1365 | const uint8_t* tableR; |
| 1366 | const uint8_t* tableG; |
| 1367 | const uint8_t* tableB; |
| 1368 | |
| 1369 | bool isWhite = lum >= WHITE_LUMINANCE_LIMIT; |
| 1370 | bool isBlack = lum <= BLACK_LUMINANCE_LIMIT; |
| 1371 | if ((gGammaTables[0] || gGammaTables[1]) && (isBlack || isWhite)) { |
| 1372 | tableR = tableG = tableB = gGammaTables[isBlack ? 0 : 1]; |
| 1373 | } else { |
| 1374 | tableR = tableG = tableB = getIdentityTable(); |
| 1375 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1376 | #endif |
| 1377 | |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1378 | const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag); |
| 1379 | const bool doVert = fLCDIsVert; |
| 1380 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1381 | switch ( fFace->glyph->format ) { |
| 1382 | case FT_GLYPH_FORMAT_OUTLINE: { |
| 1383 | FT_Outline* outline = &fFace->glyph->outline; |
| 1384 | FT_BBox bbox; |
| 1385 | FT_Bitmap target; |
| 1386 | |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1387 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1388 | emboldenOutline(outline); |
| 1389 | } |
| 1390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1391 | int dx = 0, dy = 0; |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 1392 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1393 | dx = FixedToDot6(glyph.getSubXFixed()); |
| 1394 | dy = FixedToDot6(glyph.getSubYFixed()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1395 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 1396 | dy = -dy; |
| 1397 | } |
| 1398 | FT_Outline_Get_CBox(outline, &bbox); |
| 1399 | /* |
| 1400 | what we really want to do for subpixel is |
| 1401 | offset(dx, dy) |
| 1402 | compute_bounds |
| 1403 | offset(bbox & !63) |
| 1404 | but that is two calls to offset, so we do the following, which |
| 1405 | achieves the same thing with only one offset call. |
| 1406 | */ |
| 1407 | FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63), |
| 1408 | dy - ((bbox.yMin + dy) & ~63)); |
| 1409 | |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1410 | if (SkMask::kLCD16_Format == glyph.fMaskFormat) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1411 | FT_Render_Glyph(fFace->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD); |
| 1412 | copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert, |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1413 | tableR, tableG, tableB); |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1414 | } else { |
| 1415 | target.width = glyph.fWidth; |
| 1416 | target.rows = glyph.fHeight; |
| 1417 | target.pitch = glyph.rowBytes(); |
| 1418 | target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage); |
| 1419 | target.pixel_mode = compute_pixel_mode( |
| 1420 | (SkMask::Format)fRec.fMaskFormat); |
| 1421 | target.num_grays = 256; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1422 | |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1423 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 1424 | FT_Outline_Get_Bitmap(gFTLibrary, outline, &target); |
| 1425 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1426 | } break; |
| 1427 | |
| 1428 | case FT_GLYPH_FORMAT_BITMAP: { |
agl@chromium.org | e76073b | 2010-06-04 20:31:17 +0000 | [diff] [blame] | 1429 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1430 | FT_GlyphSlot_Own_Bitmap(fFace->glyph); |
| 1431 | FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0); |
| 1432 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1433 | SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width); |
| 1434 | SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows); |
| 1435 | SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top); |
| 1436 | SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left); |
| 1437 | |
| 1438 | const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer; |
| 1439 | uint8_t* dst = (uint8_t*)glyph.fImage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1440 | |
agl@chromium.org | 558434a | 2009-08-11 17:22:38 +0000 | [diff] [blame] | 1441 | if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY || |
| 1442 | (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO && |
| 1443 | glyph.fMaskFormat == SkMask::kBW_Format)) { |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1444 | unsigned srcRowBytes = fFace->glyph->bitmap.pitch; |
| 1445 | unsigned dstRowBytes = glyph.rowBytes(); |
| 1446 | unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes); |
| 1447 | unsigned extraRowBytes = dstRowBytes - minRowBytes; |
| 1448 | |
| 1449 | for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) { |
| 1450 | memcpy(dst, src, minRowBytes); |
| 1451 | memset(dst + minRowBytes, 0, extraRowBytes); |
| 1452 | src += srcRowBytes; |
| 1453 | dst += dstRowBytes; |
| 1454 | } |
agl@chromium.org | 558434a | 2009-08-11 17:22:38 +0000 | [diff] [blame] | 1455 | } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO && |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 1456 | glyph.fMaskFormat == SkMask::kA8_Format) { |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1457 | for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) { |
| 1458 | uint8_t byte = 0; |
| 1459 | int bits = 0; |
| 1460 | const uint8_t* src_row = src; |
| 1461 | uint8_t* dst_row = dst; |
| 1462 | |
| 1463 | for (int x = 0; x < fFace->glyph->bitmap.width; ++x) { |
| 1464 | if (!bits) { |
| 1465 | byte = *src_row++; |
| 1466 | bits = 8; |
| 1467 | } |
| 1468 | |
| 1469 | *dst_row++ = byte & 0x80 ? 0xff : 0; |
| 1470 | bits--; |
| 1471 | byte <<= 1; |
| 1472 | } |
| 1473 | |
| 1474 | src += fFace->glyph->bitmap.pitch; |
| 1475 | dst += glyph.rowBytes(); |
| 1476 | } |
reed@google.com | 7382407 | 2011-06-23 13:17:30 +0000 | [diff] [blame] | 1477 | } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 1478 | copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert, |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1479 | tableR, tableG, tableB); |
agl@chromium.org | 558434a | 2009-08-11 17:22:38 +0000 | [diff] [blame] | 1480 | } else { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1481 | SkDEBUGFAIL("unknown glyph bitmap transform needed"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } break; |
| 1484 | |
| 1485 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1486 | SkDEBUGFAIL("unknown glyph format"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1487 | goto ERROR; |
| 1488 | } |
reed@google.com | 1f6b4ae | 2011-11-22 14:20:55 +0000 | [diff] [blame] | 1489 | |
reed@google.com | d61b92b | 2012-03-02 16:02:07 +0000 | [diff] [blame] | 1490 | // We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum, |
| 1491 | // it is optional |
| 1492 | #if defined(SK_GAMMA_APPLY_TO_A8) || !defined(SK_USE_COLOR_LUMINANCE) |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1493 | if (SkMask::kA8_Format == glyph.fMaskFormat) { |
| 1494 | SkASSERT(tableR == tableG && tableR == tableB); |
| 1495 | const uint8_t* table = tableR; |
| 1496 | uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage; |
| 1497 | unsigned rowBytes = glyph.rowBytes(); |
| 1498 | |
| 1499 | for (int y = glyph.fHeight - 1; y >= 0; --y) { |
| 1500 | for (int x = glyph.fWidth - 1; x >= 0; --x) { |
| 1501 | dst[x] = table[dst[x]]; |
reed@google.com | 1f6b4ae | 2011-11-22 14:20:55 +0000 | [diff] [blame] | 1502 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1503 | dst += rowBytes; |
reed@google.com | 1f6b4ae | 2011-11-22 14:20:55 +0000 | [diff] [blame] | 1504 | } |
| 1505 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 1506 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | /////////////////////////////////////////////////////////////////////////////// |
| 1510 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1511 | #define ft2sk(x) SkFixedToScalar(Dot6ToFixed(x)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1512 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 1513 | #if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1514 | #define CONST_PARAM const |
| 1515 | #else // older freetype doesn't use const here |
| 1516 | #define CONST_PARAM |
| 1517 | #endif |
| 1518 | |
| 1519 | static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 1520 | SkPath* path = (SkPath*)ctx; |
| 1521 | path->close(); // to close the previous contour (if any) |
| 1522 | path->moveTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 1527 | SkPath* path = (SkPath*)ctx; |
| 1528 | path->lineTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 1533 | void* ctx) { |
| 1534 | SkPath* path = (SkPath*)ctx; |
| 1535 | path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y)); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 1540 | CONST_PARAM FT_Vector* pt2, void* ctx) { |
| 1541 | SkPath* path = (SkPath*)ctx; |
| 1542 | path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), |
| 1543 | -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y)); |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, |
| 1548 | SkPath* path) { |
| 1549 | SkAutoMutexAcquire ac(gFTMutex); |
| 1550 | |
| 1551 | SkASSERT(&glyph && path); |
| 1552 | |
| 1553 | if (this->setupSize()) { |
| 1554 | path->reset(); |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | uint32_t flags = fLoadGlyphFlags; |
| 1559 | flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline |
| 1560 | flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline) |
| 1561 | |
| 1562 | FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags); |
| 1563 | |
| 1564 | if (err != 0) { |
| 1565 | SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 1566 | glyph.getGlyphID(fBaseGlyphCount), flags, err)); |
| 1567 | path->reset(); |
| 1568 | return; |
| 1569 | } |
| 1570 | |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1571 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1572 | emboldenOutline(&fFace->glyph->outline); |
| 1573 | } |
| 1574 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1575 | FT_Outline_Funcs funcs; |
| 1576 | |
| 1577 | funcs.move_to = move_proc; |
| 1578 | funcs.line_to = line_proc; |
| 1579 | funcs.conic_to = quad_proc; |
| 1580 | funcs.cubic_to = cubic_proc; |
| 1581 | funcs.shift = 0; |
| 1582 | funcs.delta = 0; |
| 1583 | |
| 1584 | err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path); |
| 1585 | |
| 1586 | if (err != 0) { |
| 1587 | SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 1588 | glyph.getGlyphID(fBaseGlyphCount), flags, err)); |
| 1589 | path->reset(); |
| 1590 | return; |
| 1591 | } |
| 1592 | |
| 1593 | path->close(); |
| 1594 | } |
| 1595 | |
| 1596 | void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx, |
| 1597 | SkPaint::FontMetrics* my) { |
| 1598 | if (NULL == mx && NULL == my) { |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | SkAutoMutexAcquire ac(gFTMutex); |
| 1603 | |
| 1604 | if (this->setupSize()) { |
reed@android.com | a8a8b8b | 2009-05-04 15:00:11 +0000 | [diff] [blame] | 1605 | ERROR: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1606 | if (mx) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 1607 | sk_bzero(mx, sizeof(SkPaint::FontMetrics)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1608 | } |
| 1609 | if (my) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 1610 | sk_bzero(my, sizeof(SkPaint::FontMetrics)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1611 | } |
| 1612 | return; |
| 1613 | } |
| 1614 | |
reed@android.com | a8a8b8b | 2009-05-04 15:00:11 +0000 | [diff] [blame] | 1615 | FT_Face face = fFace; |
| 1616 | int upem = face->units_per_EM; |
| 1617 | if (upem <= 0) { |
| 1618 | goto ERROR; |
| 1619 | } |
| 1620 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1621 | SkPoint pts[6]; |
| 1622 | SkFixed ys[6]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1623 | SkFixed scaleY = fScaleY; |
| 1624 | SkFixed mxy = fMatrix22.xy; |
| 1625 | SkFixed myy = fMatrix22.yy; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1626 | SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem; |
| 1627 | SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1628 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1629 | int leading = face->height - (face->ascender + -face->descender); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1630 | if (leading < 0) { |
| 1631 | leading = 0; |
| 1632 | } |
| 1633 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1634 | // Try to get the OS/2 table from the font. This contains the specific |
| 1635 | // average font width metrics which Windows uses. |
| 1636 | TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2); |
| 1637 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1638 | ys[0] = -face->bbox.yMax; |
| 1639 | ys[1] = -face->ascender; |
| 1640 | ys[2] = -face->descender; |
| 1641 | ys[3] = -face->bbox.yMin; |
| 1642 | ys[4] = leading; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1643 | ys[5] = os2 ? os2->xAvgCharWidth : 0; |
| 1644 | |
| 1645 | SkScalar x_height; |
| 1646 | if (os2 && os2->sxHeight) { |
| 1647 | x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem)); |
| 1648 | } else { |
| 1649 | const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x'); |
| 1650 | if (x_glyph) { |
| 1651 | FT_BBox bbox; |
| 1652 | FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags); |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1653 | if (fRec.fFlags & kEmbolden_Flag) { |
| 1654 | emboldenOutline(&fFace->glyph->outline); |
| 1655 | } |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1656 | FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox); |
epoger@google.com | b90113d | 2012-01-18 19:20:39 +0000 | [diff] [blame] | 1657 | x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax)); |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1658 | } else { |
| 1659 | x_height = 0; |
| 1660 | } |
| 1661 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1662 | |
| 1663 | // convert upem-y values into scalar points |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1664 | for (int i = 0; i < 6; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1665 | SkFixed y = SkMulDiv(scaleY, ys[i], upem); |
| 1666 | SkFixed x = SkFixedMul(mxy, y); |
| 1667 | y = SkFixedMul(myy, y); |
| 1668 | pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y)); |
| 1669 | } |
| 1670 | |
| 1671 | if (mx) { |
| 1672 | mx->fTop = pts[0].fX; |
| 1673 | mx->fAscent = pts[1].fX; |
| 1674 | mx->fDescent = pts[2].fX; |
| 1675 | mx->fBottom = pts[3].fX; |
| 1676 | mx->fLeading = pts[4].fX; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1677 | mx->fAvgCharWidth = pts[5].fX; |
| 1678 | mx->fXMin = xmin; |
| 1679 | mx->fXMax = xmax; |
| 1680 | mx->fXHeight = x_height; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1681 | } |
| 1682 | if (my) { |
| 1683 | my->fTop = pts[0].fY; |
| 1684 | my->fAscent = pts[1].fY; |
| 1685 | my->fDescent = pts[2].fY; |
| 1686 | my->fBottom = pts[3].fY; |
| 1687 | my->fLeading = pts[4].fY; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 1688 | my->fAvgCharWidth = pts[5].fY; |
| 1689 | my->fXMin = xmin; |
| 1690 | my->fXMax = xmax; |
| 1691 | my->fXHeight = x_height; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | //////////////////////////////////////////////////////////////////////// |
| 1696 | //////////////////////////////////////////////////////////////////////// |
| 1697 | |
| 1698 | SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 1699 | SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc)); |
| 1700 | if (!c->success()) { |
| 1701 | SkDELETE(c); |
| 1702 | c = NULL; |
| 1703 | } |
| 1704 | return c; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /////////////////////////////////////////////////////////////////////////////// |
| 1708 | |
| 1709 | /* Export this so that other parts of our FonttHost port can make use of our |
| 1710 | ability to extract the name+style from a stream, using FreeType's api. |
| 1711 | */ |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1712 | bool find_name_and_attributes(SkStream* stream, SkString* name, |
| 1713 | SkTypeface::Style* style, bool* isFixedWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1714 | FT_Library library; |
reed@android.com | bfbd4ff | 2009-07-23 17:44:41 +0000 | [diff] [blame] | 1715 | if (FT_Init_FreeType(&library)) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1716 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | FT_Open_Args args; |
| 1720 | memset(&args, 0, sizeof(args)); |
| 1721 | |
| 1722 | const void* memoryBase = stream->getMemoryBase(); |
| 1723 | FT_StreamRec streamRec; |
| 1724 | |
| 1725 | if (NULL != memoryBase) { |
| 1726 | args.flags = FT_OPEN_MEMORY; |
| 1727 | args.memory_base = (const FT_Byte*)memoryBase; |
| 1728 | args.memory_size = stream->getLength(); |
| 1729 | } else { |
| 1730 | memset(&streamRec, 0, sizeof(streamRec)); |
| 1731 | streamRec.size = stream->read(NULL, 0); |
| 1732 | streamRec.descriptor.pointer = stream; |
| 1733 | streamRec.read = sk_stream_read; |
| 1734 | streamRec.close = sk_stream_close; |
| 1735 | |
| 1736 | args.flags = FT_OPEN_STREAM; |
| 1737 | args.stream = &streamRec; |
| 1738 | } |
| 1739 | |
| 1740 | FT_Face face; |
| 1741 | if (FT_Open_Face(library, &args, 0, &face)) { |
| 1742 | FT_Done_FreeType(library); |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1743 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1746 | int tempStyle = SkTypeface::kNormal; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1747 | if (face->style_flags & FT_STYLE_FLAG_BOLD) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1748 | tempStyle |= SkTypeface::kBold; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1749 | } |
| 1750 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1751 | tempStyle |= SkTypeface::kItalic; |
| 1752 | } |
| 1753 | |
| 1754 | if (name) { |
| 1755 | name->set(face->family_name); |
| 1756 | } |
| 1757 | if (style) { |
| 1758 | *style = (SkTypeface::Style) tempStyle; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1759 | } |
reed@google.com | 5b31b0f | 2011-02-23 14:41:42 +0000 | [diff] [blame] | 1760 | if (isFixedWidth) { |
| 1761 | *isFixedWidth = FT_IS_FIXED_WIDTH(face); |
| 1762 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1763 | |
| 1764 | FT_Done_Face(face); |
| 1765 | FT_Done_FreeType(library); |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1766 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1767 | } |