2015-07-28 07:59:28 +00:00
|
|
|
/* Copyright (c) 2013-2015 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "util/gui/font.h"
|
2015-08-23 07:57:34 +00:00
|
|
|
#include "util/gui/font-metrics.h"
|
2015-07-28 07:59:28 +00:00
|
|
|
|
|
|
|
#include <vita2d.h>
|
|
|
|
|
2016-01-06 05:11:31 +00:00
|
|
|
#define CELL_HEIGHT 32
|
|
|
|
#define CELL_WIDTH 32
|
|
|
|
#define GLYPH_HEIGHT 24
|
2015-07-28 07:59:28 +00:00
|
|
|
|
2016-01-06 05:11:31 +00:00
|
|
|
extern const uint8_t _binary_font2x_png_start[];
|
2016-01-09 08:54:18 +00:00
|
|
|
extern const uint8_t _binary_icons2x_png_start[];
|
2015-08-29 02:44:12 +00:00
|
|
|
|
2015-07-28 07:59:28 +00:00
|
|
|
struct GUIFont {
|
|
|
|
vita2d_texture* tex;
|
2016-01-09 08:54:18 +00:00
|
|
|
vita2d_texture* icons;
|
2015-07-28 07:59:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct GUIFont* GUIFontCreate(void) {
|
2015-08-29 02:44:12 +00:00
|
|
|
struct GUIFont* font = malloc(sizeof(struct GUIFont));
|
|
|
|
if (!font) {
|
2015-07-28 07:59:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-06 05:11:31 +00:00
|
|
|
font->tex = vita2d_load_PNG_buffer(_binary_font2x_png_start);
|
2016-01-09 08:54:18 +00:00
|
|
|
font->icons = vita2d_load_PNG_buffer(_binary_icons2x_png_start);
|
2015-08-29 02:44:12 +00:00
|
|
|
return font;
|
2015-07-28 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUIFontDestroy(struct GUIFont* font) {
|
|
|
|
vita2d_free_texture(font->tex);
|
2016-01-09 08:54:18 +00:00
|
|
|
vita2d_free_texture(font->icons);
|
2015-07-28 07:59:28 +00:00
|
|
|
free(font);
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:11:51 +00:00
|
|
|
unsigned GUIFontHeight(const struct GUIFont* font) {
|
2015-07-28 07:59:28 +00:00
|
|
|
UNUSED(font);
|
2016-01-06 05:11:31 +00:00
|
|
|
return GLYPH_HEIGHT;
|
2015-07-28 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 04:41:49 +00:00
|
|
|
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
|
|
|
|
UNUSED(font);
|
|
|
|
if (glyph > 0x7F) {
|
2015-11-01 17:17:01 +00:00
|
|
|
glyph = '?';
|
2015-08-27 04:41:49 +00:00
|
|
|
}
|
2015-08-30 00:05:46 +00:00
|
|
|
return defaultFontMetrics[glyph].width * 2;
|
2015-08-27 04:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
|
|
|
|
if (glyph > 0x7F) {
|
2015-11-01 17:17:01 +00:00
|
|
|
glyph = '?';
|
2015-07-28 07:59:28 +00:00
|
|
|
}
|
2015-08-27 04:41:49 +00:00
|
|
|
struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
|
2016-01-30 22:02:35 +00:00
|
|
|
vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top * 2,
|
2016-01-06 05:11:31 +00:00
|
|
|
(glyph & 15) * CELL_WIDTH + metric.padding.left * 2,
|
|
|
|
(glyph >> 4) * CELL_HEIGHT + metric.padding.top * 2,
|
|
|
|
CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2,
|
|
|
|
CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2,
|
2016-01-30 22:02:35 +00:00
|
|
|
color);
|
2015-07-28 07:59:28 +00:00
|
|
|
}
|
2016-01-09 08:54:18 +00:00
|
|
|
|
|
|
|
void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
|
|
|
|
if (icon >= GUI_ICON_MAX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct GUIIconMetric metric = defaultIconMetrics[icon];
|
|
|
|
switch (align & GUI_ALIGN_HCENTER) {
|
|
|
|
case GUI_ALIGN_HCENTER:
|
|
|
|
x -= metric.width;
|
|
|
|
break;
|
|
|
|
case GUI_ALIGN_RIGHT:
|
|
|
|
x -= metric.width * 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (align & GUI_ALIGN_VCENTER) {
|
|
|
|
case GUI_ALIGN_VCENTER:
|
|
|
|
y -= metric.height;
|
|
|
|
break;
|
|
|
|
case GUI_ALIGN_BOTTOM:
|
|
|
|
y -= metric.height * 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (orient) {
|
|
|
|
case GUI_ORIENT_HMIRROR:
|
2016-01-30 22:02:35 +00:00
|
|
|
vita2d_draw_texture_tint_part_scale(font->icons, x + metric.width * 2, y,
|
2016-01-09 08:54:18 +00:00
|
|
|
metric.x * 2, metric.y * 2,
|
|
|
|
metric.width * 2, metric.height * 2,
|
|
|
|
-1, 1, color);
|
|
|
|
return;
|
|
|
|
case GUI_ORIENT_VMIRROR:
|
2016-01-30 22:02:35 +00:00
|
|
|
vita2d_draw_texture_tint_part_scale(font->icons, x, y + metric.height * 2,
|
2016-01-09 08:54:18 +00:00
|
|
|
metric.x * 2, metric.y * 2,
|
|
|
|
metric.width * 2, metric.height * 2,
|
|
|
|
1, -1, color);
|
|
|
|
return;
|
|
|
|
case GUI_ORIENT_0:
|
|
|
|
default:
|
|
|
|
// TOOD: Rotate
|
|
|
|
vita2d_draw_texture_tint_part(font->icons, x, y,
|
|
|
|
metric.x * 2, metric.y * 2,
|
|
|
|
metric.width * 2, metric.height * 2,
|
|
|
|
color);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-30 22:02:35 +00:00
|
|
|
|
|
|
|
void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
|
|
|
|
if (icon >= GUI_ICON_MAX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct GUIIconMetric metric = defaultIconMetrics[icon];
|
|
|
|
vita2d_draw_texture_tint_part_scale(font->icons, x, y,
|
|
|
|
metric.x * 2, metric.y * 2,
|
|
|
|
metric.width * 2, metric.height * 2,
|
|
|
|
w / (float) metric.width, h / (float) metric.height, color);
|
|
|
|
}
|