Skip to content

Instantly share code, notes, and snippets.

@k-takata
Last active November 25, 2017 09:48
Show Gist options
  • Save k-takata/60565df2bd746800c62080767adccbec to your computer and use it in GitHub Desktop.
Save k-takata/60565df2bd746800c62080767adccbec to your computer and use it in GitHub Desktop.
diff --git a/src/gui_dwrite.cpp b/src/gui_dwrite.cpp
index 20ac338d7..dcd2bf559 100644
--- a/src/gui_dwrite.cpp
+++ b/src/gui_dwrite.cpp
@@ -222,6 +222,7 @@ struct DWriteContext {
ID2D1Factory *mD2D1Factory;
ID2D1DCRenderTarget *mRT;
+ ID2D1GdiInteropRenderTarget *mGDIRT;
ID2D1SolidColorBrush *mBrush;
IDWriteFactory *mDWriteFactory;
@@ -231,6 +232,7 @@ struct DWriteContext {
IDWriteRenderingParams *mRenderingParams;
HFONT mLastHFont;
+ HFONT mFallbackHFont;
LOGFONTW mFallbackLF;
IDWriteTextFormat *mTextFormat;
DWRITE_FONT_WEIGHT mFontWeight;
@@ -514,12 +516,14 @@ DWriteContext::DWriteContext() :
mDrawing(false),
mD2D1Factory(NULL),
mRT(NULL),
+ mGDIRT(NULL),
mBrush(NULL),
mDWriteFactory(NULL),
mDWriteFactory2(NULL),
mGdiInterop(NULL),
mRenderingParams(NULL),
mLastHFont(NULL),
+ mFallbackHFont(NULL),
mTextFormat(NULL),
mFontWeight(DWRITE_FONT_WEIGHT_NORMAL),
mFontStyle(DWRITE_FONT_STYLE_NORMAL),
@@ -538,13 +542,21 @@ DWriteContext::DWriteContext() :
D2D1_RENDER_TARGET_TYPE_DEFAULT,
{ DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
0, 0,
- D2D1_RENDER_TARGET_USAGE_NONE,
+ /*D2D1_RENDER_TARGET_USAGE_NONE*/ D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE,
D2D1_FEATURE_LEVEL_DEFAULT
};
hr = mD2D1Factory->CreateDCRenderTarget(&props, &mRT);
_RPT2(_CRT_WARN, "CreateDCRenderTarget: hr=%p p=%p\n", hr, mRT);
}
+ if (SUCCEEDED(hr))
+ {
+ hr = mRT->QueryInterface(
+ __uuidof(ID2D1GdiInteropRenderTarget),
+ (void**)&mGDIRT);
+ _RPT2(_CRT_WARN, "QueryInterface: hr=%p p=%p\n", hr, mGDIRT);
+ }
+
if (SUCCEEDED(hr))
{
hr = mRT->CreateSolidColorBrush(
@@ -597,6 +609,7 @@ DWriteContext::~DWriteContext()
SafeRelease(&mDWriteFactory);
SafeRelease(&mDWriteFactory2);
SafeRelease(&mBrush);
+ SafeRelease(&mGDIRT);
SafeRelease(&mRT);
SafeRelease(&mD2D1Factory);
}
@@ -752,12 +765,17 @@ DWriteContext::SetFont(HFONT hFont)
hr = SetFontByLOGFONT(lf);
if (SUCCEEDED(hr))
+ {
mLastHFont = hFont;
+ mFallbackHFont = NULL;
+ }
else
{
- SetFontByLOGFONT(mFallbackLF);
+ //SetFontByLOGFONT(mFallbackLF);
+ //mLastHFont = hFont;
+ //_RPT1(_CRT_WARN, "SetFont use fallback font: hr=%08X\n", hr);
mLastHFont = hFont;
- _RPT1(_CRT_WARN, "SetFont use fallback font: hr=%08X\n", hr);
+ mFallbackHFont = hFont;
}
}
@@ -793,6 +811,23 @@ DWriteContext::DrawText(const WCHAR* text, int len,
{
AssureDrawing();
+ if (mFallbackHFont)
+ {
+ HDC hdc = NULL;
+ HRESULT hr = mGDIRT->GetDC(D2D1_DC_INITIALIZE_MODE_COPY, &hdc);
+
+ if (SUCCEEDED(hr))
+ {
+ HGDIOBJ hOldFont = SelectObject(hdc, (HGDIOBJ)mFallbackHFont);
+ SetTextColor(hdc, color);
+ ExtTextOutW(hdc, x, y,
+ 0/*foptions*/, NULL/*pcliprect*/, text, len, NULL/*unicodepdy*/);
+ SelectObject(hdc, hOldFont);
+ mGDIRT->ReleaseDC(NULL);
+ }
+ return;
+ }
+
HRESULT hr;
IDWriteTextLayout *textLayout = NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment