src/platform/psp2/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 <vita2d.h>
9
10#define GLYPH_HEIGHT 11
11#define GLYPH_WIDTH 14
12#define FONT_TRACKING 10
13#define CELL_HEIGHT 16
14#define CELL_WIDTH 16
15
16extern const uint8_t _binary_font_png_start[];
17
18struct GUIFont {
19 vita2d_texture* tex;
20};
21
22struct GUIFont* GUIFontCreate(void) {
23 struct GUIFont* font = malloc(sizeof(struct GUIFont));
24 if (!font) {
25 return 0;
26 }
27 font->tex = vita2d_load_PNG_buffer(_binary_font_png_start);
28 return font;
29}
30
31void GUIFontDestroy(struct GUIFont* font) {
32 vita2d_free_texture(font->tex);
33 free(font);
34}
35
36int GUIFontHeight(const struct GUIFont* font) {
37 UNUSED(font);
38 return GLYPH_HEIGHT;
39}
40
41void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
42 UNUSED(align); // TODO
43 char buffer[256];
44 va_list args;
45 va_start(args, text);
46 int len = vsnprintf(buffer, sizeof(buffer), text, args);
47 va_end(args);
48 int i;
49 for (i = 0; i < len; ++i) {
50 char c = buffer[i];
51 if (c > 0x7F) {
52 c = 0;
53 }
54 vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT,
55 (c & 15) * CELL_WIDTH + ((CELL_WIDTH - GLYPH_WIDTH) >> 1),
56 (c >> 4) * CELL_HEIGHT + ((CELL_HEIGHT - GLYPH_HEIGHT) >> 1),
57 GLYPH_WIDTH, GLYPH_HEIGHT, color);
58 x += FONT_TRACKING;
59 }
60}