blob: fd85bb28ba07ae77ff284469ebaca265fc674d2d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* 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.org309485b2009-07-21 17:41:32 +000018#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#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.orgcc3096b2009-04-22 22:09:04 +000035#include FT_TRUETYPE_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000036#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000037// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
38#include FT_SYNTHESIS_H
agl@chromium.org309485b2009-07-21 17:41:32 +000039
reed@android.comf5493692009-07-22 19:21:01 +000040#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +000041#include FT_LCD_FILTER_H
42#endif
43
reed@android.com8a1c16f2008-12-17 15:59:43 +000044#ifdef FT_ADVANCES_H
45#include FT_ADVANCES_H
46#endif
47
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000048#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.org309485b2009-07-21 17:41:32 +000055#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000056#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000057#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000058#endif
59
reed@android.com8a1c16f2008-12-17 15:59:43 +000060//#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
75struct SkFaceRec;
76
77static SkMutex gFTMutex;
78static int gFTCount;
79static FT_Library gFTLibrary;
80static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000081static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
82static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
84/////////////////////////////////////////////////////////////////////////
85
agl@chromium.orge76073b2010-06-04 20:31:17 +000086// 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.
88static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
89
agl@chromium.org309485b2009-07-21 17:41:32 +000090static bool
91InitFreetype() {
92 FT_Error err = FT_Init_FreeType(&gFTLibrary);
93 if (err)
94 return false;
95
reed@android.comf5493692009-07-22 19:21:01 +000096#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +000097 // Setup LCD filtering. This reduces colour fringes for LCD rendered
98 // glyphs.
99 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000100 gLCDSupport = err == 0;
agl@chromium.org309485b2009-07-21 17:41:32 +0000101#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000102 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000103
104 return true;
105}
106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107class SkScalerContext_FreeType : public SkScalerContext {
108public:
109 SkScalerContext_FreeType(const SkDescriptor* desc);
110 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000111
reed@android.com62900b42009-02-11 15:07:19 +0000112 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000113 return fFaceRec != NULL &&
114 fFTSize != NULL &&
115 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117
118protected:
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.com9d3a9852010-01-08 14:07:42 +0000127 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128
129private:
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.org4526a842010-02-05 23:08:20 +0000138 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139};
140
141///////////////////////////////////////////////////////////////////////////
142///////////////////////////////////////////////////////////////////////////
143
144#include "SkStream.h"
145
146struct 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.comb1d9d2e2009-03-04 17:37:51 +0000154 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkFaceRec(SkStream* strm, uint32_t fontID);
156 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000157 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
159};
160
161extern "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
192SkFaceRec::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.com4516f472009-06-29 16:25:36 +0000196 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 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.com62900b42009-02-11 15:07:19 +0000203// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204static 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.com8a1c16f2008-12-17 15:59:43 +0000218 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.com8a1c16f2008-12-17 15:59:43 +0000244 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
255static 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.com36a4c2a2009-07-22 19:52:11 +0000280void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000281 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.com36a4c2a2009-07-22 19:52:11 +0000292 SkPaint::Hinting h = rec->getHinting();
293 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
agl@chromium.orgb4234a22010-01-21 11:43:44 +0000294 // collapse full->normal hinting if we're not doing LCD
reed@android.come4d0bc02009-07-24 19:53:20 +0000295 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.com36a4c2a2009-07-22 19:52:11 +0000299 }
reed@android.come4d0bc02009-07-24 19:53:20 +0000300 rec->setHinting(h);
reed@android.com36a4c2a2009-07-22 19:52:11 +0000301}
302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000304 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 SkAutoMutexAcquire ac(gFTMutex);
306
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000308 if (!InitFreetype()) {
309 sk_throw();
310 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 }
312 ++gFTCount;
313
314 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000315 fFTSize = NULL;
316 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000318 if (NULL == fFaceRec) {
319 return;
320 }
321 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322
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.org309485b2009-07-21 17:41:32 +0000336 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337#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.come4d0bc02009-07-24 19:53:20 +0000366 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367
agl@chromium.org70a303f2010-05-10 14:15:50 +0000368 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.com8a1c16f2008-12-17 15:59:43 +0000395 }
396
agl@chromium.org99e1b902010-01-05 01:19:44 +0000397 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000398 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000399
reed@android.come4d0bc02009-07-24 19:53:20 +0000400 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 }
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
437SkScalerContext_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*/
457FT_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.comb1d9d2e2009-03-04 17:37:51 +0000465 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 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.org4526a842010-02-05 23:08:20 +0000483void 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.com8a1c16f2008-12-17 15:59:43 +0000490unsigned SkScalerContext_FreeType::generateGlyphCount() const {
491 return fFace->num_glyphs;
492}
493
494uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
495 return SkToU16(FT_Get_Char_Index( fFace, uni ));
496}
497
reed@android.com9d3a9852010-01-08 14:07:42 +0000498SkUnichar 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.com8a1c16f2008-12-17 15:59:43 +0000513static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
514 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000515 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.com8a1c16f2008-12-17 15:59:43 +0000519 case SkMask::kBW_Format:
520 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 case SkMask::kA8_Format:
522 default:
523 return FT_PIXEL_MODE_GRAY;
524 }
525}
526
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527void 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.com62900b42009-02-11 15:07:19 +0000536 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 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
560void 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.com62900b42009-02-11 15:07:19 +0000577 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578 return;
579 }
580
581 switch ( fFace->glyph->format ) {
582 case FT_GLYPH_FORMAT_OUTLINE:
583 FT_BBox bbox;
584
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000585 if (fRec.fFlags & kEmbolden_Flag) {
586 emboldenOutline(&fFace->glyph->outline);
587 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
589
agl@chromium.org309485b2009-07-21 17:41:32 +0000590 if (fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 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.orge76073b2010-06-04 20:31:17 +0000612 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.com8a1c16f2008-12-17 15:59:43 +0000616 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.org309485b2009-07-21 17:41:32 +0000627 if (!fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 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.comf5493692009-07-22 19:21:01 +0000645#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000646namespace skia_freetype_support {
647// extern functions from SkFontHost_FreeType_Subpixel
648extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
649extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
650}
651
652using namespace skia_freetype_support;
653#endif
654
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655void 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.org309485b2009-07-21 17:41:32 +0000673 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
674 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
675
reed@android.com8a1c16f2008-12-17 15:59:43 +0000676 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.org4526a842010-02-05 23:08:20 +0000682 if (fRec.fFlags & kEmbolden_Flag) {
683 emboldenOutline(outline);
684 }
685
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686 int dx = 0, dy = 0;
agl@chromium.org309485b2009-07-21 17:41:32 +0000687 if (fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 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.comf5493692009-07-22 19:21:01 +0000705#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000706 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.com8a1c16f2008-12-17 15:59:43 +0000722 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.orge76073b2010-06-04 20:31:17 +0000735 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.com8a1c16f2008-12-17 15:59:43 +0000739 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.com8a1c16f2008-12-17 15:59:43 +0000746
agl@chromium.org558434a2009-08-11 17:22:38 +0000747 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.orgcc3096b2009-04-22 22:09:04 +0000750 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.org558434a2009-08-11 17:22:38 +0000761 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000762 (glyph.fMaskFormat == SkMask::kA8_Format ||
763 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
764 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000765 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.org558434a2009-08-11 17:22:38 +0000785 } else {
786 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000788
789 if (lcdRenderMode)
790 glyph.expandA8ToLCD();
791
reed@android.com8a1c16f2008-12-17 15:59:43 +0000792 } 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.com6f252972009-01-14 16:46:16 +0000804#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 #define CONST_PARAM const
806#else // older freetype doesn't use const here
807 #define CONST_PARAM
808#endif
809
810static 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
817static 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
823static 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
830static 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
838void 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.org4526a842010-02-05 23:08:20 +0000862 if (fRec.fFlags & kEmbolden_Flag) {
863 emboldenOutline(&fFace->glyph->outline);
864 }
865
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 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
887void 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.coma8a8b8b2009-05-04 15:00:11 +0000896 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +0000898 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899 }
900 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +0000901 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 }
903 return;
904 }
905
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000906 FT_Face face = fFace;
907 int upem = face->units_per_EM;
908 if (upem <= 0) {
909 goto ERROR;
910 }
911
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000912 SkPoint pts[6];
913 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914 SkFixed scaleY = fScaleY;
915 SkFixed mxy = fMatrix22.xy;
916 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000917 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
918 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000919
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000920 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921 if (leading < 0) {
922 leading = 0;
923 }
924
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000925 // 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.com8a1c16f2008-12-17 15:59:43 +0000929 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.orgcc3096b2009-04-22 22:09:04 +0000934 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.org4526a842010-02-05 23:08:20 +0000944 if (fRec.fFlags & kEmbolden_Flag) {
945 emboldenOutline(&fFace->glyph->outline);
946 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000947 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.com8a1c16f2008-12-17 15:59:43 +0000953
954 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000955 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956 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.orgcc3096b2009-04-22 22:09:04 +0000968 mx->fAvgCharWidth = pts[5].fX;
969 mx->fXMin = xmin;
970 mx->fXMax = xmax;
971 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972 }
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.orgcc3096b2009-04-22 22:09:04 +0000979 my->fAvgCharWidth = pts[5].fY;
980 my->fXMin = xmin;
981 my->fXMax = xmax;
982 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 }
984}
985
986////////////////////////////////////////////////////////////////////////
987////////////////////////////////////////////////////////////////////////
988
989SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +0000990 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
991 if (!c->success()) {
992 SkDELETE(c);
993 c = NULL;
994 }
995 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000996}
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*/
1003SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) {
1004 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001005 if (FT_Init_FreeType(&library)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 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}