Support multiple fonts

This commit is contained in:
K. Lange 2018-04-18 18:05:19 +09:00 committed by Kevin Lange
parent 93b57bea2c
commit 5aead12a89
7 changed files with 162 additions and 136 deletions

View File

@ -400,9 +400,9 @@ static void redraw_alttab(void) {
draw_sprite_scaled(actx, icon, center_x_a(24), ALTTAB_OFFSET, 24, 24); draw_sprite_scaled(actx, icon, center_x_a(24), ALTTAB_OFFSET, 24, 24);
} }
int t = draw_sdf_string_width(ad->name, 18); int t = draw_sdf_string_width(ad->name, 18, SDF_FONT_THIN);
draw_sdf_string(actx, center_x_a(t), 12+ALTTAB_OFFSET+16, ad->name, 18, rgb(255,255,255)); draw_sdf_string(actx, center_x_a(t), 12+ALTTAB_OFFSET+16, ad->name, 18, rgb(255,255,255), SDF_FONT_THIN);
} }
flip(actx); flip(actx);
@ -649,7 +649,7 @@ static void redraw_appmenu(int item) {
uint32_t color = (i == item) ? rgb(255,255,255) : rgb(0,0,0); uint32_t color = (i == item) ? rgb(255,255,255) : rgb(0,0,0);
draw_sdf_string(bctx, 30, offset + 2, applications[i].title, 18, color); draw_sdf_string(bctx, 30, offset + 2, applications[i].title, 18, color, SDF_FONT_THIN);
offset += APPMENU_ITEM_HEIGHT; offset += APPMENU_ITEM_HEIGHT;
} }
@ -686,22 +686,22 @@ static void redraw(void) {
/* Hours : Minutes : Seconds */ /* Hours : Minutes : Seconds */
strftime(buffer, 80, "%H:%M:%S", timeinfo); strftime(buffer, 80, "%H:%M:%S", timeinfo);
draw_sdf_string(ctx, width - TIME_LEFT, 2, buffer, 18, txt_color); draw_sdf_string(ctx, width - TIME_LEFT, 2, buffer, 18, txt_color, SDF_FONT_BOLD);
/* Day-of-week */ /* Day-of-week */
strftime(buffer, 80, "%A", timeinfo); strftime(buffer, 80, "%A", timeinfo);
t = draw_sdf_string_width(buffer, 10); t = draw_sdf_string_width(buffer, 10, SDF_FONT_BOLD);
t = (DATE_WIDTH - t) / 2; t = (DATE_WIDTH - t) / 2;
draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 2, buffer, 10, txt_color); draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 2, buffer, 10, txt_color, SDF_FONT_BOLD);
/* Month Day */ /* Month Day */
strftime(buffer, 80, "%h %e", timeinfo); strftime(buffer, 80, "%h %e", timeinfo);
t = draw_sdf_string_width(buffer, 10); t = draw_sdf_string_width(buffer, 10, SDF_FONT_THIN);
t = (DATE_WIDTH - t) / 2; t = (DATE_WIDTH - t) / 2;
draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 12, buffer, 10, txt_color); draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 12, buffer, 10, txt_color, SDF_FONT_THIN);
/* Applications menu */ /* Applications menu */
draw_sdf_string(ctx, 10, 2, "Applications", 18, appmenu ? HILIGHT_COLOR : txt_color); draw_sdf_string(ctx, 10, 2, "Applications", 18, appmenu ? HILIGHT_COLOR : txt_color, SDF_FONT_BOLD);
/* Draw each widget */ /* Draw each widget */
/* - Volume */ /* - Volume */
@ -732,11 +732,6 @@ static void redraw(void) {
break; break;
} }
#if 0
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
#endif
if (title_width > MIN_TEXT_WIDTH) { if (title_width > MIN_TEXT_WIDTH) {
memset(tmp_title, 0x0, 50); memset(tmp_title, 0x0, 50);
@ -749,7 +744,7 @@ static void redraw(void) {
if (!ad->name[i]) break; if (!ad->name[i]) break;
} }
while (draw_sdf_string_width(tmp_title, 16) > title_width - ICON_PADDING) { while (draw_sdf_string_width(tmp_title, 16, SDF_FONT_THIN) > title_width - ICON_PADDING) {
t_l--; t_l--;
tmp_title[t_l] = '.'; tmp_title[t_l] = '.';
tmp_title[t_l+1] = '.'; tmp_title[t_l+1] = '.';
@ -785,14 +780,14 @@ static void redraw(void) {
/* Then draw the window title, with appropriate color */ /* Then draw the window title, with appropriate color */
if (j == focused_app) { if (j == focused_app) {
/* Current hilighted - title should be a light blue */ /* Current hilighted - title should be a light blue */
draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET, s, 16, HILIGHT_COLOR); draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET + 2, s, 16, HILIGHT_COLOR, SDF_FONT_THIN);
} else { } else {
if (ad->flags & 1) { if (ad->flags & 1) {
/* Top window should be white */ /* Top window should be white */
draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET, s, 16, FOCUS_COLOR); draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET + 2, s, 16, FOCUS_COLOR, SDF_FONT_THIN);
} else { } else {
/* Otherwise, off white */ /* Otherwise, off white */
draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET, s, 16, txt_color); draw_sdf_string(ctx, APP_OFFSET + i + ICON_SIZE + ICON_PADDING * 2, TEXT_Y_OFFSET + 2, s, 16, txt_color, SDF_FONT_THIN);
} }
} }
} }

View File

@ -49,8 +49,10 @@ void redraw() {
decors(); decors();
draw_sdf_string(ctx, 30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0)); draw_sdf_string(ctx, 30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_THIN);
draw_sdf_string(ctx, 30, 60, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0)); draw_sdf_string(ctx, 30, 60, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_THIN);
draw_sdf_string(ctx, 30, 90, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD);
draw_sdf_string(ctx, 30,120, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD);
} }
void resize_finish(int w, int h) { void resize_finish(int w, int h) {

View File

@ -1,4 +1,9 @@
#pragma once #pragma once
extern int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int size, uint32_t color); enum sdf_font {
extern int draw_sdf_string_width(char * str, int size); SDF_FONT_THIN,
SDF_FONT_BOLD,
};
extern int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int size, uint32_t color, int font);
extern int draw_sdf_string_width(char * str, int size, int font);

View File

Before

Width:  |  Height:  |  Size: 732 KiB

After

Width:  |  Height:  |  Size: 732 KiB

BIN
base/usr/share/sdf_thin.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 732 KiB

View File

@ -79,19 +79,19 @@ static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * c
#define EXTRA_SPACE 40 #define EXTRA_SPACE 40
if (draw_sdf_string_width(tmp_title, 18) + EXTRA_SPACE > width) { if (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) + EXTRA_SPACE > width) {
while (t_l >= 0 && (draw_sdf_string_width(tmp_title, 18) + EXTRA_SPACE > width)) { while (t_l >= 0 && (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0'; tmp_title[t_l] = '\0';
t_l--; t_l--;
} }
} }
if (strlen(tmp_title)) { if (strlen(tmp_title)) {
int title_offset = (width / 2) - (draw_sdf_string_width(tmp_title, 18) / 2); int title_offset = (width / 2) - (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) / 2);
if (decors_active == 0) { if (decors_active == 0) {
draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(226,226,226)); draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(226,226,226), SDF_FONT_BOLD);
} else { } else {
draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(147,147,147)); draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(147,147,147), SDF_FONT_BOLD);
} }
} }

View File

@ -7,16 +7,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <toaru/graphics.h> #include <toaru/graphics.h>
#include <toaru/sdf.h>
#define BASE_WIDTH 50 #define BASE_WIDTH 50
#define BASE_HEIGHT 50 #define BASE_HEIGHT 50
#define GAMMA 1.7 #define GAMMA 1.7
static sprite_t _font_data; static sprite_t _font_data_thin;
static sprite_t _font_data_bold;
struct { struct {
char code; char code;
size_t width; size_t width_bold;
} _char_data[] = { } _char_data[] = {
{'!', 20}, {'!', 20},
{'"', 35}, {'"', 35},
@ -121,11 +123,32 @@ static int loaded = 0;
__attribute__((constructor)) __attribute__((constructor))
static void _init_sdf(void) { static void _init_sdf(void) {
/* Load the font. */ /* Load the font. */
load_sprite(&_font_data, "/usr/share/sdf.bmp"); load_sprite(&_font_data_thin, "/usr/share/sdf_thin.bmp");
load_sprite(&_font_data_bold, "/usr/share/sdf_bold.bmp");
loaded = 1; loaded = 1;
} }
static int draw_sdf_character(gfx_context_t * ctx, int32_t x, int32_t y, int ch, int size, uint32_t color, sprite_t * tmp) { static sprite_t * _select_font(int font) {
switch (font) {
case SDF_FONT_BOLD:
return &_font_data_bold;
case SDF_FONT_THIN:
default:
return &_font_data_thin;
}
}
static int _select_width(char ch, int font) {
switch (font) {
case SDF_FONT_BOLD:
return _char_data[ch].width_bold;
case SDF_FONT_THIN:
default:
return _char_data[ch].width_bold * 0.8;
}
}
static int draw_sdf_character(gfx_context_t * ctx, int32_t x, int32_t y, int ch, int size, uint32_t color, sprite_t * tmp, int font, sprite_t * _font_data) {
if (ch != ' ' && ch < '!' || ch > '~') { if (ch != ' ' && ch < '!' || ch > '~') {
/* TODO: Draw missing symbol? */ /* TODO: Draw missing symbol? */
return 0; return 0;
@ -139,9 +162,9 @@ static int draw_sdf_character(gfx_context_t * ctx, int32_t x, int32_t y, int ch,
} }
double scale = (double)size / 50.0; double scale = (double)size / 50.0;
int width = _char_data[ch].width * scale; int width = _select_width(ch, font) * scale;
int fx = ((BASE_WIDTH * ch) % _font_data.width) * scale; int fx = ((BASE_WIDTH * ch) % _font_data->width) * scale;
int fy = (((BASE_WIDTH * ch) / _font_data.width) * BASE_HEIGHT) * scale; int fy = (((BASE_WIDTH * ch) / _font_data->width) * BASE_HEIGHT) * scale;
int height = BASE_HEIGHT * ((double)size / 50.0); int height = BASE_HEIGHT * ((double)size / 50.0);
@ -168,18 +191,20 @@ static int draw_sdf_character(gfx_context_t * ctx, int32_t x, int32_t y, int ch,
} }
int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int size, uint32_t color) { int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int size, uint32_t color, int font) {
sprite_t * _font_data = _select_font(font);
if (!loaded) return 0; if (!loaded) return 0;
double scale = (double)size / 50.0; double scale = (double)size / 50.0;
sprite_t * tmp = create_sprite(scale * _font_data.width, scale * _font_data.height, ALPHA_OPAQUE); sprite_t * tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE);
gfx_context_t * t = init_graphics_sprite(tmp); gfx_context_t * t = init_graphics_sprite(tmp);
draw_sprite_scaled(t, &_font_data, 0, 0, tmp->width, tmp->height); draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height);
int32_t out_width = 0; int32_t out_width = 0;
while (*str) { while (*str) {
int w = draw_sdf_character(ctx,x,y,*str,size,color,tmp); int w = draw_sdf_character(ctx,x,y,*str,size,color,tmp,font,_font_data);
out_width += w; out_width += w;
x += w; x += w;
str++; str++;
@ -191,8 +216,7 @@ int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int s
return out_width; return out_width;
} }
static int char_width(char ch, int font) {
static int char_width(char ch) {
if (ch != ' ' && ch < '!' || ch > '~') { if (ch != ' ' && ch < '!' || ch > '~') {
/* TODO: Draw missing symbol? */ /* TODO: Draw missing symbol? */
return 0; return 0;
@ -205,16 +229,16 @@ static int char_width(char ch) {
ch -= '!'; ch -= '!';
} }
return _char_data[ch].width; return _select_width(ch, font);
} }
int draw_sdf_string_width(char * str, int size) { int draw_sdf_string_width(char * str, int size, int font) {
double scale = (double)size / 50.0; double scale = (double)size / 50.0;
int32_t out_width = 0; int32_t out_width = 0;
while (*str) { while (*str) {
int w = char_width(*str) * scale; int w = char_width(*str,font) * scale;
out_width += w; out_width += w;
str++; str++;
} }