all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

mGBA Game Boy Advance Emulator

src/util/gui/font.c (view raw)

 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 "util/gui/font.h"
 7
 8#include "util/string.h"
 9
10unsigned GUIFontSpanWidth(const struct GUIFont* font, const char* text) {
11	unsigned width = 0;
12	size_t len = strlen(text);
13	while (len) {
14		uint32_t c = utf8Char(&text, &len);
15		if (c == '\1') {
16			c = utf8Char(&text, &len);
17			if (c < GUI_ICON_MAX) {
18				unsigned w;
19				GUIFontIconMetrics(font, c, &w, 0);
20				width += w;
21			}
22		} else {
23			width += GUIFontGlyphWidth(font, c);
24		}
25	}
26	return width;
27}
28
29void GUIFontPrint(const struct GUIFont* font, int x, int y, enum GUIAlignment align, uint32_t color, const char* text) {
30	switch (align & GUI_ALIGN_HCENTER) {
31	case GUI_ALIGN_HCENTER:
32		x -= GUIFontSpanWidth(font, text) / 2;
33		break;
34	case GUI_ALIGN_RIGHT:
35		x -= GUIFontSpanWidth(font, text);
36		break;
37	default:
38		break;
39	}
40	size_t len = strlen(text);
41	while (len) {
42		uint32_t c = utf8Char(&text, &len);
43		if (c == '\1') {
44			c = utf8Char(&text, &len);
45			if (c < GUI_ICON_MAX) {
46				GUIFontDrawIcon(font, x, y, GUI_ALIGN_BOTTOM, GUI_ORIENT_0, color, c);
47				unsigned w;
48				GUIFontIconMetrics(font, c, &w, 0);
49				x += w;
50			}
51		} else {
52			GUIFontDrawGlyph(font, x, y, color, c);
53			x += GUIFontGlyphWidth(font, c);
54		}
55	}
56}
57
58void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUIAlignment align, uint32_t color, const char* text, ...) {
59	char buffer[256];
60	va_list args;
61	va_start(args, text);
62	vsnprintf(buffer, sizeof(buffer), text, args);
63	va_end(args);
64	GUIFontPrint(font, x, y, align, color, buffer);
65}