diff --git a/include/plutovg.h b/include/plutovg.h index f95eecc..e79d33a 100644 --- a/include/plutovg.h +++ b/include/plutovg.h @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/source/plutovg-canvas.c b/source/plutovg-canvas.c index a82cd46..6c2a698 100644 --- a/source/plutovg-canvas.c +++ b/source/plutovg-canvas.c @@ -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) { diff --git a/source/plutovg-font.c b/source/plutovg-font.c index 17a363f..5d0f667 100644 --- a/source/plutovg-font.c +++ b/source/plutovg-font.c @@ -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) { @@ -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; @@ -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: @@ -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);