comparison src/libmlk-core/core/font.c @ 320:8f9937403749

misc: improve loading of data
author David Demelier <markand@malikania.fr>
date Fri, 01 Oct 2021 20:30:00 +0200
parents libmlk-core/core/font.c@d01e83210ca2
children 979960e65f76
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * font.c -- basic font management
3 *
4 * Copyright (c) 2020-2021 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 "error.h"
26 #include "font.h"
27 #include "texture_p.h"
28 #include "util.h"
29
30 int
31 font_open(struct font *font, const char *path, unsigned int size)
32 {
33 assert(font);
34 assert(path);
35
36 if (!(font->handle = TTF_OpenFont(path, size)))
37 return errorf("%s", SDL_GetError());
38
39 return 0;
40 }
41
42 int
43 font_openmem(struct font *font, const void *buffer, size_t buflen, unsigned int size)
44 {
45 assert(font);
46 assert(buffer);
47
48 SDL_RWops *ops;
49
50 if (!(ops = SDL_RWFromConstMem(buffer, buflen)) ||
51 (!(font->handle = TTF_OpenFontRW(ops, 1, size))))
52 return errorf("%s", SDL_GetError());
53
54 return 0;
55 }
56
57 int
58 font_ok(const struct font *font)
59 {
60 return font && font->handle;
61 }
62
63 int
64 font_render(struct font *font, struct texture *tex, const char *text, unsigned int color)
65 {
66 assert(font_ok(font));
67 assert(text);
68
69 SDL_Color fg = {
70 .r = COLOR_R(color),
71 .g = COLOR_G(color),
72 .b = COLOR_B(color),
73 .a = COLOR_A(color)
74 };
75 SDL_Surface *surface;
76 SDL_Surface *(*func)(TTF_Font *, const char *, SDL_Color);
77
78 switch (font->style) {
79 case FONT_STYLE_ANTIALIASED:
80 func = TTF_RenderUTF8_Blended;
81 break;
82 default:
83 func = TTF_RenderUTF8_Solid;
84 break;
85 }
86
87 if (!(surface = func(font->handle, text, fg)))
88 return errorf("%s", SDL_GetError());
89
90 return texture_from_surface(tex, surface);
91 }
92
93 unsigned int
94 font_height(const struct font *font)
95 {
96 assert(font_ok(font));
97
98 return TTF_FontHeight(font->handle);
99 }
100
101 int
102 font_query(const struct font *font, const char *text, unsigned int *w, unsigned int *h)
103 {
104 assert(font_ok(font));
105 assert(text);
106
107 if (w)
108 *w = 0;
109 if (h)
110 *h = 0;
111
112 if (TTF_SizeUTF8(font->handle, text, (int *)w, (int *)h) != 0)
113 return errorf("%s", SDL_GetError());
114
115 return 0;
116 }
117
118 void
119 font_finish(struct font *font)
120 {
121 assert(font);
122
123 if (font->handle)
124 TTF_CloseFont(font->handle);
125
126 memset(font, 0, sizeof (*font));
127 }