initial commit
[retroray] / src / font.c
1 /*
2 Deep Runner - 6dof shooter game for the SGI O2.
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include "font.h"
19
20 int load_font(struct font *font, const char *fname)
21 {
22         if(!(font->dtxfont = dtx_open_font_glyphmap(fname))) {
23                 fprintf(stderr, "failed to load font: %s\n", fname);
24                 return -1;
25         }
26         font->size = dtx_get_glyphmap_ptsize(dtx_get_glyphmap(font->dtxfont, 0));
27         dtx_use_font(font->dtxfont, font->size);
28         font->height = dtx_line_height();
29         font->baseline = font->height * 0.2;
30         return 0;
31 }
32
33 void destroy_font(struct font *font)
34 {
35         if(!font) return;
36
37         dtx_close_font(font->dtxfont);
38 }
39
40 void use_font(struct font *font)
41 {
42         dtx_use_font(font->dtxfont, font->size);
43 }
44
45 float font_strwidth(struct font *font, const char *str)
46 {
47         dtx_use_font(font->dtxfont, font->size);
48         return dtx_string_width(str);
49 }