[WIP] Fonts update

This commit is contained in:
Piotr Krygier committed 2025-06-20 13:44:33 +02:00
1 parent 99d7546e3d
commit c1a8b14a90
2 files changed
+36 -9

No files matched your search

+35 -8
View File
@@ -5,24 +5,37 @@
#include "freetype/fttypes.h" #include "freetype/fttypes.h"
#include "locale_fonts.h" #include "locale_fonts.h"
#include "rse_font_errors.h" #include "rse_font_errors.h"
#include "src/mesh_controller.h"
#include "src/vulkan_commons.h"
#include "utilities/logger.h" #include "utilities/logger.h"
#include "utilities/rse_commons.h" #include "utilities/rse_commons.h"
#include "utilities/rse_errors_common.h" #include "utilities/rse_errors_common.h"
#include "rse_vulkan_image.h" #include "rse_vulkan_image.h"
#include "ft2build.h"
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
#include FT_FREETYPE_H #include FT_FREETYPE_H
#define FONT_CHARACTERS_COUNT 126 /* Load only ASCII characters that are needed. TODO: Add unicode characters */
#define FONT_CHARACTERS_COUNT 94
uint16_t g_font_textures[FONT_CHARACTERS_COUNT]; struct font_data_t {
uint16_t mesh_id;
uint16_t texture_id;
uint16_t width;
uint16_t height;
uint16_t bearing_x;
uint16_t bearing_y;
uint16_t advance;
};
struct font_data_t g_fonts_data[FONT_CHARACTERS_COUNT];
FT_Library g_library; FT_Library g_library;
rse_err_t rse_fonts_initialize() rse_err_t rse_fonts_initialize()
{ {
FT_Error error; FT_Error error;
memset(g_fonts_data, 0, sizeof(g_fonts_data));
error = FT_Init_FreeType(&g_library); error = FT_Init_FreeType(&g_library);
if (error) { if (error) {
@@ -39,6 +52,8 @@ rse_err_t rse_fonts_load_from_file(const char* file_path)
FT_Error error; FT_Error error;
FT_Face face; FT_Face face;
size_t iter = 0U; size_t iter = 0U;
struct rse_vertex_t font_vertices[4];
const uint16_t font_indices[] = {0, 1, 2, 2, 3, 0};
error = FT_New_Face(g_library, file_path, 0, &face); error = FT_New_Face(g_library, file_path, 0, &face);
@@ -47,8 +62,8 @@ rse_err_t rse_fonts_load_from_file(const char* file_path)
return FONT_ERROR_LOAD_FAILED; return FONT_ERROR_LOAD_FAILED;
} }
/* By default we set font size to 16px. TODO: change later? */ /* By default we set font size to 200px. TODO: change later? It looks OK though.. It looks OK though.... */
error = FT_Set_Pixel_Sizes(face, 0, 48); error = FT_Set_Pixel_Sizes(face, 0, 200);
if (error) { if (error) {
LOGF(font_messages[FONT_CHANGE_SIZE_FAILED]); LOGF(font_messages[FONT_CHANGE_SIZE_FAILED]);
@@ -57,18 +72,30 @@ rse_err_t rse_fonts_load_from_file(const char* file_path)
/* Convert font face to textured image */ /* Convert font face to textured image */
/* Loading only needed ascii characters, from 40 (space) to 126 (~) */ /* Loading only needed ascii characters, from 40 (space) to 126 (~) */
for (iter = 33U; iter < face->num_glyphs && iter < 127U; ++iter) { for (iter = 0U; iter < face->num_glyphs && iter < FONT_CHARACTERS_COUNT; ++iter) {
error = FT_Load_Char(face, iter, FT_LOAD_RENDER); error = FT_Load_Char(face, iter + 33U, FT_LOAD_RENDER);
if (error) { if (error) {
LOGF(font_messages[FONT_LOAD_GLYPH_FAILED]); LOGF(font_messages[FONT_LOAD_GLYPH_FAILED]);
return FONT_ERROR_LOAD_GLYPH_FAILED; return FONT_ERROR_LOAD_GLYPH_FAILED;
} }
g_fonts_data[iter].width = face->glyph->bitmap.width;
g_fonts_data[iter].height = face->glyph->bitmap.rows;
g_fonts_data[iter].bearing_x = face->glyph->bitmap_left;
g_fonts_data[iter].bearing_y = face->glyph->bitmap_top;
g_fonts_data[iter].advance = face->glyph->advance.x;
font_vertices[0] = (struct rse_vertex_t) {
{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}
};
font_vertices[1] = (struct rse_vertex_t) {
{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}
};
STATUS_CHECK(rse_load_texture_from_bitmat(face->glyph->bitmap.buffer, STATUS_CHECK(rse_load_texture_from_bitmat(face->glyph->bitmap.buffer,
face->glyph->bitmap.width, face->glyph->bitmap.width,
face->glyph->bitmap.rows, face->glyph->bitmap.rows,
&g_font_textures[iter], VK_FORMAT_R8_UNORM)); &g_fonts_data[iter].texture_id, VK_FORMAT_R8_UNORM));
} }
return status; return status;
} }
+1 -1
View File
@@ -412,7 +412,7 @@ rse_err_t rse_load_texture_from_bitmat(const unsigned char* pixel_buffer, int wi
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
/* Create actual Vulkan Image */ /* Create actual Vulkan Image */
STATUS_CHECK(create_textured_image(width, STATUS_CHECK(create_textured_image(width,
height, height,
color_format, color_format,
pixel_buffer, texture_id)); pixel_buffer, texture_id));