Support use of custom fonts in miniui

Bug: 29547343
Change-Id: I398160c85daac90ffab2fa9bb2e96795b9e9885a
(cherry picked from commit 35fff61b1c)
This commit is contained in:
Damien Bargiacchi 2016-08-11 15:57:03 -07:00 committed by Tao Bao
parent b76960c984
commit d5d34d70a5
5 changed files with 84 additions and 66 deletions

View file

@ -1,14 +1,14 @@
struct { struct {
unsigned width; unsigned width;
unsigned height; unsigned height;
unsigned cwidth; unsigned char_width;
unsigned cheight; unsigned char_height;
unsigned char rundata[2973]; unsigned char rundata[2973];
} font = { } font = {
.width = 960, .width = 960,
.height = 18, .height = 18,
.cwidth = 10, .char_width = 10,
.cheight = 18, .char_height = 18,
.rundata = { .rundata = {
0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x55,0x82,0x06,0x82,0x02,0x82,0x10,0x82, 0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x55,0x82,0x06,0x82,0x02,0x82,0x10,0x82,
0x11,0x83,0x08,0x82,0x0a,0x82,0x04,0x82,0x46,0x82,0x08,0x82,0x07,0x84,0x06, 0x11,0x83,0x08,0x82,0x0a,0x82,0x04,0x82,0x46,0x82,0x08,0x82,0x07,0x84,0x06,

View file

@ -35,12 +35,6 @@
#include "minui.h" #include "minui.h"
#include "graphics.h" #include "graphics.h"
struct GRFont {
GRSurface* texture;
int cwidth;
int cheight;
};
static GRFont* gr_font = NULL; static GRFont* gr_font = NULL;
static minui_backend* gr_backend = 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; 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; return font->char_width * strlen(s);
*y = gr_font->cheight; }
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, 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; 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; x += overscan_offset_x;
y += overscan_offset_y; y += overscan_offset_y;
unsigned char ch; unsigned char ch;
while ((ch = *s++)) { 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 > '~') { if (ch < ' ' || ch > '~') {
ch = '?'; ch = '?';
} }
unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
(bold ? font->cheight * font->texture->row_bytes : 0); (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; 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, text_blend(src_p, font->texture->row_bytes,
dst_p, gr_draw->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,18 +264,30 @@ unsigned int gr_get_height(GRSurface* surface) {
return surface->height; 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) static void gr_init_font(void)
{ {
gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1)); gr_font = reinterpret_cast<GRFont*>(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) { if (res == 0) {
// The font image should be a 96x2 array of character images. The return;
// 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); printf("failed to read font: res=%d\n", res);
// fall back to the compiled-in font. // fall back to the compiled-in font.
@ -298,9 +307,8 @@ static void gr_init_font(void)
bits += (data & 0x7f); bits += (data & 0x7f);
} }
gr_font->cwidth = font.cwidth; gr_font->char_width = font.char_width;
gr_font->cheight = font.cheight; gr_font->char_height = font.char_height;
}
} }
#if 0 #if 0

View file

@ -33,6 +33,12 @@ struct GRSurface {
unsigned char* data; unsigned char* data;
}; };
struct GRFont {
GRSurface* texture;
int char_width;
int char_height;
};
int gr_init(); int gr_init();
void gr_exit(); void gr_exit();
@ -45,10 +51,14 @@ void gr_fb_blank(bool blank);
void gr_clear(); // clear entire surface to current color void gr_clear(); // clear entire surface to current color
void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); 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); void gr_fill(int x1, int y1, int x2, int y2);
void gr_text(int x, int y, const char *s, bool bold);
void gr_texticon(int x, int y, GRSurface* icon); void gr_texticon(int x, int y, GRSurface* icon);
int gr_measure(const char *s);
void gr_font_size(int *x, int *y); const GRFont* gr_sys_font();
int gr_init_font(const char* name, GRFont* dest);
void gr_text(const GRFont* font, int x, int y, const char *s, bool bold);
int gr_measure(const GRFont* font, const char *s);
void gr_font_size(const GRFont* font, int *x, int *y);
void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
unsigned int gr_get_width(GRSurface* surface); unsigned int gr_get_width(GRSurface* surface);

View file

@ -248,7 +248,7 @@ void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
} }
void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) { void ScreenRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
gr_text(x, *y, line, bold); gr_text(gr_sys_font(), x, *y, line, bold);
*y += char_height_ + 4; *y += char_height_ + 4;
} }
@ -304,10 +304,10 @@ void ScreenRecoveryUI::draw_screen_locked() {
gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2); gr_fill(0, y - 2, gr_fb_width(), y + char_height_ + 2);
// Bold white text for the selected item. // Bold white text for the selected item.
SetColor(MENU_SEL_FG); SetColor(MENU_SEL_FG);
gr_text(4, y, menu_[i], true); gr_text(gr_sys_font(), 4, y, menu_[i], true);
SetColor(MENU); SetColor(MENU);
} else { } else {
gr_text(4, y, menu_[i], false); gr_text(gr_sys_font(), 4, y, menu_[i], false);
} }
y += char_height_ + 4; y += char_height_ + 4;
} }
@ -323,7 +323,7 @@ void ScreenRecoveryUI::draw_screen_locked() {
for (int ty = gr_fb_height() - char_height_; for (int ty = gr_fb_height() - char_height_;
ty >= y && count < text_rows_; ty >= y && count < text_rows_;
ty -= char_height_, ++count) { ty -= char_height_, ++count) {
gr_text(0, ty, text_[row], false); gr_text(gr_sys_font(), 0, ty, text_[row], false);
--row; --row;
if (row < 0) row = text_rows_ - 1; if (row < 0) row = text_rows_ - 1;
} }
@ -442,7 +442,7 @@ void ScreenRecoveryUI::Init() {
density_ = static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f; density_ = static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f;
is_large_ = gr_fb_height() > PixelsFromDp(800); is_large_ = gr_fb_height() > PixelsFromDp(800);
gr_font_size(&char_width_, &char_height_); gr_font_size(gr_sys_font(), &char_width_, &char_height_);
text_rows_ = gr_fb_height() / char_height_; text_rows_ = gr_fb_height() / char_height_;
text_cols_ = gr_fb_width() / char_width_; text_cols_ = gr_fb_width() / char_width_;

View file

@ -177,7 +177,7 @@ void WearRecoveryUI::draw_screen_locked()
// items don't fit on the screen. // items don't fit on the screen.
if (menu_items > menu_end - menu_start) { if (menu_items > menu_end - menu_start) {
sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items); sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
gr_text(x+4, y, cur_selection_str, 1); gr_text(gr_sys_font(), x+4, y, cur_selection_str, 1);
y += char_height_+4; y += char_height_+4;
} }
@ -192,10 +192,10 @@ void WearRecoveryUI::draw_screen_locked()
gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2); gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2);
// white text of selected item // white text of selected item
SetColor(MENU_SEL_FG); SetColor(MENU_SEL_FG);
if (menu[i][0]) gr_text(x+4, y, menu[i], 1); if (menu[i][0]) gr_text(gr_sys_font(), x+4, y, menu[i], 1);
SetColor(MENU); SetColor(MENU);
} else { } else if (menu[i][0]) {
if (menu[i][0]) gr_text(x+4, y, menu[i], 0); gr_text(gr_sys_font(), x+4, y, menu[i], 0);
} }
y += char_height_+4; y += char_height_+4;
} }
@ -216,7 +216,7 @@ void WearRecoveryUI::draw_screen_locked()
for (int ty = gr_fb_height() - char_height_ - outer_height; for (int ty = gr_fb_height() - char_height_ - outer_height;
ty > y+2 && count < text_rows; ty > y+2 && count < text_rows;
ty -= char_height_, ++count) { ty -= char_height_, ++count) {
gr_text(x+4, ty, text[row], 0); gr_text(gr_sys_font(), x+4, ty, text[row], 0);
--row; --row;
if (row < 0) row = text_rows-1; if (row < 0) row = text_rows-1;
} }
@ -285,7 +285,7 @@ void WearRecoveryUI::Init()
{ {
gr_init(); gr_init();
gr_font_size(&char_width_, &char_height_); gr_font_size(gr_sys_font(), &char_width_, &char_height_);
text_col = text_row = 0; text_col = text_row = 0;
text_rows = (gr_fb_height()) / char_height_; text_rows = (gr_fb_height()) / char_height_;