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 "TextFormatGDIP.h"
|
2013-04-09 17:35:49 +00:00
|
|
|
#include "FontCollectionGDIP.h"
|
2013-03-25 15:36:12 +00:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2013-05-01 11:38:52 +00:00
|
|
|
TextFormatGDIP::TextFormatGDIP()
|
2013-03-25 15:36:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextFormatGDIP::~TextFormatGDIP()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-09 17:35:49 +00:00
|
|
|
void TextFormatGDIP::SetProperties(
|
|
|
|
const WCHAR* fontFamily, int size, bool bold, bool italic,
|
|
|
|
const FontCollection* fontCollection)
|
2013-03-25 15:36:12 +00:00
|
|
|
{
|
2013-04-09 17:35:49 +00:00
|
|
|
auto fontCollectionGDIP = (FontCollectionGDIP*)fontCollection;
|
|
|
|
|
2013-05-01 11:38:52 +00:00
|
|
|
m_Font.reset();
|
2013-03-25 15:36:12 +00:00
|
|
|
|
2013-05-01 11:38:52 +00:00
|
|
|
m_FontFamily.reset(new Gdiplus::FontFamily(fontFamily));
|
2013-03-25 15:36:12 +00:00
|
|
|
if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
|
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_FontFamily.reset();
|
2013-03-28 13:09:46 +00:00
|
|
|
|
|
|
|
// Not found in system collection so try the private collection.
|
2013-04-09 17:35:49 +00:00
|
|
|
if (fontCollectionGDIP && fontCollectionGDIP->m_PrivateCollection)
|
2013-03-28 13:09:46 +00:00
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_FontFamily.reset(new Gdiplus::FontFamily(fontFamily, fontCollectionGDIP->m_PrivateCollection));
|
2013-03-28 13:09:46 +00:00
|
|
|
if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
|
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_FontFamily.reset();
|
2013-03-28 13:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-25 15:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gdiplus::FontStyle style = Gdiplus::FontStyleRegular;
|
|
|
|
if (bold && italic)
|
|
|
|
{
|
|
|
|
style = Gdiplus::FontStyleBoldItalic;
|
|
|
|
}
|
|
|
|
else if (bold)
|
|
|
|
{
|
|
|
|
style = Gdiplus::FontStyleBold;
|
|
|
|
}
|
|
|
|
else if (italic)
|
|
|
|
{
|
|
|
|
style = Gdiplus::FontStyleItalic;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size != 0)
|
|
|
|
{
|
|
|
|
// Adjust the font size with screen DPI.
|
|
|
|
HDC dc = GetDC(0);
|
|
|
|
const int dpi = GetDeviceCaps(dc, LOGPIXELSX);
|
|
|
|
ReleaseDC(0, dc);
|
|
|
|
const Gdiplus::REAL fontSize = (Gdiplus::REAL)size * (96.0f / dpi);
|
|
|
|
|
|
|
|
if (m_FontFamily)
|
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_Font.reset(new Gdiplus::Font(m_FontFamily.get(), fontSize, style));
|
2013-03-25 15:36:12 +00:00
|
|
|
if (m_Font->GetLastStatus() != Gdiplus::Ok)
|
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_Font.reset();
|
2013-03-25 15:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Font)
|
|
|
|
{
|
|
|
|
// Use default font ("Arial" or GenericSansSerif).
|
2013-05-01 11:38:52 +00:00
|
|
|
m_Font.reset(new Gdiplus::Font(L"Arial", fontSize, style));
|
2013-03-25 15:36:12 +00:00
|
|
|
if (m_Font->GetLastStatus() != Gdiplus::Ok)
|
|
|
|
{
|
2013-05-01 11:38:52 +00:00
|
|
|
m_Font.reset();
|
2013-03-25 15:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatGDIP::SetTrimming(bool trim)
|
|
|
|
{
|
|
|
|
if (trim)
|
|
|
|
{
|
|
|
|
m_StringFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisCharacter);
|
|
|
|
m_StringFormat.SetFormatFlags(0x0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_StringFormat.SetTrimming(Gdiplus::StringTrimmingNone);
|
|
|
|
m_StringFormat.SetFormatFlags(Gdiplus::StringFormatFlagsNoClip | Gdiplus::StringFormatFlagsNoWrap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatGDIP::SetHorizontalAlignment(HorizontalAlignment alignment)
|
|
|
|
{
|
|
|
|
static_assert(Gdiplus::StringAlignmentNear == (int)HorizontalAlignment::Left, "Enum mismatch");
|
|
|
|
static_assert(Gdiplus::StringAlignmentCenter == (int)HorizontalAlignment::Center, "Enum mismatch");
|
|
|
|
static_assert(Gdiplus::StringAlignmentFar == (int)HorizontalAlignment::Right, "Enum mismatch");
|
|
|
|
|
|
|
|
__super::SetHorizontalAlignment(alignment);
|
|
|
|
m_StringFormat.SetAlignment((Gdiplus::StringAlignment)alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFormatGDIP::SetVerticalAlignment(VerticalAlignment alignment)
|
|
|
|
{
|
|
|
|
static_assert(Gdiplus::StringAlignmentNear == (int)VerticalAlignment::Top, "Enum mismatch");
|
|
|
|
static_assert(Gdiplus::StringAlignmentCenter == (int)VerticalAlignment::Center, "Enum mismatch");
|
|
|
|
static_assert(Gdiplus::StringAlignmentFar == (int)VerticalAlignment::Bottom, "Enum mismatch");
|
|
|
|
|
|
|
|
__super::SetVerticalAlignment(alignment);
|
|
|
|
m_StringFormat.SetLineAlignment((Gdiplus::StringAlignment)alignment);
|
|
|
|
}
|
|
|
|
|
2013-04-05 21:07:51 +00:00
|
|
|
} // namespace Gfx
|