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