2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// Font.h
|
|
|
|
//
|
|
|
|
// Loading, unloading, caching and rendering of fonts.
|
|
|
|
// Also functions for word wrapping and similar.
|
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_RESOURCES_FONT_H
|
|
|
|
#define ES_CORE_RESOURCES_FONT_H
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "math/Vector2f.h"
|
|
|
|
#include "math/Vector2i.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
#include "ThemeData.h"
|
2020-12-16 22:59:00 +00:00
|
|
|
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include <vector>
|
2013-10-04 23:24:41 +00:00
|
|
|
|
|
|
|
class TextCache;
|
|
|
|
|
2020-11-17 22:06:54 +00:00
|
|
|
#define FONT_SIZE_MINI (static_cast<unsigned int>(0.030f * Math::min(static_cast<int>( \
|
|
|
|
Renderer::getScreenHeight()), static_cast<int>(Renderer::getScreenWidth()))))
|
|
|
|
#define FONT_SIZE_SMALL (static_cast<unsigned int>(0.035f * Math::min(static_cast<int>( \
|
|
|
|
Renderer::getScreenHeight()), static_cast<int>(Renderer::getScreenWidth()))))
|
|
|
|
#define FONT_SIZE_MEDIUM (static_cast<unsigned int>(0.045f * Math::min(static_cast<int>( \
|
|
|
|
Renderer::getScreenHeight()), static_cast<int>(Renderer::getScreenWidth()))))
|
|
|
|
#define FONT_SIZE_LARGE (static_cast<unsigned int>(0.085f * Math::min(static_cast<int>( \
|
|
|
|
Renderer::getScreenHeight()), static_cast<int>(Renderer::getScreenWidth()))))
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 17:35:43 +00:00
|
|
|
#define FONT_PATH_LIGHT ":/fonts/opensans_hebrew_condensed_light.ttf"
|
|
|
|
#define FONT_PATH_REGULAR ":/fonts/opensans_hebrew_condensed_regular.ttf"
|
2014-03-17 00:52:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
enum Alignment {
|
|
|
|
ALIGN_LEFT,
|
|
|
|
ALIGN_CENTER, // Centers both horizontally and vertically.
|
|
|
|
ALIGN_RIGHT,
|
|
|
|
ALIGN_TOP,
|
|
|
|
ALIGN_BOTTOM
|
2014-05-01 19:47:33 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// A TrueType Font renderer that uses FreeType and OpenGL.
|
|
|
|
// The library is automatically initialized when it's needed.
|
2013-10-04 23:24:41 +00:00
|
|
|
class Font : public IReloadable
|
|
|
|
{
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
static void initLibrary();
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
static std::shared_ptr<Font> get(int size, const std::string& path = getDefaultPath());
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
virtual ~Font();
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Returns the expected size of a string when rendered. Extra spacing is applied to the Y axis.
|
|
|
|
Vector2f sizeText(std::string text, float lineSpacing = 1.5f);
|
|
|
|
TextCache* buildTextCache(const std::string& text, float offsetX,
|
2020-11-16 16:46:36 +00:00
|
|
|
float offsetY, unsigned int color, float lineSpacing = 1.5f);
|
2020-06-21 12:25:28 +00:00
|
|
|
TextCache* buildTextCache(
|
|
|
|
const std::string& text,
|
|
|
|
Vector2f offset,
|
|
|
|
unsigned int color,
|
|
|
|
float xLen,
|
|
|
|
Alignment alignment = ALIGN_LEFT,
|
|
|
|
float lineSpacing = 1.5f);
|
|
|
|
void renderTextCache(TextCache* cache);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Inserts newlines into text to make it wrap properly.
|
|
|
|
std::string wrapText(std::string text, float xLen);
|
|
|
|
// Returns the expected size of a string after wrapping is applied.
|
|
|
|
Vector2f sizeWrappedText(std::string text, float xLen, float lineSpacing = 1.5f);
|
|
|
|
// Returns the position of of the cursor after moving "cursor" characters.
|
|
|
|
Vector2f getWrappedTextCursorOffset(std::string text, float xLen, size_t cursor,
|
|
|
|
float lineSpacing = 1.5f);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
float getHeight(float lineSpacing = 1.5f) const;
|
|
|
|
float getLetterHeight();
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
void unload(std::shared_ptr<ResourceManager>& rm) override;
|
|
|
|
void reload(std::shared_ptr<ResourceManager>& rm) override;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
int getSize() const;
|
|
|
|
inline const std::string& getPath() const { return mPath; }
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-12-16 22:59:00 +00:00
|
|
|
inline static std::string getDefaultPath() { return FONT_PATH_REGULAR; }
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
static std::shared_ptr<Font> getFromTheme(const ThemeData::ThemeElement* elem,
|
|
|
|
unsigned int properties, const std::shared_ptr<Font>& orig);
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Returns an approximation of VRAM used by this font's texture (in bytes).
|
|
|
|
size_t getMemUsage() const;
|
|
|
|
// Returns an approximation of total VRAM used by font textures (in bytes).
|
|
|
|
static size_t getTotalMemUsage();
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2013-10-04 23:24:41 +00:00
|
|
|
private:
|
2020-06-21 12:25:28 +00:00
|
|
|
static FT_Library sLibrary;
|
2020-08-15 07:28:47 +00:00
|
|
|
static std::map<std::pair<std::string, int>, std::weak_ptr<Font>> sFontMap;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Font(int size, const std::string& path);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
struct FontTexture {
|
|
|
|
unsigned int textureId;
|
|
|
|
Vector2i textureSize;
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Vector2i writePos;
|
|
|
|
int rowHeight;
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
FontTexture();
|
|
|
|
~FontTexture();
|
|
|
|
bool findEmpty(const Vector2i& size, Vector2i& cursor_out);
|
2014-08-11 23:05:18 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// You must call initTexture() after creating a FontTexture to get a textureId.
|
|
|
|
// Initializes the OpenGL texture according to this FontTexture's settings,
|
|
|
|
// updating textureId.
|
|
|
|
void initTexture();
|
|
|
|
// Deinitializes the OpenGL texture if any exists, is automatically called
|
|
|
|
// in the destructor.
|
|
|
|
void deinitTexture();
|
|
|
|
};
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
struct FontFace {
|
|
|
|
const ResourceData data;
|
|
|
|
FT_Face face;
|
2014-08-30 20:36:56 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
FontFace(ResourceData&& d, int size);
|
|
|
|
virtual ~FontFace();
|
|
|
|
};
|
2014-08-30 20:36:56 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
void rebuildTextures();
|
|
|
|
void unloadTextures();
|
2014-08-11 23:05:18 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<FontTexture> mTextures;
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
void getTextureForNewGlyph(const Vector2i& glyphSize, FontTexture*& tex_out,
|
|
|
|
Vector2i& cursor_out);
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::map< unsigned int, std::unique_ptr<FontFace> > mFaceCache;
|
|
|
|
FT_Face getFaceForChar(unsigned int id);
|
|
|
|
void clearFaceCache();
|
2014-08-30 20:36:56 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
struct Glyph {
|
|
|
|
FontTexture* texture;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Vector2f texPos;
|
|
|
|
Vector2f texSize; // In texels!
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Vector2f advance;
|
|
|
|
Vector2f bearing;
|
|
|
|
};
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::map<unsigned int, Glyph> mGlyphMap;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Glyph* getGlyph(unsigned int id);
|
2014-07-27 21:44:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
int mMaxGlyphHeight;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
const int mSize;
|
|
|
|
const std::string mPath;
|
2014-05-12 22:05:28 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
float getNewlineStartOffset(const std::string& text, const unsigned int& charStart,
|
|
|
|
const float& xLen, const Alignment& alignment);
|
2014-05-13 01:03:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
friend TextCache;
|
2013-10-04 23:24:41 +00:00
|
|
|
};
|
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
// Used to store a sort of "pre-rendered" string.
|
2020-06-21 12:25:28 +00:00
|
|
|
// When a TextCache is constructed (Font::buildTextCache()), the vertices and texture coordinates
|
|
|
|
// of the string are calculated and stored in the TextCache object. Rendering a previously
|
|
|
|
// constructed TextCache (Font::renderTextCache) every frame is MUCH faster than rebuilding
|
|
|
|
// one every frame. Keep in mind you still need the Font object to render a TextCache (as the
|
|
|
|
// Font holds the OpenGL texture), and if a Font changes your TextCache may become invalid.
|
2013-10-04 23:24:41 +00:00
|
|
|
class TextCache
|
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
protected:
|
2020-06-21 12:25:28 +00:00
|
|
|
struct VertexList {
|
|
|
|
std::vector<Renderer::Vertex> verts;
|
|
|
|
// This is a pointer because the texture ID can change during
|
|
|
|
// deinit/reinit (when launching a game).
|
|
|
|
unsigned int* textureIdPtr;
|
|
|
|
};
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<VertexList> vertexLists;
|
2014-07-27 21:44:02 +00:00
|
|
|
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
struct CacheMetrics {
|
|
|
|
Vector2f size;
|
|
|
|
} metrics;
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
void setColor(unsigned int color);
|
2013-10-04 23:24:41 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
friend Font;
|
2013-10-04 23:24:41 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_RESOURCES_FONT_H
|