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
8unsigned GUIFontSpanWidth(const struct GUIFont* font, const char* text) {
9 unsigned width = 0;
10 size_t i;
11 for (i = 0; text[i]; ++i) {
12 char c = text[i];
13 width += GUIFontGlyphWidth(font, c);
14 }
15 return width;
16}
17
18void GUIFontPrint(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text) {
19 switch (align) {
20 case GUI_TEXT_CENTER:
21 x -= GUIFontSpanWidth(font, text) / 2;
22 break;
23 case GUI_TEXT_RIGHT:
24 x -= GUIFontSpanWidth(font, text);
25 break;
26 default:
27 break;
28 }
29 size_t len = strlen(text);
30 while (len) {
31 uint32_t c = utf8Char(&text, &len);
32 GUIFontDrawGlyph(font, x, y, color, c);
33 x += GUIFontGlyphWidth(font, c);
34 }
35}
36
37void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
38 char buffer[256];
39 va_list args;
40 va_start(args, text);
41 vsnprintf(buffer, sizeof(buffer), text, args);
42 va_end(args);
43 GUIFontPrint(font, x, y, align, color, buffer);
44}