blob: b3cc7832e58f85ecfb9adf688e0c9ba8b03252cf [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
agl@chromium.org309485b2009-07-21 17:41:32 +000042#include FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000043
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
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000073using namespace skia_advanced_typeface_metrics_utils;
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075//////////////////////////////////////////////////////////////////////////
76
77struct SkFaceRec;
78
79static SkMutex gFTMutex;
80static int gFTCount;
81static FT_Library gFTLibrary;
82static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000083static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
84static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86/////////////////////////////////////////////////////////////////////////
87
agl@chromium.orge76073b2010-06-04 20:31:17 +000088// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
89// This value was chosen by eyeballing the result in Firefox and trying to match it.
90static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
91
agl@chromium.org309485b2009-07-21 17:41:32 +000092static bool
93InitFreetype() {
94 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +000095 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +000096 return false;
reed@google.comea2333d2011-03-14 16:44:56 +000097 }
agl@chromium.org309485b2009-07-21 17:41:32 +000098
agl@chromium.org309485b2009-07-21 17:41:32 +000099 // Setup LCD filtering. This reduces colour fringes for LCD rendered
100 // glyphs.
101 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000102 gLCDSupport = err == 0;
reed@android.com61608aa2009-07-31 14:52:54 +0000103 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000104
105 return true;
106}
107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108class SkScalerContext_FreeType : public SkScalerContext {
109public:
110 SkScalerContext_FreeType(const SkDescriptor* desc);
111 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000112
reed@android.com62900b42009-02-11 15:07:19 +0000113 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000114 return fFaceRec != NULL &&
115 fFTSize != NULL &&
116 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000117 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
119protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000120 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 virtual uint16_t generateCharToGlyph(SkUnichar uni);
122 virtual void generateAdvance(SkGlyph* glyph);
123 virtual void generateMetrics(SkGlyph* glyph);
124 virtual void generateImage(const SkGlyph& glyph);
125 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
126 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
127 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000128 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129
130private:
131 SkFaceRec* fFaceRec;
132 FT_Face fFace; // reference to shared face in gFaceRecHead
133 FT_Size fFTSize; // our own copy
134 SkFixed fScaleX, fScaleY;
135 FT_Matrix fMatrix22;
136 uint32_t fLoadGlyphFlags;
137
138 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000139 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140};
141
142///////////////////////////////////////////////////////////////////////////
143///////////////////////////////////////////////////////////////////////////
144
145#include "SkStream.h"
146
147struct SkFaceRec {
148 SkFaceRec* fNext;
149 FT_Face fFace;
150 FT_StreamRec fFTStream;
151 SkStream* fSkStream;
152 uint32_t fRefCnt;
153 uint32_t fFontID;
154
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000155 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 SkFaceRec(SkStream* strm, uint32_t fontID);
157 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000158 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 }
160};
161
162extern "C" {
163 static unsigned long sk_stream_read(FT_Stream stream,
164 unsigned long offset,
165 unsigned char* buffer,
166 unsigned long count ) {
167 SkStream* str = (SkStream*)stream->descriptor.pointer;
168
169 if (count) {
170 if (!str->rewind()) {
171 return 0;
172 } else {
173 unsigned long ret;
174 if (offset) {
175 ret = str->read(NULL, offset);
176 if (ret != offset) {
177 return 0;
178 }
179 }
180 ret = str->read(buffer, count);
181 if (ret != count) {
182 return 0;
183 }
184 count = ret;
185 }
186 }
187 return count;
188 }
189
190 static void sk_stream_close( FT_Stream stream) {}
191}
192
193SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
194 : fSkStream(strm), fFontID(fontID) {
195// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
196
reed@android.com4516f472009-06-29 16:25:36 +0000197 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 fFTStream.size = fSkStream->getLength();
199 fFTStream.descriptor.pointer = fSkStream;
200 fFTStream.read = sk_stream_read;
201 fFTStream.close = sk_stream_close;
202}
203
reed@android.com62900b42009-02-11 15:07:19 +0000204// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205static SkFaceRec* ref_ft_face(uint32_t fontID) {
206 SkFaceRec* rec = gFaceRecHead;
207 while (rec) {
208 if (rec->fFontID == fontID) {
209 SkASSERT(rec->fFace);
210 rec->fRefCnt += 1;
211 return rec;
212 }
213 rec = rec->fNext;
214 }
215
216 SkStream* strm = SkFontHost::OpenStream(fontID);
217 if (NULL == strm) {
218 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 return 0;
220 }
221
222 // this passes ownership of strm to the rec
223 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
224
225 FT_Open_Args args;
226 memset(&args, 0, sizeof(args));
227 const void* memoryBase = strm->getMemoryBase();
228
229 if (NULL != memoryBase) {
230//printf("mmap(%s)\n", keyString.c_str());
231 args.flags = FT_OPEN_MEMORY;
232 args.memory_base = (const FT_Byte*)memoryBase;
233 args.memory_size = strm->getLength();
234 } else {
235//printf("fopen(%s)\n", keyString.c_str());
236 args.flags = FT_OPEN_STREAM;
237 args.stream = &rec->fFTStream;
238 }
239
agl@chromium.org61a678a2010-08-06 18:08:18 +0000240 int face_index;
241 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
242 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
243 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244
245 if (err) { // bad filename, try the default font
246 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
247 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 return 0;
249 } else {
250 SkASSERT(rec->fFace);
251 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
252 rec->fNext = gFaceRecHead;
253 gFaceRecHead = rec;
254 rec->fRefCnt = 1;
255 return rec;
256 }
257}
258
259static void unref_ft_face(FT_Face face) {
260 SkFaceRec* rec = gFaceRecHead;
261 SkFaceRec* prev = NULL;
262 while (rec) {
263 SkFaceRec* next = rec->fNext;
264 if (rec->fFace == face) {
265 if (--rec->fRefCnt == 0) {
266 if (prev) {
267 prev->fNext = next;
268 } else {
269 gFaceRecHead = next;
270 }
271 FT_Done_Face(face);
272 SkDELETE(rec);
273 }
274 return;
275 }
276 prev = rec;
277 rec = next;
278 }
279 SkASSERT("shouldn't get here, face not in list");
280}
281
282///////////////////////////////////////////////////////////////////////////
283
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000284// Work around for old versions of freetype.
285static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
286 FT_Int32 loadFlags, FT_Fixed* advances) {
287#ifdef FT_ADVANCES_H
288 return FT_Get_Advances(face, start, count, loadFlags, advances);
289#else
290 if (!face || start >= face->num_glyphs ||
291 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
292 return 6; // "Invalid argument."
293 }
294 if (count == 0)
295 return 0;
296
297 for (int i = 0; i < count; i++) {
298 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
299 if (err)
300 return err;
301 advances[i] = face->glyph->advance.x;
302 }
303
304 return 0;
305#endif
306}
307
308static bool canEmbed(FT_Face face) {
309#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
310 FT_UShort fsType = FT_Get_FSType_Flags(face);
311 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
312 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
313#else
314 // No embedding is 0x2 and bitmap embedding only is 0x200.
315 TT_OS2* os2_table;
316 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
317 return (os2_table->fsType & 0x202) == 0;
318 }
319 return false; // We tried, fail safe.
320#endif
321}
322
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000323static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
324 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
325 if (!glyph_id)
326 return false;
327 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
328 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
329 return true;
330}
331
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000332static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000333 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000334 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
335 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000336 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000337 SkASSERT(data);
338 *data = advance;
339 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000340}
341
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000342static void populate_glyph_to_unicode(FT_Face& face,
343 SkTDArray<SkUnichar>* glyphToUnicode) {
344 // Check and see if we have Unicode cmaps.
345 for (int i = 0; i < face->num_charmaps; ++i) {
346 // CMaps known to support Unicode:
347 // Platform ID Encoding ID Name
348 // ----------- ----------- -----------------------------------
349 // 0 0,1 Apple Unicode
350 // 0 3 Apple Unicode 2.0 (preferred)
351 // 3 1 Microsoft Unicode UCS-2
352 // 3 10 Microsoft Unicode UCS-4 (preferred)
353 //
354 // See Apple TrueType Reference Manual
355 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
356 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
357 // Microsoft OpenType Specification
358 // http://www.microsoft.com/typography/otspec/cmap.htm
359
360 FT_UShort platformId = face->charmaps[i]->platform_id;
361 FT_UShort encodingId = face->charmaps[i]->encoding_id;
362
363 if (platformId != 0 && platformId != 3) {
364 continue;
365 }
366 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
367 continue;
368 }
369 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
370 (platformId == 0 && encodingId == 3));
371
372 FT_Set_Charmap(face, face->charmaps[i]);
373 if (glyphToUnicode->isEmpty()) {
374 glyphToUnicode->setCount(face->num_glyphs);
375 memset(glyphToUnicode->begin(), 0,
376 sizeof(SkUnichar) * face->num_glyphs);
377 }
378
379 // Iterate through each cmap entry.
380 FT_UInt glyphIndex;
381 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
382 glyphIndex != 0;
383 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
384 if (charCode &&
385 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
386 (*glyphToUnicode)[glyphIndex] = charCode;
387 }
388 }
389 }
390}
391
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000392// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000393SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000394 uint32_t fontID,
395 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000396#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000397 return NULL;
398#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000399 SkAutoMutexAcquire ac(gFTMutex);
400 FT_Library libInit = NULL;
401 if (gFTCount == 0) {
402 if (!InitFreetype())
403 sk_throw();
404 libInit = gFTLibrary;
405 }
406 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
407 SkFaceRec* rec = ref_ft_face(fontID);
408 if (NULL == rec)
409 return NULL;
410 FT_Face face = rec->fFace;
411
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000412 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
413 info->fFontName.set(FT_Get_Postscript_Name(face));
414 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
415 info->fLastGlyphID = face->num_glyphs - 1;
416 info->fEmSize = 1000;
417
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000418 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000419 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000420 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000421 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000422 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000423 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000424 cid = true;
425 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000426 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000427 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000428 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000429 cid = true;
430 TT_Header* ttHeader;
431 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
432 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000433 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000434 }
435 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000436
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000437 info->fStyle = 0;
438 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000439 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000441 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000442 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
443 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000444 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
445
446 PS_FontInfoRec ps_info;
447 TT_Postscript* tt_info;
448 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
449 info->fItalicAngle = ps_info.italic_angle;
450 } else if ((tt_info =
451 (TT_Postscript*)FT_Get_Sfnt_Table(face,
452 ft_sfnt_post)) != NULL) {
453 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
454 } else {
455 info->fItalicAngle = 0;
456 }
457
458 info->fAscent = face->ascender;
459 info->fDescent = face->descender;
460
461 // Figure out a good guess for StemV - Min width of i, I, !, 1.
462 // This probably isn't very good with an italic font.
463 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000464 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 char stem_chars[] = {'i', 'I', '!', '1'};
466 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
467 FT_BBox bbox;
468 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
469 int16_t width = bbox.xMax - bbox.xMin;
470 if (width > 0 && width < min_width) {
471 min_width = width;
472 info->fStemV = min_width;
473 }
474 }
475 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000476
477 TT_PCLT* pclt_info;
478 TT_OS2* os2_table;
479 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
480 info->fCapHeight = pclt_info->CapHeight;
481 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
482 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000483 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000484 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000485 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000486 } else if ((os2_table =
487 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
488 info->fCapHeight = os2_table->sCapHeight;
489 } else {
490 // Figure out a good guess for CapHeight: average the height of M and X.
491 FT_BBox m_bbox, x_bbox;
492 bool got_m, got_x;
493 got_m = GetLetterCBox(face, 'M', &m_bbox);
494 got_x = GetLetterCBox(face, 'X', &x_bbox);
495 if (got_m && got_x) {
496 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
497 x_bbox.yMin) / 2;
498 } else if (got_m && !got_x) {
499 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
500 } else if (!got_m && got_x) {
501 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
502 }
503 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000504
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000505 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
506 face->bbox.xMax, face->bbox.yMin);
507
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000508 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
509 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
510 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
511 }
512
513 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000514 if (FT_IS_FIXED_WIDTH(face)) {
515 appendRange(&info->fGlyphWidths, 0);
516 int16_t advance = face->max_advance_width;
517 info->fGlyphWidths->fAdvance.append(1, &advance);
518 finishRange(info->fGlyphWidths.get(), 0,
519 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000520 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000521 appendRange(&info->fGlyphWidths, 0);
522 // So as to not blow out the stack, get advances in batches.
523 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
524 FT_Fixed advances[128];
525 int advanceCount = 128;
526 if (gID + advanceCount > face->num_glyphs)
527 advanceCount = face->num_glyphs - gID + 1;
528 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
529 advances);
530 for (int i = 0; i < advanceCount; i++) {
531 int16_t advance = advances[gID + i];
532 info->fGlyphWidths->fAdvance.append(1, &advance);
533 }
534 }
535 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
536 SkAdvancedTypefaceMetrics::WidthRange::kRange);
537 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000538 info->fGlyphWidths.reset(
539 getAdvanceData(face, face->num_glyphs, &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000540 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000541 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000542
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000543 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
544 FT_HAS_VERTICAL(face)) {
545 SkASSERT(false); // Not implemented yet.
546 }
547
548 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
549 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
550 // Postscript fonts may contain more than 255 glyphs, so we end up
551 // using multiple font descriptions with a glyph ordering. Record
552 // the name of each glyph.
553 info->fGlyphNames.reset(
554 new SkAutoTArray<SkString>(face->num_glyphs));
555 for (int gID = 0; gID < face->num_glyphs; gID++) {
556 char glyphName[128]; // PS limit for names is 127 bytes.
557 FT_Get_Glyph_Name(face, gID, glyphName, 128);
558 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000559 }
560 }
561
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000562 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
563 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
564 face->num_charmaps) {
565 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
566 }
567
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000568 if (!canEmbed(face))
569 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
570
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000571 unref_ft_face(face);
572 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000573#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000574}
reed@google.com618ef5e2011-01-26 22:10:41 +0000575///////////////////////////////////////////////////////////////////////////
576
577void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
578 if (!gLCDSupportValid) {
579 InitFreetype();
580 FT_Done_FreeType(gFTLibrary);
581 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000582
reed@google.com8abde0a2011-03-14 17:45:33 +0000583 if (!gLCDSupport && (rec->isLCD() || SkMask::kLCD16_Format == rec->fMaskFormat)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000584 // If the runtime Freetype library doesn't support LCD mode, we disable
585 // it here.
586 rec->fMaskFormat = SkMask::kA8_Format;
587 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000588
reed@google.com618ef5e2011-01-26 22:10:41 +0000589 SkPaint::Hinting h = rec->getHinting();
590 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
591 // collapse full->normal hinting if we're not doing LCD
592 h = SkPaint::kNormal_Hinting;
593 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
594 SkPaint::kNo_Hinting != h) {
595 // to do subpixel, we must have at most slight hinting
596 h = SkPaint::kSlight_Hinting;
597 }
598 rec->setHinting(h);
599}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000600
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000601#ifdef ANDROID
602uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
603 SkAutoMutexAcquire ac(gFTMutex);
604 SkFaceRec *rec = ref_ft_face(fontID);
605 uint16_t unitsPerEm = 0;
606
607 if (rec != NULL && rec->fFace != NULL) {
608 unitsPerEm = rec->fFace->units_per_EM;
609 unref_ft_face(rec->fFace);
610 }
611
612 return (uint32_t)unitsPerEm;
613}
614#endif
615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000617 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618 SkAutoMutexAcquire ac(gFTMutex);
619
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000621 if (!InitFreetype()) {
622 sk_throw();
623 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 }
625 ++gFTCount;
626
627 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000628 fFTSize = NULL;
629 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000631 if (NULL == fFaceRec) {
632 return;
633 }
634 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000635
636 // compute our factors from the record
637
638 SkMatrix m;
639
640 fRec.getSingleMatrix(&m);
641
642#ifdef DUMP_STRIKE_CREATION
643 SkString keyString;
644 SkFontHost::GetDescriptorKeyString(desc, &keyString);
645 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
646 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
647 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
648 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000649 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000650#endif
651
652 // now compute our scale factors
653 SkScalar sx = m.getScaleX();
654 SkScalar sy = m.getScaleY();
655
656 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
657 // sort of give up on hinting
658 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
659 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
660 sx = sy = SkScalarAve(sx, sy);
661
662 SkScalar inv = SkScalarInvert(sx);
663
664 // flip the skew elements to go from our Y-down system to FreeType's
665 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
666 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
667 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
668 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
669 } else {
670 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
671 fMatrix22.xy = fMatrix22.yx = 0;
672 }
673
674 fScaleX = SkScalarToFixed(sx);
675 fScaleY = SkScalarToFixed(sy);
676
677 // compute the flags we send to Load_Glyph
678 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000679 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680
agl@chromium.org70a303f2010-05-10 14:15:50 +0000681 if (SkMask::kBW_Format == fRec.fMaskFormat) {
682 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
683 loadFlags = FT_LOAD_TARGET_MONO;
684 if (fRec.getHinting() == SkPaint::kNo_Hinting)
685 loadFlags = FT_LOAD_NO_HINTING;
686 } else {
687 switch (fRec.getHinting()) {
688 case SkPaint::kNo_Hinting:
689 loadFlags = FT_LOAD_NO_HINTING;
690 break;
691 case SkPaint::kSlight_Hinting:
692 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
693 break;
694 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000695 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
696 loadFlags = FT_LOAD_FORCE_AUTOHINT;
697 else
698 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000699 break;
700 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000701 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
702 loadFlags = FT_LOAD_FORCE_AUTOHINT;
703 break;
704 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000705 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comea2333d2011-03-14 16:44:56 +0000706 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat ||
707 SkMask::kLCD16_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000708 loadFlags = FT_LOAD_TARGET_LCD;
reed@google.comea2333d2011-03-14 16:44:56 +0000709 } else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000710 loadFlags = FT_LOAD_TARGET_LCD_V;
reed@google.comea2333d2011-03-14 16:44:56 +0000711 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000712 break;
713 default:
714 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
715 break;
716 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717 }
718
agl@chromium.org99e1b902010-01-05 01:19:44 +0000719 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000720 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000721
reed@google.com96a9f7912011-05-06 11:49:30 +0000722 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
723 // advances, as fontconfig and cairo do.
724 // See http://code.google.com/p/skia/issues/detail?id=222.
725 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
726
reed@android.come4d0bc02009-07-24 19:53:20 +0000727 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000728 }
729
730 // now create the FT_Size
731
732 {
733 FT_Error err;
734
735 err = FT_New_Size(fFace, &fFTSize);
736 if (err != 0) {
737 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
738 fFaceRec->fFontID, fScaleX, fScaleY, err));
739 fFace = NULL;
740 return;
741 }
742
743 err = FT_Activate_Size(fFTSize);
744 if (err != 0) {
745 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
746 fFaceRec->fFontID, fScaleX, fScaleY, err));
747 fFTSize = NULL;
748 }
749
750 err = FT_Set_Char_Size( fFace,
751 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
752 72, 72);
753 if (err != 0) {
754 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
755 fFaceRec->fFontID, fScaleX, fScaleY, err));
756 fFace = NULL;
757 return;
758 }
759
760 FT_Set_Transform( fFace, &fMatrix22, NULL);
761 }
762}
763
764SkScalerContext_FreeType::~SkScalerContext_FreeType() {
765 if (fFTSize != NULL) {
766 FT_Done_Size(fFTSize);
767 }
768
769 SkAutoMutexAcquire ac(gFTMutex);
770
771 if (fFace != NULL) {
772 unref_ft_face(fFace);
773 }
774 if (--gFTCount == 0) {
775// SkDEBUGF(("FT_Done_FreeType\n"));
776 FT_Done_FreeType(gFTLibrary);
777 SkDEBUGCODE(gFTLibrary = NULL;)
778 }
779}
780
781/* We call this before each use of the fFace, since we may be sharing
782 this face with other context (at different sizes).
783*/
784FT_Error SkScalerContext_FreeType::setupSize() {
785 /* In the off-chance that a font has been removed, we want to error out
786 right away, so call resolve just to be sure.
787
788 TODO: perhaps we can skip this, by walking the global font cache and
789 killing all of the contexts when we know that a given fontID is going
790 away...
791 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000792 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793 return (FT_Error)-1;
794 }
795
796 FT_Error err = FT_Activate_Size(fFTSize);
797
798 if (err != 0) {
799 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
800 fFaceRec->fFontID, fScaleX, fScaleY, err));
801 fFTSize = NULL;
802 } else {
803 // seems we need to reset this every time (not sure why, but without it
804 // I get random italics from some other fFTSize)
805 FT_Set_Transform( fFace, &fMatrix22, NULL);
806 }
807 return err;
808}
809
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000810void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
811 FT_Pos strength;
812 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
813 / 24;
814 FT_Outline_Embolden(outline, strength);
815}
816
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000817unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 return fFace->num_glyphs;
819}
820
821uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
822 return SkToU16(FT_Get_Char_Index( fFace, uni ));
823}
824
reed@android.com9d3a9852010-01-08 14:07:42 +0000825SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
826 // iterate through each cmap entry, looking for matching glyph indices
827 FT_UInt glyphIndex;
828 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
829
830 while (glyphIndex != 0) {
831 if (glyphIndex == glyph) {
832 return charCode;
833 }
834 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
835 }
836
837 return 0;
838}
839
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
841 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000842 case SkMask::kHorizontalLCD_Format:
843 case SkMask::kVerticalLCD_Format:
844 SkASSERT(!"An LCD format should never be passed here");
845 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 case SkMask::kBW_Format:
847 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 case SkMask::kA8_Format:
849 default:
850 return FT_PIXEL_MODE_GRAY;
851 }
852}
853
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
855#ifdef FT_ADVANCES_H
856 /* unhinted and light hinted text have linearly scaled advances
857 * which are very cheap to compute with some font formats...
858 */
859 {
860 SkAutoMutexAcquire ac(gFTMutex);
861
862 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000863 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000864 return;
865 }
866
867 FT_Error error;
868 FT_Fixed advance;
869
870 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
871 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
872 &advance );
873 if (0 == error) {
874 glyph->fRsbDelta = 0;
875 glyph->fLsbDelta = 0;
876 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
877 glyph->fAdvanceY = 0;
878 return;
879 }
880 }
881#endif /* FT_ADVANCES_H */
882 /* otherwise, we need to load/hint the glyph, which is slower */
883 this->generateMetrics(glyph);
884 return;
885}
886
887void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
888 SkAutoMutexAcquire ac(gFTMutex);
889
890 glyph->fRsbDelta = 0;
891 glyph->fLsbDelta = 0;
892
893 FT_Error err;
894
895 if (this->setupSize()) {
896 goto ERROR;
897 }
898
899 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
900 if (err != 0) {
901 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
902 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
903 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000904 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905 return;
906 }
907
908 switch ( fFace->glyph->format ) {
909 case FT_GLYPH_FORMAT_OUTLINE:
910 FT_BBox bbox;
911
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000912 if (fRec.fFlags & kEmbolden_Flag) {
913 emboldenOutline(&fFace->glyph->outline);
914 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000915 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
916
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000917 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918 int dx = glyph->getSubXFixed() >> 10;
919 int dy = glyph->getSubYFixed() >> 10;
920 // negate dy since freetype-y-goes-up and skia-y-goes-down
921 bbox.xMin += dx;
922 bbox.yMin -= dy;
923 bbox.xMax += dx;
924 bbox.yMax -= dy;
925 }
926
927 bbox.xMin &= ~63;
928 bbox.yMin &= ~63;
929 bbox.xMax = (bbox.xMax + 63) & ~63;
930 bbox.yMax = (bbox.yMax + 63) & ~63;
931
932 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
933 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
934 glyph->fTop = -SkToS16(bbox.yMax >> 6);
935 glyph->fLeft = SkToS16(bbox.xMin >> 6);
936 break;
937
938 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000939 if (fRec.fFlags & kEmbolden_Flag) {
940 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
941 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
942 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
944 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
945 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
946 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
947 break;
948
949 default:
950 SkASSERT(!"unknown glyph format");
951 goto ERROR;
952 }
953
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000954 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000955 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
956 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
957 if (fRec.fFlags & kDevKernText_Flag) {
958 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
959 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
960 }
961 } else {
962 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
963 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
964 }
965
966#ifdef ENABLE_GLYPH_SPEW
967 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
968 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
969#endif
970}
971
reed@android.comf5493692009-07-22 19:21:01 +0000972#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000973namespace skia_freetype_support {
974// extern functions from SkFontHost_FreeType_Subpixel
975extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
976extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
977}
978
979using namespace skia_freetype_support;
980#endif
981
reed@google.comea2333d2011-03-14 16:44:56 +0000982static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap) {
reed@google.com260db922011-03-14 18:09:32 +0000983 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
reed@google.comea2333d2011-03-14 16:44:56 +0000984 SkASSERT(glyph.fHeight == bitmap.rows);
985
reed@google.com260db922011-03-14 18:09:32 +0000986 const uint8_t* src = bitmap.buffer + 3;
reed@google.comea2333d2011-03-14 16:44:56 +0000987 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
988 size_t dstRB = glyph.rowBytes();
989 int width = glyph.fWidth;
990
991 for (int y = 0; y < glyph.fHeight; y++) {
992 const uint8_t* triple = src;
993 for (int x = 0; x < width; x++) {
994 dst[x] = SkPackRGB16(triple[0] >> 3, triple[1] >> 2, triple[2] >> 3);
995 triple += 3;
996 }
997 src += bitmap.pitch;
998 dst = (uint16_t*)((char*)dst + dstRB);
999 }
1000}
1001
reed@android.com8a1c16f2008-12-17 15:59:43 +00001002void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1003 SkAutoMutexAcquire ac(gFTMutex);
1004
1005 FT_Error err;
1006
1007 if (this->setupSize()) {
1008 goto ERROR;
1009 }
1010
1011 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1012 if (err != 0) {
1013 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1014 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1015 ERROR:
1016 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1017 return;
1018 }
1019
agl@chromium.org309485b2009-07-21 17:41:32 +00001020 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1021 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1022
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023 switch ( fFace->glyph->format ) {
1024 case FT_GLYPH_FORMAT_OUTLINE: {
1025 FT_Outline* outline = &fFace->glyph->outline;
1026 FT_BBox bbox;
1027 FT_Bitmap target;
1028
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001029 if (fRec.fFlags & kEmbolden_Flag) {
1030 emboldenOutline(outline);
1031 }
1032
reed@android.com8a1c16f2008-12-17 15:59:43 +00001033 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001034 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001035 dx = glyph.getSubXFixed() >> 10;
1036 dy = glyph.getSubYFixed() >> 10;
1037 // negate dy since freetype-y-goes-up and skia-y-goes-down
1038 dy = -dy;
1039 }
1040 FT_Outline_Get_CBox(outline, &bbox);
1041 /*
1042 what we really want to do for subpixel is
1043 offset(dx, dy)
1044 compute_bounds
1045 offset(bbox & !63)
1046 but that is two calls to offset, so we do the following, which
1047 achieves the same thing with only one offset call.
1048 */
1049 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1050 dy - ((bbox.yMin + dy) & ~63));
1051
reed@android.comf5493692009-07-22 19:21:01 +00001052#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +00001053 if (lcdRenderMode) {
1054 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
1055 // we have to call FT_Render_Glyph and memcpy the image out.
1056 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1057 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
1058 FT_Render_Glyph(fFace->glyph, mode);
1059
1060 if (isVertical)
1061 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
1062 else
1063 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
1064
1065 break;
1066 }
1067#endif
1068
reed@google.comea2333d2011-03-14 16:44:56 +00001069 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1070 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
1071 copyFT2LCD16(glyph, fFace->glyph->bitmap);
1072 } else {
1073 target.width = glyph.fWidth;
1074 target.rows = glyph.fHeight;
1075 target.pitch = glyph.rowBytes();
1076 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1077 target.pixel_mode = compute_pixel_mode(
1078 (SkMask::Format)fRec.fMaskFormat);
1079 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080
reed@google.comea2333d2011-03-14 16:44:56 +00001081 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1082 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1083 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 } break;
1085
1086 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001087 if (fRec.fFlags & kEmbolden_Flag) {
1088 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1089 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1090 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1092 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1093 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1094 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1095
1096 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1097 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098
agl@chromium.org558434a2009-08-11 17:22:38 +00001099 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1100 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1101 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001102 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1103 unsigned dstRowBytes = glyph.rowBytes();
1104 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1105 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1106
1107 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1108 memcpy(dst, src, minRowBytes);
1109 memset(dst + minRowBytes, 0, extraRowBytes);
1110 src += srcRowBytes;
1111 dst += dstRowBytes;
1112 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001113 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +00001114 (glyph.fMaskFormat == SkMask::kA8_Format ||
1115 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1116 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001117 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1118 uint8_t byte = 0;
1119 int bits = 0;
1120 const uint8_t* src_row = src;
1121 uint8_t* dst_row = dst;
1122
1123 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1124 if (!bits) {
1125 byte = *src_row++;
1126 bits = 8;
1127 }
1128
1129 *dst_row++ = byte & 0x80 ? 0xff : 0;
1130 bits--;
1131 byte <<= 1;
1132 }
1133
1134 src += fFace->glyph->bitmap.pitch;
1135 dst += glyph.rowBytes();
1136 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001137 } else {
1138 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 }
agl@chromium.org309485b2009-07-21 17:41:32 +00001140
1141 if (lcdRenderMode)
1142 glyph.expandA8ToLCD();
1143
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144 } break;
1145
1146 default:
1147 SkASSERT(!"unknown glyph format");
1148 goto ERROR;
1149 }
1150}
1151
1152///////////////////////////////////////////////////////////////////////////////
1153
1154#define ft2sk(x) SkFixedToScalar((x) << 10)
1155
reed@android.com6f252972009-01-14 16:46:16 +00001156#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 #define CONST_PARAM const
1158#else // older freetype doesn't use const here
1159 #define CONST_PARAM
1160#endif
1161
1162static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1163 SkPath* path = (SkPath*)ctx;
1164 path->close(); // to close the previous contour (if any)
1165 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1166 return 0;
1167}
1168
1169static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1170 SkPath* path = (SkPath*)ctx;
1171 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1172 return 0;
1173}
1174
1175static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1176 void* ctx) {
1177 SkPath* path = (SkPath*)ctx;
1178 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1179 return 0;
1180}
1181
1182static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1183 CONST_PARAM FT_Vector* pt2, void* ctx) {
1184 SkPath* path = (SkPath*)ctx;
1185 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1186 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1187 return 0;
1188}
1189
1190void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1191 SkPath* path) {
1192 SkAutoMutexAcquire ac(gFTMutex);
1193
1194 SkASSERT(&glyph && path);
1195
1196 if (this->setupSize()) {
1197 path->reset();
1198 return;
1199 }
1200
1201 uint32_t flags = fLoadGlyphFlags;
1202 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1203 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1204
1205 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1206
1207 if (err != 0) {
1208 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1209 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1210 path->reset();
1211 return;
1212 }
1213
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001214 if (fRec.fFlags & kEmbolden_Flag) {
1215 emboldenOutline(&fFace->glyph->outline);
1216 }
1217
reed@android.com8a1c16f2008-12-17 15:59:43 +00001218 FT_Outline_Funcs funcs;
1219
1220 funcs.move_to = move_proc;
1221 funcs.line_to = line_proc;
1222 funcs.conic_to = quad_proc;
1223 funcs.cubic_to = cubic_proc;
1224 funcs.shift = 0;
1225 funcs.delta = 0;
1226
1227 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1228
1229 if (err != 0) {
1230 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1231 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1232 path->reset();
1233 return;
1234 }
1235
1236 path->close();
1237}
1238
1239void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1240 SkPaint::FontMetrics* my) {
1241 if (NULL == mx && NULL == my) {
1242 return;
1243 }
1244
1245 SkAutoMutexAcquire ac(gFTMutex);
1246
1247 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001248 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001249 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001250 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251 }
1252 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001253 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001254 }
1255 return;
1256 }
1257
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001258 FT_Face face = fFace;
1259 int upem = face->units_per_EM;
1260 if (upem <= 0) {
1261 goto ERROR;
1262 }
1263
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001264 SkPoint pts[6];
1265 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266 SkFixed scaleY = fScaleY;
1267 SkFixed mxy = fMatrix22.xy;
1268 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001269 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1270 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001272 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 if (leading < 0) {
1274 leading = 0;
1275 }
1276
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001277 // Try to get the OS/2 table from the font. This contains the specific
1278 // average font width metrics which Windows uses.
1279 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 ys[0] = -face->bbox.yMax;
1282 ys[1] = -face->ascender;
1283 ys[2] = -face->descender;
1284 ys[3] = -face->bbox.yMin;
1285 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001286 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1287
1288 SkScalar x_height;
1289 if (os2 && os2->sxHeight) {
1290 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1291 } else {
1292 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1293 if (x_glyph) {
1294 FT_BBox bbox;
1295 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001296 if (fRec.fFlags & kEmbolden_Flag) {
1297 emboldenOutline(&fFace->glyph->outline);
1298 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001299 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1300 x_height = SkIntToScalar(bbox.yMax) / 64;
1301 } else {
1302 x_height = 0;
1303 }
1304 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305
1306 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001307 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1309 SkFixed x = SkFixedMul(mxy, y);
1310 y = SkFixedMul(myy, y);
1311 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1312 }
1313
1314 if (mx) {
1315 mx->fTop = pts[0].fX;
1316 mx->fAscent = pts[1].fX;
1317 mx->fDescent = pts[2].fX;
1318 mx->fBottom = pts[3].fX;
1319 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001320 mx->fAvgCharWidth = pts[5].fX;
1321 mx->fXMin = xmin;
1322 mx->fXMax = xmax;
1323 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324 }
1325 if (my) {
1326 my->fTop = pts[0].fY;
1327 my->fAscent = pts[1].fY;
1328 my->fDescent = pts[2].fY;
1329 my->fBottom = pts[3].fY;
1330 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001331 my->fAvgCharWidth = pts[5].fY;
1332 my->fXMin = xmin;
1333 my->fXMax = xmax;
1334 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 }
1336}
1337
1338////////////////////////////////////////////////////////////////////////
1339////////////////////////////////////////////////////////////////////////
1340
1341SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001342 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1343 if (!c->success()) {
1344 SkDELETE(c);
1345 c = NULL;
1346 }
1347 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348}
1349
1350///////////////////////////////////////////////////////////////////////////////
1351
1352/* Export this so that other parts of our FonttHost port can make use of our
1353 ability to extract the name+style from a stream, using FreeType's api.
1354*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001355SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1356 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001358 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001359 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001360 return SkTypeface::kNormal;
1361 }
1362
1363 FT_Open_Args args;
1364 memset(&args, 0, sizeof(args));
1365
1366 const void* memoryBase = stream->getMemoryBase();
1367 FT_StreamRec streamRec;
1368
1369 if (NULL != memoryBase) {
1370 args.flags = FT_OPEN_MEMORY;
1371 args.memory_base = (const FT_Byte*)memoryBase;
1372 args.memory_size = stream->getLength();
1373 } else {
1374 memset(&streamRec, 0, sizeof(streamRec));
1375 streamRec.size = stream->read(NULL, 0);
1376 streamRec.descriptor.pointer = stream;
1377 streamRec.read = sk_stream_read;
1378 streamRec.close = sk_stream_close;
1379
1380 args.flags = FT_OPEN_STREAM;
1381 args.stream = &streamRec;
1382 }
1383
1384 FT_Face face;
1385 if (FT_Open_Face(library, &args, 0, &face)) {
1386 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001387 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 return SkTypeface::kNormal;
1389 }
1390
1391 name->set(face->family_name);
1392 int style = SkTypeface::kNormal;
1393
1394 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1395 style |= SkTypeface::kBold;
1396 }
1397 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1398 style |= SkTypeface::kItalic;
1399 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001400 if (isFixedWidth) {
1401 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1402 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403
1404 FT_Done_Face(face);
1405 FT_Done_FreeType(library);
1406 return (SkTypeface::Style)style;
1407}