Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: src/ports/SkFontHost_FreeType.cpp

Issue 672723002: Extend SkFontMgr_Custom to cover ttc, otf, pfb. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Keep FT_Stream alive, protect library with mutex. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/fonts/SkFontMgr_fontconfig.cpp ('k') | src/ports/SkFontHost_FreeType_common.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkAdvancedTypefaceMetrics.h" 9 #include "SkAdvancedTypefaceMetrics.h"
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
(...skipping 1651 matching lines...) Expand 10 before | Expand all | Expand 10 after
1662 return 0; 1662 return 0;
1663 } 1663 }
1664 } 1664 }
1665 1665
1666 return size; 1666 return size;
1667 } 1667 }
1668 1668
1669 /////////////////////////////////////////////////////////////////////////////// 1669 ///////////////////////////////////////////////////////////////////////////////
1670 /////////////////////////////////////////////////////////////////////////////// 1670 ///////////////////////////////////////////////////////////////////////////////
1671 1671
1672 #include "SkTSearch.h" 1672
1673 /*static*/ bool SkTypeface_FreeType::ScanFont( 1673 SkTypeface_FreeType::Scanner::Scanner() {
1674 SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* is FixedPitch) 1674 if (FT_Init_FreeType(&fLibrary)) {
1675 fLibrary = NULL;
1676 }
1677 }
1678 SkTypeface_FreeType::Scanner::~Scanner() {
1679 FT_Done_FreeType(fLibrary);
1680 }
1681
1682 FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex,
1683 FT_Stream ftStream) const
1675 { 1684 {
1676 FT_Library library; 1685 if (fLibrary == NULL) {
1677 if (FT_Init_FreeType(&library)) { 1686 return NULL;
1678 return false;
1679 } 1687 }
1680 1688
1681 FT_Open_Args args; 1689 FT_Open_Args args;
1682 memset(&args, 0, sizeof(args)); 1690 memset(&args, 0, sizeof(args));
1683 1691
1684 const void* memoryBase = stream->getMemoryBase(); 1692 const void* memoryBase = stream->getMemoryBase();
1685 FT_StreamRec streamRec;
1686 1693
1687 if (memoryBase) { 1694 if (memoryBase) {
1688 args.flags = FT_OPEN_MEMORY; 1695 args.flags = FT_OPEN_MEMORY;
1689 args.memory_base = (const FT_Byte*)memoryBase; 1696 args.memory_base = (const FT_Byte*)memoryBase;
1690 args.memory_size = stream->getLength(); 1697 args.memory_size = stream->getLength();
1691 } else { 1698 } else {
1692 memset(&streamRec, 0, sizeof(streamRec)); 1699 memset(ftStream, 0, sizeof(*ftStream));
1693 streamRec.size = stream->getLength(); 1700 ftStream->size = stream->getLength();
1694 streamRec.descriptor.pointer = stream; 1701 ftStream->descriptor.pointer = stream;
1695 streamRec.read = sk_stream_read; 1702 ftStream->read = sk_stream_read;
1696 streamRec.close = sk_stream_close; 1703 ftStream->close = sk_stream_close;
1697 1704
1698 args.flags = FT_OPEN_STREAM; 1705 args.flags = FT_OPEN_STREAM;
1699 args.stream = &streamRec; 1706 args.stream = ftStream;
1700 } 1707 }
1701 1708
1702 FT_Face face; 1709 FT_Face face;
1703 if (FT_Open_Face(library, &args, ttcIndex, &face)) { 1710 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) {
1704 FT_Done_FreeType(library); 1711 return NULL;
1712 }
1713 return face;
1714 }
1715
1716 bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFace s) const {
1717 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1718
1719 FT_StreamRec streamRec;
1720 FT_Face face = this->openFace(stream, -1, &streamRec);
1721 if (NULL == face) {
1705 return false; 1722 return false;
1706 } 1723 }
1707 1724
1725 *numFaces = face->num_faces;
1726
1727 FT_Done_Face(face);
1728 return true;
1729 }
1730
1731 #include "SkTSearch.h"
1732 bool SkTypeface_FreeType::Scanner::scanFont(
1733 SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* is FixedPitch) const
1734 {
1735 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1736
1737 FT_StreamRec streamRec;
1738 FT_Face face = this->openFace(stream, ttcIndex, &streamRec);
1739 if (NULL == face) {
1740 return false;
1741 }
1742
1708 int weight = SkFontStyle::kNormal_Weight; 1743 int weight = SkFontStyle::kNormal_Weight;
1709 int width = SkFontStyle::kNormal_Width; 1744 int width = SkFontStyle::kNormal_Width;
1710 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant; 1745 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
1711 if (face->style_flags & FT_STYLE_FLAG_BOLD) { 1746 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1712 weight = SkFontStyle::kBold_Weight; 1747 weight = SkFontStyle::kBold_Weight;
1713 } 1748 }
1714 if (face->style_flags & FT_STYLE_FLAG_ITALIC) { 1749 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1715 slant = SkFontStyle::kItalic_Slant; 1750 slant = SkFontStyle::kItalic_Slant;
1716 } 1751 }
1717 1752
1718 PS_FontInfoRec psFontInfo; 1753 PS_FontInfoRec psFontInfo;
1719 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2)); 1754 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
1720 if (os2 && os2->version != 0xffff) { 1755 if (os2 && os2->version != 0xffff) {
1721 weight = os2->usWeightClass; 1756 weight = os2->usWeightClass;
1722 width = os2->usWidthClass; 1757 width = os2->usWidthClass;
1723 } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) { 1758 } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) {
1724 static const struct { 1759 static const struct {
1725 char const * const name; 1760 char const * const name;
1726 int const weight; 1761 int const weight;
1727 } commonWeights [] = { 1762 } commonWeights [] = {
1728 // There are probably more common names, but these are known to exis t. 1763 // There are probably more common names, but these are known to exis t.
1729 { "black", SkFontStyle::kBlack_Weight }, 1764 { "black", SkFontStyle::kBlack_Weight },
1730 { "bold", SkFontStyle::kBold_Weight }, 1765 { "bold", SkFontStyle::kBold_Weight },
1731 { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight) /2 }, 1766 { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight) /2 },
1732 { "demi", SkFontStyle::kSemiBold_Weight }, 1767 { "demi", SkFontStyle::kSemiBold_Weight },
1733 { "demibold", SkFontStyle::kSemiBold_Weight }, 1768 { "demibold", SkFontStyle::kSemiBold_Weight },
1769 { "extra", SkFontStyle::kExtraBold_Weight },
1734 { "extrabold", SkFontStyle::kExtraBold_Weight }, 1770 { "extrabold", SkFontStyle::kExtraBold_Weight },
1735 { "extralight", SkFontStyle::kExtraLight_Weight }, 1771 { "extralight", SkFontStyle::kExtraLight_Weight },
1772 { "hairline", SkFontStyle::kThin_Weight },
1736 { "heavy", SkFontStyle::kBlack_Weight }, 1773 { "heavy", SkFontStyle::kBlack_Weight },
1737 { "light", SkFontStyle::kLight_Weight }, 1774 { "light", SkFontStyle::kLight_Weight },
1738 { "medium", SkFontStyle::kMedium_Weight }, 1775 { "medium", SkFontStyle::kMedium_Weight },
1739 { "normal", SkFontStyle::kNormal_Weight }, 1776 { "normal", SkFontStyle::kNormal_Weight },
1777 { "plain", SkFontStyle::kNormal_Weight },
1740 { "regular", SkFontStyle::kNormal_Weight }, 1778 { "regular", SkFontStyle::kNormal_Weight },
1779 { "roman", SkFontStyle::kNormal_Weight },
1741 { "semibold", SkFontStyle::kSemiBold_Weight }, 1780 { "semibold", SkFontStyle::kSemiBold_Weight },
1781 { "standard", SkFontStyle::kNormal_Weight },
1742 { "thin", SkFontStyle::kThin_Weight }, 1782 { "thin", SkFontStyle::kThin_Weight },
1743 { "ultra", SkFontStyle::kExtraBold_Weight }, 1783 { "ultra", SkFontStyle::kExtraBold_Weight },
1744 { "ultrablack", 1000 }, 1784 { "ultrablack", 1000 },
1745 { "ultrabold", SkFontStyle::kExtraBold_Weight }, 1785 { "ultrabold", SkFontStyle::kExtraBold_Weight },
1746 { "ultraheavy", 1000 }, 1786 { "ultraheavy", 1000 },
1747 { "ultralight", SkFontStyle::kExtraLight_Weight }, 1787 { "ultralight", SkFontStyle::kExtraLight_Weight },
1748 }; 1788 };
1749 int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(c ommonWeights), 1789 int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(c ommonWeights),
1750 psFontInfo.weight, sizeof(commonWeights[ 0])); 1790 psFontInfo.weight, sizeof(commonWeights[ 0]));
1751 if (index >= 0) { 1791 if (index >= 0) {
1752 weight = commonWeights[index].weight; 1792 weight = commonWeights[index].weight;
1753 } else { 1793 } else {
1754 SkDEBUGF(("Do not know weight for: %s\n", psFontInfo.weight)); 1794 SkDEBUGF(("Do not know weight for: %s (%s) \n", face->family_name, p sFontInfo.weight));
1755 } 1795 }
1756 } 1796 }
1757 1797
1758 if (name) { 1798 if (name) {
1759 name->set(face->family_name); 1799 name->set(face->family_name);
1760 } 1800 }
1761 if (style) { 1801 if (style) {
1762 *style = SkFontStyle(weight, width, slant); 1802 *style = SkFontStyle(weight, width, slant);
1763 } 1803 }
1764 if (isFixedPitch) { 1804 if (isFixedPitch) {
1765 *isFixedPitch = FT_IS_FIXED_WIDTH(face); 1805 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
1766 } 1806 }
1767 1807
1768 FT_Done_Face(face); 1808 FT_Done_Face(face);
1769 FT_Done_FreeType(library);
1770 return true; 1809 return true;
1771 } 1810 }
OLDNEW
« no previous file with comments | « src/fonts/SkFontMgr_fontconfig.cpp ('k') | src/ports/SkFontHost_FreeType_common.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698