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