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