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);
}
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);
@ -649,7 +649,7 @@ static void redraw_appmenu(int item) {
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;
}
@ -686,22 +686,22 @@ static void redraw(void) {
/* Hours : Minutes : Seconds */
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 */
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;
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 */
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;
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 */
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 */
/* - Volume */
@ -732,11 +732,6 @@ static void redraw(void) {
break;
}
#if 0
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
#endif
if (title_width > MIN_TEXT_WIDTH) {
memset(tmp_title, 0x0, 50);
@ -749,7 +744,7 @@ static void redraw(void) {
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--;
tmp_title[t_l] = '.';
tmp_title[t_l+1] = '.';
@ -785,14 +780,14 @@ static void redraw(void) {
/* Then draw the window title, with appropriate color */
if (j == focused_app) {
/* 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 {
if (ad->flags & 1) {
/* 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 {
/* 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();
draw_sdf_string(ctx, 30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0));
draw_sdf_string(ctx, 30, 60, "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), 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) {

View File

@ -1,4 +1,9 @@
#pragma once
extern int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, char * str, int size, uint32_t color);
extern int draw_sdf_string_width(char * str, int size);
enum sdf_font {
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
if (draw_sdf_string_width(tmp_title, 18) + EXTRA_SPACE > width) {
while (t_l >= 0 && (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, SDF_FONT_BOLD) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0';
t_l--;
}
}
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) {
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 {
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);
}
}

242
lib/sdf.c
View File

@ -7,112 +7,114 @@
#include <stdlib.h>
#include <toaru/graphics.h>
#include <toaru/sdf.h>
#define BASE_WIDTH 50
#define BASE_HEIGHT 50
#define GAMMA 1.7
static sprite_t _font_data;
static sprite_t _font_data_thin;
static sprite_t _font_data_bold;
struct {
char code;
size_t width;
size_t width_bold;
} _char_data[] = {
{'!', 20},
{'"', 35},
{'#', 40},
{'$', 35},
{'%', 35},
{'&', 35},
{'!', 20},
{'"', 35},
{'#', 40},
{'$', 35},
{'%', 35},
{'&', 35},
{'\'', 35},
{'(', 35},
{')', 35},
{'*', 35},
{'+', 35},
{',', 35},
{'-', 35},
{'.', 35},
{'/', 35},
{'0', 32},
{'1', 32},
{'2', 32},
{'3', 32},
{'4', 32},
{'5', 32},
{'6', 32},
{'7', 32},
{'8', 32},
{'9', 32},
{':', 26},
{';', 35},
{'<', 35},
{'=', 35},
{'>', 35},
{'?', 35},
{'@', 50},
{'A', 35},
{'B', 35},
{'C', 34},
{'D', 36},
{'E', 34},
{'F', 35},
{'G', 35},
{'H', 35},
{'I', 22},
{'J', 24},
{'K', 35},
{'L', 32},
{'M', 45},
{'N', 36},
{'O', 38},
{'P', 35},
{'Q', 38},
{'R', 36},
{'S', 36},
{'T', 35},
{'U', 35},
{'V', 37},
{'W', 50},
{'X', 35},
{'Y', 35},
{'Z', 35},
{'[', 35},
{'(', 35},
{')', 35},
{'*', 35},
{'+', 35},
{',', 35},
{'-', 35},
{'.', 35},
{'/', 35},
{'0', 32},
{'1', 32},
{'2', 32},
{'3', 32},
{'4', 32},
{'5', 32},
{'6', 32},
{'7', 32},
{'8', 32},
{'9', 32},
{':', 26},
{';', 35},
{'<', 35},
{'=', 35},
{'>', 35},
{'?', 35},
{'@', 50},
{'A', 35},
{'B', 35},
{'C', 34},
{'D', 36},
{'E', 34},
{'F', 35},
{'G', 35},
{'H', 35},
{'I', 22},
{'J', 24},
{'K', 35},
{'L', 32},
{'M', 45},
{'N', 36},
{'O', 38},
{'P', 35},
{'Q', 38},
{'R', 36},
{'S', 36},
{'T', 35},
{'U', 35},
{'V', 37},
{'W', 50},
{'X', 35},
{'Y', 35},
{'Z', 35},
{'[', 35},
{'\\', 35},
{']', 35},
{'^', 35},
{'_', 35},
{'`', 35},
{'a', 32},
{'b', 32},
{'c', 32},
{'d', 32},
{'e', 32},
{'f', 30},
{'g', 32},
{'h', 32},
{'i', 16},
{'j', 16},
{'k', 30},
{'l', 16},
{'m', 48},
{'n', 34},
{'o', 32},
{'p', 32},
{'q', 32},
{'r', 25},
{'s', 32},
{'t', 29},
{'u', 32},
{'v', 32},
{'w', 42},
{'x', 32},
{'y', 32},
{'z', 32},
{'{', 32},
{'|', 32},
{'}', 32},
{'~', 32},
{' ', 20},
{']', 35},
{'^', 35},
{'_', 35},
{'`', 35},
{'a', 32},
{'b', 32},
{'c', 32},
{'d', 32},
{'e', 32},
{'f', 30},
{'g', 32},
{'h', 32},
{'i', 16},
{'j', 16},
{'k', 30},
{'l', 16},
{'m', 48},
{'n', 34},
{'o', 32},
{'p', 32},
{'q', 32},
{'r', 25},
{'s', 32},
{'t', 29},
{'u', 32},
{'v', 32},
{'w', 42},
{'x', 32},
{'y', 32},
{'z', 32},
{'{', 32},
{'|', 32},
{'}', 32},
{'~', 32},
{' ', 20},
{0,0},
};
@ -121,11 +123,32 @@ static int loaded = 0;
__attribute__((constructor))
static void _init_sdf(void) {
/* 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;
}
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 > '~') {
/* TODO: Draw missing symbol? */
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;
int width = _char_data[ch].width * scale;
int fx = ((BASE_WIDTH * ch) % _font_data.width) * scale;
int fy = (((BASE_WIDTH * ch) / _font_data.width) * BASE_HEIGHT) * scale;
int width = _select_width(ch, font) * scale;
int fx = ((BASE_WIDTH * ch) % _font_data->width) * scale;
int fy = (((BASE_WIDTH * ch) / _font_data->width) * BASE_HEIGHT) * scale;
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;
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);
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;
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;
x += w;
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;
}
static int char_width(char ch) {
static int char_width(char ch, int font) {
if (ch != ' ' && ch < '!' || ch > '~') {
/* TODO: Draw missing symbol? */
return 0;
@ -205,16 +229,16 @@ static int char_width(char 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;
int32_t out_width = 0;
while (*str) {
int w = char_width(*str) * scale;
int w = char_width(*str,font) * scale;
out_width += w;
str++;
}