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