2013-03-25 15:36:12 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Birunthan Mohanathas
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TextFormatD2D.h"
|
|
|
|
#include "CanvasD2D.h"
|
2013-04-12 14:31:12 +00:00
|
|
|
#include "Util/DWriteHelpers.h"
|
2013-03-25 15:36:12 +00:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2013-08-10 10:54:53 +00:00
|
|
|
TextFormatD2D::TextFormatD2D() :
|
|
|
|
m_ExtraHeight(),
|
|
|
|
m_LineGap()
|
2013-03-25 15:36:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextFormatD2D::~TextFormatD2D()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatD2D::Dispose()
|
|
|
|
{
|
2013-05-01 11:10:31 +00:00
|
|
|
m_TextFormat.Reset();
|
|
|
|
m_TextLayout.Reset();
|
|
|
|
m_InlineEllipsis.Reset();
|
2013-03-25 15:36:12 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 10:54:53 +00:00
|
|
|
void TextFormatD2D::CreateLayout(
|
|
|
|
const WCHAR* str, UINT strLen, float maxW, float maxH, bool gdiEmulation)
|
2013-03-28 13:51:12 +00:00
|
|
|
{
|
|
|
|
bool strChanged = false;
|
|
|
|
if (strLen != m_LastString.length() ||
|
|
|
|
memcmp(str, m_LastString.c_str(), (strLen + 1) * sizeof(WCHAR)) != 0)
|
|
|
|
{
|
|
|
|
strChanged = true;
|
|
|
|
m_LastString.assign(str, strLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_TextLayout && !strChanged)
|
|
|
|
{
|
|
|
|
if (maxW != m_TextLayout->GetMaxWidth())
|
|
|
|
{
|
|
|
|
m_TextLayout->SetMaxWidth(maxW);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxH != m_TextLayout->GetMaxHeight())
|
|
|
|
{
|
2013-08-10 09:03:40 +00:00
|
|
|
m_TextLayout->SetMaxHeight(maxH);
|
2013-03-28 13:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-01 11:10:31 +00:00
|
|
|
CanvasD2D::c_DWFactory->CreateTextLayout(
|
|
|
|
str, strLen, m_TextFormat.Get(), maxW, maxH, m_TextLayout.ReleaseAndGetAddressOf());
|
2013-08-10 10:54:53 +00:00
|
|
|
|
|
|
|
if (gdiEmulation && m_TextLayout)
|
|
|
|
{
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteTextLayout1> textLayout1;
|
|
|
|
m_TextLayout.As(&textLayout1);
|
|
|
|
|
|
|
|
const float xOffset = m_TextFormat->GetFontSize() / 6.0f;
|
|
|
|
const float emOffset = xOffset / 24.0f;
|
|
|
|
const DWRITE_TEXT_RANGE range = {0, strLen};
|
|
|
|
textLayout1->SetCharacterSpacing(emOffset, emOffset, 0.0f, range);
|
|
|
|
}
|
2013-03-28 13:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-09 17:35:49 +00:00
|
|
|
void TextFormatD2D::SetProperties(
|
|
|
|
const WCHAR* fontFamily, int size, bool bold, bool italic,
|
|
|
|
const FontCollection* fontCollection)
|
2013-03-25 15:36:12 +00:00
|
|
|
{
|
2013-04-12 16:41:47 +00:00
|
|
|
auto fontCollectionD2D = (FontCollectionD2D*)fontCollection;
|
|
|
|
|
2013-03-25 15:36:12 +00:00
|
|
|
Dispose();
|
|
|
|
|
2013-04-12 16:41:47 +00:00
|
|
|
WCHAR dwriteFamilyName[LF_FACESIZE];
|
|
|
|
DWRITE_FONT_WEIGHT dwriteFontWeight =
|
|
|
|
bold ? DWRITE_FONT_WEIGHT_BOLD : DWRITE_FONT_WEIGHT_REGULAR;
|
|
|
|
DWRITE_FONT_STYLE dwriteFontStyle =
|
|
|
|
italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
|
|
|
|
DWRITE_FONT_STRETCH dwriteFontStretch = DWRITE_FONT_STRETCH_NORMAL;
|
2013-08-10 10:54:53 +00:00
|
|
|
const float dwriteFontSize = size * (4.0f / 3.0f);
|
2013-04-12 16:41:47 +00:00
|
|
|
|
2013-04-07 11:30:50 +00:00
|
|
|
// |fontFamily| uses the GDI/GDI+ font naming convention so try to create DirectWrite font
|
|
|
|
// using the GDI family name and then create a text format using the DirectWrite family name
|
|
|
|
// obtained from it.
|
2013-04-12 16:18:56 +00:00
|
|
|
HRESULT hr = Util::GetDWritePropertiesFromGDIProperties(
|
2013-05-01 11:10:31 +00:00
|
|
|
CanvasD2D::c_DWFactory.Get(), fontFamily, bold, italic, dwriteFontWeight, dwriteFontStyle,
|
2013-04-12 16:18:56 +00:00
|
|
|
dwriteFontStretch, dwriteFamilyName, _countof(dwriteFamilyName));
|
|
|
|
if (SUCCEEDED(hr))
|
2013-04-07 11:30:50 +00:00
|
|
|
{
|
2013-04-08 15:24:09 +00:00
|
|
|
hr = CanvasD2D::c_DWFactory->CreateTextFormat(
|
2013-04-12 16:18:56 +00:00
|
|
|
dwriteFamilyName,
|
2013-04-08 15:24:09 +00:00
|
|
|
nullptr,
|
2013-04-12 16:18:56 +00:00
|
|
|
dwriteFontWeight,
|
|
|
|
dwriteFontStyle,
|
|
|
|
dwriteFontStretch,
|
2013-08-10 10:54:53 +00:00
|
|
|
dwriteFontSize,
|
2013-04-08 15:24:09 +00:00
|
|
|
L"",
|
|
|
|
&m_TextFormat);
|
2013-04-07 11:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2013-04-12 15:12:45 +00:00
|
|
|
IDWriteFontCollection* dwriteFontCollection = nullptr;
|
|
|
|
|
|
|
|
// If |fontFamily| is not in the system collection, use the font collection from
|
|
|
|
// |fontCollectionD2D| if possible.
|
2013-05-01 11:10:31 +00:00
|
|
|
if (!Util::IsFamilyInSystemFontCollection(CanvasD2D::c_DWFactory.Get(), fontFamily) &&
|
2013-04-12 15:12:45 +00:00
|
|
|
(fontCollectionD2D && fontCollectionD2D->InitializeCollection()))
|
|
|
|
{
|
2013-04-12 16:41:47 +00:00
|
|
|
IDWriteFont* dwriteFont = Util::FindDWriteFontInFontCollectionByGDIFamilyName(
|
|
|
|
fontCollectionD2D->m_Collection, fontFamily);
|
|
|
|
if (dwriteFont)
|
|
|
|
{
|
|
|
|
hr = Util::GetFamilyNameFromDWriteFont(
|
|
|
|
dwriteFont, dwriteFamilyName, _countof(dwriteFamilyName));
|
|
|
|
{
|
|
|
|
fontFamily = dwriteFamilyName;
|
|
|
|
Util::GetPropertiesFromDWriteFont(
|
|
|
|
dwriteFont, bold, italic, &dwriteFontWeight, &dwriteFontStyle,
|
|
|
|
&dwriteFontStretch);
|
|
|
|
}
|
|
|
|
|
|
|
|
dwriteFont->Release();
|
|
|
|
}
|
|
|
|
|
2013-04-12 15:12:45 +00:00
|
|
|
dwriteFontCollection = fontCollectionD2D->m_Collection;
|
|
|
|
}
|
|
|
|
|
2013-04-07 11:30:50 +00:00
|
|
|
// Fallback in case above fails.
|
2013-04-07 11:32:41 +00:00
|
|
|
hr = CanvasD2D::c_DWFactory->CreateTextFormat(
|
2013-04-07 11:30:50 +00:00
|
|
|
fontFamily,
|
2013-04-12 15:12:45 +00:00
|
|
|
dwriteFontCollection,
|
2013-04-12 16:41:47 +00:00
|
|
|
dwriteFontWeight,
|
|
|
|
dwriteFontStyle,
|
|
|
|
dwriteFontStretch,
|
2013-08-10 10:54:53 +00:00
|
|
|
dwriteFontSize,
|
2013-04-07 11:30:50 +00:00
|
|
|
L"",
|
|
|
|
&m_TextFormat);
|
|
|
|
}
|
2013-03-25 15:36:12 +00:00
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
SetHorizontalAlignment(GetHorizontalAlignment());
|
|
|
|
SetVerticalAlignment(GetVerticalAlignment());
|
2013-08-10 10:54:53 +00:00
|
|
|
|
|
|
|
// TODO: Clean this up and check for errors.
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteFontCollection> collection;
|
|
|
|
m_TextFormat->GetFontCollection(collection.GetAddressOf());
|
|
|
|
|
|
|
|
UINT32 familyNameIndex;
|
|
|
|
BOOL exists;
|
|
|
|
collection->FindFamilyName(dwriteFamilyName, &familyNameIndex, &exists);
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteFontFamily> fontFamily;
|
|
|
|
collection->GetFontFamily(familyNameIndex, fontFamily.GetAddressOf());
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteFont> font;
|
|
|
|
fontFamily->GetFirstMatchingFont(
|
|
|
|
m_TextFormat->GetFontWeight(),
|
|
|
|
m_TextFormat->GetFontStretch(),
|
|
|
|
m_TextFormat->GetFontStyle(),
|
|
|
|
font.GetAddressOf());
|
|
|
|
|
|
|
|
DWRITE_FONT_METRICS fmetrics;
|
|
|
|
font->GetMetrics(&fmetrics);
|
|
|
|
|
|
|
|
// GDI+ compatibility: GDI+ adds extra padding below the string when |m_AccurateText| is
|
|
|
|
// |false|. The bottom padding seems to be based on the font metrics so we can calculate it
|
|
|
|
// once and keep using it regardless of the actual string. In some cases, GDI+ also adds
|
|
|
|
// the line gap to the overall height so we will store it as well.
|
|
|
|
const float pixelsPerDesignUnit = dwriteFontSize / (float)fmetrics.designUnitsPerEm;
|
|
|
|
m_ExtraHeight =
|
|
|
|
(((float)fmetrics.designUnitsPerEm / 8.0f) - fmetrics.lineGap) * pixelsPerDesignUnit;
|
|
|
|
m_LineGap = fmetrics.lineGap * pixelsPerDesignUnit;
|
2013-03-25 15:36:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 10:54:53 +00:00
|
|
|
DWRITE_TEXT_METRICS TextFormatD2D::GetMetrics(
|
|
|
|
const WCHAR* str, UINT strLen, bool gdiEmulation, float maxWidth)
|
|
|
|
{
|
|
|
|
// GDI+ compatibility: If the last character is a newline, GDI+ measurements seem to ignore it.
|
|
|
|
bool strippedLastNewLine = false;
|
|
|
|
if (str[strLen - 1] == L'\n')
|
|
|
|
{
|
|
|
|
strippedLastNewLine = true;
|
|
|
|
--strLen;
|
|
|
|
|
|
|
|
if (str[strLen - 1] == L'\r')
|
|
|
|
{
|
|
|
|
--strLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWRITE_TEXT_METRICS metrics = {0};
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteTextLayout> textLayout;
|
|
|
|
HRESULT hr = CanvasD2D::c_DWFactory->CreateTextLayout(
|
|
|
|
str,
|
|
|
|
strLen,
|
|
|
|
m_TextFormat.Get(),
|
|
|
|
maxWidth,
|
|
|
|
10000,
|
|
|
|
textLayout.GetAddressOf());
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
const float xOffset = m_TextFormat->GetFontSize() / 6.0f;
|
|
|
|
if (gdiEmulation)
|
|
|
|
{
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteTextLayout1> textLayout1;
|
|
|
|
textLayout.As(&textLayout1);
|
|
|
|
|
|
|
|
const float emOffset = xOffset / 24.0f;
|
|
|
|
const DWRITE_TEXT_RANGE range = {0, strLen};
|
|
|
|
textLayout1->SetCharacterSpacing(emOffset, emOffset, 0.0f, range);
|
|
|
|
}
|
|
|
|
|
|
|
|
textLayout->GetMetrics(&metrics);
|
|
|
|
if (metrics.width > 0.0f)
|
|
|
|
{
|
|
|
|
if (gdiEmulation)
|
|
|
|
{
|
|
|
|
metrics.width += xOffset * 2;
|
|
|
|
metrics.height += m_ExtraHeight;
|
|
|
|
|
|
|
|
// GDI+ compatibility: If the string contains a newline (even if it is the
|
|
|
|
// stripped last character), GDI+ adds the line gap to the overall height.
|
|
|
|
if (strippedLastNewLine || wmemchr(str, L'\n', strLen) != nullptr)
|
|
|
|
{
|
|
|
|
metrics.height += m_LineGap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// GDI+ compatibility: Get rid of the height that DirectWrite assigns to zero-width
|
|
|
|
// strings.
|
|
|
|
metrics.height = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:36:12 +00:00
|
|
|
void TextFormatD2D::SetTrimming(bool trim)
|
|
|
|
{
|
|
|
|
IDWriteInlineObject* inlineObject = nullptr;
|
|
|
|
DWRITE_TRIMMING trimming = {};
|
|
|
|
DWRITE_WORD_WRAPPING wordWrapping = DWRITE_WORD_WRAPPING_NO_WRAP;
|
|
|
|
if (trim)
|
|
|
|
{
|
2013-03-28 13:52:17 +00:00
|
|
|
if (!m_InlineEllipsis)
|
|
|
|
{
|
2013-05-01 11:10:31 +00:00
|
|
|
CanvasD2D::c_DWFactory->CreateEllipsisTrimmingSign(
|
|
|
|
m_TextFormat.Get(), m_InlineEllipsis.GetAddressOf());
|
2013-03-28 13:52:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 11:10:31 +00:00
|
|
|
inlineObject = m_InlineEllipsis.Get();
|
2013-03-25 15:36:12 +00:00
|
|
|
trimming.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER;
|
|
|
|
wordWrapping = DWRITE_WORD_WRAPPING_WRAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_TextFormat->SetTrimming(&trimming, inlineObject);
|
|
|
|
m_TextFormat->SetWordWrapping(wordWrapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatD2D::SetHorizontalAlignment(HorizontalAlignment alignment)
|
|
|
|
{
|
|
|
|
__super::SetHorizontalAlignment(alignment);
|
|
|
|
|
|
|
|
if (m_TextFormat)
|
|
|
|
{
|
|
|
|
m_TextFormat->SetTextAlignment(
|
|
|
|
(alignment == HorizontalAlignment::Left) ? DWRITE_TEXT_ALIGNMENT_LEADING :
|
|
|
|
(alignment == HorizontalAlignment::Center) ? DWRITE_TEXT_ALIGNMENT_CENTER :
|
|
|
|
DWRITE_TEXT_ALIGNMENT_TRAILING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatD2D::SetVerticalAlignment(VerticalAlignment alignment)
|
|
|
|
{
|
|
|
|
__super::SetVerticalAlignment(alignment);
|
|
|
|
|
|
|
|
if (m_TextFormat)
|
|
|
|
{
|
|
|
|
m_TextFormat->SetParagraphAlignment(
|
|
|
|
(alignment == VerticalAlignment::Top) ? DWRITE_PARAGRAPH_ALIGNMENT_NEAR :
|
|
|
|
(alignment == VerticalAlignment::Center) ? DWRITE_PARAGRAPH_ALIGNMENT_CENTER :
|
|
|
|
DWRITE_PARAGRAPH_ALIGNMENT_FAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 21:07:51 +00:00
|
|
|
} // namespace Gfx
|