BLT-With-Graphics-Template/include/blt/gfx/font/font.h

85 lines
2.5 KiB
C
Raw Normal View History

2024-11-11 02:29:28 -05:00
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_WITH_GRAPHICS_FONT_H
#define BLT_WITH_GRAPHICS_FONT_H
#include <ft2build.h>
#include FT_FREETYPE_H
#include <string>
#include <string_view>
#include <memory>
#include <optional>
2024-11-11 16:47:25 -05:00
#include "blt/std/types.h"
#include <blt/math/vectors.h>
2024-11-11 02:29:28 -05:00
namespace blt::gfx::font
{
extern const unsigned int default_font_compressed_data[];
extern const unsigned int default_font_compressed_size;
2024-11-11 19:21:42 -05:00
FT_Library* create();
2024-11-11 02:29:28 -05:00
2024-11-11 19:21:42 -05:00
void cleanup(FT_Library* lib = nullptr);
2024-11-11 02:29:28 -05:00
class font_face_t
{
public:
2024-11-13 15:24:58 -05:00
explicit font_face_t(std::string_view path, std::string_view font_name);
2024-11-11 02:29:28 -05:00
2024-11-13 15:24:58 -05:00
explicit font_face_t(std::string_view font_name, const blt::u8* data, blt::size_t size, bool compressed = false);
2024-11-11 02:29:28 -05:00
2024-11-11 19:21:42 -05:00
void set_pixel_sizes(blt::u32 width, blt::u32 height) const;
2024-11-11 02:29:28 -05:00
2024-11-11 16:47:25 -05:00
[[nodiscard]] std::optional<int> load_char(FT_ULong c) const;
2024-11-11 02:29:28 -05:00
[[nodiscard]] FT_Face* get() const
2024-11-13 15:24:58 -05:00
{ return face.get(); }
[[nodiscard]] std::string_view get_name() const
{ return font_name; }
2024-11-11 02:29:28 -05:00
private:
// decompressed data ownership
2024-11-11 19:21:42 -05:00
std::shared_ptr<blt::u8[]> internal_uncompressed_data;
2024-11-11 02:29:28 -05:00
std::shared_ptr<FT_Face> face;
2024-11-13 15:24:58 -05:00
std::string font_name;
2024-11-11 02:29:28 -05:00
};
struct font_file_t
{
2024-11-11 16:47:25 -05:00
font_face_t font;
blt::u64 character_min_index;
blt::u64 character_max_index;
font_file_t(font_face_t font, u64 characterMinIndex, u64 characterMaxIndex);
};
struct glyph_t
{
blt::vec2ui size;
blt::vec2ui bearing;
blt::i64 advance;
glyph_t(const vec2ui& size, const vec2ui& bearing, blt::i64 advance);
2024-11-11 02:29:28 -05:00
};
}
#endif //BLT_WITH_GRAPHICS_FONT_H