blob: 9af21bc9812761e825875489de1295f0c9f01f49 [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
agl@chromium.org61a678a2010-08-06 18:08:18 +0000239 int face_index;
240 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
241 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
242 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
244 if (err) { // bad filename, try the default font
245 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
246 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 return 0;
248 } else {
249 SkASSERT(rec->fFace);
250 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
251 rec->fNext = gFaceRecHead;
252 gFaceRecHead = rec;
253 rec->fRefCnt = 1;
254 return rec;
255 }
256}
257
258static void unref_ft_face(FT_Face face) {
259 SkFaceRec* rec = gFaceRecHead;
260 SkFaceRec* prev = NULL;
261 while (rec) {
262 SkFaceRec* next = rec->fNext;
263 if (rec->fFace == face) {
264 if (--rec->fRefCnt == 0) {
265 if (prev) {
266 prev->fNext = next;
267 } else {
268 gFaceRecHead = next;
269 }
270 FT_Done_Face(face);
271 SkDELETE(rec);
272 }
273 return;
274 }
275 prev = rec;
276 rec = next;
277 }
278 SkASSERT("shouldn't get here, face not in list");
279}
280
281///////////////////////////////////////////////////////////////////////////
282
reed@android.com36a4c2a2009-07-22 19:52:11 +0000283void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000284 if (!gLCDSupportValid) {
285 InitFreetype();
286 FT_Done_FreeType(gFTLibrary);
287 }
288
289 if (!gLCDSupport && rec->isLCD()) {
290 // If the runtime Freetype library doesn't support LCD mode, we disable
291 // it here.
292 rec->fMaskFormat = SkMask::kA8_Format;
293 }
294
reed@android.com36a4c2a2009-07-22 19:52:11 +0000295 SkPaint::Hinting h = rec->getHinting();
296 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
agl@chromium.orgb4234a22010-01-21 11:43:44 +0000297 // collapse full->normal hinting if we're not doing LCD
reed@android.come4d0bc02009-07-24 19:53:20 +0000298 h = SkPaint::kNormal_Hinting;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000299 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
300 SkPaint::kNo_Hinting != h) {
reed@android.come4d0bc02009-07-24 19:53:20 +0000301 // to do subpixel, we must have at most slight hinting
302 h = SkPaint::kSlight_Hinting;
reed@android.com36a4c2a2009-07-22 19:52:11 +0000303 }
reed@android.come4d0bc02009-07-24 19:53:20 +0000304 rec->setHinting(h);
reed@android.com36a4c2a2009-07-22 19:52:11 +0000305}
306
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000308 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 SkAutoMutexAcquire ac(gFTMutex);
310
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000312 if (!InitFreetype()) {
313 sk_throw();
314 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315 }
316 ++gFTCount;
317
318 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000319 fFTSize = NULL;
320 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000322 if (NULL == fFaceRec) {
323 return;
324 }
325 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326
327 // compute our factors from the record
328
329 SkMatrix m;
330
331 fRec.getSingleMatrix(&m);
332
333#ifdef DUMP_STRIKE_CREATION
334 SkString keyString;
335 SkFontHost::GetDescriptorKeyString(desc, &keyString);
336 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
337 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
338 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
339 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000340 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341#endif
342
343 // now compute our scale factors
344 SkScalar sx = m.getScaleX();
345 SkScalar sy = m.getScaleY();
346
347 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
348 // sort of give up on hinting
349 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
350 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
351 sx = sy = SkScalarAve(sx, sy);
352
353 SkScalar inv = SkScalarInvert(sx);
354
355 // flip the skew elements to go from our Y-down system to FreeType's
356 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
357 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
358 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
359 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
360 } else {
361 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
362 fMatrix22.xy = fMatrix22.yx = 0;
363 }
364
365 fScaleX = SkScalarToFixed(sx);
366 fScaleY = SkScalarToFixed(sy);
367
368 // compute the flags we send to Load_Glyph
369 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000370 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371
agl@chromium.org70a303f2010-05-10 14:15:50 +0000372 if (SkMask::kBW_Format == fRec.fMaskFormat) {
373 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
374 loadFlags = FT_LOAD_TARGET_MONO;
375 if (fRec.getHinting() == SkPaint::kNo_Hinting)
376 loadFlags = FT_LOAD_NO_HINTING;
377 } else {
378 switch (fRec.getHinting()) {
379 case SkPaint::kNo_Hinting:
380 loadFlags = FT_LOAD_NO_HINTING;
381 break;
382 case SkPaint::kSlight_Hinting:
383 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
384 break;
385 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000386 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
387 loadFlags = FT_LOAD_FORCE_AUTOHINT;
388 else
389 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000390 break;
391 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000392 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
393 loadFlags = FT_LOAD_FORCE_AUTOHINT;
394 break;
395 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000396 loadFlags = FT_LOAD_TARGET_NORMAL;
397 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat)
398 loadFlags = FT_LOAD_TARGET_LCD;
399 else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat)
400 loadFlags = FT_LOAD_TARGET_LCD_V;
401 break;
402 default:
403 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
404 break;
405 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 }
407
agl@chromium.org99e1b902010-01-05 01:19:44 +0000408 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000409 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000410
reed@android.come4d0bc02009-07-24 19:53:20 +0000411 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 }
413
414 // now create the FT_Size
415
416 {
417 FT_Error err;
418
419 err = FT_New_Size(fFace, &fFTSize);
420 if (err != 0) {
421 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
422 fFaceRec->fFontID, fScaleX, fScaleY, err));
423 fFace = NULL;
424 return;
425 }
426
427 err = FT_Activate_Size(fFTSize);
428 if (err != 0) {
429 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
430 fFaceRec->fFontID, fScaleX, fScaleY, err));
431 fFTSize = NULL;
432 }
433
434 err = FT_Set_Char_Size( fFace,
435 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
436 72, 72);
437 if (err != 0) {
438 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
439 fFaceRec->fFontID, fScaleX, fScaleY, err));
440 fFace = NULL;
441 return;
442 }
443
444 FT_Set_Transform( fFace, &fMatrix22, NULL);
445 }
446}
447
448SkScalerContext_FreeType::~SkScalerContext_FreeType() {
449 if (fFTSize != NULL) {
450 FT_Done_Size(fFTSize);
451 }
452
453 SkAutoMutexAcquire ac(gFTMutex);
454
455 if (fFace != NULL) {
456 unref_ft_face(fFace);
457 }
458 if (--gFTCount == 0) {
459// SkDEBUGF(("FT_Done_FreeType\n"));
460 FT_Done_FreeType(gFTLibrary);
461 SkDEBUGCODE(gFTLibrary = NULL;)
462 }
463}
464
465/* We call this before each use of the fFace, since we may be sharing
466 this face with other context (at different sizes).
467*/
468FT_Error SkScalerContext_FreeType::setupSize() {
469 /* In the off-chance that a font has been removed, we want to error out
470 right away, so call resolve just to be sure.
471
472 TODO: perhaps we can skip this, by walking the global font cache and
473 killing all of the contexts when we know that a given fontID is going
474 away...
475 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000476 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 return (FT_Error)-1;
478 }
479
480 FT_Error err = FT_Activate_Size(fFTSize);
481
482 if (err != 0) {
483 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
484 fFaceRec->fFontID, fScaleX, fScaleY, err));
485 fFTSize = NULL;
486 } else {
487 // seems we need to reset this every time (not sure why, but without it
488 // I get random italics from some other fFTSize)
489 FT_Set_Transform( fFace, &fMatrix22, NULL);
490 }
491 return err;
492}
493
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000494void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
495 FT_Pos strength;
496 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
497 / 24;
498 FT_Outline_Embolden(outline, strength);
499}
500
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501unsigned SkScalerContext_FreeType::generateGlyphCount() const {
502 return fFace->num_glyphs;
503}
504
505uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
506 return SkToU16(FT_Get_Char_Index( fFace, uni ));
507}
508
reed@android.com9d3a9852010-01-08 14:07:42 +0000509SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
510 // iterate through each cmap entry, looking for matching glyph indices
511 FT_UInt glyphIndex;
512 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
513
514 while (glyphIndex != 0) {
515 if (glyphIndex == glyph) {
516 return charCode;
517 }
518 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
519 }
520
521 return 0;
522}
523
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
525 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000526 case SkMask::kHorizontalLCD_Format:
527 case SkMask::kVerticalLCD_Format:
528 SkASSERT(!"An LCD format should never be passed here");
529 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530 case SkMask::kBW_Format:
531 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532 case SkMask::kA8_Format:
533 default:
534 return FT_PIXEL_MODE_GRAY;
535 }
536}
537
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
539#ifdef FT_ADVANCES_H
540 /* unhinted and light hinted text have linearly scaled advances
541 * which are very cheap to compute with some font formats...
542 */
543 {
544 SkAutoMutexAcquire ac(gFTMutex);
545
546 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000547 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 return;
549 }
550
551 FT_Error error;
552 FT_Fixed advance;
553
554 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
555 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
556 &advance );
557 if (0 == error) {
558 glyph->fRsbDelta = 0;
559 glyph->fLsbDelta = 0;
560 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
561 glyph->fAdvanceY = 0;
562 return;
563 }
564 }
565#endif /* FT_ADVANCES_H */
566 /* otherwise, we need to load/hint the glyph, which is slower */
567 this->generateMetrics(glyph);
568 return;
569}
570
571void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
572 SkAutoMutexAcquire ac(gFTMutex);
573
574 glyph->fRsbDelta = 0;
575 glyph->fLsbDelta = 0;
576
577 FT_Error err;
578
579 if (this->setupSize()) {
580 goto ERROR;
581 }
582
583 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
584 if (err != 0) {
585 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
586 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
587 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000588 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589 return;
590 }
591
592 switch ( fFace->glyph->format ) {
593 case FT_GLYPH_FORMAT_OUTLINE:
594 FT_BBox bbox;
595
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000596 if (fRec.fFlags & kEmbolden_Flag) {
597 emboldenOutline(&fFace->glyph->outline);
598 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
600
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000601 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 int dx = glyph->getSubXFixed() >> 10;
603 int dy = glyph->getSubYFixed() >> 10;
604 // negate dy since freetype-y-goes-up and skia-y-goes-down
605 bbox.xMin += dx;
606 bbox.yMin -= dy;
607 bbox.xMax += dx;
608 bbox.yMax -= dy;
609 }
610
611 bbox.xMin &= ~63;
612 bbox.yMin &= ~63;
613 bbox.xMax = (bbox.xMax + 63) & ~63;
614 bbox.yMax = (bbox.yMax + 63) & ~63;
615
616 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
617 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
618 glyph->fTop = -SkToS16(bbox.yMax >> 6);
619 glyph->fLeft = SkToS16(bbox.xMin >> 6);
620 break;
621
622 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000623 if (fRec.fFlags & kEmbolden_Flag) {
624 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
625 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
626 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
628 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
629 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
630 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
631 break;
632
633 default:
634 SkASSERT(!"unknown glyph format");
635 goto ERROR;
636 }
637
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000638 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
640 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
641 if (fRec.fFlags & kDevKernText_Flag) {
642 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
643 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
644 }
645 } else {
646 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
647 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
648 }
649
650#ifdef ENABLE_GLYPH_SPEW
651 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
652 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
653#endif
654}
655
reed@android.comf5493692009-07-22 19:21:01 +0000656#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000657namespace skia_freetype_support {
658// extern functions from SkFontHost_FreeType_Subpixel
659extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
660extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
661}
662
663using namespace skia_freetype_support;
664#endif
665
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
667 SkAutoMutexAcquire ac(gFTMutex);
668
669 FT_Error err;
670
671 if (this->setupSize()) {
672 goto ERROR;
673 }
674
675 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
676 if (err != 0) {
677 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
678 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
679 ERROR:
680 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
681 return;
682 }
683
agl@chromium.org309485b2009-07-21 17:41:32 +0000684 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
685 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
686
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 switch ( fFace->glyph->format ) {
688 case FT_GLYPH_FORMAT_OUTLINE: {
689 FT_Outline* outline = &fFace->glyph->outline;
690 FT_BBox bbox;
691 FT_Bitmap target;
692
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000693 if (fRec.fFlags & kEmbolden_Flag) {
694 emboldenOutline(outline);
695 }
696
reed@android.com8a1c16f2008-12-17 15:59:43 +0000697 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000698 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699 dx = glyph.getSubXFixed() >> 10;
700 dy = glyph.getSubYFixed() >> 10;
701 // negate dy since freetype-y-goes-up and skia-y-goes-down
702 dy = -dy;
703 }
704 FT_Outline_Get_CBox(outline, &bbox);
705 /*
706 what we really want to do for subpixel is
707 offset(dx, dy)
708 compute_bounds
709 offset(bbox & !63)
710 but that is two calls to offset, so we do the following, which
711 achieves the same thing with only one offset call.
712 */
713 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
714 dy - ((bbox.yMin + dy) & ~63));
715
reed@android.comf5493692009-07-22 19:21:01 +0000716#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000717 if (lcdRenderMode) {
718 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
719 // we have to call FT_Render_Glyph and memcpy the image out.
720 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
721 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
722 FT_Render_Glyph(fFace->glyph, mode);
723
724 if (isVertical)
725 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
726 else
727 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
728
729 break;
730 }
731#endif
732
reed@android.com8a1c16f2008-12-17 15:59:43 +0000733 target.width = glyph.fWidth;
734 target.rows = glyph.fHeight;
735 target.pitch = glyph.rowBytes();
736 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
737 target.pixel_mode = compute_pixel_mode(
738 (SkMask::Format)fRec.fMaskFormat);
739 target.num_grays = 256;
740
741 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
742 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
743 } break;
744
745 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +0000746 if (fRec.fFlags & kEmbolden_Flag) {
747 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
748 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
749 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
751 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
752 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
753 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
754
755 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
756 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000757
agl@chromium.org558434a2009-08-11 17:22:38 +0000758 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
759 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
760 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000761 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
762 unsigned dstRowBytes = glyph.rowBytes();
763 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
764 unsigned extraRowBytes = dstRowBytes - minRowBytes;
765
766 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
767 memcpy(dst, src, minRowBytes);
768 memset(dst + minRowBytes, 0, extraRowBytes);
769 src += srcRowBytes;
770 dst += dstRowBytes;
771 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000772 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000773 (glyph.fMaskFormat == SkMask::kA8_Format ||
774 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
775 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000776 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
777 uint8_t byte = 0;
778 int bits = 0;
779 const uint8_t* src_row = src;
780 uint8_t* dst_row = dst;
781
782 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
783 if (!bits) {
784 byte = *src_row++;
785 bits = 8;
786 }
787
788 *dst_row++ = byte & 0x80 ? 0xff : 0;
789 bits--;
790 byte <<= 1;
791 }
792
793 src += fFace->glyph->bitmap.pitch;
794 dst += glyph.rowBytes();
795 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000796 } else {
797 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000798 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000799
800 if (lcdRenderMode)
801 glyph.expandA8ToLCD();
802
reed@android.com8a1c16f2008-12-17 15:59:43 +0000803 } break;
804
805 default:
806 SkASSERT(!"unknown glyph format");
807 goto ERROR;
808 }
809}
810
811///////////////////////////////////////////////////////////////////////////////
812
813#define ft2sk(x) SkFixedToScalar((x) << 10)
814
reed@android.com6f252972009-01-14 16:46:16 +0000815#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +0000816 #define CONST_PARAM const
817#else // older freetype doesn't use const here
818 #define CONST_PARAM
819#endif
820
821static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
822 SkPath* path = (SkPath*)ctx;
823 path->close(); // to close the previous contour (if any)
824 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
825 return 0;
826}
827
828static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
829 SkPath* path = (SkPath*)ctx;
830 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
831 return 0;
832}
833
834static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
835 void* ctx) {
836 SkPath* path = (SkPath*)ctx;
837 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
838 return 0;
839}
840
841static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
842 CONST_PARAM FT_Vector* pt2, void* ctx) {
843 SkPath* path = (SkPath*)ctx;
844 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
845 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
846 return 0;
847}
848
849void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
850 SkPath* path) {
851 SkAutoMutexAcquire ac(gFTMutex);
852
853 SkASSERT(&glyph && path);
854
855 if (this->setupSize()) {
856 path->reset();
857 return;
858 }
859
860 uint32_t flags = fLoadGlyphFlags;
861 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
862 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
863
864 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
865
866 if (err != 0) {
867 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
868 glyph.getGlyphID(fBaseGlyphCount), flags, err));
869 path->reset();
870 return;
871 }
872
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000873 if (fRec.fFlags & kEmbolden_Flag) {
874 emboldenOutline(&fFace->glyph->outline);
875 }
876
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877 FT_Outline_Funcs funcs;
878
879 funcs.move_to = move_proc;
880 funcs.line_to = line_proc;
881 funcs.conic_to = quad_proc;
882 funcs.cubic_to = cubic_proc;
883 funcs.shift = 0;
884 funcs.delta = 0;
885
886 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
887
888 if (err != 0) {
889 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
890 glyph.getGlyphID(fBaseGlyphCount), flags, err));
891 path->reset();
892 return;
893 }
894
895 path->close();
896}
897
898void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
899 SkPaint::FontMetrics* my) {
900 if (NULL == mx && NULL == my) {
901 return;
902 }
903
904 SkAutoMutexAcquire ac(gFTMutex);
905
906 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000907 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +0000909 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910 }
911 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +0000912 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913 }
914 return;
915 }
916
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000917 FT_Face face = fFace;
918 int upem = face->units_per_EM;
919 if (upem <= 0) {
920 goto ERROR;
921 }
922
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000923 SkPoint pts[6];
924 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 SkFixed scaleY = fScaleY;
926 SkFixed mxy = fMatrix22.xy;
927 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000928 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
929 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000930
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000931 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000932 if (leading < 0) {
933 leading = 0;
934 }
935
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000936 // Try to get the OS/2 table from the font. This contains the specific
937 // average font width metrics which Windows uses.
938 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
939
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940 ys[0] = -face->bbox.yMax;
941 ys[1] = -face->ascender;
942 ys[2] = -face->descender;
943 ys[3] = -face->bbox.yMin;
944 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000945 ys[5] = os2 ? os2->xAvgCharWidth : 0;
946
947 SkScalar x_height;
948 if (os2 && os2->sxHeight) {
949 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
950 } else {
951 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
952 if (x_glyph) {
953 FT_BBox bbox;
954 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000955 if (fRec.fFlags & kEmbolden_Flag) {
956 emboldenOutline(&fFace->glyph->outline);
957 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000958 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
959 x_height = SkIntToScalar(bbox.yMax) / 64;
960 } else {
961 x_height = 0;
962 }
963 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964
965 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000966 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
968 SkFixed x = SkFixedMul(mxy, y);
969 y = SkFixedMul(myy, y);
970 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
971 }
972
973 if (mx) {
974 mx->fTop = pts[0].fX;
975 mx->fAscent = pts[1].fX;
976 mx->fDescent = pts[2].fX;
977 mx->fBottom = pts[3].fX;
978 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000979 mx->fAvgCharWidth = pts[5].fX;
980 mx->fXMin = xmin;
981 mx->fXMax = xmax;
982 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 }
984 if (my) {
985 my->fTop = pts[0].fY;
986 my->fAscent = pts[1].fY;
987 my->fDescent = pts[2].fY;
988 my->fBottom = pts[3].fY;
989 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000990 my->fAvgCharWidth = pts[5].fY;
991 my->fXMin = xmin;
992 my->fXMax = xmax;
993 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 }
995}
996
997////////////////////////////////////////////////////////////////////////
998////////////////////////////////////////////////////////////////////////
999
1000SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001001 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1002 if (!c->success()) {
1003 SkDELETE(c);
1004 c = NULL;
1005 }
1006 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001007}
1008
1009///////////////////////////////////////////////////////////////////////////////
1010
1011/* Export this so that other parts of our FonttHost port can make use of our
1012 ability to extract the name+style from a stream, using FreeType's api.
1013*/
1014SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) {
1015 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001016 if (FT_Init_FreeType(&library)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017 name->set(NULL);
1018 return SkTypeface::kNormal;
1019 }
1020
1021 FT_Open_Args args;
1022 memset(&args, 0, sizeof(args));
1023
1024 const void* memoryBase = stream->getMemoryBase();
1025 FT_StreamRec streamRec;
1026
1027 if (NULL != memoryBase) {
1028 args.flags = FT_OPEN_MEMORY;
1029 args.memory_base = (const FT_Byte*)memoryBase;
1030 args.memory_size = stream->getLength();
1031 } else {
1032 memset(&streamRec, 0, sizeof(streamRec));
1033 streamRec.size = stream->read(NULL, 0);
1034 streamRec.descriptor.pointer = stream;
1035 streamRec.read = sk_stream_read;
1036 streamRec.close = sk_stream_close;
1037
1038 args.flags = FT_OPEN_STREAM;
1039 args.stream = &streamRec;
1040 }
1041
1042 FT_Face face;
1043 if (FT_Open_Face(library, &args, 0, &face)) {
1044 FT_Done_FreeType(library);
1045 name->set(NULL);
1046 return SkTypeface::kNormal;
1047 }
1048
1049 name->set(face->family_name);
1050 int style = SkTypeface::kNormal;
1051
1052 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1053 style |= SkTypeface::kBold;
1054 }
1055 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1056 style |= SkTypeface::kItalic;
1057 }
1058
1059 FT_Done_Face(face);
1060 FT_Done_FreeType(library);
1061 return (SkTypeface::Style)style;
1062}