michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_DWRITECOMMON_H michael@0: #define GFX_DWRITECOMMON_H michael@0: michael@0: // Mozilla includes michael@0: #include "nscore.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "cairo-features.h" michael@0: #include "gfxFontConstants.h" michael@0: #include "nsTArray.h" michael@0: #include "gfxWindowsPlatform.h" michael@0: #include "nsIUUIDGenerator.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: static DWRITE_FONT_STRETCH michael@0: DWriteFontStretchFromStretch(int16_t aStretch) michael@0: { michael@0: switch (aStretch) { michael@0: case NS_FONT_STRETCH_ULTRA_CONDENSED: michael@0: return DWRITE_FONT_STRETCH_ULTRA_CONDENSED; michael@0: case NS_FONT_STRETCH_EXTRA_CONDENSED: michael@0: return DWRITE_FONT_STRETCH_EXTRA_CONDENSED; michael@0: case NS_FONT_STRETCH_CONDENSED: michael@0: return DWRITE_FONT_STRETCH_CONDENSED; michael@0: case NS_FONT_STRETCH_SEMI_CONDENSED: michael@0: return DWRITE_FONT_STRETCH_SEMI_CONDENSED; michael@0: case NS_FONT_STRETCH_NORMAL: michael@0: return DWRITE_FONT_STRETCH_NORMAL; michael@0: case NS_FONT_STRETCH_SEMI_EXPANDED: michael@0: return DWRITE_FONT_STRETCH_SEMI_EXPANDED; michael@0: case NS_FONT_STRETCH_EXPANDED: michael@0: return DWRITE_FONT_STRETCH_EXPANDED; michael@0: case NS_FONT_STRETCH_EXTRA_EXPANDED: michael@0: return DWRITE_FONT_STRETCH_EXTRA_EXPANDED; michael@0: case NS_FONT_STRETCH_ULTRA_EXPANDED: michael@0: return DWRITE_FONT_STRETCH_ULTRA_EXPANDED; michael@0: default: michael@0: return DWRITE_FONT_STRETCH_UNDEFINED; michael@0: } michael@0: } michael@0: michael@0: static int16_t michael@0: FontStretchFromDWriteStretch(DWRITE_FONT_STRETCH aStretch) michael@0: { michael@0: switch (aStretch) { michael@0: case DWRITE_FONT_STRETCH_ULTRA_CONDENSED: michael@0: return NS_FONT_STRETCH_ULTRA_CONDENSED; michael@0: case DWRITE_FONT_STRETCH_EXTRA_CONDENSED: michael@0: return NS_FONT_STRETCH_EXTRA_CONDENSED; michael@0: case DWRITE_FONT_STRETCH_CONDENSED: michael@0: return NS_FONT_STRETCH_CONDENSED; michael@0: case DWRITE_FONT_STRETCH_SEMI_CONDENSED: michael@0: return NS_FONT_STRETCH_SEMI_CONDENSED; michael@0: case DWRITE_FONT_STRETCH_NORMAL: michael@0: return NS_FONT_STRETCH_NORMAL; michael@0: case DWRITE_FONT_STRETCH_SEMI_EXPANDED: michael@0: return NS_FONT_STRETCH_SEMI_EXPANDED; michael@0: case DWRITE_FONT_STRETCH_EXPANDED: michael@0: return NS_FONT_STRETCH_EXPANDED; michael@0: case DWRITE_FONT_STRETCH_EXTRA_EXPANDED: michael@0: return NS_FONT_STRETCH_EXTRA_EXPANDED; michael@0: case DWRITE_FONT_STRETCH_ULTRA_EXPANDED: michael@0: return NS_FONT_STRETCH_ULTRA_EXPANDED; michael@0: default: michael@0: return NS_FONT_STRETCH_NORMAL; michael@0: } michael@0: } michael@0: michael@0: struct ffReferenceKey michael@0: { michael@0: FallibleTArray *mArray; michael@0: nsID mGUID; michael@0: }; michael@0: michael@0: class gfxDWriteFontFileLoader : public IDWriteFontFileLoader michael@0: { michael@0: public: michael@0: gfxDWriteFontFileLoader() michael@0: { michael@0: } michael@0: michael@0: // IUnknown interface michael@0: IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) michael@0: { michael@0: if (iid == __uuidof(IDWriteFontFileLoader)) { michael@0: *ppObject = static_cast(this); michael@0: return S_OK; michael@0: } else if (iid == __uuidof(IUnknown)) { michael@0: *ppObject = static_cast(this); michael@0: return S_OK; michael@0: } else { michael@0: return E_NOINTERFACE; michael@0: } michael@0: } michael@0: michael@0: IFACEMETHOD_(ULONG, AddRef)() michael@0: { michael@0: return 1; michael@0: } michael@0: michael@0: IFACEMETHOD_(ULONG, Release)() michael@0: { michael@0: return 1; michael@0: } michael@0: michael@0: // IDWriteFontFileLoader methods michael@0: /** michael@0: * Important! Note the key here -has- to be a pointer to an michael@0: * FallibleTArray. michael@0: */ michael@0: virtual HRESULT STDMETHODCALLTYPE michael@0: CreateStreamFromKey(void const* fontFileReferenceKey, michael@0: UINT32 fontFileReferenceKeySize, michael@0: OUT IDWriteFontFileStream** fontFileStream); michael@0: michael@0: /** michael@0: * Gets the singleton loader instance. Note that when using this font michael@0: * loader, the key must be a pointer to an FallibleTArray. This michael@0: * array will be empty when the function returns. michael@0: */ michael@0: static IDWriteFontFileLoader* Instance() michael@0: { michael@0: if (!mInstance) { michael@0: mInstance = new gfxDWriteFontFileLoader(); michael@0: gfxWindowsPlatform::GetPlatform()->GetDWriteFactory()-> michael@0: RegisterFontFileLoader(mInstance); michael@0: } michael@0: return mInstance; michael@0: } michael@0: michael@0: private: michael@0: static IDWriteFontFileLoader* mInstance; michael@0: }; michael@0: michael@0: class gfxDWriteFontFileStream MOZ_FINAL : public IDWriteFontFileStream michael@0: { michael@0: public: michael@0: /** michael@0: * Used by the FontFileLoader to create a new font stream, michael@0: * this font stream is created from data in memory. The memory michael@0: * passed may be released after object creation, it will be michael@0: * copied internally. michael@0: * michael@0: * @param aData Font data michael@0: */ michael@0: gfxDWriteFontFileStream(FallibleTArray *aData); michael@0: ~gfxDWriteFontFileStream(); michael@0: michael@0: // IUnknown interface michael@0: IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) michael@0: { michael@0: if (iid == __uuidof(IDWriteFontFileStream)) { michael@0: *ppObject = static_cast(this); michael@0: return S_OK; michael@0: } else if (iid == __uuidof(IUnknown)) { michael@0: *ppObject = static_cast(this); michael@0: return S_OK; michael@0: } else { michael@0: return E_NOINTERFACE; michael@0: } michael@0: } michael@0: michael@0: IFACEMETHOD_(ULONG, AddRef)() michael@0: { michael@0: NS_PRECONDITION(int32_t(mRefCnt) >= 0, "illegal refcnt"); michael@0: ++mRefCnt; michael@0: return mRefCnt; michael@0: } michael@0: michael@0: IFACEMETHOD_(ULONG, Release)() michael@0: { michael@0: NS_PRECONDITION(0 != mRefCnt, "dup release"); michael@0: --mRefCnt; michael@0: if (mRefCnt == 0) { michael@0: delete this; michael@0: return 0; michael@0: } michael@0: return mRefCnt; michael@0: } michael@0: michael@0: // IDWriteFontFileStream methods michael@0: virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart, michael@0: UINT64 fileOffset, michael@0: UINT64 fragmentSize, michael@0: OUT void** fragmentContext); michael@0: michael@0: virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext); michael@0: michael@0: virtual HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64* fileSize); michael@0: michael@0: virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime); michael@0: michael@0: private: michael@0: FallibleTArray mData; michael@0: nsAutoRefCnt mRefCnt; michael@0: }; michael@0: michael@0: #endif /* GFX_DWRITECOMMON_H */