diff --git a/include/lib/simplefb.h b/include/lib/simplefb.h index 1e99df0..8347447 100644 --- a/include/lib/simplefb.h +++ b/include/lib/simplefb.h @@ -28,6 +28,7 @@ struct video_info { int width; int height; int stride; + int scale_f; void *address; }; @@ -35,6 +36,8 @@ extern void __simplefb_raw_print(const char *text, int text_x, int text_y, color text_color); extern void simplefb_probe(void *data); +int get_font_scale_factor(void); + typedef struct _font_params { int width; int height; diff --git a/include/lib/video/font.h b/include/lib/video/font.h index d9c06aa..ef63d8a 100644 --- a/include/lib/video/font.h +++ b/include/lib/video/font.h @@ -9,11 +9,6 @@ #define FONTW 8 #define FONTH 16 -// Define scaled font dimensions --> 8x16 becomes 16x32 -#define SCALE_FACTOR 2 -#define SCALED_FONTW (FONTW * SCALE_FACTOR) -#define SCALED_FONTH (FONTH * SCALE_FACTOR) - static unsigned char letters[256][16] = { // 32 ' '--- { diff --git a/lib/debug/debug.c b/lib/debug/debug.c index f7e73d4..68daa9e 100644 --- a/lib/debug/debug.c +++ b/lib/debug/debug.c @@ -69,7 +69,7 @@ static inline void fb_output(const char *prefix, const char *message, color text { #ifdef CONFIG_SIMPLE_FB const int y_pos = 5; - const int prefix_width = strlen(prefix) * SCALED_FONTW; + const int prefix_width = strlen(prefix) * FONTW * get_font_scale_factor(); __simplefb_raw_print(prefix, 0, y_pos, text_color); __simplefb_raw_print(message, prefix_width, y_pos, gray); diff --git a/lib/simplefb/simplefb.c b/lib/simplefb/simplefb.c index 7528555..f49edaf 100644 --- a/lib/simplefb/simplefb.c +++ b/lib/simplefb/simplefb.c @@ -46,6 +46,32 @@ static void draw_pixel(volatile char *fb, int x, int y, int width, int stride, } } +#define SCALED_FONTW (FONTW * fb_info->scale_f) +#define SCALED_FONTH (FONTH * fb_info->scale_f) + +int get_font_scale_factor() +{ + /* integer sqrt inline */ + int n = fb_info->width * fb_info->width + fb_info->height * fb_info->height; + int x = n; + int y = (x + 1) / 2; + + while (y < x) { + x = y; + y = (x + n / x) / 2; + } + int diag = x; + + /* determine scale factor from pseudo-diagonal */ + int scale = diag / 750; + + /* clamp the result */ + if (scale < 1) + scale = 1; + + return scale; +} + void __simplefb_raw_print(const char *text, int text_x, int text_y, color text_color) { @@ -100,10 +126,10 @@ void __simplefb_raw_print(const char *text, int text_x, int text_y, for (int x = 0; x < FONTW; x++) { if (((b << x) & 0b10000000) > 0) { - for (int dy = 0; dy < SCALE_FACTOR; dy++) { - for (int dx = 0; dx < SCALE_FACTOR; dx++) { - draw_pixel(fb_info->address, current_x + x * SCALE_FACTOR + dx, - current_y + y * SCALE_FACTOR + dy, + for (int dy = 0; dy < fb_info->scale_f; dy++) { + for (int dx = 0; dx < fb_info->scale_f; dx++) { + draw_pixel(fb_info->address, current_x + x * fb_info->scale_f + dx, + current_y + y * fb_info->scale_f + dy, fb_info->width, fb_info->stride, text_color); } } @@ -121,4 +147,6 @@ void simplefb_probe(void *data) clean_fbmem((char*)fb_info->address, fb_info->width, fb_info->height, fb_info->stride); + + fb_info->scale_f = get_font_scale_factor(); }