From d5d34d70a5d9c24e214855bb7e1e5402a96ec7cc Mon Sep 17 00:00:00 2001 From: Damien Bargiacchi Date: Thu, 11 Aug 2016 15:57:03 -0700 Subject: Support use of custom fonts in miniui Bug: 29547343 Change-Id: I398160c85daac90ffab2fa9bb2e96795b9e9885a (cherry picked from commit 35fff61b1c0d736d090a1cd1bb4e99141cc88ad8) --- minui/graphics.cpp | 102 +++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 47 deletions(-) (limited to 'minui/graphics.cpp') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index c0eea9e38..43ec83f08 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -35,12 +35,6 @@ #include "minui.h" #include "graphics.h" -struct GRFont { - GRSurface* texture; - int cwidth; - int cheight; -}; - static GRFont* gr_font = NULL; static minui_backend* gr_backend = NULL; @@ -60,15 +54,20 @@ static bool outside(int x, int y) return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; } -int gr_measure(const char *s) +const GRFont* gr_sys_font() { - return gr_font->cwidth * strlen(s); + return gr_font; } -void gr_font_size(int *x, int *y) +int gr_measure(const GRFont* font, const char *s) { - *x = gr_font->cwidth; - *y = gr_font->cheight; + return font->char_width * strlen(s); +} + +void gr_font_size(const GRFont* font, int *x, int *y) +{ + *x = font->char_width; + *y = font->char_height; } static void text_blend(unsigned char* src_p, int src_row_bytes, @@ -103,34 +102,32 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } -void gr_text(int x, int y, const char *s, bool bold) +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold) { - GRFont* font = gr_font; - if (!font->texture || gr_current_a == 0) return; - bold = bold && (font->texture->height != font->cheight); + bold = bold && (font->texture->height != font->char_height); x += overscan_offset_x; y += overscan_offset_y; unsigned char ch; while ((ch = *s++)) { - if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; + if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break; if (ch < ' ' || ch > '~') { ch = '?'; } - unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + - (bold ? font->cheight * font->texture->row_bytes : 0); + unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) + + (bold ? font->char_height * font->texture->row_bytes : 0); unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; text_blend(src_p, font->texture->row_bytes, dst_p, gr_draw->row_bytes, - font->cwidth, font->cheight); + font->char_width, font->char_height); - x += font->cwidth; + x += font->char_width; } } @@ -267,40 +264,51 @@ unsigned int gr_get_height(GRSurface* surface) { return surface->height; } +int gr_init_font(const char* name, GRFont* dest) { + int res = res_create_alpha_surface(name, &(dest->texture)); + if (res < 0) { + return res; + } + + // 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. + dest->char_width = dest->texture->width / 96; + dest->char_height = dest->texture->height / 2; + + return 0; +} + static void gr_init_font(void) { gr_font = reinterpret_cast(calloc(sizeof(*gr_font), 1)); - int res = res_create_alpha_surface("font", &(gr_font->texture)); + int res = gr_init_font("font", gr_font); 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 = reinterpret_cast(malloc(sizeof(*gr_font->texture))); - gr_font->texture->width = font.width; - gr_font->texture->height = font.height; - gr_font->texture->row_bytes = font.width; - gr_font->texture->pixel_bytes = 1; - - unsigned char* bits = reinterpret_cast(malloc(font.width * font.height)); - gr_font->texture->data = reinterpret_cast(bits); - - unsigned char data; - unsigned char* in = font.rundata; - while((data = *in++)) { - memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); - bits += (data & 0x7f); - } + return; + } - gr_font->cwidth = font.cwidth; - gr_font->cheight = font.cheight; + printf("failed to read font: res=%d\n", res); + + // fall back to the compiled-in font. + gr_font->texture = reinterpret_cast(malloc(sizeof(*gr_font->texture))); + gr_font->texture->width = font.width; + gr_font->texture->height = font.height; + gr_font->texture->row_bytes = font.width; + gr_font->texture->pixel_bytes = 1; + + unsigned char* bits = reinterpret_cast(malloc(font.width * font.height)); + gr_font->texture->data = reinterpret_cast(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->char_width = font.char_width; + gr_font->char_height = font.char_height; } #if 0 -- cgit v1.2.3 From 97eda9db705f0435dd049e1fa93bd878defa747c Mon Sep 17 00:00:00 2001 From: Damien Bargiacchi Date: Fri, 9 Sep 2016 07:14:08 -0700 Subject: Have gr_init_font alloc memory for the font Change-Id: I8ccf369d52011bc5d07d8e041fe558ce734a78fc (cherry picked from commit d00f5eb63a8e4690f9bef1e943d539d052444d9b) --- minui/graphics.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'minui/graphics.cpp') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index 43ec83f08..ab56a6fd6 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -264,33 +264,41 @@ unsigned int gr_get_height(GRSurface* surface) { return surface->height; } -int gr_init_font(const char* name, GRFont* dest) { - int res = res_create_alpha_surface(name, &(dest->texture)); +int gr_init_font(const char* name, GRFont** dest) { + GRFont* font = reinterpret_cast(calloc(1, sizeof(*gr_font))); + if (font == nullptr) { + return -1; + } + + int res = res_create_alpha_surface(name, &(font->texture)); if (res < 0) { + free(font); return res; } // 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. - dest->char_width = dest->texture->width / 96; - dest->char_height = dest->texture->height / 2; + font->char_width = font->texture->width / 96; + font->char_height = font->texture->height / 2; + + *dest = font; return 0; } static void gr_init_font(void) { - gr_font = reinterpret_cast(calloc(sizeof(*gr_font), 1)); - - int res = gr_init_font("font", gr_font); + int res = gr_init_font("font", &gr_font); if (res == 0) { return; } printf("failed to read font: res=%d\n", res); + // fall back to the compiled-in font. + gr_font = reinterpret_cast(calloc(1, sizeof(*gr_font))); gr_font->texture = reinterpret_cast(malloc(sizeof(*gr_font->texture))); gr_font->texture->width = font.width; gr_font->texture->height = font.height; -- cgit v1.2.3