2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #if defined(SK_BUILD_FOR_WIN32)
12 #include "SkDWriteFontFileStream.h"
13 #include "SkFontMgr.h"
14 #include "SkHRESULT.h"
17 #include "SkTScopedComPtr.h"
18 #include "SkTypeface.h"
19 #include "SkTypefaceCache.h"
20 #include "SkTypeface_win_dw.h"
27 ////////////////////////////////////////////////////////////////////////////////
29 class StreamFontFileLoader : public IDWriteFontFileLoader {
32 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
33 virtual ULONG STDMETHODCALLTYPE AddRef();
34 virtual ULONG STDMETHODCALLTYPE Release();
36 // IDWriteFontFileLoader methods
37 virtual HRESULT STDMETHODCALLTYPE CreateStreamFromKey(
38 void const* fontFileReferenceKey,
39 UINT32 fontFileReferenceKeySize,
40 IDWriteFontFileStream** fontFileStream);
42 // Takes ownership of stream.
43 static HRESULT Create(SkStreamAsset* stream, StreamFontFileLoader** streamFontFileLoader) {
44 *streamFontFileLoader = new StreamFontFileLoader(stream);
45 if (nullptr == *streamFontFileLoader) {
51 std::unique_ptr<SkStreamAsset> fStream;
54 StreamFontFileLoader(SkStreamAsset* stream) : fStream(stream), fRefCount(1) { }
55 virtual ~StreamFontFileLoader() { }
60 HRESULT StreamFontFileLoader::QueryInterface(REFIID iid, void** ppvObject) {
61 if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontFileLoader)) {
71 ULONG StreamFontFileLoader::AddRef() {
72 return InterlockedIncrement(&fRefCount);
75 ULONG StreamFontFileLoader::Release() {
76 ULONG newCount = InterlockedDecrement(&fRefCount);
83 HRESULT StreamFontFileLoader::CreateStreamFromKey(
84 void const* fontFileReferenceKey,
85 UINT32 fontFileReferenceKeySize,
86 IDWriteFontFileStream** fontFileStream)
88 SkTScopedComPtr<SkDWriteFontFileStreamWrapper> stream;
89 HR(SkDWriteFontFileStreamWrapper::Create(fStream->duplicate(), &stream));
90 *fontFileStream = stream.release();
94 ////////////////////////////////////////////////////////////////////////////////
96 class StreamFontFileEnumerator : public IDWriteFontFileEnumerator {
99 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
100 virtual ULONG STDMETHODCALLTYPE AddRef();
101 virtual ULONG STDMETHODCALLTYPE Release();
103 // IDWriteFontFileEnumerator methods
104 virtual HRESULT STDMETHODCALLTYPE MoveNext(BOOL* hasCurrentFile);
105 virtual HRESULT STDMETHODCALLTYPE GetCurrentFontFile(IDWriteFontFile** fontFile);
107 static HRESULT Create(IDWriteFactory* factory, IDWriteFontFileLoader* fontFileLoader,
108 StreamFontFileEnumerator** streamFontFileEnumerator) {
109 *streamFontFileEnumerator = new StreamFontFileEnumerator(factory, fontFileLoader);
110 if (nullptr == *streamFontFileEnumerator) {
111 return E_OUTOFMEMORY;
116 StreamFontFileEnumerator(IDWriteFactory* factory, IDWriteFontFileLoader* fontFileLoader);
117 virtual ~StreamFontFileEnumerator() { }
121 SkTScopedComPtr<IDWriteFactory> fFactory;
122 SkTScopedComPtr<IDWriteFontFile> fCurrentFile;
123 SkTScopedComPtr<IDWriteFontFileLoader> fFontFileLoader;
127 StreamFontFileEnumerator::StreamFontFileEnumerator(IDWriteFactory* factory,
128 IDWriteFontFileLoader* fontFileLoader)
130 , fFactory(SkRefComPtr(factory))
132 , fFontFileLoader(SkRefComPtr(fontFileLoader))
136 HRESULT StreamFontFileEnumerator::QueryInterface(REFIID iid, void** ppvObject) {
137 if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontFileEnumerator)) {
142 *ppvObject = nullptr;
143 return E_NOINTERFACE;
147 ULONG StreamFontFileEnumerator::AddRef() {
148 return InterlockedIncrement(&fRefCount);
151 ULONG StreamFontFileEnumerator::Release() {
152 ULONG newCount = InterlockedDecrement(&fRefCount);
159 HRESULT StreamFontFileEnumerator::MoveNext(BOOL* hasCurrentFile) {
160 *hasCurrentFile = FALSE;
168 HR(fFactory->CreateCustomFontFileReference(
169 &dummy, //cannot be nullptr
170 sizeof(dummy), //even if this is 0
171 fFontFileLoader.get(),
174 *hasCurrentFile = TRUE;
178 HRESULT StreamFontFileEnumerator::GetCurrentFontFile(IDWriteFontFile** fontFile) {
179 if (fCurrentFile.get() == nullptr) {
184 *fontFile = SkRefComPtr(fCurrentFile.get());
188 ////////////////////////////////////////////////////////////////////////////////
190 class StreamFontCollectionLoader : public IDWriteFontCollectionLoader {
193 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
194 virtual ULONG STDMETHODCALLTYPE AddRef();
195 virtual ULONG STDMETHODCALLTYPE Release();
197 // IDWriteFontCollectionLoader methods
198 virtual HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey(
199 IDWriteFactory* factory,
200 void const* collectionKey,
201 UINT32 collectionKeySize,
202 IDWriteFontFileEnumerator** fontFileEnumerator);
204 static HRESULT Create(IDWriteFontFileLoader* fontFileLoader,
205 StreamFontCollectionLoader** streamFontCollectionLoader) {
206 *streamFontCollectionLoader = new StreamFontCollectionLoader(fontFileLoader);
207 if (nullptr == *streamFontCollectionLoader) {
208 return E_OUTOFMEMORY;
213 StreamFontCollectionLoader(IDWriteFontFileLoader* fontFileLoader)
215 , fFontFileLoader(SkRefComPtr(fontFileLoader))
217 virtual ~StreamFontCollectionLoader() { }
220 SkTScopedComPtr<IDWriteFontFileLoader> fFontFileLoader;
223 HRESULT StreamFontCollectionLoader::QueryInterface(REFIID iid, void** ppvObject) {
224 if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontCollectionLoader)) {
229 *ppvObject = nullptr;
230 return E_NOINTERFACE;
234 ULONG StreamFontCollectionLoader::AddRef() {
235 return InterlockedIncrement(&fRefCount);
238 ULONG StreamFontCollectionLoader::Release() {
239 ULONG newCount = InterlockedDecrement(&fRefCount);
246 HRESULT StreamFontCollectionLoader::CreateEnumeratorFromKey(
247 IDWriteFactory* factory,
248 void const* collectionKey,
249 UINT32 collectionKeySize,
250 IDWriteFontFileEnumerator** fontFileEnumerator)
252 SkTScopedComPtr<StreamFontFileEnumerator> enumerator;
253 HR(StreamFontFileEnumerator::Create(factory, fFontFileLoader.get(), &enumerator));
254 *fontFileEnumerator = enumerator.release();
258 ////////////////////////////////////////////////////////////////////////////////
260 class SkFontMgr_DirectWrite : public SkFontMgr {
262 /** localeNameLength must include the null terminator. */
263 SkFontMgr_DirectWrite(IDWriteFactory* factory, IDWriteFontCollection* fontCollection,
264 IDWriteFontFallback* fallback, WCHAR* localeName, int localeNameLength)
265 : fFactory(SkRefComPtr(factory))
266 , fFontFallback(SkSafeRefComPtr(fallback))
267 , fFontCollection(SkRefComPtr(fontCollection))
268 , fLocaleName(localeNameLength)
270 if (!SUCCEEDED(fFactory->QueryInterface(&fFactory2))) {
271 // IUnknown::QueryInterface states that if it fails, punk will be set to nullptr.
272 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/26/96777.aspx
273 SkASSERT_RELEASE(nullptr == fFactory2.get());
275 if (fFontFallback.get()) {
276 // factory must be provided if fallback is non-null, else the fallback will not be used.
277 SkASSERT(fFactory2.get());
279 memcpy(fLocaleName.get(), localeName, localeNameLength * sizeof(WCHAR));
283 int onCountFamilies() const override;
284 void onGetFamilyName(int index, SkString* familyName) const override;
285 SkFontStyleSet* onCreateStyleSet(int index) const override;
286 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
287 SkTypeface* onMatchFamilyStyle(const char familyName[],
288 const SkFontStyle& fontstyle) const override;
289 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
290 const char* bcp47[], int bcp47Count,
291 SkUnichar character) const override;
292 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
293 const SkFontStyle& fontstyle) const override;
294 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override;
295 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override;
296 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override;
297 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const override;
300 HRESULT getByFamilyName(const WCHAR familyName[], IDWriteFontFamily** fontFamily) const;
301 HRESULT getDefaultFontFamily(IDWriteFontFamily** fontFamily) const;
303 /** Creates a typeface using a typeface cache. */
304 SkTypeface* createTypefaceFromDWriteFont(IDWriteFontFace* fontFace,
306 IDWriteFontFamily* fontFamily) const;
308 SkTScopedComPtr<IDWriteFactory> fFactory;
309 SkTScopedComPtr<IDWriteFactory2> fFactory2;
310 SkTScopedComPtr<IDWriteFontFallback> fFontFallback;
311 SkTScopedComPtr<IDWriteFontCollection> fFontCollection;
312 SkSMallocWCHAR fLocaleName;
313 mutable SkMutex fTFCacheMutex;
314 mutable SkTypefaceCache fTFCache;
316 friend class SkFontStyleSet_DirectWrite;
317 friend class FontFallbackRenderer;
320 class SkFontStyleSet_DirectWrite : public SkFontStyleSet {
322 SkFontStyleSet_DirectWrite(const SkFontMgr_DirectWrite* fontMgr,
323 IDWriteFontFamily* fontFamily)
324 : fFontMgr(SkRef(fontMgr))
325 , fFontFamily(SkRefComPtr(fontFamily))
328 int count() override;
329 void getStyle(int index, SkFontStyle* fs, SkString* styleName) override;
330 SkTypeface* createTypeface(int index) override;
331 SkTypeface* matchStyle(const SkFontStyle& pattern) override;
334 sk_sp<const SkFontMgr_DirectWrite> fFontMgr;
335 SkTScopedComPtr<IDWriteFontFamily> fFontFamily;
338 static HRESULT are_same(IUnknown* a, IUnknown* b, bool& same) {
339 SkTScopedComPtr<IUnknown> iunkA;
340 HRM(a->QueryInterface(&iunkA), "Failed to QI<IUnknown> for a.");
342 SkTScopedComPtr<IUnknown> iunkB;
343 HRM(b->QueryInterface(&iunkB), "Failed to QI<IUnknown> for b.");
345 same = (iunkA.get() == iunkB.get());
349 struct ProtoDWriteTypeface {
350 IDWriteFontFace* fDWriteFontFace;
351 IDWriteFont* fDWriteFont;
352 IDWriteFontFamily* fDWriteFontFamily;
355 static bool FindByDWriteFont(SkTypeface* cached, void* ctx) {
356 DWriteFontTypeface* cshFace = reinterpret_cast<DWriteFontTypeface*>(cached);
357 ProtoDWriteTypeface* ctxFace = reinterpret_cast<ProtoDWriteTypeface*>(ctx);
360 //Check to see if the two fonts are identical.
361 HRB(are_same(cshFace->fDWriteFont.get(), ctxFace->fDWriteFont, same));
366 HRB(are_same(cshFace->fDWriteFontFace.get(), ctxFace->fDWriteFontFace, same));
371 //Check if the two fonts share the same loader and have the same key.
374 HRB(cshFace->fDWriteFontFace->GetFiles(&cshNumFiles, nullptr));
375 HRB(ctxFace->fDWriteFontFace->GetFiles(&ctxNumFiles, nullptr));
376 if (cshNumFiles != ctxNumFiles) {
380 SkTScopedComPtr<IDWriteFontFile> cshFontFile;
381 SkTScopedComPtr<IDWriteFontFile> ctxFontFile;
382 HRB(cshFace->fDWriteFontFace->GetFiles(&cshNumFiles, &cshFontFile));
383 HRB(ctxFace->fDWriteFontFace->GetFiles(&ctxNumFiles, &ctxFontFile));
385 //for (each file) { //we currently only admit fonts from one file.
386 SkTScopedComPtr<IDWriteFontFileLoader> cshFontFileLoader;
387 SkTScopedComPtr<IDWriteFontFileLoader> ctxFontFileLoader;
388 HRB(cshFontFile->GetLoader(&cshFontFileLoader));
389 HRB(ctxFontFile->GetLoader(&ctxFontFileLoader));
390 HRB(are_same(cshFontFileLoader.get(), ctxFontFileLoader.get(), same));
396 const void* cshRefKey;
397 UINT32 cshRefKeySize;
398 const void* ctxRefKey;
399 UINT32 ctxRefKeySize;
400 HRB(cshFontFile->GetReferenceKey(&cshRefKey, &cshRefKeySize));
401 HRB(ctxFontFile->GetReferenceKey(&ctxRefKey, &ctxRefKeySize));
402 if (cshRefKeySize != ctxRefKeySize) {
405 if (0 != memcmp(cshRefKey, ctxRefKey, ctxRefKeySize)) {
409 //TODO: better means than comparing name strings?
410 //NOTE: .ttc and fake bold/italic will end up here.
411 SkTScopedComPtr<IDWriteLocalizedStrings> cshFamilyNames;
412 SkTScopedComPtr<IDWriteLocalizedStrings> cshFaceNames;
413 HRB(cshFace->fDWriteFontFamily->GetFamilyNames(&cshFamilyNames));
414 HRB(cshFace->fDWriteFont->GetFaceNames(&cshFaceNames));
415 UINT32 cshFamilyNameLength;
416 UINT32 cshFaceNameLength;
417 HRB(cshFamilyNames->GetStringLength(0, &cshFamilyNameLength));
418 HRB(cshFaceNames->GetStringLength(0, &cshFaceNameLength));
420 SkTScopedComPtr<IDWriteLocalizedStrings> ctxFamilyNames;
421 SkTScopedComPtr<IDWriteLocalizedStrings> ctxFaceNames;
422 HRB(ctxFace->fDWriteFontFamily->GetFamilyNames(&ctxFamilyNames));
423 HRB(ctxFace->fDWriteFont->GetFaceNames(&ctxFaceNames));
424 UINT32 ctxFamilyNameLength;
425 UINT32 ctxFaceNameLength;
426 HRB(ctxFamilyNames->GetStringLength(0, &ctxFamilyNameLength));
427 HRB(ctxFaceNames->GetStringLength(0, &ctxFaceNameLength));
429 if (cshFamilyNameLength != ctxFamilyNameLength ||
430 cshFaceNameLength != ctxFaceNameLength)
435 SkSMallocWCHAR cshFamilyName(cshFamilyNameLength+1);
436 SkSMallocWCHAR cshFaceName(cshFaceNameLength+1);
437 HRB(cshFamilyNames->GetString(0, cshFamilyName.get(), cshFamilyNameLength+1));
438 HRB(cshFaceNames->GetString(0, cshFaceName.get(), cshFaceNameLength+1));
440 SkSMallocWCHAR ctxFamilyName(ctxFamilyNameLength+1);
441 SkSMallocWCHAR ctxFaceName(ctxFaceNameLength+1);
442 HRB(ctxFamilyNames->GetString(0, ctxFamilyName.get(), ctxFamilyNameLength+1));
443 HRB(ctxFaceNames->GetString(0, ctxFaceName.get(), ctxFaceNameLength+1));
445 return wcscmp(cshFamilyName.get(), ctxFamilyName.get()) == 0 &&
446 wcscmp(cshFaceName.get(), ctxFaceName.get()) == 0;
449 SkTypeface* SkFontMgr_DirectWrite::createTypefaceFromDWriteFont(
450 IDWriteFontFace* fontFace,
452 IDWriteFontFamily* fontFamily) const {
453 SkAutoMutexAcquire ama(fTFCacheMutex);
454 ProtoDWriteTypeface spec = { fontFace, font, fontFamily };
455 SkTypeface* face = fTFCache.findByProcAndRef(FindByDWriteFont, &spec);
456 if (nullptr == face) {
457 face = DWriteFontTypeface::Create(fFactory.get(), fontFace, font, fontFamily);
465 int SkFontMgr_DirectWrite::onCountFamilies() const {
466 return fFontCollection->GetFontFamilyCount();
469 void SkFontMgr_DirectWrite::onGetFamilyName(int index, SkString* familyName) const {
470 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
471 HRVM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requested family.");
473 SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
474 HRVM(fontFamily->GetFamilyNames(&familyNames), "Could not get family names.");
476 sk_get_locale_string(familyNames.get(), fLocaleName.get(), familyName);
479 SkFontStyleSet* SkFontMgr_DirectWrite::onCreateStyleSet(int index) const {
480 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
481 HRNM(fFontCollection->GetFontFamily(index, &fontFamily), "Could not get requested family.");
483 return new SkFontStyleSet_DirectWrite(this, fontFamily.get());
486 SkFontStyleSet* SkFontMgr_DirectWrite::onMatchFamily(const char familyName[]) const {
487 SkSMallocWCHAR dwFamilyName;
488 HRN(sk_cstring_to_wchar(familyName, &dwFamilyName));
492 HRNM(fFontCollection->FindFamilyName(dwFamilyName.get(), &index, &exists),
493 "Failed while finding family by name.");
498 return this->onCreateStyleSet(index);
501 SkTypeface* SkFontMgr_DirectWrite::onMatchFamilyStyle(const char familyName[],
502 const SkFontStyle& fontstyle) const {
503 sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
504 return sset->matchStyle(fontstyle);
507 class FontFallbackRenderer : public IDWriteTextRenderer {
509 FontFallbackRenderer(const SkFontMgr_DirectWrite* outer, UINT32 character)
510 : fRefCount(1), fOuter(SkSafeRef(outer)), fCharacter(character), fResolvedTypeface(nullptr) {
513 virtual ~FontFallbackRenderer() { }
515 // IDWriteTextRenderer methods
516 virtual HRESULT STDMETHODCALLTYPE DrawGlyphRun(
517 void* clientDrawingContext,
518 FLOAT baselineOriginX,
519 FLOAT baselineOriginY,
520 DWRITE_MEASURING_MODE measuringMode,
521 DWRITE_GLYPH_RUN const* glyphRun,
522 DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
523 IUnknown* clientDrawingEffect) override
525 SkTScopedComPtr<IDWriteFont> font;
526 HRM(fOuter->fFontCollection->GetFontFromFontFace(glyphRun->fontFace, &font),
527 "Could not get font from font face.");
529 // It is possible that the font passed does not actually have the requested character,
530 // due to no font being found and getting the fallback font.
531 // Check that the font actually contains the requested character.
533 HRM(font->HasCharacter(fCharacter, &exists), "Could not find character.");
536 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
537 HRM(font->GetFontFamily(&fontFamily), "Could not get family.");
538 fResolvedTypeface = fOuter->createTypefaceFromDWriteFont(glyphRun->fontFace,
546 virtual HRESULT STDMETHODCALLTYPE DrawUnderline(
547 void* clientDrawingContext,
548 FLOAT baselineOriginX,
549 FLOAT baselineOriginY,
550 DWRITE_UNDERLINE const* underline,
551 IUnknown* clientDrawingEffect) override
552 { return E_NOTIMPL; }
554 virtual HRESULT STDMETHODCALLTYPE DrawStrikethrough(
555 void* clientDrawingContext,
556 FLOAT baselineOriginX,
557 FLOAT baselineOriginY,
558 DWRITE_STRIKETHROUGH const* strikethrough,
559 IUnknown* clientDrawingEffect) override
560 { return E_NOTIMPL; }
562 virtual HRESULT STDMETHODCALLTYPE DrawInlineObject(
563 void* clientDrawingContext,
566 IDWriteInlineObject* inlineObject,
569 IUnknown* clientDrawingEffect) override
570 { return E_NOTIMPL; }
572 // IDWritePixelSnapping methods
573 virtual HRESULT STDMETHODCALLTYPE IsPixelSnappingDisabled(
574 void* clientDrawingContext,
575 BOOL* isDisabled) override
581 virtual HRESULT STDMETHODCALLTYPE GetCurrentTransform(
582 void* clientDrawingContext,
583 DWRITE_MATRIX* transform) override
585 const DWRITE_MATRIX ident = { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 };
590 virtual HRESULT STDMETHODCALLTYPE GetPixelsPerDip(
591 void* clientDrawingContext,
592 FLOAT* pixelsPerDip) override
594 *pixelsPerDip = 1.0f;
599 ULONG STDMETHODCALLTYPE AddRef() override {
600 return InterlockedIncrement(&fRefCount);
603 ULONG STDMETHODCALLTYPE Release() override {
604 ULONG newCount = InterlockedDecrement(&fRefCount);
611 virtual HRESULT STDMETHODCALLTYPE QueryInterface(IID const& riid, void** ppvObject) override{
612 if (__uuidof(IUnknown) == riid ||
613 __uuidof(IDWritePixelSnapping) == riid ||
614 __uuidof(IDWriteTextRenderer) == riid)
620 *ppvObject = nullptr;
624 SkTypeface* FallbackTypeface() { return fResolvedTypeface; }
628 sk_sp<const SkFontMgr_DirectWrite> fOuter;
630 SkTypeface* fResolvedTypeface;
633 class FontFallbackSource : public IDWriteTextAnalysisSource {
635 FontFallbackSource(const WCHAR* string, UINT32 length, const WCHAR* locale,
636 IDWriteNumberSubstitution* numberSubstitution)
640 , fNumberSubstitution(numberSubstitution)
643 virtual ~FontFallbackSource() { }
645 // IDWriteTextAnalysisSource methods
646 virtual HRESULT STDMETHODCALLTYPE GetTextAtPosition(
648 WCHAR const** textString,
649 UINT32* textLength) override
651 if (fLength <= textPosition) {
652 *textString = nullptr;
656 *textString = fString + textPosition;
657 *textLength = fLength - textPosition;
661 virtual HRESULT STDMETHODCALLTYPE GetTextBeforePosition(
663 WCHAR const** textString,
664 UINT32* textLength) override
666 if (textPosition < 1 || fLength <= textPosition) {
667 *textString = nullptr;
671 *textString = fString;
672 *textLength = textPosition;
676 virtual DWRITE_READING_DIRECTION STDMETHODCALLTYPE GetParagraphReadingDirection() override {
677 // TODO: this is also interesting.
678 return DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
681 virtual HRESULT STDMETHODCALLTYPE GetLocaleName(
684 WCHAR const** localeName) override
686 *localeName = fLocale;
690 virtual HRESULT STDMETHODCALLTYPE GetNumberSubstitution(
693 IDWriteNumberSubstitution** numberSubstitution) override
695 *numberSubstitution = fNumberSubstitution;
700 ULONG STDMETHODCALLTYPE AddRef() override {
701 return InterlockedIncrement(&fRefCount);
704 ULONG STDMETHODCALLTYPE Release() override {
705 ULONG newCount = InterlockedDecrement(&fRefCount);
712 virtual HRESULT STDMETHODCALLTYPE QueryInterface(IID const& riid, void** ppvObject) override{
713 if (__uuidof(IUnknown) == riid ||
714 __uuidof(IDWriteTextAnalysisSource) == riid)
720 *ppvObject = nullptr;
726 const WCHAR* fString;
728 const WCHAR* fLocale;
729 IDWriteNumberSubstitution* fNumberSubstitution;
732 SkTypeface* SkFontMgr_DirectWrite::onMatchFamilyStyleCharacter(const char familyName[],
733 const SkFontStyle& style,
734 const char* bcp47[], int bcp47Count,
735 SkUnichar character) const
737 const DWriteStyle dwStyle(style);
739 const WCHAR* dwFamilyName = nullptr;
740 SkSMallocWCHAR dwFamilyNameLocal;
742 HRN(sk_cstring_to_wchar(familyName, &dwFamilyNameLocal));
743 dwFamilyName = dwFamilyNameLocal;
747 UINT32 strLen = static_cast<UINT32>(
748 SkUTF16_FromUnichar(character, reinterpret_cast<uint16_t*>(str)));
750 const SkSMallocWCHAR* dwBcp47;
751 SkSMallocWCHAR dwBcp47Local;
752 if (bcp47Count < 1) {
753 dwBcp47 = &fLocaleName;
755 // TODO: support fallback stack.
756 // TODO: DirectWrite supports 'zh-CN' or 'zh-Hans', but 'zh' misses completely
757 // and may produce a Japanese font.
758 HRN(sk_cstring_to_wchar(bcp47[bcp47Count - 1], &dwBcp47Local));
759 dwBcp47 = &dwBcp47Local;
762 if (fFactory2.get()) {
763 SkTScopedComPtr<IDWriteFontFallback> systemFontFallback;
764 IDWriteFontFallback* fontFallback = fFontFallback.get();
766 HRNM(fFactory2->GetSystemFontFallback(&systemFontFallback),
767 "Could not get system fallback.");
768 fontFallback = systemFontFallback.get();
771 SkTScopedComPtr<IDWriteNumberSubstitution> numberSubstitution;
772 HRNM(fFactory2->CreateNumberSubstitution(DWRITE_NUMBER_SUBSTITUTION_METHOD_NONE, nullptr, TRUE,
773 &numberSubstitution),
774 "Could not create number substitution.");
775 SkTScopedComPtr<FontFallbackSource> fontFallbackSource(
776 new FontFallbackSource(str, strLen, *dwBcp47, numberSubstitution.get()));
779 SkTScopedComPtr<IDWriteFont> font;
781 HRNM(fontFallback->MapCharacters(fontFallbackSource.get(),
784 fFontCollection.get(),
792 "Could not map characters");
797 SkTScopedComPtr<IDWriteFontFace> fontFace;
798 HRNM(font->CreateFontFace(&fontFace), "Could not get font face from font.");
800 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
801 HRNM(font->GetFontFamily(&fontFamily), "Could not get family from font.");
802 return this->createTypefaceFromDWriteFont(fontFace.get(), font.get(), fontFamily.get());
805 SkTScopedComPtr<IDWriteTextFormat> fallbackFormat;
806 HRNM(fFactory->CreateTextFormat(dwFamilyName ? dwFamilyName : L"",
807 fFontCollection.get(),
814 "Could not create text format.");
816 SkTScopedComPtr<IDWriteTextLayout> fallbackLayout;
817 HRNM(fFactory->CreateTextLayout(str, strLen, fallbackFormat.get(),
820 "Could not create text layout.");
822 SkTScopedComPtr<FontFallbackRenderer> fontFallbackRenderer(
823 new FontFallbackRenderer(this, character));
825 HRNM(fallbackLayout->Draw(nullptr, fontFallbackRenderer.get(), 50.0f, 50.0f),
826 "Could not draw layout with renderer.");
828 return fontFallbackRenderer->FallbackTypeface();
831 SkTypeface* SkFontMgr_DirectWrite::onMatchFaceStyle(const SkTypeface* familyMember,
832 const SkFontStyle& fontstyle) const {
834 SkFontStyleSet_DirectWrite sset(
835 this, ((DWriteFontTypeface*)familyMember)->fDWriteFontFamily.get()
837 return sset.matchStyle(fontstyle);
840 template <typename T> class SkAutoIDWriteUnregister {
842 SkAutoIDWriteUnregister(IDWriteFactory* factory, T* unregister)
843 : fFactory(factory), fUnregister(unregister)
846 ~SkAutoIDWriteUnregister() {
848 unregister(fFactory, fUnregister);
853 T* old = fUnregister;
854 fUnregister = nullptr;
859 HRESULT unregister(IDWriteFactory* factory, IDWriteFontFileLoader* unregister) {
860 return factory->UnregisterFontFileLoader(unregister);
863 HRESULT unregister(IDWriteFactory* factory, IDWriteFontCollectionLoader* unregister) {
864 return factory->UnregisterFontCollectionLoader(unregister);
867 IDWriteFactory* fFactory;
871 SkTypeface* SkFontMgr_DirectWrite::onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const {
872 SkTScopedComPtr<StreamFontFileLoader> fontFileLoader;
873 // This transfers ownership of stream to the new object.
874 HRN(StreamFontFileLoader::Create(stream, &fontFileLoader));
875 HRN(fFactory->RegisterFontFileLoader(fontFileLoader.get()));
876 SkAutoIDWriteUnregister<StreamFontFileLoader> autoUnregisterFontFileLoader(
877 fFactory.get(), fontFileLoader.get());
879 SkTScopedComPtr<StreamFontCollectionLoader> fontCollectionLoader;
880 HRN(StreamFontCollectionLoader::Create(fontFileLoader.get(), &fontCollectionLoader));
881 HRN(fFactory->RegisterFontCollectionLoader(fontCollectionLoader.get()));
882 SkAutoIDWriteUnregister<StreamFontCollectionLoader> autoUnregisterFontCollectionLoader(
883 fFactory.get(), fontCollectionLoader.get());
885 SkTScopedComPtr<IDWriteFontCollection> fontCollection;
886 HRN(fFactory->CreateCustomFontCollection(fontCollectionLoader.get(), nullptr, 0, &fontCollection));
888 // Find the first non-simulated font which has the given ttc index.
889 UINT32 familyCount = fontCollection->GetFontFamilyCount();
890 for (UINT32 familyIndex = 0; familyIndex < familyCount; ++familyIndex) {
891 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
892 HRN(fontCollection->GetFontFamily(familyIndex, &fontFamily));
894 UINT32 fontCount = fontFamily->GetFontCount();
895 for (UINT32 fontIndex = 0; fontIndex < fontCount; ++fontIndex) {
896 SkTScopedComPtr<IDWriteFont> font;
897 HRN(fontFamily->GetFont(fontIndex, &font));
898 if (font->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE) {
902 SkTScopedComPtr<IDWriteFontFace> fontFace;
903 HRN(font->CreateFontFace(&fontFace));
905 UINT32 faceIndex = fontFace->GetIndex();
906 if (faceIndex == ttcIndex) {
907 return DWriteFontTypeface::Create(fFactory.get(),
908 fontFace.get(), font.get(), fontFamily.get(),
909 autoUnregisterFontFileLoader.detatch(),
910 autoUnregisterFontCollectionLoader.detatch());
918 SkTypeface* SkFontMgr_DirectWrite::onCreateFromData(SkData* data, int ttcIndex) const {
919 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIndex);
922 SkTypeface* SkFontMgr_DirectWrite::onCreateFromFile(const char path[], int ttcIndex) const {
923 return this->createFromStream(SkStream::MakeFromFile(path).release(), ttcIndex);
926 HRESULT SkFontMgr_DirectWrite::getByFamilyName(const WCHAR wideFamilyName[],
927 IDWriteFontFamily** fontFamily) const {
930 HR(fFontCollection->FindFamilyName(wideFamilyName, &index, &exists));
933 HR(fFontCollection->GetFontFamily(index, fontFamily));
938 HRESULT SkFontMgr_DirectWrite::getDefaultFontFamily(IDWriteFontFamily** fontFamily) const {
939 NONCLIENTMETRICSW metrics;
940 metrics.cbSize = sizeof(metrics);
941 if (0 == SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
947 HRM(this->getByFamilyName(metrics.lfMessageFont.lfFaceName, fontFamily),
948 "Could not create DWrite font family from LOGFONT.");
952 SkTypeface* SkFontMgr_DirectWrite::onLegacyCreateTypeface(const char familyName[],
953 SkFontStyle style) const {
954 SkTScopedComPtr<IDWriteFontFamily> fontFamily;
956 SkSMallocWCHAR wideFamilyName;
957 if (SUCCEEDED(sk_cstring_to_wchar(familyName, &wideFamilyName))) {
958 this->getByFamilyName(wideFamilyName, &fontFamily);
962 if (nullptr == fontFamily.get()) {
963 // No family with given name, try default.
964 HRNM(this->getDefaultFontFamily(&fontFamily), "Could not get default font family.");
967 if (nullptr == fontFamily.get()) {
968 // Could not obtain the default font.
969 HRNM(fFontCollection->GetFontFamily(0, &fontFamily),
970 "Could not get default-default font family.");
973 SkTScopedComPtr<IDWriteFont> font;
974 DWriteStyle dwStyle(style);
975 HRNM(fontFamily->GetFirstMatchingFont(dwStyle.fWeight, dwStyle.fWidth, dwStyle.fSlant, &font),
976 "Could not get matching font.");
978 SkTScopedComPtr<IDWriteFontFace> fontFace;
979 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
981 return this->createTypefaceFromDWriteFont(fontFace.get(), font.get(), fontFamily.get());
984 ///////////////////////////////////////////////////////////////////////////////
986 int SkFontStyleSet_DirectWrite::count() {
987 return fFontFamily->GetFontCount();
990 SkTypeface* SkFontStyleSet_DirectWrite::createTypeface(int index) {
991 SkTScopedComPtr<IDWriteFont> font;
992 HRNM(fFontFamily->GetFont(index, &font), "Could not get font.");
994 SkTScopedComPtr<IDWriteFontFace> fontFace;
995 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
997 return fFontMgr->createTypefaceFromDWriteFont(fontFace.get(), font.get(), fFontFamily.get());
1000 void SkFontStyleSet_DirectWrite::getStyle(int index, SkFontStyle* fs, SkString* styleName) {
1001 SkTScopedComPtr<IDWriteFont> font;
1002 HRVM(fFontFamily->GetFont(index, &font), "Could not get font.");
1005 *fs = get_style(font.get());
1009 SkTScopedComPtr<IDWriteLocalizedStrings> faceNames;
1010 if (SUCCEEDED(font->GetFaceNames(&faceNames))) {
1011 sk_get_locale_string(faceNames.get(), fFontMgr->fLocaleName.get(), styleName);
1016 SkTypeface* SkFontStyleSet_DirectWrite::matchStyle(const SkFontStyle& pattern) {
1017 SkTScopedComPtr<IDWriteFont> font;
1018 DWriteStyle dwStyle(pattern);
1019 // TODO: perhaps use GetMatchingFonts and get the least simulated?
1020 HRNM(fFontFamily->GetFirstMatchingFont(dwStyle.fWeight, dwStyle.fWidth, dwStyle.fSlant, &font),
1021 "Could not match font in family.");
1023 SkTScopedComPtr<IDWriteFontFace> fontFace;
1024 HRNM(font->CreateFontFace(&fontFace), "Could not create font face.");
1026 return fFontMgr->createTypefaceFromDWriteFont(fontFace.get(), font.get(),
1030 ////////////////////////////////////////////////////////////////////////////////
1031 #include "SkTypeface_win.h"
1033 SK_API sk_sp<SkFontMgr> SkFontMgr_New_DirectWrite(IDWriteFactory* factory,
1034 IDWriteFontCollection* collection) {
1035 return SkFontMgr_New_DirectWrite(factory, collection, nullptr);
1038 SK_API sk_sp<SkFontMgr> SkFontMgr_New_DirectWrite(IDWriteFactory* factory,
1039 IDWriteFontCollection* collection,
1040 IDWriteFontFallback* fallback) {
1041 if (nullptr == factory) {
1042 factory = sk_get_dwrite_factory();
1043 if (nullptr == factory) {
1048 SkTScopedComPtr<IDWriteFontCollection> systemFontCollection;
1049 if (nullptr == collection) {
1050 HRNM(factory->GetSystemFontCollection(&systemFontCollection, FALSE),
1051 "Could not get system font collection.");
1052 collection = systemFontCollection.get();
1055 WCHAR localeNameStorage[LOCALE_NAME_MAX_LENGTH];
1056 WCHAR* localeName = nullptr;
1057 int localeNameLen = 0;
1059 // Dynamically load GetUserDefaultLocaleName function, as it is not available on XP.
1060 SkGetUserDefaultLocaleNameProc getUserDefaultLocaleNameProc = nullptr;
1061 HRESULT hr = SkGetGetUserDefaultLocaleNameProc(&getUserDefaultLocaleNameProc);
1062 if (nullptr == getUserDefaultLocaleNameProc) {
1063 SK_TRACEHR(hr, "Could not get GetUserDefaultLocaleName.");
1065 localeNameLen = getUserDefaultLocaleNameProc(localeNameStorage, LOCALE_NAME_MAX_LENGTH);
1066 if (localeNameLen) {
1067 localeName = localeNameStorage;
1071 return sk_make_sp<SkFontMgr_DirectWrite>(factory, collection, fallback,
1072 localeName, localeNameLen);
1075 #include "SkFontMgr_indirect.h"
1076 SK_API sk_sp<SkFontMgr> SkFontMgr_New_DirectWriteRenderer(sk_sp<SkRemotableFontMgr> proxy) {
1077 sk_sp<SkFontMgr> impl(SkFontMgr_New_DirectWrite());
1081 return sk_make_sp<SkFontMgr_Indirect>(std::move(impl), std::move(proxy));
1083 #endif//defined(SK_BUILD_FOR_WIN32)