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 <mgba-util/gui/font.h>
7#include <mgba-util/gui/font-metrics.h>
8#include <mgba-util/string.h>
9
10#include <vita2d.h>
11
12#define CELL_HEIGHT 32
13#define CELL_WIDTH 32
14#define FONT_SIZE 1.25f
15
16extern const uint8_t _binary_icons2x_png_start[];
17
18struct GUIFont {
19 vita2d_pgf* pgf;
20 vita2d_texture* icons;
21};
22
23struct GUIFont* GUIFontCreate(void) {
24 struct GUIFont* font = malloc(sizeof(struct GUIFont));
25 if (!font) {
26 return 0;
27 }
28 font->pgf = vita2d_load_default_pgf();
29 font->icons = vita2d_load_PNG_buffer(_binary_icons2x_png_start);
30 return font;
31}
32
33void GUIFontDestroy(struct GUIFont* font) {
34 vita2d_free_pgf(font->pgf);
35 vita2d_free_texture(font->icons);
36 free(font);
37}
38
39unsigned GUIFontHeight(const struct GUIFont* font) {
40 return vita2d_pgf_text_height(font->pgf, FONT_SIZE, "M") + 9;
41}
42
43unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
44 char base[5] = { 0 };
45 toUtf8(glyph, base);
46 return vita2d_pgf_text_width(font->pgf, FONT_SIZE, base);
47}
48
49void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
50 UNUSED(font);
51 if (icon >= GUI_ICON_MAX) {
52 if (w) {
53 *w = 0;
54 }
55 if (h) {
56 *h = 0;
57 }
58 } else {
59 if (w) {
60 *w = defaultIconMetrics[icon].width * 2;
61 }
62 if (h) {
63 *h = defaultIconMetrics[icon].height * 2;
64 }
65 }
66}
67
68void GUIFontDrawGlyph(struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
69 char base[5] = { 0 };
70 toUtf8(glyph, base);
71 vita2d_pgf_draw_text(font->pgf, x, y, color, FONT_SIZE, base);
72}
73
74void GUIFontDrawIcon(struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
75 if (icon >= GUI_ICON_MAX) {
76 return;
77 }
78 struct GUIIconMetric metric = defaultIconMetrics[icon];
79 switch (align & GUI_ALIGN_HCENTER) {
80 case GUI_ALIGN_HCENTER:
81 x -= metric.width;
82 break;
83 case GUI_ALIGN_RIGHT:
84 x -= metric.width * 2;
85 break;
86 }
87 switch (align & GUI_ALIGN_VCENTER) {
88 case GUI_ALIGN_VCENTER:
89 y -= metric.height;
90 break;
91 case GUI_ALIGN_BOTTOM:
92 y -= metric.height * 2;
93 break;
94 }
95
96 switch (orient) {
97 case GUI_ORIENT_HMIRROR:
98 vita2d_draw_texture_tint_part_scale(font->icons, x + metric.width * 2, y,
99 metric.x * 2, metric.y * 2,
100 metric.width * 2, metric.height * 2,
101 -1, 1, color);
102 return;
103 case GUI_ORIENT_VMIRROR:
104 vita2d_draw_texture_tint_part_scale(font->icons, x, y + metric.height * 2,
105 metric.x * 2, metric.y * 2,
106 metric.width * 2, metric.height * 2,
107 1, -1, color);
108 return;
109 case GUI_ORIENT_0:
110 default:
111 // TOOD: Rotate
112 vita2d_draw_texture_tint_part(font->icons, x, y,
113 metric.x * 2, metric.y * 2,
114 metric.width * 2, metric.height * 2,
115 color);
116 break;
117 }
118}
119
120void GUIFontDrawIconSize(struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
121 if (icon >= GUI_ICON_MAX) {
122 return;
123 }
124 struct GUIIconMetric metric = defaultIconMetrics[icon];
125 vita2d_draw_texture_tint_part_scale(font->icons, x, y,
126 metric.x * 2, metric.y * 2,
127 metric.width * 2, metric.height * 2,
128 w ? (w / (float) metric.width) : 1, h ? (h / (float) metric.height) : 1, color);
129}