toaruos/lib/sdf.c

238 lines
6.1 KiB
C
Raw Normal View History

2018-08-15 04:23:41 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
2018-04-18 07:27:37 +03:00
*
2018-08-15 04:23:41 +03:00
* Signed Distance Field text rasterization library
2018-04-18 07:27:37 +03:00
*/
#include <stdio.h>
2018-04-18 08:36:28 +03:00
#include <stdlib.h>
#include <syscall.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 12:05:19 +03:00
static sprite_t _font_data_thin;
static sprite_t _font_data_bold;
static sprite_t _font_data_mono;
static sprite_t _font_data_mono_bold;
static sprite_t _font_data_mono_oblique;
static sprite_t _font_data_mono_bold_oblique;
2018-04-18 07:27:37 +03:00
static hashmap_t * _font_cache;
static volatile int _sdf_lock = 0;
static double gamma = 1.7;
struct CharData{
2018-04-18 07:27:37 +03:00
char code;
2018-04-18 12:05:19 +03:00
size_t width_bold;
size_t width_thin;
size_t width_mono;
} _char_data[256];
2018-04-18 07:27:37 +03:00
2018-04-18 08:53:04 +03:00
static int loaded = 0;
static int offset(int ch) {
/* Calculate offset into table above */
return ch;
}
static char * _font_data = NULL;
static size_t _font_data_size = 0;
static void load_font(sprite_t * sprite, int font) {
uint32_t * _font_data_i = (uint32_t*)_font_data;
sprite->width = _font_data_i[font * 3 + 1];
sprite->height = _font_data_i[font * 3 + 2];
int offset = _font_data_i[font * 3 + 3];
sprite->bitmap = (uint32_t *)&_font_data[offset];
sprite->alpha = 0;
sprite->masks = NULL;
sprite->blank = 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);
{
char tmp[100];
char * display = getenv("DISPLAY");
if (!display) display = "compositor";
sprintf(tmp, "sys.%s.fonts", display);
_font_data = (char *)syscall_shm_obtain(tmp, &_font_data_size);
}
if (!_font_data_size) return;
load_font(&_font_data_thin, SDF_FONT_THIN);
load_font(&_font_data_bold, SDF_FONT_BOLD);
load_font(&_font_data_mono, SDF_FONT_MONO);
load_font(&_font_data_mono_bold, SDF_FONT_MONO_BOLD);
load_font(&_font_data_mono_oblique, SDF_FONT_MONO_OBLIQUE);
load_font(&_font_data_mono_bold_oblique, SDF_FONT_MONO_BOLD_OBLIQUE);
FILE * fi = fopen("/etc/sdf.conf", "r");
char tmp[1024];
char * s = tmp;
for (int i = 0; i < 256; ++i) {
_char_data[i].code = i;
_char_data[i].width_bold = 25;
_char_data[i].width_thin = 20;
_char_data[i].width_mono = 25;
}
while ((s = fgets(tmp, 1024, fi))) {
if (strlen(s) < 1) continue;
int i = offset(*s);
s++; s++;
char t = *s;
s++; s++;
int o = atoi(s);
if (t == 'b') {
_char_data[i].width_bold = o;
} else if (t == 't') {
_char_data[i].width_thin = o;
} else if (t == 'm') {
_char_data[i].width_mono = o;
}
}
fclose(fi);
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_MONO:
return &_font_data_mono;
case SDF_FONT_MONO_BOLD:
return &_font_data_mono_bold;
case SDF_FONT_MONO_OBLIQUE:
return &_font_data_mono_oblique;
case SDF_FONT_MONO_BOLD_OBLIQUE:
return &_font_data_mono_bold_oblique;
2018-04-18 12:05:19 +03:00
case SDF_FONT_THIN:
default:
return &_font_data_thin;
}
}
static int _select_width(char ch, int font) {
switch (font) {
case SDF_FONT_BOLD:
2018-04-25 08:03:29 +03:00
return _char_data[(int)ch].width_bold;
case SDF_FONT_MONO:
case SDF_FONT_MONO_BOLD:
case SDF_FONT_MONO_OBLIQUE:
case SDF_FONT_MONO_BOLD_OBLIQUE:
return _char_data[(int)ch].width_mono;
2018-04-18 12:05:19 +03:00
case SDF_FONT_THIN:
default:
return _char_data[(int)ch].width_thin;
2018-04-18 12:05:19 +03:00
}
}
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 < 0 || ch > 255) return 0;
2018-04-18 07:27:37 +03:00
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) {
2018-05-11 16:54:16 +03:00
if (y + j < 0) continue;
if (y + j >= ctx->height) continue;
if (fy+j >= tmp->height) continue;
for (int i = 0; i < size; ++i) {
2018-04-18 07:27:37 +03:00
/* TODO needs to do bilinear filter */
2018-05-11 16:54:16 +03:00
if (fx+i >= tmp->width) continue;
if (x + i < 0) continue;
if (x + i >= ctx->width) continue;
2018-04-18 07:27:37 +03:00
uint32_t c = SPRITE((tmp), fx+i, fy+j);
double dist = (double)_RED(c) / 255.0;
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(_ALP(color)*a,0,0));
2018-04-18 07:27:37 +03:00
}
}
return width;
}
int draw_sdf_string_gamma(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, int size, uint32_t color, int font, double _gamma) {
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)));
}
2018-04-18 08:36:28 +03:00
2018-04-18 07:27:37 +03:00
int32_t out_width = 0;
gamma = _gamma;
2018-04-18 07:27:37 +03:00
while (*str) {
int w = draw_sdf_character(ctx,x,y,*((uint8_t *)str),size,color,tmp,font,_font_data);
2018-04-18 07:27:37 +03:00
out_width += w;
x += w;
str++;
}
spin_unlock(&_sdf_lock);
2018-04-18 08:36:28 +03:00
2018-04-18 07:27:37 +03:00
return out_width;
}
2018-04-18 08:36:28 +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) {
return draw_sdf_string_gamma(ctx,x,y,str,size,color,font,1.7);
}
2018-04-18 08:36:28 +03:00
static int char_width(char ch, int font) {
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;
}