blob: eed4dca3bdfccd7703dde861c14c1d294de30d31 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000012#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkDescriptor.h"
14#include "SkFDot6.h"
bungeman@google.com3aacb412012-03-13 14:55:12 +000015#include "SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkFontHost.h"
17#include "SkMask.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000018#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000019#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkStream.h"
21#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000023#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25#include <ft2build.h>
26#include FT_FREETYPE_H
27#include FT_OUTLINE_H
28#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000029#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000030#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000031#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000032// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
33#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000034#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000035#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000036#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000037#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039#ifdef FT_ADVANCES_H
40#include FT_ADVANCES_H
41#endif
42
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000043#if 0
44// Also include the files by name for build tools which require this.
45#include <freetype/freetype.h>
46#include <freetype/ftoutln.h>
47#include <freetype/ftsizes.h>
48#include <freetype/tttables.h>
49#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000050#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000051#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000052#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000053#endif
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055//#define ENABLE_GLYPH_SPEW // for tracing calls
56//#define DUMP_STRIKE_CREATION
57
reed@google.com1ac83502012-02-28 17:06:02 +000058//#define SK_GAMMA_APPLY_TO_A8
bungeman@google.com3aacb412012-03-13 14:55:12 +000059//#define SK_GAMMA_SRGB
reed@google.comec9ed812012-03-01 19:40:21 +000060
61#ifndef SK_GAMMA_CONTRAST
62 #define SK_GAMMA_CONTRAST 0x66
63#endif
64#ifndef SK_GAMMA_EXPONENT
65 #define SK_GAMMA_EXPONENT 2.2
66#endif
reed@google.com1ac83502012-02-28 17:06:02 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068#ifdef SK_DEBUG
69 #define SkASSERT_CONTINUE(pred) \
70 do { \
71 if (!(pred)) \
72 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
73 } while (false)
74#else
75 #define SkASSERT_CONTINUE(pred)
76#endif
77
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000078using namespace skia_advanced_typeface_metrics_utils;
79
reed@google.comeffc5012011-06-27 16:44:46 +000080static bool isLCD(const SkScalerContext::Rec& rec) {
81 switch (rec.fMaskFormat) {
82 case SkMask::kLCD16_Format:
83 case SkMask::kLCD32_Format:
84 return true;
85 default:
86 return false;
87 }
88}
89
reed@android.com8a1c16f2008-12-17 15:59:43 +000090//////////////////////////////////////////////////////////////////////////
91
92struct SkFaceRec;
93
digit@google.com1771cbf2012-01-26 21:26:40 +000094SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095static int gFTCount;
96static FT_Library gFTLibrary;
97static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000098static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
99static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +0000100static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
reed@google.comffe49f52011-11-22 19:42:41 +0000102static const uint8_t* gGammaTables[2];
103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104/////////////////////////////////////////////////////////////////////////
105
agl@chromium.orge76073b2010-06-04 20:31:17 +0000106// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
107// This value was chosen by eyeballing the result in Firefox and trying to match it.
108static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
109
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000110// convert from Skia's fixed (16.16) to FreeType's fixed (26.6) representation
111static inline int FixedToDot6(SkFixed x) { return x >> 10; }
112// convert from FreeType's fixed (26.6) to Skia's fixed (16.16) representation
113static inline SkFixed Dot6ToFixed(int x) { return x << 10; }
114
agl@chromium.org309485b2009-07-21 17:41:32 +0000115static bool
116InitFreetype() {
117 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000118 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000119 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000120 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000121
agl@chromium.org309485b2009-07-21 17:41:32 +0000122 // Setup LCD filtering. This reduces colour fringes for LCD rendered
123 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000124#ifdef FT_LCD_FILTER_H
bungeman@google.comd2dae962012-03-09 20:31:17 +0000125 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
126// err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000127 gLCDSupport = err == 0;
reed@google.coma1c32562012-03-01 19:38:23 +0000128 if (gLCDSupport) {
129 gLCDExtra = 2; //DEFAULT and LIGHT add one pixel to each side.
130 }
epoger@google.com5070d792011-06-29 20:43:14 +0000131#else
132 gLCDSupport = false;
133#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000134 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000135
136 return true;
137}
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139class SkScalerContext_FreeType : public SkScalerContext {
140public:
141 SkScalerContext_FreeType(const SkDescriptor* desc);
142 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000143
reed@android.com62900b42009-02-11 15:07:19 +0000144 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000145 return fFaceRec != NULL &&
146 fFTSize != NULL &&
147 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
150protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000151 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 virtual uint16_t generateCharToGlyph(SkUnichar uni);
153 virtual void generateAdvance(SkGlyph* glyph);
154 virtual void generateMetrics(SkGlyph* glyph);
155 virtual void generateImage(const SkGlyph& glyph);
156 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
157 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
158 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000159 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160
161private:
162 SkFaceRec* fFaceRec;
163 FT_Face fFace; // reference to shared face in gFaceRecHead
164 FT_Size fFTSize; // our own copy
165 SkFixed fScaleX, fScaleY;
166 FT_Matrix fMatrix22;
167 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000168 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000169 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000172 void emboldenOutline(FT_Outline* outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000173 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
174 bool snapToPixelBoundary = false);
175 void updateGlyphIfLCD(SkGlyph* glyph);
bungeman@google.com875eb982012-04-12 15:53:23 +0000176 void updateGlyphPosIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177};
178
179///////////////////////////////////////////////////////////////////////////
180///////////////////////////////////////////////////////////////////////////
181
182#include "SkStream.h"
183
184struct SkFaceRec {
185 SkFaceRec* fNext;
186 FT_Face fFace;
187 FT_StreamRec fFTStream;
188 SkStream* fSkStream;
189 uint32_t fRefCnt;
190 uint32_t fFontID;
191
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000192 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 SkFaceRec(SkStream* strm, uint32_t fontID);
194 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000195 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 }
197};
198
199extern "C" {
200 static unsigned long sk_stream_read(FT_Stream stream,
201 unsigned long offset,
202 unsigned char* buffer,
203 unsigned long count ) {
204 SkStream* str = (SkStream*)stream->descriptor.pointer;
205
206 if (count) {
207 if (!str->rewind()) {
208 return 0;
209 } else {
210 unsigned long ret;
211 if (offset) {
212 ret = str->read(NULL, offset);
213 if (ret != offset) {
214 return 0;
215 }
216 }
217 ret = str->read(buffer, count);
218 if (ret != count) {
219 return 0;
220 }
221 count = ret;
222 }
223 }
224 return count;
225 }
226
227 static void sk_stream_close( FT_Stream stream) {}
228}
229
230SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000231 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
233
reed@android.com4516f472009-06-29 16:25:36 +0000234 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 fFTStream.size = fSkStream->getLength();
236 fFTStream.descriptor.pointer = fSkStream;
237 fFTStream.read = sk_stream_read;
238 fFTStream.close = sk_stream_close;
239}
240
reed@android.com62900b42009-02-11 15:07:19 +0000241// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242static SkFaceRec* ref_ft_face(uint32_t fontID) {
243 SkFaceRec* rec = gFaceRecHead;
244 while (rec) {
245 if (rec->fFontID == fontID) {
246 SkASSERT(rec->fFace);
247 rec->fRefCnt += 1;
248 return rec;
249 }
250 rec = rec->fNext;
251 }
252
253 SkStream* strm = SkFontHost::OpenStream(fontID);
254 if (NULL == strm) {
255 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 return 0;
257 }
258
259 // this passes ownership of strm to the rec
260 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
261
262 FT_Open_Args args;
263 memset(&args, 0, sizeof(args));
264 const void* memoryBase = strm->getMemoryBase();
265
266 if (NULL != memoryBase) {
267//printf("mmap(%s)\n", keyString.c_str());
268 args.flags = FT_OPEN_MEMORY;
269 args.memory_base = (const FT_Byte*)memoryBase;
270 args.memory_size = strm->getLength();
271 } else {
272//printf("fopen(%s)\n", keyString.c_str());
273 args.flags = FT_OPEN_STREAM;
274 args.stream = &rec->fFTStream;
275 }
276
agl@chromium.org61a678a2010-08-06 18:08:18 +0000277 int face_index;
278 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
279 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
280 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281
282 if (err) { // bad filename, try the default font
283 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
284 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 return 0;
286 } else {
287 SkASSERT(rec->fFace);
288 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
289 rec->fNext = gFaceRecHead;
290 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 return rec;
292 }
293}
294
295static void unref_ft_face(FT_Face face) {
296 SkFaceRec* rec = gFaceRecHead;
297 SkFaceRec* prev = NULL;
298 while (rec) {
299 SkFaceRec* next = rec->fNext;
300 if (rec->fFace == face) {
301 if (--rec->fRefCnt == 0) {
302 if (prev) {
303 prev->fNext = next;
304 } else {
305 gFaceRecHead = next;
306 }
307 FT_Done_Face(face);
308 SkDELETE(rec);
309 }
310 return;
311 }
312 prev = rec;
313 rec = next;
314 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000315 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316}
317
318///////////////////////////////////////////////////////////////////////////
319
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000320// Work around for old versions of freetype.
321static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
322 FT_Int32 loadFlags, FT_Fixed* advances) {
323#ifdef FT_ADVANCES_H
324 return FT_Get_Advances(face, start, count, loadFlags, advances);
325#else
326 if (!face || start >= face->num_glyphs ||
327 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
328 return 6; // "Invalid argument."
329 }
330 if (count == 0)
331 return 0;
332
333 for (int i = 0; i < count; i++) {
334 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
335 if (err)
336 return err;
337 advances[i] = face->glyph->advance.x;
338 }
339
340 return 0;
341#endif
342}
343
344static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000345#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000346 FT_UShort fsType = FT_Get_FSType_Flags(face);
347 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
348 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
349#else
350 // No embedding is 0x2 and bitmap embedding only is 0x200.
351 TT_OS2* os2_table;
352 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
353 return (os2_table->fsType & 0x202) == 0;
354 }
355 return false; // We tried, fail safe.
356#endif
357}
358
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000359static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
360 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
361 if (!glyph_id)
362 return false;
363 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
364 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
365 return true;
366}
367
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000368static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000369 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000370 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
371 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000372 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000373 SkASSERT(data);
374 *data = advance;
375 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000376}
377
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000378static void populate_glyph_to_unicode(FT_Face& face,
379 SkTDArray<SkUnichar>* glyphToUnicode) {
380 // Check and see if we have Unicode cmaps.
381 for (int i = 0; i < face->num_charmaps; ++i) {
382 // CMaps known to support Unicode:
383 // Platform ID Encoding ID Name
384 // ----------- ----------- -----------------------------------
385 // 0 0,1 Apple Unicode
386 // 0 3 Apple Unicode 2.0 (preferred)
387 // 3 1 Microsoft Unicode UCS-2
388 // 3 10 Microsoft Unicode UCS-4 (preferred)
389 //
390 // See Apple TrueType Reference Manual
391 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
392 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
393 // Microsoft OpenType Specification
394 // http://www.microsoft.com/typography/otspec/cmap.htm
395
396 FT_UShort platformId = face->charmaps[i]->platform_id;
397 FT_UShort encodingId = face->charmaps[i]->encoding_id;
398
399 if (platformId != 0 && platformId != 3) {
400 continue;
401 }
402 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
403 continue;
404 }
405 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
406 (platformId == 0 && encodingId == 3));
407
408 FT_Set_Charmap(face, face->charmaps[i]);
409 if (glyphToUnicode->isEmpty()) {
410 glyphToUnicode->setCount(face->num_glyphs);
411 memset(glyphToUnicode->begin(), 0,
412 sizeof(SkUnichar) * face->num_glyphs);
413 }
414
415 // Iterate through each cmap entry.
416 FT_UInt glyphIndex;
417 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
418 glyphIndex != 0;
419 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
420 if (charCode &&
421 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
422 (*glyphToUnicode)[glyphIndex] = charCode;
423 }
424 }
425 }
426}
427
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000428// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000429SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000430 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000431 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
432 const uint32_t* glyphIDs,
433 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000434#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000435 return NULL;
436#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000437 SkAutoMutexAcquire ac(gFTMutex);
438 FT_Library libInit = NULL;
439 if (gFTCount == 0) {
440 if (!InitFreetype())
441 sk_throw();
442 libInit = gFTLibrary;
443 }
444 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
445 SkFaceRec* rec = ref_ft_face(fontID);
446 if (NULL == rec)
447 return NULL;
448 FT_Face face = rec->fFace;
449
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000450 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
451 info->fFontName.set(FT_Get_Postscript_Name(face));
452 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
453 info->fLastGlyphID = face->num_glyphs - 1;
454 info->fEmSize = 1000;
455
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000456 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000457 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000458 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000459 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000461 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000462 cid = true;
463 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000464 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000465 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000466 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000467 cid = true;
468 TT_Header* ttHeader;
469 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
470 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000471 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000472 }
473 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000474
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000475 info->fStyle = 0;
476 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000477 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000478 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000479 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000480 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
481 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000482 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
483
484 PS_FontInfoRec ps_info;
485 TT_Postscript* tt_info;
486 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
487 info->fItalicAngle = ps_info.italic_angle;
488 } else if ((tt_info =
489 (TT_Postscript*)FT_Get_Sfnt_Table(face,
490 ft_sfnt_post)) != NULL) {
491 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
492 } else {
493 info->fItalicAngle = 0;
494 }
495
496 info->fAscent = face->ascender;
497 info->fDescent = face->descender;
498
499 // Figure out a good guess for StemV - Min width of i, I, !, 1.
500 // This probably isn't very good with an italic font.
501 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000502 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000503 char stem_chars[] = {'i', 'I', '!', '1'};
504 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
505 FT_BBox bbox;
506 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
507 int16_t width = bbox.xMax - bbox.xMin;
508 if (width > 0 && width < min_width) {
509 min_width = width;
510 info->fStemV = min_width;
511 }
512 }
513 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000514
515 TT_PCLT* pclt_info;
516 TT_OS2* os2_table;
517 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
518 info->fCapHeight = pclt_info->CapHeight;
519 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
520 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000521 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000522 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000523 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000524 } else if ((os2_table =
525 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
526 info->fCapHeight = os2_table->sCapHeight;
527 } else {
528 // Figure out a good guess for CapHeight: average the height of M and X.
529 FT_BBox m_bbox, x_bbox;
530 bool got_m, got_x;
531 got_m = GetLetterCBox(face, 'M', &m_bbox);
532 got_x = GetLetterCBox(face, 'X', &x_bbox);
533 if (got_m && got_x) {
534 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
535 x_bbox.yMin) / 2;
536 } else if (got_m && !got_x) {
537 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
538 } else if (!got_m && got_x) {
539 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
540 }
541 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000542
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000543 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
544 face->bbox.xMax, face->bbox.yMin);
545
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000546 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000547 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
548 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
549 }
550
551 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000552 if (FT_IS_FIXED_WIDTH(face)) {
553 appendRange(&info->fGlyphWidths, 0);
554 int16_t advance = face->max_advance_width;
555 info->fGlyphWidths->fAdvance.append(1, &advance);
556 finishRange(info->fGlyphWidths.get(), 0,
557 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000558 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000559 appendRange(&info->fGlyphWidths, 0);
560 // So as to not blow out the stack, get advances in batches.
561 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
562 FT_Fixed advances[128];
563 int advanceCount = 128;
564 if (gID + advanceCount > face->num_glyphs)
565 advanceCount = face->num_glyphs - gID + 1;
566 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
567 advances);
568 for (int i = 0; i < advanceCount; i++) {
569 int16_t advance = advances[gID + i];
570 info->fGlyphWidths->fAdvance.append(1, &advance);
571 }
572 }
573 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
574 SkAdvancedTypefaceMetrics::WidthRange::kRange);
575 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000576 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000577 getAdvanceData(face,
578 face->num_glyphs,
579 glyphIDs,
580 glyphIDsCount,
581 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000582 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000583 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000584
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000585 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
586 FT_HAS_VERTICAL(face)) {
587 SkASSERT(false); // Not implemented yet.
588 }
589
590 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
591 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
592 // Postscript fonts may contain more than 255 glyphs, so we end up
593 // using multiple font descriptions with a glyph ordering. Record
594 // the name of each glyph.
595 info->fGlyphNames.reset(
596 new SkAutoTArray<SkString>(face->num_glyphs));
597 for (int gID = 0; gID < face->num_glyphs; gID++) {
598 char glyphName[128]; // PS limit for names is 127 bytes.
599 FT_Get_Glyph_Name(face, gID, glyphName, 128);
600 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601 }
602 }
603
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000604 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
605 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
606 face->num_charmaps) {
607 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
608 }
609
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000610 if (!canEmbed(face))
611 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
612
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000613 unref_ft_face(face);
614 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000615#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000616}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000617
reed@google.com618ef5e2011-01-26 22:10:41 +0000618///////////////////////////////////////////////////////////////////////////
619
reed@google.comffe49f52011-11-22 19:42:41 +0000620#define BLACK_LUMINANCE_LIMIT 0x40
621#define WHITE_LUMINANCE_LIMIT 0xA0
622
reed@google.com8ed436c2011-07-21 14:12:36 +0000623static bool bothZero(SkScalar a, SkScalar b) {
624 return 0 == a && 0 == b;
625}
626
627// returns false if there is any non-90-rotation or skew
628static bool isAxisAligned(const SkScalerContext::Rec& rec) {
629 return 0 == rec.fPreSkewX &&
630 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
631 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
632}
633
reed@google.com618ef5e2011-01-26 22:10:41 +0000634void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000635 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
636 //Cap the requested size as larger sizes give bogus values.
637 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000638 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
639 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000640 }
641
reed@google.com618ef5e2011-01-26 22:10:41 +0000642 if (!gLCDSupportValid) {
643 InitFreetype();
644 FT_Done_FreeType(gFTLibrary);
645 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000646
reed@google.comeffc5012011-06-27 16:44:46 +0000647 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000648 // If the runtime Freetype library doesn't support LCD mode, we disable
649 // it here.
650 rec->fMaskFormat = SkMask::kA8_Format;
651 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000652
reed@google.com618ef5e2011-01-26 22:10:41 +0000653 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000654 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000655 // collapse full->normal hinting if we're not doing LCD
656 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000657 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000658 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000659 if (SkPaint::kNo_Hinting != h) {
660 h = SkPaint::kSlight_Hinting;
661 }
662 }
663
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000664#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000665 // rotated text looks bad with hinting, so we disable it as needed
666 if (!isAxisAligned(*rec)) {
667 h = SkPaint::kNo_Hinting;
668 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000669#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000670 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000671
reed@google.com1ac83502012-02-28 17:06:02 +0000672#ifndef SK_USE_COLOR_LUMINANCE
reed@google.comffe49f52011-11-22 19:42:41 +0000673 // for compatibility at the moment, discretize luminance to 3 settings
674 // black, white, gray. This helps with fontcache utilization, since we
675 // won't create multiple entries that in the end map to the same results.
676 {
677 unsigned lum = rec->getLuminanceByte();
678 if (gGammaTables[0] || gGammaTables[1]) {
679 if (lum <= BLACK_LUMINANCE_LIMIT) {
680 lum = 0;
681 } else if (lum >= WHITE_LUMINANCE_LIMIT) {
682 lum = SkScalerContext::kLuminance_Max;
683 } else {
684 lum = SkScalerContext::kLuminance_Max >> 1;
685 }
686 } else {
687 lum = 0; // no gamma correct, so use 0 since SkPaint uses that
688 // when measuring text w/o regard for luminance
689 }
690 rec->setLuminanceBits(lum);
691 }
reed@google.com1ac83502012-02-28 17:06:02 +0000692#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000693}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000694
djsollen@google.comda957722011-11-16 17:00:46 +0000695#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000696uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
697 SkAutoMutexAcquire ac(gFTMutex);
698 SkFaceRec *rec = ref_ft_face(fontID);
699 uint16_t unitsPerEm = 0;
700
701 if (rec != NULL && rec->fFace != NULL) {
702 unitsPerEm = rec->fFace->units_per_EM;
703 unref_ft_face(rec->fFace);
704 }
705
706 return (uint32_t)unitsPerEm;
707}
708#endif
709
reed@android.com8a1c16f2008-12-17 15:59:43 +0000710SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000711 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000712 SkAutoMutexAcquire ac(gFTMutex);
713
reed@android.com8a1c16f2008-12-17 15:59:43 +0000714 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000715 if (!InitFreetype()) {
716 sk_throw();
717 }
reed@google.comffe49f52011-11-22 19:42:41 +0000718 SkFontHost::GetGammaTables(gGammaTables);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719 }
720 ++gFTCount;
721
722 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000723 fFTSize = NULL;
724 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000726 if (NULL == fFaceRec) {
727 return;
728 }
729 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730
731 // compute our factors from the record
732
733 SkMatrix m;
734
735 fRec.getSingleMatrix(&m);
736
737#ifdef DUMP_STRIKE_CREATION
738 SkString keyString;
739 SkFontHost::GetDescriptorKeyString(desc, &keyString);
740 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
741 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
742 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
743 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000744 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745#endif
746
747 // now compute our scale factors
748 SkScalar sx = m.getScaleX();
749 SkScalar sy = m.getScaleY();
750
751 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
752 // sort of give up on hinting
753 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
754 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
755 sx = sy = SkScalarAve(sx, sy);
756
757 SkScalar inv = SkScalarInvert(sx);
758
759 // flip the skew elements to go from our Y-down system to FreeType's
760 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
761 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
762 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
763 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
764 } else {
765 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
766 fMatrix22.xy = fMatrix22.yx = 0;
767 }
768
769 fScaleX = SkScalarToFixed(sx);
770 fScaleY = SkScalarToFixed(sy);
771
reed@google.coma1bfa212012-03-08 21:57:12 +0000772 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
773
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774 // compute the flags we send to Load_Glyph
775 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000776 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000777 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778
agl@chromium.org70a303f2010-05-10 14:15:50 +0000779 if (SkMask::kBW_Format == fRec.fMaskFormat) {
780 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
781 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000782 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000783 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000784 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000785 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000786 } else {
787 switch (fRec.getHinting()) {
788 case SkPaint::kNo_Hinting:
789 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000790 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000791 break;
792 case SkPaint::kSlight_Hinting:
793 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
794 break;
795 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000796 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
797 loadFlags = FT_LOAD_FORCE_AUTOHINT;
798 else
799 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000800 break;
801 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000802 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
803 loadFlags = FT_LOAD_FORCE_AUTOHINT;
804 break;
805 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000806 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000807 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000808 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000809 loadFlags = FT_LOAD_TARGET_LCD_V;
810 } else {
811 loadFlags = FT_LOAD_TARGET_LCD;
812 }
reed@google.comea2333d2011-03-14 16:44:56 +0000813 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000814 break;
815 default:
816 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
817 break;
818 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 }
820
reed@google.comeffc5012011-06-27 16:44:46 +0000821 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000822 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000823 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000824
reed@google.com96a9f7912011-05-06 11:49:30 +0000825 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
826 // advances, as fontconfig and cairo do.
827 // See http://code.google.com/p/skia/issues/detail?id=222.
828 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
829
reed@android.come4d0bc02009-07-24 19:53:20 +0000830 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000831 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832 }
833
834 // now create the FT_Size
835
836 {
837 FT_Error err;
838
839 err = FT_New_Size(fFace, &fFTSize);
840 if (err != 0) {
841 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
842 fFaceRec->fFontID, fScaleX, fScaleY, err));
843 fFace = NULL;
844 return;
845 }
846
847 err = FT_Activate_Size(fFTSize);
848 if (err != 0) {
849 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
850 fFaceRec->fFontID, fScaleX, fScaleY, err));
851 fFTSize = NULL;
852 }
853
854 err = FT_Set_Char_Size( fFace,
855 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
856 72, 72);
857 if (err != 0) {
858 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
859 fFaceRec->fFontID, fScaleX, fScaleY, err));
860 fFace = NULL;
861 return;
862 }
863
864 FT_Set_Transform( fFace, &fMatrix22, NULL);
865 }
866}
867
868SkScalerContext_FreeType::~SkScalerContext_FreeType() {
869 if (fFTSize != NULL) {
870 FT_Done_Size(fFTSize);
871 }
872
873 SkAutoMutexAcquire ac(gFTMutex);
874
875 if (fFace != NULL) {
876 unref_ft_face(fFace);
877 }
878 if (--gFTCount == 0) {
879// SkDEBUGF(("FT_Done_FreeType\n"));
880 FT_Done_FreeType(gFTLibrary);
881 SkDEBUGCODE(gFTLibrary = NULL;)
882 }
883}
884
885/* We call this before each use of the fFace, since we may be sharing
886 this face with other context (at different sizes).
887*/
888FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889 FT_Error err = FT_Activate_Size(fFTSize);
890
891 if (err != 0) {
892 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
893 fFaceRec->fFontID, fScaleX, fScaleY, err));
894 fFTSize = NULL;
895 } else {
896 // seems we need to reset this every time (not sure why, but without it
897 // I get random italics from some other fFTSize)
898 FT_Set_Transform( fFace, &fMatrix22, NULL);
899 }
900 return err;
901}
902
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000903void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
904 FT_Pos strength;
905 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
906 / 24;
907 FT_Outline_Embolden(outline, strength);
908}
909
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000910unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 return fFace->num_glyphs;
912}
913
914uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
915 return SkToU16(FT_Get_Char_Index( fFace, uni ));
916}
917
reed@android.com9d3a9852010-01-08 14:07:42 +0000918SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
919 // iterate through each cmap entry, looking for matching glyph indices
920 FT_UInt glyphIndex;
921 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
922
923 while (glyphIndex != 0) {
924 if (glyphIndex == glyph) {
925 return charCode;
926 }
927 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
928 }
929
930 return 0;
931}
932
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
934 switch (format) {
935 case SkMask::kBW_Format:
936 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 case SkMask::kA8_Format:
938 default:
939 return FT_PIXEL_MODE_GRAY;
940 }
941}
942
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
944#ifdef FT_ADVANCES_H
945 /* unhinted and light hinted text have linearly scaled advances
946 * which are very cheap to compute with some font formats...
947 */
reed@google.combdc99882011-11-21 14:36:57 +0000948 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 SkAutoMutexAcquire ac(gFTMutex);
950
951 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000952 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953 return;
954 }
955
956 FT_Error error;
957 FT_Fixed advance;
958
959 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
960 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
961 &advance );
962 if (0 == error) {
963 glyph->fRsbDelta = 0;
964 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +0000965 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
966 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967 return;
968 }
969 }
970#endif /* FT_ADVANCES_H */
971 /* otherwise, we need to load/hint the glyph, which is slower */
972 this->generateMetrics(glyph);
973 return;
974}
975
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000976void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
977 FT_BBox* bbox,
978 bool snapToPixelBoundary) {
979
980 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
981
982 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
983 int dx = FixedToDot6(glyph->getSubXFixed());
984 int dy = FixedToDot6(glyph->getSubYFixed());
985 // negate dy since freetype-y-goes-up and skia-y-goes-down
986 bbox->xMin += dx;
987 bbox->yMin -= dy;
988 bbox->xMax += dx;
989 bbox->yMax -= dy;
990 }
991
992 // outset the box to integral boundaries
993 if (snapToPixelBoundary) {
994 bbox->xMin &= ~63;
995 bbox->yMin &= ~63;
996 bbox->xMax = (bbox->xMax + 63) & ~63;
997 bbox->yMax = (bbox->yMax + 63) & ~63;
998 }
999}
1000
1001void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1002 if (isLCD(fRec)) {
1003 if (fLCDIsVert) {
1004 glyph->fHeight += gLCDExtra;
1005 glyph->fTop -= gLCDExtra >> 1;
1006 } else {
1007 glyph->fWidth += gLCDExtra;
1008 glyph->fLeft -= gLCDExtra >> 1;
1009 }
1010 }
1011}
bungeman@google.com875eb982012-04-12 15:53:23 +00001012void SkScalerContext_FreeType::updateGlyphPosIfLCD(SkGlyph* glyph) {
1013 if (isLCD(fRec)) {
1014 if (fLCDIsVert) {
1015 glyph->fTop -= gLCDExtra >> 1;
1016 } else {
1017 glyph->fLeft -= gLCDExtra >> 1;
1018 }
1019 }
1020}
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001021
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1023 SkAutoMutexAcquire ac(gFTMutex);
1024
1025 glyph->fRsbDelta = 0;
1026 glyph->fLsbDelta = 0;
1027
1028 FT_Error err;
1029
1030 if (this->setupSize()) {
1031 goto ERROR;
1032 }
1033
1034 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1035 if (err != 0) {
1036 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1037 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1038 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001039 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001040 return;
1041 }
1042
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001043 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001046 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001047 FT_BBox bbox;
1048
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001049 if (0 == fFace->glyph->outline.n_contours) {
1050 glyph->fWidth = 0;
1051 glyph->fHeight = 0;
1052 glyph->fTop = 0;
1053 glyph->fLeft = 0;
1054 break;
1055 }
1056
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001057 if (fRec.fFlags & kEmbolden_Flag) {
1058 emboldenOutline(&fFace->glyph->outline);
1059 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001061 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001062
1063 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1064 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1065 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1066 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001067
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001068 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
1069 vLeft = Dot6ToFixed(bbox.xMin);
1070 vTop = Dot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001071 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001072
1073 updateGlyphIfLCD(glyph);
1074
reed@android.com8a1c16f2008-12-17 15:59:43 +00001075 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001076 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077
1078 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001079 if (fRec.fFlags & kEmbolden_Flag) {
1080 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1081 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1082 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1084 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1085 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1086 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1087 break;
1088
1089 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001090 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 goto ERROR;
1092 }
1093
bungeman@google.com34f10262012-03-23 18:11:47 +00001094 if (fDoLinearMetrics) {
1095 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1096 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1097 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1099 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001100
reed@android.com8a1c16f2008-12-17 15:59:43 +00001101 if (fRec.fFlags & kDevKernText_Flag) {
1102 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1103 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1104 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 }
1106
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001107 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1108 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1109
1110 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1111
1112 FT_Matrix identityMatrix;
1113 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1114 identityMatrix.xy = identityMatrix.yx = 0;
1115
1116 // if the matrix is not the identity matrix then we need to re-load the
1117 // glyph with the identity matrix to get the necessary bounding box
1118 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1119
1120 FT_Set_Transform(fFace, &identityMatrix, NULL);
1121
1122 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1123 if (err != 0) {
1124 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1125 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1126 goto ERROR;
1127 }
1128
1129 if (fRec.fFlags & kEmbolden_Flag) {
1130 emboldenOutline(&fFace->glyph->outline);
1131 }
1132 }
1133
1134 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001135 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001136 getBBoxForCurrentGlyph(glyph, &bbox);
1137
1138 // compute the vertical gap above and below the glyph if the glyph were
1139 // centered within the linearVertAdvance
1140 SkFixed vGap = (fFace->glyph->linearVertAdvance - Dot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
1141
1142 // the origin point of the glyph when rendered vertically
1143 FT_Vector vOrigin;
1144 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
1145 vOrigin.y = vGap + Dot6ToFixed(bbox.yMax);
1146
1147 // transform the vertical origin based on the matrix of the actual glyph
1148 FT_Vector_Transform(&vOrigin, &fMatrix22);
1149
1150 // compute a new offset vector for the glyph by subtracting the vertical
1151 // origin from the original horizontal offset vector
1152 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1153 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1154
bungeman@google.com875eb982012-04-12 15:53:23 +00001155 updateGlyphPosIfLCD(glyph);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001156
1157 // use the vertical advance values computed by freetype
1158 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1159 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1160 }
1161
1162
reed@android.com8a1c16f2008-12-17 15:59:43 +00001163#ifdef ENABLE_GLYPH_SPEW
1164 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1165 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1166#endif
1167}
1168
reed@google.com1ac83502012-02-28 17:06:02 +00001169///////////////////////////////////////////////////////////////////////////////
1170
bungeman@google.com3aacb412012-03-13 14:55:12 +00001171#ifdef SK_USE_COLOR_LUMINANCE
1172
1173static float apply_contrast(float srca, float contrast) {
1174 return srca + ((1.0f - srca) * contrast * srca);
reed@google.comc5181342011-05-17 20:52:46 +00001175}
1176
bungeman@google.com3aacb412012-03-13 14:55:12 +00001177#ifdef SK_GAMMA_SRGB
1178static float lin(float per) {
1179 if (per <= 0.04045f) {
1180 return per / 12.92f;
reed@google.comc5181342011-05-17 20:52:46 +00001181 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001182 return powf((per + 0.055f) / 1.055, 2.4f);
reed@google.com1ac83502012-02-28 17:06:02 +00001183}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001184static float per(float lin) {
1185 if (lin <= 0.0031308f) {
1186 return lin * 12.92f;
reed@google.com1ac83502012-02-28 17:06:02 +00001187 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001188 return 1.055f * powf(lin, 1.0f / 2.4f) - 0.055f;
1189}
1190#else //SK_GAMMA_SRGB
1191static float lin(float per) {
1192 const float g = SK_GAMMA_EXPONENT;
1193 return powf(per, g);
1194}
1195static float per(float lin) {
1196 const float g = SK_GAMMA_EXPONENT;
1197 return powf(lin, 1.0f / g);
1198}
1199#endif //SK_GAMMA_SRGB
reed@google.com1ac83502012-02-28 17:06:02 +00001200
bungeman@google.com3aacb412012-03-13 14:55:12 +00001201static void build_gamma_table(uint8_t table[256], int srcI) {
1202 const float src = (float)srcI / 255.0f;
1203 const float linSrc = lin(src);
1204 const float linDst = 1.0f - linSrc;
1205 const float dst = per(linDst);
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001206
reed@google.com613e9fe2012-02-29 15:08:00 +00001207 // have our contrast value taper off to 0 as the src luminance becomes white
bungeman@google.com3aacb412012-03-13 14:55:12 +00001208 const float contrast = SK_GAMMA_CONTRAST / 255.0f * linDst;
1209 const float step = 1.0f / 256.0f;
reed@google.com1ac83502012-02-28 17:06:02 +00001210
bungeman@google.com3aacb412012-03-13 14:55:12 +00001211 //Remove discontinuity and instability when src is close to dst.
1212 if (fabs(src - dst) < 0.01f) {
1213 float rawSrca = 0.0f;
1214 for (int i = 0; i < 256; ++i, rawSrca += step) {
1215 float srca = apply_contrast(rawSrca, contrast);
1216 table[i] = sk_float_round2int(255.0f * srca);
1217 }
1218 } else {
1219 float rawSrca = 0.0f;
1220 for (int i = 0; i < 256; ++i, rawSrca += step) {
1221 float srca = apply_contrast(rawSrca, contrast);
1222 SkASSERT(srca <= 1.0f);
1223 float dsta = 1 - srca;
reed@google.com1ac83502012-02-28 17:06:02 +00001224
bungeman@google.com3aacb412012-03-13 14:55:12 +00001225 //Calculate the output we want.
1226 float linOut = (linSrc * srca + dsta * linDst);
1227 SkASSERT(linOut <= 1.0f);
1228 float out = per(linOut);
reed@google.com1ac83502012-02-28 17:06:02 +00001229
bungeman@google.com3aacb412012-03-13 14:55:12 +00001230 //Undo what the blit blend will do.
1231 float result = (out - dst) / (src - dst);
1232 SkASSERT(sk_float_round2int(255.0f * result) <= 255);
1233
1234 table[i] = sk_float_round2int(255.0f * result);
1235 }
reed@google.com1ac83502012-02-28 17:06:02 +00001236 }
1237}
1238
1239static const uint8_t* getGammaTable(U8CPU luminance) {
1240 static uint8_t gGammaTables[4][256];
1241 static bool gInited;
1242 if (!gInited) {
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001243 build_gamma_table(gGammaTables[0], 0x00);
1244 build_gamma_table(gGammaTables[1], 0x55);
1245 build_gamma_table(gGammaTables[2], 0xAA);
1246 build_gamma_table(gGammaTables[3], 0xFF);
reed@google.com1ac83502012-02-28 17:06:02 +00001247
1248 gInited = true;
1249 }
1250 SkASSERT(0 == (luminance >> 8));
1251 return gGammaTables[luminance >> 6];
1252}
1253
bungeman@google.com3aacb412012-03-13 14:55:12 +00001254#else //SK_USE_COLOR_LUMINANCE
reed@google.comd61b92b2012-03-02 16:02:07 +00001255static const uint8_t* getIdentityTable() {
1256 static bool gOnce;
1257 static uint8_t gIdentityTable[256];
1258 if (!gOnce) {
1259 for (int i = 0; i < 256; ++i) {
1260 gIdentityTable[i] = i;
1261 }
1262 gOnce = true;
1263 }
1264 return gIdentityTable;
1265}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001266#endif //SK_USE_COLOR_LUMINANCE
reed@google.com1ac83502012-02-28 17:06:02 +00001267
1268static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
reed@google.comc5181342011-05-17 20:52:46 +00001269 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1270}
1271
reed@google.com73824072011-06-23 13:17:30 +00001272static uint16_t grayToRGB16(U8CPU gray) {
1273 SkASSERT(gray <= 255);
1274 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1275}
1276
1277static int bittst(const uint8_t data[], int bitOffset) {
1278 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001279 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001280 return lowBit & 1;
1281}
1282
reed@google.comeffc5012011-06-27 16:44:46 +00001283static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
reed@google.coma1bfa212012-03-08 21:57:12 +00001284 int lcdIsBGR, bool lcdIsVert, const uint8_t* tableR,
reed@google.com1ac83502012-02-28 17:06:02 +00001285 const uint8_t* tableG, const uint8_t* tableB) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001286 if (lcdIsVert) {
1287 SkASSERT(3 * glyph.fHeight == bitmap.rows);
1288 } else {
1289 SkASSERT(glyph.fHeight == bitmap.rows);
1290 }
1291
reed@google.comea2333d2011-03-14 16:44:56 +00001292 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001293 const size_t dstRB = glyph.rowBytes();
1294 const int width = glyph.fWidth;
1295 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001296
reed@google.com73824072011-06-23 13:17:30 +00001297 switch (bitmap.pixel_mode) {
1298 case FT_PIXEL_MODE_MONO: {
1299 for (int y = 0; y < glyph.fHeight; ++y) {
1300 for (int x = 0; x < width; ++x) {
1301 dst[x] = -bittst(src, x);
1302 }
1303 dst = (uint16_t*)((char*)dst + dstRB);
1304 src += bitmap.pitch;
1305 }
1306 } break;
1307 case FT_PIXEL_MODE_GRAY: {
1308 for (int y = 0; y < glyph.fHeight; ++y) {
1309 for (int x = 0; x < width; ++x) {
1310 dst[x] = grayToRGB16(src[x]);
1311 }
1312 dst = (uint16_t*)((char*)dst + dstRB);
1313 src += bitmap.pitch;
1314 }
1315 } break;
1316 default: {
reed@google.coma1bfa212012-03-08 21:57:12 +00001317 SkASSERT(lcdIsVert || (glyph.fWidth * 3 == bitmap.width));
reed@google.com73824072011-06-23 13:17:30 +00001318 for (int y = 0; y < glyph.fHeight; y++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001319 if (lcdIsVert) { // vertical stripes
1320 const uint8_t* srcR = src;
1321 const uint8_t* srcG = srcR + bitmap.pitch;
1322 const uint8_t* srcB = srcG + bitmap.pitch;
1323 if (lcdIsBGR) {
1324 SkTSwap(srcR, srcB);
reed@google.comeffc5012011-06-27 16:44:46 +00001325 }
reed@google.comeffc5012011-06-27 16:44:46 +00001326 for (int x = 0; x < width; x++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001327 dst[x] = packTriple(tableR[*srcR++],
1328 tableG[*srcG++],
1329 tableB[*srcB++]);
reed@google.comeffc5012011-06-27 16:44:46 +00001330 }
reed@google.coma1bfa212012-03-08 21:57:12 +00001331 src += 3 * bitmap.pitch;
1332 } else { // horizontal stripes
1333 const uint8_t* triple = src;
1334 if (lcdIsBGR) {
1335 for (int x = 0; x < width; x++) {
1336 dst[x] = packTriple(tableR[triple[2]],
1337 tableG[triple[1]],
1338 tableB[triple[0]]);
1339 triple += 3;
1340 }
1341 } else {
1342 for (int x = 0; x < width; x++) {
1343 dst[x] = packTriple(tableR[triple[0]],
1344 tableG[triple[1]],
1345 tableB[triple[2]]);
1346 triple += 3;
1347 }
1348 }
1349 src += bitmap.pitch;
reed@google.com73824072011-06-23 13:17:30 +00001350 }
reed@google.com73824072011-06-23 13:17:30 +00001351 dst = (uint16_t*)((char*)dst + dstRB);
1352 }
1353 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001354 }
1355}
1356
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1358 SkAutoMutexAcquire ac(gFTMutex);
1359
1360 FT_Error err;
1361
1362 if (this->setupSize()) {
1363 goto ERROR;
1364 }
1365
1366 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1367 if (err != 0) {
1368 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1369 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1370 ERROR:
1371 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1372 return;
1373 }
1374
reed@google.com1ac83502012-02-28 17:06:02 +00001375#ifdef SK_USE_COLOR_LUMINANCE
1376 SkColor lumColor = fRec.getLuminanceColor();
1377 const uint8_t* tableR = getGammaTable(SkColorGetR(lumColor));
1378 const uint8_t* tableG = getGammaTable(SkColorGetG(lumColor));
1379 const uint8_t* tableB = getGammaTable(SkColorGetB(lumColor));
1380#else
1381 unsigned lum = fRec.getLuminanceByte();
reed@google.comd61b92b2012-03-02 16:02:07 +00001382 const uint8_t* tableR;
1383 const uint8_t* tableG;
1384 const uint8_t* tableB;
1385
1386 bool isWhite = lum >= WHITE_LUMINANCE_LIMIT;
1387 bool isBlack = lum <= BLACK_LUMINANCE_LIMIT;
1388 if ((gGammaTables[0] || gGammaTables[1]) && (isBlack || isWhite)) {
1389 tableR = tableG = tableB = gGammaTables[isBlack ? 0 : 1];
1390 } else {
1391 tableR = tableG = tableB = getIdentityTable();
1392 }
reed@google.com1ac83502012-02-28 17:06:02 +00001393#endif
1394
reed@google.coma1bfa212012-03-08 21:57:12 +00001395 const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
1396 const bool doVert = fLCDIsVert;
1397
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398 switch ( fFace->glyph->format ) {
1399 case FT_GLYPH_FORMAT_OUTLINE: {
1400 FT_Outline* outline = &fFace->glyph->outline;
1401 FT_BBox bbox;
1402 FT_Bitmap target;
1403
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001404 if (fRec.fFlags & kEmbolden_Flag) {
1405 emboldenOutline(outline);
1406 }
1407
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001409 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001410 dx = FixedToDot6(glyph.getSubXFixed());
1411 dy = FixedToDot6(glyph.getSubYFixed());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001412 // negate dy since freetype-y-goes-up and skia-y-goes-down
1413 dy = -dy;
1414 }
1415 FT_Outline_Get_CBox(outline, &bbox);
1416 /*
1417 what we really want to do for subpixel is
1418 offset(dx, dy)
1419 compute_bounds
1420 offset(bbox & !63)
1421 but that is two calls to offset, so we do the following, which
1422 achieves the same thing with only one offset call.
1423 */
1424 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1425 dy - ((bbox.yMin + dy) & ~63));
1426
reed@google.comea2333d2011-03-14 16:44:56 +00001427 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001428 FT_Render_Glyph(fFace->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD);
1429 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001430 tableR, tableG, tableB);
reed@google.comea2333d2011-03-14 16:44:56 +00001431 } else {
1432 target.width = glyph.fWidth;
1433 target.rows = glyph.fHeight;
1434 target.pitch = glyph.rowBytes();
1435 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1436 target.pixel_mode = compute_pixel_mode(
1437 (SkMask::Format)fRec.fMaskFormat);
1438 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001439
reed@google.comea2333d2011-03-14 16:44:56 +00001440 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1441 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1442 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001443 } break;
1444
1445 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001446 if (fRec.fFlags & kEmbolden_Flag) {
1447 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1448 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1449 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001450 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1451 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1452 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1453 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1454
1455 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1456 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001457
agl@chromium.org558434a2009-08-11 17:22:38 +00001458 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1459 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1460 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001461 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1462 unsigned dstRowBytes = glyph.rowBytes();
1463 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1464 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1465
1466 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1467 memcpy(dst, src, minRowBytes);
1468 memset(dst + minRowBytes, 0, extraRowBytes);
1469 src += srcRowBytes;
1470 dst += dstRowBytes;
1471 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001472 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001473 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001474 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1475 uint8_t byte = 0;
1476 int bits = 0;
1477 const uint8_t* src_row = src;
1478 uint8_t* dst_row = dst;
1479
1480 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1481 if (!bits) {
1482 byte = *src_row++;
1483 bits = 8;
1484 }
1485
1486 *dst_row++ = byte & 0x80 ? 0xff : 0;
1487 bits--;
1488 byte <<= 1;
1489 }
1490
1491 src += fFace->glyph->bitmap.pitch;
1492 dst += glyph.rowBytes();
1493 }
reed@google.com73824072011-06-23 13:17:30 +00001494 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001495 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001496 tableR, tableG, tableB);
agl@chromium.org558434a2009-08-11 17:22:38 +00001497 } else {
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001498 SkDEBUGFAIL("unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001499 }
1500 } break;
1501
1502 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001503 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001504 goto ERROR;
1505 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001506
reed@google.comd61b92b2012-03-02 16:02:07 +00001507// We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum,
1508// it is optional
1509#if defined(SK_GAMMA_APPLY_TO_A8) || !defined(SK_USE_COLOR_LUMINANCE)
reed@google.com1ac83502012-02-28 17:06:02 +00001510 if (SkMask::kA8_Format == glyph.fMaskFormat) {
1511 SkASSERT(tableR == tableG && tableR == tableB);
1512 const uint8_t* table = tableR;
1513 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1514 unsigned rowBytes = glyph.rowBytes();
1515
1516 for (int y = glyph.fHeight - 1; y >= 0; --y) {
1517 for (int x = glyph.fWidth - 1; x >= 0; --x) {
1518 dst[x] = table[dst[x]];
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001519 }
reed@google.com1ac83502012-02-28 17:06:02 +00001520 dst += rowBytes;
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001521 }
1522 }
reed@google.com1ac83502012-02-28 17:06:02 +00001523#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524}
1525
1526///////////////////////////////////////////////////////////////////////////////
1527
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001528#define ft2sk(x) SkFixedToScalar(Dot6ToFixed(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +00001529
reed@android.com6f252972009-01-14 16:46:16 +00001530#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001531 #define CONST_PARAM const
1532#else // older freetype doesn't use const here
1533 #define CONST_PARAM
1534#endif
1535
1536static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1537 SkPath* path = (SkPath*)ctx;
1538 path->close(); // to close the previous contour (if any)
1539 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1540 return 0;
1541}
1542
1543static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1544 SkPath* path = (SkPath*)ctx;
1545 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1546 return 0;
1547}
1548
1549static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1550 void* ctx) {
1551 SkPath* path = (SkPath*)ctx;
1552 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1553 return 0;
1554}
1555
1556static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1557 CONST_PARAM FT_Vector* pt2, void* ctx) {
1558 SkPath* path = (SkPath*)ctx;
1559 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1560 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1561 return 0;
1562}
1563
1564void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1565 SkPath* path) {
1566 SkAutoMutexAcquire ac(gFTMutex);
1567
1568 SkASSERT(&glyph && path);
1569
1570 if (this->setupSize()) {
1571 path->reset();
1572 return;
1573 }
1574
1575 uint32_t flags = fLoadGlyphFlags;
1576 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1577 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1578
1579 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1580
1581 if (err != 0) {
1582 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1583 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1584 path->reset();
1585 return;
1586 }
1587
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001588 if (fRec.fFlags & kEmbolden_Flag) {
1589 emboldenOutline(&fFace->glyph->outline);
1590 }
1591
reed@android.com8a1c16f2008-12-17 15:59:43 +00001592 FT_Outline_Funcs funcs;
1593
1594 funcs.move_to = move_proc;
1595 funcs.line_to = line_proc;
1596 funcs.conic_to = quad_proc;
1597 funcs.cubic_to = cubic_proc;
1598 funcs.shift = 0;
1599 funcs.delta = 0;
1600
1601 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1602
1603 if (err != 0) {
1604 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1605 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1606 path->reset();
1607 return;
1608 }
1609
1610 path->close();
1611}
1612
1613void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1614 SkPaint::FontMetrics* my) {
1615 if (NULL == mx && NULL == my) {
1616 return;
1617 }
1618
1619 SkAutoMutexAcquire ac(gFTMutex);
1620
1621 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001622 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001623 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001624 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001625 }
1626 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001627 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628 }
1629 return;
1630 }
1631
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001632 FT_Face face = fFace;
1633 int upem = face->units_per_EM;
1634 if (upem <= 0) {
1635 goto ERROR;
1636 }
1637
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001638 SkPoint pts[6];
1639 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001640 SkFixed scaleY = fScaleY;
1641 SkFixed mxy = fMatrix22.xy;
1642 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001643 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1644 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001645
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001646 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001647 if (leading < 0) {
1648 leading = 0;
1649 }
1650
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001651 // Try to get the OS/2 table from the font. This contains the specific
1652 // average font width metrics which Windows uses.
1653 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1654
reed@android.com8a1c16f2008-12-17 15:59:43 +00001655 ys[0] = -face->bbox.yMax;
1656 ys[1] = -face->ascender;
1657 ys[2] = -face->descender;
1658 ys[3] = -face->bbox.yMin;
1659 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001660 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1661
1662 SkScalar x_height;
1663 if (os2 && os2->sxHeight) {
1664 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1665 } else {
1666 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1667 if (x_glyph) {
1668 FT_BBox bbox;
1669 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001670 if (fRec.fFlags & kEmbolden_Flag) {
1671 emboldenOutline(&fFace->glyph->outline);
1672 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001673 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001674 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001675 } else {
1676 x_height = 0;
1677 }
1678 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001679
1680 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001681 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001682 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1683 SkFixed x = SkFixedMul(mxy, y);
1684 y = SkFixedMul(myy, y);
1685 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1686 }
1687
1688 if (mx) {
1689 mx->fTop = pts[0].fX;
1690 mx->fAscent = pts[1].fX;
1691 mx->fDescent = pts[2].fX;
1692 mx->fBottom = pts[3].fX;
1693 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001694 mx->fAvgCharWidth = pts[5].fX;
1695 mx->fXMin = xmin;
1696 mx->fXMax = xmax;
1697 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001698 }
1699 if (my) {
1700 my->fTop = pts[0].fY;
1701 my->fAscent = pts[1].fY;
1702 my->fDescent = pts[2].fY;
1703 my->fBottom = pts[3].fY;
1704 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001705 my->fAvgCharWidth = pts[5].fY;
1706 my->fXMin = xmin;
1707 my->fXMax = xmax;
1708 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001709 }
1710}
1711
1712////////////////////////////////////////////////////////////////////////
1713////////////////////////////////////////////////////////////////////////
1714
1715SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001716 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1717 if (!c->success()) {
1718 SkDELETE(c);
1719 c = NULL;
1720 }
1721 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001722}
1723
1724///////////////////////////////////////////////////////////////////////////////
1725
1726/* Export this so that other parts of our FonttHost port can make use of our
1727 ability to extract the name+style from a stream, using FreeType's api.
1728*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001729bool find_name_and_attributes(SkStream* stream, SkString* name,
1730 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001731 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001732 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001733 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001734 }
1735
1736 FT_Open_Args args;
1737 memset(&args, 0, sizeof(args));
1738
1739 const void* memoryBase = stream->getMemoryBase();
1740 FT_StreamRec streamRec;
1741
1742 if (NULL != memoryBase) {
1743 args.flags = FT_OPEN_MEMORY;
1744 args.memory_base = (const FT_Byte*)memoryBase;
1745 args.memory_size = stream->getLength();
1746 } else {
1747 memset(&streamRec, 0, sizeof(streamRec));
1748 streamRec.size = stream->read(NULL, 0);
1749 streamRec.descriptor.pointer = stream;
1750 streamRec.read = sk_stream_read;
1751 streamRec.close = sk_stream_close;
1752
1753 args.flags = FT_OPEN_STREAM;
1754 args.stream = &streamRec;
1755 }
1756
1757 FT_Face face;
1758 if (FT_Open_Face(library, &args, 0, &face)) {
1759 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001760 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001761 }
1762
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001763 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001764 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001765 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001766 }
1767 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001768 tempStyle |= SkTypeface::kItalic;
1769 }
1770
1771 if (name) {
1772 name->set(face->family_name);
1773 }
1774 if (style) {
1775 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001776 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001777 if (isFixedWidth) {
1778 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1779 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001780
1781 FT_Done_Face(face);
1782 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001783 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001784}