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 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 18 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkScalerContext.h" |
| 20 | #include "SkBitmap.h" |
| 21 | #include "SkCanvas.h" |
| 22 | #include "SkDescriptor.h" |
| 23 | #include "SkFDot6.h" |
| 24 | #include "SkFontHost.h" |
| 25 | #include "SkMask.h" |
| 26 | #include "SkStream.h" |
| 27 | #include "SkString.h" |
| 28 | #include "SkThread.h" |
| 29 | #include "SkTemplates.h" |
| 30 | |
| 31 | #include <ft2build.h> |
| 32 | #include FT_FREETYPE_H |
| 33 | #include FT_OUTLINE_H |
| 34 | #include FT_SIZES_H |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 35 | #include FT_TRUETYPE_TABLES_H |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 36 | |
reed@android.com | f549369 | 2009-07-22 19:21:01 +0000 | [diff] [blame] | 37 | #if defined(SK_SUPPORT_LCDTEXT) |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 38 | #include FT_LCD_FILTER_H |
| 39 | #endif |
| 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | #ifdef FT_ADVANCES_H |
| 42 | #include FT_ADVANCES_H |
| 43 | #endif |
| 44 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 45 | #if 0 |
| 46 | // Also include the files by name for build tools which require this. |
| 47 | #include <freetype/freetype.h> |
| 48 | #include <freetype/ftoutln.h> |
| 49 | #include <freetype/ftsizes.h> |
| 50 | #include <freetype/tttables.h> |
| 51 | #include <freetype/ftadvanc.h> |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 52 | #include <freetype/ftlcdfil.h> |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | //#define ENABLE_GLYPH_SPEW // for tracing calls |
| 56 | //#define DUMP_STRIKE_CREATION |
| 57 | |
| 58 | #ifdef SK_DEBUG |
| 59 | #define SkASSERT_CONTINUE(pred) \ |
| 60 | do { \ |
| 61 | if (!(pred)) \ |
| 62 | SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \ |
| 63 | } while (false) |
| 64 | #else |
| 65 | #define SkASSERT_CONTINUE(pred) |
| 66 | #endif |
| 67 | |
| 68 | ////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | struct SkFaceRec; |
| 71 | |
| 72 | static SkMutex gFTMutex; |
| 73 | static int gFTCount; |
| 74 | static FT_Library gFTLibrary; |
| 75 | static SkFaceRec* gFaceRecHead; |
agl@chromium.org | f18d876 | 2009-07-28 18:38:08 +0000 | [diff] [blame^] | 76 | static bool gLCDSupportValid; // true iff |gLCDSupport| has been set. |
| 77 | static bool gLCDSupport; // true iff LCD is supported by the runtime. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | ///////////////////////////////////////////////////////////////////////// |
| 80 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 81 | static bool |
| 82 | InitFreetype() { |
| 83 | FT_Error err = FT_Init_FreeType(&gFTLibrary); |
| 84 | if (err) |
| 85 | return false; |
| 86 | |
reed@android.com | f549369 | 2009-07-22 19:21:01 +0000 | [diff] [blame] | 87 | #if defined(SK_SUPPORT_LCDTEXT) |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 88 | // Setup LCD filtering. This reduces colour fringes for LCD rendered |
| 89 | // glyphs. |
| 90 | err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT); |
agl@chromium.org | f18d876 | 2009-07-28 18:38:08 +0000 | [diff] [blame^] | 91 | gLCDSupport = err == 0; |
| 92 | gLCDSupportValid = true; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | class SkScalerContext_FreeType : public SkScalerContext { |
| 99 | public: |
| 100 | SkScalerContext_FreeType(const SkDescriptor* desc); |
| 101 | virtual ~SkScalerContext_FreeType(); |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 102 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 103 | bool success() const { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 104 | return fFaceRec != NULL && |
| 105 | fFTSize != NULL && |
| 106 | fFace != NULL; |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 107 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | virtual unsigned generateGlyphCount() const; |
| 111 | virtual uint16_t generateCharToGlyph(SkUnichar uni); |
| 112 | virtual void generateAdvance(SkGlyph* glyph); |
| 113 | virtual void generateMetrics(SkGlyph* glyph); |
| 114 | virtual void generateImage(const SkGlyph& glyph); |
| 115 | virtual void generatePath(const SkGlyph& glyph, SkPath* path); |
| 116 | virtual void generateFontMetrics(SkPaint::FontMetrics* mx, |
| 117 | SkPaint::FontMetrics* my); |
| 118 | |
| 119 | private: |
| 120 | SkFaceRec* fFaceRec; |
| 121 | FT_Face fFace; // reference to shared face in gFaceRecHead |
| 122 | FT_Size fFTSize; // our own copy |
| 123 | SkFixed fScaleX, fScaleY; |
| 124 | FT_Matrix fMatrix22; |
| 125 | uint32_t fLoadGlyphFlags; |
| 126 | |
| 127 | FT_Error setupSize(); |
| 128 | }; |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////// |
| 131 | /////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | #include "SkStream.h" |
| 134 | |
| 135 | struct SkFaceRec { |
| 136 | SkFaceRec* fNext; |
| 137 | FT_Face fFace; |
| 138 | FT_StreamRec fFTStream; |
| 139 | SkStream* fSkStream; |
| 140 | uint32_t fRefCnt; |
| 141 | uint32_t fFontID; |
| 142 | |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 143 | // assumes ownership of the stream, will call unref() when its done |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | SkFaceRec(SkStream* strm, uint32_t fontID); |
| 145 | ~SkFaceRec() { |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 146 | fSkStream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | extern "C" { |
| 151 | static unsigned long sk_stream_read(FT_Stream stream, |
| 152 | unsigned long offset, |
| 153 | unsigned char* buffer, |
| 154 | unsigned long count ) { |
| 155 | SkStream* str = (SkStream*)stream->descriptor.pointer; |
| 156 | |
| 157 | if (count) { |
| 158 | if (!str->rewind()) { |
| 159 | return 0; |
| 160 | } else { |
| 161 | unsigned long ret; |
| 162 | if (offset) { |
| 163 | ret = str->read(NULL, offset); |
| 164 | if (ret != offset) { |
| 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | ret = str->read(buffer, count); |
| 169 | if (ret != count) { |
| 170 | return 0; |
| 171 | } |
| 172 | count = ret; |
| 173 | } |
| 174 | } |
| 175 | return count; |
| 176 | } |
| 177 | |
| 178 | static void sk_stream_close( FT_Stream stream) {} |
| 179 | } |
| 180 | |
| 181 | SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID) |
| 182 | : fSkStream(strm), fFontID(fontID) { |
| 183 | // SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm)); |
| 184 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 185 | sk_bzero(&fFTStream, sizeof(fFTStream)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | fFTStream.size = fSkStream->getLength(); |
| 187 | fFTStream.descriptor.pointer = fSkStream; |
| 188 | fFTStream.read = sk_stream_read; |
| 189 | fFTStream.close = sk_stream_close; |
| 190 | } |
| 191 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 192 | // Will return 0 on failure |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | static SkFaceRec* ref_ft_face(uint32_t fontID) { |
| 194 | SkFaceRec* rec = gFaceRecHead; |
| 195 | while (rec) { |
| 196 | if (rec->fFontID == fontID) { |
| 197 | SkASSERT(rec->fFace); |
| 198 | rec->fRefCnt += 1; |
| 199 | return rec; |
| 200 | } |
| 201 | rec = rec->fNext; |
| 202 | } |
| 203 | |
| 204 | SkStream* strm = SkFontHost::OpenStream(fontID); |
| 205 | if (NULL == strm) { |
| 206 | SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | // this passes ownership of strm to the rec |
| 211 | rec = SkNEW_ARGS(SkFaceRec, (strm, fontID)); |
| 212 | |
| 213 | FT_Open_Args args; |
| 214 | memset(&args, 0, sizeof(args)); |
| 215 | const void* memoryBase = strm->getMemoryBase(); |
| 216 | |
| 217 | if (NULL != memoryBase) { |
| 218 | //printf("mmap(%s)\n", keyString.c_str()); |
| 219 | args.flags = FT_OPEN_MEMORY; |
| 220 | args.memory_base = (const FT_Byte*)memoryBase; |
| 221 | args.memory_size = strm->getLength(); |
| 222 | } else { |
| 223 | //printf("fopen(%s)\n", keyString.c_str()); |
| 224 | args.flags = FT_OPEN_STREAM; |
| 225 | args.stream = &rec->fFTStream; |
| 226 | } |
| 227 | |
| 228 | FT_Error err = FT_Open_Face(gFTLibrary, &args, 0, &rec->fFace); |
| 229 | |
| 230 | if (err) { // bad filename, try the default font |
| 231 | fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID); |
| 232 | SkDELETE(rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } else { |
| 235 | SkASSERT(rec->fFace); |
| 236 | //fprintf(stderr, "Opened font '%s'\n", filename.c_str()); |
| 237 | rec->fNext = gFaceRecHead; |
| 238 | gFaceRecHead = rec; |
| 239 | rec->fRefCnt = 1; |
| 240 | return rec; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void unref_ft_face(FT_Face face) { |
| 245 | SkFaceRec* rec = gFaceRecHead; |
| 246 | SkFaceRec* prev = NULL; |
| 247 | while (rec) { |
| 248 | SkFaceRec* next = rec->fNext; |
| 249 | if (rec->fFace == face) { |
| 250 | if (--rec->fRefCnt == 0) { |
| 251 | if (prev) { |
| 252 | prev->fNext = next; |
| 253 | } else { |
| 254 | gFaceRecHead = next; |
| 255 | } |
| 256 | FT_Done_Face(face); |
| 257 | SkDELETE(rec); |
| 258 | } |
| 259 | return; |
| 260 | } |
| 261 | prev = rec; |
| 262 | rec = next; |
| 263 | } |
| 264 | SkASSERT("shouldn't get here, face not in list"); |
| 265 | } |
| 266 | |
| 267 | /////////////////////////////////////////////////////////////////////////// |
| 268 | |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 269 | void SkFontHost::FilterRec(SkScalerContext::Rec* rec) { |
agl@chromium.org | f18d876 | 2009-07-28 18:38:08 +0000 | [diff] [blame^] | 270 | if (!gLCDSupportValid) { |
| 271 | InitFreetype(); |
| 272 | FT_Done_FreeType(gFTLibrary); |
| 273 | } |
| 274 | |
| 275 | if (!gLCDSupport && rec->isLCD()) { |
| 276 | // If the runtime Freetype library doesn't support LCD mode, we disable |
| 277 | // it here. |
| 278 | rec->fMaskFormat = SkMask::kA8_Format; |
| 279 | } |
| 280 | |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 281 | SkPaint::Hinting h = rec->getHinting(); |
| 282 | if (SkPaint::kFull_Hinting == h && !rec->isLCD()) { |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 283 | // collapse full->normaling hinting if we're not doing LCD |
| 284 | h = SkPaint::kNormal_Hinting; |
| 285 | } else if (rec->fSubpixelPositioning && SkPaint::kNo_Hinting != h) { |
| 286 | // to do subpixel, we must have at most slight hinting |
| 287 | h = SkPaint::kSlight_Hinting; |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 288 | } |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 289 | rec->setHinting(h); |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 290 | } |
| 291 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc) |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 293 | : SkScalerContext(desc) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | SkAutoMutexAcquire ac(gFTMutex); |
| 295 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | if (gFTCount == 0) { |
reed@android.com | 659aaf9 | 2009-07-23 15:20:21 +0000 | [diff] [blame] | 297 | if (!InitFreetype()) { |
| 298 | sk_throw(); |
| 299 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | } |
| 301 | ++gFTCount; |
| 302 | |
| 303 | // load the font file |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 304 | fFTSize = NULL; |
| 305 | fFace = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | fFaceRec = ref_ft_face(fRec.fFontID); |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 307 | if (NULL == fFaceRec) { |
| 308 | return; |
| 309 | } |
| 310 | fFace = fFaceRec->fFace; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | |
| 312 | // compute our factors from the record |
| 313 | |
| 314 | SkMatrix m; |
| 315 | |
| 316 | fRec.getSingleMatrix(&m); |
| 317 | |
| 318 | #ifdef DUMP_STRIKE_CREATION |
| 319 | SkString keyString; |
| 320 | SkFontHost::GetDescriptorKeyString(desc, &keyString); |
| 321 | printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize), |
| 322 | SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX), |
| 323 | SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]), |
| 324 | SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]), |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 325 | fRec.getHinting(), fRec.fMaskFormat, keyString.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 326 | #endif |
| 327 | |
| 328 | // now compute our scale factors |
| 329 | SkScalar sx = m.getScaleX(); |
| 330 | SkScalar sy = m.getScaleY(); |
| 331 | |
| 332 | if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) { |
| 333 | // sort of give up on hinting |
| 334 | sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX())); |
| 335 | sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy)); |
| 336 | sx = sy = SkScalarAve(sx, sy); |
| 337 | |
| 338 | SkScalar inv = SkScalarInvert(sx); |
| 339 | |
| 340 | // flip the skew elements to go from our Y-down system to FreeType's |
| 341 | fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv)); |
| 342 | fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv)); |
| 343 | fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv)); |
| 344 | fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv)); |
| 345 | } else { |
| 346 | fMatrix22.xx = fMatrix22.yy = SK_Fixed1; |
| 347 | fMatrix22.xy = fMatrix22.yx = 0; |
| 348 | } |
| 349 | |
| 350 | fScaleX = SkScalarToFixed(sx); |
| 351 | fScaleY = SkScalarToFixed(sy); |
| 352 | |
| 353 | // compute the flags we send to Load_Glyph |
| 354 | { |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 355 | FT_Int32 loadFlags = FT_LOAD_DEFAULT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 357 | switch (fRec.getHinting()) { |
| 358 | case SkPaint::kNo_Hinting: |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 359 | loadFlags = FT_LOAD_NO_HINTING; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | break; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 361 | case SkPaint::kSlight_Hinting: |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 362 | loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | break; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 364 | case SkPaint::kNormal_Hinting: |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 365 | loadFlags = FT_LOAD_TARGET_NORMAL; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 366 | break; |
| 367 | case SkPaint::kFull_Hinting: |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 368 | loadFlags = FT_LOAD_TARGET_NORMAL; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 369 | if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat) |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 370 | loadFlags = FT_LOAD_TARGET_LCD; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 371 | else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat) |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 372 | loadFlags = FT_LOAD_TARGET_LCD_V; |
| 373 | break; |
| 374 | default: |
| 375 | SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 379 | fLoadGlyphFlags = loadFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // now create the FT_Size |
| 383 | |
| 384 | { |
| 385 | FT_Error err; |
| 386 | |
| 387 | err = FT_New_Size(fFace, &fFTSize); |
| 388 | if (err != 0) { |
| 389 | SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n", |
| 390 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 391 | fFace = NULL; |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | err = FT_Activate_Size(fFTSize); |
| 396 | if (err != 0) { |
| 397 | SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 398 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 399 | fFTSize = NULL; |
| 400 | } |
| 401 | |
| 402 | err = FT_Set_Char_Size( fFace, |
| 403 | SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY), |
| 404 | 72, 72); |
| 405 | if (err != 0) { |
| 406 | SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 407 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 408 | fFace = NULL; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | FT_Set_Transform( fFace, &fMatrix22, NULL); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | SkScalerContext_FreeType::~SkScalerContext_FreeType() { |
| 417 | if (fFTSize != NULL) { |
| 418 | FT_Done_Size(fFTSize); |
| 419 | } |
| 420 | |
| 421 | SkAutoMutexAcquire ac(gFTMutex); |
| 422 | |
| 423 | if (fFace != NULL) { |
| 424 | unref_ft_face(fFace); |
| 425 | } |
| 426 | if (--gFTCount == 0) { |
| 427 | // SkDEBUGF(("FT_Done_FreeType\n")); |
| 428 | FT_Done_FreeType(gFTLibrary); |
| 429 | SkDEBUGCODE(gFTLibrary = NULL;) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* We call this before each use of the fFace, since we may be sharing |
| 434 | this face with other context (at different sizes). |
| 435 | */ |
| 436 | FT_Error SkScalerContext_FreeType::setupSize() { |
| 437 | /* In the off-chance that a font has been removed, we want to error out |
| 438 | right away, so call resolve just to be sure. |
| 439 | |
| 440 | TODO: perhaps we can skip this, by walking the global font cache and |
| 441 | killing all of the contexts when we know that a given fontID is going |
| 442 | away... |
| 443 | */ |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 444 | if (!SkFontHost::ValidFontID(fRec.fFontID)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 445 | return (FT_Error)-1; |
| 446 | } |
| 447 | |
| 448 | FT_Error err = FT_Activate_Size(fFTSize); |
| 449 | |
| 450 | if (err != 0) { |
| 451 | SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n", |
| 452 | fFaceRec->fFontID, fScaleX, fScaleY, err)); |
| 453 | fFTSize = NULL; |
| 454 | } else { |
| 455 | // seems we need to reset this every time (not sure why, but without it |
| 456 | // I get random italics from some other fFTSize) |
| 457 | FT_Set_Transform( fFace, &fMatrix22, NULL); |
| 458 | } |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | unsigned SkScalerContext_FreeType::generateGlyphCount() const { |
| 463 | return fFace->num_glyphs; |
| 464 | } |
| 465 | |
| 466 | uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) { |
| 467 | return SkToU16(FT_Get_Char_Index( fFace, uni )); |
| 468 | } |
| 469 | |
| 470 | static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) { |
| 471 | switch (format) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 472 | case SkMask::kHorizontalLCD_Format: |
| 473 | case SkMask::kVerticalLCD_Format: |
| 474 | SkASSERT(!"An LCD format should never be passed here"); |
| 475 | return FT_PIXEL_MODE_GRAY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | case SkMask::kBW_Format: |
| 477 | return FT_PIXEL_MODE_MONO; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | case SkMask::kA8_Format: |
| 479 | default: |
| 480 | return FT_PIXEL_MODE_GRAY; |
| 481 | } |
| 482 | } |
| 483 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) { |
| 485 | #ifdef FT_ADVANCES_H |
| 486 | /* unhinted and light hinted text have linearly scaled advances |
| 487 | * which are very cheap to compute with some font formats... |
| 488 | */ |
| 489 | { |
| 490 | SkAutoMutexAcquire ac(gFTMutex); |
| 491 | |
| 492 | if (this->setupSize()) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 493 | glyph->zeroMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
| 497 | FT_Error error; |
| 498 | FT_Fixed advance; |
| 499 | |
| 500 | error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount), |
| 501 | fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY, |
| 502 | &advance ); |
| 503 | if (0 == error) { |
| 504 | glyph->fRsbDelta = 0; |
| 505 | glyph->fLsbDelta = 0; |
| 506 | glyph->fAdvanceX = advance; // advance *2/3; //DEBUG |
| 507 | glyph->fAdvanceY = 0; |
| 508 | return; |
| 509 | } |
| 510 | } |
| 511 | #endif /* FT_ADVANCES_H */ |
| 512 | /* otherwise, we need to load/hint the glyph, which is slower */ |
| 513 | this->generateMetrics(glyph); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) { |
| 518 | SkAutoMutexAcquire ac(gFTMutex); |
| 519 | |
| 520 | glyph->fRsbDelta = 0; |
| 521 | glyph->fLsbDelta = 0; |
| 522 | |
| 523 | FT_Error err; |
| 524 | |
| 525 | if (this->setupSize()) { |
| 526 | goto ERROR; |
| 527 | } |
| 528 | |
| 529 | err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags ); |
| 530 | if (err != 0) { |
| 531 | SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 532 | fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err)); |
| 533 | ERROR: |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 534 | glyph->zeroMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | |
| 538 | switch ( fFace->glyph->format ) { |
| 539 | case FT_GLYPH_FORMAT_OUTLINE: |
| 540 | FT_BBox bbox; |
| 541 | |
| 542 | FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox); |
| 543 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 544 | if (fRec.fSubpixelPositioning) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | int dx = glyph->getSubXFixed() >> 10; |
| 546 | int dy = glyph->getSubYFixed() >> 10; |
| 547 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 548 | bbox.xMin += dx; |
| 549 | bbox.yMin -= dy; |
| 550 | bbox.xMax += dx; |
| 551 | bbox.yMax -= dy; |
| 552 | } |
| 553 | |
| 554 | bbox.xMin &= ~63; |
| 555 | bbox.yMin &= ~63; |
| 556 | bbox.xMax = (bbox.xMax + 63) & ~63; |
| 557 | bbox.yMax = (bbox.yMax + 63) & ~63; |
| 558 | |
| 559 | glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6); |
| 560 | glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6); |
| 561 | glyph->fTop = -SkToS16(bbox.yMax >> 6); |
| 562 | glyph->fLeft = SkToS16(bbox.xMin >> 6); |
| 563 | break; |
| 564 | |
| 565 | case FT_GLYPH_FORMAT_BITMAP: |
| 566 | glyph->fWidth = SkToU16(fFace->glyph->bitmap.width); |
| 567 | glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows); |
| 568 | glyph->fTop = -SkToS16(fFace->glyph->bitmap_top); |
| 569 | glyph->fLeft = SkToS16(fFace->glyph->bitmap_left); |
| 570 | break; |
| 571 | |
| 572 | default: |
| 573 | SkASSERT(!"unknown glyph format"); |
| 574 | goto ERROR; |
| 575 | } |
| 576 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 577 | if (!fRec.fSubpixelPositioning) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x); |
| 579 | glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y); |
| 580 | if (fRec.fFlags & kDevKernText_Flag) { |
| 581 | glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta); |
| 582 | glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta); |
| 583 | } |
| 584 | } else { |
| 585 | glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance); |
| 586 | glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance); |
| 587 | } |
| 588 | |
| 589 | #ifdef ENABLE_GLYPH_SPEW |
| 590 | SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY)); |
| 591 | SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth)); |
| 592 | #endif |
| 593 | } |
| 594 | |
reed@android.com | f549369 | 2009-07-22 19:21:01 +0000 | [diff] [blame] | 595 | #if defined(SK_SUPPORT_LCDTEXT) |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 596 | namespace skia_freetype_support { |
| 597 | // extern functions from SkFontHost_FreeType_Subpixel |
| 598 | extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source); |
| 599 | extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source); |
| 600 | } |
| 601 | |
| 602 | using namespace skia_freetype_support; |
| 603 | #endif |
| 604 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) { |
| 606 | SkAutoMutexAcquire ac(gFTMutex); |
| 607 | |
| 608 | FT_Error err; |
| 609 | |
| 610 | if (this->setupSize()) { |
| 611 | goto ERROR; |
| 612 | } |
| 613 | |
| 614 | err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags); |
| 615 | if (err != 0) { |
| 616 | SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n", |
| 617 | glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err)); |
| 618 | ERROR: |
| 619 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 620 | return; |
| 621 | } |
| 622 | |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 623 | const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format || |
| 624 | fRec.fMaskFormat == SkMask::kVerticalLCD_Format; |
| 625 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 626 | switch ( fFace->glyph->format ) { |
| 627 | case FT_GLYPH_FORMAT_OUTLINE: { |
| 628 | FT_Outline* outline = &fFace->glyph->outline; |
| 629 | FT_BBox bbox; |
| 630 | FT_Bitmap target; |
| 631 | |
| 632 | int dx = 0, dy = 0; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 633 | if (fRec.fSubpixelPositioning) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 634 | dx = glyph.getSubXFixed() >> 10; |
| 635 | dy = glyph.getSubYFixed() >> 10; |
| 636 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 637 | dy = -dy; |
| 638 | } |
| 639 | FT_Outline_Get_CBox(outline, &bbox); |
| 640 | /* |
| 641 | what we really want to do for subpixel is |
| 642 | offset(dx, dy) |
| 643 | compute_bounds |
| 644 | offset(bbox & !63) |
| 645 | but that is two calls to offset, so we do the following, which |
| 646 | achieves the same thing with only one offset call. |
| 647 | */ |
| 648 | FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63), |
| 649 | dy - ((bbox.yMin + dy) & ~63)); |
| 650 | |
reed@android.com | f549369 | 2009-07-22 19:21:01 +0000 | [diff] [blame] | 651 | #if defined(SK_SUPPORT_LCDTEXT) |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 652 | if (lcdRenderMode) { |
| 653 | // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case |
| 654 | // we have to call FT_Render_Glyph and memcpy the image out. |
| 655 | const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format; |
| 656 | FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD; |
| 657 | FT_Render_Glyph(fFace->glyph, mode); |
| 658 | |
| 659 | if (isVertical) |
| 660 | CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap); |
| 661 | else |
| 662 | CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap); |
| 663 | |
| 664 | break; |
| 665 | } |
| 666 | #endif |
| 667 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | target.width = glyph.fWidth; |
| 669 | target.rows = glyph.fHeight; |
| 670 | target.pitch = glyph.rowBytes(); |
| 671 | target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage); |
| 672 | target.pixel_mode = compute_pixel_mode( |
| 673 | (SkMask::Format)fRec.fMaskFormat); |
| 674 | target.num_grays = 256; |
| 675 | |
| 676 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 677 | FT_Outline_Get_Bitmap(gFTLibrary, outline, &target); |
| 678 | } break; |
| 679 | |
| 680 | case FT_GLYPH_FORMAT_BITMAP: { |
| 681 | SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width); |
| 682 | SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows); |
| 683 | SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top); |
| 684 | SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left); |
| 685 | |
| 686 | const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer; |
| 687 | uint8_t* dst = (uint8_t*)glyph.fImage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 688 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 689 | if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) { |
| 690 | unsigned srcRowBytes = fFace->glyph->bitmap.pitch; |
| 691 | unsigned dstRowBytes = glyph.rowBytes(); |
| 692 | unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes); |
| 693 | unsigned extraRowBytes = dstRowBytes - minRowBytes; |
| 694 | |
| 695 | for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) { |
| 696 | memcpy(dst, src, minRowBytes); |
| 697 | memset(dst + minRowBytes, 0, extraRowBytes); |
| 698 | src += srcRowBytes; |
| 699 | dst += dstRowBytes; |
| 700 | } |
| 701 | } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) { |
| 702 | for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) { |
| 703 | uint8_t byte = 0; |
| 704 | int bits = 0; |
| 705 | const uint8_t* src_row = src; |
| 706 | uint8_t* dst_row = dst; |
| 707 | |
| 708 | for (int x = 0; x < fFace->glyph->bitmap.width; ++x) { |
| 709 | if (!bits) { |
| 710 | byte = *src_row++; |
| 711 | bits = 8; |
| 712 | } |
| 713 | |
| 714 | *dst_row++ = byte & 0x80 ? 0xff : 0; |
| 715 | bits--; |
| 716 | byte <<= 1; |
| 717 | } |
| 718 | |
| 719 | src += fFace->glyph->bitmap.pitch; |
| 720 | dst += glyph.rowBytes(); |
| 721 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 722 | } |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 723 | |
| 724 | if (lcdRenderMode) |
| 725 | glyph.expandA8ToLCD(); |
| 726 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | } break; |
| 728 | |
| 729 | default: |
| 730 | SkASSERT(!"unknown glyph format"); |
| 731 | goto ERROR; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /////////////////////////////////////////////////////////////////////////////// |
| 736 | |
| 737 | #define ft2sk(x) SkFixedToScalar((x) << 10) |
| 738 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 739 | #if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | #define CONST_PARAM const |
| 741 | #else // older freetype doesn't use const here |
| 742 | #define CONST_PARAM |
| 743 | #endif |
| 744 | |
| 745 | static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 746 | SkPath* path = (SkPath*)ctx; |
| 747 | path->close(); // to close the previous contour (if any) |
| 748 | path->moveTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 753 | SkPath* path = (SkPath*)ctx; |
| 754 | path->lineTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 759 | void* ctx) { |
| 760 | SkPath* path = (SkPath*)ctx; |
| 761 | path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y)); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 766 | CONST_PARAM FT_Vector* pt2, void* ctx) { |
| 767 | SkPath* path = (SkPath*)ctx; |
| 768 | path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), |
| 769 | -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y)); |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, |
| 774 | SkPath* path) { |
| 775 | SkAutoMutexAcquire ac(gFTMutex); |
| 776 | |
| 777 | SkASSERT(&glyph && path); |
| 778 | |
| 779 | if (this->setupSize()) { |
| 780 | path->reset(); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | uint32_t flags = fLoadGlyphFlags; |
| 785 | flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline |
| 786 | flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline) |
| 787 | |
| 788 | FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags); |
| 789 | |
| 790 | if (err != 0) { |
| 791 | SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 792 | glyph.getGlyphID(fBaseGlyphCount), flags, err)); |
| 793 | path->reset(); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | FT_Outline_Funcs funcs; |
| 798 | |
| 799 | funcs.move_to = move_proc; |
| 800 | funcs.line_to = line_proc; |
| 801 | funcs.conic_to = quad_proc; |
| 802 | funcs.cubic_to = cubic_proc; |
| 803 | funcs.shift = 0; |
| 804 | funcs.delta = 0; |
| 805 | |
| 806 | err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path); |
| 807 | |
| 808 | if (err != 0) { |
| 809 | SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", |
| 810 | glyph.getGlyphID(fBaseGlyphCount), flags, err)); |
| 811 | path->reset(); |
| 812 | return; |
| 813 | } |
| 814 | |
| 815 | path->close(); |
| 816 | } |
| 817 | |
| 818 | void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx, |
| 819 | SkPaint::FontMetrics* my) { |
| 820 | if (NULL == mx && NULL == my) { |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | SkAutoMutexAcquire ac(gFTMutex); |
| 825 | |
| 826 | if (this->setupSize()) { |
reed@android.com | a8a8b8b | 2009-05-04 15:00:11 +0000 | [diff] [blame] | 827 | ERROR: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 828 | if (mx) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 829 | sk_bzero(mx, sizeof(SkPaint::FontMetrics)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 830 | } |
| 831 | if (my) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 832 | sk_bzero(my, sizeof(SkPaint::FontMetrics)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 833 | } |
| 834 | return; |
| 835 | } |
| 836 | |
reed@android.com | a8a8b8b | 2009-05-04 15:00:11 +0000 | [diff] [blame] | 837 | FT_Face face = fFace; |
| 838 | int upem = face->units_per_EM; |
| 839 | if (upem <= 0) { |
| 840 | goto ERROR; |
| 841 | } |
| 842 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 843 | SkPoint pts[6]; |
| 844 | SkFixed ys[6]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | SkFixed scaleY = fScaleY; |
| 846 | SkFixed mxy = fMatrix22.xy; |
| 847 | SkFixed myy = fMatrix22.yy; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 848 | SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem; |
| 849 | SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 850 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 851 | int leading = face->height - (face->ascender + -face->descender); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 852 | if (leading < 0) { |
| 853 | leading = 0; |
| 854 | } |
| 855 | |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 856 | // Try to get the OS/2 table from the font. This contains the specific |
| 857 | // average font width metrics which Windows uses. |
| 858 | TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2); |
| 859 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 860 | ys[0] = -face->bbox.yMax; |
| 861 | ys[1] = -face->ascender; |
| 862 | ys[2] = -face->descender; |
| 863 | ys[3] = -face->bbox.yMin; |
| 864 | ys[4] = leading; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 865 | ys[5] = os2 ? os2->xAvgCharWidth : 0; |
| 866 | |
| 867 | SkScalar x_height; |
| 868 | if (os2 && os2->sxHeight) { |
| 869 | x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem)); |
| 870 | } else { |
| 871 | const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x'); |
| 872 | if (x_glyph) { |
| 873 | FT_BBox bbox; |
| 874 | FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags); |
| 875 | FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox); |
| 876 | x_height = SkIntToScalar(bbox.yMax) / 64; |
| 877 | } else { |
| 878 | x_height = 0; |
| 879 | } |
| 880 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 881 | |
| 882 | // convert upem-y values into scalar points |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 883 | for (int i = 0; i < 6; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 884 | SkFixed y = SkMulDiv(scaleY, ys[i], upem); |
| 885 | SkFixed x = SkFixedMul(mxy, y); |
| 886 | y = SkFixedMul(myy, y); |
| 887 | pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y)); |
| 888 | } |
| 889 | |
| 890 | if (mx) { |
| 891 | mx->fTop = pts[0].fX; |
| 892 | mx->fAscent = pts[1].fX; |
| 893 | mx->fDescent = pts[2].fX; |
| 894 | mx->fBottom = pts[3].fX; |
| 895 | mx->fLeading = pts[4].fX; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 896 | mx->fAvgCharWidth = pts[5].fX; |
| 897 | mx->fXMin = xmin; |
| 898 | mx->fXMax = xmax; |
| 899 | mx->fXHeight = x_height; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 900 | } |
| 901 | if (my) { |
| 902 | my->fTop = pts[0].fY; |
| 903 | my->fAscent = pts[1].fY; |
| 904 | my->fDescent = pts[2].fY; |
| 905 | my->fBottom = pts[3].fY; |
| 906 | my->fLeading = pts[4].fY; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 907 | my->fAvgCharWidth = pts[5].fY; |
| 908 | my->fXMin = xmin; |
| 909 | my->fXMax = xmax; |
| 910 | my->fXHeight = x_height; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | //////////////////////////////////////////////////////////////////////// |
| 915 | //////////////////////////////////////////////////////////////////////// |
| 916 | |
| 917 | SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 918 | SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc)); |
| 919 | if (!c->success()) { |
| 920 | SkDELETE(c); |
| 921 | c = NULL; |
| 922 | } |
| 923 | return c; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /////////////////////////////////////////////////////////////////////////////// |
| 927 | |
| 928 | /* Export this so that other parts of our FonttHost port can make use of our |
| 929 | ability to extract the name+style from a stream, using FreeType's api. |
| 930 | */ |
| 931 | SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) { |
| 932 | FT_Library library; |
reed@android.com | bfbd4ff | 2009-07-23 17:44:41 +0000 | [diff] [blame] | 933 | if (FT_Init_FreeType(&library)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 934 | name->set(NULL); |
| 935 | return SkTypeface::kNormal; |
| 936 | } |
| 937 | |
| 938 | FT_Open_Args args; |
| 939 | memset(&args, 0, sizeof(args)); |
| 940 | |
| 941 | const void* memoryBase = stream->getMemoryBase(); |
| 942 | FT_StreamRec streamRec; |
| 943 | |
| 944 | if (NULL != memoryBase) { |
| 945 | args.flags = FT_OPEN_MEMORY; |
| 946 | args.memory_base = (const FT_Byte*)memoryBase; |
| 947 | args.memory_size = stream->getLength(); |
| 948 | } else { |
| 949 | memset(&streamRec, 0, sizeof(streamRec)); |
| 950 | streamRec.size = stream->read(NULL, 0); |
| 951 | streamRec.descriptor.pointer = stream; |
| 952 | streamRec.read = sk_stream_read; |
| 953 | streamRec.close = sk_stream_close; |
| 954 | |
| 955 | args.flags = FT_OPEN_STREAM; |
| 956 | args.stream = &streamRec; |
| 957 | } |
| 958 | |
| 959 | FT_Face face; |
| 960 | if (FT_Open_Face(library, &args, 0, &face)) { |
| 961 | FT_Done_FreeType(library); |
| 962 | name->set(NULL); |
| 963 | return SkTypeface::kNormal; |
| 964 | } |
| 965 | |
| 966 | name->set(face->family_name); |
| 967 | int style = SkTypeface::kNormal; |
| 968 | |
| 969 | if (face->style_flags & FT_STYLE_FLAG_BOLD) { |
| 970 | style |= SkTypeface::kBold; |
| 971 | } |
| 972 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) { |
| 973 | style |= SkTypeface::kItalic; |
| 974 | } |
| 975 | |
| 976 | FT_Done_Face(face); |
| 977 | FT_Done_FreeType(library); |
| 978 | return (SkTypeface::Style)style; |
| 979 | } |