blob: 8928b1e14f2712f9f571c13d50cab227cdcea47f [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.comc5181342011-05-17 20:52:46 +000075// SK_FREETYPE_LCD_LERP should be 0...256, where 0 means no color reduction
76// and 256 means 100% color reduction (e.g. gray)
77//
78#ifndef SK_FREETYPE_LCD_LERP
79 #define SK_FREETYPE_LCD_LERP 128
80#endif
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082//////////////////////////////////////////////////////////////////////////
83
84struct SkFaceRec;
85
86static SkMutex gFTMutex;
87static int gFTCount;
88static FT_Library gFTLibrary;
89static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000090static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
91static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93/////////////////////////////////////////////////////////////////////////
94
agl@chromium.orge76073b2010-06-04 20:31:17 +000095// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
96// This value was chosen by eyeballing the result in Firefox and trying to match it.
97static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
98
agl@chromium.org309485b2009-07-21 17:41:32 +000099static bool
100InitFreetype() {
101 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000102 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000103 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000104 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000105
agl@chromium.org309485b2009-07-21 17:41:32 +0000106 // Setup LCD filtering. This reduces colour fringes for LCD rendered
107 // glyphs.
108 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000109 gLCDSupport = err == 0;
reed@android.com61608aa2009-07-31 14:52:54 +0000110 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000111
112 return true;
113}
114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115class SkScalerContext_FreeType : public SkScalerContext {
116public:
117 SkScalerContext_FreeType(const SkDescriptor* desc);
118 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000119
reed@android.com62900b42009-02-11 15:07:19 +0000120 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000121 return fFaceRec != NULL &&
122 fFTSize != NULL &&
123 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000124 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
126protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000127 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 virtual uint16_t generateCharToGlyph(SkUnichar uni);
129 virtual void generateAdvance(SkGlyph* glyph);
130 virtual void generateMetrics(SkGlyph* glyph);
131 virtual void generateImage(const SkGlyph& glyph);
132 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
133 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
134 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000135 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137private:
138 SkFaceRec* fFaceRec;
139 FT_Face fFace; // reference to shared face in gFaceRecHead
140 FT_Size fFTSize; // our own copy
141 SkFixed fScaleX, fScaleY;
142 FT_Matrix fMatrix22;
143 uint32_t fLoadGlyphFlags;
144
145 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000146 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147};
148
149///////////////////////////////////////////////////////////////////////////
150///////////////////////////////////////////////////////////////////////////
151
152#include "SkStream.h"
153
154struct SkFaceRec {
155 SkFaceRec* fNext;
156 FT_Face fFace;
157 FT_StreamRec fFTStream;
158 SkStream* fSkStream;
159 uint32_t fRefCnt;
160 uint32_t fFontID;
161
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000162 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 SkFaceRec(SkStream* strm, uint32_t fontID);
164 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000165 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 }
167};
168
169extern "C" {
170 static unsigned long sk_stream_read(FT_Stream stream,
171 unsigned long offset,
172 unsigned char* buffer,
173 unsigned long count ) {
174 SkStream* str = (SkStream*)stream->descriptor.pointer;
175
176 if (count) {
177 if (!str->rewind()) {
178 return 0;
179 } else {
180 unsigned long ret;
181 if (offset) {
182 ret = str->read(NULL, offset);
183 if (ret != offset) {
184 return 0;
185 }
186 }
187 ret = str->read(buffer, count);
188 if (ret != count) {
189 return 0;
190 }
191 count = ret;
192 }
193 }
194 return count;
195 }
196
197 static void sk_stream_close( FT_Stream stream) {}
198}
199
200SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
201 : fSkStream(strm), fFontID(fontID) {
202// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
203
reed@android.com4516f472009-06-29 16:25:36 +0000204 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 fFTStream.size = fSkStream->getLength();
206 fFTStream.descriptor.pointer = fSkStream;
207 fFTStream.read = sk_stream_read;
208 fFTStream.close = sk_stream_close;
209}
210
reed@android.com62900b42009-02-11 15:07:19 +0000211// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212static SkFaceRec* ref_ft_face(uint32_t fontID) {
213 SkFaceRec* rec = gFaceRecHead;
214 while (rec) {
215 if (rec->fFontID == fontID) {
216 SkASSERT(rec->fFace);
217 rec->fRefCnt += 1;
218 return rec;
219 }
220 rec = rec->fNext;
221 }
222
223 SkStream* strm = SkFontHost::OpenStream(fontID);
224 if (NULL == strm) {
225 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 return 0;
227 }
228
229 // this passes ownership of strm to the rec
230 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
231
232 FT_Open_Args args;
233 memset(&args, 0, sizeof(args));
234 const void* memoryBase = strm->getMemoryBase();
235
236 if (NULL != memoryBase) {
237//printf("mmap(%s)\n", keyString.c_str());
238 args.flags = FT_OPEN_MEMORY;
239 args.memory_base = (const FT_Byte*)memoryBase;
240 args.memory_size = strm->getLength();
241 } else {
242//printf("fopen(%s)\n", keyString.c_str());
243 args.flags = FT_OPEN_STREAM;
244 args.stream = &rec->fFTStream;
245 }
246
agl@chromium.org61a678a2010-08-06 18:08:18 +0000247 int face_index;
248 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
249 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
250 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251
252 if (err) { // bad filename, try the default font
253 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
254 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 return 0;
256 } else {
257 SkASSERT(rec->fFace);
258 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
259 rec->fNext = gFaceRecHead;
260 gFaceRecHead = rec;
261 rec->fRefCnt = 1;
262 return rec;
263 }
264}
265
266static void unref_ft_face(FT_Face face) {
267 SkFaceRec* rec = gFaceRecHead;
268 SkFaceRec* prev = NULL;
269 while (rec) {
270 SkFaceRec* next = rec->fNext;
271 if (rec->fFace == face) {
272 if (--rec->fRefCnt == 0) {
273 if (prev) {
274 prev->fNext = next;
275 } else {
276 gFaceRecHead = next;
277 }
278 FT_Done_Face(face);
279 SkDELETE(rec);
280 }
281 return;
282 }
283 prev = rec;
284 rec = next;
285 }
286 SkASSERT("shouldn't get here, face not in list");
287}
288
289///////////////////////////////////////////////////////////////////////////
290
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000291// Work around for old versions of freetype.
292static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
293 FT_Int32 loadFlags, FT_Fixed* advances) {
294#ifdef FT_ADVANCES_H
295 return FT_Get_Advances(face, start, count, loadFlags, advances);
296#else
297 if (!face || start >= face->num_glyphs ||
298 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
299 return 6; // "Invalid argument."
300 }
301 if (count == 0)
302 return 0;
303
304 for (int i = 0; i < count; i++) {
305 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
306 if (err)
307 return err;
308 advances[i] = face->glyph->advance.x;
309 }
310
311 return 0;
312#endif
313}
314
315static bool canEmbed(FT_Face face) {
316#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
317 FT_UShort fsType = FT_Get_FSType_Flags(face);
318 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
319 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
320#else
321 // No embedding is 0x2 and bitmap embedding only is 0x200.
322 TT_OS2* os2_table;
323 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
324 return (os2_table->fsType & 0x202) == 0;
325 }
326 return false; // We tried, fail safe.
327#endif
328}
329
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000330static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
331 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
332 if (!glyph_id)
333 return false;
334 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
335 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
336 return true;
337}
338
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000339static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000340 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000341 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
342 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000343 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000344 SkASSERT(data);
345 *data = advance;
346 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000347}
348
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000349static void populate_glyph_to_unicode(FT_Face& face,
350 SkTDArray<SkUnichar>* glyphToUnicode) {
351 // Check and see if we have Unicode cmaps.
352 for (int i = 0; i < face->num_charmaps; ++i) {
353 // CMaps known to support Unicode:
354 // Platform ID Encoding ID Name
355 // ----------- ----------- -----------------------------------
356 // 0 0,1 Apple Unicode
357 // 0 3 Apple Unicode 2.0 (preferred)
358 // 3 1 Microsoft Unicode UCS-2
359 // 3 10 Microsoft Unicode UCS-4 (preferred)
360 //
361 // See Apple TrueType Reference Manual
362 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
363 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
364 // Microsoft OpenType Specification
365 // http://www.microsoft.com/typography/otspec/cmap.htm
366
367 FT_UShort platformId = face->charmaps[i]->platform_id;
368 FT_UShort encodingId = face->charmaps[i]->encoding_id;
369
370 if (platformId != 0 && platformId != 3) {
371 continue;
372 }
373 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
374 continue;
375 }
376 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
377 (platformId == 0 && encodingId == 3));
378
379 FT_Set_Charmap(face, face->charmaps[i]);
380 if (glyphToUnicode->isEmpty()) {
381 glyphToUnicode->setCount(face->num_glyphs);
382 memset(glyphToUnicode->begin(), 0,
383 sizeof(SkUnichar) * face->num_glyphs);
384 }
385
386 // Iterate through each cmap entry.
387 FT_UInt glyphIndex;
388 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
389 glyphIndex != 0;
390 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
391 if (charCode &&
392 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
393 (*glyphToUnicode)[glyphIndex] = charCode;
394 }
395 }
396 }
397}
398
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000399// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000400SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000401 uint32_t fontID,
402 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000403#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000404 return NULL;
405#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000406 SkAutoMutexAcquire ac(gFTMutex);
407 FT_Library libInit = NULL;
408 if (gFTCount == 0) {
409 if (!InitFreetype())
410 sk_throw();
411 libInit = gFTLibrary;
412 }
413 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
414 SkFaceRec* rec = ref_ft_face(fontID);
415 if (NULL == rec)
416 return NULL;
417 FT_Face face = rec->fFace;
418
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000419 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
420 info->fFontName.set(FT_Get_Postscript_Name(face));
421 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
422 info->fLastGlyphID = face->num_glyphs - 1;
423 info->fEmSize = 1000;
424
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000425 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000426 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000427 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000428 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000429 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000430 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431 cid = true;
432 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000433 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000434 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000435 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000436 cid = true;
437 TT_Header* ttHeader;
438 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
439 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000440 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000441 }
442 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000444 info->fStyle = 0;
445 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000446 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000447 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000448 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000449 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
450 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000451 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
452
453 PS_FontInfoRec ps_info;
454 TT_Postscript* tt_info;
455 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
456 info->fItalicAngle = ps_info.italic_angle;
457 } else if ((tt_info =
458 (TT_Postscript*)FT_Get_Sfnt_Table(face,
459 ft_sfnt_post)) != NULL) {
460 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
461 } else {
462 info->fItalicAngle = 0;
463 }
464
465 info->fAscent = face->ascender;
466 info->fDescent = face->descender;
467
468 // Figure out a good guess for StemV - Min width of i, I, !, 1.
469 // This probably isn't very good with an italic font.
470 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000471 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000472 char stem_chars[] = {'i', 'I', '!', '1'};
473 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
474 FT_BBox bbox;
475 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
476 int16_t width = bbox.xMax - bbox.xMin;
477 if (width > 0 && width < min_width) {
478 min_width = width;
479 info->fStemV = min_width;
480 }
481 }
482 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483
484 TT_PCLT* pclt_info;
485 TT_OS2* os2_table;
486 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
487 info->fCapHeight = pclt_info->CapHeight;
488 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
489 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000490 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000491 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000492 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000493 } else if ((os2_table =
494 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
495 info->fCapHeight = os2_table->sCapHeight;
496 } else {
497 // Figure out a good guess for CapHeight: average the height of M and X.
498 FT_BBox m_bbox, x_bbox;
499 bool got_m, got_x;
500 got_m = GetLetterCBox(face, 'M', &m_bbox);
501 got_x = GetLetterCBox(face, 'X', &x_bbox);
502 if (got_m && got_x) {
503 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
504 x_bbox.yMin) / 2;
505 } else if (got_m && !got_x) {
506 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
507 } else if (!got_m && got_x) {
508 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
509 }
510 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000511
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000512 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
513 face->bbox.xMax, face->bbox.yMin);
514
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000515 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
516 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
517 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
518 }
519
520 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000521 if (FT_IS_FIXED_WIDTH(face)) {
522 appendRange(&info->fGlyphWidths, 0);
523 int16_t advance = face->max_advance_width;
524 info->fGlyphWidths->fAdvance.append(1, &advance);
525 finishRange(info->fGlyphWidths.get(), 0,
526 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000527 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000528 appendRange(&info->fGlyphWidths, 0);
529 // So as to not blow out the stack, get advances in batches.
530 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
531 FT_Fixed advances[128];
532 int advanceCount = 128;
533 if (gID + advanceCount > face->num_glyphs)
534 advanceCount = face->num_glyphs - gID + 1;
535 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
536 advances);
537 for (int i = 0; i < advanceCount; i++) {
538 int16_t advance = advances[gID + i];
539 info->fGlyphWidths->fAdvance.append(1, &advance);
540 }
541 }
542 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
543 SkAdvancedTypefaceMetrics::WidthRange::kRange);
544 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000545 info->fGlyphWidths.reset(
546 getAdvanceData(face, face->num_glyphs, &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000547 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000548 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000549
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000550 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
551 FT_HAS_VERTICAL(face)) {
552 SkASSERT(false); // Not implemented yet.
553 }
554
555 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
556 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
557 // Postscript fonts may contain more than 255 glyphs, so we end up
558 // using multiple font descriptions with a glyph ordering. Record
559 // the name of each glyph.
560 info->fGlyphNames.reset(
561 new SkAutoTArray<SkString>(face->num_glyphs));
562 for (int gID = 0; gID < face->num_glyphs; gID++) {
563 char glyphName[128]; // PS limit for names is 127 bytes.
564 FT_Get_Glyph_Name(face, gID, glyphName, 128);
565 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000566 }
567 }
568
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000569 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
570 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
571 face->num_charmaps) {
572 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
573 }
574
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000575 if (!canEmbed(face))
576 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
577
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000578 unref_ft_face(face);
579 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000580#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000581}
reed@google.com618ef5e2011-01-26 22:10:41 +0000582///////////////////////////////////////////////////////////////////////////
583
584void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
585 if (!gLCDSupportValid) {
586 InitFreetype();
587 FT_Done_FreeType(gFTLibrary);
588 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000589
reed@google.com8abde0a2011-03-14 17:45:33 +0000590 if (!gLCDSupport && (rec->isLCD() || SkMask::kLCD16_Format == rec->fMaskFormat)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000591 // If the runtime Freetype library doesn't support LCD mode, we disable
592 // it here.
593 rec->fMaskFormat = SkMask::kA8_Format;
594 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000595
reed@google.com618ef5e2011-01-26 22:10:41 +0000596 SkPaint::Hinting h = rec->getHinting();
597 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
598 // collapse full->normal hinting if we're not doing LCD
599 h = SkPaint::kNormal_Hinting;
600 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
601 SkPaint::kNo_Hinting != h) {
602 // to do subpixel, we must have at most slight hinting
603 h = SkPaint::kSlight_Hinting;
604 }
605 rec->setHinting(h);
606}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000607
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000608#ifdef ANDROID
609uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
610 SkAutoMutexAcquire ac(gFTMutex);
611 SkFaceRec *rec = ref_ft_face(fontID);
612 uint16_t unitsPerEm = 0;
613
614 if (rec != NULL && rec->fFace != NULL) {
615 unitsPerEm = rec->fFace->units_per_EM;
616 unref_ft_face(rec->fFace);
617 }
618
619 return (uint32_t)unitsPerEm;
620}
621#endif
622
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000624 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 SkAutoMutexAcquire ac(gFTMutex);
626
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000628 if (!InitFreetype()) {
629 sk_throw();
630 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631 }
632 ++gFTCount;
633
634 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000635 fFTSize = NULL;
636 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000638 if (NULL == fFaceRec) {
639 return;
640 }
641 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642
643 // compute our factors from the record
644
645 SkMatrix m;
646
647 fRec.getSingleMatrix(&m);
648
649#ifdef DUMP_STRIKE_CREATION
650 SkString keyString;
651 SkFontHost::GetDescriptorKeyString(desc, &keyString);
652 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
653 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
654 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
655 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000656 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657#endif
658
659 // now compute our scale factors
660 SkScalar sx = m.getScaleX();
661 SkScalar sy = m.getScaleY();
662
663 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
664 // sort of give up on hinting
665 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
666 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
667 sx = sy = SkScalarAve(sx, sy);
668
669 SkScalar inv = SkScalarInvert(sx);
670
671 // flip the skew elements to go from our Y-down system to FreeType's
672 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
673 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
674 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
675 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
676 } else {
677 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
678 fMatrix22.xy = fMatrix22.yx = 0;
679 }
680
681 fScaleX = SkScalarToFixed(sx);
682 fScaleY = SkScalarToFixed(sy);
683
684 // compute the flags we send to Load_Glyph
685 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000686 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687
agl@chromium.org70a303f2010-05-10 14:15:50 +0000688 if (SkMask::kBW_Format == fRec.fMaskFormat) {
689 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
690 loadFlags = FT_LOAD_TARGET_MONO;
691 if (fRec.getHinting() == SkPaint::kNo_Hinting)
692 loadFlags = FT_LOAD_NO_HINTING;
693 } else {
694 switch (fRec.getHinting()) {
695 case SkPaint::kNo_Hinting:
696 loadFlags = FT_LOAD_NO_HINTING;
697 break;
698 case SkPaint::kSlight_Hinting:
699 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
700 break;
701 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000702 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
703 loadFlags = FT_LOAD_FORCE_AUTOHINT;
704 else
705 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000706 break;
707 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000708 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
709 loadFlags = FT_LOAD_FORCE_AUTOHINT;
710 break;
711 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000712 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comea2333d2011-03-14 16:44:56 +0000713 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat ||
714 SkMask::kLCD16_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000715 loadFlags = FT_LOAD_TARGET_LCD;
reed@google.comea2333d2011-03-14 16:44:56 +0000716 } else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000717 loadFlags = FT_LOAD_TARGET_LCD_V;
reed@google.comea2333d2011-03-14 16:44:56 +0000718 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000719 break;
720 default:
721 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
722 break;
723 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724 }
725
agl@chromium.org99e1b902010-01-05 01:19:44 +0000726 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000727 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000728
reed@google.com96a9f7912011-05-06 11:49:30 +0000729 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
730 // advances, as fontconfig and cairo do.
731 // See http://code.google.com/p/skia/issues/detail?id=222.
732 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
733
reed@android.come4d0bc02009-07-24 19:53:20 +0000734 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000735 }
736
737 // now create the FT_Size
738
739 {
740 FT_Error err;
741
742 err = FT_New_Size(fFace, &fFTSize);
743 if (err != 0) {
744 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
745 fFaceRec->fFontID, fScaleX, fScaleY, err));
746 fFace = NULL;
747 return;
748 }
749
750 err = FT_Activate_Size(fFTSize);
751 if (err != 0) {
752 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
753 fFaceRec->fFontID, fScaleX, fScaleY, err));
754 fFTSize = NULL;
755 }
756
757 err = FT_Set_Char_Size( fFace,
758 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
759 72, 72);
760 if (err != 0) {
761 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
762 fFaceRec->fFontID, fScaleX, fScaleY, err));
763 fFace = NULL;
764 return;
765 }
766
767 FT_Set_Transform( fFace, &fMatrix22, NULL);
768 }
769}
770
771SkScalerContext_FreeType::~SkScalerContext_FreeType() {
772 if (fFTSize != NULL) {
773 FT_Done_Size(fFTSize);
774 }
775
776 SkAutoMutexAcquire ac(gFTMutex);
777
778 if (fFace != NULL) {
779 unref_ft_face(fFace);
780 }
781 if (--gFTCount == 0) {
782// SkDEBUGF(("FT_Done_FreeType\n"));
783 FT_Done_FreeType(gFTLibrary);
784 SkDEBUGCODE(gFTLibrary = NULL;)
785 }
786}
787
788/* We call this before each use of the fFace, since we may be sharing
789 this face with other context (at different sizes).
790*/
791FT_Error SkScalerContext_FreeType::setupSize() {
792 /* In the off-chance that a font has been removed, we want to error out
793 right away, so call resolve just to be sure.
794
795 TODO: perhaps we can skip this, by walking the global font cache and
796 killing all of the contexts when we know that a given fontID is going
797 away...
798 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000799 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 return (FT_Error)-1;
801 }
802
803 FT_Error err = FT_Activate_Size(fFTSize);
804
805 if (err != 0) {
806 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
807 fFaceRec->fFontID, fScaleX, fScaleY, err));
808 fFTSize = NULL;
809 } else {
810 // seems we need to reset this every time (not sure why, but without it
811 // I get random italics from some other fFTSize)
812 FT_Set_Transform( fFace, &fMatrix22, NULL);
813 }
814 return err;
815}
816
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000817void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
818 FT_Pos strength;
819 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
820 / 24;
821 FT_Outline_Embolden(outline, strength);
822}
823
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000824unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825 return fFace->num_glyphs;
826}
827
828uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
829 return SkToU16(FT_Get_Char_Index( fFace, uni ));
830}
831
reed@android.com9d3a9852010-01-08 14:07:42 +0000832SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
833 // iterate through each cmap entry, looking for matching glyph indices
834 FT_UInt glyphIndex;
835 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
836
837 while (glyphIndex != 0) {
838 if (glyphIndex == glyph) {
839 return charCode;
840 }
841 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
842 }
843
844 return 0;
845}
846
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
848 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000849 case SkMask::kHorizontalLCD_Format:
850 case SkMask::kVerticalLCD_Format:
851 SkASSERT(!"An LCD format should never be passed here");
852 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 case SkMask::kBW_Format:
854 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855 case SkMask::kA8_Format:
856 default:
857 return FT_PIXEL_MODE_GRAY;
858 }
859}
860
reed@android.com8a1c16f2008-12-17 15:59:43 +0000861void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
862#ifdef FT_ADVANCES_H
863 /* unhinted and light hinted text have linearly scaled advances
864 * which are very cheap to compute with some font formats...
865 */
866 {
867 SkAutoMutexAcquire ac(gFTMutex);
868
869 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000870 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871 return;
872 }
873
874 FT_Error error;
875 FT_Fixed advance;
876
877 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
878 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
879 &advance );
880 if (0 == error) {
881 glyph->fRsbDelta = 0;
882 glyph->fLsbDelta = 0;
883 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
884 glyph->fAdvanceY = 0;
885 return;
886 }
887 }
888#endif /* FT_ADVANCES_H */
889 /* otherwise, we need to load/hint the glyph, which is slower */
890 this->generateMetrics(glyph);
891 return;
892}
893
894void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
895 SkAutoMutexAcquire ac(gFTMutex);
896
897 glyph->fRsbDelta = 0;
898 glyph->fLsbDelta = 0;
899
900 FT_Error err;
901
902 if (this->setupSize()) {
903 goto ERROR;
904 }
905
906 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
907 if (err != 0) {
908 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
909 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
910 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000911 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 return;
913 }
914
915 switch ( fFace->glyph->format ) {
916 case FT_GLYPH_FORMAT_OUTLINE:
917 FT_BBox bbox;
918
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000919 if (fRec.fFlags & kEmbolden_Flag) {
920 emboldenOutline(&fFace->glyph->outline);
921 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
923
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000924 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 int dx = glyph->getSubXFixed() >> 10;
926 int dy = glyph->getSubYFixed() >> 10;
927 // negate dy since freetype-y-goes-up and skia-y-goes-down
928 bbox.xMin += dx;
929 bbox.yMin -= dy;
930 bbox.xMax += dx;
931 bbox.yMax -= dy;
932 }
933
934 bbox.xMin &= ~63;
935 bbox.yMin &= ~63;
936 bbox.xMax = (bbox.xMax + 63) & ~63;
937 bbox.yMax = (bbox.yMax + 63) & ~63;
938
939 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
940 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
941 glyph->fTop = -SkToS16(bbox.yMax >> 6);
942 glyph->fLeft = SkToS16(bbox.xMin >> 6);
943 break;
944
945 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000946 if (fRec.fFlags & kEmbolden_Flag) {
947 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
948 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
949 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
951 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
952 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
953 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
954 break;
955
956 default:
957 SkASSERT(!"unknown glyph format");
958 goto ERROR;
959 }
960
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000961 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000962 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
963 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
964 if (fRec.fFlags & kDevKernText_Flag) {
965 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
966 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
967 }
968 } else {
969 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
970 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
971 }
972
973#ifdef ENABLE_GLYPH_SPEW
974 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
975 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
976#endif
977}
978
reed@android.comf5493692009-07-22 19:21:01 +0000979#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000980namespace skia_freetype_support {
981// extern functions from SkFontHost_FreeType_Subpixel
982extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
983extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
984}
985
986using namespace skia_freetype_support;
987#endif
988
reed@google.comc5181342011-05-17 20:52:46 +0000989static int lerp(int start, int end, int percent) {
990 return start + ((end - start) * percent >> 8);
991}
992
993static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
994 if (SK_FREETYPE_LCD_LERP) {
995 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
996 unsigned sum = r + 2 * g + b >> 2;
997 r = lerp(r, sum, SK_FREETYPE_LCD_LERP);
998 g = lerp(g, sum, SK_FREETYPE_LCD_LERP);
999 b = lerp(b, sum, SK_FREETYPE_LCD_LERP);
1000 }
1001 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1002}
1003
reed@google.comea2333d2011-03-14 16:44:56 +00001004static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap) {
reed@google.com260db922011-03-14 18:09:32 +00001005 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
reed@google.comea2333d2011-03-14 16:44:56 +00001006 SkASSERT(glyph.fHeight == bitmap.rows);
1007
reed@google.com260db922011-03-14 18:09:32 +00001008 const uint8_t* src = bitmap.buffer + 3;
reed@google.comea2333d2011-03-14 16:44:56 +00001009 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
1010 size_t dstRB = glyph.rowBytes();
1011 int width = glyph.fWidth;
1012
1013 for (int y = 0; y < glyph.fHeight; y++) {
1014 const uint8_t* triple = src;
1015 for (int x = 0; x < width; x++) {
reed@google.comc5181342011-05-17 20:52:46 +00001016 dst[x] = packTriple(triple[0], triple[1], triple[2]);
reed@google.comea2333d2011-03-14 16:44:56 +00001017 triple += 3;
1018 }
1019 src += bitmap.pitch;
1020 dst = (uint16_t*)((char*)dst + dstRB);
1021 }
1022}
1023
reed@android.com8a1c16f2008-12-17 15:59:43 +00001024void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1025 SkAutoMutexAcquire ac(gFTMutex);
1026
1027 FT_Error err;
1028
1029 if (this->setupSize()) {
1030 goto ERROR;
1031 }
1032
1033 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1034 if (err != 0) {
1035 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1036 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1037 ERROR:
1038 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1039 return;
1040 }
1041
agl@chromium.org309485b2009-07-21 17:41:32 +00001042 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1043 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 switch ( fFace->glyph->format ) {
1046 case FT_GLYPH_FORMAT_OUTLINE: {
1047 FT_Outline* outline = &fFace->glyph->outline;
1048 FT_BBox bbox;
1049 FT_Bitmap target;
1050
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001051 if (fRec.fFlags & kEmbolden_Flag) {
1052 emboldenOutline(outline);
1053 }
1054
reed@android.com8a1c16f2008-12-17 15:59:43 +00001055 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001056 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001057 dx = glyph.getSubXFixed() >> 10;
1058 dy = glyph.getSubYFixed() >> 10;
1059 // negate dy since freetype-y-goes-up and skia-y-goes-down
1060 dy = -dy;
1061 }
1062 FT_Outline_Get_CBox(outline, &bbox);
1063 /*
1064 what we really want to do for subpixel is
1065 offset(dx, dy)
1066 compute_bounds
1067 offset(bbox & !63)
1068 but that is two calls to offset, so we do the following, which
1069 achieves the same thing with only one offset call.
1070 */
1071 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1072 dy - ((bbox.yMin + dy) & ~63));
1073
reed@android.comf5493692009-07-22 19:21:01 +00001074#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +00001075 if (lcdRenderMode) {
1076 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
1077 // we have to call FT_Render_Glyph and memcpy the image out.
1078 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
1079 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
1080 FT_Render_Glyph(fFace->glyph, mode);
1081
1082 if (isVertical)
1083 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
1084 else
1085 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
1086
1087 break;
1088 }
1089#endif
1090
reed@google.comea2333d2011-03-14 16:44:56 +00001091 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1092 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
1093 copyFT2LCD16(glyph, fFace->glyph->bitmap);
1094 } else {
1095 target.width = glyph.fWidth;
1096 target.rows = glyph.fHeight;
1097 target.pitch = glyph.rowBytes();
1098 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1099 target.pixel_mode = compute_pixel_mode(
1100 (SkMask::Format)fRec.fMaskFormat);
1101 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001102
reed@google.comea2333d2011-03-14 16:44:56 +00001103 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1104 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1105 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 } break;
1107
1108 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001109 if (fRec.fFlags & kEmbolden_Flag) {
1110 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1111 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001113 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1114 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1115 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1116 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1117
1118 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1119 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001120
agl@chromium.org558434a2009-08-11 17:22:38 +00001121 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1122 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1123 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001124 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1125 unsigned dstRowBytes = glyph.rowBytes();
1126 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1127 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1128
1129 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1130 memcpy(dst, src, minRowBytes);
1131 memset(dst + minRowBytes, 0, extraRowBytes);
1132 src += srcRowBytes;
1133 dst += dstRowBytes;
1134 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001135 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +00001136 (glyph.fMaskFormat == SkMask::kA8_Format ||
1137 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
1138 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001139 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1140 uint8_t byte = 0;
1141 int bits = 0;
1142 const uint8_t* src_row = src;
1143 uint8_t* dst_row = dst;
1144
1145 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1146 if (!bits) {
1147 byte = *src_row++;
1148 bits = 8;
1149 }
1150
1151 *dst_row++ = byte & 0x80 ? 0xff : 0;
1152 bits--;
1153 byte <<= 1;
1154 }
1155
1156 src += fFace->glyph->bitmap.pitch;
1157 dst += glyph.rowBytes();
1158 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001159 } else {
1160 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001161 }
agl@chromium.org309485b2009-07-21 17:41:32 +00001162
1163 if (lcdRenderMode)
1164 glyph.expandA8ToLCD();
1165
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166 } break;
1167
1168 default:
1169 SkASSERT(!"unknown glyph format");
1170 goto ERROR;
1171 }
1172}
1173
1174///////////////////////////////////////////////////////////////////////////////
1175
1176#define ft2sk(x) SkFixedToScalar((x) << 10)
1177
reed@android.com6f252972009-01-14 16:46:16 +00001178#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001179 #define CONST_PARAM const
1180#else // older freetype doesn't use const here
1181 #define CONST_PARAM
1182#endif
1183
1184static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1185 SkPath* path = (SkPath*)ctx;
1186 path->close(); // to close the previous contour (if any)
1187 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1188 return 0;
1189}
1190
1191static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1192 SkPath* path = (SkPath*)ctx;
1193 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1194 return 0;
1195}
1196
1197static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1198 void* ctx) {
1199 SkPath* path = (SkPath*)ctx;
1200 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1201 return 0;
1202}
1203
1204static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1205 CONST_PARAM FT_Vector* pt2, void* ctx) {
1206 SkPath* path = (SkPath*)ctx;
1207 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1208 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1209 return 0;
1210}
1211
1212void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1213 SkPath* path) {
1214 SkAutoMutexAcquire ac(gFTMutex);
1215
1216 SkASSERT(&glyph && path);
1217
1218 if (this->setupSize()) {
1219 path->reset();
1220 return;
1221 }
1222
1223 uint32_t flags = fLoadGlyphFlags;
1224 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1225 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1226
1227 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1228
1229 if (err != 0) {
1230 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1231 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1232 path->reset();
1233 return;
1234 }
1235
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001236 if (fRec.fFlags & kEmbolden_Flag) {
1237 emboldenOutline(&fFace->glyph->outline);
1238 }
1239
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 FT_Outline_Funcs funcs;
1241
1242 funcs.move_to = move_proc;
1243 funcs.line_to = line_proc;
1244 funcs.conic_to = quad_proc;
1245 funcs.cubic_to = cubic_proc;
1246 funcs.shift = 0;
1247 funcs.delta = 0;
1248
1249 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1250
1251 if (err != 0) {
1252 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1253 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1254 path->reset();
1255 return;
1256 }
1257
1258 path->close();
1259}
1260
1261void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1262 SkPaint::FontMetrics* my) {
1263 if (NULL == mx && NULL == my) {
1264 return;
1265 }
1266
1267 SkAutoMutexAcquire ac(gFTMutex);
1268
1269 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001270 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001272 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 }
1274 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001275 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001276 }
1277 return;
1278 }
1279
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001280 FT_Face face = fFace;
1281 int upem = face->units_per_EM;
1282 if (upem <= 0) {
1283 goto ERROR;
1284 }
1285
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001286 SkPoint pts[6];
1287 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001288 SkFixed scaleY = fScaleY;
1289 SkFixed mxy = fMatrix22.xy;
1290 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001291 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1292 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001294 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001295 if (leading < 0) {
1296 leading = 0;
1297 }
1298
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001299 // Try to get the OS/2 table from the font. This contains the specific
1300 // average font width metrics which Windows uses.
1301 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1302
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 ys[0] = -face->bbox.yMax;
1304 ys[1] = -face->ascender;
1305 ys[2] = -face->descender;
1306 ys[3] = -face->bbox.yMin;
1307 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001308 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1309
1310 SkScalar x_height;
1311 if (os2 && os2->sxHeight) {
1312 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1313 } else {
1314 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1315 if (x_glyph) {
1316 FT_BBox bbox;
1317 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001318 if (fRec.fFlags & kEmbolden_Flag) {
1319 emboldenOutline(&fFace->glyph->outline);
1320 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001321 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1322 x_height = SkIntToScalar(bbox.yMax) / 64;
1323 } else {
1324 x_height = 0;
1325 }
1326 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327
1328 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001329 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1331 SkFixed x = SkFixedMul(mxy, y);
1332 y = SkFixedMul(myy, y);
1333 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1334 }
1335
1336 if (mx) {
1337 mx->fTop = pts[0].fX;
1338 mx->fAscent = pts[1].fX;
1339 mx->fDescent = pts[2].fX;
1340 mx->fBottom = pts[3].fX;
1341 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001342 mx->fAvgCharWidth = pts[5].fX;
1343 mx->fXMin = xmin;
1344 mx->fXMax = xmax;
1345 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 }
1347 if (my) {
1348 my->fTop = pts[0].fY;
1349 my->fAscent = pts[1].fY;
1350 my->fDescent = pts[2].fY;
1351 my->fBottom = pts[3].fY;
1352 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001353 my->fAvgCharWidth = pts[5].fY;
1354 my->fXMin = xmin;
1355 my->fXMax = xmax;
1356 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357 }
1358}
1359
1360////////////////////////////////////////////////////////////////////////
1361////////////////////////////////////////////////////////////////////////
1362
1363SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001364 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1365 if (!c->success()) {
1366 SkDELETE(c);
1367 c = NULL;
1368 }
1369 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001370}
1371
1372///////////////////////////////////////////////////////////////////////////////
1373
1374/* Export this so that other parts of our FonttHost port can make use of our
1375 ability to extract the name+style from a stream, using FreeType's api.
1376*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001377SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1378 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001379 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001380 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001381 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 return SkTypeface::kNormal;
1383 }
1384
1385 FT_Open_Args args;
1386 memset(&args, 0, sizeof(args));
1387
1388 const void* memoryBase = stream->getMemoryBase();
1389 FT_StreamRec streamRec;
1390
1391 if (NULL != memoryBase) {
1392 args.flags = FT_OPEN_MEMORY;
1393 args.memory_base = (const FT_Byte*)memoryBase;
1394 args.memory_size = stream->getLength();
1395 } else {
1396 memset(&streamRec, 0, sizeof(streamRec));
1397 streamRec.size = stream->read(NULL, 0);
1398 streamRec.descriptor.pointer = stream;
1399 streamRec.read = sk_stream_read;
1400 streamRec.close = sk_stream_close;
1401
1402 args.flags = FT_OPEN_STREAM;
1403 args.stream = &streamRec;
1404 }
1405
1406 FT_Face face;
1407 if (FT_Open_Face(library, &args, 0, &face)) {
1408 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001409 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001410 return SkTypeface::kNormal;
1411 }
1412
1413 name->set(face->family_name);
1414 int style = SkTypeface::kNormal;
1415
1416 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1417 style |= SkTypeface::kBold;
1418 }
1419 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1420 style |= SkTypeface::kItalic;
1421 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001422 if (isFixedWidth) {
1423 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1424 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425
1426 FT_Done_Face(face);
1427 FT_Done_FreeType(library);
1428 return (SkTypeface::Style)style;
1429}