OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkAdvancedTypefaceMetrics.h" | 8 #include "SkAdvancedTypefaceMetrics.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkDescriptor.h" | 12 #include "SkDescriptor.h" |
13 #include "SkFDot6.h" | 13 #include "SkFDot6.h" |
| 14 #include "SkFontDescriptor.h" |
14 #include "SkFontHost_FreeType_common.h" | 15 #include "SkFontHost_FreeType_common.h" |
15 #include "SkGlyph.h" | 16 #include "SkGlyph.h" |
16 #include "SkMask.h" | 17 #include "SkMask.h" |
17 #include "SkMaskGamma.h" | 18 #include "SkMaskGamma.h" |
18 #include "SkMatrix22.h" | 19 #include "SkMatrix22.h" |
19 #include "SkOTUtils.h" | 20 #include "SkOTUtils.h" |
20 #include "SkScalerContext.h" | 21 #include "SkScalerContext.h" |
21 #include "SkStream.h" | 22 #include "SkStream.h" |
22 #include "SkString.h" | 23 #include "SkString.h" |
23 #include "SkTemplates.h" | 24 #include "SkTemplates.h" |
24 #include "SkThread.h" | 25 #include "SkThread.h" |
25 #include "SkTypes.h" | 26 #include "SkTypes.h" |
26 | 27 |
27 #if defined(SK_CAN_USE_DLOPEN) | 28 #if defined(SK_CAN_USE_DLOPEN) |
28 #include <dlfcn.h> | 29 #include <dlfcn.h> |
29 #endif | 30 #endif |
30 #include <ft2build.h> | 31 #include <ft2build.h> |
31 #include FT_ADVANCES_H | 32 #include FT_ADVANCES_H |
32 #include FT_BITMAP_H | 33 #include FT_BITMAP_H |
33 #include FT_FREETYPE_H | 34 #include FT_FREETYPE_H |
34 #include FT_LCD_FILTER_H | 35 #include FT_LCD_FILTER_H |
35 #include FT_MODULE_H | 36 #include FT_MODULE_H |
| 37 #include FT_MULTIPLE_MASTERS_H |
36 #include FT_OUTLINE_H | 38 #include FT_OUTLINE_H |
37 #include FT_SIZES_H | 39 #include FT_SIZES_H |
38 #include FT_SYSTEM_H | 40 #include FT_SYSTEM_H |
39 #include FT_TRUETYPE_TABLES_H | 41 #include FT_TRUETYPE_TABLES_H |
40 #include FT_TYPE1_TABLES_H | 42 #include FT_TYPE1_TABLES_H |
41 #include FT_XFREE86_H | 43 #include FT_XFREE86_H |
42 | 44 |
43 // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA | 45 // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA |
44 // were introduced in FreeType 2.5.0. | 46 // were introduced in FreeType 2.5.0. |
45 // The following may be removed once FreeType 2.5.0 is required to build. | 47 // The following may be removed once FreeType 2.5.0 is required to build. |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID) | 268 SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID) |
267 : fNext(NULL), fSkStream(stream), fRefCnt(1), fFontID(fontID) | 269 : fNext(NULL), fSkStream(stream), fRefCnt(1), fFontID(fontID) |
268 { | 270 { |
269 sk_bzero(&fFTStream, sizeof(fFTStream)); | 271 sk_bzero(&fFTStream, sizeof(fFTStream)); |
270 fFTStream.size = fSkStream->getLength(); | 272 fFTStream.size = fSkStream->getLength(); |
271 fFTStream.descriptor.pointer = fSkStream; | 273 fFTStream.descriptor.pointer = fSkStream; |
272 fFTStream.read = sk_ft_stream_io; | 274 fFTStream.read = sk_ft_stream_io; |
273 fFTStream.close = sk_ft_stream_close; | 275 fFTStream.close = sk_ft_stream_close; |
274 } | 276 } |
275 | 277 |
| 278 static void ft_face_setup_axes(FT_Face face, const SkFontData& data) { |
| 279 if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { |
| 280 return; |
| 281 } |
| 282 |
| 283 SkDEBUGCODE( |
| 284 FT_MM_Var* variations = NULL; |
| 285 if (FT_Get_MM_Var(face, &variations)) { |
| 286 SkDEBUGF(("INFO: font %s claims variations, but none found.\n", face
->family_name)); |
| 287 return; |
| 288 } |
| 289 SkAutoFree autoFreeVariations(variations); |
| 290 |
| 291 if (static_cast<FT_UInt>(data.getAxisCount()) != variations->num_axis) { |
| 292 SkDEBUGF(("INFO: font %s has %d variations, but %d were specified.\n
", |
| 293 face->family_name, variations->num_axis, data.getAxisCount()
)); |
| 294 return; |
| 295 } |
| 296 ) |
| 297 |
| 298 SkAutoSTMalloc<4, FT_Fixed> coords(data.getAxisCount()); |
| 299 for (int i = 0; i < data.getAxisCount(); ++i) { |
| 300 coords[i] = data.getAxis()[i]; |
| 301 } |
| 302 if (FT_Set_Var_Design_Coordinates(face, data.getAxisCount(), coords.get()))
{ |
| 303 SkDEBUGF(("INFO: font %s has variations, but specified variations could
not be set.\n", |
| 304 face->family_name)); |
| 305 return; |
| 306 } |
| 307 } |
| 308 |
276 // Will return 0 on failure | 309 // Will return 0 on failure |
277 // Caller must lock gFTMutex before calling this function. | 310 // Caller must lock gFTMutex before calling this function. |
278 static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { | 311 static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { |
279 gFTMutex.assertHeld(); | 312 gFTMutex.assertHeld(); |
280 | 313 |
281 const SkFontID fontID = typeface->uniqueID(); | 314 const SkFontID fontID = typeface->uniqueID(); |
282 SkFaceRec* rec = gFaceRecHead; | 315 SkFaceRec* rec = gFaceRecHead; |
283 while (rec) { | 316 while (rec) { |
284 if (rec->fFontID == fontID) { | 317 if (rec->fFontID == fontID) { |
285 SkASSERT(rec->fFace); | 318 SkASSERT(rec->fFace); |
286 rec->fRefCnt += 1; | 319 rec->fRefCnt += 1; |
287 return rec; | 320 return rec; |
288 } | 321 } |
289 rec = rec->fNext; | 322 rec = rec->fNext; |
290 } | 323 } |
291 | 324 |
292 int face_index; | 325 SkFontData* data = typeface->createFontData(); |
293 SkStreamAsset* stream = typeface->openStream(&face_index); | 326 if (NULL == data || !data->hasStream()) { |
294 if (NULL == stream) { | |
295 return NULL; | 327 return NULL; |
296 } | 328 } |
297 | 329 |
298 // this passes ownership of stream to the rec | 330 // this passes ownership of stream to the rec |
299 rec = SkNEW_ARGS(SkFaceRec, (stream, fontID)); | 331 rec = SkNEW_ARGS(SkFaceRec, (data->detachStream(), fontID)); |
300 | 332 |
301 FT_Open_Args args; | 333 FT_Open_Args args; |
302 memset(&args, 0, sizeof(args)); | 334 memset(&args, 0, sizeof(args)); |
303 const void* memoryBase = stream->getMemoryBase(); | 335 const void* memoryBase = rec->fSkStream->getMemoryBase(); |
304 if (memoryBase) { | 336 if (memoryBase) { |
305 args.flags = FT_OPEN_MEMORY; | 337 args.flags = FT_OPEN_MEMORY; |
306 args.memory_base = (const FT_Byte*)memoryBase; | 338 args.memory_base = (const FT_Byte*)memoryBase; |
307 args.memory_size = stream->getLength(); | 339 args.memory_size = rec->fSkStream->getLength(); |
308 } else { | 340 } else { |
309 args.flags = FT_OPEN_STREAM; | 341 args.flags = FT_OPEN_STREAM; |
310 args.stream = &rec->fFTStream; | 342 args.stream = &rec->fFTStream; |
311 } | 343 } |
312 | 344 |
313 FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, face_index, &rec->
fFace); | 345 FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, data->getIndex(),
&rec->fFace); |
314 if (err) { // bad filename, try the default font | 346 if (err) { |
315 SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID)); | 347 SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID)); |
316 SkDELETE(rec); | 348 SkDELETE(rec); |
317 return NULL; | 349 return NULL; |
318 } | 350 } |
319 SkASSERT(rec->fFace); | 351 SkASSERT(rec->fFace); |
| 352 |
| 353 ft_face_setup_axes(rec->fFace, *data); |
| 354 |
320 rec->fNext = gFaceRecHead; | 355 rec->fNext = gFaceRecHead; |
321 gFaceRecHead = rec; | 356 gFaceRecHead = rec; |
322 return rec; | 357 return rec; |
323 } | 358 } |
324 | 359 |
325 // Caller must lock gFTMutex before calling this function. | 360 // Caller must lock gFTMutex before calling this function. |
326 static void unref_ft_face(FT_Face face) { | 361 static void unref_ft_face(FT_Face face) { |
327 gFTMutex.assertHeld(); | 362 gFTMutex.assertHeld(); |
328 | 363 |
329 SkFaceRec* rec = gFaceRecHead; | 364 SkFaceRec* rec = gFaceRecHead; |
(...skipping 1313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1643 } | 1678 } |
1644 | 1679 |
1645 *numFaces = face->num_faces; | 1680 *numFaces = face->num_faces; |
1646 | 1681 |
1647 FT_Done_Face(face); | 1682 FT_Done_Face(face); |
1648 return true; | 1683 return true; |
1649 } | 1684 } |
1650 | 1685 |
1651 #include "SkTSearch.h" | 1686 #include "SkTSearch.h" |
1652 bool SkTypeface_FreeType::Scanner::scanFont( | 1687 bool SkTypeface_FreeType::Scanner::scanFont( |
1653 SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* is
FixedPitch) const | 1688 SkStream* stream, int ttcIndex, |
| 1689 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axe
s) const |
1654 { | 1690 { |
1655 SkAutoMutexAcquire libraryLock(fLibraryMutex); | 1691 SkAutoMutexAcquire libraryLock(fLibraryMutex); |
1656 | 1692 |
1657 FT_StreamRec streamRec; | 1693 FT_StreamRec streamRec; |
1658 FT_Face face = this->openFace(stream, ttcIndex, &streamRec); | 1694 FT_Face face = this->openFace(stream, ttcIndex, &streamRec); |
1659 if (NULL == face) { | 1695 if (NULL == face) { |
1660 return false; | 1696 return false; |
1661 } | 1697 } |
1662 | 1698 |
1663 int weight = SkFontStyle::kNormal_Weight; | 1699 int weight = SkFontStyle::kNormal_Weight; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1719 if (name) { | 1755 if (name) { |
1720 name->set(face->family_name); | 1756 name->set(face->family_name); |
1721 } | 1757 } |
1722 if (style) { | 1758 if (style) { |
1723 *style = SkFontStyle(weight, width, slant); | 1759 *style = SkFontStyle(weight, width, slant); |
1724 } | 1760 } |
1725 if (isFixedPitch) { | 1761 if (isFixedPitch) { |
1726 *isFixedPitch = FT_IS_FIXED_WIDTH(face); | 1762 *isFixedPitch = FT_IS_FIXED_WIDTH(face); |
1727 } | 1763 } |
1728 | 1764 |
| 1765 if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) { |
| 1766 FT_MM_Var* variations = NULL; |
| 1767 FT_Error err = FT_Get_MM_Var(face, &variations); |
| 1768 if (err) { |
| 1769 SkDEBUGF(("INFO: font %s claims to have variations, but none found.\
n", |
| 1770 face->family_name)); |
| 1771 return false; |
| 1772 } |
| 1773 SkAutoFree autoFreeVariations(variations); |
| 1774 |
| 1775 axes->reset(variations->num_axis); |
| 1776 for (FT_UInt i = 0; i < variations->num_axis; ++i) { |
| 1777 const FT_Var_Axis& ftAxis = variations->axis[i]; |
| 1778 (*axes)[i].fTag = ftAxis.tag; |
| 1779 (*axes)[i].fMinimum = ftAxis.minimum; |
| 1780 (*axes)[i].fDefault = ftAxis.def; |
| 1781 (*axes)[i].fMaximum = ftAxis.maximum; |
| 1782 } |
| 1783 } |
| 1784 |
1729 FT_Done_Face(face); | 1785 FT_Done_Face(face); |
1730 return true; | 1786 return true; |
1731 } | 1787 } |
OLD | NEW |