Skip to content

Commit

Permalink
Refactoring plutovg_font_face_t
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 26, 2024
1 parent 9b5b469 commit ab08211
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions include/plutovg.h
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ PLUTOVG_API void plutovg_font_face_get_metrics(const plutovg_font_face_t* face,
* @param left_side_bearing Pointer to store the left side bearing of the glyph.
* @param extents Pointer to a `plutovg_rect_t` object to store the glyph bounding box.
*/
PLUTOVG_API void plutovg_font_face_get_glyph_metrics(const plutovg_font_face_t* face, float size, plutovg_codepoint_t codepoint, float* advance_width, float* left_side_bearing, plutovg_rect_t* extents);
PLUTOVG_API void plutovg_font_face_get_glyph_metrics(plutovg_font_face_t* face, float size, plutovg_codepoint_t codepoint, float* advance_width, float* left_side_bearing, plutovg_rect_t* extents);

/**
* @brief Retrieves the path of a glyph and its advance width.
Expand All @@ -860,7 +860,7 @@ PLUTOVG_API void plutovg_font_face_get_glyph_metrics(const plutovg_font_face_t*
* @param path Pointer to a `plutovg_path_t` object to store the glyph path.
* @return The advance width of the glyph.
*/
PLUTOVG_API float plutovg_font_face_get_glyph_path(const plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_t* path);
PLUTOVG_API float plutovg_font_face_get_glyph_path(plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_t* path);

/**
* @brief Traverses the path of a glyph and calls a callback for each path element.
Expand All @@ -874,7 +874,7 @@ PLUTOVG_API float plutovg_font_face_get_glyph_path(const plutovg_font_face_t* fa
* @param closure User-defined data passed to the callback function.
* @return The advance width of the glyph.
*/
PLUTOVG_API float plutovg_font_face_traverse_glyph_path(const plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_traverse_func_t traverse_func, void* closure);
PLUTOVG_API float plutovg_font_face_traverse_glyph_path(plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_traverse_func_t traverse_func, void* closure);

/**
* @brief Computes the bounding box of a text string and its advance width.
Expand All @@ -887,7 +887,7 @@ PLUTOVG_API float plutovg_font_face_traverse_glyph_path(const plutovg_font_face_
* @param extents Pointer to a `plutovg_rect_t` object to store the bounding box of the text.
* @return The total advance width of the text.
*/
PLUTOVG_API float plutovg_font_face_text_extents(const plutovg_font_face_t* face, float size, const void* text, int length, plutovg_text_encoding_t encoding, plutovg_rect_t* extents);
PLUTOVG_API float plutovg_font_face_text_extents(plutovg_font_face_t* face, float size, const void* text, int length, plutovg_text_encoding_t encoding, plutovg_rect_t* extents);

/**
* @brief Represents a color with red, green, blue, and alpha components.
Expand Down Expand Up @@ -2169,7 +2169,7 @@ PLUTOVG_API float plutovg_canvas_clip_text(plutovg_canvas_t* canvas, const void*
* @param line_gap The line gap of the font.
* @param extents The bounding box of the font.
*/
PLUTOVG_API void plutovg_canvas_font_metrics(plutovg_canvas_t* canvas, float* ascent, float* descent, float* line_gap, plutovg_rect_t* extents);
PLUTOVG_API void plutovg_canvas_font_metrics(const plutovg_canvas_t* canvas, float* ascent, float* descent, float* line_gap, plutovg_rect_t* extents);

/**
* @brief Retrieves metrics for a specific glyph.
Expand Down
2 changes: 1 addition & 1 deletion source/plutovg-canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ float plutovg_canvas_clip_text(plutovg_canvas_t* canvas, const void* text, int l
return advance_width;
}

void plutovg_canvas_font_metrics(plutovg_canvas_t* canvas, float* ascent, float* descent, float* line_gap, plutovg_rect_t* extents)
void plutovg_canvas_font_metrics(const plutovg_canvas_t* canvas, float* ascent, float* descent, float* line_gap, plutovg_rect_t* extents)
{
plutovg_state_t* state = canvas->state;
if(state->font_face && state->font_size > 0.f) {
Expand Down
34 changes: 17 additions & 17 deletions source/plutovg-font.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,31 +251,31 @@ void plutovg_font_face_get_metrics(const plutovg_font_face_t* face, float size,
}
}

static glyph_t* get_glyph(const plutovg_font_face_t* face, plutovg_codepoint_t codepoint)
static glyph_t* plutovg_font_face_get_glyph(plutovg_font_face_t* face, plutovg_codepoint_t codepoint)
{
unsigned int msb = (codepoint >> 8) & 0xFF;
if(face->glyphs[msb] == NULL) {
((plutovg_font_face_t*)face)->glyphs[msb] = calloc(GLYPH_CACHE_SIZE, sizeof(glyph_t*));
face->glyphs[msb] = calloc(GLYPH_CACHE_SIZE, sizeof(glyph_t*));
}

unsigned int lsb = codepoint & 0xFF;
if(face->glyphs[msb][lsb]) {
return face->glyphs[msb][lsb];
if(face->glyphs[msb][lsb] == NULL) {
glyph_t* glyph = malloc(sizeof(glyph_t));
glyph->index = stbtt_FindGlyphIndex(&face->info, codepoint);
glyph->nvertices = stbtt_GetGlyphShape(&face->info, glyph->index, &glyph->vertices);
stbtt_GetGlyphHMetrics(&face->info, glyph->index, &glyph->advance_width, &glyph->left_side_bearing);
if(!stbtt_GetGlyphBox(&face->info, glyph->index, &glyph->x1, &glyph->y1, &glyph->x2, &glyph->y2))
glyph->x1 = glyph->y1 = glyph->x2 = glyph->y2 = 0;
face->glyphs[msb][lsb] = glyph;
}

glyph_t* glyph = malloc(sizeof(glyph_t));
glyph->index = stbtt_FindGlyphIndex(&face->info, codepoint);
glyph->nvertices = stbtt_GetGlyphShape(&face->info, glyph->index, &glyph->vertices);
stbtt_GetGlyphHMetrics(&face->info, glyph->index, &glyph->advance_width, &glyph->left_side_bearing);
if(!stbtt_GetGlyphBox(&face->info, glyph->index, &glyph->x1, &glyph->y1, &glyph->x2, &glyph->y2))
glyph->x1 = glyph->y1 = glyph->x2 = glyph->y2 = 0;
return (face->glyphs[msb][lsb] = glyph);
return face->glyphs[msb][lsb];
}

void plutovg_font_face_get_glyph_metrics(const plutovg_font_face_t* face, float size, plutovg_codepoint_t codepoint, float* advance_width, float* left_side_bearing, plutovg_rect_t* extents)
void plutovg_font_face_get_glyph_metrics(plutovg_font_face_t* face, float size, plutovg_codepoint_t codepoint, float* advance_width, float* left_side_bearing, plutovg_rect_t* extents)
{
float scale = plutovg_font_face_get_scale(face, size);
glyph_t* glyph = get_glyph(face, codepoint);
glyph_t* glyph = plutovg_font_face_get_glyph(face, codepoint);
if(advance_width) *advance_width = glyph->advance_width * scale;
if(left_side_bearing) *left_side_bearing = glyph->left_side_bearing * scale;
if(extents) {
Expand Down Expand Up @@ -304,12 +304,12 @@ static void glyph_traverse_func(void* closure, plutovg_path_command_t command, c
}
}

float plutovg_font_face_get_glyph_path(const plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_t* path)
float plutovg_font_face_get_glyph_path(plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_t* path)
{
return plutovg_font_face_traverse_glyph_path(face, size, x, y, codepoint, glyph_traverse_func, path);
}

float plutovg_font_face_traverse_glyph_path(const plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_traverse_func_t traverse_func, void* closure)
float plutovg_font_face_traverse_glyph_path(plutovg_font_face_t* face, float size, float x, float y, plutovg_codepoint_t codepoint, plutovg_path_traverse_func_t traverse_func, void* closure)
{
float scale = plutovg_font_face_get_scale(face, size);
plutovg_matrix_t matrix;
Expand All @@ -318,7 +318,7 @@ float plutovg_font_face_traverse_glyph_path(const plutovg_font_face_t* face, flo

plutovg_point_t points[3];
plutovg_point_t current_point = {0, 0};
glyph_t* glyph = get_glyph(face, codepoint);
glyph_t* glyph = plutovg_font_face_get_glyph(face, codepoint);
for(int i = 0; i < glyph->nvertices; i++) {
switch(glyph->vertices[i].type) {
case STBTT_vmove:
Expand Down Expand Up @@ -365,7 +365,7 @@ float plutovg_font_face_traverse_glyph_path(const plutovg_font_face_t* face, flo
return glyph->advance_width * scale;
}

float plutovg_font_face_text_extents(const plutovg_font_face_t* face, float size, const void* text, int length, plutovg_text_encoding_t encoding, plutovg_rect_t* extents)
float plutovg_font_face_text_extents(plutovg_font_face_t* face, float size, const void* text, int length, plutovg_text_encoding_t encoding, plutovg_rect_t* extents)
{
plutovg_text_iterator_t it;
plutovg_text_iterator_init(&it, text, length, encoding);
Expand Down

0 comments on commit ab08211

Please sign in to comment.