blah/include/blah_spritefont.h

126 lines
3.2 KiB
C
Raw Normal View History

2020-08-26 15:38:01 +08:00
#pragma once
#include <blah_common.h>
#include <blah_string.h>
#include <blah_vector.h>
#include <blah_subtexture.h>
#include <blah_font.h>
2020-08-26 15:38:01 +08:00
namespace Blah
{
2021-03-21 17:08:28 +08:00
// Sprite Font is a bitmap font implementation for font rendering.
// It can be constructed from a Font (ttf) and will automatically create
// texture atlases for rendering. You can add your own characters
// and textures to it.
class SpriteFont
{
2021-03-21 17:08:28 +08:00
public:
// Spritefont uses u32 codepoints
using Codepoint = u32;
2021-03-21 17:08:28 +08:00
// CharSet is a Vector of Character Ranges
struct CharRange;
using CharSet = Vector<CharRange>;
2021-03-21 17:08:28 +08:00
// Character range, used for building the Sprite Font
struct CharRange
{
Codepoint from;
Codepoint to;
2021-03-21 17:08:28 +08:00
CharRange();
CharRange(Codepoint single);
CharRange(Codepoint from, Codepoint to);
static const CharSet ASCII;
};
// Character Entry
2020-08-26 15:38:01 +08:00
struct Character
{
Codepoint codepoint = 0;
int glyph = 0;
2020-08-26 15:38:01 +08:00
Subtexture subtexture;
float advance = 0;
Vec2f offset;
2020-08-26 15:38:01 +08:00
};
struct Kerning
{
Codepoint a;
Codepoint b;
float value;
};
2021-03-21 17:08:28 +08:00
// SpriteFont name
String name;
2021-03-21 17:08:28 +08:00
// Height, in pixels
float size = 0;
2021-03-21 17:08:28 +08:00
// Ascent, in pixels
float ascent = 0;
2021-03-21 17:08:28 +08:00
// Descent, in pixels
float descent = 0;
2021-03-21 17:08:28 +08:00
// Line Gap, in pixels
float line_gap = 0;
SpriteFont() = default;
2021-03-21 17:08:28 +08:00
SpriteFont(const FilePath& file, float size);
SpriteFont(const FilePath& file, float size, const CharSet& charset);
SpriteFont(const FontRef& font, float size);
SpriteFont(const FontRef& font, float size, const CharSet& charset);
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// releases all assets used by the spritefont
void clear();
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// gets the height of the sprite font
float height() const { return ascent - descent; }
2021-03-21 17:08:28 +08:00
// gets the line height of the sprite font (height + line gap)
float line_height() const { return ascent - descent + line_gap; }
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// returns a list of all texture atlases
const Vector<TextureRef>& textures() { return m_atlas; }
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// calculates the width of the given string
2020-08-26 15:38:01 +08:00
float width_of(const String& text) const;
2021-03-21 17:08:28 +08:00
// calculates the width of the next line
2020-08-26 15:38:01 +08:00
float width_of_line(const String& text, int start = 0) const;
2021-03-21 17:08:28 +08:00
// calculates the height of the given string
2020-08-26 15:38:01 +08:00
float height_of(const String& text) const;
2021-03-21 17:08:28 +08:00
// disposes the existing spritefont data and rebuilds from the given font file
void rebuild(const FilePath& file, float size, const CharSet& charset);
// disposes the existing spritefont data and rebuilds from the given font
void rebuild(const FontRef& font, float size, const CharSet& charset);
2021-03-21 17:08:28 +08:00
// gets the kerning between two characters
float get_kerning(Codepoint codepoint0, Codepoint codepoint1) const;
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// sets the kerning between two characters
void set_kerning(Codepoint codepoint0, Codepoint codepoint1, float kerning);
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// gets the character at the given codepoint
Character& get_character(Codepoint codepoint);
// gets the character at the given codepoint
const Character& get_character(Codepoint codepoint) const;
// gets the character at the given codepoint
Character& operator[](Codepoint codepoint);
// gets the character at the given codepoint
const Character& operator[](Codepoint codepoint) const;
private:
Vector<Character> m_characters;
Vector<Kerning> m_kerning;
2021-03-21 17:08:28 +08:00
Vector<TextureRef> m_atlas;
2020-08-26 15:38:01 +08:00
};
}