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 32
12#define CELL_WIDTH 32
13#define GLYPH_HEIGHT 24
14
15extern const uint8_t _binary_font2x_png_start[];
16extern const uint8_t _binary_icons2x_png_start[];
17
18struct GUIFont {
19 vita2d_texture* tex;
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->tex = vita2d_load_PNG_buffer(_binary_font2x_png_start);
29 font->icons = vita2d_load_PNG_buffer(_binary_icons2x_png_start);
30 return font;
31}
32
33void GUIFontDestroy(struct GUIFont* font) {
34 vita2d_free_texture(font->tex);
35 vita2d_free_texture(font->icons);
36 free(font);
37}
38
39unsigned GUIFontHeight(const struct GUIFont* font) {
40 UNUSED(font);
41 return GLYPH_HEIGHT;
42}
43
44unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
45 UNUSED(font);
46 if (glyph > 0x7F) {
47 glyph = '?';
48 }
49 return defaultFontMetrics[glyph].width * 2;
50}
51
52void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
53 UNUSED(font);
54 if (icon >= GUI_ICON_MAX) {
55 if (w) {
56 *w = 0;
57 }
58 if (h) {
59 *h = 0;
60 }
61 } else {
62 if (w) {
63 *w = defaultIconMetrics[icon].width * 2;
64 }
65 if (h) {
66 *h = defaultIconMetrics[icon].height * 2;
67 }
68 }
69}
70
71void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
72 if (glyph > 0x7F) {
73 glyph = '?';
74 }
75 struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
76 vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top * 2,
77 (glyph & 15) * CELL_WIDTH + metric.padding.left * 2,
78 (glyph >> 4) * CELL_HEIGHT + metric.padding.top * 2,
79 CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2,
80 CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2,
81 color);
82}
83
84void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
85 if (icon >= GUI_ICON_MAX) {
86 return;
87 }
88 struct GUIIconMetric metric = defaultIconMetrics[icon];
89 switch (align & GUI_ALIGN_HCENTER) {
90 case GUI_ALIGN_HCENTER:
91 x -= metric.width;
92 break;
93 case GUI_ALIGN_RIGHT:
94 x -= metric.width * 2;
95 break;
96 }
97 switch (align & GUI_ALIGN_VCENTER) {
98 case GUI_ALIGN_VCENTER:
99 y -= metric.height;
100 break;
101 case GUI_ALIGN_BOTTOM:
102 y -= metric.height * 2;
103 break;
104 }
105
106 switch (orient) {
107 case GUI_ORIENT_HMIRROR:
108 vita2d_draw_texture_tint_part_scale(font->icons, x + metric.width * 2, y,
109 metric.x * 2, metric.y * 2,
110 metric.width * 2, metric.height * 2,
111 -1, 1, color);
112 return;
113 case GUI_ORIENT_VMIRROR:
114 vita2d_draw_texture_tint_part_scale(font->icons, x, y + metric.height * 2,
115 metric.x * 2, metric.y * 2,
116 metric.width * 2, metric.height * 2,
117 1, -1, color);
118 return;
119 case GUI_ORIENT_0:
120 default:
121 // TOOD: Rotate
122 vita2d_draw_texture_tint_part(font->icons, x, y,
123 metric.x * 2, metric.y * 2,
124 metric.width * 2, metric.height * 2,
125 color);
126 break;
127 }
128}
129
130void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
131 if (icon >= GUI_ICON_MAX) {
132 return;
133 }
134 struct GUIIconMetric metric = defaultIconMetrics[icon];
135 vita2d_draw_texture_tint_part_scale(font->icons, x, y,
136 metric.x * 2, metric.y * 2,
137 metric.width * 2, metric.height * 2,
138 w ? (w / (float) metric.width) : 1, h ? (h / (float) metric.height) : 1, color);
139}