fix: narrowing warning & improper height calculation

This commit is contained in:
27justin 2024-11-16 13:12:58 +01:00
parent ecf07c614c
commit cc2e69f33b

View File

@ -72,7 +72,7 @@ static cairo_t *Clay__Cairo = NULL;
// Return a null-terminated copy of Clay_String `str`. // Return a null-terminated copy of Clay_String `str`.
// Callee is required to free. // Callee is required to free.
static inline char *Clay_Cairo__NullTerminate(Clay_String *str) { static inline char *Clay_Cairo__NullTerminate(Clay_String *str) {
char *copy = malloc(str->length + 1); char *copy = (char*) malloc(str->length + 1);
if (!copy) { if (!copy) {
fprintf(stderr, "Memory allocation failed\n"); fprintf(stderr, "Memory allocation failed\n");
return NULL; return NULL;
@ -96,8 +96,8 @@ static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_Text
// brute-forcing it until the text boundaries look // brute-forcing it until the text boundaries look
// okay-ish. You should probably rather use a proper text // okay-ish. You should probably rather use a proper text
// shaping engine like HarfBuzz or Pango. // shaping engine like HarfBuzz or Pango.
.width = te.x_advance * 1.9, .width = ((float) te.x_advance) * 1.9f,
.height = config->fontSize .height = (float) config->fontSize
}; };
} }
@ -153,8 +153,8 @@ static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_Text
// Return dimensions // Return dimensions
return (Clay_Dimensions){ return (Clay_Dimensions){
.width = glyph_extents.width, .width = (float) glyph_extents.width,
.height = glyph_extents.height .height = (float) glyph_extents.height
}; };
} }
@ -238,7 +238,7 @@ void Clay_Cairo_Render(Clay_RenderCommandArray commands) {
cairo_select_font_face(Clay__Cairo, font_family, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_select_font_face(Clay__Cairo, font_family, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, command->config.textElementConfig->fontSize); cairo_set_font_size(cr, command->config.textElementConfig->fontSize);
cairo_move_to(cr, bb.x, bb.y + bb.height / 2.f); cairo_move_to(cr, bb.x, bb.y + bb.height);
cairo_set_source_rgba(cr, CLAY_TO_CAIRO(color)); cairo_set_source_rgba(cr, CLAY_TO_CAIRO(color));
cairo_show_text(cr, text); cairo_show_text(cr, text);
@ -338,9 +338,7 @@ void Clay_Cairo_Render(Clay_RenderCommandArray commands) {
Clay_ImageElementConfig *config = command->config.imageElementConfig; Clay_ImageElementConfig *config = command->config.imageElementConfig;
Clay_BoundingBox bb = command->boundingBox; Clay_BoundingBox bb = command->boundingBox;
char *path = malloc(config->path.length + 1); char *path = Clay_Cairo__NullTerminate(&config->path);
memcpy(path, config->path.chars, config->path.length);
path[config->path.length] = '\0';
cairo_surface_t *surf = cairo_image_surface_create_from_png(path), cairo_surface_t *surf = cairo_image_surface_create_from_png(path),
*origin = cairo_get_target(cr); *origin = cairo_get_target(cr);