Gfx: Move DWrite helpers to Util

This commit is contained in:
Birunthan Mohanathas 2013-04-12 17:31:12 +03:00
parent b8bda82226
commit 4f26f777c9
2 changed files with 4 additions and 116 deletions

View File

@ -18,6 +18,7 @@
#include "TextFormatD2D.h"
#include "CanvasD2D.h"
#include "Util/DWriteHelpers.h"
namespace Gfx {
@ -103,9 +104,9 @@ void TextFormatD2D::SetProperties(
DWRITE_FONT_WEIGHT dwFontWeight;
DWRITE_FONT_STYLE dwFontStyle;
DWRITE_FONT_STRETCH dwFontStretch;
if (GetDWPropertiesFromGDIProperties(
fontFamily, bold, italic, dwFontWeight, dwFontStyle, dwFontStretch, dwFamilyName,
_countof(dwFamilyName)))
if (Util::GetDWritePropertiesFromGDIProperties(
CanvasD2D::c_DWFactory, fontFamily, bold, italic, dwFontWeight, dwFontStyle, dwFontStretch,
dwFamilyName, _countof(dwFamilyName)))
{
hr = CanvasD2D::c_DWFactory->CreateTextFormat(
dwFamilyName,
@ -190,101 +191,4 @@ void TextFormatD2D::SetVerticalAlignment(VerticalAlignment alignment)
}
}
bool TextFormatD2D::GetDWPropertiesFromGDIProperties(
const WCHAR* gdiFamilyName, const bool gdiBold, const bool gdiItalic,
DWRITE_FONT_WEIGHT& dwFontWeight, DWRITE_FONT_STYLE& dwFontStyle,
DWRITE_FONT_STRETCH& dwFontStretch, WCHAR* dwFamilyName, UINT dwFamilyNameSize)
{
bool result = false;
IDWriteFont* dwFont = CreateDWFontFromGDIFamilyName(gdiFamilyName);
if (dwFont)
{
if (GetFamilyNameFromDWFont(dwFont, dwFamilyName, dwFamilyNameSize))
{
dwFontWeight = dwFont->GetWeight();
if (gdiBold)
{
if (dwFontWeight == DWRITE_FONT_WEIGHT_NORMAL)
{
dwFontWeight = DWRITE_FONT_WEIGHT_BOLD;
}
else if (dwFontWeight < DWRITE_FONT_WEIGHT_ULTRA_BOLD)
{
// If 'gdiFamilyName' was e.g. 'Segoe UI Light', |dwFontWeight| wil be equal to
// DWRITE_FONT_WEIGHT_LIGHT. If |gdiBold| is true in that case, we need to
// increase the weight a little more for similar results with GDI+.
// TODO: Is +100 enough?
dwFontWeight = (DWRITE_FONT_WEIGHT)(dwFontWeight + 100);
}
}
dwFontStyle = dwFont->GetStyle();
if (gdiItalic && dwFontStyle == DWRITE_FONT_STYLE_NORMAL)
{
dwFontStyle = DWRITE_FONT_STYLE_ITALIC;
}
dwFontStretch = dwFont->GetStretch();
result = true;
}
dwFont->Release();
}
return result;
}
IDWriteFont* TextFormatD2D::CreateDWFontFromGDIFamilyName(const WCHAR* fontFamily)
{
IDWriteGdiInterop* dwGdiInterop;
HRESULT hr = CanvasD2D::c_DWFactory->GetGdiInterop(&dwGdiInterop);
if (SUCCEEDED(hr))
{
LOGFONT lf = {};
wcscpy_s(lf.lfFaceName, fontFamily);
lf.lfHeight = -12;
lf.lfWeight = FW_DONTCARE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY;
lf.lfPitchAndFamily = VARIABLE_PITCH;
IDWriteFont* dwFont;
hr = dwGdiInterop->CreateFontFromLOGFONT(&lf, &dwFont);
if (SUCCEEDED(hr))
{
return dwFont;
}
dwGdiInterop->Release();
}
return nullptr;
}
bool TextFormatD2D::GetFamilyNameFromDWFont(IDWriteFont* font, WCHAR* buffer, const UINT bufferSize)
{
IDWriteFontFamily* dwFontFamily;
HRESULT hr = font->GetFontFamily(&dwFontFamily);
return SUCCEEDED(hr) ? GetFamilyNameFromDWFontFamily(dwFontFamily, buffer, bufferSize) : false;
}
bool TextFormatD2D::GetFamilyNameFromDWFontFamily(IDWriteFontFamily* fontFamily, WCHAR* buffer, const UINT bufferSize)
{
bool result = false;
IDWriteLocalizedStrings* dwFamilyNames;
HRESULT hr = fontFamily->GetFamilyNames(&dwFamilyNames);
if (SUCCEEDED(hr))
{
// TODO: Determine the best index?
hr = dwFamilyNames->GetString(0, buffer, bufferSize);
result = SUCCEEDED(hr);
dwFamilyNames->Release();
}
return result;
}
} // namespace Gfx

View File

@ -55,22 +55,6 @@ private:
// changes.
void CreateLayout(const WCHAR* str, UINT strLen, float maxW, float maxH);
// Maps the GDI family name and italic/bold flags to the DirectWrite family name, weight, style,
// and stretch.
static bool GetDWPropertiesFromGDIProperties(
const WCHAR* gdiFamilyName, const bool gdiBold, const bool gdiItalic,
DWRITE_FONT_WEIGHT& dwFontWeight, DWRITE_FONT_STYLE& dwFontStyle,
DWRITE_FONT_STRETCH& dwFontStretch, WCHAR* dwFamilyName, UINT dwFamilyNameSize);
// Creates a IDWriteFont using the GDI family name instead of the DirectWrite family name. For
// example, 'Segoe UI' and 'Segoe UI Semibold' are separate family names with GDI whereas
// DirectWrite uses the family name 'Segoe UI' for both and differentiates them by the font
// style.
static IDWriteFont* CreateDWFontFromGDIFamilyName(const WCHAR* fontFamily);
static bool GetFamilyNameFromDWFont(IDWriteFont* font, WCHAR* buffer, UINT bufferSize);
static bool GetFamilyNameFromDWFontFamily(IDWriteFontFamily* fontFamily, WCHAR* buffer, UINT bufferSize);
IDWriteTextFormat* m_TextFormat;
IDWriteTextLayout* m_TextLayout;
IDWriteInlineObject* m_InlineEllipsis;