toaruos/lib/sdf.c

263 lines
5.0 KiB
C
Raw Normal View History

2018-04-18 07:27:37 +03:00
/* vim: ts=4 sw=4 noexpandtab
*
* Signed Distance Field Text Library
*/
#include <stdio.h>
2018-04-18 08:36:28 +03:00
#include <stdlib.h>
2018-04-18 07:27:37 +03:00
#include <toaru/graphics.h>
#include <toaru/hashmap.h>
2018-04-18 12:05:19 +03:00
#include <toaru/sdf.h>
#include <toaru/spinlock.h>
2018-04-18 07:27:37 +03:00
#define BASE_WIDTH 50
#define BASE_HEIGHT 50
2018-04-18 08:36:28 +03:00
#define GAMMA 1.7
2018-04-18 07:27:37 +03:00
2018-04-18 12:05:19 +03:00
static sprite_t _font_data_thin;
static sprite_t _font_data_bold;
2018-04-18 07:27:37 +03:00
static hashmap_t * _font_cache;
static volatile int _sdf_lock = 0;
2018-04-18 07:27:37 +03:00
struct {
char code;
2018-04-18 12:05:19 +03:00
size_t width_bold;
2018-04-18 07:27:37 +03:00
} _char_data[] = {
2018-04-18 12:05:19 +03:00
{'!', 20},
{'"', 35},
{'#', 40},
{'$', 35},
{'%', 35},
{'&', 35},
2018-04-18 07:27:37 +03:00
{'\'', 35},
2018-04-21 14:55:19 +03:00
{'(', 22},
{')', 22},
2018-04-18 12:05:19 +03:00
{'*', 35},
{'+', 35},
{',', 35},
2018-04-21 14:55:19 +03:00
{'-', 30},
{'.', 18},
{'/', 24},
2018-04-18 12:05:19 +03:00
{'0', 32},
{'1', 32},
{'2', 32},
{'3', 32},
{'4', 32},
{'5', 32},
{'6', 32},
{'7', 32},
{'8', 32},
{'9', 32},
2018-04-18 12:14:28 +03:00
{':', 22},
{';', 22},
2018-04-18 12:05:19 +03:00
{'<', 35},
{'=', 35},
{'>', 35},
{'?', 35},
{'@', 50},
{'A', 35},
{'B', 35},
{'C', 34},
{'D', 36},
{'E', 34},
2018-04-18 12:09:44 +03:00
{'F', 34},
2018-04-18 12:05:19 +03:00
{'G', 35},
{'H', 35},
{'I', 22},
{'J', 24},
{'K', 35},
{'L', 32},
{'M', 45},
{'N', 36},
{'O', 38},
{'P', 35},
{'Q', 38},
{'R', 36},
2018-04-18 12:09:44 +03:00
{'S', 35},
2018-04-18 12:05:19 +03:00
{'T', 35},
{'U', 35},
{'V', 37},
{'W', 50},
{'X', 35},
2018-04-21 14:55:19 +03:00
{'Y', 32},
2018-04-18 12:05:19 +03:00
{'Z', 35},
{'[', 35},
2018-04-18 07:27:37 +03:00
{'\\', 35},
2018-04-18 12:05:19 +03:00
{']', 35},
{'^', 35},
{'_', 35},
{'`', 35},
{'a', 32},
{'b', 32},
2018-04-18 12:09:44 +03:00
{'c', 29},
2018-04-18 12:05:19 +03:00
{'d', 32},
{'e', 32},
2018-04-18 12:09:44 +03:00
{'f', 25},
2018-04-18 12:05:19 +03:00
{'g', 32},
{'h', 32},
{'i', 16},
{'j', 16},
{'k', 30},
{'l', 16},
2018-04-18 12:09:44 +03:00
{'m', 47},
{'n', 33},
2018-04-18 12:05:19 +03:00
{'o', 32},
{'p', 32},
{'q', 32},
{'r', 25},
2018-04-18 12:09:44 +03:00
{'s', 31},
{'t', 26},
2018-04-18 12:05:19 +03:00
{'u', 32},
{'v', 32},
{'w', 42},
{'x', 32},
{'y', 32},
{'z', 32},
{'{', 32},
{'|', 32},
{'}', 32},
{'~', 32},
{' ', 20},
2018-04-18 07:27:37 +03:00
{0,0},
};
2018-04-18 08:53:04 +03:00
static int loaded = 0;
2018-04-18 07:27:37 +03:00
__attribute__((constructor))
static void _init_sdf(void) {
/* Load the font. */
_font_cache = hashmap_create_int(10);
2018-04-18 12:05:19 +03:00
load_sprite(&_font_data_thin, "/usr/share/sdf_thin.bmp");
load_sprite(&_font_data_bold, "/usr/share/sdf_bold.bmp");
2018-04-18 08:53:04 +03:00
loaded = 1;
2018-04-18 07:27:37 +03:00
}
2018-04-18 12:05:19 +03:00
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) {
2018-04-18 07:27:37 +03:00
if (ch != ' ' && ch < '!' || ch > '~') {
/* TODO: Draw missing symbol? */
return 0;
}
/* Calculate offset into table above */
if (ch == ' ') {
ch = '~' + 1 - '!';
} else {
ch -= '!';
}
double scale = (double)size / 50.0;
2018-04-18 12:05:19 +03:00
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;
2018-04-18 07:27:37 +03:00
int height = BASE_HEIGHT * ((double)size / 50.0);
2018-04-18 08:53:04 +03:00
2018-04-18 07:27:37 +03:00
/* ignore size */
for (int j = 0; j < height; ++j) {
for (int i = 0; i < size; ++i) {
2018-04-18 07:27:37 +03:00
/* TODO needs to do bilinear filter */
if (fx+i > tmp->width) continue;
if (fy+j > tmp->height) continue;
uint32_t c = SPRITE((tmp), fx+i, fy+j);
double dist = (double)_RED(c) / 255.0;
2018-04-18 08:36:28 +03:00
double edge0 = 0.75 - GAMMA * 1.4142 / (double)size;
double edge1 = 0.75 + GAMMA * 1.4142 / (double)size;
2018-04-18 07:27:37 +03:00
double a = (dist - edge0) / (edge1 - edge0);
if (a < 0.0) a = 0.0;
if (a > 1.0) a = 1.0;
a = a * a * (3 - 2 * a);
GFX(ctx,x+i,y+j) = alpha_blend(GFX(ctx,x+i,y+j), color, rgb(255*a,0,0));
}
}
return width;
}
2018-04-21 10:28:57 +03:00
int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, int size, uint32_t color, int font) {
2018-04-18 12:05:19 +03:00
sprite_t * _font_data = _select_font(font);
2018-04-18 08:36:28 +03:00
2018-04-18 08:53:04 +03:00
if (!loaded) return 0;
2018-04-18 08:36:28 +03:00
double scale = (double)size / 50.0;
int scale_height = scale * _font_data->height;
sprite_t * tmp;
spin_lock(&_sdf_lock);
if (!hashmap_has(_font_cache, (void *)(scale_height | (font << 16)))) {
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);
free(t);
hashmap_set(_font_cache, (void *)(scale_height | (font << 16)), tmp);
} else {
tmp = hashmap_get(_font_cache, (void *)(scale_height | (font << 16)));
}
spin_unlock(&_sdf_lock);
2018-04-18 08:36:28 +03:00
2018-04-18 07:27:37 +03:00
int32_t out_width = 0;
while (*str) {
2018-04-18 12:05:19 +03:00
int w = draw_sdf_character(ctx,x,y,*str,size,color,tmp,font,_font_data);
2018-04-18 07:27:37 +03:00
out_width += w;
x += w;
str++;
}
2018-04-18 08:36:28 +03:00
2018-04-18 07:27:37 +03:00
return out_width;
}
2018-04-18 12:05:19 +03:00
static int char_width(char ch, int font) {
2018-04-18 08:36:28 +03:00
if (ch != ' ' && ch < '!' || ch > '~') {
/* TODO: Draw missing symbol? */
return 0;
}
/* Calculate offset into table above */
if (ch == ' ') {
ch = '~' + 1 - '!';
} else {
ch -= '!';
}
2018-04-18 12:05:19 +03:00
return _select_width(ch, font);
2018-04-18 08:36:28 +03:00
}
2018-04-21 10:28:57 +03:00
int draw_sdf_string_width(const char * str, int size, int font) {
2018-04-18 08:36:28 +03:00
double scale = (double)size / 50.0;
int32_t out_width = 0;
while (*str) {
2018-04-18 12:05:19 +03:00
int w = char_width(*str,font) * scale;
2018-04-18 08:36:28 +03:00
out_width += w;
str++;
}
return out_width;
}