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