blob: 1c40ad6aa2f0bbc18243e7481b9965dedf6266bd [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@google.combde3c8e2011-05-18 11:58:10 +000075// SK_FREETYPE_LCD_LERP should be 0...256
76// 0 means no color reduction (e.g. just as returned from FreeType)
77// 256 means 100% color reduction (e.g. gray)
reed@google.comc5181342011-05-17 20:52:46 +000078//
79#ifndef SK_FREETYPE_LCD_LERP
reed@google.combde3c8e2011-05-18 11:58:10 +000080 #define SK_FREETYPE_LCD_LERP 96
reed@google.comc5181342011-05-17 20:52:46 +000081#endif
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083//////////////////////////////////////////////////////////////////////////
84
85struct SkFaceRec;
86
87static SkMutex gFTMutex;
88static int gFTCount;
89static FT_Library gFTLibrary;
90static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000091static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
92static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
94/////////////////////////////////////////////////////////////////////////
95
agl@chromium.orge76073b2010-06-04 20:31:17 +000096// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
97// This value was chosen by eyeballing the result in Firefox and trying to match it.
98static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
99
agl@chromium.org309485b2009-07-21 17:41:32 +0000100static bool
101InitFreetype() {
102 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000103 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000104 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000105 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000106
agl@chromium.org309485b2009-07-21 17:41:32 +0000107 // Setup LCD filtering. This reduces colour fringes for LCD rendered
108 // glyphs.
109 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000110 gLCDSupport = err == 0;
reed@android.com61608aa2009-07-31 14:52:54 +0000111 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000112
113 return true;
114}
115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116class SkScalerContext_FreeType : public SkScalerContext {
117public:
118 SkScalerContext_FreeType(const SkDescriptor* desc);
119 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000120
reed@android.com62900b42009-02-11 15:07:19 +0000121 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000122 return fFaceRec != NULL &&
123 fFTSize != NULL &&
124 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000125 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
127protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000128 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 virtual uint16_t generateCharToGlyph(SkUnichar uni);
130 virtual void generateAdvance(SkGlyph* glyph);
131 virtual void generateMetrics(SkGlyph* glyph);
132 virtual void generateImage(const SkGlyph& glyph);
133 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
134 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
135 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000136 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137
138private:
139 SkFaceRec* fFaceRec;
140 FT_Face fFace; // reference to shared face in gFaceRecHead
141 FT_Size fFTSize; // our own copy
142 SkFixed fScaleX, fScaleY;
143 FT_Matrix fMatrix22;
144 uint32_t fLoadGlyphFlags;
145
146 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000147 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148};
149
150///////////////////////////////////////////////////////////////////////////
151///////////////////////////////////////////////////////////////////////////
152
153#include "SkStream.h"
154
155struct SkFaceRec {
156 SkFaceRec* fNext;
157 FT_Face fFace;
158 FT_StreamRec fFTStream;
159 SkStream* fSkStream;
160 uint32_t fRefCnt;
161 uint32_t fFontID;
162
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000163 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 SkFaceRec(SkStream* strm, uint32_t fontID);
165 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000166 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 }
168};
169
170extern "C" {
171 static unsigned long sk_stream_read(FT_Stream stream,
172 unsigned long offset,
173 unsigned char* buffer,
174 unsigned long count ) {
175 SkStream* str = (SkStream*)stream->descriptor.pointer;
176
177 if (count) {
178 if (!str->rewind()) {
179 return 0;
180 } else {
181 unsigned long ret;
182 if (offset) {
183 ret = str->read(NULL, offset);
184 if (ret != offset) {
185 return 0;
186 }
187 }
188 ret = str->read(buffer, count);
189 if (ret != count) {
190 return 0;
191 }
192 count = ret;
193 }
194 }
195 return count;
196 }
197
198 static void sk_stream_close( FT_Stream stream) {}
199}
200
201SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
202 : fSkStream(strm), fFontID(fontID) {
203// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
204
reed@android.com4516f472009-06-29 16:25:36 +0000205 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 fFTStream.size = fSkStream->getLength();
207 fFTStream.descriptor.pointer = fSkStream;
208 fFTStream.read = sk_stream_read;
209 fFTStream.close = sk_stream_close;
210}
211
reed@android.com62900b42009-02-11 15:07:19 +0000212// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213static SkFaceRec* ref_ft_face(uint32_t fontID) {
214 SkFaceRec* rec = gFaceRecHead;
215 while (rec) {
216 if (rec->fFontID == fontID) {
217 SkASSERT(rec->fFace);
218 rec->fRefCnt += 1;
219 return rec;
220 }
221 rec = rec->fNext;
222 }
223
224 SkStream* strm = SkFontHost::OpenStream(fontID);
225 if (NULL == strm) {
226 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 return 0;
228 }
229
230 // this passes ownership of strm to the rec
231 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
232
233 FT_Open_Args args;
234 memset(&args, 0, sizeof(args));
235 const void* memoryBase = strm->getMemoryBase();
236
237 if (NULL != memoryBase) {
238//printf("mmap(%s)\n", keyString.c_str());
239 args.flags = FT_OPEN_MEMORY;
240 args.memory_base = (const FT_Byte*)memoryBase;
241 args.memory_size = strm->getLength();
242 } else {
243//printf("fopen(%s)\n", keyString.c_str());
244 args.flags = FT_OPEN_STREAM;
245 args.stream = &rec->fFTStream;
246 }
247
agl@chromium.org61a678a2010-08-06 18:08:18 +0000248 int face_index;
249 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
250 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
251 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252
253 if (err) { // bad filename, try the default font
254 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
255 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 return 0;
257 } else {
258 SkASSERT(rec->fFace);
259 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
260 rec->fNext = gFaceRecHead;
261 gFaceRecHead = rec;
262 rec->fRefCnt = 1;
263 return rec;
264 }
265}
266
267static void unref_ft_face(FT_Face face) {
268 SkFaceRec* rec = gFaceRecHead;
269 SkFaceRec* prev = NULL;
270 while (rec) {
271 SkFaceRec* next = rec->fNext;
272 if (rec->fFace == face) {
273 if (--rec->fRefCnt == 0) {
274 if (prev) {
275 prev->fNext = next;
276 } else {
277 gFaceRecHead = next;
278 }
279 FT_Done_Face(face);
280 SkDELETE(rec);
281 }
282 return;
283 }
284 prev = rec;
285 rec = next;
286 }
287 SkASSERT("shouldn't get here, face not in list");
288}
289
290///////////////////////////////////////////////////////////////////////////
291
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000292// Work around for old versions of freetype.
293static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
294 FT_Int32 loadFlags, FT_Fixed* advances) {
295#ifdef FT_ADVANCES_H
296 return FT_Get_Advances(face, start, count, loadFlags, advances);
297#else
298 if (!face || start >= face->num_glyphs ||
299 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
300 return 6; // "Invalid argument."
301 }
302 if (count == 0)
303 return 0;
304
305 for (int i = 0; i < count; i++) {
306 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
307 if (err)
308 return err;
309 advances[i] = face->glyph->advance.x;
310 }
311
312 return 0;
313#endif
314}
315
316static bool canEmbed(FT_Face face) {
317#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
318 FT_UShort fsType = FT_Get_FSType_Flags(face);
319 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
320 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
321#else
322 // No embedding is 0x2 and bitmap embedding only is 0x200.
323 TT_OS2* os2_table;
324 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
325 return (os2_table->fsType & 0x202) == 0;
326 }
327 return false; // We tried, fail safe.
328#endif
329}
330
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000331static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
332 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
333 if (!glyph_id)
334 return false;
335 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
336 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
337 return true;
338}
339
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000340static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000341 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000342 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
343 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000344 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000345 SkASSERT(data);
346 *data = advance;
347 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000348}
349
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000350static void populate_glyph_to_unicode(FT_Face& face,
351 SkTDArray<SkUnichar>* glyphToUnicode) {
352 // Check and see if we have Unicode cmaps.
353 for (int i = 0; i < face->num_charmaps; ++i) {
354 // CMaps known to support Unicode:
355 // Platform ID Encoding ID Name
356 // ----------- ----------- -----------------------------------
357 // 0 0,1 Apple Unicode
358 // 0 3 Apple Unicode 2.0 (preferred)
359 // 3 1 Microsoft Unicode UCS-2
360 // 3 10 Microsoft Unicode UCS-4 (preferred)
361 //
362 // See Apple TrueType Reference Manual
363 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
364 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
365 // Microsoft OpenType Specification
366 // http://www.microsoft.com/typography/otspec/cmap.htm
367
368 FT_UShort platformId = face->charmaps[i]->platform_id;
369 FT_UShort encodingId = face->charmaps[i]->encoding_id;
370
371 if (platformId != 0 && platformId != 3) {
372 continue;
373 }
374 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
375 continue;
376 }
377 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
378 (platformId == 0 && encodingId == 3));
379
380 FT_Set_Charmap(face, face->charmaps[i]);
381 if (glyphToUnicode->isEmpty()) {
382 glyphToUnicode->setCount(face->num_glyphs);
383 memset(glyphToUnicode->begin(), 0,
384 sizeof(SkUnichar) * face->num_glyphs);
385 }
386
387 // Iterate through each cmap entry.
388 FT_UInt glyphIndex;
389 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
390 glyphIndex != 0;
391 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
392 if (charCode &&
393 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
394 (*glyphToUnicode)[glyphIndex] = charCode;
395 }
396 }
397 }
398}
399
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000400// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000401SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000402 uint32_t fontID,
403 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000404#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000405 return NULL;
406#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000407 SkAutoMutexAcquire ac(gFTMutex);
408 FT_Library libInit = NULL;
409 if (gFTCount == 0) {
410 if (!InitFreetype())
411 sk_throw();
412 libInit = gFTLibrary;
413 }
414 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
415 SkFaceRec* rec = ref_ft_face(fontID);
416 if (NULL == rec)
417 return NULL;
418 FT_Face face = rec->fFace;
419
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000420 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
421 info->fFontName.set(FT_Get_Postscript_Name(face));
422 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
423 info->fLastGlyphID = face->num_glyphs - 1;
424 info->fEmSize = 1000;
425
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000426 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000427 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000428 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000429 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000430 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000431 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000432 cid = true;
433 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000434 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000435 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000436 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000437 cid = true;
438 TT_Header* ttHeader;
439 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
440 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000441 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000442 }
443 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000444
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000445 info->fStyle = 0;
446 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000447 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000449 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000450 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
451 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000452 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
453
454 PS_FontInfoRec ps_info;
455 TT_Postscript* tt_info;
456 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
457 info->fItalicAngle = ps_info.italic_angle;
458 } else if ((tt_info =
459 (TT_Postscript*)FT_Get_Sfnt_Table(face,
460 ft_sfnt_post)) != NULL) {
461 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
462 } else {
463 info->fItalicAngle = 0;
464 }
465
466 info->fAscent = face->ascender;
467 info->fDescent = face->descender;
468
469 // Figure out a good guess for StemV - Min width of i, I, !, 1.
470 // This probably isn't very good with an italic font.
471 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000472 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000473 char stem_chars[] = {'i', 'I', '!', '1'};
474 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
475 FT_BBox bbox;
476 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
477 int16_t width = bbox.xMax - bbox.xMin;
478 if (width > 0 && width < min_width) {
479 min_width = width;
480 info->fStemV = min_width;
481 }
482 }
483 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000484
485 TT_PCLT* pclt_info;
486 TT_OS2* os2_table;
487 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
488 info->fCapHeight = pclt_info->CapHeight;
489 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
490 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000491 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000492 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000493 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494 } else if ((os2_table =
495 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
496 info->fCapHeight = os2_table->sCapHeight;
497 } else {
498 // Figure out a good guess for CapHeight: average the height of M and X.
499 FT_BBox m_bbox, x_bbox;
500 bool got_m, got_x;
501 got_m = GetLetterCBox(face, 'M', &m_bbox);
502 got_x = GetLetterCBox(face, 'X', &x_bbox);
503 if (got_m && got_x) {
504 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
505 x_bbox.yMin) / 2;
506 } else if (got_m && !got_x) {
507 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
508 } else if (!got_m && got_x) {
509 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
510 }
511 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000512
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000513 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
514 face->bbox.xMax, face->bbox.yMin);
515
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000516 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
517 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
518 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
519 }
520
521 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000522 if (FT_IS_FIXED_WIDTH(face)) {
523 appendRange(&info->fGlyphWidths, 0);
524 int16_t advance = face->max_advance_width;
525 info->fGlyphWidths->fAdvance.append(1, &advance);
526 finishRange(info->fGlyphWidths.get(), 0,
527 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000528 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000529 appendRange(&info->fGlyphWidths, 0);
530 // So as to not blow out the stack, get advances in batches.
531 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
532 FT_Fixed advances[128];
533 int advanceCount = 128;
534 if (gID + advanceCount > face->num_glyphs)
535 advanceCount = face->num_glyphs - gID + 1;
536 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
537 advances);
538 for (int i = 0; i < advanceCount; i++) {
539 int16_t advance = advances[gID + i];
540 info->fGlyphWidths->fAdvance.append(1, &advance);
541 }
542 }
543 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
544 SkAdvancedTypefaceMetrics::WidthRange::kRange);
545 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000546 info->fGlyphWidths.reset(
547 getAdvanceData(face, face->num_glyphs, &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000548 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000549 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000550
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000551 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
552 FT_HAS_VERTICAL(face)) {
553 SkASSERT(false); // Not implemented yet.
554 }
555
556 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
557 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
558 // Postscript fonts may contain more than 255 glyphs, so we end up
559 // using multiple font descriptions with a glyph ordering. Record
560 // the name of each glyph.
561 info->fGlyphNames.reset(
562 new SkAutoTArray<SkString>(face->num_glyphs));
563 for (int gID = 0; gID < face->num_glyphs; gID++) {
564 char glyphName[128]; // PS limit for names is 127 bytes.
565 FT_Get_Glyph_Name(face, gID, glyphName, 128);
566 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000567 }
568 }
569
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000570 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
571 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
572 face->num_charmaps) {
573 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
574 }
575
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000576 if (!canEmbed(face))
577 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
578
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000579 unref_ft_face(face);
580 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000581#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000582}
reed@google.com618ef5e2011-01-26 22:10:41 +0000583///////////////////////////////////////////////////////////////////////////
584
585void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
586 if (!gLCDSupportValid) {
587 InitFreetype();
588 FT_Done_FreeType(gFTLibrary);
589 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000590
reed@google.com8abde0a2011-03-14 17:45:33 +0000591 if (!gLCDSupport && (rec->isLCD() || SkMask::kLCD16_Format == rec->fMaskFormat)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000592 // If the runtime Freetype library doesn't support LCD mode, we disable
593 // it here.
594 rec->fMaskFormat = SkMask::kA8_Format;
595 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000596
reed@google.com618ef5e2011-01-26 22:10:41 +0000597 SkPaint::Hinting h = rec->getHinting();
598 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
599 // collapse full->normal hinting if we're not doing LCD
600 h = SkPaint::kNormal_Hinting;
601 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
602 SkPaint::kNo_Hinting != h) {
603 // to do subpixel, we must have at most slight hinting
604 h = SkPaint::kSlight_Hinting;
605 }
606 rec->setHinting(h);
607}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000608
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000609#ifdef ANDROID
610uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
611 SkAutoMutexAcquire ac(gFTMutex);
612 SkFaceRec *rec = ref_ft_face(fontID);
613 uint16_t unitsPerEm = 0;
614
615 if (rec != NULL && rec->fFace != NULL) {
616 unitsPerEm = rec->fFace->units_per_EM;
617 unref_ft_face(rec->fFace);
618 }
619
620 return (uint32_t)unitsPerEm;
621}
622#endif
623
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000625 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 SkAutoMutexAcquire ac(gFTMutex);
627
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000629 if (!InitFreetype()) {
630 sk_throw();
631 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000632 }
633 ++gFTCount;
634
635 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000636 fFTSize = NULL;
637 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000638 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000639 if (NULL == fFaceRec) {
640 return;
641 }
642 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643
644 // compute our factors from the record
645
646 SkMatrix m;
647
648 fRec.getSingleMatrix(&m);
649
650#ifdef DUMP_STRIKE_CREATION
651 SkString keyString;
652 SkFontHost::GetDescriptorKeyString(desc, &keyString);
653 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
654 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
655 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
656 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000657 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658#endif
659
660 // now compute our scale factors
661 SkScalar sx = m.getScaleX();
662 SkScalar sy = m.getScaleY();
663
664 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
665 // sort of give up on hinting
666 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
667 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
668 sx = sy = SkScalarAve(sx, sy);
669
670 SkScalar inv = SkScalarInvert(sx);
671
672 // flip the skew elements to go from our Y-down system to FreeType's
673 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
674 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
675 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
676 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
677 } else {
678 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
679 fMatrix22.xy = fMatrix22.yx = 0;
680 }
681
682 fScaleX = SkScalarToFixed(sx);
683 fScaleY = SkScalarToFixed(sy);
684
685 // compute the flags we send to Load_Glyph
686 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000687 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688
agl@chromium.org70a303f2010-05-10 14:15:50 +0000689 if (SkMask::kBW_Format == fRec.fMaskFormat) {
690 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
691 loadFlags = FT_LOAD_TARGET_MONO;
692 if (fRec.getHinting() == SkPaint::kNo_Hinting)
693 loadFlags = FT_LOAD_NO_HINTING;
694 } else {
695 switch (fRec.getHinting()) {
696 case SkPaint::kNo_Hinting:
697 loadFlags = FT_LOAD_NO_HINTING;
698 break;
699 case SkPaint::kSlight_Hinting:
700 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
701 break;
702 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000703 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
704 loadFlags = FT_LOAD_FORCE_AUTOHINT;
705 else
706 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000707 break;
708 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000709 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
710 loadFlags = FT_LOAD_FORCE_AUTOHINT;
711 break;
712 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000713 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comea2333d2011-03-14 16:44:56 +0000714 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat ||
715 SkMask::kLCD16_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000716 loadFlags = FT_LOAD_TARGET_LCD;
reed@google.comea2333d2011-03-14 16:44:56 +0000717 } else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000718 loadFlags = FT_LOAD_TARGET_LCD_V;
reed@google.comea2333d2011-03-14 16:44:56 +0000719 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000720 break;
721 default:
722 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
723 break;
724 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725 }
726
agl@chromium.org99e1b902010-01-05 01:19:44 +0000727 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000728 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000729
reed@google.com96a9f7912011-05-06 11:49:30 +0000730 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
731 // advances, as fontconfig and cairo do.
732 // See http://code.google.com/p/skia/issues/detail?id=222.
733 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
734
reed@android.come4d0bc02009-07-24 19:53:20 +0000735 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 }
737
738 // now create the FT_Size
739
740 {
741 FT_Error err;
742
743 err = FT_New_Size(fFace, &fFTSize);
744 if (err != 0) {
745 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
746 fFaceRec->fFontID, fScaleX, fScaleY, err));
747 fFace = NULL;
748 return;
749 }
750
751 err = FT_Activate_Size(fFTSize);
752 if (err != 0) {
753 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
754 fFaceRec->fFontID, fScaleX, fScaleY, err));
755 fFTSize = NULL;
756 }
757
758 err = FT_Set_Char_Size( fFace,
759 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
760 72, 72);
761 if (err != 0) {
762 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
763 fFaceRec->fFontID, fScaleX, fScaleY, err));
764 fFace = NULL;
765 return;
766 }
767
768 FT_Set_Transform( fFace, &fMatrix22, NULL);
769 }
770}
771
772SkScalerContext_FreeType::~SkScalerContext_FreeType() {
773 if (fFTSize != NULL) {
774 FT_Done_Size(fFTSize);
775 }
776
777 SkAutoMutexAcquire ac(gFTMutex);
778
779 if (fFace != NULL) {
780 unref_ft_face(fFace);
781 }
782 if (--gFTCount == 0) {
783// SkDEBUGF(("FT_Done_FreeType\n"));
784 FT_Done_FreeType(gFTLibrary);
785 SkDEBUGCODE(gFTLibrary = NULL;)
786 }
787}
788
789/* We call this before each use of the fFace, since we may be sharing
790 this face with other context (at different sizes).
791*/
792FT_Error SkScalerContext_FreeType::setupSize() {
793 /* In the off-chance that a font has been removed, we want to error out
794 right away, so call resolve just to be sure.
795
796 TODO: perhaps we can skip this, by walking the global font cache and
797 killing all of the contexts when we know that a given fontID is going
798 away...
799 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000800 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 return (FT_Error)-1;
802 }
803
804 FT_Error err = FT_Activate_Size(fFTSize);
805
806 if (err != 0) {
807 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
808 fFaceRec->fFontID, fScaleX, fScaleY, err));
809 fFTSize = NULL;
810 } else {
811 // seems we need to reset this every time (not sure why, but without it
812 // I get random italics from some other fFTSize)
813 FT_Set_Transform( fFace, &fMatrix22, NULL);
814 }
815 return err;
816}
817
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000818void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
819 FT_Pos strength;
820 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
821 / 24;
822 FT_Outline_Embolden(outline, strength);
823}
824
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000825unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826 return fFace->num_glyphs;
827}
828
829uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
830 return SkToU16(FT_Get_Char_Index( fFace, uni ));
831}
832
reed@android.com9d3a9852010-01-08 14:07:42 +0000833SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
834 // iterate through each cmap entry, looking for matching glyph indices
835 FT_UInt glyphIndex;
836 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
837
838 while (glyphIndex != 0) {
839 if (glyphIndex == glyph) {
840 return charCode;
841 }
842 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
843 }
844
845 return 0;
846}
847
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
849 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000850 case SkMask::kHorizontalLCD_Format:
851 case SkMask::kVerticalLCD_Format:
852 SkASSERT(!"An LCD format should never be passed here");
853 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854 case SkMask::kBW_Format:
855 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000856 case SkMask::kA8_Format:
857 default:
858 return FT_PIXEL_MODE_GRAY;
859 }
860}
861
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
863#ifdef FT_ADVANCES_H
864 /* unhinted and light hinted text have linearly scaled advances
865 * which are very cheap to compute with some font formats...
866 */
867 {
868 SkAutoMutexAcquire ac(gFTMutex);
869
870 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000871 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 return;
873 }
874
875 FT_Error error;
876 FT_Fixed advance;
877
878 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
879 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
880 &advance );
881 if (0 == error) {
882 glyph->fRsbDelta = 0;
883 glyph->fLsbDelta = 0;
884 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
885 glyph->fAdvanceY = 0;
886 return;
887 }
888 }
889#endif /* FT_ADVANCES_H */
890 /* otherwise, we need to load/hint the glyph, which is slower */
891 this->generateMetrics(glyph);
892 return;
893}
894
895void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
896 SkAutoMutexAcquire ac(gFTMutex);
897
898 glyph->fRsbDelta = 0;
899 glyph->fLsbDelta = 0;
900
901 FT_Error err;
902
903 if (this->setupSize()) {
904 goto ERROR;
905 }
906
907 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
908 if (err != 0) {
909 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
910 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
911 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000912 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913 return;
914 }
915
916 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000917 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918 FT_BBox bbox;
919
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000920 if (fRec.fFlags & kEmbolden_Flag) {
921 emboldenOutline(&fFace->glyph->outline);
922 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000923 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
924
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000925 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926 int dx = glyph->getSubXFixed() >> 10;
927 int dy = glyph->getSubYFixed() >> 10;
928 // negate dy since freetype-y-goes-up and skia-y-goes-down
929 bbox.xMin += dx;
930 bbox.yMin -= dy;
931 bbox.xMax += dx;
932 bbox.yMax -= dy;
933 }
934
935 bbox.xMin &= ~63;
936 bbox.yMin &= ~63;
937 bbox.xMax = (bbox.xMax + 63) & ~63;
938 bbox.yMax = (bbox.yMax + 63) & ~63;
939
940 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
941 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
942 glyph->fTop = -SkToS16(bbox.yMax >> 6);
943 glyph->fLeft = SkToS16(bbox.xMin >> 6);
944 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000945 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946
947 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000948 if (fRec.fFlags & kEmbolden_Flag) {
949 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
950 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
951 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000952 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
953 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
954 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
955 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
956 break;
957
958 default:
959 SkASSERT(!"unknown glyph format");
960 goto ERROR;
961 }
962
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000963 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
965 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
966 if (fRec.fFlags & kDevKernText_Flag) {
967 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
968 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
969 }
970 } else {
971 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
972 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
973 }
974
975#ifdef ENABLE_GLYPH_SPEW
976 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
977 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
978#endif
979}
980
reed@android.comf5493692009-07-22 19:21:01 +0000981#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000982namespace skia_freetype_support {
983// extern functions from SkFontHost_FreeType_Subpixel
984extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
985extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
986}
987
988using namespace skia_freetype_support;
989#endif
990
reed@google.combde3c8e2011-05-18 11:58:10 +0000991static int lerp(int start, int end) {
992 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
993 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +0000994}
995
996static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
997 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +0000998 // want (a+b+c)/3, but we approx to avoid the divide
999 unsigned ave = (5 * (r + g + b) + b) >> 4;
1000 r = lerp(r, ave);
1001 g = lerp(g, ave);
1002 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001003 }
1004 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1005}
1006
reed@google.com73824072011-06-23 13:17:30 +00001007static uint16_t grayToRGB16(U8CPU gray) {
1008 SkASSERT(gray <= 255);
1009 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1010}
1011
1012static int bittst(const uint8_t data[], int bitOffset) {
1013 SkASSERT(bitOffset >= 0);
1014 int lowBit = data[bitOffset >> 3] >> (1 << (~bitOffset & 7));
1015 return lowBit & 1;
1016}
1017
reed@google.comea2333d2011-03-14 16:44:56 +00001018static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap) {
reed@google.comea2333d2011-03-14 16:44:56 +00001019 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001020 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001021 const size_t dstRB = glyph.rowBytes();
1022 const int width = glyph.fWidth;
1023 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001024
reed@google.com73824072011-06-23 13:17:30 +00001025 switch (bitmap.pixel_mode) {
1026 case FT_PIXEL_MODE_MONO: {
1027 for (int y = 0; y < glyph.fHeight; ++y) {
1028 for (int x = 0; x < width; ++x) {
1029 dst[x] = -bittst(src, x);
1030 }
1031 dst = (uint16_t*)((char*)dst + dstRB);
1032 src += bitmap.pitch;
1033 }
1034 } break;
1035 case FT_PIXEL_MODE_GRAY: {
1036 for (int y = 0; y < glyph.fHeight; ++y) {
1037 for (int x = 0; x < width; ++x) {
1038 dst[x] = grayToRGB16(src[x]);
1039 }
1040 dst = (uint16_t*)((char*)dst + dstRB);
1041 src += bitmap.pitch;
1042 }
1043 } break;
1044 default: {
1045 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1046 src += 3;
1047 for (int y = 0; y < glyph.fHeight; y++) {
1048 const uint8_t* triple = src;
1049 for (int x = 0; x < width; x++) {
1050 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1051 triple += 3;
1052 }
1053 src += bitmap.pitch;
1054 dst = (uint16_t*)((char*)dst + dstRB);
1055 }
1056 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001057 }
1058}
1059
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1061 SkAutoMutexAcquire ac(gFTMutex);
1062
1063 FT_Error err;
1064
1065 if (this->setupSize()) {
1066 goto ERROR;
1067 }
1068
1069 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1070 if (err != 0) {
1071 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1072 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1073 ERROR:
1074 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1075 return;
1076 }
1077
agl@chromium.org309485b2009-07-21 17:41:32 +00001078 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1079 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1080
reed@android.com8a1c16f2008-12-17 15:59:43 +00001081 switch ( fFace->glyph->format ) {
1082 case FT_GLYPH_FORMAT_OUTLINE: {
1083 FT_Outline* outline = &fFace->glyph->outline;
1084 FT_BBox bbox;
1085 FT_Bitmap target;
1086
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001087 if (fRec.fFlags & kEmbolden_Flag) {
1088 emboldenOutline(outline);
1089 }
1090
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001092 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001093 dx = glyph.getSubXFixed() >> 10;
1094 dy = glyph.getSubYFixed() >> 10;
1095 // negate dy since freetype-y-goes-up and skia-y-goes-down
1096 dy = -dy;
1097 }
1098 FT_Outline_Get_CBox(outline, &bbox);
1099 /*
1100 what we really want to do for subpixel is
1101 offset(dx, dy)
1102 compute_bounds
1103 offset(bbox & !63)
1104 but that is two calls to offset, so we do the following, which
1105 achieves the same thing with only one offset call.
1106 */
1107 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1108 dy - ((bbox.yMin + dy) & ~63));
1109
reed@android.comf5493692009-07-22 19:21:01 +00001110#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +00001111 if (lcdRenderMode) {
1112 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
1113 // we have to call FT_Render_Glyph and memcpy the image out.
1114 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1115 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
1116 FT_Render_Glyph(fFace->glyph, mode);
1117
1118 if (isVertical)
1119 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
1120 else
1121 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
1122
1123 break;
1124 }
1125#endif
1126
reed@google.comea2333d2011-03-14 16:44:56 +00001127 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1128 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
1129 copyFT2LCD16(glyph, fFace->glyph->bitmap);
1130 } else {
1131 target.width = glyph.fWidth;
1132 target.rows = glyph.fHeight;
1133 target.pitch = glyph.rowBytes();
1134 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1135 target.pixel_mode = compute_pixel_mode(
1136 (SkMask::Format)fRec.fMaskFormat);
1137 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138
reed@google.comea2333d2011-03-14 16:44:56 +00001139 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1140 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1141 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142 } break;
1143
1144 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001145 if (fRec.fFlags & kEmbolden_Flag) {
1146 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1147 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1150 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1151 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1152 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1153
1154 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1155 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001156
agl@chromium.org558434a2009-08-11 17:22:38 +00001157 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1158 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1159 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001160 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1161 unsigned dstRowBytes = glyph.rowBytes();
1162 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1163 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1164
1165 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1166 memcpy(dst, src, minRowBytes);
1167 memset(dst + minRowBytes, 0, extraRowBytes);
1168 src += srcRowBytes;
1169 dst += dstRowBytes;
1170 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001171 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +00001172 (glyph.fMaskFormat == SkMask::kA8_Format ||
1173 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1174 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001175 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1176 uint8_t byte = 0;
1177 int bits = 0;
1178 const uint8_t* src_row = src;
1179 uint8_t* dst_row = dst;
1180
1181 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1182 if (!bits) {
1183 byte = *src_row++;
1184 bits = 8;
1185 }
1186
1187 *dst_row++ = byte & 0x80 ? 0xff : 0;
1188 bits--;
1189 byte <<= 1;
1190 }
1191
1192 src += fFace->glyph->bitmap.pitch;
1193 dst += glyph.rowBytes();
1194 }
reed@google.com73824072011-06-23 13:17:30 +00001195 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1196 copyFT2LCD16(glyph, fFace->glyph->bitmap);
agl@chromium.org558434a2009-08-11 17:22:38 +00001197 } else {
reed@google.com73824072011-06-23 13:17:30 +00001198 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 }
agl@chromium.org309485b2009-07-21 17:41:32 +00001200
1201 if (lcdRenderMode)
1202 glyph.expandA8ToLCD();
1203
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 } break;
1205
1206 default:
1207 SkASSERT(!"unknown glyph format");
1208 goto ERROR;
1209 }
1210}
1211
1212///////////////////////////////////////////////////////////////////////////////
1213
1214#define ft2sk(x) SkFixedToScalar((x) << 10)
1215
reed@android.com6f252972009-01-14 16:46:16 +00001216#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 #define CONST_PARAM const
1218#else // older freetype doesn't use const here
1219 #define CONST_PARAM
1220#endif
1221
1222static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1223 SkPath* path = (SkPath*)ctx;
1224 path->close(); // to close the previous contour (if any)
1225 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1226 return 0;
1227}
1228
1229static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1230 SkPath* path = (SkPath*)ctx;
1231 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1232 return 0;
1233}
1234
1235static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1236 void* ctx) {
1237 SkPath* path = (SkPath*)ctx;
1238 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1239 return 0;
1240}
1241
1242static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1243 CONST_PARAM FT_Vector* pt2, void* ctx) {
1244 SkPath* path = (SkPath*)ctx;
1245 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1246 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1247 return 0;
1248}
1249
1250void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1251 SkPath* path) {
1252 SkAutoMutexAcquire ac(gFTMutex);
1253
1254 SkASSERT(&glyph && path);
1255
1256 if (this->setupSize()) {
1257 path->reset();
1258 return;
1259 }
1260
1261 uint32_t flags = fLoadGlyphFlags;
1262 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1263 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1264
1265 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1266
1267 if (err != 0) {
1268 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1269 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1270 path->reset();
1271 return;
1272 }
1273
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001274 if (fRec.fFlags & kEmbolden_Flag) {
1275 emboldenOutline(&fFace->glyph->outline);
1276 }
1277
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 FT_Outline_Funcs funcs;
1279
1280 funcs.move_to = move_proc;
1281 funcs.line_to = line_proc;
1282 funcs.conic_to = quad_proc;
1283 funcs.cubic_to = cubic_proc;
1284 funcs.shift = 0;
1285 funcs.delta = 0;
1286
1287 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1288
1289 if (err != 0) {
1290 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1291 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1292 path->reset();
1293 return;
1294 }
1295
1296 path->close();
1297}
1298
1299void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1300 SkPaint::FontMetrics* my) {
1301 if (NULL == mx && NULL == my) {
1302 return;
1303 }
1304
1305 SkAutoMutexAcquire ac(gFTMutex);
1306
1307 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001308 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001310 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311 }
1312 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001313 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314 }
1315 return;
1316 }
1317
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001318 FT_Face face = fFace;
1319 int upem = face->units_per_EM;
1320 if (upem <= 0) {
1321 goto ERROR;
1322 }
1323
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001324 SkPoint pts[6];
1325 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 SkFixed scaleY = fScaleY;
1327 SkFixed mxy = fMatrix22.xy;
1328 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001329 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1330 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001332 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001333 if (leading < 0) {
1334 leading = 0;
1335 }
1336
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001337 // Try to get the OS/2 table from the font. This contains the specific
1338 // average font width metrics which Windows uses.
1339 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1340
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341 ys[0] = -face->bbox.yMax;
1342 ys[1] = -face->ascender;
1343 ys[2] = -face->descender;
1344 ys[3] = -face->bbox.yMin;
1345 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001346 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1347
1348 SkScalar x_height;
1349 if (os2 && os2->sxHeight) {
1350 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1351 } else {
1352 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1353 if (x_glyph) {
1354 FT_BBox bbox;
1355 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001356 if (fRec.fFlags & kEmbolden_Flag) {
1357 emboldenOutline(&fFace->glyph->outline);
1358 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001359 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1360 x_height = SkIntToScalar(bbox.yMax) / 64;
1361 } else {
1362 x_height = 0;
1363 }
1364 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001365
1366 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001367 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1369 SkFixed x = SkFixedMul(mxy, y);
1370 y = SkFixedMul(myy, y);
1371 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1372 }
1373
1374 if (mx) {
1375 mx->fTop = pts[0].fX;
1376 mx->fAscent = pts[1].fX;
1377 mx->fDescent = pts[2].fX;
1378 mx->fBottom = pts[3].fX;
1379 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001380 mx->fAvgCharWidth = pts[5].fX;
1381 mx->fXMin = xmin;
1382 mx->fXMax = xmax;
1383 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 }
1385 if (my) {
1386 my->fTop = pts[0].fY;
1387 my->fAscent = pts[1].fY;
1388 my->fDescent = pts[2].fY;
1389 my->fBottom = pts[3].fY;
1390 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001391 my->fAvgCharWidth = pts[5].fY;
1392 my->fXMin = xmin;
1393 my->fXMax = xmax;
1394 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395 }
1396}
1397
1398////////////////////////////////////////////////////////////////////////
1399////////////////////////////////////////////////////////////////////////
1400
1401SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001402 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1403 if (!c->success()) {
1404 SkDELETE(c);
1405 c = NULL;
1406 }
1407 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408}
1409
1410///////////////////////////////////////////////////////////////////////////////
1411
1412/* Export this so that other parts of our FonttHost port can make use of our
1413 ability to extract the name+style from a stream, using FreeType's api.
1414*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001415SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1416 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001417 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001418 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001419 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001420 return SkTypeface::kNormal;
1421 }
1422
1423 FT_Open_Args args;
1424 memset(&args, 0, sizeof(args));
1425
1426 const void* memoryBase = stream->getMemoryBase();
1427 FT_StreamRec streamRec;
1428
1429 if (NULL != memoryBase) {
1430 args.flags = FT_OPEN_MEMORY;
1431 args.memory_base = (const FT_Byte*)memoryBase;
1432 args.memory_size = stream->getLength();
1433 } else {
1434 memset(&streamRec, 0, sizeof(streamRec));
1435 streamRec.size = stream->read(NULL, 0);
1436 streamRec.descriptor.pointer = stream;
1437 streamRec.read = sk_stream_read;
1438 streamRec.close = sk_stream_close;
1439
1440 args.flags = FT_OPEN_STREAM;
1441 args.stream = &streamRec;
1442 }
1443
1444 FT_Face face;
1445 if (FT_Open_Face(library, &args, 0, &face)) {
1446 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001447 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001448 return SkTypeface::kNormal;
1449 }
1450
1451 name->set(face->family_name);
1452 int style = SkTypeface::kNormal;
1453
1454 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1455 style |= SkTypeface::kBold;
1456 }
1457 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1458 style |= SkTypeface::kItalic;
1459 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001460 if (isFixedWidth) {
1461 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1462 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001463
1464 FT_Done_Face(face);
1465 FT_Done_FreeType(library);
1466 return (SkTypeface::Style)style;
1467}