1 | #include <stdio.h> |
2 | #include <stdbool.h> |
3 | #include <stdint.h> |
4 | #include <assert.h> |
5 | #include <math.h> |
6 | #include <ft2build.h> |
7 | #include <freetype/ftlcdfil.h> |
8 | #include <freetype/ftoutln.h> |
9 | #include FT_FREETYPE_H |
10 | |
11 | #ifdef _WIN32 |
12 | #include <windows.h> |
13 | #include "utfconv.h" |
14 | #endif |
15 | |
16 | #include "renderer.h" |
17 | #include "renwindow.h" |
18 | |
19 | #define MAX_GLYPHSET 256 |
20 | #define MAX_LOADABLE_GLYPHSETS 1024 |
21 | #define SUBPIXEL_BITMAPS_CACHED 3 |
22 | |
23 | RenWindow window_renderer = {0}; |
24 | static FT_Library library; |
25 | |
26 | // draw_rect_surface is used as a 1x1 surface to simplify ren_draw_rect with blending |
27 | static SDL_Surface *draw_rect_surface; |
28 | |
29 | static void* check_alloc(void *ptr) { |
30 | if (!ptr) { |
31 | fprintf(stderr, "Fatal error: memory allocation failed\n" ); |
32 | exit(EXIT_FAILURE); |
33 | } |
34 | return ptr; |
35 | } |
36 | |
37 | /************************* Fonts *************************/ |
38 | |
39 | typedef struct { |
40 | unsigned short x0, x1, y0, y1, loaded; |
41 | short bitmap_left, bitmap_top; |
42 | float xadvance; |
43 | } GlyphMetric; |
44 | |
45 | typedef struct { |
46 | SDL_Surface* surface; |
47 | GlyphMetric metrics[MAX_GLYPHSET]; |
48 | } GlyphSet; |
49 | |
50 | typedef struct RenFont { |
51 | FT_Face face; |
52 | GlyphSet* sets[SUBPIXEL_BITMAPS_CACHED][MAX_LOADABLE_GLYPHSETS]; |
53 | float size, space_advance, tab_advance; |
54 | unsigned short max_height, baseline, height; |
55 | ERenFontAntialiasing antialiasing; |
56 | ERenFontHinting hinting; |
57 | unsigned char style; |
58 | unsigned short underline_thickness; |
59 | #ifdef _WIN32 |
60 | unsigned char *file; |
61 | HANDLE file_handle; |
62 | #endif |
63 | char path[]; |
64 | } RenFont; |
65 | |
66 | static const char* utf8_to_codepoint(const char *p, unsigned *dst) { |
67 | const unsigned char *up = (unsigned char*)p; |
68 | unsigned res, n; |
69 | switch (*p & 0xf0) { |
70 | case 0xf0 : res = *up & 0x07; n = 3; break; |
71 | case 0xe0 : res = *up & 0x0f; n = 2; break; |
72 | case 0xd0 : |
73 | case 0xc0 : res = *up & 0x1f; n = 1; break; |
74 | default : res = *up; n = 0; break; |
75 | } |
76 | while (n--) { |
77 | res = (res << 6) | (*(++up) & 0x3f); |
78 | } |
79 | *dst = res; |
80 | return (const char*)up + 1; |
81 | } |
82 | |
83 | static int font_set_load_options(RenFont* font) { |
84 | int load_target = font->antialiasing == FONT_ANTIALIASING_NONE ? FT_LOAD_TARGET_MONO |
85 | : (font->hinting == FONT_HINTING_SLIGHT ? FT_LOAD_TARGET_LIGHT : FT_LOAD_TARGET_NORMAL); |
86 | int hinting = font->hinting == FONT_HINTING_NONE ? FT_LOAD_NO_HINTING : FT_LOAD_FORCE_AUTOHINT; |
87 | return load_target | hinting; |
88 | } |
89 | |
90 | static int font_set_render_options(RenFont* font) { |
91 | if (font->antialiasing == FONT_ANTIALIASING_NONE) |
92 | return FT_RENDER_MODE_MONO; |
93 | if (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL) { |
94 | unsigned char weights[] = { 0x10, 0x40, 0x70, 0x40, 0x10 } ; |
95 | switch (font->hinting) { |
96 | case FONT_HINTING_NONE: FT_Library_SetLcdFilter(library, FT_LCD_FILTER_NONE); break; |
97 | case FONT_HINTING_SLIGHT: |
98 | case FONT_HINTING_FULL: FT_Library_SetLcdFilterWeights(library, weights); break; |
99 | } |
100 | return FT_RENDER_MODE_LCD; |
101 | } else { |
102 | switch (font->hinting) { |
103 | case FONT_HINTING_NONE: return FT_RENDER_MODE_NORMAL; break; |
104 | case FONT_HINTING_SLIGHT: return FT_RENDER_MODE_LIGHT; break; |
105 | case FONT_HINTING_FULL: return FT_RENDER_MODE_LIGHT; break; |
106 | } |
107 | } |
108 | return 0; |
109 | } |
110 | |
111 | static int font_set_style(FT_Outline* outline, int x_translation, unsigned char style) { |
112 | FT_Outline_Translate(outline, x_translation, 0 ); |
113 | if (style & FONT_STYLE_SMOOTH) |
114 | FT_Outline_Embolden(outline, 1 << 5); |
115 | if (style & FONT_STYLE_BOLD) |
116 | FT_Outline_EmboldenXY(outline, 1 << 5, 0); |
117 | if (style & FONT_STYLE_ITALIC) { |
118 | FT_Matrix matrix = { 1 << 16, 1 << 14, 0, 1 << 16 }; |
119 | FT_Outline_Transform(outline, &matrix); |
120 | } |
121 | return 0; |
122 | } |
123 | |
124 | static void font_load_glyphset(RenFont* font, int idx) { |
125 | unsigned int render_option = font_set_render_options(font), load_option = font_set_load_options(font); |
126 | int bitmaps_cached = font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? SUBPIXEL_BITMAPS_CACHED : 1; |
127 | unsigned int byte_width = font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 3 : 1; |
128 | for (int j = 0, pen_x = 0; j < bitmaps_cached; ++j) { |
129 | GlyphSet* set = check_alloc(calloc(1, sizeof(GlyphSet))); |
130 | font->sets[j][idx] = set; |
131 | for (int i = 0; i < MAX_GLYPHSET; ++i) { |
132 | int glyph_index = FT_Get_Char_Index(font->face, i + idx * MAX_GLYPHSET); |
133 | if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option | FT_LOAD_BITMAP_METRICS_ONLY) |
134 | || font_set_style(&font->face->glyph->outline, j * (64 / SUBPIXEL_BITMAPS_CACHED), font->style) || FT_Render_Glyph(font->face->glyph, render_option)) { |
135 | continue; |
136 | } |
137 | FT_GlyphSlot slot = font->face->glyph; |
138 | int glyph_width = slot->bitmap.width / byte_width; |
139 | if (font->antialiasing == FONT_ANTIALIASING_NONE) |
140 | glyph_width *= 8; |
141 | set->metrics[i] = (GlyphMetric){ pen_x, pen_x + glyph_width, 0, slot->bitmap.rows, true, slot->bitmap_left, slot->bitmap_top, (slot->advance.x + slot->lsb_delta - slot->rsb_delta) / 64.0f}; |
142 | pen_x += glyph_width; |
143 | font->max_height = slot->bitmap.rows > font->max_height ? slot->bitmap.rows : font->max_height; |
144 | // In order to fix issues with monospacing; we need the unhinted xadvance; as FreeType doesn't correctly report the hinted advance for spaces on monospace fonts (like RobotoMono). See #843. |
145 | if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, (load_option | FT_LOAD_BITMAP_METRICS_ONLY | FT_LOAD_NO_HINTING) & ~FT_LOAD_FORCE_AUTOHINT) |
146 | || font_set_style(&font->face->glyph->outline, j * (64 / SUBPIXEL_BITMAPS_CACHED), font->style) || FT_Render_Glyph(font->face->glyph, render_option)) { |
147 | continue; |
148 | } |
149 | slot = font->face->glyph; |
150 | set->metrics[i].xadvance = slot->advance.x / 64.0f; |
151 | } |
152 | if (pen_x == 0) |
153 | continue; |
154 | set->surface = check_alloc(SDL_CreateRGBSurface(0, pen_x, font->max_height, font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 24 : 8, 0, 0, 0, 0)); |
155 | uint8_t* pixels = set->surface->pixels; |
156 | for (int i = 0; i < MAX_GLYPHSET; ++i) { |
157 | int glyph_index = FT_Get_Char_Index(font->face, i + idx * MAX_GLYPHSET); |
158 | if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option)) |
159 | continue; |
160 | FT_GlyphSlot slot = font->face->glyph; |
161 | font_set_style(&slot->outline, (64 / bitmaps_cached) * j, font->style); |
162 | if (FT_Render_Glyph(slot, render_option)) |
163 | continue; |
164 | for (unsigned int line = 0; line < slot->bitmap.rows; ++line) { |
165 | int target_offset = set->surface->pitch * line + set->metrics[i].x0 * byte_width; |
166 | int source_offset = line * slot->bitmap.pitch; |
167 | if (font->antialiasing == FONT_ANTIALIASING_NONE) { |
168 | for (unsigned int column = 0; column < slot->bitmap.width; ++column) { |
169 | int current_source_offset = source_offset + (column / 8); |
170 | int source_pixel = slot->bitmap.buffer[current_source_offset]; |
171 | pixels[++target_offset] = ((source_pixel >> (7 - (column % 8))) & 0x1) << 7; |
172 | } |
173 | } else |
174 | memcpy(&pixels[target_offset], &slot->bitmap.buffer[source_offset], slot->bitmap.width); |
175 | } |
176 | } |
177 | } |
178 | } |
179 | |
180 | static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int subpixel_idx) { |
181 | int idx = (codepoint >> 8) % MAX_LOADABLE_GLYPHSETS; |
182 | if (!font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx]) |
183 | font_load_glyphset(font, idx); |
184 | return font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx]; |
185 | } |
186 | |
187 | static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) { |
188 | if (!metric) { |
189 | return NULL; |
190 | } |
191 | if (bitmap_index < 0) |
192 | bitmap_index += SUBPIXEL_BITMAPS_CACHED; |
193 | for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { |
194 | *set = font_get_glyphset(fonts[i], codepoint, bitmap_index); |
195 | *metric = &(*set)->metrics[codepoint % 256]; |
196 | if ((*metric)->loaded || codepoint < 0xFF) |
197 | return fonts[i]; |
198 | } |
199 | if (*metric && !(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1) |
200 | return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index); |
201 | return fonts[0]; |
202 | } |
203 | |
204 | static void font_clear_glyph_cache(RenFont* font) { |
205 | for (int i = 0; i < SUBPIXEL_BITMAPS_CACHED; ++i) { |
206 | for (int j = 0; j < MAX_LOADABLE_GLYPHSETS; ++j) { |
207 | if (font->sets[i][j]) { |
208 | if (font->sets[i][j]->surface) |
209 | SDL_FreeSurface(font->sets[i][j]->surface); |
210 | free(font->sets[i][j]); |
211 | font->sets[i][j] = NULL; |
212 | } |
213 | } |
214 | } |
215 | } |
216 | |
217 | RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style) { |
218 | FT_Face face = NULL; |
219 | |
220 | #ifdef _WIN32 |
221 | |
222 | HANDLE file = INVALID_HANDLE_VALUE; |
223 | DWORD read; |
224 | int font_file_len = 0; |
225 | unsigned char *font_file = NULL; |
226 | wchar_t *wpath = NULL; |
227 | |
228 | if ((wpath = utfconv_utf8towc(path)) == NULL) |
229 | return NULL; |
230 | |
231 | if ((file = CreateFileW(wpath, |
232 | GENERIC_READ, |
233 | FILE_SHARE_READ, // or else we can't copy fonts |
234 | NULL, |
235 | OPEN_EXISTING, |
236 | FILE_ATTRIBUTE_NORMAL, |
237 | NULL)) == INVALID_HANDLE_VALUE) |
238 | goto failure; |
239 | |
240 | if ((font_file_len = GetFileSize(file, NULL)) == INVALID_FILE_SIZE) |
241 | goto failure; |
242 | |
243 | font_file = check_alloc(malloc(font_file_len * sizeof(unsigned char))); |
244 | if (!ReadFile(file, font_file, font_file_len, &read, NULL) || read != font_file_len) |
245 | goto failure; |
246 | |
247 | free(wpath); |
248 | wpath = NULL; |
249 | |
250 | if (FT_New_Memory_Face(library, font_file, read, 0, &face)) |
251 | goto failure; |
252 | |
253 | #else |
254 | |
255 | if (FT_New_Face(library, path, 0, &face)) |
256 | return NULL; |
257 | |
258 | #endif |
259 | |
260 | const int surface_scale = renwin_get_surface(window_renderer).scale; |
261 | if (FT_Set_Pixel_Sizes(face, 0, (int)(size*surface_scale))) |
262 | goto failure; |
263 | int len = strlen(path); |
264 | RenFont* font = check_alloc(calloc(1, sizeof(RenFont) + len + 1)); |
265 | strcpy(font->path, path); |
266 | font->face = face; |
267 | font->size = size; |
268 | font->height = (short)((face->height / (float)face->units_per_EM) * font->size); |
269 | font->baseline = (short)((face->ascender / (float)face->units_per_EM) * font->size); |
270 | font->antialiasing = antialiasing; |
271 | font->hinting = hinting; |
272 | font->style = style; |
273 | |
274 | #ifdef _WIN32 |
275 | // we need to keep this for freetype |
276 | font->file = font_file; |
277 | font->file_handle = file; |
278 | #endif |
279 | |
280 | if(FT_IS_SCALABLE(face)) |
281 | font->underline_thickness = (unsigned short)((face->underline_thickness / (float)face->units_per_EM) * font->size); |
282 | if(!font->underline_thickness) font->underline_thickness = ceil((double) font->height / 14.0); |
283 | |
284 | if (FT_Load_Char(face, ' ', font_set_load_options(font))) { |
285 | free(font); |
286 | goto failure; |
287 | } |
288 | font->space_advance = face->glyph->advance.x / 64.0f; |
289 | font->tab_advance = font->space_advance * 2; |
290 | return font; |
291 | |
292 | failure: |
293 | #ifdef _WIN32 |
294 | free(wpath); |
295 | free(font_file); |
296 | if (file != INVALID_HANDLE_VALUE) CloseHandle(file); |
297 | #endif |
298 | if (face != NULL) |
299 | FT_Done_Face(face); |
300 | return NULL; |
301 | } |
302 | |
303 | RenFont* ren_font_copy(RenWindow *window_renderer, RenFont* font, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, int style) { |
304 | antialiasing = antialiasing == -1 ? font->antialiasing : antialiasing; |
305 | hinting = hinting == -1 ? font->hinting : hinting; |
306 | style = style == -1 ? font->style : style; |
307 | |
308 | return ren_font_load(window_renderer, font->path, size, antialiasing, hinting, style); |
309 | } |
310 | |
311 | const char* ren_font_get_path(RenFont *font) { |
312 | return font->path; |
313 | } |
314 | |
315 | void ren_font_free(RenFont* font) { |
316 | font_clear_glyph_cache(font); |
317 | FT_Done_Face(font->face); |
318 | #ifdef _WIN32 |
319 | free(font->file); |
320 | CloseHandle(font->file_handle); |
321 | #endif |
322 | free(font); |
323 | } |
324 | |
325 | void ren_font_group_set_tab_size(RenFont **fonts, int n) { |
326 | for (int j = 0; j < FONT_FALLBACK_MAX && fonts[j]; ++j) { |
327 | for (int i = 0; i < (fonts[j]->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? SUBPIXEL_BITMAPS_CACHED : 1); ++i) |
328 | font_get_glyphset(fonts[j], '\t', i)->metrics['\t'].xadvance = fonts[j]->space_advance * n; |
329 | } |
330 | } |
331 | |
332 | int ren_font_group_get_tab_size(RenFont **fonts) { |
333 | float advance = font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance; |
334 | if (fonts[0]->space_advance) { |
335 | advance /= fonts[0]->space_advance; |
336 | } |
337 | return advance; |
338 | } |
339 | |
340 | float ren_font_group_get_size(RenFont **fonts) { |
341 | return fonts[0]->size; |
342 | } |
343 | |
344 | void ren_font_group_set_size(RenWindow *window_renderer, RenFont **fonts, float size) { |
345 | const int surface_scale = renwin_get_surface(window_renderer).scale; |
346 | for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { |
347 | font_clear_glyph_cache(fonts[i]); |
348 | FT_Face face = fonts[i]->face; |
349 | FT_Set_Pixel_Sizes(face, 0, (int)(size*surface_scale)); |
350 | fonts[i]->size = size; |
351 | fonts[i]->height = (short)((face->height / (float)face->units_per_EM) * size); |
352 | fonts[i]->baseline = (short)((face->ascender / (float)face->units_per_EM) * size); |
353 | FT_Load_Char(face, ' ', font_set_load_options(fonts[i])); |
354 | fonts[i]->space_advance = face->glyph->advance.x / 64.0f; |
355 | fonts[i]->tab_advance = fonts[i]->space_advance * 2; |
356 | } |
357 | } |
358 | |
359 | int ren_font_group_get_height(RenFont **fonts) { |
360 | return fonts[0]->height; |
361 | } |
362 | |
363 | double ren_font_group_get_width(RenWindow *window_renderer, RenFont **fonts, const char *text, size_t len) { |
364 | double width = 0; |
365 | const char* end = text + len; |
366 | GlyphMetric* metric = NULL; GlyphSet* set = NULL; |
367 | while (text < end) { |
368 | unsigned int codepoint; |
369 | text = utf8_to_codepoint(text, &codepoint); |
370 | RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, 0); |
371 | if (!metric) |
372 | break; |
373 | width += (!font || metric->xadvance) ? metric->xadvance : fonts[0]->space_advance; |
374 | } |
375 | const int surface_scale = renwin_get_surface(window_renderer).scale; |
376 | return width / surface_scale; |
377 | } |
378 | |
379 | double ren_draw_text(RenSurface *rs, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) { |
380 | SDL_Surface *surface = rs->surface; |
381 | SDL_Rect clip; |
382 | SDL_GetClipRect(surface, &clip); |
383 | |
384 | const int surface_scale = rs->scale; |
385 | double pen_x = x * surface_scale; |
386 | y *= surface_scale; |
387 | int bytes_per_pixel = surface->format->BytesPerPixel; |
388 | const char* end = text + len; |
389 | uint8_t* destination_pixels = surface->pixels; |
390 | int clip_end_x = clip.x + clip.w, clip_end_y = clip.y + clip.h; |
391 | |
392 | RenFont* last = NULL; |
393 | double last_pen_x = x; |
394 | bool underline = fonts[0]->style & FONT_STYLE_UNDERLINE; |
395 | bool strikethrough = fonts[0]->style & FONT_STYLE_STRIKETHROUGH; |
396 | |
397 | while (text < end) { |
398 | unsigned int codepoint, r, g, b; |
399 | text = utf8_to_codepoint(text, &codepoint); |
400 | GlyphSet* set = NULL; GlyphMetric* metric = NULL; |
401 | RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED)); |
402 | if (!metric) |
403 | break; |
404 | int start_x = floor(pen_x) + metric->bitmap_left; |
405 | int end_x = (metric->x1 - metric->x0) + start_x; |
406 | int glyph_end = metric->x1, glyph_start = metric->x0; |
407 | if (!metric->loaded && codepoint > 0xFF) |
408 | ren_draw_rect(rs, (RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color); |
409 | if (set->surface && color.a > 0 && end_x >= clip.x && start_x < clip_end_x) { |
410 | uint8_t* source_pixels = set->surface->pixels; |
411 | for (int line = metric->y0; line < metric->y1; ++line) { |
412 | int target_y = line + y - metric->bitmap_top + font->baseline * surface_scale; |
413 | if (target_y < clip.y) |
414 | continue; |
415 | if (target_y >= clip_end_y) |
416 | break; |
417 | if (start_x + (glyph_end - glyph_start) >= clip_end_x) |
418 | glyph_end = glyph_start + (clip_end_x - start_x); |
419 | if (start_x < clip.x) { |
420 | int offset = clip.x - start_x; |
421 | start_x += offset; |
422 | glyph_start += offset; |
423 | } |
424 | uint32_t* destination_pixel = (uint32_t*)&(destination_pixels[surface->pitch * target_y + start_x * bytes_per_pixel]); |
425 | uint8_t* source_pixel = &source_pixels[line * set->surface->pitch + glyph_start * (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 3 : 1)]; |
426 | for (int x = glyph_start; x < glyph_end; ++x) { |
427 | uint32_t destination_color = *destination_pixel; |
428 | // the standard way of doing this would be SDL_GetRGBA, but that introduces a performance regression. needs to be investigated |
429 | SDL_Color dst = { (destination_color & surface->format->Rmask) >> surface->format->Rshift, (destination_color & surface->format->Gmask) >> surface->format->Gshift, (destination_color & surface->format->Bmask) >> surface->format->Bshift, (destination_color & surface->format->Amask) >> surface->format->Ashift }; |
430 | SDL_Color src; |
431 | |
432 | if (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL) { |
433 | src.r = *(source_pixel++); |
434 | src.g = *(source_pixel++); |
435 | } |
436 | else { |
437 | src.r = *(source_pixel); |
438 | src.g = *(source_pixel); |
439 | } |
440 | |
441 | src.b = *(source_pixel++); |
442 | src.a = 0xFF; |
443 | |
444 | r = (color.r * src.r * color.a + dst.r * (65025 - src.r * color.a) + 32767) / 65025; |
445 | g = (color.g * src.g * color.a + dst.g * (65025 - src.g * color.a) + 32767) / 65025; |
446 | b = (color.b * src.b * color.a + dst.b * (65025 - src.b * color.a) + 32767) / 65025; |
447 | // the standard way of doing this would be SDL_GetRGBA, but that introduces a performance regression. needs to be investigated |
448 | *destination_pixel++ = dst.a << surface->format->Ashift | r << surface->format->Rshift | g << surface->format->Gshift | b << surface->format->Bshift; |
449 | } |
450 | } |
451 | } |
452 | |
453 | float adv = metric->xadvance ? metric->xadvance : font->space_advance; |
454 | |
455 | if(!last) last = font; |
456 | else if(font != last || text == end) { |
457 | double local_pen_x = text == end ? pen_x + adv : pen_x; |
458 | if (underline) |
459 | ren_draw_rect(rs, (RenRect){last_pen_x, y / surface_scale + last->height - 1, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color); |
460 | if (strikethrough) |
461 | ren_draw_rect(rs, (RenRect){last_pen_x, y / surface_scale + last->height / 2, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color); |
462 | last = font; |
463 | last_pen_x = pen_x; |
464 | } |
465 | |
466 | pen_x += adv; |
467 | } |
468 | return pen_x / surface_scale; |
469 | } |
470 | |
471 | /******************* Rectangles **********************/ |
472 | static inline RenColor blend_pixel(RenColor dst, RenColor src) { |
473 | int ia = 0xff - src.a; |
474 | dst.r = ((src.r * src.a) + (dst.r * ia)) >> 8; |
475 | dst.g = ((src.g * src.a) + (dst.g * ia)) >> 8; |
476 | dst.b = ((src.b * src.a) + (dst.b * ia)) >> 8; |
477 | return dst; |
478 | } |
479 | |
480 | void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color) { |
481 | if (color.a == 0) { return; } |
482 | |
483 | SDL_Surface *surface = rs->surface; |
484 | const int surface_scale = rs->scale; |
485 | |
486 | SDL_Rect dest_rect = { rect.x * surface_scale, |
487 | rect.y * surface_scale, |
488 | rect.width * surface_scale, |
489 | rect.height * surface_scale }; |
490 | |
491 | if (color.a == 0xff) { |
492 | uint32_t translated = SDL_MapRGB(surface->format, color.r, color.g, color.b); |
493 | SDL_FillRect(surface, &dest_rect, translated); |
494 | } else { |
495 | // Seems like SDL doesn't handle clipping as we expect when using |
496 | // scaled blitting, so we "clip" manually. |
497 | SDL_Rect clip; |
498 | SDL_GetClipRect(surface, &clip); |
499 | if (!SDL_IntersectRect(&clip, &dest_rect, &dest_rect)) return; |
500 | |
501 | uint32_t *pixel = (uint32_t *)draw_rect_surface->pixels; |
502 | *pixel = SDL_MapRGBA(draw_rect_surface->format, color.r, color.g, color.b, color.a); |
503 | SDL_BlitScaled(draw_rect_surface, NULL, surface, &dest_rect); |
504 | } |
505 | } |
506 | |
507 | /*************** Window Management ****************/ |
508 | void ren_free_window_resources(RenWindow *window_renderer) { |
509 | extern uint8_t *command_buf; |
510 | extern size_t command_buf_size; |
511 | renwin_free(window_renderer); |
512 | SDL_FreeSurface(draw_rect_surface); |
513 | free(command_buf); |
514 | command_buf = NULL; |
515 | command_buf_size = 0; |
516 | } |
517 | |
518 | // TODO remove global and return RenWindow* |
519 | void ren_init(SDL_Window *win) { |
520 | assert(win); |
521 | int error = FT_Init_FreeType( &library ); |
522 | if ( error ) { |
523 | fprintf(stderr, "internal font error when starting the application\n" ); |
524 | return; |
525 | } |
526 | window_renderer.window = win; |
527 | renwin_init_surface(&window_renderer); |
528 | renwin_clip_to_surface(&window_renderer); |
529 | draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32, |
530 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); |
531 | } |
532 | |
533 | |
534 | void ren_resize_window(RenWindow *window_renderer) { |
535 | renwin_resize_surface(window_renderer); |
536 | } |
537 | |
538 | |
539 | void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count) { |
540 | static bool initial_frame = true; |
541 | if (initial_frame) { |
542 | renwin_show_window(window_renderer); |
543 | initial_frame = false; |
544 | } |
545 | renwin_update_rects(window_renderer, rects, count); |
546 | } |
547 | |
548 | |
549 | void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect) { |
550 | renwin_set_clip_rect(window_renderer, rect); |
551 | } |
552 | |
553 | |
554 | void ren_get_size(RenWindow *window_renderer, int *x, int *y) { |
555 | RenSurface rs = renwin_get_surface(window_renderer); |
556 | *x = rs.surface->w / rs.scale; |
557 | *y = rs.surface->h / rs.scale; |
558 | } |
559 | |