From 6fd59ac07d91eb373f4269a40e688aa82a6ccc6e Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Wed, 6 Mar 2013 15:01:11 -0800 Subject: more font improvements and cleanup Get rid of the notion of a font's "ascent"; the reference point for drawing is the top-left corner of the character box rather than the baseline. Add some more space between the menu entries and make the highlight bar around the text. Replace the default font.png with two images; the build system will include one or the other based on the resolutions of the device. Restore the original compiled-in bitmap font, to fall back on when font.png can't be found (eg, in the charger binary). Add support for bold text (when a font.png image is used). Change-Id: I6d211a486a3636f20208502b1cd2aeae8b9f5b02 --- minui/graphics.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'minui/graphics.c') diff --git a/minui/graphics.c b/minui/graphics.c index ba68a9538..4968eac7a 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -30,6 +30,7 @@ #include +#include "font_10x18.h" #include "minui.h" #if defined(RECOVERY_BGRA) @@ -49,7 +50,6 @@ typedef struct { GGLSurface* texture; unsigned cwidth; unsigned cheight; - unsigned ascent; } GRFont; static GRFont *gr_font = 0; @@ -223,7 +223,7 @@ 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; @@ -231,11 +231,11 @@ int gr_text(int x, int y, const char *s) 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->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); @@ -245,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; @@ -326,18 +327,37 @@ static void gr_init_font(void) gr_font = calloc(sizeof(*gr_font), 1); int res = res_create_surface("font", (void**)&(gr_font->texture)); - if (res != 0) { + 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); - gr_font->texture = NULL; - return; + + // 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); + } + + gr_font->cwidth = font.cwidth; + gr_font->cheight = font.cheight; } // interpret the grayscale as alpha gr_font->texture->format = GGL_PIXEL_FORMAT_A_8; - - gr_font->cwidth = gr_font->texture->width / 96; - gr_font->cheight = gr_font->texture->height; - gr_font->ascent = gr_font->cheight - 2; } int gr_init(void) -- cgit v1.2.3