blob: be5446e0f0384e1e7f8738c374449911286c51f0 [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 }
reed@google.com8ed436c2011-07-21 14:12:36 +0000633 // rotated text looks bad with hinting, so we disable it as needed
634 if (!isAxisAligned(*rec)) {
635 h = SkPaint::kNo_Hinting;
636 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000637 rec->setHinting(h);
638}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000639
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000640#ifdef ANDROID
641uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
642 SkAutoMutexAcquire ac(gFTMutex);
643 SkFaceRec *rec = ref_ft_face(fontID);
644 uint16_t unitsPerEm = 0;
645
646 if (rec != NULL && rec->fFace != NULL) {
647 unitsPerEm = rec->fFace->units_per_EM;
648 unref_ft_face(rec->fFace);
649 }
650
651 return (uint32_t)unitsPerEm;
652}
653#endif
654
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000656 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657 SkAutoMutexAcquire ac(gFTMutex);
658
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000660 if (!InitFreetype()) {
661 sk_throw();
662 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 }
664 ++gFTCount;
665
666 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000667 fFTSize = NULL;
668 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000670 if (NULL == fFaceRec) {
671 return;
672 }
673 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674
675 // compute our factors from the record
676
677 SkMatrix m;
678
679 fRec.getSingleMatrix(&m);
680
681#ifdef DUMP_STRIKE_CREATION
682 SkString keyString;
683 SkFontHost::GetDescriptorKeyString(desc, &keyString);
684 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
685 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
686 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
687 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000688 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000689#endif
690
691 // now compute our scale factors
692 SkScalar sx = m.getScaleX();
693 SkScalar sy = m.getScaleY();
694
695 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
696 // sort of give up on hinting
697 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
698 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
699 sx = sy = SkScalarAve(sx, sy);
700
701 SkScalar inv = SkScalarInvert(sx);
702
703 // flip the skew elements to go from our Y-down system to FreeType's
704 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
705 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
706 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
707 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
708 } else {
709 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
710 fMatrix22.xy = fMatrix22.yx = 0;
711 }
712
713 fScaleX = SkScalarToFixed(sx);
714 fScaleY = SkScalarToFixed(sy);
715
716 // compute the flags we send to Load_Glyph
717 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000718 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719
agl@chromium.org70a303f2010-05-10 14:15:50 +0000720 if (SkMask::kBW_Format == fRec.fMaskFormat) {
721 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
722 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000723 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000724 loadFlags = FT_LOAD_NO_HINTING;
reed@google.comeffc5012011-06-27 16:44:46 +0000725 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000726 } else {
727 switch (fRec.getHinting()) {
728 case SkPaint::kNo_Hinting:
729 loadFlags = FT_LOAD_NO_HINTING;
730 break;
731 case SkPaint::kSlight_Hinting:
732 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
733 break;
734 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000735 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
736 loadFlags = FT_LOAD_FORCE_AUTOHINT;
737 else
738 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000739 break;
740 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000741 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
742 loadFlags = FT_LOAD_FORCE_AUTOHINT;
743 break;
744 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000745 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000746 if (isLCD(fRec)) {
747 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
748 loadFlags = FT_LOAD_TARGET_LCD_V;
749 } else {
750 loadFlags = FT_LOAD_TARGET_LCD;
751 }
reed@google.comea2333d2011-03-14 16:44:56 +0000752 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000753 break;
754 default:
755 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
756 break;
757 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000758 }
759
reed@google.comeffc5012011-06-27 16:44:46 +0000760 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000761 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000762 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000763
reed@google.com96a9f7912011-05-06 11:49:30 +0000764 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
765 // advances, as fontconfig and cairo do.
766 // See http://code.google.com/p/skia/issues/detail?id=222.
767 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
768
reed@android.come4d0bc02009-07-24 19:53:20 +0000769 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 }
771
772 // now create the FT_Size
773
774 {
775 FT_Error err;
776
777 err = FT_New_Size(fFace, &fFTSize);
778 if (err != 0) {
779 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
780 fFaceRec->fFontID, fScaleX, fScaleY, err));
781 fFace = NULL;
782 return;
783 }
784
785 err = FT_Activate_Size(fFTSize);
786 if (err != 0) {
787 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
788 fFaceRec->fFontID, fScaleX, fScaleY, err));
789 fFTSize = NULL;
790 }
791
792 err = FT_Set_Char_Size( fFace,
793 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
794 72, 72);
795 if (err != 0) {
796 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
797 fFaceRec->fFontID, fScaleX, fScaleY, err));
798 fFace = NULL;
799 return;
800 }
801
802 FT_Set_Transform( fFace, &fMatrix22, NULL);
803 }
804}
805
806SkScalerContext_FreeType::~SkScalerContext_FreeType() {
807 if (fFTSize != NULL) {
808 FT_Done_Size(fFTSize);
809 }
810
811 SkAutoMutexAcquire ac(gFTMutex);
812
813 if (fFace != NULL) {
814 unref_ft_face(fFace);
815 }
816 if (--gFTCount == 0) {
817// SkDEBUGF(("FT_Done_FreeType\n"));
818 FT_Done_FreeType(gFTLibrary);
819 SkDEBUGCODE(gFTLibrary = NULL;)
820 }
821}
822
823/* We call this before each use of the fFace, since we may be sharing
824 this face with other context (at different sizes).
825*/
826FT_Error SkScalerContext_FreeType::setupSize() {
827 /* In the off-chance that a font has been removed, we want to error out
828 right away, so call resolve just to be sure.
829
830 TODO: perhaps we can skip this, by walking the global font cache and
831 killing all of the contexts when we know that a given fontID is going
832 away...
833 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000834 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 return (FT_Error)-1;
836 }
837
838 FT_Error err = FT_Activate_Size(fFTSize);
839
840 if (err != 0) {
841 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
842 fFaceRec->fFontID, fScaleX, fScaleY, err));
843 fFTSize = NULL;
844 } else {
845 // seems we need to reset this every time (not sure why, but without it
846 // I get random italics from some other fFTSize)
847 FT_Set_Transform( fFace, &fMatrix22, NULL);
848 }
849 return err;
850}
851
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000852void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
853 FT_Pos strength;
854 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
855 / 24;
856 FT_Outline_Embolden(outline, strength);
857}
858
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000859unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 return fFace->num_glyphs;
861}
862
863uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
864 return SkToU16(FT_Get_Char_Index( fFace, uni ));
865}
866
reed@android.com9d3a9852010-01-08 14:07:42 +0000867SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
868 // iterate through each cmap entry, looking for matching glyph indices
869 FT_UInt glyphIndex;
870 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
871
872 while (glyphIndex != 0) {
873 if (glyphIndex == glyph) {
874 return charCode;
875 }
876 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
877 }
878
879 return 0;
880}
881
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
883 switch (format) {
884 case SkMask::kBW_Format:
885 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 case SkMask::kA8_Format:
887 default:
888 return FT_PIXEL_MODE_GRAY;
889 }
890}
891
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
893#ifdef FT_ADVANCES_H
894 /* unhinted and light hinted text have linearly scaled advances
895 * which are very cheap to compute with some font formats...
896 */
897 {
898 SkAutoMutexAcquire ac(gFTMutex);
899
900 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000901 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 return;
903 }
904
905 FT_Error error;
906 FT_Fixed advance;
907
908 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
909 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
910 &advance );
911 if (0 == error) {
912 glyph->fRsbDelta = 0;
913 glyph->fLsbDelta = 0;
914 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
915 glyph->fAdvanceY = 0;
916 return;
917 }
918 }
919#endif /* FT_ADVANCES_H */
920 /* otherwise, we need to load/hint the glyph, which is slower */
921 this->generateMetrics(glyph);
922 return;
923}
924
925void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
926 SkAutoMutexAcquire ac(gFTMutex);
927
928 glyph->fRsbDelta = 0;
929 glyph->fLsbDelta = 0;
930
931 FT_Error err;
932
933 if (this->setupSize()) {
934 goto ERROR;
935 }
936
937 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
938 if (err != 0) {
939 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
940 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
941 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000942 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943 return;
944 }
945
946 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000947 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 FT_BBox bbox;
949
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000950 if (fRec.fFlags & kEmbolden_Flag) {
951 emboldenOutline(&fFace->glyph->outline);
952 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
954
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000955 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956 int dx = glyph->getSubXFixed() >> 10;
957 int dy = glyph->getSubYFixed() >> 10;
958 // negate dy since freetype-y-goes-up and skia-y-goes-down
959 bbox.xMin += dx;
960 bbox.yMin -= dy;
961 bbox.xMax += dx;
962 bbox.yMax -= dy;
963 }
964
965 bbox.xMin &= ~63;
966 bbox.yMin &= ~63;
967 bbox.xMax = (bbox.xMax + 63) & ~63;
968 bbox.yMax = (bbox.yMax + 63) & ~63;
969
970 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
971 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
972 glyph->fTop = -SkToS16(bbox.yMax >> 6);
973 glyph->fLeft = SkToS16(bbox.xMin >> 6);
974 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000975 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976
977 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000978 if (fRec.fFlags & kEmbolden_Flag) {
979 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
980 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
981 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000982 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
983 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
984 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
985 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
986 break;
987
988 default:
989 SkASSERT(!"unknown glyph format");
990 goto ERROR;
991 }
992
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000993 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
995 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
996 if (fRec.fFlags & kDevKernText_Flag) {
997 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
998 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
999 }
1000 } else {
1001 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1002 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1003 }
1004
1005#ifdef ENABLE_GLYPH_SPEW
1006 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1007 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1008#endif
1009}
1010
reed@google.combde3c8e2011-05-18 11:58:10 +00001011static int lerp(int start, int end) {
1012 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1013 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001014}
1015
1016static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1017 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001018 // want (a+b+c)/3, but we approx to avoid the divide
1019 unsigned ave = (5 * (r + g + b) + b) >> 4;
1020 r = lerp(r, ave);
1021 g = lerp(g, ave);
1022 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001023 }
1024 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1025}
1026
reed@google.com73824072011-06-23 13:17:30 +00001027static uint16_t grayToRGB16(U8CPU gray) {
1028 SkASSERT(gray <= 255);
1029 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1030}
1031
1032static int bittst(const uint8_t data[], int bitOffset) {
1033 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001034 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001035 return lowBit & 1;
1036}
1037
reed@google.comeffc5012011-06-27 16:44:46 +00001038static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1039 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001040 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001041 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001042 const size_t dstRB = glyph.rowBytes();
1043 const int width = glyph.fWidth;
1044 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001045
reed@google.com73824072011-06-23 13:17:30 +00001046 switch (bitmap.pixel_mode) {
1047 case FT_PIXEL_MODE_MONO: {
1048 for (int y = 0; y < glyph.fHeight; ++y) {
1049 for (int x = 0; x < width; ++x) {
1050 dst[x] = -bittst(src, x);
1051 }
1052 dst = (uint16_t*)((char*)dst + dstRB);
1053 src += bitmap.pitch;
1054 }
1055 } break;
1056 case FT_PIXEL_MODE_GRAY: {
1057 for (int y = 0; y < glyph.fHeight; ++y) {
1058 for (int x = 0; x < width; ++x) {
1059 dst[x] = grayToRGB16(src[x]);
1060 }
1061 dst = (uint16_t*)((char*)dst + dstRB);
1062 src += bitmap.pitch;
1063 }
1064 } break;
1065 default: {
1066 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1067 src += 3;
1068 for (int y = 0; y < glyph.fHeight; y++) {
1069 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001070 if (lcdIsBGR) {
1071 for (int x = 0; x < width; x++) {
1072 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1073 triple += 3;
1074 }
1075 } else {
1076 for (int x = 0; x < width; x++) {
1077 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1078 triple += 3;
1079 }
reed@google.com73824072011-06-23 13:17:30 +00001080 }
1081 src += bitmap.pitch;
1082 dst = (uint16_t*)((char*)dst + dstRB);
1083 }
1084 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001085 }
1086}
1087
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1089 SkAutoMutexAcquire ac(gFTMutex);
1090
1091 FT_Error err;
1092
1093 if (this->setupSize()) {
1094 goto ERROR;
1095 }
1096
1097 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1098 if (err != 0) {
1099 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1100 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1101 ERROR:
1102 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1103 return;
1104 }
1105
1106 switch ( fFace->glyph->format ) {
1107 case FT_GLYPH_FORMAT_OUTLINE: {
1108 FT_Outline* outline = &fFace->glyph->outline;
1109 FT_BBox bbox;
1110 FT_Bitmap target;
1111
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001112 if (fRec.fFlags & kEmbolden_Flag) {
1113 emboldenOutline(outline);
1114 }
1115
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001117 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001118 dx = glyph.getSubXFixed() >> 10;
1119 dy = glyph.getSubYFixed() >> 10;
1120 // negate dy since freetype-y-goes-up and skia-y-goes-down
1121 dy = -dy;
1122 }
1123 FT_Outline_Get_CBox(outline, &bbox);
1124 /*
1125 what we really want to do for subpixel is
1126 offset(dx, dy)
1127 compute_bounds
1128 offset(bbox & !63)
1129 but that is two calls to offset, so we do the following, which
1130 achieves the same thing with only one offset call.
1131 */
1132 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1133 dy - ((bbox.yMin + dy) & ~63));
1134
reed@google.comea2333d2011-03-14 16:44:56 +00001135 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1136 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001137 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1138 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001139 } else {
1140 target.width = glyph.fWidth;
1141 target.rows = glyph.fHeight;
1142 target.pitch = glyph.rowBytes();
1143 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1144 target.pixel_mode = compute_pixel_mode(
1145 (SkMask::Format)fRec.fMaskFormat);
1146 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001147
reed@google.comea2333d2011-03-14 16:44:56 +00001148 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1149 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1150 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151 } break;
1152
1153 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001154 if (fRec.fFlags & kEmbolden_Flag) {
1155 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1156 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1157 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001158 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1159 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1160 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1161 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1162
1163 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1164 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165
agl@chromium.org558434a2009-08-11 17:22:38 +00001166 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1167 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1168 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001169 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1170 unsigned dstRowBytes = glyph.rowBytes();
1171 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1172 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1173
1174 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1175 memcpy(dst, src, minRowBytes);
1176 memset(dst + minRowBytes, 0, extraRowBytes);
1177 src += srcRowBytes;
1178 dst += dstRowBytes;
1179 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001180 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001181 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001182 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1183 uint8_t byte = 0;
1184 int bits = 0;
1185 const uint8_t* src_row = src;
1186 uint8_t* dst_row = dst;
1187
1188 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1189 if (!bits) {
1190 byte = *src_row++;
1191 bits = 8;
1192 }
1193
1194 *dst_row++ = byte & 0x80 ? 0xff : 0;
1195 bits--;
1196 byte <<= 1;
1197 }
1198
1199 src += fFace->glyph->bitmap.pitch;
1200 dst += glyph.rowBytes();
1201 }
reed@google.com73824072011-06-23 13:17:30 +00001202 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001203 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1204 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001205 } else {
reed@google.com73824072011-06-23 13:17:30 +00001206 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001207 }
1208 } break;
1209
1210 default:
1211 SkASSERT(!"unknown glyph format");
1212 goto ERROR;
1213 }
1214}
1215
1216///////////////////////////////////////////////////////////////////////////////
1217
1218#define ft2sk(x) SkFixedToScalar((x) << 10)
1219
reed@android.com6f252972009-01-14 16:46:16 +00001220#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 #define CONST_PARAM const
1222#else // older freetype doesn't use const here
1223 #define CONST_PARAM
1224#endif
1225
1226static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1227 SkPath* path = (SkPath*)ctx;
1228 path->close(); // to close the previous contour (if any)
1229 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1230 return 0;
1231}
1232
1233static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1234 SkPath* path = (SkPath*)ctx;
1235 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1236 return 0;
1237}
1238
1239static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1240 void* ctx) {
1241 SkPath* path = (SkPath*)ctx;
1242 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1243 return 0;
1244}
1245
1246static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1247 CONST_PARAM FT_Vector* pt2, void* ctx) {
1248 SkPath* path = (SkPath*)ctx;
1249 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1250 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1251 return 0;
1252}
1253
1254void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1255 SkPath* path) {
1256 SkAutoMutexAcquire ac(gFTMutex);
1257
1258 SkASSERT(&glyph && path);
1259
1260 if (this->setupSize()) {
1261 path->reset();
1262 return;
1263 }
1264
1265 uint32_t flags = fLoadGlyphFlags;
1266 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1267 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1268
1269 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1270
1271 if (err != 0) {
1272 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1273 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1274 path->reset();
1275 return;
1276 }
1277
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001278 if (fRec.fFlags & kEmbolden_Flag) {
1279 emboldenOutline(&fFace->glyph->outline);
1280 }
1281
reed@android.com8a1c16f2008-12-17 15:59:43 +00001282 FT_Outline_Funcs funcs;
1283
1284 funcs.move_to = move_proc;
1285 funcs.line_to = line_proc;
1286 funcs.conic_to = quad_proc;
1287 funcs.cubic_to = cubic_proc;
1288 funcs.shift = 0;
1289 funcs.delta = 0;
1290
1291 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1292
1293 if (err != 0) {
1294 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1295 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1296 path->reset();
1297 return;
1298 }
1299
1300 path->close();
1301}
1302
1303void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1304 SkPaint::FontMetrics* my) {
1305 if (NULL == mx && NULL == my) {
1306 return;
1307 }
1308
1309 SkAutoMutexAcquire ac(gFTMutex);
1310
1311 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001312 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001314 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001315 }
1316 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001317 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 }
1319 return;
1320 }
1321
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001322 FT_Face face = fFace;
1323 int upem = face->units_per_EM;
1324 if (upem <= 0) {
1325 goto ERROR;
1326 }
1327
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001328 SkPoint pts[6];
1329 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330 SkFixed scaleY = fScaleY;
1331 SkFixed mxy = fMatrix22.xy;
1332 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001333 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1334 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001336 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001337 if (leading < 0) {
1338 leading = 0;
1339 }
1340
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001341 // Try to get the OS/2 table from the font. This contains the specific
1342 // average font width metrics which Windows uses.
1343 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1344
reed@android.com8a1c16f2008-12-17 15:59:43 +00001345 ys[0] = -face->bbox.yMax;
1346 ys[1] = -face->ascender;
1347 ys[2] = -face->descender;
1348 ys[3] = -face->bbox.yMin;
1349 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001350 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1351
1352 SkScalar x_height;
1353 if (os2 && os2->sxHeight) {
1354 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1355 } else {
1356 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1357 if (x_glyph) {
1358 FT_BBox bbox;
1359 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001360 if (fRec.fFlags & kEmbolden_Flag) {
1361 emboldenOutline(&fFace->glyph->outline);
1362 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001363 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1364 x_height = SkIntToScalar(bbox.yMax) / 64;
1365 } else {
1366 x_height = 0;
1367 }
1368 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369
1370 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001371 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1373 SkFixed x = SkFixedMul(mxy, y);
1374 y = SkFixedMul(myy, y);
1375 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1376 }
1377
1378 if (mx) {
1379 mx->fTop = pts[0].fX;
1380 mx->fAscent = pts[1].fX;
1381 mx->fDescent = pts[2].fX;
1382 mx->fBottom = pts[3].fX;
1383 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001384 mx->fAvgCharWidth = pts[5].fX;
1385 mx->fXMin = xmin;
1386 mx->fXMax = xmax;
1387 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 }
1389 if (my) {
1390 my->fTop = pts[0].fY;
1391 my->fAscent = pts[1].fY;
1392 my->fDescent = pts[2].fY;
1393 my->fBottom = pts[3].fY;
1394 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001395 my->fAvgCharWidth = pts[5].fY;
1396 my->fXMin = xmin;
1397 my->fXMax = xmax;
1398 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001399 }
1400}
1401
1402////////////////////////////////////////////////////////////////////////
1403////////////////////////////////////////////////////////////////////////
1404
1405SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001406 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1407 if (!c->success()) {
1408 SkDELETE(c);
1409 c = NULL;
1410 }
1411 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001412}
1413
1414///////////////////////////////////////////////////////////////////////////////
1415
1416/* Export this so that other parts of our FonttHost port can make use of our
1417 ability to extract the name+style from a stream, using FreeType's api.
1418*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001419SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1420 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001422 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001423 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424 return SkTypeface::kNormal;
1425 }
1426
1427 FT_Open_Args args;
1428 memset(&args, 0, sizeof(args));
1429
1430 const void* memoryBase = stream->getMemoryBase();
1431 FT_StreamRec streamRec;
1432
1433 if (NULL != memoryBase) {
1434 args.flags = FT_OPEN_MEMORY;
1435 args.memory_base = (const FT_Byte*)memoryBase;
1436 args.memory_size = stream->getLength();
1437 } else {
1438 memset(&streamRec, 0, sizeof(streamRec));
1439 streamRec.size = stream->read(NULL, 0);
1440 streamRec.descriptor.pointer = stream;
1441 streamRec.read = sk_stream_read;
1442 streamRec.close = sk_stream_close;
1443
1444 args.flags = FT_OPEN_STREAM;
1445 args.stream = &streamRec;
1446 }
1447
1448 FT_Face face;
1449 if (FT_Open_Face(library, &args, 0, &face)) {
1450 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001451 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001452 return SkTypeface::kNormal;
1453 }
1454
1455 name->set(face->family_name);
1456 int style = SkTypeface::kNormal;
1457
1458 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1459 style |= SkTypeface::kBold;
1460 }
1461 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1462 style |= SkTypeface::kItalic;
1463 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001464 if (isFixedWidth) {
1465 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1466 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001467
1468 FT_Done_Face(face);
1469 FT_Done_FreeType(library);
1470 return (SkTypeface::Style)style;
1471}