Gfx: Add FontCollection implementation

The D2D implementation is a stub for now.
This commit is contained in:
Birunthan Mohanathas
2013-04-09 20:35:49 +03:00
parent 4d22c5ac61
commit 0b3a4d86c4
19 changed files with 353 additions and 41 deletions

View File

@ -19,6 +19,7 @@
#ifndef RM_GFX_CANVAS_H_
#define RM_GFX_CANVAS_H_
#include "FontCollection.h"
#include "TextFormat.h"
#include <Windows.h>
#include <GdiPlus.h>
@ -53,6 +54,7 @@ public:
virtual HDC GetDC() = 0;
virtual void ReleaseDC(HDC dc) = 0;
virtual FontCollection* CreateFontCollection() = 0;
virtual TextFormat* CreateTextFormat() = 0;
virtual bool IsTransparentPixel(int x, int y) = 0;

View File

@ -20,6 +20,7 @@
#define RM_GFX_CANVASD2D_H_
#include "Canvas.h"
#include "FontCollectionD2D.h"
#include "TextFormatD2D.h"
#include "WICBitmapDIB.h"
#include <string>
@ -48,7 +49,8 @@ public:
virtual HDC GetDC() override;
virtual void ReleaseDC(HDC dc) override;
virtual FontCollection* CreateFontCollection() override { return new FontCollectionD2D(); }
virtual TextFormat* CreateTextFormat() override { return new TextFormatD2D(); }
virtual bool IsTransparentPixel(int x, int y) override;

View File

@ -20,6 +20,7 @@
#define RM_GFX_CANVASGDIP_H_
#include "Canvas.h"
#include "FontCollectionGDIP.h"
#include "TextFormatGDIP.h"
#include <string>
#include <GdiPlus.h>
@ -44,6 +45,7 @@ public:
virtual HDC GetDC() override;
virtual void ReleaseDC(HDC dc) override;
virtual FontCollection* CreateFontCollection() override { return new FontCollectionGDIP(); }
virtual TextFormat* CreateTextFormat() override { return new TextFormatGDIP(); }
virtual bool IsTransparentPixel(int x, int y) override;

View File

@ -0,0 +1,31 @@
/*
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 "FontCollection.h"
namespace Gfx {
FontCollection::FontCollection()
{
}
FontCollection::~FontCollection()
{
}
} // namespace Gfx

View File

@ -0,0 +1,44 @@
/*
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.
*/
#ifndef RM_GFX_FONTCOLLECTION_H_
#define RM_GFX_FONTCOLLECTION_H_
#include <Windows.h>
namespace Gfx {
// Interface for a collection of fonts that may or may not be installed on the system.
class __declspec(novtable) FontCollection
{
public:
virtual ~FontCollection();
// Adds a file to the collection. Returns true if the file was successfully added.
virtual bool AddFile(const WCHAR* file) = 0;
protected:
FontCollection();
private:
FontCollection(const FontCollection& other) {}
};
} // namespace Gfx
#endif

View File

@ -0,0 +1,38 @@
/*
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 "FontCollectionD2D.h"
#include <GdiPlus.h>
namespace Gfx {
FontCollectionD2D::FontCollectionD2D() : FontCollection()
{
}
FontCollectionD2D::~FontCollectionD2D()
{
}
bool FontCollectionD2D::AddFile(const WCHAR* file)
{
// FIXME.
return true;
}
} // namespace Gfx

View File

@ -0,0 +1,48 @@
/*
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.
*/
#ifndef RM_GFX_FONTCOLLECTIOND2D_H_
#define RM_GFX_FONTCOLLECTIOND2D_H_
#include "FontCollection.h"
namespace Gfx {
// Wraps the DirectWrite IDWriteFontCollection for use with CanvasD2D.
class FontCollectionD2D final : public FontCollection
{
public:
virtual ~FontCollectionD2D();
virtual bool AddFile(const WCHAR* file) override;
protected:
FontCollectionD2D();
private:
friend class CanvasD2D;
friend class TextFormatD2D;
FontCollectionD2D(const FontCollectionD2D& other) {}
void Dispose();
};
} // namespace Gfx
#endif

View File

@ -0,0 +1,54 @@
/*
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 "FontCollectionGDIP.h"
#include <GdiPlus.h>
namespace Gfx {
FontCollectionGDIP::FontCollectionGDIP() : FontCollection(),
m_PrivateCollection()
{
}
FontCollectionGDIP::~FontCollectionGDIP()
{
Dispose();
}
void FontCollectionGDIP::Dispose()
{
if (m_PrivateCollection)
{
delete m_PrivateCollection;
m_PrivateCollection = nullptr;
}
}
bool FontCollectionGDIP::AddFile(const WCHAR* file)
{
if (!m_PrivateCollection)
{
m_PrivateCollection = new Gdiplus::PrivateFontCollection();
}
const Gdiplus::Status status = m_PrivateCollection->AddFontFile(file);
return status == Gdiplus::Ok;
}
} // namespace Gfx

View File

@ -0,0 +1,54 @@
/*
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.
*/
#ifndef RM_GFX_FONTCOLLECTIONGDIP_H_
#define RM_GFX_FONTCOLLECTIONGDIP_H_
#include "FontCollection.h"
namespace Gdiplus {
class PrivateFontCollection;
}
namespace Gfx {
// Wraps the GDI+ PrivateFontCollection for use with CanvasGDIP.
class FontCollectionGDIP final : public FontCollection
{
public:
virtual ~FontCollectionGDIP();
virtual bool AddFile(const WCHAR* file) override;
protected:
FontCollectionGDIP();
private:
friend class CanvasGDIP;
friend class TextFormatGDIP;
FontCollectionGDIP(const FontCollectionGDIP& other) {}
void Dispose();
Gdiplus::PrivateFontCollection* m_PrivateCollection;
};
} // namespace Gfx
#endif

View File

@ -20,10 +20,11 @@
#define RM_GFX_TEXTFORMAT_H_
#include <Windows.h>
#include <GdiPlus.h>
namespace Gfx {
class FontCollection;
enum class HorizontalAlignment : BYTE
{
Left,
@ -44,7 +45,10 @@ public:
virtual ~TextFormat();
virtual bool IsInitialized() const = 0;
virtual void SetProperties(const WCHAR* fontFamily, int size, bool bold, bool italic, Gdiplus::PrivateFontCollection* fontCollection) = 0;
virtual void SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection) = 0;
virtual void SetTrimming(bool trim) = 0;

View File

@ -88,7 +88,9 @@ void TextFormatD2D::CreateLayout(const WCHAR* str, UINT strLen, float maxW, floa
}
}
void TextFormatD2D::SetProperties(const WCHAR* fontFamily, int size, bool bold, bool italic, Gdiplus::PrivateFontCollection* fontCollection)
void TextFormatD2D::SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection)
{
Dispose();

View File

@ -32,7 +32,10 @@ public:
virtual ~TextFormatD2D();
virtual bool IsInitialized() const override { return m_TextFormat != nullptr; }
virtual void SetProperties(const WCHAR* fontFamily, int size, bool bold, bool italic, Gdiplus::PrivateFontCollection* fontCollection) override;
virtual void SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection) override;
virtual void SetTrimming(bool trim) override;
virtual void SetHorizontalAlignment(HorizontalAlignment alignment) override;

View File

@ -17,6 +17,7 @@
*/
#include "TextFormatGDIP.h"
#include "FontCollectionGDIP.h"
namespace Gfx {
@ -40,8 +41,12 @@ void TextFormatGDIP::Dispose()
m_Font = nullptr;
}
void TextFormatGDIP::SetProperties(const WCHAR* fontFamily, int size, bool bold, bool italic, Gdiplus::PrivateFontCollection* fontCollection)
void TextFormatGDIP::SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection)
{
auto fontCollectionGDIP = (FontCollectionGDIP*)fontCollection;
Dispose();
m_FontFamily = new Gdiplus::FontFamily(fontFamily);
@ -51,9 +56,9 @@ void TextFormatGDIP::SetProperties(const WCHAR* fontFamily, int size, bool bold,
m_FontFamily = nullptr;
// Not found in system collection so try the private collection.
if (fontCollection)
if (fontCollectionGDIP && fontCollectionGDIP->m_PrivateCollection)
{
m_FontFamily = new Gdiplus::FontFamily(fontFamily, fontCollection);
m_FontFamily = new Gdiplus::FontFamily(fontFamily, fontCollectionGDIP->m_PrivateCollection);
if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
{
delete m_FontFamily;

View File

@ -31,7 +31,10 @@ public:
virtual ~TextFormatGDIP();
virtual bool IsInitialized() const override { return m_Font != nullptr; }
virtual void SetProperties(const WCHAR* fontFamily, int size, bool bold, bool italic, Gdiplus::PrivateFontCollection* fontCollection) override;
virtual void SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection) override;
virtual void SetTrimming(bool trim) override;
virtual void SetHorizontalAlignment(HorizontalAlignment alignment) override;