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 "util/gui/font-metrics.h"
8
9#include <vita2d.h>
10
11#define CELL_HEIGHT 16
12#define CELL_WIDTH 16
13#define GLYPH_HEIGHT 12
14
15extern const uint8_t _binary_font_png_start[];
16
17struct GUIFont {
18 vita2d_texture* tex;
19};
20
21struct GUIFont* GUIFontCreate(void) {
22 struct GUIFont* font = malloc(sizeof(struct GUIFont));
23 if (!font) {
24 return 0;
25 }
26 font->tex = vita2d_load_PNG_buffer(_binary_font_png_start);
27 return font;
28}
29
30void GUIFontDestroy(struct GUIFont* font) {
31 vita2d_free_texture(font->tex);
32 free(font);
33}
34
35unsigned GUIFontHeight(const struct GUIFont* font) {
36 UNUSED(font);
37 return GLYPH_HEIGHT * 2;
38}
39
40unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
41 UNUSED(font);
42 if (glyph > 0x7F) {
43 glyph = '?';
44 }
45 return defaultFontMetrics[glyph].width * 2;
46}
47
48void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
49 if (glyph > 0x7F) {
50 glyph = '?';
51 }
52 struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
53 vita2d_draw_texture_tint_part_scale(font->tex, x, y + (-GLYPH_HEIGHT + metric.padding.top) * 2,
54 (glyph & 15) * CELL_WIDTH + metric.padding.left,
55 (glyph >> 4) * CELL_HEIGHT + metric.padding.top,
56 CELL_WIDTH - (metric.padding.left + metric.padding.right),
57 CELL_HEIGHT - (metric.padding.top + metric.padding.bottom),
58 2, 2, color);
59}