2020-08-26 15:38:01 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#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 14:24:18 +08:00
|
|
|
{
|
2021-03-21 17:08:28 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Spritefont uses u32 codepoints
|
|
|
|
using Codepoint = u32;
|
2021-03-21 14:24:18 +08:00
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
// CharSet is a Vector of Character Ranges
|
|
|
|
struct CharRange;
|
|
|
|
using CharSet = Vector<CharRange>;
|
2021-03-21 14:24:18 +08:00
|
|
|
|
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 14:24:18 +08:00
|
|
|
|
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
|
|
|
|
{
|
2022-02-12 07:18:10 +08:00
|
|
|
Codepoint codepoint = 0;
|
|
|
|
int glyph = 0;
|
2020-08-26 15:38:01 +08:00
|
|
|
Subtexture subtexture;
|
|
|
|
float advance = 0;
|
2021-12-13 12:41:23 +08:00
|
|
|
Vec2f offset;
|
2020-08-26 15:38:01 +08:00
|
|
|
};
|
|
|
|
|
2022-02-12 07:18:10 +08:00
|
|
|
struct Kerning
|
|
|
|
{
|
|
|
|
Codepoint a;
|
|
|
|
Codepoint b;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
// SpriteFont name
|
2020-09-21 15:36:19 +08:00
|
|
|
String name;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// Height, in pixels
|
2022-02-12 16:18:08 +08:00
|
|
|
float size = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// Ascent, in pixels
|
2022-02-12 16:18:08 +08:00
|
|
|
float ascent = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// Descent, in pixels
|
2022-02-12 16:18:08 +08:00
|
|
|
float descent = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// Line Gap, in pixels
|
2022-02-12 16:18:08 +08:00
|
|
|
float line_gap = 0;
|
2020-09-21 15:36:19 +08:00
|
|
|
|
2022-02-12 16:18:08 +08:00
|
|
|
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);
|
2022-02-12 16:18:08 +08:00
|
|
|
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
|
2022-02-12 16:18:08 +08:00
|
|
|
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
|
2020-09-21 15:36:19 +08:00
|
|
|
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)
|
2020-09-21 15:36:19 +08:00
|
|
|
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
|
2020-10-25 06:21:36 +08:00
|
|
|
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
|
2022-02-12 16:18:08 +08:00
|
|
|
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:
|
2022-02-12 07:18:10 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|