1 /* Copyright (c) 2013-2015 Jeffrey Pfau
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <mgba-util/gui/font.h>
7 #include <mgba-util/gui/font-metrics.h>
8 #include "icons.h"
9 #include "font.h"
10 
11 #include <malloc.h>
12 #include <ogc/tpl.h>
13 
14 #define GLYPH_HEIGHT 24
15 #define CELL_HEIGHT 32
16 #define CELL_WIDTH 32
17 
18 struct GUIFont {
19 	TPLFile tdf;
20 	TPLFile iconsTdf;
21 };
22 
GUIFontCreate(void)23 struct GUIFont* GUIFontCreate(void) {
24 	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
25 	if (!guiFont) {
26 		return 0;
27 	}
28 
29 	// libogc's TPL code modifies and frees this itself...
30 	void* fontTpl = memalign(32, font_size);
31 	if (!fontTpl) {
32 		free(guiFont);
33 		return 0;
34 	}
35 	memcpy(fontTpl, font, font_size);
36 	TPL_OpenTPLFromMemory(&guiFont->tdf, fontTpl, font_size);
37 
38 	void* iconsTpl = memalign(32, icons_size);
39 	if (!iconsTpl) {
40 		TPL_CloseTPLFile(&guiFont->tdf);
41 		free(guiFont);
42 		return 0;
43 	}
44 	memcpy(iconsTpl, icons, icons_size);
45 	TPL_OpenTPLFromMemory(&guiFont->iconsTdf, iconsTpl, icons_size);
46 	return guiFont;
47 }
48 
GUIFontDestroy(struct GUIFont * font)49 void GUIFontDestroy(struct GUIFont* font) {
50 	TPL_CloseTPLFile(&font->tdf);
51 	TPL_CloseTPLFile(&font->iconsTdf);
52 	free(font);
53 }
54 
GUIFontHeight(const struct GUIFont * font)55 unsigned GUIFontHeight(const struct GUIFont* font) {
56 	UNUSED(font);
57 	return GLYPH_HEIGHT;
58 }
59 
GUIFontGlyphWidth(const struct GUIFont * font,uint32_t glyph)60 unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
61 	UNUSED(font);
62 	if (glyph > 0x7F) {
63 		glyph = '?';
64 	}
65 	return defaultFontMetrics[glyph].width * 2;
66 }
67 
GUIFontIconMetrics(const struct GUIFont * font,enum GUIIcon icon,unsigned * w,unsigned * h)68 void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
69 	UNUSED(font);
70 	if (icon >= GUI_ICON_MAX) {
71 		if (w) {
72 			*w = 0;
73 		}
74 		if (h) {
75 			*h = 0;
76 		}
77 	} else {
78 		if (w) {
79 			*w = defaultIconMetrics[icon].width * 2;
80 		}
81 		if (h) {
82 			*h = defaultIconMetrics[icon].height * 2;
83 		}
84 	}
85 }
86 
GUIFontDrawGlyph(struct GUIFont * font,int x,int y,uint32_t color,uint32_t glyph)87 void GUIFontDrawGlyph(struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
88 	color = (color >> 24) | (color << 8);
89 	GXTexObj tex;
90 	TPL_GetTexture(&font->tdf, 0, &tex);
91 	GX_LoadTexObj(&tex, GX_TEXMAP0);
92 
93 	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
94 	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
95 
96 	if (glyph > 0x7F) {
97 		glyph = '?';
98 	}
99 	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
100 	s16 tx = (glyph & 15) * CELL_WIDTH + metric.padding.left * 2;
101 	s16 ty = (glyph >> 4) * CELL_HEIGHT + metric.padding.top * 2;
102 	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
103 	GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top * 2);
104 	GX_Color1u32(color);
105 	GX_TexCoord2f32(tx / 512.f, ty / 256.f);
106 
107 	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2, y - GLYPH_HEIGHT + metric.padding.top * 2);
108 	GX_Color1u32(color);
109 	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2) / 512.f, ty / 256.f);
110 
111 	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom * 2);
112 	GX_Color1u32(color);
113 	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2) / 512.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2) / 256.f);
114 
115 	GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom * 2);
116 	GX_Color1u32(color);
117 	GX_TexCoord2f32(tx / 512.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2) / 256.f);
118 	GX_End();
119 }
120 
GUIFontDrawIcon(struct GUIFont * font,int x,int y,enum GUIAlignment align,enum GUIOrientation orient,uint32_t color,enum GUIIcon icon)121 void GUIFontDrawIcon(struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
122 	if (icon >= GUI_ICON_MAX) {
123 		return;
124 	}
125 
126 	color = (color >> 24) | (color << 8);
127 	GXTexObj tex;
128 
129 	TPL_GetTexture(&font->iconsTdf, 0, &tex);
130 	GX_LoadTexObj(&tex, GX_TEXMAP0);
131 
132 	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
133 	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
134 
135 	struct GUIIconMetric metric = defaultIconMetrics[icon];
136 	switch (align & GUI_ALIGN_HCENTER) {
137 	case GUI_ALIGN_HCENTER:
138 		x -= metric.width;
139 		break;
140 	case GUI_ALIGN_RIGHT:
141 		x -= metric.width * 2;
142 		break;
143 	}
144 	switch (align & GUI_ALIGN_VCENTER) {
145 	case GUI_ALIGN_VCENTER:
146 		y -= metric.height;
147 		break;
148 	case GUI_ALIGN_BOTTOM:
149 		y -= metric.height * 2;
150 		break;
151 	}
152 
153 	float u[4];
154 	float v[4];
155 
156 	switch (orient) {
157 	case GUI_ORIENT_0:
158 	default:
159 		// TODO: Rotations
160 		u[0] = u[3] = metric.x / 256.f;
161 		u[1] = u[2] = (metric.x + metric.width) / 256.f;
162 		v[0] = v[1] = (metric.y + metric.height) / 64.f;
163 		v[2] = v[3] = metric.y / 64.f;
164 		break;
165 	case GUI_ORIENT_HMIRROR:
166 		u[0] = u[3] = (metric.x + metric.width) / 256.f;
167 		u[1] = u[2] = metric.x / 256.f;
168 		v[0] = v[1] = (metric.y + metric.height) / 64.f;
169 		v[2] = v[3] = metric.y / 64.f;
170 		break;
171 	case GUI_ORIENT_VMIRROR:
172 		u[0] = u[3] = metric.x / 256.f;
173 		u[1] = u[2] = (metric.x + metric.width) / 256.f;
174 		v[0] = v[1] = metric.y / 64.f;
175 		v[2] = v[3] = (metric.y + metric.height) / 64.f;
176 		break;
177 	}
178 
179 	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
180 	GX_Position2s16(x, y + metric.height * 2);
181 	GX_Color1u32(color);
182 	GX_TexCoord2f32(u[0], v[0]);
183 
184 	GX_Position2s16(x + metric.width * 2, y + metric.height * 2);
185 	GX_Color1u32(color);
186 	GX_TexCoord2f32(u[1], v[1]);
187 
188 	GX_Position2s16(x + metric.width * 2, y);
189 	GX_Color1u32(color);
190 	GX_TexCoord2f32(u[2], v[2]);
191 
192 	GX_Position2s16(x, y);
193 	GX_Color1u32(color);
194 	GX_TexCoord2f32(u[3], v[3]);
195 	GX_End();
196 }
197 
GUIFontDrawIconSize(struct GUIFont * font,int x,int y,int w,int h,uint32_t color,enum GUIIcon icon)198 void GUIFontDrawIconSize(struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
199 	if (icon >= GUI_ICON_MAX) {
200 		return;
201 	}
202 
203 	color = (color >> 24) | (color << 8);
204 	GXTexObj tex;
205 
206 	TPL_GetTexture(&font->iconsTdf, 0, &tex);
207 	GX_LoadTexObj(&tex, GX_TEXMAP0);
208 
209 	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
210 	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
211 
212 	struct GUIIconMetric metric = defaultIconMetrics[icon];
213 
214 	float u[4];
215 	float v[4];
216 
217 	if (!h) {
218 		h = metric.height * 2;
219 	}
220 	if (!w) {
221 		w = metric.width * 2;
222 	}
223 
224 	u[0] = u[3] = metric.x / 256.f;
225 	u[1] = u[2] = (metric.x + metric.width) / 256.f;
226 	v[0] = v[1] = (metric.y + metric.height) / 64.f;
227 	v[2] = v[3] = metric.y / 64.f;
228 
229 	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
230 	GX_Position2s16(x, y + h);
231 	GX_Color1u32(color);
232 	GX_TexCoord2f32(u[0], v[0]);
233 
234 	GX_Position2s16(x + w, y + h);
235 	GX_Color1u32(color);
236 	GX_TexCoord2f32(u[1], v[1]);
237 
238 	GX_Position2s16(x + w, y);
239 	GX_Color1u32(color);
240 	GX_TexCoord2f32(u[2], v[2]);
241 
242 	GX_Position2s16(x, y);
243 	GX_Color1u32(color);
244 	GX_TexCoord2f32(u[3], v[3]);
245 	GX_End();
246 }
247