|
| 1 | +#define WOB_FILE "font_freetype.c" |
| 2 | + |
| 3 | +#include "font.h" |
| 4 | +#include "log.h" |
| 5 | + |
| 6 | +#include <wayland-util.h> |
| 7 | +#include <ft2build.h> |
| 8 | +#include FT_FREETYPE_H |
| 9 | +#include FT_GLYPH_H |
| 10 | + |
| 11 | +FT_Library library; |
| 12 | + |
| 13 | +struct wob_font { |
| 14 | + char *name; |
| 15 | + FT_Face data; |
| 16 | + struct wl_list link; |
| 17 | +}; |
| 18 | + |
| 19 | +struct wob_font_manager { |
| 20 | + struct wl_list fonts; |
| 21 | +}; |
| 22 | + |
| 23 | +void |
| 24 | +draw_glyph(uint32_t *pixels, size_t stride, FT_Bitmap *ft_bitmap, struct wob_color font_color) |
| 25 | +{ |
| 26 | + for (unsigned int row = 0; row < ft_bitmap->rows; row += 1) { |
| 27 | + for (unsigned int width = 0; width < ft_bitmap->width; width += 1) { |
| 28 | + uint8_t alpha = ft_bitmap->buffer[row * ft_bitmap->width + width]; |
| 29 | + |
| 30 | + struct wob_color background = wob_color_from_argb8888(pixels[width]); |
| 31 | + struct wob_color foreground = font_color; |
| 32 | + |
| 33 | + foreground.a = alpha / 255.0f; |
| 34 | + foreground = wob_color_premultiply_alpha(foreground); |
| 35 | + |
| 36 | + struct wob_color result = wob_color_blend_premultiplied(foreground, background); |
| 37 | + |
| 38 | + pixels[width] = wob_color_to_argb(result); |
| 39 | + } |
| 40 | + pixels += stride; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct wob_font_manager * |
| 45 | +wob_font_manager_create() |
| 46 | +{ |
| 47 | + struct wob_font_manager *manager = malloc(sizeof(struct wob_font_manager)); |
| 48 | + |
| 49 | + FT_Init_FreeType(&library); |
| 50 | + |
| 51 | + wl_list_init(&manager->fonts); |
| 52 | + |
| 53 | + return manager; |
| 54 | +} |
| 55 | + |
| 56 | +void |
| 57 | +wob_font_manager_destroy(struct wob_font_manager *manager) |
| 58 | +{ |
| 59 | + struct wob_font *font, *font_tmp; |
| 60 | + wl_list_for_each_safe (font, font_tmp, &manager->fonts, link) { |
| 61 | + free(font->name); |
| 62 | + FT_Done_Face(font->data); |
| 63 | + |
| 64 | + free(font); |
| 65 | + } |
| 66 | + |
| 67 | + FT_Done_FreeType(library); |
| 68 | +} |
| 69 | + |
| 70 | +void |
| 71 | +wob_font_manager_load_font(struct wob_font_manager *manager, const char *fpath) |
| 72 | +{ |
| 73 | + struct wob_font *font = calloc(1, sizeof(struct wob_font)); |
| 74 | + font->name = strdup(fpath); |
| 75 | + FT_New_Face(library, fpath, 0, &font->data); |
| 76 | + |
| 77 | + wl_list_insert(&manager->fonts, &font->link); |
| 78 | +} |
| 79 | + |
| 80 | +struct wob_font * |
| 81 | +wob_font_manager_get(struct wob_font_manager *manager, const char *fpath) |
| 82 | +{ |
| 83 | + struct wob_font *font; |
| 84 | + wl_list_for_each (font, &manager->fonts, link) { |
| 85 | + if (strcmp(font->name, fpath) == 0) { |
| 86 | + return font; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return NULL; |
| 91 | +} |
| 92 | + |
| 93 | +struct wob_font_text_dimensions |
| 94 | +wob_font_render_text_dimensions(struct wob_font *font, char *text, int font_size) |
| 95 | +{ |
| 96 | + struct wob_font_text_dimensions dimensions = {.h = 0, .w = 0}; |
| 97 | + |
| 98 | + FT_Face ft_face = font->data; |
| 99 | + FT_Set_Pixel_Sizes(ft_face, 0, font_size); |
| 100 | + FT_UInt previous = 0; |
| 101 | + |
| 102 | + bool has_kerning = FT_HAS_KERNING(ft_face); |
| 103 | + |
| 104 | + for (char *c = text; *c != '\0'; c += 1) { |
| 105 | + FT_UInt glyph_index = FT_Get_Char_Index(ft_face, *c); |
| 106 | + FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_DEFAULT); |
| 107 | + |
| 108 | + FT_Glyph glyph; |
| 109 | + FT_Get_Glyph(ft_face->glyph, &glyph); |
| 110 | + |
| 111 | + FT_BBox glyph_bbox; |
| 112 | + FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_pixels, &glyph_bbox); |
| 113 | + FT_Vector delta; |
| 114 | + |
| 115 | + dimensions.h = glyph_bbox.yMax; |
| 116 | + dimensions.w += glyph_bbox.xMax; |
| 117 | + |
| 118 | + if (has_kerning && previous != 0) { |
| 119 | + FT_Get_Kerning( ft_face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); |
| 120 | + dimensions.w += delta.x / 64; |
| 121 | + } |
| 122 | + |
| 123 | + previous = glyph_index; |
| 124 | + } |
| 125 | + |
| 126 | + return dimensions; |
| 127 | +} |
| 128 | + |
| 129 | +void |
| 130 | +wob_font_render_text(struct wob_font *font, char *text, int font_size, struct wob_color font_color, uint32_t *argb8888_buffer, size_t argb8888_buffer_size) |
| 131 | +{ |
| 132 | + FT_Face ft_face = font->data; |
| 133 | + FT_UInt previous = 0; |
| 134 | + FT_Set_Pixel_Sizes(ft_face, 0, font_size); |
| 135 | + |
| 136 | + bool has_kerning = FT_HAS_KERNING(ft_face); |
| 137 | + |
| 138 | + for (const char *c = text; *c != '\0'; c += 1) { |
| 139 | + FT_Vector delta; |
| 140 | + FT_UInt glyph_index = FT_Get_Char_Index(ft_face, *c); |
| 141 | + FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_DEFAULT); |
| 142 | + FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL); |
| 143 | + |
| 144 | + draw_glyph(argb8888_buffer, argb8888_buffer_size, &ft_face->glyph->bitmap, font_color); |
| 145 | + |
| 146 | + argb8888_buffer += ft_face->glyph->advance.x / 64; |
| 147 | + if (has_kerning && previous != 0) { |
| 148 | + FT_Get_Kerning( ft_face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); |
| 149 | + argb8888_buffer += delta.x / 64; |
| 150 | + } |
| 151 | + |
| 152 | + previous = glyph_index; |
| 153 | + } |
| 154 | +} |
0 commit comments