comparison libmlk-core/mlk/core/font.c @ 431:8f59201dc76b

core: cleanup hierarchy
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2022 20:23:14 +0200
parents src/libmlk-core/core/font.c@1645433e008d
children e1eebc6bf25d
comparison
equal deleted inserted replaced
430:1645433e008d 431:8f59201dc76b
1 /*
2 * font.c -- basic font management
3 *
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <string.h>
21
22 #include <SDL_ttf.h>
23
24 #include "color.h"
25 #include "err.h"
26 #include "error.h"
27 #include "font.h"
28 #include "texture_p.h"
29 #include "util.h"
30
31 int
32 font_open(struct font *font, const char *path, unsigned int size)
33 {
34 assert(font);
35 assert(path);
36
37 if (!(font->handle = TTF_OpenFont(path, size)))
38 return ERR_SDL;
39
40 return 0;
41 }
42
43 int
44 font_openmem(struct font *font, const void *buffer, size_t buflen, unsigned int size)
45 {
46 assert(font);
47 assert(buffer);
48
49 SDL_RWops *ops;
50
51 if (!(ops = SDL_RWFromConstMem(buffer, buflen)) ||
52 (!(font->handle = TTF_OpenFontRW(ops, 1, size))))
53 return ERR_SDL;
54
55 return 0;
56 }
57
58 int
59 font_ok(const struct font *font)
60 {
61 return font && font->handle;
62 }
63
64 int
65 font_render(struct font *font, struct texture *tex, const char *text, unsigned long color)
66 {
67 assert(font_ok(font));
68 assert(text);
69
70 SDL_Color fg = {
71 .r = COLOR_R(color),
72 .g = COLOR_G(color),
73 .b = COLOR_B(color),
74 .a = COLOR_A(color)
75 };
76 SDL_Surface *surface;
77 SDL_Surface *(*func)(TTF_Font *, const char *, SDL_Color);
78
79 switch (font->style) {
80 case FONT_STYLE_ANTIALIASED:
81 func = TTF_RenderUTF8_Blended;
82 break;
83 default:
84 func = TTF_RenderUTF8_Solid;
85 break;
86 }
87
88 if (!(surface = func(font->handle, text, fg)))
89 return ERR_SDL;
90
91 return texture_from_surface(tex, surface);
92 }
93
94 unsigned int
95 font_height(const struct font *font)
96 {
97 assert(font_ok(font));
98
99 return TTF_FontHeight(font->handle);
100 }
101
102 int
103 font_query(const struct font *font, const char *text, unsigned int *w, unsigned int *h)
104 {
105 assert(font_ok(font));
106 assert(text);
107
108 if (w)
109 *w = 0;
110 if (h)
111 *h = 0;
112
113 if (TTF_SizeUTF8(font->handle, text, (int *)w, (int *)h) != 0)
114 return ERR_SDL;
115
116 return 0;
117 }
118
119 void
120 font_finish(struct font *font)
121 {
122 assert(font);
123
124 if (font->handle)
125 TTF_CloseFont(font->handle);
126
127 memset(font, 0, sizeof (*font));
128 }