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