blob: ec2c6fe2f5742c3ba47084546cadcb1cacbb7b91 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkBitmap.h"
19#include "SkCanvas.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkDescriptor.h"
22#include "SkFDot6.h"
23#include "SkFontHost.h"
24#include "SkMask.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000025#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000026#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkStream.h"
28#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000030#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
32#include <ft2build.h>
33#include FT_FREETYPE_H
34#include FT_OUTLINE_H
35#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000036#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000037#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000038#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000039// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
40#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000041#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000042#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000043#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000044#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000045
reed@android.com8a1c16f2008-12-17 15:59:43 +000046#ifdef FT_ADVANCES_H
47#include FT_ADVANCES_H
48#endif
49
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000050#if 0
51// Also include the files by name for build tools which require this.
52#include <freetype/freetype.h>
53#include <freetype/ftoutln.h>
54#include <freetype/ftsizes.h>
55#include <freetype/tttables.h>
56#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000057#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000058#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000059#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000060#endif
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062//#define ENABLE_GLYPH_SPEW // for tracing calls
63//#define DUMP_STRIKE_CREATION
64
65#ifdef SK_DEBUG
66 #define SkASSERT_CONTINUE(pred) \
67 do { \
68 if (!(pred)) \
69 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
70 } while (false)
71#else
72 #define SkASSERT_CONTINUE(pred)
73#endif
74
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000075using namespace skia_advanced_typeface_metrics_utils;
76
reed@google.combde3c8e2011-05-18 11:58:10 +000077// SK_FREETYPE_LCD_LERP should be 0...256
78// 0 means no color reduction (e.g. just as returned from FreeType)
79// 256 means 100% color reduction (e.g. gray)
reed@google.comc5181342011-05-17 20:52:46 +000080//
81#ifndef SK_FREETYPE_LCD_LERP
reed@google.combde3c8e2011-05-18 11:58:10 +000082 #define SK_FREETYPE_LCD_LERP 96
reed@google.comc5181342011-05-17 20:52:46 +000083#endif
84
reed@google.comeffc5012011-06-27 16:44:46 +000085static bool isLCD(const SkScalerContext::Rec& rec) {
86 switch (rec.fMaskFormat) {
87 case SkMask::kLCD16_Format:
88 case SkMask::kLCD32_Format:
89 return true;
90 default:
91 return false;
92 }
93}
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095//////////////////////////////////////////////////////////////////////////
96
97struct SkFaceRec;
98
99static SkMutex gFTMutex;
100static int gFTCount;
101static FT_Library gFTLibrary;
102static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000103static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
104static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
106/////////////////////////////////////////////////////////////////////////
107
agl@chromium.orge76073b2010-06-04 20:31:17 +0000108// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
109// This value was chosen by eyeballing the result in Firefox and trying to match it.
110static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
111
agl@chromium.org309485b2009-07-21 17:41:32 +0000112static bool
113InitFreetype() {
114 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000115 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000117 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000118
agl@chromium.org309485b2009-07-21 17:41:32 +0000119 // Setup LCD filtering. This reduces colour fringes for LCD rendered
120 // glyphs.
epoger@google.com5070d792011-06-29 20:43:14 +0000121#ifdef FT_LCD_FILTER_DEFAULT
agl@chromium.org309485b2009-07-21 17:41:32 +0000122 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000123 gLCDSupport = err == 0;
epoger@google.com5070d792011-06-29 20:43:14 +0000124#else
125 gLCDSupport = false;
126#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000127 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000128
129 return true;
130}
131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132class SkScalerContext_FreeType : public SkScalerContext {
133public:
134 SkScalerContext_FreeType(const SkDescriptor* desc);
135 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000136
reed@android.com62900b42009-02-11 15:07:19 +0000137 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000138 return fFaceRec != NULL &&
139 fFTSize != NULL &&
140 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000141 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
143protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000144 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 virtual uint16_t generateCharToGlyph(SkUnichar uni);
146 virtual void generateAdvance(SkGlyph* glyph);
147 virtual void generateMetrics(SkGlyph* glyph);
148 virtual void generateImage(const SkGlyph& glyph);
149 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
150 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
151 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000152 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
154private:
155 SkFaceRec* fFaceRec;
156 FT_Face fFace; // reference to shared face in gFaceRecHead
157 FT_Size fFTSize; // our own copy
158 SkFixed fScaleX, fScaleY;
159 FT_Matrix fMatrix22;
160 uint32_t fLoadGlyphFlags;
161
162 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000163 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164};
165
166///////////////////////////////////////////////////////////////////////////
167///////////////////////////////////////////////////////////////////////////
168
169#include "SkStream.h"
170
171struct SkFaceRec {
172 SkFaceRec* fNext;
173 FT_Face fFace;
174 FT_StreamRec fFTStream;
175 SkStream* fSkStream;
176 uint32_t fRefCnt;
177 uint32_t fFontID;
178
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000179 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 SkFaceRec(SkStream* strm, uint32_t fontID);
181 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000182 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 }
184};
185
186extern "C" {
187 static unsigned long sk_stream_read(FT_Stream stream,
188 unsigned long offset,
189 unsigned char* buffer,
190 unsigned long count ) {
191 SkStream* str = (SkStream*)stream->descriptor.pointer;
192
193 if (count) {
194 if (!str->rewind()) {
195 return 0;
196 } else {
197 unsigned long ret;
198 if (offset) {
199 ret = str->read(NULL, offset);
200 if (ret != offset) {
201 return 0;
202 }
203 }
204 ret = str->read(buffer, count);
205 if (ret != count) {
206 return 0;
207 }
208 count = ret;
209 }
210 }
211 return count;
212 }
213
214 static void sk_stream_close( FT_Stream stream) {}
215}
216
217SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
218 : fSkStream(strm), fFontID(fontID) {
219// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
220
reed@android.com4516f472009-06-29 16:25:36 +0000221 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 fFTStream.size = fSkStream->getLength();
223 fFTStream.descriptor.pointer = fSkStream;
224 fFTStream.read = sk_stream_read;
225 fFTStream.close = sk_stream_close;
226}
227
reed@android.com62900b42009-02-11 15:07:19 +0000228// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229static SkFaceRec* ref_ft_face(uint32_t fontID) {
230 SkFaceRec* rec = gFaceRecHead;
231 while (rec) {
232 if (rec->fFontID == fontID) {
233 SkASSERT(rec->fFace);
234 rec->fRefCnt += 1;
235 return rec;
236 }
237 rec = rec->fNext;
238 }
239
240 SkStream* strm = SkFontHost::OpenStream(fontID);
241 if (NULL == strm) {
242 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 return 0;
244 }
245
246 // this passes ownership of strm to the rec
247 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
248
249 FT_Open_Args args;
250 memset(&args, 0, sizeof(args));
251 const void* memoryBase = strm->getMemoryBase();
252
253 if (NULL != memoryBase) {
254//printf("mmap(%s)\n", keyString.c_str());
255 args.flags = FT_OPEN_MEMORY;
256 args.memory_base = (const FT_Byte*)memoryBase;
257 args.memory_size = strm->getLength();
258 } else {
259//printf("fopen(%s)\n", keyString.c_str());
260 args.flags = FT_OPEN_STREAM;
261 args.stream = &rec->fFTStream;
262 }
263
agl@chromium.org61a678a2010-08-06 18:08:18 +0000264 int face_index;
265 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
266 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
267 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268
269 if (err) { // bad filename, try the default font
270 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
271 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 return 0;
273 } else {
274 SkASSERT(rec->fFace);
275 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
276 rec->fNext = gFaceRecHead;
277 gFaceRecHead = rec;
278 rec->fRefCnt = 1;
279 return rec;
280 }
281}
282
283static void unref_ft_face(FT_Face face) {
284 SkFaceRec* rec = gFaceRecHead;
285 SkFaceRec* prev = NULL;
286 while (rec) {
287 SkFaceRec* next = rec->fNext;
288 if (rec->fFace == face) {
289 if (--rec->fRefCnt == 0) {
290 if (prev) {
291 prev->fNext = next;
292 } else {
293 gFaceRecHead = next;
294 }
295 FT_Done_Face(face);
296 SkDELETE(rec);
297 }
298 return;
299 }
300 prev = rec;
301 rec = next;
302 }
303 SkASSERT("shouldn't get here, face not in list");
304}
305
306///////////////////////////////////////////////////////////////////////////
307
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000308// Work around for old versions of freetype.
309static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
310 FT_Int32 loadFlags, FT_Fixed* advances) {
311#ifdef FT_ADVANCES_H
312 return FT_Get_Advances(face, start, count, loadFlags, advances);
313#else
314 if (!face || start >= face->num_glyphs ||
315 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
316 return 6; // "Invalid argument."
317 }
318 if (count == 0)
319 return 0;
320
321 for (int i = 0; i < count; i++) {
322 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
323 if (err)
324 return err;
325 advances[i] = face->glyph->advance.x;
326 }
327
328 return 0;
329#endif
330}
331
332static bool canEmbed(FT_Face face) {
333#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
334 FT_UShort fsType = FT_Get_FSType_Flags(face);
335 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
336 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
337#else
338 // No embedding is 0x2 and bitmap embedding only is 0x200.
339 TT_OS2* os2_table;
340 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
341 return (os2_table->fsType & 0x202) == 0;
342 }
343 return false; // We tried, fail safe.
344#endif
345}
346
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000347static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
348 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
349 if (!glyph_id)
350 return false;
351 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
352 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
353 return true;
354}
355
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000356static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000357 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000358 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
359 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000360 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000361 SkASSERT(data);
362 *data = advance;
363 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000364}
365
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000366static void populate_glyph_to_unicode(FT_Face& face,
367 SkTDArray<SkUnichar>* glyphToUnicode) {
368 // Check and see if we have Unicode cmaps.
369 for (int i = 0; i < face->num_charmaps; ++i) {
370 // CMaps known to support Unicode:
371 // Platform ID Encoding ID Name
372 // ----------- ----------- -----------------------------------
373 // 0 0,1 Apple Unicode
374 // 0 3 Apple Unicode 2.0 (preferred)
375 // 3 1 Microsoft Unicode UCS-2
376 // 3 10 Microsoft Unicode UCS-4 (preferred)
377 //
378 // See Apple TrueType Reference Manual
379 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
380 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
381 // Microsoft OpenType Specification
382 // http://www.microsoft.com/typography/otspec/cmap.htm
383
384 FT_UShort platformId = face->charmaps[i]->platform_id;
385 FT_UShort encodingId = face->charmaps[i]->encoding_id;
386
387 if (platformId != 0 && platformId != 3) {
388 continue;
389 }
390 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
391 continue;
392 }
393 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
394 (platformId == 0 && encodingId == 3));
395
396 FT_Set_Charmap(face, face->charmaps[i]);
397 if (glyphToUnicode->isEmpty()) {
398 glyphToUnicode->setCount(face->num_glyphs);
399 memset(glyphToUnicode->begin(), 0,
400 sizeof(SkUnichar) * face->num_glyphs);
401 }
402
403 // Iterate through each cmap entry.
404 FT_UInt glyphIndex;
405 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
406 glyphIndex != 0;
407 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
408 if (charCode &&
409 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
410 (*glyphToUnicode)[glyphIndex] = charCode;
411 }
412 }
413 }
414}
415
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000416// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000417SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000418 uint32_t fontID,
419 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000420#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000421 return NULL;
422#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000423 SkAutoMutexAcquire ac(gFTMutex);
424 FT_Library libInit = NULL;
425 if (gFTCount == 0) {
426 if (!InitFreetype())
427 sk_throw();
428 libInit = gFTLibrary;
429 }
430 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
431 SkFaceRec* rec = ref_ft_face(fontID);
432 if (NULL == rec)
433 return NULL;
434 FT_Face face = rec->fFace;
435
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000436 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
437 info->fFontName.set(FT_Get_Postscript_Name(face));
438 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
439 info->fLastGlyphID = face->num_glyphs - 1;
440 info->fEmSize = 1000;
441
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000442 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000444 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000445 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000446 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000447 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448 cid = true;
449 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000450 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000451 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000452 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000453 cid = true;
454 TT_Header* ttHeader;
455 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
456 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000457 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 }
459 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461 info->fStyle = 0;
462 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000463 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000464 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000466 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
467 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000468 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
469
470 PS_FontInfoRec ps_info;
471 TT_Postscript* tt_info;
472 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
473 info->fItalicAngle = ps_info.italic_angle;
474 } else if ((tt_info =
475 (TT_Postscript*)FT_Get_Sfnt_Table(face,
476 ft_sfnt_post)) != NULL) {
477 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
478 } else {
479 info->fItalicAngle = 0;
480 }
481
482 info->fAscent = face->ascender;
483 info->fDescent = face->descender;
484
485 // Figure out a good guess for StemV - Min width of i, I, !, 1.
486 // This probably isn't very good with an italic font.
487 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000488 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000489 char stem_chars[] = {'i', 'I', '!', '1'};
490 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
491 FT_BBox bbox;
492 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
493 int16_t width = bbox.xMax - bbox.xMin;
494 if (width > 0 && width < min_width) {
495 min_width = width;
496 info->fStemV = min_width;
497 }
498 }
499 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000500
501 TT_PCLT* pclt_info;
502 TT_OS2* os2_table;
503 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
504 info->fCapHeight = pclt_info->CapHeight;
505 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
506 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000507 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000509 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000510 } else if ((os2_table =
511 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
512 info->fCapHeight = os2_table->sCapHeight;
513 } else {
514 // Figure out a good guess for CapHeight: average the height of M and X.
515 FT_BBox m_bbox, x_bbox;
516 bool got_m, got_x;
517 got_m = GetLetterCBox(face, 'M', &m_bbox);
518 got_x = GetLetterCBox(face, 'X', &x_bbox);
519 if (got_m && got_x) {
520 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
521 x_bbox.yMin) / 2;
522 } else if (got_m && !got_x) {
523 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
524 } else if (!got_m && got_x) {
525 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
526 }
527 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000528
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000529 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
530 face->bbox.xMax, face->bbox.yMin);
531
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000532 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
533 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
534 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
535 }
536
537 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000538 if (FT_IS_FIXED_WIDTH(face)) {
539 appendRange(&info->fGlyphWidths, 0);
540 int16_t advance = face->max_advance_width;
541 info->fGlyphWidths->fAdvance.append(1, &advance);
542 finishRange(info->fGlyphWidths.get(), 0,
543 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000544 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000545 appendRange(&info->fGlyphWidths, 0);
546 // So as to not blow out the stack, get advances in batches.
547 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
548 FT_Fixed advances[128];
549 int advanceCount = 128;
550 if (gID + advanceCount > face->num_glyphs)
551 advanceCount = face->num_glyphs - gID + 1;
552 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
553 advances);
554 for (int i = 0; i < advanceCount; i++) {
555 int16_t advance = advances[gID + i];
556 info->fGlyphWidths->fAdvance.append(1, &advance);
557 }
558 }
559 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
560 SkAdvancedTypefaceMetrics::WidthRange::kRange);
561 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000562 info->fGlyphWidths.reset(
563 getAdvanceData(face, face->num_glyphs, &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000564 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000565 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000566
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000567 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
568 FT_HAS_VERTICAL(face)) {
569 SkASSERT(false); // Not implemented yet.
570 }
571
572 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
573 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
574 // Postscript fonts may contain more than 255 glyphs, so we end up
575 // using multiple font descriptions with a glyph ordering. Record
576 // the name of each glyph.
577 info->fGlyphNames.reset(
578 new SkAutoTArray<SkString>(face->num_glyphs));
579 for (int gID = 0; gID < face->num_glyphs; gID++) {
580 char glyphName[128]; // PS limit for names is 127 bytes.
581 FT_Get_Glyph_Name(face, gID, glyphName, 128);
582 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000583 }
584 }
585
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000586 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
587 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
588 face->num_charmaps) {
589 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
590 }
591
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000592 if (!canEmbed(face))
593 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
594
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000595 unref_ft_face(face);
596 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000597#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000598}
reed@google.com618ef5e2011-01-26 22:10:41 +0000599///////////////////////////////////////////////////////////////////////////
600
601void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
602 if (!gLCDSupportValid) {
603 InitFreetype();
604 FT_Done_FreeType(gFTLibrary);
605 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000606
reed@google.comeffc5012011-06-27 16:44:46 +0000607 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000608 // If the runtime Freetype library doesn't support LCD mode, we disable
609 // it here.
610 rec->fMaskFormat = SkMask::kA8_Format;
611 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000612
reed@google.com618ef5e2011-01-26 22:10:41 +0000613 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000614 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000615 // collapse full->normal hinting if we're not doing LCD
616 h = SkPaint::kNormal_Hinting;
617 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
618 SkPaint::kNo_Hinting != h) {
619 // to do subpixel, we must have at most slight hinting
620 h = SkPaint::kSlight_Hinting;
621 }
622 rec->setHinting(h);
623}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000624
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000625#ifdef ANDROID
626uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
627 SkAutoMutexAcquire ac(gFTMutex);
628 SkFaceRec *rec = ref_ft_face(fontID);
629 uint16_t unitsPerEm = 0;
630
631 if (rec != NULL && rec->fFace != NULL) {
632 unitsPerEm = rec->fFace->units_per_EM;
633 unref_ft_face(rec->fFace);
634 }
635
636 return (uint32_t)unitsPerEm;
637}
638#endif
639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000641 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642 SkAutoMutexAcquire ac(gFTMutex);
643
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000645 if (!InitFreetype()) {
646 sk_throw();
647 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000648 }
649 ++gFTCount;
650
651 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000652 fFTSize = NULL;
653 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000655 if (NULL == fFaceRec) {
656 return;
657 }
658 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659
660 // compute our factors from the record
661
662 SkMatrix m;
663
664 fRec.getSingleMatrix(&m);
665
666#ifdef DUMP_STRIKE_CREATION
667 SkString keyString;
668 SkFontHost::GetDescriptorKeyString(desc, &keyString);
669 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
670 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
671 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
672 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000673 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674#endif
675
676 // now compute our scale factors
677 SkScalar sx = m.getScaleX();
678 SkScalar sy = m.getScaleY();
679
680 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
681 // sort of give up on hinting
682 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
683 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
684 sx = sy = SkScalarAve(sx, sy);
685
686 SkScalar inv = SkScalarInvert(sx);
687
688 // flip the skew elements to go from our Y-down system to FreeType's
689 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
690 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
691 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
692 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
693 } else {
694 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
695 fMatrix22.xy = fMatrix22.yx = 0;
696 }
697
698 fScaleX = SkScalarToFixed(sx);
699 fScaleY = SkScalarToFixed(sy);
700
701 // compute the flags we send to Load_Glyph
702 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000703 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704
agl@chromium.org70a303f2010-05-10 14:15:50 +0000705 if (SkMask::kBW_Format == fRec.fMaskFormat) {
706 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
707 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000708 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000709 loadFlags = FT_LOAD_NO_HINTING;
reed@google.comeffc5012011-06-27 16:44:46 +0000710 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000711 } else {
712 switch (fRec.getHinting()) {
713 case SkPaint::kNo_Hinting:
714 loadFlags = FT_LOAD_NO_HINTING;
715 break;
716 case SkPaint::kSlight_Hinting:
717 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
718 break;
719 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000720 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
721 loadFlags = FT_LOAD_FORCE_AUTOHINT;
722 else
723 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000724 break;
725 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000726 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
727 loadFlags = FT_LOAD_FORCE_AUTOHINT;
728 break;
729 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000730 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000731 if (isLCD(fRec)) {
732 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
733 loadFlags = FT_LOAD_TARGET_LCD_V;
734 } else {
735 loadFlags = FT_LOAD_TARGET_LCD;
736 }
reed@google.comea2333d2011-03-14 16:44:56 +0000737 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000738 break;
739 default:
740 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
741 break;
742 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000743 }
744
reed@google.comeffc5012011-06-27 16:44:46 +0000745 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000746 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000747 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000748
reed@google.com96a9f7912011-05-06 11:49:30 +0000749 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
750 // advances, as fontconfig and cairo do.
751 // See http://code.google.com/p/skia/issues/detail?id=222.
752 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
753
reed@android.come4d0bc02009-07-24 19:53:20 +0000754 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000755 }
756
757 // now create the FT_Size
758
759 {
760 FT_Error err;
761
762 err = FT_New_Size(fFace, &fFTSize);
763 if (err != 0) {
764 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
765 fFaceRec->fFontID, fScaleX, fScaleY, err));
766 fFace = NULL;
767 return;
768 }
769
770 err = FT_Activate_Size(fFTSize);
771 if (err != 0) {
772 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
773 fFaceRec->fFontID, fScaleX, fScaleY, err));
774 fFTSize = NULL;
775 }
776
777 err = FT_Set_Char_Size( fFace,
778 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
779 72, 72);
780 if (err != 0) {
781 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
782 fFaceRec->fFontID, fScaleX, fScaleY, err));
783 fFace = NULL;
784 return;
785 }
786
787 FT_Set_Transform( fFace, &fMatrix22, NULL);
788 }
789}
790
791SkScalerContext_FreeType::~SkScalerContext_FreeType() {
792 if (fFTSize != NULL) {
793 FT_Done_Size(fFTSize);
794 }
795
796 SkAutoMutexAcquire ac(gFTMutex);
797
798 if (fFace != NULL) {
799 unref_ft_face(fFace);
800 }
801 if (--gFTCount == 0) {
802// SkDEBUGF(("FT_Done_FreeType\n"));
803 FT_Done_FreeType(gFTLibrary);
804 SkDEBUGCODE(gFTLibrary = NULL;)
805 }
806}
807
808/* We call this before each use of the fFace, since we may be sharing
809 this face with other context (at different sizes).
810*/
811FT_Error SkScalerContext_FreeType::setupSize() {
812 /* In the off-chance that a font has been removed, we want to error out
813 right away, so call resolve just to be sure.
814
815 TODO: perhaps we can skip this, by walking the global font cache and
816 killing all of the contexts when we know that a given fontID is going
817 away...
818 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000819 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 return (FT_Error)-1;
821 }
822
823 FT_Error err = FT_Activate_Size(fFTSize);
824
825 if (err != 0) {
826 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
827 fFaceRec->fFontID, fScaleX, fScaleY, err));
828 fFTSize = NULL;
829 } else {
830 // seems we need to reset this every time (not sure why, but without it
831 // I get random italics from some other fFTSize)
832 FT_Set_Transform( fFace, &fMatrix22, NULL);
833 }
834 return err;
835}
836
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000837void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
838 FT_Pos strength;
839 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
840 / 24;
841 FT_Outline_Embolden(outline, strength);
842}
843
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000844unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 return fFace->num_glyphs;
846}
847
848uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
849 return SkToU16(FT_Get_Char_Index( fFace, uni ));
850}
851
reed@android.com9d3a9852010-01-08 14:07:42 +0000852SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
853 // iterate through each cmap entry, looking for matching glyph indices
854 FT_UInt glyphIndex;
855 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
856
857 while (glyphIndex != 0) {
858 if (glyphIndex == glyph) {
859 return charCode;
860 }
861 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
862 }
863
864 return 0;
865}
866
reed@android.com8a1c16f2008-12-17 15:59:43 +0000867static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
868 switch (format) {
869 case SkMask::kBW_Format:
870 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871 case SkMask::kA8_Format:
872 default:
873 return FT_PIXEL_MODE_GRAY;
874 }
875}
876
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
878#ifdef FT_ADVANCES_H
879 /* unhinted and light hinted text have linearly scaled advances
880 * which are very cheap to compute with some font formats...
881 */
882 {
883 SkAutoMutexAcquire ac(gFTMutex);
884
885 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000886 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887 return;
888 }
889
890 FT_Error error;
891 FT_Fixed advance;
892
893 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
894 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
895 &advance );
896 if (0 == error) {
897 glyph->fRsbDelta = 0;
898 glyph->fLsbDelta = 0;
899 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
900 glyph->fAdvanceY = 0;
901 return;
902 }
903 }
904#endif /* FT_ADVANCES_H */
905 /* otherwise, we need to load/hint the glyph, which is slower */
906 this->generateMetrics(glyph);
907 return;
908}
909
910void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
911 SkAutoMutexAcquire ac(gFTMutex);
912
913 glyph->fRsbDelta = 0;
914 glyph->fLsbDelta = 0;
915
916 FT_Error err;
917
918 if (this->setupSize()) {
919 goto ERROR;
920 }
921
922 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
923 if (err != 0) {
924 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
925 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
926 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000927 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928 return;
929 }
930
931 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000932 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933 FT_BBox bbox;
934
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000935 if (fRec.fFlags & kEmbolden_Flag) {
936 emboldenOutline(&fFace->glyph->outline);
937 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
939
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000940 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941 int dx = glyph->getSubXFixed() >> 10;
942 int dy = glyph->getSubYFixed() >> 10;
943 // negate dy since freetype-y-goes-up and skia-y-goes-down
944 bbox.xMin += dx;
945 bbox.yMin -= dy;
946 bbox.xMax += dx;
947 bbox.yMax -= dy;
948 }
949
950 bbox.xMin &= ~63;
951 bbox.yMin &= ~63;
952 bbox.xMax = (bbox.xMax + 63) & ~63;
953 bbox.yMax = (bbox.yMax + 63) & ~63;
954
955 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
956 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
957 glyph->fTop = -SkToS16(bbox.yMax >> 6);
958 glyph->fLeft = SkToS16(bbox.xMin >> 6);
959 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000960 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961
962 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000963 if (fRec.fFlags & kEmbolden_Flag) {
964 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
965 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
966 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
968 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
969 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
970 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
971 break;
972
973 default:
974 SkASSERT(!"unknown glyph format");
975 goto ERROR;
976 }
977
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000978 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000979 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
980 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
981 if (fRec.fFlags & kDevKernText_Flag) {
982 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
983 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
984 }
985 } else {
986 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
987 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
988 }
989
990#ifdef ENABLE_GLYPH_SPEW
991 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
992 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
993#endif
994}
995
reed@google.combde3c8e2011-05-18 11:58:10 +0000996static int lerp(int start, int end) {
997 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
998 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +0000999}
1000
1001static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1002 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001003 // want (a+b+c)/3, but we approx to avoid the divide
1004 unsigned ave = (5 * (r + g + b) + b) >> 4;
1005 r = lerp(r, ave);
1006 g = lerp(g, ave);
1007 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001008 }
1009 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1010}
1011
reed@google.com73824072011-06-23 13:17:30 +00001012static uint16_t grayToRGB16(U8CPU gray) {
1013 SkASSERT(gray <= 255);
1014 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1015}
1016
1017static int bittst(const uint8_t data[], int bitOffset) {
1018 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001019 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001020 return lowBit & 1;
1021}
1022
reed@google.comeffc5012011-06-27 16:44:46 +00001023static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1024 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001025 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001026 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001027 const size_t dstRB = glyph.rowBytes();
1028 const int width = glyph.fWidth;
1029 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001030
reed@google.com73824072011-06-23 13:17:30 +00001031 switch (bitmap.pixel_mode) {
1032 case FT_PIXEL_MODE_MONO: {
1033 for (int y = 0; y < glyph.fHeight; ++y) {
1034 for (int x = 0; x < width; ++x) {
1035 dst[x] = -bittst(src, x);
1036 }
1037 dst = (uint16_t*)((char*)dst + dstRB);
1038 src += bitmap.pitch;
1039 }
1040 } break;
1041 case FT_PIXEL_MODE_GRAY: {
1042 for (int y = 0; y < glyph.fHeight; ++y) {
1043 for (int x = 0; x < width; ++x) {
1044 dst[x] = grayToRGB16(src[x]);
1045 }
1046 dst = (uint16_t*)((char*)dst + dstRB);
1047 src += bitmap.pitch;
1048 }
1049 } break;
1050 default: {
1051 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1052 src += 3;
1053 for (int y = 0; y < glyph.fHeight; y++) {
1054 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001055 if (lcdIsBGR) {
1056 for (int x = 0; x < width; x++) {
1057 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1058 triple += 3;
1059 }
1060 } else {
1061 for (int x = 0; x < width; x++) {
1062 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1063 triple += 3;
1064 }
reed@google.com73824072011-06-23 13:17:30 +00001065 }
1066 src += bitmap.pitch;
1067 dst = (uint16_t*)((char*)dst + dstRB);
1068 }
1069 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001070 }
1071}
1072
reed@android.com8a1c16f2008-12-17 15:59:43 +00001073void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1074 SkAutoMutexAcquire ac(gFTMutex);
1075
1076 FT_Error err;
1077
1078 if (this->setupSize()) {
1079 goto ERROR;
1080 }
1081
1082 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1083 if (err != 0) {
1084 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1085 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1086 ERROR:
1087 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1088 return;
1089 }
1090
1091 switch ( fFace->glyph->format ) {
1092 case FT_GLYPH_FORMAT_OUTLINE: {
1093 FT_Outline* outline = &fFace->glyph->outline;
1094 FT_BBox bbox;
1095 FT_Bitmap target;
1096
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001097 if (fRec.fFlags & kEmbolden_Flag) {
1098 emboldenOutline(outline);
1099 }
1100
reed@android.com8a1c16f2008-12-17 15:59:43 +00001101 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001102 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001103 dx = glyph.getSubXFixed() >> 10;
1104 dy = glyph.getSubYFixed() >> 10;
1105 // negate dy since freetype-y-goes-up and skia-y-goes-down
1106 dy = -dy;
1107 }
1108 FT_Outline_Get_CBox(outline, &bbox);
1109 /*
1110 what we really want to do for subpixel is
1111 offset(dx, dy)
1112 compute_bounds
1113 offset(bbox & !63)
1114 but that is two calls to offset, so we do the following, which
1115 achieves the same thing with only one offset call.
1116 */
1117 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1118 dy - ((bbox.yMin + dy) & ~63));
1119
reed@google.comea2333d2011-03-14 16:44:56 +00001120 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1121 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001122 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1123 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001124 } else {
1125 target.width = glyph.fWidth;
1126 target.rows = glyph.fHeight;
1127 target.pitch = glyph.rowBytes();
1128 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1129 target.pixel_mode = compute_pixel_mode(
1130 (SkMask::Format)fRec.fMaskFormat);
1131 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132
reed@google.comea2333d2011-03-14 16:44:56 +00001133 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1134 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1135 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 } break;
1137
1138 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001139 if (fRec.fFlags & kEmbolden_Flag) {
1140 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1141 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1142 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1144 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1145 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1146 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1147
1148 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1149 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150
agl@chromium.org558434a2009-08-11 17:22:38 +00001151 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1152 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1153 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001154 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1155 unsigned dstRowBytes = glyph.rowBytes();
1156 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1157 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1158
1159 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1160 memcpy(dst, src, minRowBytes);
1161 memset(dst + minRowBytes, 0, extraRowBytes);
1162 src += srcRowBytes;
1163 dst += dstRowBytes;
1164 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001165 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001166 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001167 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1168 uint8_t byte = 0;
1169 int bits = 0;
1170 const uint8_t* src_row = src;
1171 uint8_t* dst_row = dst;
1172
1173 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1174 if (!bits) {
1175 byte = *src_row++;
1176 bits = 8;
1177 }
1178
1179 *dst_row++ = byte & 0x80 ? 0xff : 0;
1180 bits--;
1181 byte <<= 1;
1182 }
1183
1184 src += fFace->glyph->bitmap.pitch;
1185 dst += glyph.rowBytes();
1186 }
reed@google.com73824072011-06-23 13:17:30 +00001187 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001188 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1189 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001190 } else {
reed@google.com73824072011-06-23 13:17:30 +00001191 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001192 }
1193 } break;
1194
1195 default:
1196 SkASSERT(!"unknown glyph format");
1197 goto ERROR;
1198 }
1199}
1200
1201///////////////////////////////////////////////////////////////////////////////
1202
1203#define ft2sk(x) SkFixedToScalar((x) << 10)
1204
reed@android.com6f252972009-01-14 16:46:16 +00001205#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 #define CONST_PARAM const
1207#else // older freetype doesn't use const here
1208 #define CONST_PARAM
1209#endif
1210
1211static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1212 SkPath* path = (SkPath*)ctx;
1213 path->close(); // to close the previous contour (if any)
1214 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1215 return 0;
1216}
1217
1218static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1219 SkPath* path = (SkPath*)ctx;
1220 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1221 return 0;
1222}
1223
1224static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1225 void* ctx) {
1226 SkPath* path = (SkPath*)ctx;
1227 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1228 return 0;
1229}
1230
1231static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1232 CONST_PARAM FT_Vector* pt2, void* ctx) {
1233 SkPath* path = (SkPath*)ctx;
1234 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1235 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1236 return 0;
1237}
1238
1239void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1240 SkPath* path) {
1241 SkAutoMutexAcquire ac(gFTMutex);
1242
1243 SkASSERT(&glyph && path);
1244
1245 if (this->setupSize()) {
1246 path->reset();
1247 return;
1248 }
1249
1250 uint32_t flags = fLoadGlyphFlags;
1251 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1252 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1253
1254 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1255
1256 if (err != 0) {
1257 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1258 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1259 path->reset();
1260 return;
1261 }
1262
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001263 if (fRec.fFlags & kEmbolden_Flag) {
1264 emboldenOutline(&fFace->glyph->outline);
1265 }
1266
reed@android.com8a1c16f2008-12-17 15:59:43 +00001267 FT_Outline_Funcs funcs;
1268
1269 funcs.move_to = move_proc;
1270 funcs.line_to = line_proc;
1271 funcs.conic_to = quad_proc;
1272 funcs.cubic_to = cubic_proc;
1273 funcs.shift = 0;
1274 funcs.delta = 0;
1275
1276 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1277
1278 if (err != 0) {
1279 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1280 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1281 path->reset();
1282 return;
1283 }
1284
1285 path->close();
1286}
1287
1288void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1289 SkPaint::FontMetrics* my) {
1290 if (NULL == mx && NULL == my) {
1291 return;
1292 }
1293
1294 SkAutoMutexAcquire ac(gFTMutex);
1295
1296 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001297 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001298 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001299 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300 }
1301 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001302 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 }
1304 return;
1305 }
1306
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001307 FT_Face face = fFace;
1308 int upem = face->units_per_EM;
1309 if (upem <= 0) {
1310 goto ERROR;
1311 }
1312
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001313 SkPoint pts[6];
1314 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001315 SkFixed scaleY = fScaleY;
1316 SkFixed mxy = fMatrix22.xy;
1317 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001318 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1319 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001321 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001322 if (leading < 0) {
1323 leading = 0;
1324 }
1325
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001326 // Try to get the OS/2 table from the font. This contains the specific
1327 // average font width metrics which Windows uses.
1328 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1329
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330 ys[0] = -face->bbox.yMax;
1331 ys[1] = -face->ascender;
1332 ys[2] = -face->descender;
1333 ys[3] = -face->bbox.yMin;
1334 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001335 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1336
1337 SkScalar x_height;
1338 if (os2 && os2->sxHeight) {
1339 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1340 } else {
1341 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1342 if (x_glyph) {
1343 FT_BBox bbox;
1344 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001345 if (fRec.fFlags & kEmbolden_Flag) {
1346 emboldenOutline(&fFace->glyph->outline);
1347 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001348 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1349 x_height = SkIntToScalar(bbox.yMax) / 64;
1350 } else {
1351 x_height = 0;
1352 }
1353 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001354
1355 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001356 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1358 SkFixed x = SkFixedMul(mxy, y);
1359 y = SkFixedMul(myy, y);
1360 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1361 }
1362
1363 if (mx) {
1364 mx->fTop = pts[0].fX;
1365 mx->fAscent = pts[1].fX;
1366 mx->fDescent = pts[2].fX;
1367 mx->fBottom = pts[3].fX;
1368 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001369 mx->fAvgCharWidth = pts[5].fX;
1370 mx->fXMin = xmin;
1371 mx->fXMax = xmax;
1372 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373 }
1374 if (my) {
1375 my->fTop = pts[0].fY;
1376 my->fAscent = pts[1].fY;
1377 my->fDescent = pts[2].fY;
1378 my->fBottom = pts[3].fY;
1379 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001380 my->fAvgCharWidth = pts[5].fY;
1381 my->fXMin = xmin;
1382 my->fXMax = xmax;
1383 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 }
1385}
1386
1387////////////////////////////////////////////////////////////////////////
1388////////////////////////////////////////////////////////////////////////
1389
1390SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001391 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1392 if (!c->success()) {
1393 SkDELETE(c);
1394 c = NULL;
1395 }
1396 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001397}
1398
1399///////////////////////////////////////////////////////////////////////////////
1400
1401/* Export this so that other parts of our FonttHost port can make use of our
1402 ability to extract the name+style from a stream, using FreeType's api.
1403*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001404SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1405 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001407 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001408 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 return SkTypeface::kNormal;
1410 }
1411
1412 FT_Open_Args args;
1413 memset(&args, 0, sizeof(args));
1414
1415 const void* memoryBase = stream->getMemoryBase();
1416 FT_StreamRec streamRec;
1417
1418 if (NULL != memoryBase) {
1419 args.flags = FT_OPEN_MEMORY;
1420 args.memory_base = (const FT_Byte*)memoryBase;
1421 args.memory_size = stream->getLength();
1422 } else {
1423 memset(&streamRec, 0, sizeof(streamRec));
1424 streamRec.size = stream->read(NULL, 0);
1425 streamRec.descriptor.pointer = stream;
1426 streamRec.read = sk_stream_read;
1427 streamRec.close = sk_stream_close;
1428
1429 args.flags = FT_OPEN_STREAM;
1430 args.stream = &streamRec;
1431 }
1432
1433 FT_Face face;
1434 if (FT_Open_Face(library, &args, 0, &face)) {
1435 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001436 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001437 return SkTypeface::kNormal;
1438 }
1439
1440 name->set(face->family_name);
1441 int style = SkTypeface::kNormal;
1442
1443 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1444 style |= SkTypeface::kBold;
1445 }
1446 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1447 style |= SkTypeface::kItalic;
1448 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001449 if (isFixedWidth) {
1450 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1451 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001452
1453 FT_Done_Face(face);
1454 FT_Done_FreeType(library);
1455 return (SkTypeface::Style)style;
1456}