summaryrefslogtreecommitdiffstats
path: root/minui
diff options
context:
space:
mode:
Diffstat (limited to 'minui')
-rw-r--r--minui/graphics.c65
-rw-r--r--minui/minui.h2
-rw-r--r--minui/resources.c20
3 files changed, 51 insertions, 36 deletions
diff --git a/minui/graphics.c b/minui/graphics.c
index 747b2dbc6..4968eac7a 100644
--- a/minui/graphics.c
+++ b/minui/graphics.c
@@ -47,10 +47,9 @@
#define NUM_BUFFERS 2
typedef struct {
- GGLSurface texture;
+ GGLSurface* texture;
unsigned cwidth;
unsigned cheight;
- unsigned ascent;
} GRFont;
static GRFont *gr_font = 0;
@@ -224,18 +223,20 @@ void gr_font_size(int *x, int *y)
*y = gr_font->cheight;
}
-int gr_text(int x, int y, const char *s)
+int gr_text(int x, int y, const char *s, int bold)
{
GGLContext *gl = gr_context;
GRFont *font = gr_font;
unsigned off;
+ if (!font->texture) return x;
+
+ bold = bold && (font->texture->height != font->cheight);
+
x += overscan_offset_x;
y += overscan_offset_y;
- y -= font->ascent;
-
- gl->bindTexture(gl, &font->texture);
+ gl->bindTexture(gl, font->texture);
gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
@@ -244,7 +245,8 @@ int gr_text(int x, int y, const char *s)
while((off = *s++)) {
off -= 32;
if (off < 96) {
- gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
+ gl->texCoord2i(gl, (off * font->cwidth) - x,
+ (bold ? font->cheight : 0) - y);
gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
}
x += font->cwidth;
@@ -322,31 +324,40 @@ unsigned int gr_get_height(gr_surface surface) {
static void gr_init_font(void)
{
- GGLSurface *ftex;
- unsigned char *bits, *rle;
- unsigned char *in, data;
-
gr_font = calloc(sizeof(*gr_font), 1);
- ftex = &gr_font->texture;
-
- bits = malloc(font.width * font.height);
- ftex->version = sizeof(*ftex);
- ftex->width = font.width;
- ftex->height = font.height;
- ftex->stride = font.width;
- ftex->data = (void*) bits;
- ftex->format = GGL_PIXEL_FORMAT_A_8;
+ int res = res_create_surface("font", (void**)&(gr_font->texture));
+ if (res == 0) {
+ // The font image should be a 96x2 array of character images. The
+ // columns are the printable ASCII characters 0x20 - 0x7f. The
+ // top row is regular text; the bottom row is bold.
+ gr_font->cwidth = gr_font->texture->width / 96;
+ gr_font->cheight = gr_font->texture->height / 2;
+ } else {
+ printf("failed to read font: res=%d\n", res);
+
+ // fall back to the compiled-in font.
+ gr_font->texture = malloc(sizeof(*gr_font->texture));
+ gr_font->texture->width = font.width;
+ gr_font->texture->height = font.height;
+ gr_font->texture->stride = font.width;
+
+ unsigned char* bits = malloc(font.width * font.height);
+ gr_font->texture->data = (void*) bits;
+
+ unsigned char data;
+ unsigned char* in = font.rundata;
+ while((data = *in++)) {
+ memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
+ bits += (data & 0x7f);
+ }
- in = font.rundata;
- while((data = *in++)) {
- memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
- bits += (data & 0x7f);
+ gr_font->cwidth = font.cwidth;
+ gr_font->cheight = font.cheight;
}
- gr_font->cwidth = font.cwidth;
- gr_font->cheight = font.cheight;
- gr_font->ascent = font.cheight - 2;
+ // interpret the grayscale as alpha
+ gr_font->texture->format = GGL_PIXEL_FORMAT_A_8;
}
int gr_init(void)
diff --git a/minui/minui.h b/minui/minui.h
index bc43bb596..1b8dd059b 100644
--- a/minui/minui.h
+++ b/minui/minui.h
@@ -37,7 +37,7 @@ void gr_fb_blank(bool blank);
void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
void gr_fill(int x1, int y1, int x2, int y2);
-int gr_text(int x, int y, const char *s);
+int gr_text(int x, int y, const char *s, int bold);
void gr_texticon(int x, int y, gr_surface icon);
int gr_measure(const char *s);
void gr_font_size(int *x, int *y);
diff --git a/minui/resources.c b/minui/resources.c
index 065f4317e..72f39fbaa 100644
--- a/minui/resources.c
+++ b/minui/resources.c
@@ -93,22 +93,23 @@ int res_create_surface(const char* name, gr_surface* pSurface) {
png_set_sig_bytes(png_ptr, sizeof(header));
png_read_info(png_ptr, info_ptr);
- size_t width = info_ptr->width;
- size_t height = info_ptr->height;
- size_t stride = 4 * width;
- size_t pixelSize = stride * height;
-
int color_type = info_ptr->color_type;
int bit_depth = info_ptr->bit_depth;
int channels = info_ptr->channels;
if (!(bit_depth == 8 &&
((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
(channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
- (channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE)))) {
+ (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
+ color_type == PNG_COLOR_TYPE_GRAY))))) {
return -7;
goto exit;
}
+ size_t width = info_ptr->width;
+ size_t height = info_ptr->height;
+ size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
+ size_t pixelSize = stride * height;
+
surface = malloc(sizeof(GGLSurface) + pixelSize);
if (surface == NULL) {
result = -8;
@@ -120,8 +121,8 @@ int res_create_surface(const char* name, gr_surface* pSurface) {
surface->height = height;
surface->stride = width; /* Yes, pixels, not bytes */
surface->data = pData;
- surface->format = (channels == 3) ?
- GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;
+ surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 :
+ ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8));
int alpha = 0;
if (color_type == PNG_COLOR_TYPE_PALETTE) {
@@ -131,6 +132,9 @@ int res_create_surface(const char* name, gr_surface* pSurface) {
png_set_tRNS_to_alpha(png_ptr);
alpha = 1;
}
+ if (color_type == PNG_COLOR_TYPE_GRAY) {
+ alpha = 1;
+ }
unsigned int y;
if (channels == 3 || (channels == 1 && !alpha)) {