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

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

Issue 23058002: allow both GDI and DW fontmgrs at the same time (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « src/ports/SkFontHost_win.cpp ('k') | src/ports/SkFontMgr_default_dw.cpp » ('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 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
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 "SkTypes.h" 8 #include "SkTypes.h"
9 #undef GetGlyphIndices 9 #undef GetGlyphIndices
10 10
11 #include "SkAdvancedTypefaceMetrics.h" 11 #include "SkAdvancedTypefaceMetrics.h"
12 #include "SkColorFilter.h" 12 #include "SkColorFilter.h"
13 #include "SkDWriteFontFileStream.h" 13 #include "SkDWriteFontFileStream.h"
14 #include "SkDWriteGeometrySink.h" 14 #include "SkDWriteGeometrySink.h"
15 #include "SkDescriptor.h" 15 #include "SkDescriptor.h"
16 #include "SkEndian.h" 16 #include "SkEndian.h"
17 #include "SkFontDescriptor.h" 17 #include "SkFontDescriptor.h"
18 #include "SkFontHost.h" 18 #include "SkFontHost.h"
19 #include "SkFontMgr.h"
19 #include "SkFontStream.h" 20 #include "SkFontStream.h"
20 #include "SkGlyph.h" 21 #include "SkGlyph.h"
21 #include "SkHRESULT.h" 22 #include "SkHRESULT.h"
22 #include "SkMaskGamma.h" 23 #include "SkMaskGamma.h"
23 #include "SkOTTable_head.h" 24 #include "SkOTTable_head.h"
24 #include "SkOTTable_hhea.h" 25 #include "SkOTTable_hhea.h"
25 #include "SkOTTable_OS_2.h" 26 #include "SkOTTable_OS_2.h"
26 #include "SkOTTable_post.h" 27 #include "SkOTTable_post.h"
27 #include "SkPath.h" 28 #include "SkPath.h"
28 #include "SkStream.h" 29 #include "SkStream.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 skname->resize(len - 1); 69 skname->resize(len - 1);
69 len = WideCharToMultiByte(CP_UTF8, 0, name, -1, skname->writable_str(), len, NULL, NULL); 70 len = WideCharToMultiByte(CP_UTF8, 0, name, -1, skname->writable_str(), len, NULL, NULL);
70 if (0 == len) { 71 if (0 == len) {
71 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wcha r."); 72 HRM(HRESULT_FROM_WIN32(GetLastError()), "Could not convert utf-8 to wcha r.");
72 } 73 }
73 return S_OK; 74 return S_OK;
74 } 75 }
75 76
76 /////////////////////////////////////////////////////////////////////////////// 77 ///////////////////////////////////////////////////////////////////////////////
77 78
79 class StreamFontFileLoader;
80
81 class SkFontMgr_DirectWrite : public SkFontMgr {
82 public:
83 /** localeNameLength must include the null terminator. */
84 SkFontMgr_DirectWrite(IDWriteFontCollection* fontCollection,
85 WCHAR* localeName, int localeNameLength)
86 : fFontCollection(SkRefComPtr(fontCollection))
87 , fLocaleName(localeNameLength)
88 {
89 memcpy(fLocaleName.get(), localeName, localeNameLength * sizeof(WCHAR));
90 }
91
92 SkTypefaceCache* getTypefaceCache() { return &fTFCache; }
93
94 SkTypeface* createTypefaceFromDWriteFont(IDWriteFontFace* fontFace,
95 IDWriteFont* font,
96 IDWriteFontFamily* fontFamily,
97 StreamFontFileLoader* = NULL,
98 IDWriteFontCollectionLoader* = NULL );
99
100 protected:
101 virtual int onCountFamilies() SK_OVERRIDE;
102 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE;
103 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE;
104 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) SK_OVERRIDE;
105 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
106 const SkFontStyle& fontstyle) SK_OVER RIDE;
107 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
108 const SkFontStyle& fontstyle) SK_OVERRI DE;
109 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OV ERRIDE;
110 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE ;
111 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVE RRIDE;
112 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
113 unsigned styleBits) SK_OVERRIDE;
114
115 private:
116 friend class SkFontStyleSet_DirectWrite;
117 SkTScopedComPtr<IDWriteFontCollection> fFontCollection;
118 SkSMallocWCHAR fLocaleName;
119 SkTypefaceCache fTFCache;
120 };
121
122 class SkFontStyleSet_DirectWrite : public SkFontStyleSet {
123 public:
124 SkFontStyleSet_DirectWrite(SkFontMgr_DirectWrite* fontMgr, IDWriteFontFamily * fontFamily)
125 : fFontMgr(SkRef(fontMgr))
126 , fFontFamily(SkRefComPtr(fontFamily))
127 { }
128
129 virtual int count() SK_OVERRIDE;
130 virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName) SK_OV ERRIDE;
131 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE;
132 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE;
133
134 private:
135 SkAutoTUnref<SkFontMgr_DirectWrite> fFontMgr;
136 SkTScopedComPtr<IDWriteFontFamily> fFontFamily;
137 };
138
139 ///////////////////////////////////////////////////////////////////////////////
140
78 class DWriteOffscreen { 141 class DWriteOffscreen {
79 public: 142 public:
80 DWriteOffscreen() : fWidth(0), fHeight(0) { 143 DWriteOffscreen() : fWidth(0), fHeight(0) {
81 } 144 }
82 145
83 void init(IDWriteFontFace* fontFace, const DWRITE_MATRIX& xform, FLOAT fontS ize) { 146 void init(IDWriteFontFace* fontFace, const DWRITE_MATRIX& xform, FLOAT fontS ize) {
84 fFontFace = fontFace; 147 fFontFace = fontFace;
85 fFontSize = fontSize; 148 fFontSize = fontSize;
86 fXform = xform; 149 fXform = xform;
87 } 150 }
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 760
698 SkSMallocWCHAR dwFontFamilyNameChar(dwFontFamilyNameLength+1); 761 SkSMallocWCHAR dwFontFamilyNameChar(dwFontFamilyNameLength+1);
699 SkSMallocWCHAR dwFontNameChar(dwFontNameLength+1); 762 SkSMallocWCHAR dwFontNameChar(dwFontNameLength+1);
700 HRB(dwFontFamilyNames->GetString(0, dwFontFamilyNameChar.get(), dwFontFamily NameLength+1)); 763 HRB(dwFontFamilyNames->GetString(0, dwFontFamilyNameChar.get(), dwFontFamily NameLength+1));
701 HRB(dwFontNames->GetString(0, dwFontNameChar.get(), dwFontNameLength+1)); 764 HRB(dwFontNames->GetString(0, dwFontNameChar.get(), dwFontNameLength+1));
702 765
703 return wcscmp(dwFaceFontFamilyNameChar.get(), dwFontFamilyNameChar.get()) == 0 && 766 return wcscmp(dwFaceFontFamilyNameChar.get(), dwFontFamilyNameChar.get()) == 0 &&
704 wcscmp(dwFaceFontNameChar.get(), dwFontNameChar.get()) == 0; 767 wcscmp(dwFaceFontNameChar.get(), dwFontNameChar.get()) == 0;
705 } 768 }
706 769
707 SkTypeface* SkCreateTypefaceFromDWriteFont(IDWriteFontFace* fontFace,
708 IDWriteFont* font,
709 IDWriteFontFamily* fontFamily,
710 StreamFontFileLoader* fontFileLoader = NULL,
711 IDWriteFontCollectionLoader* fontColl ectionLoader = NULL) {
712 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByDWriteFont, font) ;
713 if (NULL == face) {
714 face = DWriteFontTypeface::Create(fontFace, font, fontFamily,
715 fontFileLoader, fontCollectionLoader);
716 SkTypefaceCache::Add(face, get_style(font), fontCollectionLoader != NULL );
717 }
718 return face;
719 }
720
721 void SkDWriteFontFromTypeface(const SkTypeface* face, IDWriteFont** font) { 770 void SkDWriteFontFromTypeface(const SkTypeface* face, IDWriteFont** font) {
722 if (NULL == face) { 771 if (NULL == face) {
723 HRVM(get_default_font(font), "Could not get default font."); 772 HRVM(get_default_font(font), "Could not get default font.");
724 } else { 773 } else {
725 *font = SkRefComPtr(static_cast<const DWriteFontTypeface*>(face)->fDWrit eFont.get()); 774 *font = SkRefComPtr(static_cast<const DWriteFontTypeface*>(face)->fDWrit eFont.get());
726 } 775 }
727 } 776 }
728 777
729 SkScalerContext_DW::SkScalerContext_DW(DWriteFontTypeface* typeface, 778 SkScalerContext_DW::SkScalerContext_DW(DWriteFontTypeface* typeface,
730 const SkDescriptor* desc) 779 const SkDescriptor* desc)
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 HRN(fontFamily->GetFont(fontIndex, &font)); 1300 HRN(fontFamily->GetFont(fontIndex, &font));
1252 if (font->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE) { 1301 if (font->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE) {
1253 continue; 1302 continue;
1254 } 1303 }
1255 1304
1256 SkTScopedComPtr<IDWriteFontFace> fontFace; 1305 SkTScopedComPtr<IDWriteFontFace> fontFace;
1257 HRN(font->CreateFontFace(&fontFace)); 1306 HRN(font->CreateFontFace(&fontFace));
1258 1307
1259 UINT32 faceIndex = fontFace->GetIndex(); 1308 UINT32 faceIndex = fontFace->GetIndex();
1260 if (faceIndex == ttcIndex) { 1309 if (faceIndex == ttcIndex) {
1261 return SkCreateTypefaceFromDWriteFont(fontFace.get(), font.get() , fontFamily.get(), 1310 return DWriteFontTypeface::Create(fontFace.get(), font.get(), fo ntFamily.get(),
1262 autoUnregisterFontFileLoad er.detatch(), 1311 autoUnregisterFontFileLoader.d etatch(),
1263 autoUnregisterFontCollecti onLoader.detatch()); 1312 autoUnregisterFontCollectionLo ader.detatch());
1264 } 1313 }
1265 } 1314 }
1266 } 1315 }
1267 1316
1268 return NULL; 1317 return NULL;
1269 } 1318 }
1270 1319
1271 SkStream* DWriteFontTypeface::onOpenStream(int* ttcIndex) const { 1320 SkStream* DWriteFontTypeface::onOpenStream(int* ttcIndex) const {
1272 *ttcIndex = fDWriteFontFace->GetIndex(); 1321 *ttcIndex = fDWriteFontFace->GetIndex();
1273 1322
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 glyphIDsCount, 1638 glyphIDsCount,
1590 getWidthAdvance)); 1639 getWidthAdvance));
1591 } 1640 }
1592 } 1641 }
1593 1642
1594 return info; 1643 return info;
1595 } 1644 }
1596 1645
1597 static SkTypeface* create_typeface(const SkTypeface* familyFace, 1646 static SkTypeface* create_typeface(const SkTypeface* familyFace,
1598 const char familyName[], 1647 const char familyName[],
1599 unsigned style) { 1648 unsigned style,
1649 SkFontMgr_DirectWrite* fontMgr) {
1600 HRESULT hr; 1650 HRESULT hr;
1601 SkTScopedComPtr<IDWriteFontFamily> fontFamily; 1651 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1602 if (familyFace) { 1652 if (familyFace) {
1603 const DWriteFontTypeface* face = static_cast<const DWriteFontTypeface*>( familyFace); 1653 const DWriteFontTypeface* face = static_cast<const DWriteFontTypeface*>( familyFace);
1604 *(&fontFamily) = SkRefComPtr(face->fDWriteFontFamily.get()); 1654 *(&fontFamily) = SkRefComPtr(face->fDWriteFontFamily.get());
1605 1655
1606 } else if (familyName) { 1656 } else if (familyName) {
1607 hr = get_by_family_name(familyName, &fontFamily); 1657 hr = get_by_family_name(familyName, &fontFamily);
1608 } 1658 }
1609 1659
(...skipping 10 matching lines...) Expand all
1620 : DWRITE_FONT_WEIGHT_NORMAL; 1670 : DWRITE_FONT_WEIGHT_NORMAL;
1621 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_UNDEFINED; 1671 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_UNDEFINED;
1622 DWRITE_FONT_STYLE italic = (style & SkTypeface::kItalic) 1672 DWRITE_FONT_STYLE italic = (style & SkTypeface::kItalic)
1623 ? DWRITE_FONT_STYLE_ITALIC 1673 ? DWRITE_FONT_STYLE_ITALIC
1624 : DWRITE_FONT_STYLE_NORMAL; 1674 : DWRITE_FONT_STYLE_NORMAL;
1625 hr = fontFamily->GetFirstMatchingFont(weight, stretch, italic, &font); 1675 hr = fontFamily->GetFirstMatchingFont(weight, stretch, italic, &font);
1626 1676
1627 SkTScopedComPtr<IDWriteFontFace> fontFace; 1677 SkTScopedComPtr<IDWriteFontFace> fontFace;
1628 hr = font->CreateFontFace(&fontFace); 1678 hr = font->CreateFontFace(&fontFace);
1629 1679
1630 return SkCreateTypefaceFromDWriteFont(fontFace.get(), font.get(), fontFamily .get()); 1680 return fontMgr->createTypefaceFromDWriteFont(fontFace.get(), font.get(), fon tFamily.get());
1631 } 1681 }
1632 1682
1633 SkTypeface* DWriteFontTypeface::onRefMatchingStyle(Style style) const { 1683 SkTypeface* DWriteFontTypeface::onRefMatchingStyle(Style style) const {
1634 return create_typeface(this, NULL, style); 1684 SkFontMgr_DirectWrite* fontMgr = NULL;
1685 // todo: should each typeface have a ref to its fontmgr/cache?
1686 return create_typeface(this, NULL, style, fontMgr);
1635 } 1687 }
1636 1688
1637 /////////////////////////////////////////////////////////////////////////////// 1689 ///////////////////////////////////////////////////////////////////////////////
1638 1690
1639 #include "SkFontMgr.h"
1640
1641 static void get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* prefe redLocale, 1691 static void get_locale_string(IDWriteLocalizedStrings* names, const WCHAR* prefe redLocale,
1642 SkString* skname) { 1692 SkString* skname) {
1643 UINT32 nameIndex = 0; 1693 UINT32 nameIndex = 0;
1644 if (preferedLocale) { 1694 if (preferedLocale) {
1645 // Ignore any errors and continue with index 0 if there is a problem. 1695 // Ignore any errors and continue with index 0 if there is a problem.
1646 BOOL nameExists; 1696 BOOL nameExists;
1647 names->FindLocaleName(preferedLocale, &nameIndex, &nameExists); 1697 names->FindLocaleName(preferedLocale, &nameIndex, &nameExists);
1648 if (!nameExists) { 1698 if (!nameExists) {
1649 nameIndex = 0; 1699 nameIndex = 0;
1650 } 1700 }
1651 } 1701 }
1652 1702
1653 UINT32 nameLength; 1703 UINT32 nameLength;
1654 HRVM(names->GetStringLength(nameIndex, &nameLength), "Could not get name len gth."); 1704 HRVM(names->GetStringLength(nameIndex, &nameLength), "Could not get name len gth.");
1655 nameLength += 1; 1705 nameLength += 1;
1656 1706
1657 SkSMallocWCHAR name(nameLength); 1707 SkSMallocWCHAR name(nameLength);
1658 HRVM(names->GetString(nameIndex, name.get(), nameLength), "Could not get str ing."); 1708 HRVM(names->GetString(nameIndex, name.get(), nameLength), "Could not get str ing.");
1659 1709
1660 HRV(wchar_to_skstring(name.get(), skname)); 1710 HRV(wchar_to_skstring(name.get(), skname));
1661 } 1711 }
1662 1712
1663 class SkFontMgr_DirectWrite; 1713 SkTypeface* SkFontMgr_DirectWrite::createTypefaceFromDWriteFont(
1714 IDWriteFontFace* fontFace,
1715 IDWriteFont* font,
1716 IDWriteFontFamily* fontFamily,
1717 StreamFontFileLoader* fontFileLoader,
1718 IDWriteFontCollectionLoader* fontColl ectionLoader) {
1719 SkTypeface* face = fTFCache.findByProcAndRef(FindByDWriteFont, font);
1720 if (NULL == face) {
1721 face = DWriteFontTypeface::Create(fontFace, font, fontFamily,
1722 fontFileLoader, fontCollectionLoader);
1723 if (face) {
1724 fTFCache.add(face, get_style(font), fontCollectionLoader != NULL);
1725 }
1726 }
1727 return face;
1728 }
1664 1729
1665 class SkFontStyleSet_DirectWrite : public SkFontStyleSet { 1730 int SkFontMgr_DirectWrite::onCountFamilies() {
1666 public: 1731 return fFontCollection->GetFontFamilyCount();
1667 SkFontStyleSet_DirectWrite(const SkFontMgr_DirectWrite* fontMgr, IDWriteFont Family* fontFamily) 1732 }
1668 : fFontMgr(SkRef(fontMgr))
1669 , fFontFamily(SkRefComPtr(fontFamily))
1670 { }
1671 1733
1672 virtual int count() SK_OVERRIDE { 1734 void SkFontMgr_DirectWrite::onGetFamilyName(int index, SkString* familyName) {
1673 return fFontFamily->GetFontCount(); 1735 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1736 HRVM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requ ested family.");
1737
1738 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
1739 HRVM(fontFamily->GetFamilyNames(&familyNames), "Could not get family names." );
1740
1741 get_locale_string(familyNames.get(), fLocaleName.get(), familyName);
1742 }
1743
1744 SkFontStyleSet* SkFontMgr_DirectWrite::onCreateStyleSet(int index) {
1745 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1746 HRNM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requ ested family.");
1747
1748 return SkNEW_ARGS(SkFontStyleSet_DirectWrite, (this, fontFamily.get()));
1749 }
1750
1751 SkFontStyleSet* SkFontMgr_DirectWrite::onMatchFamily(const char familyName[]) {
1752 SkSMallocWCHAR dwFamilyName;
1753 HRN(cstring_to_wchar(familyName, &dwFamilyName));
1754
1755 UINT32 index;
1756 BOOL exists;
1757 HRNM(fFontCollection->FindFamilyName(dwFamilyName.get(), &index, &exists),
1758 "Failed while finding family by name.");
1759 if (!exists) {
1760 return NULL;
1674 } 1761 }
1675 1762
1676 virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName); 1763 return this->onCreateStyleSet(index);
1764 }
1677 1765
1678 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { 1766 SkTypeface* SkFontMgr_DirectWrite::onMatchFamilyStyle(const char familyName[],
1679 SkTScopedComPtr<IDWriteFont> font; 1767 const SkFontStyle& fontsty le) {
1680 HRNM(fFontFamily->GetFont(index, &font), "Could not get font."); 1768 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
1769 return sset->matchStyle(fontstyle);
1770 }
1681 1771
1682 SkTScopedComPtr<IDWriteFontFace> fontFace; 1772 SkTypeface* SkFontMgr_DirectWrite::onMatchFaceStyle(const SkTypeface* familyMemb er,
1683 HRNM(font->CreateFontFace(&fontFace), "Could not create font face."); 1773 const SkFontStyle& fontstyle ) {
1774 SkString familyName;
1775 SkFontStyleSet_DirectWrite sset(
1776 this, ((DWriteFontTypeface*)familyMember)->fDWriteFontFamily.get()
1777 );
1778 return sset.matchStyle(fontstyle);
1779 }
1684 1780
1685 return SkCreateTypefaceFromDWriteFont(fontFace.get(), font.get(), fFontF amily.get()); 1781 SkTypeface* SkFontMgr_DirectWrite::onCreateFromStream(SkStream* stream, int ttcI ndex) {
1686 } 1782 return create_from_stream(stream, ttcIndex);
1783 }
1687 1784
1688 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { 1785 SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) {
1689 DWRITE_FONT_STYLE slant; 1786 SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
1690 switch (pattern.slant()) { 1787 return this->createFromStream(stream, ttcIndex);
1691 case SkFontStyle::kUpright_Slant: 1788 }
1692 slant = DWRITE_FONT_STYLE_NORMAL;
1693 break;
1694 case SkFontStyle::kItalic_Slant:
1695 slant = DWRITE_FONT_STYLE_ITALIC;
1696 break;
1697 default:
1698 SkASSERT(false);
1699 }
1700 1789
1701 DWRITE_FONT_WEIGHT weight = (DWRITE_FONT_WEIGHT)pattern.weight(); 1790 SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIn dex) {
1702 DWRITE_FONT_STRETCH width = (DWRITE_FONT_STRETCH)pattern.width(); 1791 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
1792 return this->createFromStream(stream, ttcIndex);
1793 }
1703 1794
1704 SkTScopedComPtr<IDWriteFont> font; 1795 SkTypeface* SkFontMgr_DirectWrite::onLegacyCreateTypeface(const char familyName[ ],
1705 // TODO: perhaps use GetMatchingFonts and get the least simulated? 1796 unsigned styleBits) {
1706 HRNM(fFontFamily->GetFirstMatchingFont(weight, width, slant, &font), 1797 return create_typeface(NULL, familyName, styleBits, this);
1707 "Could not match font in family."); 1798 }
1708 1799
1709 SkTScopedComPtr<IDWriteFontFace> fontFace; 1800 ///////////////////////////////////////////////////////////////////////////////
1710 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
1711 1801
1712 return SkCreateTypefaceFromDWriteFont(fontFace.get(), font.get(), fFontF amily.get()); 1802 int SkFontStyleSet_DirectWrite::count() {
1713 } 1803 return fFontFamily->GetFontCount();
1804 }
1714 1805
1715 private: 1806 SkTypeface* SkFontStyleSet_DirectWrite::createTypeface(int index) {
1716 SkAutoTUnref<const SkFontMgr_DirectWrite> fFontMgr; 1807 SkTScopedComPtr<IDWriteFont> font;
1717 SkTScopedComPtr<IDWriteFontFamily> fFontFamily; 1808 HRNM(fFontFamily->GetFont(index, &font), "Could not get font.");
1718 };
1719 1809
1720 class SkFontMgr_DirectWrite : public SkFontMgr { 1810 SkTScopedComPtr<IDWriteFontFace> fontFace;
1721 public: 1811 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
1722 /** localeNameLength must include the null terminator. */
1723 SkFontMgr_DirectWrite(IDWriteFontCollection* fontCollection,
1724 WCHAR* localeName, int localeNameLength)
1725 : fFontCollection(SkRefComPtr(fontCollection))
1726 , fLocaleName(localeNameLength)
1727 {
1728 memcpy(fLocaleName.get(), localeName, localeNameLength * sizeof(WCHAR));
1729 }
1730 1812
1731 private: 1813 return fFontMgr->createTypefaceFromDWriteFont(fontFace.get(), font.get(), fF ontFamily.get());
1732 friend class SkFontStyleSet_DirectWrite; 1814 }
1733 SkTScopedComPtr<IDWriteFontCollection> fFontCollection;
1734 SkSMallocWCHAR fLocaleName;
1735
1736 protected:
1737 virtual int onCountFamilies() SK_OVERRIDE {
1738 return fFontCollection->GetFontFamilyCount();
1739 }
1740 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
1741 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1742 HRVM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requested family.");
1743
1744 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
1745 HRVM(fontFamily->GetFamilyNames(&familyNames), "Could not get family nam es.");
1746
1747 get_locale_string(familyNames.get(), fLocaleName.get(), familyName);
1748 }
1749 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE {
1750 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1751 HRNM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requested family.");
1752
1753 return SkNEW_ARGS(SkFontStyleSet_DirectWrite, (this, fontFamily.get()));
1754 }
1755 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) SK_OVERRIDE {
1756 SkSMallocWCHAR dwFamilyName;
1757 HRN(cstring_to_wchar(familyName, &dwFamilyName));
1758
1759 UINT32 index;
1760 BOOL exists;
1761 HRNM(fFontCollection->FindFamilyName(dwFamilyName.get(), &index, &exists ),
1762 "Failed while finding family by name.");
1763 if (!exists) {
1764 return NULL;
1765 }
1766
1767 return this->onCreateStyleSet(index);
1768 }
1769
1770 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
1771 const SkFontStyle& fontstyle) SK_OVER RIDE {
1772 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
1773 return sset->matchStyle(fontstyle);
1774 }
1775 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
1776 const SkFontStyle& fontstyle) SK_OVERRI DE {
1777 SkString familyName;
1778 SkFontStyleSet_DirectWrite sset(
1779 this, ((DWriteFontTypeface*)familyMember)->fDWriteFontFamily.get()
1780 );
1781 return sset.matchStyle(fontstyle);
1782 }
1783 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OV ERRIDE {
1784 return create_from_stream(stream, ttcIndex);
1785 }
1786 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE {
1787 SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
1788 return this->createFromStream(stream, ttcIndex);
1789 }
1790 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVE RRIDE {
1791 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
1792 return this->createFromStream(stream, ttcIndex);
1793 }
1794
1795 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
1796 unsigned styleBits) SK_OVERRIDE {
1797 return create_typeface(NULL, familyName, styleBits);
1798 }
1799 };
1800 1815
1801 void SkFontStyleSet_DirectWrite::getStyle(int index, SkFontStyle* fs, SkString* styleName) { 1816 void SkFontStyleSet_DirectWrite::getStyle(int index, SkFontStyle* fs, SkString* styleName) {
1802 SkTScopedComPtr<IDWriteFont> font; 1817 SkTScopedComPtr<IDWriteFont> font;
1803 HRVM(fFontFamily->GetFont(index, &font), "Could not get font."); 1818 HRVM(fFontFamily->GetFont(index, &font), "Could not get font.");
1804 1819
1805 SkFontStyle::Slant slant; 1820 SkFontStyle::Slant slant;
1806 switch (font->GetStyle()) { 1821 switch (font->GetStyle()) {
1807 case DWRITE_FONT_STYLE_NORMAL: 1822 case DWRITE_FONT_STYLE_NORMAL:
1808 slant = SkFontStyle::kUpright_Slant; 1823 slant = SkFontStyle::kUpright_Slant;
1809 break; 1824 break;
1810 case DWRITE_FONT_STYLE_OBLIQUE: 1825 case DWRITE_FONT_STYLE_OBLIQUE:
1811 case DWRITE_FONT_STYLE_ITALIC: 1826 case DWRITE_FONT_STYLE_ITALIC:
1812 slant = SkFontStyle::kItalic_Slant; 1827 slant = SkFontStyle::kItalic_Slant;
1813 break; 1828 break;
1814 default: 1829 default:
1815 SkASSERT(false); 1830 SkASSERT(false);
1816 } 1831 }
1817 1832
1818 int weight = font->GetWeight(); 1833 int weight = font->GetWeight();
1819 int width = font->GetStretch(); 1834 int width = font->GetStretch();
1820 1835
1821 *fs = SkFontStyle(weight, width, slant); 1836 *fs = SkFontStyle(weight, width, slant);
1822 1837
1823 SkTScopedComPtr<IDWriteLocalizedStrings> faceNames; 1838 SkTScopedComPtr<IDWriteLocalizedStrings> faceNames;
1824 if (SUCCEEDED(font->GetFaceNames(&faceNames))) { 1839 if (SUCCEEDED(font->GetFaceNames(&faceNames))) {
1825 get_locale_string(faceNames.get(), fFontMgr->fLocaleName.get(), styleNam e); 1840 get_locale_string(faceNames.get(), fFontMgr->fLocaleName.get(), styleNam e);
1826 } 1841 }
1827 } 1842 }
1828 1843
1844 SkTypeface* SkFontStyleSet_DirectWrite::matchStyle(const SkFontStyle& pattern) {
1845 DWRITE_FONT_STYLE slant;
1846 switch (pattern.slant()) {
1847 case SkFontStyle::kUpright_Slant:
1848 slant = DWRITE_FONT_STYLE_NORMAL;
1849 break;
1850 case SkFontStyle::kItalic_Slant:
1851 slant = DWRITE_FONT_STYLE_ITALIC;
1852 break;
1853 default:
1854 SkASSERT(false);
1855 }
1856
1857 DWRITE_FONT_WEIGHT weight = (DWRITE_FONT_WEIGHT)pattern.weight();
1858 DWRITE_FONT_STRETCH width = (DWRITE_FONT_STRETCH)pattern.width();
1859
1860 SkTScopedComPtr<IDWriteFont> font;
1861 // TODO: perhaps use GetMatchingFonts and get the least simulated?
1862 HRNM(fFontFamily->GetFirstMatchingFont(weight, width, slant, &font),
1863 "Could not match font in family.");
1864
1865 SkTScopedComPtr<IDWriteFontFace> fontFace;
1866 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
1867
1868 return fFontMgr->createTypefaceFromDWriteFont(fontFace.get(), font.get(),
1869 fFontFamily.get());
1870 }
1871
1829 /////////////////////////////////////////////////////////////////////////////// 1872 ///////////////////////////////////////////////////////////////////////////////
1830 1873
1831 #ifndef SK_FONTHOST_USES_FONTMGR 1874 #ifndef SK_FONTHOST_USES_FONTMGR
1832 1875
1833 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, 1876 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
1834 const char familyName[], 1877 const char familyName[],
1835 SkTypeface::Style style) { 1878 SkTypeface::Style style) {
1836 return create_typeface(familyFace, familyName, style); 1879 return create_typeface(familyFace, familyName, style, NULL);
1837 } 1880 }
1838 1881
1839 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { 1882 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
1840 printf("SkFontHost::CreateTypefaceFromFile unimplemented"); 1883 printf("SkFontHost::CreateTypefaceFromFile unimplemented");
1841 return NULL; 1884 return NULL;
1842 } 1885 }
1843 1886
1844 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { 1887 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
bungeman-skia 2013/08/16 14:20:38 When SK_FONTHOST_USES_FONTMGR is not defined, this
1845 return create_from_stream(stream, 0); 1888 return create_from_stream(stream, 0);
1846 } 1889 }
1847 1890
1848 #endif 1891 #endif
1849 1892
1850 SkFontMgr* SkFontMgr::Factory() { 1893 extern SkFontMgr* SkFontMgr_New_DirectWrite();
1894 SkFontMgr* SkFontMgr_New_DirectWrite() {
1851 IDWriteFactory* factory; 1895 IDWriteFactory* factory;
1852 HRNM(get_dwrite_factory(&factory), "Could not get factory."); 1896 HRNM(get_dwrite_factory(&factory), "Could not get factory.");
1853 1897
1854 SkTScopedComPtr<IDWriteFontCollection> sysFontCollection; 1898 SkTScopedComPtr<IDWriteFontCollection> sysFontCollection;
1855 HRNM(factory->GetSystemFontCollection(&sysFontCollection, FALSE), 1899 HRNM(factory->GetSystemFontCollection(&sysFontCollection, FALSE),
1856 "Could not get system font collection."); 1900 "Could not get system font collection.");
1857 1901
1858 WCHAR localeNameStorage[LOCALE_NAME_MAX_LENGTH]; 1902 WCHAR localeNameStorage[LOCALE_NAME_MAX_LENGTH];
1859 WCHAR* localeName = NULL; 1903 WCHAR* localeName = NULL;
1860 int localeNameLen = GetUserDefaultLocaleName(localeNameStorage, LOCALE_NAME_ MAX_LENGTH); 1904 int localeNameLen = GetUserDefaultLocaleName(localeNameStorage, LOCALE_NAME_ MAX_LENGTH);
1861 if (localeNameLen) { 1905 if (localeNameLen) {
1862 localeName = localeNameStorage; 1906 localeName = localeNameStorage;
1863 }; 1907 };
1864 1908
1865 return SkNEW_ARGS(SkFontMgr_DirectWrite, (sysFontCollection.get(), localeNam e, localeNameLen)); 1909 return SkNEW_ARGS(SkFontMgr_DirectWrite, (sysFontCollection.get(), localeNam e, localeNameLen));
1866 } 1910 }
1911
OLDNEW
« no previous file with comments | « src/ports/SkFontHost_win.cpp ('k') | src/ports/SkFontMgr_default_dw.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698