2020-08-26 15:38:01 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_common.h>
|
|
|
|
#include <blah_stream.h>
|
|
|
|
#include <blah_image.h>
|
|
|
|
#include <blah_string.h>
|
|
|
|
#include <blah_vector.h>
|
|
|
|
#include <blah_filesystem.h>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
namespace Blah
|
|
|
|
{
|
2022-02-12 16:18:08 +08:00
|
|
|
class Font;
|
|
|
|
using FontRef = Ref<Font>;
|
|
|
|
|
|
|
|
// Loads fonts from file and can blit individual characters to images.
|
|
|
|
// Use Font::create() to instantiate a FontRef.
|
2020-08-26 15:38:01 +08:00
|
|
|
class Font
|
|
|
|
{
|
2022-02-12 16:18:08 +08:00
|
|
|
private:
|
|
|
|
Font() = default;
|
|
|
|
|
2020-08-26 15:38:01 +08:00
|
|
|
public:
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// Font uses u32 Codepoints
|
|
|
|
using Codepoint = u32;
|
|
|
|
|
|
|
|
// Information provided for a single Character
|
|
|
|
struct Character
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
2021-03-21 17:08:28 +08:00
|
|
|
// character's glyph index
|
2020-08-26 15:38:01 +08:00
|
|
|
int glyph = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// width of the character, in pixels
|
2020-08-26 15:38:01 +08:00
|
|
|
int width = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// height of the character, in pixels
|
2020-08-26 15:38:01 +08:00
|
|
|
int height = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// advance (how much to move horizontally)
|
2020-08-26 15:38:01 +08:00
|
|
|
float advance = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// render x-offset
|
2020-10-25 06:21:36 +08:00
|
|
|
float offset_x = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// render y-offset
|
2020-10-25 06:21:36 +08:00
|
|
|
float offset_y = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// scale the character was created at
|
2020-08-26 15:38:01 +08:00
|
|
|
float scale = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// whether the character has a visible glyph
|
2020-10-25 06:21:36 +08:00
|
|
|
bool has_glyph = false;
|
2020-08-26 15:38:01 +08:00
|
|
|
};
|
|
|
|
|
2022-02-12 16:18:08 +08:00
|
|
|
// creates a new font from the given file path
|
|
|
|
static FontRef create(const FilePath& path);
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2022-02-12 16:18:08 +08:00
|
|
|
// creates a new font from the Stream
|
|
|
|
static FontRef create(Stream& stream);
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
// returns the font family name
|
|
|
|
const String& family_name() const;
|
|
|
|
|
|
|
|
// returns the font style name
|
|
|
|
const String& style_name() const;
|
|
|
|
|
|
|
|
// the font ascent
|
2020-10-25 06:21:36 +08:00
|
|
|
int ascent() const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// the font descent
|
2020-10-25 06:21:36 +08:00
|
|
|
int descent() const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// the font line gap (space between lines)
|
2020-10-25 06:21:36 +08:00
|
|
|
int line_gap() const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// the height of the font
|
2020-10-25 06:21:36 +08:00
|
|
|
int height() const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// the height of the line, including line gap
|
2020-10-25 06:21:36 +08:00
|
|
|
int line_height() const;
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
// gets the glyph index for the given codepoint
|
2020-10-25 06:21:36 +08:00
|
|
|
int get_glyph(Codepoint codepoint) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// gets the font scale for the given font size in pixels
|
2020-10-25 06:21:36 +08:00
|
|
|
float get_scale(float size) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// gets the font kerning between 2 glyphs
|
2020-10-25 06:21:36 +08:00
|
|
|
float get_kerning(int glyph1, int glyph2, float scale) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// gets character data for the given glyph, at the provided scale
|
|
|
|
Character get_character(int glyph, float scale) const;
|
|
|
|
|
|
|
|
// Blits a character to the provided pixel array.
|
|
|
|
// The pixel array must be at least ch.width * ch.height in size!
|
|
|
|
// If the character doesn't exist, this will do nothing and return false.
|
|
|
|
bool get_image(const Character& ch, Color* pixels) const;
|
|
|
|
|
|
|
|
// Returns an image of the provided character.
|
|
|
|
// If the character doesn't exist, this will return an empty image.
|
|
|
|
Image get_image(const Character& ch) const;
|
|
|
|
|
2020-08-26 15:38:01 +08:00
|
|
|
private:
|
2022-02-12 16:18:08 +08:00
|
|
|
Ref<void> m_font;
|
|
|
|
Vector<u8> m_buffer;
|
2020-10-25 06:21:36 +08:00
|
|
|
String m_family_name;
|
|
|
|
String m_style_name;
|
2022-02-12 16:18:08 +08:00
|
|
|
int m_ascent = 0;
|
|
|
|
int m_descent = 0;
|
|
|
|
int m_line_gap = 0;
|
2020-08-26 15:38:01 +08:00
|
|
|
};
|
|
|
|
}
|