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