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

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

Issue 77023006: Only SkFontMgr_New_DirectWrite should call GetSystemFontCollection. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years 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 | « no previous file | no next file » | 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
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 const SkFontStyle& fontstyle) SK_OVER RIDE; 106 const SkFontStyle& fontstyle) SK_OVER RIDE;
107 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, 107 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
108 const SkFontStyle& fontstyle) SK_OVERRI DE; 108 const SkFontStyle& fontstyle) SK_OVERRI DE;
109 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OV ERRIDE; 109 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OV ERRIDE;
110 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE ; 110 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE ;
111 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVE RRIDE; 111 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVE RRIDE;
112 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], 112 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
113 unsigned styleBits) SK_OVERRIDE; 113 unsigned styleBits) SK_OVERRIDE;
114 114
115 private: 115 private:
116 HRESULT getByFamilyName(const WCHAR familyName[], IDWriteFontFamily** fontFa mily);
117 HRESULT getDefaultFontFamily(IDWriteFontFamily** fontFamily);
118
116 SkMutex fTFCacheMutex; 119 SkMutex fTFCacheMutex;
117 void Add(SkTypeface* face, SkTypeface::Style requestedStyle, bool strong) { 120 void Add(SkTypeface* face, SkTypeface::Style requestedStyle, bool strong) {
118 SkAutoMutexAcquire ama(fTFCacheMutex); 121 SkAutoMutexAcquire ama(fTFCacheMutex);
119 fTFCache.add(face, requestedStyle, strong); 122 fTFCache.add(face, requestedStyle, strong);
120 } 123 }
121 124
122 SkTypeface* FindByProcAndRef(SkTypefaceCache::FindProc proc, void* ctx) { 125 SkTypeface* FindByProcAndRef(SkTypefaceCache::FindProc proc, void* ctx) {
123 SkAutoMutexAcquire ama(fTFCacheMutex); 126 SkAutoMutexAcquire ama(fTFCacheMutex);
124 SkTypeface* typeface = fTFCache.findByProcAndRef(proc, ctx); 127 SkTypeface* typeface = fTFCache.findByProcAndRef(proc, ctx);
125 return typeface; 128 return typeface;
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 virtual void generateFontMetrics(SkPaint::FontMetrics* mX, 594 virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
592 SkPaint::FontMetrics* mY) SK_OVERRIDE; 595 SkPaint::FontMetrics* mY) SK_OVERRIDE;
593 596
594 private: 597 private:
595 DWriteOffscreen fOffscreen; 598 DWriteOffscreen fOffscreen;
596 DWRITE_MATRIX fXform; 599 DWRITE_MATRIX fXform;
597 SkAutoTUnref<DWriteFontTypeface> fTypeface; 600 SkAutoTUnref<DWriteFontTypeface> fTypeface;
598 int fGlyphCount; 601 int fGlyphCount;
599 }; 602 };
600 603
601 #define SK_DWRITE_DEFAULT_FONT_NAMED 1
602 #define SK_DWRITE_DEFAULT_FONT_MESSAGE 2
603 #define SK_DWRITE_DEFAULT_FONT_THEME 3
604 #define SK_DWRITE_DEFAULT_FONT_SHELLDLG 4
605 #define SK_DWRITE_DEFAULT_FONT_GDI 5
606 #define SK_DWRITE_DEFAULT_FONT_STRATEGY SK_DWRITE_DEFAULT_FONT_MESSAGE
607
608 static HRESULT get_default_font(IDWriteFont** font) {
609 IDWriteFactory* factory;
610 HRM(get_dwrite_factory(&factory), "Could not get factory.");
611
612 #if SK_DWRITE_DEFAULT_FONT_STRATEGY == SK_DWRITE_DEFAULT_FONT_NAMED
613 SkTScopedComPtr<IDWriteFontCollection> sysFonts;
614 HRM(factory->GetSystemFontCollection(&sysFonts, false),
615 "Could not get system font collection.");
616
617 UINT32 index;
618 BOOL exists;
619 //hr = sysFonts->FindFamilyName(L"Georgia", &index, &exists);
620 HRM(sysFonts->FindFamilyName(L"Microsoft Sans Serif", &index, &exists),
621 "Could not access family names.");
622
623 if (!exists) {
624 SkDEBUGF(("The hard coded font family does not exist."));
625 return E_UNEXPECTED;
626 }
627
628 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
629 HRM(sysFonts->GetFontFamily(index, &fontFamily),
630 "Could not load the requested font family.");
631
632 HRM(fontFamily->GetFont(0, font), "Could not get first font from family.");
633
634 #elif SK_DWRITE_DEFAULT_FONT_STRATEGY == SK_DWRITE_DEFAULT_FONT_MESSAGE
635 SkTScopedComPtr<IDWriteGdiInterop> gdi;
636 HRM(factory->GetGdiInterop(&gdi), "Could not get GDI interop.");
637
638 NONCLIENTMETRICSW metrics;
639 metrics.cbSize = sizeof(metrics);
640 if (0 == SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
641 sizeof(metrics),
642 &metrics,
643 0)) {
644 return E_UNEXPECTED;
645 }
646 HRM(gdi->CreateFontFromLOGFONT(&metrics.lfMessageFont, font),
647 "Could not create DWrite font from LOGFONT.");
648
649 #elif SK_DWRITE_DEFAULT_FONT_STRATEGY == SK_DWRITE_DEFAULT_FONT_THEME
650 //Theme body font?
651
652 #elif SK_DWRITE_DEFAULT_FONT_STRATEGY == SK_DWRITE_DEFAULT_FONT_SHELLDLG
653 //"MS Shell Dlg" or "MS Shell Dlg 2"?
654
655 #elif SK_DWRITE_DEFAULT_FONT_STRATEGY == SK_DWRITE_DEFAULT_FONT_GDI
656 //Never works.
657 SkTScopedComPtr<IDWriteGdiInterop> gdi;
658 HRM(factory->GetGdiInterop(&gdi), "Could not get GDI interop.");
659
660 static LOGFONTW gDefaultFont = {};
661 gDefaultFont.lfFaceName
662 HRM(gdi->CreateFontFromLOGFONT(&gDefaultFont, font),
663 "Could not create DWrite font from LOGFONT.";
664 #endif
665 return S_OK;
666 }
667
668 static bool are_same(IUnknown* a, IUnknown* b) { 604 static bool are_same(IUnknown* a, IUnknown* b) {
669 SkTScopedComPtr<IUnknown> iunkA; 605 SkTScopedComPtr<IUnknown> iunkA;
670 if (FAILED(a->QueryInterface(&iunkA))) { 606 if (FAILED(a->QueryInterface(&iunkA))) {
671 return false; 607 return false;
672 } 608 }
673 609
674 SkTScopedComPtr<IUnknown> iunkB; 610 SkTScopedComPtr<IUnknown> iunkB;
675 if (FAILED(b->QueryInterface(&iunkB))) { 611 if (FAILED(b->QueryInterface(&iunkB))) {
676 return false; 612 return false;
677 } 613 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 707
772 SkSMallocWCHAR dwFontFamilyNameChar(dwFontFamilyNameLength+1); 708 SkSMallocWCHAR dwFontFamilyNameChar(dwFontFamilyNameLength+1);
773 SkSMallocWCHAR dwFontNameChar(dwFontNameLength+1); 709 SkSMallocWCHAR dwFontNameChar(dwFontNameLength+1);
774 HRB(dwFontFamilyNames->GetString(0, dwFontFamilyNameChar.get(), dwFontFamily NameLength+1)); 710 HRB(dwFontFamilyNames->GetString(0, dwFontFamilyNameChar.get(), dwFontFamily NameLength+1));
775 HRB(dwFontNames->GetString(0, dwFontNameChar.get(), dwFontNameLength+1)); 711 HRB(dwFontNames->GetString(0, dwFontNameChar.get(), dwFontNameLength+1));
776 712
777 return wcscmp(dwFaceFontFamilyNameChar.get(), dwFontFamilyNameChar.get()) == 0 && 713 return wcscmp(dwFaceFontFamilyNameChar.get(), dwFontFamilyNameChar.get()) == 0 &&
778 wcscmp(dwFaceFontNameChar.get(), dwFontNameChar.get()) == 0; 714 wcscmp(dwFaceFontNameChar.get(), dwFontNameChar.get()) == 0;
779 } 715 }
780 716
781 void SkDWriteFontFromTypeface(const SkTypeface* face, IDWriteFont** font) {
782 if (NULL == face) {
783 HRVM(get_default_font(font), "Could not get default font.");
784 } else {
785 *font = SkRefComPtr(static_cast<const DWriteFontTypeface*>(face)->fDWrit eFont.get());
786 }
787 }
788
789 SkScalerContext_DW::SkScalerContext_DW(DWriteFontTypeface* typeface, 717 SkScalerContext_DW::SkScalerContext_DW(DWriteFontTypeface* typeface,
790 const SkDescriptor* desc) 718 const SkDescriptor* desc)
791 : SkScalerContext(typeface, desc) 719 : SkScalerContext(typeface, desc)
792 , fTypeface(SkRef(typeface)) 720 , fTypeface(SkRef(typeface))
793 , fGlyphCount(-1) { 721 , fGlyphCount(-1) {
794 SkAutoMutexAcquire ac(gFTMutex); 722 SkAutoMutexAcquire ac(gFTMutex);
795 723
796 fXform.m11 = SkScalarToFloat(fRec.fPost2x2[0][0]); 724 fXform.m11 = SkScalarToFloat(fRec.fPost2x2[0][0]);
797 fXform.m12 = SkScalarToFloat(fRec.fPost2x2[1][0]); 725 fXform.m12 = SkScalarToFloat(fRec.fPost2x2[1][0]);
798 fXform.m21 = SkScalarToFloat(fRec.fPost2x2[0][1]); 726 fXform.m21 = SkScalarToFloat(fRec.fPost2x2[0][1]);
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 &fontFileStream), 1352 &fontFileStream),
1425 "Could not create font file stream."); 1353 "Could not create font file stream.");
1426 1354
1427 return SkNEW_ARGS(SkDWriteFontFileStream, (fontFileStream.get())); 1355 return SkNEW_ARGS(SkDWriteFontFileStream, (fontFileStream.get()));
1428 } 1356 }
1429 1357
1430 SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* d esc) const { 1358 SkScalerContext* DWriteFontTypeface::onCreateScalerContext(const SkDescriptor* d esc) const {
1431 return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this) , desc)); 1359 return SkNEW_ARGS(SkScalerContext_DW, (const_cast<DWriteFontTypeface*>(this) , desc));
1432 } 1360 }
1433 1361
1434 static HRESULT get_by_family_name(const char familyName[], IDWriteFontFamily** f ontFamily) {
1435 IDWriteFactory* factory;
1436 HR(get_dwrite_factory(&factory));
1437
1438 SkTScopedComPtr<IDWriteFontCollection> sysFontCollection;
1439 HR(factory->GetSystemFontCollection(&sysFontCollection, FALSE));
1440
1441 SkSMallocWCHAR wideFamilyName;
1442 HR(cstring_to_wchar(familyName, &wideFamilyName));
1443
1444 UINT32 index;
1445 BOOL exists;
1446 HR(sysFontCollection->FindFamilyName(wideFamilyName.get(), &index, &exists)) ;
1447
1448 if (exists) {
1449 HR(sysFontCollection->GetFontFamily(index, fontFamily));
1450 return S_OK;
1451 }
1452 return S_FALSE;
1453 }
1454
1455 void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const { 1362 void DWriteFontTypeface::onFilterRec(SkScalerContext::Rec* rec) const {
1456 if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag || 1363 if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag ||
1457 rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) 1364 rec->fFlags & SkScalerContext::kLCD_Vertical_Flag)
1458 { 1365 {
1459 rec->fMaskFormat = SkMask::kA8_Format; 1366 rec->fMaskFormat = SkMask::kA8_Format;
1460 } 1367 }
1461 1368
1462 unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag | 1369 unsigned flagsWeDontSupport = SkScalerContext::kDevKernText_Flag |
1463 SkScalerContext::kAutohinting_Flag | 1370 SkScalerContext::kAutohinting_Flag |
1464 SkScalerContext::kEmbeddedBitmapText_Flag | 1371 SkScalerContext::kEmbeddedBitmapText_Flag |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
1823 SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) { 1730 SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) {
1824 SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data))); 1731 SkAutoTUnref<SkStream> stream(SkNEW_ARGS(SkMemoryStream, (data)));
1825 return this->createFromStream(stream, ttcIndex); 1732 return this->createFromStream(stream, ttcIndex);
1826 } 1733 }
1827 1734
1828 SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIn dex) { 1735 SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIn dex) {
1829 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); 1736 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
1830 return this->createFromStream(stream, ttcIndex); 1737 return this->createFromStream(stream, ttcIndex);
1831 } 1738 }
1832 1739
1740 HRESULT SkFontMgr_DirectWrite::getByFamilyName(const WCHAR wideFamilyName[],
1741 IDWriteFontFamily** fontFamily) {
1742 UINT32 index;
1743 BOOL exists;
1744 HR(fFontCollection->FindFamilyName(wideFamilyName, &index, &exists));
1745
1746 if (exists) {
1747 HR(fFontCollection->GetFontFamily(index, fontFamily));
1748 return S_OK;
1749 }
1750 return S_FALSE;
1751 }
1752
1753 HRESULT SkFontMgr_DirectWrite::getDefaultFontFamily(IDWriteFontFamily** fontFami ly) {
1754 NONCLIENTMETRICSW metrics;
1755 metrics.cbSize = sizeof(metrics);
1756 if (0 == SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
1757 sizeof(metrics),
1758 &metrics,
1759 0)) {
1760 return E_UNEXPECTED;
1761 }
1762 HRM(this->getByFamilyName(metrics.lfMessageFont.lfFaceName, fontFamily),
1763 "Could not create DWrite font family from LOGFONT.");
1764
1765 return S_OK;
1766 }
1767
1833 SkTypeface* SkFontMgr_DirectWrite::onLegacyCreateTypeface(const char familyName[ ], 1768 SkTypeface* SkFontMgr_DirectWrite::onLegacyCreateTypeface(const char familyName[ ],
1834 unsigned styleBits) { 1769 unsigned styleBits) {
1835 SkTScopedComPtr<IDWriteFontFamily> fontFamily; 1770 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
1836 if (familyName) { 1771 if (familyName) {
1837 get_by_family_name(familyName, &fontFamily); 1772 SkSMallocWCHAR wideFamilyName;
1773 if (SUCCEEDED(cstring_to_wchar(familyName, &wideFamilyName))) {
1774 this->getByFamilyName(wideFamilyName, &fontFamily);
1775 }
1838 } 1776 }
1839 1777
1840 if (NULL == fontFamily.get()) { 1778 if (NULL == fontFamily.get()) {
1841 //No good family found, go with default. 1779 // No family with given name, try default.
1842 SkTScopedComPtr<IDWriteFont> defaultFont; 1780 HRNM(this->getDefaultFontFamily(&fontFamily), "Could not get default fon t family.");
1843 HRNM(get_default_font(&defaultFont), "Could not get default font.");
1844 HRNM(defaultFont->GetFontFamily(&fontFamily), "Could not get default fon t family.");
1845 } 1781 }
1846 1782
1847 SkTScopedComPtr<IDWriteFont> font; 1783 SkTScopedComPtr<IDWriteFont> font;
1848 DWRITE_FONT_WEIGHT weight = (styleBits & SkTypeface::kBold) 1784 DWRITE_FONT_WEIGHT weight = (styleBits & SkTypeface::kBold)
1849 ? DWRITE_FONT_WEIGHT_BOLD 1785 ? DWRITE_FONT_WEIGHT_BOLD
1850 : DWRITE_FONT_WEIGHT_NORMAL; 1786 : DWRITE_FONT_WEIGHT_NORMAL;
1851 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_UNDEFINED; 1787 DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_UNDEFINED;
1852 DWRITE_FONT_STYLE italic = (styleBits & SkTypeface::kItalic) 1788 DWRITE_FONT_STYLE italic = (styleBits & SkTypeface::kItalic)
1853 ? DWRITE_FONT_STYLE_ITALIC 1789 ? DWRITE_FONT_STYLE_ITALIC
1854 : DWRITE_FONT_STYLE_NORMAL; 1790 : DWRITE_FONT_STYLE_NORMAL;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1969 SK_TRACEHR(hr, "Could not get GetUserDefaultLocaleName."); 1905 SK_TRACEHR(hr, "Could not get GetUserDefaultLocaleName.");
1970 } else { 1906 } else {
1971 localeNameLen = getUserDefaultLocaleNameProc(localeNameStorage, LOCALE_N AME_MAX_LENGTH); 1907 localeNameLen = getUserDefaultLocaleNameProc(localeNameStorage, LOCALE_N AME_MAX_LENGTH);
1972 if (localeNameLen) { 1908 if (localeNameLen) {
1973 localeName = localeNameStorage; 1909 localeName = localeNameStorage;
1974 }; 1910 };
1975 } 1911 }
1976 1912
1977 return SkNEW_ARGS(SkFontMgr_DirectWrite, (sysFontCollection.get(), localeNam e, localeNameLen)); 1913 return SkNEW_ARGS(SkFontMgr_DirectWrite, (sysFontCollection.get(), localeNam e, localeNameLen));
1978 } 1914 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698