142 lines
4.4 KiB
C
142 lines
4.4 KiB
C
#include "rse_text_renderer.h"
|
|
#include <stdint.h>
|
|
#include <strings.h>
|
|
|
|
#include "freetype/fttypes.h"
|
|
#include "locale_fonts.h"
|
|
#include "rse_font_errors.h"
|
|
#include "src/mesh_controller.h"
|
|
#include "src/vulkan_commons.h"
|
|
#include "utilities/logger.h"
|
|
#include "utilities/rse_commons.h"
|
|
#include "utilities/rse_errors_common.h"
|
|
#include "rse_vulkan_image.h"
|
|
|
|
#include "vulkan/vulkan_core.h"
|
|
#include FT_FREETYPE_H
|
|
|
|
/* Load only ASCII characters that are needed. TODO: Add unicode characters */
|
|
#define FONT_CHARACTERS_COUNT 94
|
|
#define FONT_START_CHARACTER 33U
|
|
|
|
#define FONT_SIZE 200
|
|
/* Use this to scale font to 0.0 <-> 1.0 range */
|
|
#define FONT_SCALING 1.0f/FONT_SIZE
|
|
|
|
struct font_data_t {
|
|
uint16_t mesh_id;
|
|
uint16_t texture_id;
|
|
float width;
|
|
float height;
|
|
float bearing_x;
|
|
float bearing_y;
|
|
int advance;
|
|
};
|
|
|
|
struct font_data_t g_fonts_data[FONT_CHARACTERS_COUNT];
|
|
|
|
FT_Library g_library;
|
|
rse_err_t rse_fonts_initialize()
|
|
{
|
|
FT_Error error;
|
|
|
|
memset(g_fonts_data, 0, sizeof(g_fonts_data));
|
|
error = FT_Init_FreeType(&g_library);
|
|
|
|
if (error) {
|
|
LOGF(font_messages[FONT_INITIALIZATION_FAILED]);
|
|
return FONT_ERROR_INIT_FAILED;
|
|
}
|
|
return RSE_ERROR_NO_ERROR;
|
|
}
|
|
|
|
|
|
rse_err_t rse_fonts_load_from_file(const char* file_path)
|
|
{
|
|
rse_err_t status = RSE_ERROR_NO_ERROR;
|
|
FT_Error error;
|
|
FT_Face face;
|
|
size_t iter = 0U;
|
|
struct rse_vertex_t font_vertices[4];
|
|
uint16_t font_indices[] = {0, 1, 2, 2, 3, 0};
|
|
|
|
error = FT_New_Face(g_library, file_path, 0, &face);
|
|
|
|
if (error) {
|
|
LOGF(font_messages[FONT_LOAD_FONT_FAILED]);
|
|
return FONT_ERROR_LOAD_FAILED;
|
|
}
|
|
|
|
/* By default we set font size to FONT_SIZE.*/
|
|
error = FT_Set_Pixel_Sizes(face, 0, FONT_SIZE);
|
|
|
|
if (error) {
|
|
LOGF(font_messages[FONT_CHANGE_SIZE_FAILED]);
|
|
return FONT_ERROR_CHANGE_SIZE_FAILED;
|
|
}
|
|
|
|
/* Convert font face to textured image */
|
|
/* Loading only needed ascii characters, from 40 (space) to 126 (~) */
|
|
for (iter = 0U; iter < face->num_glyphs && iter < FONT_CHARACTERS_COUNT; ++iter) {
|
|
error = FT_Load_Char(face, iter + FONT_START_CHARACTER, FT_LOAD_RENDER);
|
|
|
|
if (error) {
|
|
LOGF(font_messages[FONT_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;
|
|
|
|
LOGI("Width: %f, advance: %f", g_fonts_data[iter].width, g_fonts_data[iter].advance);
|
|
font_vertices[0] = (struct rse_vertex_t) {
|
|
{g_fonts_data[iter].bearing_x, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}
|
|
};
|
|
font_vertices[1] = (struct rse_vertex_t) {
|
|
{g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}
|
|
};
|
|
font_vertices[2] = (struct rse_vertex_t) {
|
|
{g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}
|
|
};
|
|
font_vertices[3] = (struct rse_vertex_t) {
|
|
{g_fonts_data[iter].bearing_x, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}
|
|
};
|
|
|
|
g_fonts_data[iter].mesh_id = create_mesh(font_vertices, font_indices, 8, 6);
|
|
|
|
STATUS_CHECK(rse_load_texture_from_bitmat(face->glyph->bitmap.buffer,
|
|
face->glyph->bitmap.width,
|
|
face->glyph->bitmap.rows,
|
|
&g_fonts_data[iter].texture_id, VK_FORMAT_R8_UNORM));
|
|
|
|
}
|
|
return status;
|
|
}
|
|
|
|
rse_err_t rse_print_text(const char* text)
|
|
{
|
|
rse_err_t status = RSE_ERROR_NO_ERROR;
|
|
size_t iter = 0U;
|
|
size_t charactet_code = 0U;
|
|
float advance = 0.0f;
|
|
|
|
for (iter = 0U; iter < strlen(text); ++iter) {
|
|
charactet_code = (size_t)text[iter] - FONT_START_CHARACTER;
|
|
|
|
create_mesh_instance(g_fonts_data[charactet_code].mesh_id, (struct rse_instance_data_t){
|
|
{advance, 0.0f, 0.0f},
|
|
{0.0f, 0.0f, rse_math_deg_to_radians(0.0f)},
|
|
FONT_SCALING,
|
|
g_fonts_data[charactet_code].texture_id
|
|
});
|
|
|
|
advance += (g_fonts_data[charactet_code].advance >> 6) * FONT_SCALING;
|
|
LOGI("Advance: %f", advance);
|
|
}
|
|
|
|
return status;
|
|
}
|