From 638de2057ad9cde6474608942ec1a6ed99317710 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 20 Nov 2022 12:01:35 +0900 Subject: [PATCH] font_freetype: Consider glyph information for horizontal positioning When creating a block-sized texture from a glyph, use the bitmap_left field of FT_GlyphSlot instead of just centering it horizontally. This improves positioning of punctuation marks in Japanese text. --- src/font_freetype.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/font_freetype.c b/src/font_freetype.c index fc6ae9d..9c0f143 100644 --- a/src/font_freetype.c +++ b/src/font_freetype.c @@ -76,14 +76,14 @@ static float ft_font_get_actual_size(struct font *_, float size) // Convert a glyph rendered by FreeType to a block-sized texture. // Block size is size x 1.5*size (full-width) or size/2 x 1.5*size (half-width) -static Rectangle init_glyph_texture(Texture *dst, FT_Bitmap *glyph, int bitmap_top, int size, bool half_width) +static Rectangle init_glyph_texture(Texture *dst, FT_Bitmap *glyph, int bitmap_left, int bitmap_top, int size, bool half_width) { // calculate block size and offsets int block_width = (half_width ? size/2 : size); int block_height = size + size/2; int width = block_width + GLYPH_BORDER_SIZE*2; int height = block_height + GLYPH_BORDER_SIZE*2; - int off_x = max(0, (block_width - (int)glyph->width) / 2) + GLYPH_BORDER_SIZE; + int off_x = max(0, bitmap_left) + GLYPH_BORDER_SIZE; int off_y = max(0, size - bitmap_top) + GLYPH_BORDER_SIZE; uint8_t *bitmap = xcalloc(1, width * height); @@ -136,8 +136,8 @@ static bool ft_font_get_glyph(struct font_size *size, struct glyph *glyph, uint3 // create texture from bitmap FT_Bitmap *bitmap = &font->font->glyph->bitmap; if (bitmap->pixel_mode == FT_PIXEL_MODE_GRAY) { - glyph->rect = init_glyph_texture(t, bitmap, font->font->glyph->bitmap_top, - size->size, half_width); + glyph->rect = init_glyph_texture(t, bitmap, font->font->glyph->bitmap_left, + font->font->glyph->bitmap_top, size->size, half_width); } else if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) { FT_Bitmap tmp; FT_Bitmap_New(&tmp); @@ -151,8 +151,8 @@ static bool ft_font_get_glyph(struct font_size *size, struct glyph *glyph, uint3 if (tmp.buffer[i]) tmp.buffer[i] = 255; } - glyph->rect = init_glyph_texture(t, &tmp, font->font->glyph->bitmap_top, - size->size, half_width); + glyph->rect = init_glyph_texture(t, &tmp, font->font->glyph->bitmap_left, + font->font->glyph->bitmap_top, size->size, half_width); FT_Bitmap_Done(ft_lib, &tmp); } else { WARNING("Font returned glyph with unsupported pixel mode");