blob: 0d72fa1dc5ecf4724e1a04266c8722dbfc0fd80b [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"
bungeman@google.combbe50132012-07-24 20:33:21 +000017#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkMask.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000019#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000020#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkStream.h"
22#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000024#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
26#include <ft2build.h>
27#include FT_FREETYPE_H
28#include FT_OUTLINE_H
29#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000030#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000031#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000032#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000033// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
34#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000035#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000036#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000037#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000038#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040#ifdef FT_ADVANCES_H
41#include FT_ADVANCES_H
42#endif
43
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000044#if 0
45// Also include the files by name for build tools which require this.
46#include <freetype/freetype.h>
47#include <freetype/ftoutln.h>
48#include <freetype/ftsizes.h>
49#include <freetype/tttables.h>
50#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000051#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000052#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000053#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000054#endif
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056//#define ENABLE_GLYPH_SPEW // for tracing calls
57//#define DUMP_STRIKE_CREATION
58
reed@google.com1ac83502012-02-28 17:06:02 +000059//#define SK_GAMMA_APPLY_TO_A8
bungeman@google.com3aacb412012-03-13 14:55:12 +000060//#define SK_GAMMA_SRGB
reed@google.comec9ed812012-03-01 19:40:21 +000061
62#ifndef SK_GAMMA_CONTRAST
63 #define SK_GAMMA_CONTRAST 0x66
64#endif
65#ifndef SK_GAMMA_EXPONENT
66 #define SK_GAMMA_EXPONENT 2.2
67#endif
reed@google.com1ac83502012-02-28 17:06:02 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069#ifdef SK_DEBUG
70 #define SkASSERT_CONTINUE(pred) \
71 do { \
72 if (!(pred)) \
73 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
74 } while (false)
75#else
76 #define SkASSERT_CONTINUE(pred)
77#endif
78
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000079using namespace skia_advanced_typeface_metrics_utils;
80
reed@google.comeffc5012011-06-27 16:44:46 +000081static bool isLCD(const SkScalerContext::Rec& rec) {
82 switch (rec.fMaskFormat) {
83 case SkMask::kLCD16_Format:
84 case SkMask::kLCD32_Format:
85 return true;
86 default:
87 return false;
88 }
89}
90
reed@android.com8a1c16f2008-12-17 15:59:43 +000091//////////////////////////////////////////////////////////////////////////
92
93struct SkFaceRec;
94
digit@google.com1771cbf2012-01-26 21:26:40 +000095SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096static int gFTCount;
97static FT_Library gFTLibrary;
98static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000099static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
100static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +0000101static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102
reed@google.comffe49f52011-11-22 19:42:41 +0000103static const uint8_t* gGammaTables[2];
104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105/////////////////////////////////////////////////////////////////////////
106
agl@chromium.orge76073b2010-06-04 20:31:17 +0000107// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
108// This value was chosen by eyeballing the result in Firefox and trying to match it.
109static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
110
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000111// convert from Skia's fixed (16.16) to FreeType's fixed (26.6) representation
112static inline int FixedToDot6(SkFixed x) { return x >> 10; }
113// convert from FreeType's fixed (26.6) to Skia's fixed (16.16) representation
114static inline SkFixed Dot6ToFixed(int x) { return x << 10; }
115
agl@chromium.org309485b2009-07-21 17:41:32 +0000116static bool
117InitFreetype() {
118 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000119 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000120 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000121 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000122
agl@chromium.org309485b2009-07-21 17:41:32 +0000123 // Setup LCD filtering. This reduces colour fringes for LCD rendered
124 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000125#ifdef FT_LCD_FILTER_H
bungeman@google.comd2dae962012-03-09 20:31:17 +0000126 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
127// err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000128 gLCDSupport = err == 0;
reed@google.coma1c32562012-03-01 19:38:23 +0000129 if (gLCDSupport) {
130 gLCDExtra = 2; //DEFAULT and LIGHT add one pixel to each side.
131 }
epoger@google.com5070d792011-06-29 20:43:14 +0000132#else
133 gLCDSupport = false;
134#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000135 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000136
137 return true;
138}
139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140class SkScalerContext_FreeType : public SkScalerContext {
141public:
142 SkScalerContext_FreeType(const SkDescriptor* desc);
143 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000144
reed@android.com62900b42009-02-11 15:07:19 +0000145 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000146 return fFaceRec != NULL &&
147 fFTSize != NULL &&
148 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000149 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150
151protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000152 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 virtual uint16_t generateCharToGlyph(SkUnichar uni);
154 virtual void generateAdvance(SkGlyph* glyph);
155 virtual void generateMetrics(SkGlyph* glyph);
156 virtual void generateImage(const SkGlyph& glyph);
157 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
158 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
159 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000160 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
162private:
163 SkFaceRec* fFaceRec;
164 FT_Face fFace; // reference to shared face in gFaceRecHead
165 FT_Size fFTSize; // our own copy
166 SkFixed fScaleX, fScaleY;
167 FT_Matrix fMatrix22;
168 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000169 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000170 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171
172 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000173 void emboldenOutline(FT_Outline* outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000174 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
175 bool snapToPixelBoundary = false);
176 void updateGlyphIfLCD(SkGlyph* glyph);
bungeman@google.com875eb982012-04-12 15:53:23 +0000177 void updateGlyphPosIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178};
179
180///////////////////////////////////////////////////////////////////////////
181///////////////////////////////////////////////////////////////////////////
182
183#include "SkStream.h"
184
185struct SkFaceRec {
186 SkFaceRec* fNext;
187 FT_Face fFace;
188 FT_StreamRec fFTStream;
189 SkStream* fSkStream;
190 uint32_t fRefCnt;
191 uint32_t fFontID;
192
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000193 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 SkFaceRec(SkStream* strm, uint32_t fontID);
195 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000196 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 }
198};
199
200extern "C" {
201 static unsigned long sk_stream_read(FT_Stream stream,
202 unsigned long offset,
203 unsigned char* buffer,
204 unsigned long count ) {
205 SkStream* str = (SkStream*)stream->descriptor.pointer;
206
207 if (count) {
208 if (!str->rewind()) {
209 return 0;
210 } else {
211 unsigned long ret;
212 if (offset) {
213 ret = str->read(NULL, offset);
214 if (ret != offset) {
215 return 0;
216 }
217 }
218 ret = str->read(buffer, count);
219 if (ret != count) {
220 return 0;
221 }
222 count = ret;
223 }
224 }
225 return count;
226 }
227
228 static void sk_stream_close( FT_Stream stream) {}
229}
230
231SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000232 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
234
reed@android.com4516f472009-06-29 16:25:36 +0000235 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 fFTStream.size = fSkStream->getLength();
237 fFTStream.descriptor.pointer = fSkStream;
238 fFTStream.read = sk_stream_read;
239 fFTStream.close = sk_stream_close;
240}
241
reed@android.com62900b42009-02-11 15:07:19 +0000242// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243static SkFaceRec* ref_ft_face(uint32_t fontID) {
244 SkFaceRec* rec = gFaceRecHead;
245 while (rec) {
246 if (rec->fFontID == fontID) {
247 SkASSERT(rec->fFace);
248 rec->fRefCnt += 1;
249 return rec;
250 }
251 rec = rec->fNext;
252 }
253
254 SkStream* strm = SkFontHost::OpenStream(fontID);
255 if (NULL == strm) {
256 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 return 0;
258 }
259
260 // this passes ownership of strm to the rec
261 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
262
263 FT_Open_Args args;
264 memset(&args, 0, sizeof(args));
265 const void* memoryBase = strm->getMemoryBase();
266
267 if (NULL != memoryBase) {
268//printf("mmap(%s)\n", keyString.c_str());
269 args.flags = FT_OPEN_MEMORY;
270 args.memory_base = (const FT_Byte*)memoryBase;
271 args.memory_size = strm->getLength();
272 } else {
273//printf("fopen(%s)\n", keyString.c_str());
274 args.flags = FT_OPEN_STREAM;
275 args.stream = &rec->fFTStream;
276 }
277
agl@chromium.org61a678a2010-08-06 18:08:18 +0000278 int face_index;
279 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
280 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
281 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282
283 if (err) { // bad filename, try the default font
284 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
285 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 return 0;
287 } else {
288 SkASSERT(rec->fFace);
289 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
290 rec->fNext = gFaceRecHead;
291 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 return rec;
293 }
294}
295
296static void unref_ft_face(FT_Face face) {
297 SkFaceRec* rec = gFaceRecHead;
298 SkFaceRec* prev = NULL;
299 while (rec) {
300 SkFaceRec* next = rec->fNext;
301 if (rec->fFace == face) {
302 if (--rec->fRefCnt == 0) {
303 if (prev) {
304 prev->fNext = next;
305 } else {
306 gFaceRecHead = next;
307 }
308 FT_Done_Face(face);
309 SkDELETE(rec);
310 }
311 return;
312 }
313 prev = rec;
314 rec = next;
315 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000316 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317}
318
319///////////////////////////////////////////////////////////////////////////
320
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000321// Work around for old versions of freetype.
322static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
323 FT_Int32 loadFlags, FT_Fixed* advances) {
324#ifdef FT_ADVANCES_H
325 return FT_Get_Advances(face, start, count, loadFlags, advances);
326#else
327 if (!face || start >= face->num_glyphs ||
328 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
329 return 6; // "Invalid argument."
330 }
331 if (count == 0)
332 return 0;
333
334 for (int i = 0; i < count; i++) {
335 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
336 if (err)
337 return err;
338 advances[i] = face->glyph->advance.x;
339 }
340
341 return 0;
342#endif
343}
344
345static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000346#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000347 FT_UShort fsType = FT_Get_FSType_Flags(face);
348 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
349 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
350#else
351 // No embedding is 0x2 and bitmap embedding only is 0x200.
352 TT_OS2* os2_table;
353 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
354 return (os2_table->fsType & 0x202) == 0;
355 }
356 return false; // We tried, fail safe.
357#endif
358}
359
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000360static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
361 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
362 if (!glyph_id)
363 return false;
364 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
365 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
366 return true;
367}
368
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000369static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000370 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000371 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
372 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000373 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000374 SkASSERT(data);
375 *data = advance;
376 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000377}
378
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000379static void populate_glyph_to_unicode(FT_Face& face,
380 SkTDArray<SkUnichar>* glyphToUnicode) {
381 // Check and see if we have Unicode cmaps.
382 for (int i = 0; i < face->num_charmaps; ++i) {
383 // CMaps known to support Unicode:
384 // Platform ID Encoding ID Name
385 // ----------- ----------- -----------------------------------
386 // 0 0,1 Apple Unicode
387 // 0 3 Apple Unicode 2.0 (preferred)
388 // 3 1 Microsoft Unicode UCS-2
389 // 3 10 Microsoft Unicode UCS-4 (preferred)
390 //
391 // See Apple TrueType Reference Manual
392 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
393 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
394 // Microsoft OpenType Specification
395 // http://www.microsoft.com/typography/otspec/cmap.htm
396
397 FT_UShort platformId = face->charmaps[i]->platform_id;
398 FT_UShort encodingId = face->charmaps[i]->encoding_id;
399
400 if (platformId != 0 && platformId != 3) {
401 continue;
402 }
403 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
404 continue;
405 }
406 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
407 (platformId == 0 && encodingId == 3));
408
409 FT_Set_Charmap(face, face->charmaps[i]);
410 if (glyphToUnicode->isEmpty()) {
411 glyphToUnicode->setCount(face->num_glyphs);
412 memset(glyphToUnicode->begin(), 0,
413 sizeof(SkUnichar) * face->num_glyphs);
414 }
415
416 // Iterate through each cmap entry.
417 FT_UInt glyphIndex;
418 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
419 glyphIndex != 0;
420 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
421 if (charCode &&
422 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
423 (*glyphToUnicode)[glyphIndex] = charCode;
424 }
425 }
426 }
427}
428
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000429// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000430SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000431 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000432 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
433 const uint32_t* glyphIDs,
434 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000435#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000436 return NULL;
437#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000438 SkAutoMutexAcquire ac(gFTMutex);
439 FT_Library libInit = NULL;
440 if (gFTCount == 0) {
441 if (!InitFreetype())
442 sk_throw();
443 libInit = gFTLibrary;
444 }
445 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
446 SkFaceRec* rec = ref_ft_face(fontID);
447 if (NULL == rec)
448 return NULL;
449 FT_Face face = rec->fFace;
450
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000451 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
452 info->fFontName.set(FT_Get_Postscript_Name(face));
453 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
454 info->fLastGlyphID = face->num_glyphs - 1;
455 info->fEmSize = 1000;
456
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000457 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000459 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000460 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000463 cid = true;
464 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000466 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000467 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000468 cid = true;
469 TT_Header* ttHeader;
470 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
471 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000472 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000473 }
474 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000475
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000476 info->fStyle = 0;
477 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000478 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000479 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000480 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000481 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
482 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000483 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
484
485 PS_FontInfoRec ps_info;
486 TT_Postscript* tt_info;
487 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
488 info->fItalicAngle = ps_info.italic_angle;
489 } else if ((tt_info =
490 (TT_Postscript*)FT_Get_Sfnt_Table(face,
491 ft_sfnt_post)) != NULL) {
492 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
493 } else {
494 info->fItalicAngle = 0;
495 }
496
497 info->fAscent = face->ascender;
498 info->fDescent = face->descender;
499
500 // Figure out a good guess for StemV - Min width of i, I, !, 1.
501 // This probably isn't very good with an italic font.
502 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000503 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000504 char stem_chars[] = {'i', 'I', '!', '1'};
505 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
506 FT_BBox bbox;
507 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
508 int16_t width = bbox.xMax - bbox.xMin;
509 if (width > 0 && width < min_width) {
510 min_width = width;
511 info->fStemV = min_width;
512 }
513 }
514 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000515
516 TT_PCLT* pclt_info;
517 TT_OS2* os2_table;
518 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
519 info->fCapHeight = pclt_info->CapHeight;
520 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
521 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000522 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000523 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000524 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000525 } else if ((os2_table =
526 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
527 info->fCapHeight = os2_table->sCapHeight;
528 } else {
529 // Figure out a good guess for CapHeight: average the height of M and X.
530 FT_BBox m_bbox, x_bbox;
531 bool got_m, got_x;
532 got_m = GetLetterCBox(face, 'M', &m_bbox);
533 got_x = GetLetterCBox(face, 'X', &x_bbox);
534 if (got_m && got_x) {
535 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
536 x_bbox.yMin) / 2;
537 } else if (got_m && !got_x) {
538 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
539 } else if (!got_m && got_x) {
540 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
541 }
542 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000543
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000544 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
545 face->bbox.xMax, face->bbox.yMin);
546
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000547 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000548 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
549 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
550 }
551
552 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000553 if (FT_IS_FIXED_WIDTH(face)) {
554 appendRange(&info->fGlyphWidths, 0);
555 int16_t advance = face->max_advance_width;
556 info->fGlyphWidths->fAdvance.append(1, &advance);
557 finishRange(info->fGlyphWidths.get(), 0,
558 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000559 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000560 appendRange(&info->fGlyphWidths, 0);
561 // So as to not blow out the stack, get advances in batches.
562 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
563 FT_Fixed advances[128];
564 int advanceCount = 128;
565 if (gID + advanceCount > face->num_glyphs)
566 advanceCount = face->num_glyphs - gID + 1;
567 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
568 advances);
569 for (int i = 0; i < advanceCount; i++) {
570 int16_t advance = advances[gID + i];
571 info->fGlyphWidths->fAdvance.append(1, &advance);
572 }
573 }
574 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
575 SkAdvancedTypefaceMetrics::WidthRange::kRange);
576 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000577 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000578 getAdvanceData(face,
579 face->num_glyphs,
580 glyphIDs,
581 glyphIDsCount,
582 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000583 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000584 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000585
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000586 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
587 FT_HAS_VERTICAL(face)) {
588 SkASSERT(false); // Not implemented yet.
589 }
590
591 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
592 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
593 // Postscript fonts may contain more than 255 glyphs, so we end up
594 // using multiple font descriptions with a glyph ordering. Record
595 // the name of each glyph.
596 info->fGlyphNames.reset(
597 new SkAutoTArray<SkString>(face->num_glyphs));
598 for (int gID = 0; gID < face->num_glyphs; gID++) {
599 char glyphName[128]; // PS limit for names is 127 bytes.
600 FT_Get_Glyph_Name(face, gID, glyphName, 128);
601 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000602 }
603 }
604
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000605 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
606 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
607 face->num_charmaps) {
608 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
609 }
610
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000611 if (!canEmbed(face))
612 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
613
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000614 unref_ft_face(face);
615 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000616#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000617}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000618
reed@google.com618ef5e2011-01-26 22:10:41 +0000619///////////////////////////////////////////////////////////////////////////
620
reed@google.comffe49f52011-11-22 19:42:41 +0000621#define BLACK_LUMINANCE_LIMIT 0x40
622#define WHITE_LUMINANCE_LIMIT 0xA0
623
reed@google.com8ed436c2011-07-21 14:12:36 +0000624static bool bothZero(SkScalar a, SkScalar b) {
625 return 0 == a && 0 == b;
626}
627
628// returns false if there is any non-90-rotation or skew
629static bool isAxisAligned(const SkScalerContext::Rec& rec) {
630 return 0 == rec.fPreSkewX &&
631 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
632 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
633}
634
reed@google.com618ef5e2011-01-26 22:10:41 +0000635void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000636 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
637 //Cap the requested size as larger sizes give bogus values.
638 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000639 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
640 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000641 }
642
reed@google.com618ef5e2011-01-26 22:10:41 +0000643 if (!gLCDSupportValid) {
644 InitFreetype();
645 FT_Done_FreeType(gFTLibrary);
646 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000647
reed@google.comeffc5012011-06-27 16:44:46 +0000648 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000649 // If the runtime Freetype library doesn't support LCD mode, we disable
650 // it here.
651 rec->fMaskFormat = SkMask::kA8_Format;
652 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000653
reed@google.com618ef5e2011-01-26 22:10:41 +0000654 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000655 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000656 // collapse full->normal hinting if we're not doing LCD
657 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000658 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000659 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000660 if (SkPaint::kNo_Hinting != h) {
661 h = SkPaint::kSlight_Hinting;
662 }
663 }
664
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000665#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000666 // rotated text looks bad with hinting, so we disable it as needed
667 if (!isAxisAligned(*rec)) {
668 h = SkPaint::kNo_Hinting;
669 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000670#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000671 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000672
reed@google.com1ac83502012-02-28 17:06:02 +0000673#ifndef SK_USE_COLOR_LUMINANCE
reed@google.comffe49f52011-11-22 19:42:41 +0000674 // for compatibility at the moment, discretize luminance to 3 settings
675 // black, white, gray. This helps with fontcache utilization, since we
676 // won't create multiple entries that in the end map to the same results.
677 {
678 unsigned lum = rec->getLuminanceByte();
679 if (gGammaTables[0] || gGammaTables[1]) {
680 if (lum <= BLACK_LUMINANCE_LIMIT) {
681 lum = 0;
682 } else if (lum >= WHITE_LUMINANCE_LIMIT) {
683 lum = SkScalerContext::kLuminance_Max;
684 } else {
685 lum = SkScalerContext::kLuminance_Max >> 1;
686 }
687 } else {
688 lum = 0; // no gamma correct, so use 0 since SkPaint uses that
689 // when measuring text w/o regard for luminance
690 }
691 rec->setLuminanceBits(lum);
692 }
reed@google.com1ac83502012-02-28 17:06:02 +0000693#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000694}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000695
djsollen@google.comda957722011-11-16 17:00:46 +0000696#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000697uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
698 SkAutoMutexAcquire ac(gFTMutex);
699 SkFaceRec *rec = ref_ft_face(fontID);
700 uint16_t unitsPerEm = 0;
701
702 if (rec != NULL && rec->fFace != NULL) {
703 unitsPerEm = rec->fFace->units_per_EM;
704 unref_ft_face(rec->fFace);
705 }
706
707 return (uint32_t)unitsPerEm;
708}
709#endif
710
reed@android.com8a1c16f2008-12-17 15:59:43 +0000711SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000712 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 SkAutoMutexAcquire ac(gFTMutex);
714
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000716 if (!InitFreetype()) {
717 sk_throw();
718 }
reed@google.comffe49f52011-11-22 19:42:41 +0000719 SkFontHost::GetGammaTables(gGammaTables);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720 }
721 ++gFTCount;
722
723 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000724 fFTSize = NULL;
725 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000726 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000727 if (NULL == fFaceRec) {
728 return;
729 }
730 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731
732 // compute our factors from the record
733
734 SkMatrix m;
735
736 fRec.getSingleMatrix(&m);
737
738#ifdef DUMP_STRIKE_CREATION
739 SkString keyString;
740 SkFontHost::GetDescriptorKeyString(desc, &keyString);
741 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
742 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
743 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
744 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000745 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746#endif
747
748 // now compute our scale factors
749 SkScalar sx = m.getScaleX();
750 SkScalar sy = m.getScaleY();
751
752 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
753 // sort of give up on hinting
754 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
755 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
756 sx = sy = SkScalarAve(sx, sy);
757
758 SkScalar inv = SkScalarInvert(sx);
759
760 // flip the skew elements to go from our Y-down system to FreeType's
761 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
762 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
763 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
764 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
765 } else {
766 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
767 fMatrix22.xy = fMatrix22.yx = 0;
768 }
769
770 fScaleX = SkScalarToFixed(sx);
771 fScaleY = SkScalarToFixed(sy);
772
reed@google.coma1bfa212012-03-08 21:57:12 +0000773 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
774
reed@android.com8a1c16f2008-12-17 15:59:43 +0000775 // compute the flags we send to Load_Glyph
776 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000777 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000778 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000779
agl@chromium.org70a303f2010-05-10 14:15:50 +0000780 if (SkMask::kBW_Format == fRec.fMaskFormat) {
781 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
782 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000783 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000784 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000785 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000786 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000787 } else {
788 switch (fRec.getHinting()) {
789 case SkPaint::kNo_Hinting:
790 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000791 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000792 break;
793 case SkPaint::kSlight_Hinting:
794 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
795 break;
796 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000797 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
798 loadFlags = FT_LOAD_FORCE_AUTOHINT;
799 else
800 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000801 break;
802 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000803 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
804 loadFlags = FT_LOAD_FORCE_AUTOHINT;
805 break;
806 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000807 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000808 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000809 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000810 loadFlags = FT_LOAD_TARGET_LCD_V;
811 } else {
812 loadFlags = FT_LOAD_TARGET_LCD;
813 }
reed@google.comea2333d2011-03-14 16:44:56 +0000814 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000815 break;
816 default:
817 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
818 break;
819 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 }
821
reed@google.comeffc5012011-06-27 16:44:46 +0000822 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000823 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000824 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000825
reed@google.com96a9f7912011-05-06 11:49:30 +0000826 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
827 // advances, as fontconfig and cairo do.
828 // See http://code.google.com/p/skia/issues/detail?id=222.
829 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
830
reed@android.come4d0bc02009-07-24 19:53:20 +0000831 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000832 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833 }
834
835 // now create the FT_Size
836
837 {
838 FT_Error err;
839
840 err = FT_New_Size(fFace, &fFTSize);
841 if (err != 0) {
842 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
843 fFaceRec->fFontID, fScaleX, fScaleY, err));
844 fFace = NULL;
845 return;
846 }
847
848 err = FT_Activate_Size(fFTSize);
849 if (err != 0) {
850 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
851 fFaceRec->fFontID, fScaleX, fScaleY, err));
852 fFTSize = NULL;
853 }
854
855 err = FT_Set_Char_Size( fFace,
856 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
857 72, 72);
858 if (err != 0) {
859 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
860 fFaceRec->fFontID, fScaleX, fScaleY, err));
861 fFace = NULL;
862 return;
863 }
864
865 FT_Set_Transform( fFace, &fMatrix22, NULL);
866 }
867}
868
869SkScalerContext_FreeType::~SkScalerContext_FreeType() {
870 if (fFTSize != NULL) {
871 FT_Done_Size(fFTSize);
872 }
873
874 SkAutoMutexAcquire ac(gFTMutex);
875
876 if (fFace != NULL) {
877 unref_ft_face(fFace);
878 }
879 if (--gFTCount == 0) {
880// SkDEBUGF(("FT_Done_FreeType\n"));
881 FT_Done_FreeType(gFTLibrary);
882 SkDEBUGCODE(gFTLibrary = NULL;)
883 }
884}
885
886/* We call this before each use of the fFace, since we may be sharing
887 this face with other context (at different sizes).
888*/
889FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 FT_Error err = FT_Activate_Size(fFTSize);
891
892 if (err != 0) {
893 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
894 fFaceRec->fFontID, fScaleX, fScaleY, err));
895 fFTSize = NULL;
896 } else {
897 // seems we need to reset this every time (not sure why, but without it
898 // I get random italics from some other fFTSize)
899 FT_Set_Transform( fFace, &fMatrix22, NULL);
900 }
901 return err;
902}
903
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000904void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
905 FT_Pos strength;
906 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
907 / 24;
908 FT_Outline_Embolden(outline, strength);
909}
910
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000911unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 return fFace->num_glyphs;
913}
914
915uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
916 return SkToU16(FT_Get_Char_Index( fFace, uni ));
917}
918
reed@android.com9d3a9852010-01-08 14:07:42 +0000919SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
920 // iterate through each cmap entry, looking for matching glyph indices
921 FT_UInt glyphIndex;
922 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
923
924 while (glyphIndex != 0) {
925 if (glyphIndex == glyph) {
926 return charCode;
927 }
928 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
929 }
930
931 return 0;
932}
933
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
935 switch (format) {
936 case SkMask::kBW_Format:
937 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938 case SkMask::kA8_Format:
939 default:
940 return FT_PIXEL_MODE_GRAY;
941 }
942}
943
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
945#ifdef FT_ADVANCES_H
946 /* unhinted and light hinted text have linearly scaled advances
947 * which are very cheap to compute with some font formats...
948 */
reed@google.combdc99882011-11-21 14:36:57 +0000949 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 SkAutoMutexAcquire ac(gFTMutex);
951
952 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000953 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000954 return;
955 }
956
957 FT_Error error;
958 FT_Fixed advance;
959
960 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
961 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
962 &advance );
963 if (0 == error) {
964 glyph->fRsbDelta = 0;
965 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +0000966 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
967 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000968 return;
969 }
970 }
971#endif /* FT_ADVANCES_H */
972 /* otherwise, we need to load/hint the glyph, which is slower */
973 this->generateMetrics(glyph);
974 return;
975}
976
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000977void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
978 FT_BBox* bbox,
979 bool snapToPixelBoundary) {
980
981 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
982
983 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
984 int dx = FixedToDot6(glyph->getSubXFixed());
985 int dy = FixedToDot6(glyph->getSubYFixed());
986 // negate dy since freetype-y-goes-up and skia-y-goes-down
987 bbox->xMin += dx;
988 bbox->yMin -= dy;
989 bbox->xMax += dx;
990 bbox->yMax -= dy;
991 }
992
993 // outset the box to integral boundaries
994 if (snapToPixelBoundary) {
995 bbox->xMin &= ~63;
996 bbox->yMin &= ~63;
997 bbox->xMax = (bbox->xMax + 63) & ~63;
998 bbox->yMax = (bbox->yMax + 63) & ~63;
999 }
1000}
1001
1002void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1003 if (isLCD(fRec)) {
1004 if (fLCDIsVert) {
1005 glyph->fHeight += gLCDExtra;
1006 glyph->fTop -= gLCDExtra >> 1;
1007 } else {
1008 glyph->fWidth += gLCDExtra;
1009 glyph->fLeft -= gLCDExtra >> 1;
1010 }
1011 }
1012}
bungeman@google.com875eb982012-04-12 15:53:23 +00001013void SkScalerContext_FreeType::updateGlyphPosIfLCD(SkGlyph* glyph) {
1014 if (isLCD(fRec)) {
1015 if (fLCDIsVert) {
1016 glyph->fTop -= gLCDExtra >> 1;
1017 } else {
1018 glyph->fLeft -= gLCDExtra >> 1;
1019 }
1020 }
1021}
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001022
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1024 SkAutoMutexAcquire ac(gFTMutex);
1025
1026 glyph->fRsbDelta = 0;
1027 glyph->fLsbDelta = 0;
1028
1029 FT_Error err;
1030
1031 if (this->setupSize()) {
1032 goto ERROR;
1033 }
1034
1035 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1036 if (err != 0) {
1037 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1038 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1039 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001040 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001041 return;
1042 }
1043
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001044 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001045
reed@android.com8a1c16f2008-12-17 15:59:43 +00001046 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001047 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001048 FT_BBox bbox;
1049
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001050 if (0 == fFace->glyph->outline.n_contours) {
1051 glyph->fWidth = 0;
1052 glyph->fHeight = 0;
1053 glyph->fTop = 0;
1054 glyph->fLeft = 0;
1055 break;
1056 }
1057
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001058 if (fRec.fFlags & kEmbolden_Flag) {
1059 emboldenOutline(&fFace->glyph->outline);
1060 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001062 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001063
1064 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1065 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1066 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1067 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001068
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001069 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
1070 vLeft = Dot6ToFixed(bbox.xMin);
1071 vTop = Dot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001072 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001073
1074 updateGlyphIfLCD(glyph);
1075
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001077 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001078
1079 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001080 if (fRec.fFlags & kEmbolden_Flag) {
1081 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1082 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1083 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1085 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1086 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1087 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1088 break;
1089
1090 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001091 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001092 goto ERROR;
1093 }
1094
bungeman@google.com34f10262012-03-23 18:11:47 +00001095 if (fDoLinearMetrics) {
1096 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1097 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1098 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001099 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1100 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001101
reed@android.com8a1c16f2008-12-17 15:59:43 +00001102 if (fRec.fFlags & kDevKernText_Flag) {
1103 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1104 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1105 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 }
1107
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001108 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1109 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1110
1111 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1112
1113 FT_Matrix identityMatrix;
1114 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1115 identityMatrix.xy = identityMatrix.yx = 0;
1116
1117 // if the matrix is not the identity matrix then we need to re-load the
1118 // glyph with the identity matrix to get the necessary bounding box
1119 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1120
1121 FT_Set_Transform(fFace, &identityMatrix, NULL);
1122
1123 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1124 if (err != 0) {
1125 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1126 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1127 goto ERROR;
1128 }
1129
1130 if (fRec.fFlags & kEmbolden_Flag) {
1131 emboldenOutline(&fFace->glyph->outline);
1132 }
1133 }
1134
1135 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001136 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001137 getBBoxForCurrentGlyph(glyph, &bbox);
1138
1139 // compute the vertical gap above and below the glyph if the glyph were
1140 // centered within the linearVertAdvance
1141 SkFixed vGap = (fFace->glyph->linearVertAdvance - Dot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
1142
1143 // the origin point of the glyph when rendered vertically
1144 FT_Vector vOrigin;
1145 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
1146 vOrigin.y = vGap + Dot6ToFixed(bbox.yMax);
1147
1148 // transform the vertical origin based on the matrix of the actual glyph
1149 FT_Vector_Transform(&vOrigin, &fMatrix22);
1150
1151 // compute a new offset vector for the glyph by subtracting the vertical
1152 // origin from the original horizontal offset vector
1153 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1154 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1155
bungeman@google.com875eb982012-04-12 15:53:23 +00001156 updateGlyphPosIfLCD(glyph);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001157
1158 // use the vertical advance values computed by freetype
1159 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1160 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1161 }
1162
1163
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164#ifdef ENABLE_GLYPH_SPEW
1165 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1166 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1167#endif
1168}
1169
reed@google.com1ac83502012-02-28 17:06:02 +00001170///////////////////////////////////////////////////////////////////////////////
1171
bungeman@google.com3aacb412012-03-13 14:55:12 +00001172#ifdef SK_USE_COLOR_LUMINANCE
1173
1174static float apply_contrast(float srca, float contrast) {
1175 return srca + ((1.0f - srca) * contrast * srca);
reed@google.comc5181342011-05-17 20:52:46 +00001176}
1177
bungeman@google.com3aacb412012-03-13 14:55:12 +00001178#ifdef SK_GAMMA_SRGB
1179static float lin(float per) {
1180 if (per <= 0.04045f) {
1181 return per / 12.92f;
reed@google.comc5181342011-05-17 20:52:46 +00001182 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001183 return powf((per + 0.055f) / 1.055, 2.4f);
reed@google.com1ac83502012-02-28 17:06:02 +00001184}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001185static float per(float lin) {
1186 if (lin <= 0.0031308f) {
1187 return lin * 12.92f;
reed@google.com1ac83502012-02-28 17:06:02 +00001188 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001189 return 1.055f * powf(lin, 1.0f / 2.4f) - 0.055f;
1190}
1191#else //SK_GAMMA_SRGB
1192static float lin(float per) {
1193 const float g = SK_GAMMA_EXPONENT;
1194 return powf(per, g);
1195}
1196static float per(float lin) {
1197 const float g = SK_GAMMA_EXPONENT;
1198 return powf(lin, 1.0f / g);
1199}
1200#endif //SK_GAMMA_SRGB
reed@google.com1ac83502012-02-28 17:06:02 +00001201
bungeman@google.com3aacb412012-03-13 14:55:12 +00001202static void build_gamma_table(uint8_t table[256], int srcI) {
1203 const float src = (float)srcI / 255.0f;
1204 const float linSrc = lin(src);
1205 const float linDst = 1.0f - linSrc;
1206 const float dst = per(linDst);
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001207
reed@google.com613e9fe2012-02-29 15:08:00 +00001208 // have our contrast value taper off to 0 as the src luminance becomes white
bungeman@google.com3aacb412012-03-13 14:55:12 +00001209 const float contrast = SK_GAMMA_CONTRAST / 255.0f * linDst;
1210 const float step = 1.0f / 256.0f;
reed@google.com1ac83502012-02-28 17:06:02 +00001211
bungeman@google.com3aacb412012-03-13 14:55:12 +00001212 //Remove discontinuity and instability when src is close to dst.
1213 if (fabs(src - dst) < 0.01f) {
1214 float rawSrca = 0.0f;
1215 for (int i = 0; i < 256; ++i, rawSrca += step) {
1216 float srca = apply_contrast(rawSrca, contrast);
1217 table[i] = sk_float_round2int(255.0f * srca);
1218 }
1219 } else {
1220 float rawSrca = 0.0f;
1221 for (int i = 0; i < 256; ++i, rawSrca += step) {
1222 float srca = apply_contrast(rawSrca, contrast);
1223 SkASSERT(srca <= 1.0f);
1224 float dsta = 1 - srca;
reed@google.com1ac83502012-02-28 17:06:02 +00001225
bungeman@google.com3aacb412012-03-13 14:55:12 +00001226 //Calculate the output we want.
1227 float linOut = (linSrc * srca + dsta * linDst);
1228 SkASSERT(linOut <= 1.0f);
1229 float out = per(linOut);
reed@google.com1ac83502012-02-28 17:06:02 +00001230
bungeman@google.com3aacb412012-03-13 14:55:12 +00001231 //Undo what the blit blend will do.
1232 float result = (out - dst) / (src - dst);
1233 SkASSERT(sk_float_round2int(255.0f * result) <= 255);
1234
1235 table[i] = sk_float_round2int(255.0f * result);
1236 }
reed@google.com1ac83502012-02-28 17:06:02 +00001237 }
1238}
1239
1240static const uint8_t* getGammaTable(U8CPU luminance) {
1241 static uint8_t gGammaTables[4][256];
1242 static bool gInited;
1243 if (!gInited) {
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001244 build_gamma_table(gGammaTables[0], 0x00);
1245 build_gamma_table(gGammaTables[1], 0x55);
1246 build_gamma_table(gGammaTables[2], 0xAA);
1247 build_gamma_table(gGammaTables[3], 0xFF);
reed@google.com1ac83502012-02-28 17:06:02 +00001248
1249 gInited = true;
1250 }
1251 SkASSERT(0 == (luminance >> 8));
1252 return gGammaTables[luminance >> 6];
1253}
1254
bungeman@google.com3aacb412012-03-13 14:55:12 +00001255#else //SK_USE_COLOR_LUMINANCE
reed@google.comd61b92b2012-03-02 16:02:07 +00001256static const uint8_t* getIdentityTable() {
1257 static bool gOnce;
1258 static uint8_t gIdentityTable[256];
1259 if (!gOnce) {
1260 for (int i = 0; i < 256; ++i) {
1261 gIdentityTable[i] = i;
1262 }
1263 gOnce = true;
1264 }
1265 return gIdentityTable;
1266}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001267#endif //SK_USE_COLOR_LUMINANCE
reed@google.com1ac83502012-02-28 17:06:02 +00001268
1269static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
reed@google.comc5181342011-05-17 20:52:46 +00001270 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1271}
1272
reed@google.com73824072011-06-23 13:17:30 +00001273static uint16_t grayToRGB16(U8CPU gray) {
1274 SkASSERT(gray <= 255);
1275 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1276}
1277
1278static int bittst(const uint8_t data[], int bitOffset) {
1279 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001280 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001281 return lowBit & 1;
1282}
1283
reed@google.comeffc5012011-06-27 16:44:46 +00001284static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
reed@google.coma1bfa212012-03-08 21:57:12 +00001285 int lcdIsBGR, bool lcdIsVert, const uint8_t* tableR,
reed@google.com1ac83502012-02-28 17:06:02 +00001286 const uint8_t* tableG, const uint8_t* tableB) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001287 if (lcdIsVert) {
1288 SkASSERT(3 * glyph.fHeight == bitmap.rows);
1289 } else {
1290 SkASSERT(glyph.fHeight == bitmap.rows);
1291 }
1292
reed@google.comea2333d2011-03-14 16:44:56 +00001293 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001294 const size_t dstRB = glyph.rowBytes();
1295 const int width = glyph.fWidth;
1296 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001297
reed@google.com73824072011-06-23 13:17:30 +00001298 switch (bitmap.pixel_mode) {
1299 case FT_PIXEL_MODE_MONO: {
1300 for (int y = 0; y < glyph.fHeight; ++y) {
1301 for (int x = 0; x < width; ++x) {
1302 dst[x] = -bittst(src, x);
1303 }
1304 dst = (uint16_t*)((char*)dst + dstRB);
1305 src += bitmap.pitch;
1306 }
1307 } break;
1308 case FT_PIXEL_MODE_GRAY: {
1309 for (int y = 0; y < glyph.fHeight; ++y) {
1310 for (int x = 0; x < width; ++x) {
1311 dst[x] = grayToRGB16(src[x]);
1312 }
1313 dst = (uint16_t*)((char*)dst + dstRB);
1314 src += bitmap.pitch;
1315 }
1316 } break;
1317 default: {
reed@google.coma1bfa212012-03-08 21:57:12 +00001318 SkASSERT(lcdIsVert || (glyph.fWidth * 3 == bitmap.width));
reed@google.com73824072011-06-23 13:17:30 +00001319 for (int y = 0; y < glyph.fHeight; y++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001320 if (lcdIsVert) { // vertical stripes
1321 const uint8_t* srcR = src;
1322 const uint8_t* srcG = srcR + bitmap.pitch;
1323 const uint8_t* srcB = srcG + bitmap.pitch;
1324 if (lcdIsBGR) {
1325 SkTSwap(srcR, srcB);
reed@google.comeffc5012011-06-27 16:44:46 +00001326 }
reed@google.comeffc5012011-06-27 16:44:46 +00001327 for (int x = 0; x < width; x++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001328 dst[x] = packTriple(tableR[*srcR++],
1329 tableG[*srcG++],
1330 tableB[*srcB++]);
reed@google.comeffc5012011-06-27 16:44:46 +00001331 }
reed@google.coma1bfa212012-03-08 21:57:12 +00001332 src += 3 * bitmap.pitch;
1333 } else { // horizontal stripes
1334 const uint8_t* triple = src;
1335 if (lcdIsBGR) {
1336 for (int x = 0; x < width; x++) {
1337 dst[x] = packTriple(tableR[triple[2]],
1338 tableG[triple[1]],
1339 tableB[triple[0]]);
1340 triple += 3;
1341 }
1342 } else {
1343 for (int x = 0; x < width; x++) {
1344 dst[x] = packTriple(tableR[triple[0]],
1345 tableG[triple[1]],
1346 tableB[triple[2]]);
1347 triple += 3;
1348 }
1349 }
1350 src += bitmap.pitch;
reed@google.com73824072011-06-23 13:17:30 +00001351 }
reed@google.com73824072011-06-23 13:17:30 +00001352 dst = (uint16_t*)((char*)dst + dstRB);
1353 }
1354 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001355 }
1356}
1357
reed@android.com8a1c16f2008-12-17 15:59:43 +00001358void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1359 SkAutoMutexAcquire ac(gFTMutex);
1360
1361 FT_Error err;
1362
1363 if (this->setupSize()) {
1364 goto ERROR;
1365 }
1366
1367 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1368 if (err != 0) {
1369 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1370 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1371 ERROR:
1372 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1373 return;
1374 }
1375
reed@google.com1ac83502012-02-28 17:06:02 +00001376#ifdef SK_USE_COLOR_LUMINANCE
1377 SkColor lumColor = fRec.getLuminanceColor();
1378 const uint8_t* tableR = getGammaTable(SkColorGetR(lumColor));
1379 const uint8_t* tableG = getGammaTable(SkColorGetG(lumColor));
1380 const uint8_t* tableB = getGammaTable(SkColorGetB(lumColor));
1381#else
1382 unsigned lum = fRec.getLuminanceByte();
reed@google.comd61b92b2012-03-02 16:02:07 +00001383 const uint8_t* tableR;
1384 const uint8_t* tableG;
1385 const uint8_t* tableB;
1386
1387 bool isWhite = lum >= WHITE_LUMINANCE_LIMIT;
1388 bool isBlack = lum <= BLACK_LUMINANCE_LIMIT;
1389 if ((gGammaTables[0] || gGammaTables[1]) && (isBlack || isWhite)) {
1390 tableR = tableG = tableB = gGammaTables[isBlack ? 0 : 1];
1391 } else {
1392 tableR = tableG = tableB = getIdentityTable();
1393 }
reed@google.com1ac83502012-02-28 17:06:02 +00001394#endif
1395
reed@google.coma1bfa212012-03-08 21:57:12 +00001396 const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
1397 const bool doVert = fLCDIsVert;
1398
reed@android.com8a1c16f2008-12-17 15:59:43 +00001399 switch ( fFace->glyph->format ) {
1400 case FT_GLYPH_FORMAT_OUTLINE: {
1401 FT_Outline* outline = &fFace->glyph->outline;
1402 FT_BBox bbox;
1403 FT_Bitmap target;
1404
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001405 if (fRec.fFlags & kEmbolden_Flag) {
1406 emboldenOutline(outline);
1407 }
1408
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001410 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001411 dx = FixedToDot6(glyph.getSubXFixed());
1412 dy = FixedToDot6(glyph.getSubYFixed());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413 // negate dy since freetype-y-goes-up and skia-y-goes-down
1414 dy = -dy;
1415 }
1416 FT_Outline_Get_CBox(outline, &bbox);
1417 /*
1418 what we really want to do for subpixel is
1419 offset(dx, dy)
1420 compute_bounds
1421 offset(bbox & !63)
1422 but that is two calls to offset, so we do the following, which
1423 achieves the same thing with only one offset call.
1424 */
1425 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1426 dy - ((bbox.yMin + dy) & ~63));
1427
reed@google.comea2333d2011-03-14 16:44:56 +00001428 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001429 FT_Render_Glyph(fFace->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD);
1430 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001431 tableR, tableG, tableB);
reed@google.comea2333d2011-03-14 16:44:56 +00001432 } else {
1433 target.width = glyph.fWidth;
1434 target.rows = glyph.fHeight;
1435 target.pitch = glyph.rowBytes();
1436 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1437 target.pixel_mode = compute_pixel_mode(
1438 (SkMask::Format)fRec.fMaskFormat);
1439 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440
reed@google.comea2333d2011-03-14 16:44:56 +00001441 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1442 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1443 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001444 } break;
1445
1446 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001447 if (fRec.fFlags & kEmbolden_Flag) {
1448 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1449 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1450 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001451 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1452 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1453 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1454 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1455
1456 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1457 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001458
agl@chromium.org558434a2009-08-11 17:22:38 +00001459 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1460 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1461 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001462 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1463 unsigned dstRowBytes = glyph.rowBytes();
1464 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1465 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1466
1467 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1468 memcpy(dst, src, minRowBytes);
1469 memset(dst + minRowBytes, 0, extraRowBytes);
1470 src += srcRowBytes;
1471 dst += dstRowBytes;
1472 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001473 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001474 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001475 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1476 uint8_t byte = 0;
1477 int bits = 0;
1478 const uint8_t* src_row = src;
1479 uint8_t* dst_row = dst;
1480
1481 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1482 if (!bits) {
1483 byte = *src_row++;
1484 bits = 8;
1485 }
1486
1487 *dst_row++ = byte & 0x80 ? 0xff : 0;
1488 bits--;
1489 byte <<= 1;
1490 }
1491
1492 src += fFace->glyph->bitmap.pitch;
1493 dst += glyph.rowBytes();
1494 }
reed@google.com73824072011-06-23 13:17:30 +00001495 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001496 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001497 tableR, tableG, tableB);
agl@chromium.org558434a2009-08-11 17:22:38 +00001498 } else {
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001499 SkDEBUGFAIL("unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001500 }
1501 } break;
1502
1503 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001504 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001505 goto ERROR;
1506 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001507
reed@google.comd61b92b2012-03-02 16:02:07 +00001508// We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum,
1509// it is optional
1510#if defined(SK_GAMMA_APPLY_TO_A8) || !defined(SK_USE_COLOR_LUMINANCE)
reed@google.com1ac83502012-02-28 17:06:02 +00001511 if (SkMask::kA8_Format == glyph.fMaskFormat) {
1512 SkASSERT(tableR == tableG && tableR == tableB);
1513 const uint8_t* table = tableR;
1514 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1515 unsigned rowBytes = glyph.rowBytes();
1516
1517 for (int y = glyph.fHeight - 1; y >= 0; --y) {
1518 for (int x = glyph.fWidth - 1; x >= 0; --x) {
1519 dst[x] = table[dst[x]];
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001520 }
reed@google.com1ac83502012-02-28 17:06:02 +00001521 dst += rowBytes;
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001522 }
1523 }
reed@google.com1ac83502012-02-28 17:06:02 +00001524#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001525}
1526
1527///////////////////////////////////////////////////////////////////////////////
1528
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001529#define ft2sk(x) SkFixedToScalar(Dot6ToFixed(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +00001530
reed@android.com6f252972009-01-14 16:46:16 +00001531#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001532 #define CONST_PARAM const
1533#else // older freetype doesn't use const here
1534 #define CONST_PARAM
1535#endif
1536
1537static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1538 SkPath* path = (SkPath*)ctx;
1539 path->close(); // to close the previous contour (if any)
1540 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1541 return 0;
1542}
1543
1544static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1545 SkPath* path = (SkPath*)ctx;
1546 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1547 return 0;
1548}
1549
1550static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1551 void* ctx) {
1552 SkPath* path = (SkPath*)ctx;
1553 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1554 return 0;
1555}
1556
1557static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1558 CONST_PARAM FT_Vector* pt2, void* ctx) {
1559 SkPath* path = (SkPath*)ctx;
1560 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1561 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1562 return 0;
1563}
1564
1565void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1566 SkPath* path) {
1567 SkAutoMutexAcquire ac(gFTMutex);
1568
1569 SkASSERT(&glyph && path);
1570
1571 if (this->setupSize()) {
1572 path->reset();
1573 return;
1574 }
1575
1576 uint32_t flags = fLoadGlyphFlags;
1577 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1578 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1579
1580 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1581
1582 if (err != 0) {
1583 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1584 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1585 path->reset();
1586 return;
1587 }
1588
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001589 if (fRec.fFlags & kEmbolden_Flag) {
1590 emboldenOutline(&fFace->glyph->outline);
1591 }
1592
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593 FT_Outline_Funcs funcs;
1594
1595 funcs.move_to = move_proc;
1596 funcs.line_to = line_proc;
1597 funcs.conic_to = quad_proc;
1598 funcs.cubic_to = cubic_proc;
1599 funcs.shift = 0;
1600 funcs.delta = 0;
1601
1602 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1603
1604 if (err != 0) {
1605 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1606 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1607 path->reset();
1608 return;
1609 }
1610
1611 path->close();
1612}
1613
1614void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1615 SkPaint::FontMetrics* my) {
1616 if (NULL == mx && NULL == my) {
1617 return;
1618 }
1619
1620 SkAutoMutexAcquire ac(gFTMutex);
1621
1622 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001623 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001624 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001625 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001626 }
1627 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001628 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629 }
1630 return;
1631 }
1632
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001633 FT_Face face = fFace;
1634 int upem = face->units_per_EM;
1635 if (upem <= 0) {
1636 goto ERROR;
1637 }
1638
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001639 SkPoint pts[6];
1640 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001641 SkFixed scaleY = fScaleY;
1642 SkFixed mxy = fMatrix22.xy;
1643 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001644 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1645 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001646
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001647 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001648 if (leading < 0) {
1649 leading = 0;
1650 }
1651
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001652 // Try to get the OS/2 table from the font. This contains the specific
1653 // average font width metrics which Windows uses.
1654 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1655
reed@android.com8a1c16f2008-12-17 15:59:43 +00001656 ys[0] = -face->bbox.yMax;
1657 ys[1] = -face->ascender;
1658 ys[2] = -face->descender;
1659 ys[3] = -face->bbox.yMin;
1660 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001661 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1662
1663 SkScalar x_height;
1664 if (os2 && os2->sxHeight) {
1665 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1666 } else {
1667 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1668 if (x_glyph) {
1669 FT_BBox bbox;
1670 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001671 if (fRec.fFlags & kEmbolden_Flag) {
1672 emboldenOutline(&fFace->glyph->outline);
1673 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001674 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001675 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001676 } else {
1677 x_height = 0;
1678 }
1679 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001680
1681 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001682 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001683 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1684 SkFixed x = SkFixedMul(mxy, y);
1685 y = SkFixedMul(myy, y);
1686 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1687 }
1688
1689 if (mx) {
1690 mx->fTop = pts[0].fX;
1691 mx->fAscent = pts[1].fX;
1692 mx->fDescent = pts[2].fX;
1693 mx->fBottom = pts[3].fX;
1694 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001695 mx->fAvgCharWidth = pts[5].fX;
1696 mx->fXMin = xmin;
1697 mx->fXMax = xmax;
1698 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001699 }
1700 if (my) {
1701 my->fTop = pts[0].fY;
1702 my->fAscent = pts[1].fY;
1703 my->fDescent = pts[2].fY;
1704 my->fBottom = pts[3].fY;
1705 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001706 my->fAvgCharWidth = pts[5].fY;
1707 my->fXMin = xmin;
1708 my->fXMax = xmax;
1709 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001710 }
1711}
1712
1713////////////////////////////////////////////////////////////////////////
1714////////////////////////////////////////////////////////////////////////
1715
1716SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001717 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1718 if (!c->success()) {
1719 SkDELETE(c);
1720 c = NULL;
1721 }
1722 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001723}
1724
1725///////////////////////////////////////////////////////////////////////////////
1726
1727/* Export this so that other parts of our FonttHost port can make use of our
1728 ability to extract the name+style from a stream, using FreeType's api.
1729*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001730bool find_name_and_attributes(SkStream* stream, SkString* name,
1731 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001732 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001733 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001734 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001735 }
1736
1737 FT_Open_Args args;
1738 memset(&args, 0, sizeof(args));
1739
1740 const void* memoryBase = stream->getMemoryBase();
1741 FT_StreamRec streamRec;
1742
1743 if (NULL != memoryBase) {
1744 args.flags = FT_OPEN_MEMORY;
1745 args.memory_base = (const FT_Byte*)memoryBase;
1746 args.memory_size = stream->getLength();
1747 } else {
1748 memset(&streamRec, 0, sizeof(streamRec));
1749 streamRec.size = stream->read(NULL, 0);
1750 streamRec.descriptor.pointer = stream;
1751 streamRec.read = sk_stream_read;
1752 streamRec.close = sk_stream_close;
1753
1754 args.flags = FT_OPEN_STREAM;
1755 args.stream = &streamRec;
1756 }
1757
1758 FT_Face face;
1759 if (FT_Open_Face(library, &args, 0, &face)) {
1760 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001761 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001762 }
1763
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001764 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001765 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001766 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001767 }
1768 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001769 tempStyle |= SkTypeface::kItalic;
1770 }
1771
1772 if (name) {
1773 name->set(face->family_name);
1774 }
1775 if (style) {
1776 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001777 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001778 if (isFixedWidth) {
1779 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1780 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001781
1782 FT_Done_Face(face);
1783 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001784 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001785}