Magic_Game/src/kiwano/render/DirectX/TextDrawingEffect.cpp

204 lines
5.8 KiB
C++
Raw Normal View History

2023-09-10 12:11:51 +08:00
// Copyright (c) 2023 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <tuple>
2023-09-10 12:11:51 +08:00
#include <kiwano/render/DirectX/TextDrawingEffect.h>
namespace kiwano
{
2023-09-19 20:25:06 +08:00
namespace graphics
{
namespace directx
{
2023-09-10 12:11:51 +08:00
class TextDrawingEffect : public ITextDrawingEffect
{
public:
TextDrawingEffect();
STDMETHOD(CreateDeviceResources)(_In_ ID2D1Factory* pFactory);
STDMETHOD(CreateOutlineGeomerty)
(_Out_ ID2D1Geometry** ppOutlineGeo, _In_ DWRITE_GLYPH_RUN const* glyphRun, float fOriginX, float fOriginY);
// IUnknown methods
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
virtual ULONG STDMETHODCALLTYPE AddRef();
virtual ULONG STDMETHODCALLTYPE Release();
private:
2023-09-19 20:25:06 +08:00
unsigned long cRefCount_;
ComPtr<ID2D1Factory> pFactory_;
2023-09-10 12:11:51 +08:00
// Outline geometry cache
Map<std::tuple<const DWRITE_GLYPH_RUN*, float, float>, ComPtr<ID2D1Geometry>> outlineCache_;
2023-09-10 12:11:51 +08:00
};
HRESULT ITextDrawingEffect::Create(_Out_ ITextDrawingEffect** ppTextDrawingEffect, _In_ ID2D1Factory* pFactory)
{
HRESULT hr = E_FAIL;
if (ppTextDrawingEffect)
{
TextDrawingEffect* pTextDrawingEffect = new (std::nothrow) TextDrawingEffect;
if (pTextDrawingEffect)
{
hr = pTextDrawingEffect->CreateDeviceResources(pFactory);
if (SUCCEEDED(hr))
{
pTextDrawingEffect->AddRef();
DX::SafeRelease(*ppTextDrawingEffect);
(*ppTextDrawingEffect) = pTextDrawingEffect;
return S_OK;
}
else
{
delete pTextDrawingEffect;
pTextDrawingEffect = NULL;
}
}
}
return hr;
}
TextDrawingEffect::TextDrawingEffect()
: cRefCount_(0)
, outlineCache_{}
2023-09-10 12:11:51 +08:00
{
}
STDMETHODIMP TextDrawingEffect::CreateDeviceResources(_In_ ID2D1Factory* pFactory)
{
pFactory_ = pFactory;
return S_OK;
}
STDMETHODIMP TextDrawingEffect::CreateOutlineGeomerty(_Out_ ID2D1Geometry** ppOutlineGeo,
_In_ DWRITE_GLYPH_RUN const* glyphRun, float fOriginX,
float fOriginY)
{
auto cache = outlineCache_.find(std::make_tuple(glyphRun, fOriginX, fOriginY));
if (cache != outlineCache_.end())
2023-09-10 12:11:51 +08:00
{
auto& pOutlineGeo = cache->second;
if (pOutlineGeo)
{
// Use cached geometry
pOutlineGeo->AddRef();
DX::SafeRelease(*ppOutlineGeo);
(*ppOutlineGeo) = pOutlineGeo.Get();
return S_OK;
}
2023-09-10 12:11:51 +08:00
}
HRESULT hr = S_OK;
2023-09-10 12:11:51 +08:00
ComPtr<ID2D1Geometry> pOutlineGeo;
2023-09-10 12:11:51 +08:00
if (SUCCEEDED(hr))
{
ComPtr<ID2D1PathGeometry> pPathGeometry;
2023-09-10 12:11:51 +08:00
hr = pFactory_->CreatePathGeometry(&pPathGeometry);
if (SUCCEEDED(hr))
{
ComPtr<ID2D1GeometrySink> pSink;
hr = pPathGeometry->Open(&pSink);
2023-09-10 12:11:51 +08:00
if (SUCCEEDED(hr))
{
hr = glyphRun->fontFace->GetGlyphRunOutline(
glyphRun->fontEmSize, glyphRun->glyphIndices, glyphRun->glyphAdvances, glyphRun->glyphOffsets,
glyphRun->glyphCount, glyphRun->isSideways, glyphRun->bidiLevel % 2, pSink.Get());
}
2023-09-10 12:11:51 +08:00
if (SUCCEEDED(hr))
{
hr = pSink->Close();
}
2023-09-10 12:11:51 +08:00
if (SUCCEEDED(hr))
{
const auto matrix = D2D1::Matrix3x2F(1.0f, 0.0f, 0.0f, 1.0f, fOriginX, fOriginY);
ComPtr<ID2D1TransformedGeometry> pTransformedGeometry;
2023-09-10 12:11:51 +08:00
hr = pFactory_->CreateTransformedGeometry(pPathGeometry.Get(), &matrix, &pTransformedGeometry);
if (SUCCEEDED(hr))
{
pOutlineGeo = pTransformedGeometry;
}
2023-09-10 12:11:51 +08:00
}
}
}
if (SUCCEEDED(hr))
{
pOutlineGeo->AddRef();
2023-09-10 12:11:51 +08:00
DX::SafeRelease(*ppOutlineGeo);
(*ppOutlineGeo) = pOutlineGeo.Get();
outlineCache_.insert(std::make_pair(std::make_tuple(glyphRun, fOriginX, fOriginY), pOutlineGeo));
2023-09-10 12:11:51 +08:00
}
return hr;
}
STDMETHODIMP_(unsigned long) TextDrawingEffect::AddRef()
{
return InterlockedIncrement(&cRefCount_);
}
STDMETHODIMP_(unsigned long) TextDrawingEffect::Release()
{
unsigned long newCount = InterlockedDecrement(&cRefCount_);
if (newCount == 0)
{
delete this;
return 0;
}
return newCount;
}
STDMETHODIMP TextDrawingEffect::QueryInterface(REFIID riid, void** ppvObject)
{
if (__uuidof(ITextDrawingEffect) == riid)
{
*ppvObject = this;
}
else if (__uuidof(IUnknown) == riid)
{
*ppvObject = this;
}
else
{
*ppvObject = NULL;
return E_FAIL;
}
AddRef();
return S_OK;
}
2023-09-19 20:25:06 +08:00
} // namespace directx
} // namespace graphics
2023-09-10 12:11:51 +08:00
} // namespace kiwano