all repos — mgba @ 67905d281bfecbb06f51f2ca5ac939df378734a5

mGBA Game Boy Advance Emulator

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#include "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	if (glyph > 0x7F) {
 45		glyph = '?';
 46	}
 47	char base[5] = { glyph };
 48	return vita2d_pgf_text_width(font->pgf, FONT_SIZE, base);
 49}
 50
 51void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
 52	UNUSED(font);
 53	if (icon >= GUI_ICON_MAX) {
 54		if (w) {
 55			*w = 0;
 56		}
 57		if (h) {
 58			*h = 0;
 59		}
 60	} else {
 61		if (w) {
 62			*w = defaultIconMetrics[icon].width * 2;
 63		}
 64		if (h) {
 65			*h = defaultIconMetrics[icon].height * 2;
 66		}
 67	}
 68}
 69
 70void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
 71	if (glyph > 0x7F) {
 72		glyph = '?';
 73	}
 74	char base[5] = { glyph };
 75	vita2d_pgf_draw_text(font->pgf, x, y, color, FONT_SIZE, base);
 76}
 77
 78void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
 79	if (icon >= GUI_ICON_MAX) {
 80		return;
 81	}
 82	struct GUIIconMetric metric = defaultIconMetrics[icon];
 83	switch (align & GUI_ALIGN_HCENTER) {
 84	case GUI_ALIGN_HCENTER:
 85		x -= metric.width;
 86		break;
 87	case GUI_ALIGN_RIGHT:
 88		x -= metric.width * 2;
 89		break;
 90	}
 91	switch (align & GUI_ALIGN_VCENTER) {
 92	case GUI_ALIGN_VCENTER:
 93		y -= metric.height;
 94		break;
 95	case GUI_ALIGN_BOTTOM:
 96		y -= metric.height * 2;
 97		break;
 98	}
 99
100	switch (orient) {
101	case GUI_ORIENT_HMIRROR:
102		vita2d_draw_texture_tint_part_scale(font->icons, x + metric.width * 2, y,
103		                                    metric.x * 2, metric.y * 2,
104		                                    metric.width * 2, metric.height * 2,
105		                                    -1, 1, color);
106		return;
107	case GUI_ORIENT_VMIRROR:
108		vita2d_draw_texture_tint_part_scale(font->icons, x, y + metric.height * 2,
109		                                    metric.x * 2, metric.y * 2,
110		                                    metric.width * 2, metric.height * 2,
111		                                    1, -1, color);
112		return;
113	case GUI_ORIENT_0:
114	default:
115		// TOOD: Rotate
116		vita2d_draw_texture_tint_part(font->icons, x, y,
117		                                    metric.x * 2, metric.y * 2,
118		                                    metric.width * 2, metric.height * 2,
119		                                    color);
120		break;
121	}
122}
123
124void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
125	if (icon >= GUI_ICON_MAX) {
126		return;
127	}
128	struct GUIIconMetric metric = defaultIconMetrics[icon];
129	vita2d_draw_texture_tint_part_scale(font->icons, x, y,
130	                                    metric.x * 2, metric.y * 2,
131	                                    metric.width * 2, metric.height * 2,
132	                                    w ? (w / (float) metric.width) : 1, h ? (h / (float) metric.height) : 1, color);
133}