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