1#include "FontManager.h"
3#include "Foundation/Logging/Logger.h"
5#include "ResourceStore.h"
9#include "TextureManager.h"
11#include "Services/TaskManager.h"
27 ResourceManager::initialize();
29 DefaultMonospaceFont = getNextResourceId();
30 DefaultRegularFont = getNextResourceId();
31 DefaultBoldFont = getNextResourceId();
33 defaultResource = loadFont(
"Fonts/Inconsolata-Regular.ttf", 16, DefaultMonospaceFont);
34 regularFont = loadFont(
"Fonts/SourceSansPro-Regular.ttf", 20, DefaultRegularFont);
35 boldFont = loadFont(
"Fonts/SourceCodePro-Bold.ttf", 36, DefaultBoldFont);
38 context->resourceStore->addSearchPath(
"C:/Windows/Fonts/");
41 LOG_DEBUG(logger,
"Initialized font manager.");
63 auto sharedFont = sharedFonts.find(definition);
66 if (sharedFont != sharedFonts.end() && isValidResource(sharedFont->second->getId())) {
67 return sharedFont->second;
70 if (resourceId == NoResourceId) {
71 resourceId = getNextResourceId();
83 if (definition.
fontFamily.find(
"System:") != std::string::npos) {
92 auto handle = loadResource(&loadInfo);
98 sharedFonts[definition] = handle;
105 if (!checkPreloaded(loadInfo))
return;
107 auto loadResource = [loadInfo,
this]()
109 auto loader = findLoader(loadInfo);
112 if (!loader->load(context, *loadInfo)) {
113 loadInfo->
handle->setFailedLoad();
116 LOG_WARNING(logger,
"No suitable font loader found for %s.", loadInfo->
resourceName.c_str());
118 loadInfo->
handle->setFailedLoad();
121 setProcessed(loadInfo, !loadInfo->loadSync());
124 if (loadInfo->loadSync()) {
135 inline void getCharacterQuad(
const Font::Character * chardata,
int pw,
int ph,
int charIndex,
float * xPos,
float * yPos,
Font::BakedCharacter * q,
int openGLFill,
int alignToInteger = 0)
137 const float d3dBias = openGLFill ? 0 : -0.5f;
138 float ipw = 1.0f / pw, iph = 1.0f / ph;
141 if (alignToInteger) {
142 float x = floorf((*xPos + b->xOffset) + 0.5f);
143 float y = floorf((*yPos + b->yOffset) + 0.5f);
146 q->x1 = x + b->xOffset2 - b->xOffset + d3dBias;
147 q->y1 = y + b->yOffset2 - b->yOffset + d3dBias;
149 q->x0 = *xPos + b->xOffset + d3dBias;
150 q->y0 = *yPos + b->yOffset + d3dBias;
151 q->x1 = *xPos + b->xOffset2 + d3dBias;
152 q->y1 = *yPos + b->yOffset2 + d3dBias;
160 *xPos += b->xAdvance;
167 if (characterIndex < 0) {
168 characterIndex += 256;
169 }
else if (characterIndex > 255) {
173 getCharacterQuad(font->characters.data(), font->bitmapSize, font->bitmapSize, characterIndex, &x, &y, &bakedCharacter,
true,
true);
178 LOG_WARNING(logger,
"Loading font from path %s failed.", loadInfo->
resourcePath.c_str());
183 auto textureManager = context->textureManager;
185 if (font->bitmap.empty()) {
186 LOG_ERROR(logger,
"Font contents empty.");
190 auto texture = textureManager->loadTexture2D(font->bitmap.data(),
193 TextureFormat::R8G8B8A8_UNORM_SRGB,
199 font->texture = texture;
201 texture->setName(
"Texture:" + font->getName().to_string());
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.
~FontManager() override
Destructs the FontManaer.
void clear() override
Clear the FontManager, releasing all default Font resources.
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.
void initialize() override
Initialize the FontManager, creating default Font resources.
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.
void clear() override
Clear the resource manager, cleaning up resources held by member handles.
static constexpr TaskQueueId ResourceQueue
Resource task queue.
Log implementation class.
@ Dependency
The resource is a dependency that should trigger subsequent updates if changed.
@ Resident
The resource is loaded onto the GPU.
ActivationResult
Defines results for resource activation.
@ Success
Resource activated successfully.
@ Failure
Resource activation failed.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
ResourceLoadFlags
Flags for describing how to load a resource.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
std::string fontFamily
Font family name, such as "Arial" or "Tahoma".
float fontSize
Size of the font in pixels.
Defines information used to load a Font resource.
FontDefinition definition
Definition of the Font resource.
Defines a quad with locations in screen space defined by [x0, y0] and [x1, y1].
Defines a single character from a font resource.
Font resources are used to render text with a specific font and size.
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
std::string resourcePath
Resource path. Used to locate resource.
std::string resourceName
Desired resource name. If no name is given, a default name will be chosen.
ResourceId resourceId
Unique resource identifier. Must be unique among resources of the same kind.
ResourceHandleBase handle
Handle to resource structure for holding actual resource data.
ResourceLoadFlags loadFlags
Desired loading flags. Used to specify how the resource will be loaded.