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