Cogs.Core
FontManager.h
1#pragma once
2
3#include "ResourceManager.h"
4
5#include <unordered_map>
6
7#include "Font.h"
8
9#include "IFontLoader.h"
10
12
13namespace std
14{
15 template<>
16 struct hash<Cogs::Core::FontDefinition>
17 {
18 size_t operator()(const Cogs::Core::FontDefinition & definition) const
19 {
20 size_t val = std::hash<std::string>()(definition.fontFamily);
21 val *= std::hash<float>()(definition.fontSize);
22
23 return val;
24 }
25 };
26}
27
29
30namespace Cogs
31{
32 class IGraphicsDevice;
33
34 namespace Core
35 {
39 class FontManager : public ResourceManager<Font, FontLoadInfo>
40 {
41 public:
43 explicit FontManager(Context * context) : ResourceManager(context) {}
44
46 ~FontManager() override;
47
49 void initialize() override;
50
52 void clear() override;
53
65 COGSCORE_DLL_API FontHandle loadFont(const std::string & name, float size, ResourceId resourceId, ResourceLoadFlags flags = ResourceLoadFlags::None);
66
77 FontHandle loadFont(const FontDefinition & definition, const ResourceId id, ResourceLoadFlags flags = ResourceLoadFlags::None);
78
80 void getBakedCharacter(const Font * font, int characterIndex, float & x, float & y, Font::BakedCharacter & bakedCharacter) const;
81
83 void handleLoad(FontLoadInfo * loadInfo) override;
84
86 ActivationResult handleActivation(FontHandle handle, Font * font) override;
87
88 void handleDeletion(Font* font) override;
89
91 void handleFailedLoad(const FontLoadInfo * loadInfo) override;
92
94 virtual bool shouldMergeBySource() const override { return false; }
95
96 public:
98 ResourceId DefaultMonospaceFont = NoResourceId;
99
101 ResourceId DefaultRegularFont = NoResourceId;
102
104 ResourceId DefaultBoldFont = NoResourceId;
105
108
111
114 std::unordered_map<FontDefinition, FontHandle> sharedFonts;
115 };
116 }
117}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Font manager responsible for loading, processing and lifetime of Font resources.
Definition: FontManager.h:40
ResourceId DefaultMonospaceFont
Id of the default monospace font.
Definition: FontManager.h:98
FontManager(Context *context)
Constructs a FontManager in the given context.
Definition: FontManager.h:43
std::unordered_map< FontDefinition, FontHandle > sharedFonts
Definition: FontManager.h:114
FontHandle boldFont
Handle to the bold font.
Definition: FontManager.h:110
FontHandle regularFont
Handle to the regular font.
Definition: FontManager.h:107
ActivationResult handleActivation(FontHandle handle, Font *font) override
Overridden to activate loaded font resources, triggering loading of the font Texture.
void handleDeletion(Font *font) override
Handler for deletion of resources.
ResourceId DefaultRegularFont
Id of the default font.
Definition: FontManager.h:101
~FontManager() override
Destructs the FontManaer.
Definition: FontManager.cpp:20
void clear() override
Clear the FontManager, releasing all default Font resources.
Definition: FontManager.cpp:44
void getBakedCharacter(const Font *font, int characterIndex, float &x, float &y, Font::BakedCharacter &bakedCharacter) const
Retrieve baked character information for rendering.
void handleLoad(FontLoadInfo *loadInfo) override
Overridden to load Font resources.
void handleFailedLoad(const FontLoadInfo *loadInfo) override
Handle failed Font loads, logging and cleaning up.
virtual bool shouldMergeBySource() const override
Checking unique handle should not be performed using resource path only. Handled in FontManager.
Definition: FontManager.h:94
ResourceId DefaultBoldFont
Id of the default bold font.
Definition: FontManager.h:104
void initialize() override
Initialize the FontManager, creating default Font resources.
Definition: FontManager.cpp:25
COGSCORE_DLL_API FontHandle loadFont(const std::string &name, float size, ResourceId resourceId, ResourceLoadFlags flags=ResourceLoadFlags::None)
Load a Font resource from the file resource with the given name.
Definition: FontManager.cpp:56
The generic resource manager provides a base implementation for specialized resource managers to buil...
ActivationResult
Defines results for resource activation.
Definition: ResourceBase.h:14
ResourceLoadFlags
Flags for describing how to load a resource.
Definition: ResourceFlags.h:16
@ None
No flags specified,.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
STL namespace.
Defines a Font.
Definition: Font.h:91
std::string fontFamily
Font family name, such as "Arial" or "Tahoma".
Definition: Font.h:93
float fontSize
Size of the font in pixels.
Definition: Font.h:96
Defines information used to load a Font resource.
Definition: IFontLoader.h:17
Defines a quad with locations in screen space defined by [x0, y0] and [x1, y1].
Definition: Font.h:48
Font resources are used to render text with a specific font and size.
Definition: Font.h:22
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.