blob: b7645ddce6bb399edfa33005ee3e203b2a84e8c0 [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;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000296 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
297 SkPaint::kNo_Hinting != h) {
reed@android.come4d0bc02009-07-24 19:53:20 +0000298 // to do subpixel, we must have at most slight hinting
299 h = SkPaint::kSlight_Hinting;
reed@android.com36a4c2a2009-07-22 19:52:11 +0000300 }
reed@android.come4d0bc02009-07-24 19:53:20 +0000301 rec->setHinting(h);
reed@android.com36a4c2a2009-07-22 19:52:11 +0000302}
303
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000305 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 SkAutoMutexAcquire ac(gFTMutex);
307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000309 if (!InitFreetype()) {
310 sk_throw();
311 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 }
313 ++gFTCount;
314
315 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000316 fFTSize = NULL;
317 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000319 if (NULL == fFaceRec) {
320 return;
321 }
322 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
324 // compute our factors from the record
325
326 SkMatrix m;
327
328 fRec.getSingleMatrix(&m);
329
330#ifdef DUMP_STRIKE_CREATION
331 SkString keyString;
332 SkFontHost::GetDescriptorKeyString(desc, &keyString);
333 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
334 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
335 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
336 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000337 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338#endif
339
340 // now compute our scale factors
341 SkScalar sx = m.getScaleX();
342 SkScalar sy = m.getScaleY();
343
344 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
345 // sort of give up on hinting
346 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
347 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
348 sx = sy = SkScalarAve(sx, sy);
349
350 SkScalar inv = SkScalarInvert(sx);
351
352 // flip the skew elements to go from our Y-down system to FreeType's
353 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
354 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
355 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
356 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
357 } else {
358 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
359 fMatrix22.xy = fMatrix22.yx = 0;
360 }
361
362 fScaleX = SkScalarToFixed(sx);
363 fScaleY = SkScalarToFixed(sy);
364
365 // compute the flags we send to Load_Glyph
366 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000367 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368
agl@chromium.org70a303f2010-05-10 14:15:50 +0000369 if (SkMask::kBW_Format == fRec.fMaskFormat) {
370 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
371 loadFlags = FT_LOAD_TARGET_MONO;
372 if (fRec.getHinting() == SkPaint::kNo_Hinting)
373 loadFlags = FT_LOAD_NO_HINTING;
374 } else {
375 switch (fRec.getHinting()) {
376 case SkPaint::kNo_Hinting:
377 loadFlags = FT_LOAD_NO_HINTING;
378 break;
379 case SkPaint::kSlight_Hinting:
380 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
381 break;
382 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000383 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
384 loadFlags = FT_LOAD_FORCE_AUTOHINT;
385 else
386 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000387 break;
388 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000389 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
390 loadFlags = FT_LOAD_FORCE_AUTOHINT;
391 break;
392 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000393 loadFlags = FT_LOAD_TARGET_NORMAL;
394 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat)
395 loadFlags = FT_LOAD_TARGET_LCD;
396 else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat)
397 loadFlags = FT_LOAD_TARGET_LCD_V;
398 break;
399 default:
400 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
401 break;
402 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403 }
404
agl@chromium.org99e1b902010-01-05 01:19:44 +0000405 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000406 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000407
reed@android.come4d0bc02009-07-24 19:53:20 +0000408 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 }
410
411 // now create the FT_Size
412
413 {
414 FT_Error err;
415
416 err = FT_New_Size(fFace, &fFTSize);
417 if (err != 0) {
418 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
419 fFaceRec->fFontID, fScaleX, fScaleY, err));
420 fFace = NULL;
421 return;
422 }
423
424 err = FT_Activate_Size(fFTSize);
425 if (err != 0) {
426 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
427 fFaceRec->fFontID, fScaleX, fScaleY, err));
428 fFTSize = NULL;
429 }
430
431 err = FT_Set_Char_Size( fFace,
432 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
433 72, 72);
434 if (err != 0) {
435 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
436 fFaceRec->fFontID, fScaleX, fScaleY, err));
437 fFace = NULL;
438 return;
439 }
440
441 FT_Set_Transform( fFace, &fMatrix22, NULL);
442 }
443}
444
445SkScalerContext_FreeType::~SkScalerContext_FreeType() {
446 if (fFTSize != NULL) {
447 FT_Done_Size(fFTSize);
448 }
449
450 SkAutoMutexAcquire ac(gFTMutex);
451
452 if (fFace != NULL) {
453 unref_ft_face(fFace);
454 }
455 if (--gFTCount == 0) {
456// SkDEBUGF(("FT_Done_FreeType\n"));
457 FT_Done_FreeType(gFTLibrary);
458 SkDEBUGCODE(gFTLibrary = NULL;)
459 }
460}
461
462/* We call this before each use of the fFace, since we may be sharing
463 this face with other context (at different sizes).
464*/
465FT_Error SkScalerContext_FreeType::setupSize() {
466 /* In the off-chance that a font has been removed, we want to error out
467 right away, so call resolve just to be sure.
468
469 TODO: perhaps we can skip this, by walking the global font cache and
470 killing all of the contexts when we know that a given fontID is going
471 away...
472 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000473 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 return (FT_Error)-1;
475 }
476
477 FT_Error err = FT_Activate_Size(fFTSize);
478
479 if (err != 0) {
480 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
481 fFaceRec->fFontID, fScaleX, fScaleY, err));
482 fFTSize = NULL;
483 } else {
484 // seems we need to reset this every time (not sure why, but without it
485 // I get random italics from some other fFTSize)
486 FT_Set_Transform( fFace, &fMatrix22, NULL);
487 }
488 return err;
489}
490
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000491void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
492 FT_Pos strength;
493 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
494 / 24;
495 FT_Outline_Embolden(outline, strength);
496}
497
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498unsigned SkScalerContext_FreeType::generateGlyphCount() const {
499 return fFace->num_glyphs;
500}
501
502uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
503 return SkToU16(FT_Get_Char_Index( fFace, uni ));
504}
505
reed@android.com9d3a9852010-01-08 14:07:42 +0000506SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
507 // iterate through each cmap entry, looking for matching glyph indices
508 FT_UInt glyphIndex;
509 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
510
511 while (glyphIndex != 0) {
512 if (glyphIndex == glyph) {
513 return charCode;
514 }
515 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
516 }
517
518 return 0;
519}
520
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
522 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000523 case SkMask::kHorizontalLCD_Format:
524 case SkMask::kVerticalLCD_Format:
525 SkASSERT(!"An LCD format should never be passed here");
526 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 case SkMask::kBW_Format:
528 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 case SkMask::kA8_Format:
530 default:
531 return FT_PIXEL_MODE_GRAY;
532 }
533}
534
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
536#ifdef FT_ADVANCES_H
537 /* unhinted and light hinted text have linearly scaled advances
538 * which are very cheap to compute with some font formats...
539 */
540 {
541 SkAutoMutexAcquire ac(gFTMutex);
542
543 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000544 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545 return;
546 }
547
548 FT_Error error;
549 FT_Fixed advance;
550
551 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
552 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
553 &advance );
554 if (0 == error) {
555 glyph->fRsbDelta = 0;
556 glyph->fLsbDelta = 0;
557 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
558 glyph->fAdvanceY = 0;
559 return;
560 }
561 }
562#endif /* FT_ADVANCES_H */
563 /* otherwise, we need to load/hint the glyph, which is slower */
564 this->generateMetrics(glyph);
565 return;
566}
567
568void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
569 SkAutoMutexAcquire ac(gFTMutex);
570
571 glyph->fRsbDelta = 0;
572 glyph->fLsbDelta = 0;
573
574 FT_Error err;
575
576 if (this->setupSize()) {
577 goto ERROR;
578 }
579
580 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
581 if (err != 0) {
582 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
583 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
584 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000585 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000586 return;
587 }
588
589 switch ( fFace->glyph->format ) {
590 case FT_GLYPH_FORMAT_OUTLINE:
591 FT_BBox bbox;
592
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000593 if (fRec.fFlags & kEmbolden_Flag) {
594 emboldenOutline(&fFace->glyph->outline);
595 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
597
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000598 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 int dx = glyph->getSubXFixed() >> 10;
600 int dy = glyph->getSubYFixed() >> 10;
601 // negate dy since freetype-y-goes-up and skia-y-goes-down
602 bbox.xMin += dx;
603 bbox.yMin -= dy;
604 bbox.xMax += dx;
605 bbox.yMax -= dy;
606 }
607
608 bbox.xMin &= ~63;
609 bbox.yMin &= ~63;
610 bbox.xMax = (bbox.xMax + 63) & ~63;
611 bbox.yMax = (bbox.yMax + 63) & ~63;
612
613 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
614 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
615 glyph->fTop = -SkToS16(bbox.yMax >> 6);
616 glyph->fLeft = SkToS16(bbox.xMin >> 6);
617 break;
618
619 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000620 if (fRec.fFlags & kEmbolden_Flag) {
621 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
622 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
623 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
625 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
626 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
627 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
628 break;
629
630 default:
631 SkASSERT(!"unknown glyph format");
632 goto ERROR;
633 }
634
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000635 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000636 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
637 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
638 if (fRec.fFlags & kDevKernText_Flag) {
639 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
640 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
641 }
642 } else {
643 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
644 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
645 }
646
647#ifdef ENABLE_GLYPH_SPEW
648 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
649 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
650#endif
651}
652
reed@android.comf5493692009-07-22 19:21:01 +0000653#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000654namespace skia_freetype_support {
655// extern functions from SkFontHost_FreeType_Subpixel
656extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
657extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
658}
659
660using namespace skia_freetype_support;
661#endif
662
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
664 SkAutoMutexAcquire ac(gFTMutex);
665
666 FT_Error err;
667
668 if (this->setupSize()) {
669 goto ERROR;
670 }
671
672 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
673 if (err != 0) {
674 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
675 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
676 ERROR:
677 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
678 return;
679 }
680
agl@chromium.org309485b2009-07-21 17:41:32 +0000681 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
682 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
683
reed@android.com8a1c16f2008-12-17 15:59:43 +0000684 switch ( fFace->glyph->format ) {
685 case FT_GLYPH_FORMAT_OUTLINE: {
686 FT_Outline* outline = &fFace->glyph->outline;
687 FT_BBox bbox;
688 FT_Bitmap target;
689
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000690 if (fRec.fFlags & kEmbolden_Flag) {
691 emboldenOutline(outline);
692 }
693
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000695 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000696 dx = glyph.getSubXFixed() >> 10;
697 dy = glyph.getSubYFixed() >> 10;
698 // negate dy since freetype-y-goes-up and skia-y-goes-down
699 dy = -dy;
700 }
701 FT_Outline_Get_CBox(outline, &bbox);
702 /*
703 what we really want to do for subpixel is
704 offset(dx, dy)
705 compute_bounds
706 offset(bbox & !63)
707 but that is two calls to offset, so we do the following, which
708 achieves the same thing with only one offset call.
709 */
710 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
711 dy - ((bbox.yMin + dy) & ~63));
712
reed@android.comf5493692009-07-22 19:21:01 +0000713#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000714 if (lcdRenderMode) {
715 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
716 // we have to call FT_Render_Glyph and memcpy the image out.
717 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
718 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
719 FT_Render_Glyph(fFace->glyph, mode);
720
721 if (isVertical)
722 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
723 else
724 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
725
726 break;
727 }
728#endif
729
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730 target.width = glyph.fWidth;
731 target.rows = glyph.fHeight;
732 target.pitch = glyph.rowBytes();
733 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
734 target.pixel_mode = compute_pixel_mode(
735 (SkMask::Format)fRec.fMaskFormat);
736 target.num_grays = 256;
737
738 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
739 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
740 } break;
741
742 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +0000743 if (fRec.fFlags & kEmbolden_Flag) {
744 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
745 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
746 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
748 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
749 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
750 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
751
752 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
753 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754
agl@chromium.org558434a2009-08-11 17:22:38 +0000755 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
756 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
757 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000758 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
759 unsigned dstRowBytes = glyph.rowBytes();
760 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
761 unsigned extraRowBytes = dstRowBytes - minRowBytes;
762
763 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
764 memcpy(dst, src, minRowBytes);
765 memset(dst + minRowBytes, 0, extraRowBytes);
766 src += srcRowBytes;
767 dst += dstRowBytes;
768 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000769 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000770 (glyph.fMaskFormat == SkMask::kA8_Format ||
771 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
772 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000773 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
774 uint8_t byte = 0;
775 int bits = 0;
776 const uint8_t* src_row = src;
777 uint8_t* dst_row = dst;
778
779 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
780 if (!bits) {
781 byte = *src_row++;
782 bits = 8;
783 }
784
785 *dst_row++ = byte & 0x80 ? 0xff : 0;
786 bits--;
787 byte <<= 1;
788 }
789
790 src += fFace->glyph->bitmap.pitch;
791 dst += glyph.rowBytes();
792 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000793 } else {
794 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000796
797 if (lcdRenderMode)
798 glyph.expandA8ToLCD();
799
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 } break;
801
802 default:
803 SkASSERT(!"unknown glyph format");
804 goto ERROR;
805 }
806}
807
808///////////////////////////////////////////////////////////////////////////////
809
810#define ft2sk(x) SkFixedToScalar((x) << 10)
811
reed@android.com6f252972009-01-14 16:46:16 +0000812#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813 #define CONST_PARAM const
814#else // older freetype doesn't use const here
815 #define CONST_PARAM
816#endif
817
818static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
819 SkPath* path = (SkPath*)ctx;
820 path->close(); // to close the previous contour (if any)
821 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
822 return 0;
823}
824
825static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
826 SkPath* path = (SkPath*)ctx;
827 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
828 return 0;
829}
830
831static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
832 void* ctx) {
833 SkPath* path = (SkPath*)ctx;
834 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
835 return 0;
836}
837
838static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
839 CONST_PARAM FT_Vector* pt2, void* ctx) {
840 SkPath* path = (SkPath*)ctx;
841 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
842 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
843 return 0;
844}
845
846void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
847 SkPath* path) {
848 SkAutoMutexAcquire ac(gFTMutex);
849
850 SkASSERT(&glyph && path);
851
852 if (this->setupSize()) {
853 path->reset();
854 return;
855 }
856
857 uint32_t flags = fLoadGlyphFlags;
858 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
859 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
860
861 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
862
863 if (err != 0) {
864 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
865 glyph.getGlyphID(fBaseGlyphCount), flags, err));
866 path->reset();
867 return;
868 }
869
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000870 if (fRec.fFlags & kEmbolden_Flag) {
871 emboldenOutline(&fFace->glyph->outline);
872 }
873
reed@android.com8a1c16f2008-12-17 15:59:43 +0000874 FT_Outline_Funcs funcs;
875
876 funcs.move_to = move_proc;
877 funcs.line_to = line_proc;
878 funcs.conic_to = quad_proc;
879 funcs.cubic_to = cubic_proc;
880 funcs.shift = 0;
881 funcs.delta = 0;
882
883 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
884
885 if (err != 0) {
886 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
887 glyph.getGlyphID(fBaseGlyphCount), flags, err));
888 path->reset();
889 return;
890 }
891
892 path->close();
893}
894
895void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
896 SkPaint::FontMetrics* my) {
897 if (NULL == mx && NULL == my) {
898 return;
899 }
900
901 SkAutoMutexAcquire ac(gFTMutex);
902
903 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000904 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +0000906 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 }
908 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +0000909 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910 }
911 return;
912 }
913
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000914 FT_Face face = fFace;
915 int upem = face->units_per_EM;
916 if (upem <= 0) {
917 goto ERROR;
918 }
919
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000920 SkPoint pts[6];
921 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 SkFixed scaleY = fScaleY;
923 SkFixed mxy = fMatrix22.xy;
924 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000925 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
926 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000928 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929 if (leading < 0) {
930 leading = 0;
931 }
932
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000933 // Try to get the OS/2 table from the font. This contains the specific
934 // average font width metrics which Windows uses.
935 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
936
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 ys[0] = -face->bbox.yMax;
938 ys[1] = -face->ascender;
939 ys[2] = -face->descender;
940 ys[3] = -face->bbox.yMin;
941 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000942 ys[5] = os2 ? os2->xAvgCharWidth : 0;
943
944 SkScalar x_height;
945 if (os2 && os2->sxHeight) {
946 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
947 } else {
948 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
949 if (x_glyph) {
950 FT_BBox bbox;
951 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000952 if (fRec.fFlags & kEmbolden_Flag) {
953 emboldenOutline(&fFace->glyph->outline);
954 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000955 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
956 x_height = SkIntToScalar(bbox.yMax) / 64;
957 } else {
958 x_height = 0;
959 }
960 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961
962 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000963 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
965 SkFixed x = SkFixedMul(mxy, y);
966 y = SkFixedMul(myy, y);
967 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
968 }
969
970 if (mx) {
971 mx->fTop = pts[0].fX;
972 mx->fAscent = pts[1].fX;
973 mx->fDescent = pts[2].fX;
974 mx->fBottom = pts[3].fX;
975 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000976 mx->fAvgCharWidth = pts[5].fX;
977 mx->fXMin = xmin;
978 mx->fXMax = xmax;
979 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000980 }
981 if (my) {
982 my->fTop = pts[0].fY;
983 my->fAscent = pts[1].fY;
984 my->fDescent = pts[2].fY;
985 my->fBottom = pts[3].fY;
986 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000987 my->fAvgCharWidth = pts[5].fY;
988 my->fXMin = xmin;
989 my->fXMax = xmax;
990 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 }
992}
993
994////////////////////////////////////////////////////////////////////////
995////////////////////////////////////////////////////////////////////////
996
997SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +0000998 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
999 if (!c->success()) {
1000 SkDELETE(c);
1001 c = NULL;
1002 }
1003 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001004}
1005
1006///////////////////////////////////////////////////////////////////////////////
1007
1008/* Export this so that other parts of our FonttHost port can make use of our
1009 ability to extract the name+style from a stream, using FreeType's api.
1010*/
1011SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) {
1012 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001013 if (FT_Init_FreeType(&library)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001014 name->set(NULL);
1015 return SkTypeface::kNormal;
1016 }
1017
1018 FT_Open_Args args;
1019 memset(&args, 0, sizeof(args));
1020
1021 const void* memoryBase = stream->getMemoryBase();
1022 FT_StreamRec streamRec;
1023
1024 if (NULL != memoryBase) {
1025 args.flags = FT_OPEN_MEMORY;
1026 args.memory_base = (const FT_Byte*)memoryBase;
1027 args.memory_size = stream->getLength();
1028 } else {
1029 memset(&streamRec, 0, sizeof(streamRec));
1030 streamRec.size = stream->read(NULL, 0);
1031 streamRec.descriptor.pointer = stream;
1032 streamRec.read = sk_stream_read;
1033 streamRec.close = sk_stream_close;
1034
1035 args.flags = FT_OPEN_STREAM;
1036 args.stream = &streamRec;
1037 }
1038
1039 FT_Face face;
1040 if (FT_Open_Face(library, &args, 0, &face)) {
1041 FT_Done_FreeType(library);
1042 name->set(NULL);
1043 return SkTypeface::kNormal;
1044 }
1045
1046 name->set(face->family_name);
1047 int style = SkTypeface::kNormal;
1048
1049 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1050 style |= SkTypeface::kBold;
1051 }
1052 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1053 style |= SkTypeface::kItalic;
1054 }
1055
1056 FT_Done_Face(face);
1057 FT_Done_FreeType(library);
1058 return (SkTypeface::Style)style;
1059}