sdf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * Signed Distance Field text rasterization library
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/shm.h>
  11. #include <toaru/graphics.h>
  12. #include <toaru/hashmap.h>
  13. #include <toaru/sdf.h>
  14. #include <toaru/spinlock.h>
  15. #include <toaru/decodeutf8.h>
  16. #include "apps/ununicode.h"
  17. #define BASE_WIDTH 50
  18. #define BASE_HEIGHT 50
  19. static sprite_t _font_data_thin;
  20. static sprite_t _font_data_bold;
  21. static sprite_t _font_data_oblique;
  22. static sprite_t _font_data_bold_oblique;
  23. static sprite_t _font_data_mono;
  24. static sprite_t _font_data_mono_bold;
  25. static sprite_t _font_data_mono_oblique;
  26. static sprite_t _font_data_mono_bold_oblique;
  27. static hashmap_t * _font_cache;
  28. static volatile int _sdf_lock = 0;
  29. static double gamma = 1.7;
  30. struct CharData{
  31. char code;
  32. size_t width_bold;
  33. size_t width_thin;
  34. size_t width_mono;
  35. } _char_data[256];
  36. static int loaded = 0;
  37. static int offset(int ch) {
  38. /* Calculate offset into table above */
  39. return ch;
  40. }
  41. static char * _font_data = NULL;
  42. static size_t _font_data_size = 0;
  43. static void load_font(sprite_t * sprite, int font) {
  44. uint32_t * _font_data_i = (uint32_t*)_font_data;
  45. sprite->width = _font_data_i[font * 3 + 1];
  46. sprite->height = _font_data_i[font * 3 + 2];
  47. int offset = _font_data_i[font * 3 + 3];
  48. sprite->bitmap = (uint32_t *)&_font_data[offset];
  49. sprite->alpha = 0;
  50. sprite->masks = NULL;
  51. sprite->blank = 0;
  52. }
  53. __attribute__((constructor))
  54. static void _init_sdf(void) {
  55. /* Load the font. */
  56. _font_cache = hashmap_create_int(10);
  57. {
  58. char tmp[100];
  59. char * display = getenv("DISPLAY");
  60. if (!display) display = "compositor";
  61. sprintf(tmp, "sys.%s.fonts", display);
  62. _font_data = shm_obtain(tmp, &_font_data_size);
  63. }
  64. if (!_font_data_size) return;
  65. load_font(&_font_data_thin, SDF_FONT_THIN);
  66. load_font(&_font_data_bold, SDF_FONT_BOLD);
  67. load_font(&_font_data_oblique, SDF_FONT_OBLIQUE);
  68. load_font(&_font_data_bold_oblique, SDF_FONT_BOLD_OBLIQUE);
  69. load_font(&_font_data_mono, SDF_FONT_MONO);
  70. load_font(&_font_data_mono_bold, SDF_FONT_MONO_BOLD);
  71. load_font(&_font_data_mono_oblique, SDF_FONT_MONO_OBLIQUE);
  72. load_font(&_font_data_mono_bold_oblique, SDF_FONT_MONO_BOLD_OBLIQUE);
  73. FILE * fi = fopen("/etc/sdf.conf", "r");
  74. char tmp[1024];
  75. char * s = tmp;
  76. for (int i = 0; i < 256; ++i) {
  77. _char_data[i].code = i;
  78. _char_data[i].width_bold = 25;
  79. _char_data[i].width_thin = 20;
  80. _char_data[i].width_mono = 25;
  81. }
  82. while ((s = fgets(tmp, 1024, fi))) {
  83. if (strlen(s) < 1) continue;
  84. int i = offset(*s);
  85. s++; s++;
  86. char t = *s;
  87. s++; s++;
  88. int o = atoi(s);
  89. if (t == 'b') {
  90. _char_data[i].width_bold = o;
  91. } else if (t == 't') {
  92. _char_data[i].width_thin = o;
  93. } else if (t == 'm') {
  94. _char_data[i].width_mono = o;
  95. }
  96. }
  97. fclose(fi);
  98. loaded = 1;
  99. }
  100. static sprite_t * _select_font(int font) {
  101. switch (font) {
  102. case SDF_FONT_BOLD:
  103. return &_font_data_bold;
  104. case SDF_FONT_MONO:
  105. return &_font_data_mono;
  106. case SDF_FONT_MONO_BOLD:
  107. return &_font_data_mono_bold;
  108. case SDF_FONT_MONO_OBLIQUE:
  109. return &_font_data_mono_oblique;
  110. case SDF_FONT_MONO_BOLD_OBLIQUE:
  111. return &_font_data_mono_bold_oblique;
  112. case SDF_FONT_OBLIQUE:
  113. return &_font_data_oblique;
  114. case SDF_FONT_BOLD_OBLIQUE:
  115. return &_font_data_bold_oblique;
  116. case SDF_FONT_THIN:
  117. default:
  118. return &_font_data_thin;
  119. }
  120. }
  121. static int _select_width(int _ch, int font) {
  122. int ch = (_ch >= 0 && _ch <= 128) ? _ch : (int)ununicode(_ch);
  123. switch (font) {
  124. case SDF_FONT_BOLD:
  125. case SDF_FONT_BOLD_OBLIQUE:
  126. return _char_data[ch].width_bold;
  127. case SDF_FONT_MONO:
  128. case SDF_FONT_MONO_BOLD:
  129. case SDF_FONT_MONO_OBLIQUE:
  130. case SDF_FONT_MONO_BOLD_OBLIQUE:
  131. return _char_data[ch].width_mono;
  132. case SDF_FONT_OBLIQUE:
  133. case SDF_FONT_THIN:
  134. default:
  135. return _char_data[ch].width_thin;
  136. }
  137. }
  138. 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, double buffer) {
  139. int ch = (_ch >= 0 && _ch <= 128) ? _ch : (int)ununicode(_ch);
  140. double scale = (double)size / 50.0;
  141. int width = _select_width(ch, font) * scale;
  142. int fx = ((BASE_WIDTH * ch) % _font_data->width) * scale;
  143. int fy = (((BASE_WIDTH * ch) / _font_data->width) * BASE_HEIGHT) * scale;
  144. int height = BASE_HEIGHT * ((double)size / 50.0);
  145. /* ignore size */
  146. for (int j = 0; j < height; ++j) {
  147. if (y + j < 0) continue;
  148. if (y + j >= ctx->height) continue;
  149. if (fy+j >= tmp->height) continue;
  150. for (int i = 0; i < size; ++i) {
  151. /* TODO needs to do bilinear filter */
  152. if (fx+i >= tmp->width) continue;
  153. if (x + i < 0) continue;
  154. if (x + i >= ctx->width) continue;
  155. uint32_t c = SPRITE((tmp), fx+i, fy+j);
  156. double dist = (double)_RED(c) / 255.0;
  157. double edge0 = buffer - gamma * 1.4142 / (double)size;
  158. double edge1 = buffer + gamma * 1.4142 / (double)size;
  159. double a = (dist - edge0) / (edge1 - edge0);
  160. if (a < 0.0) a = 0.0;
  161. if (a > 1.0) a = 1.0;
  162. a = a * a * (3 - 2 * a);
  163. uint32_t f_color = premultiply((color & 0xFFFFFF) | ((uint32_t)(255 * a) << 24));
  164. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(a * _ALP(color)) << 24);
  165. GFX(ctx,x+i,y+j) = alpha_blend_rgba(GFX(ctx,x+i,y+j), f_color);
  166. }
  167. }
  168. return width;
  169. }
  170. int draw_sdf_string_stroke(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, int size, uint32_t color, int font, double _gamma, double stroke) {
  171. sprite_t * _font_data = _select_font(font);
  172. if (!loaded) return 0;
  173. double scale = (double)size / 50.0;
  174. int scale_height = scale * _font_data->height;
  175. sprite_t * tmp;
  176. spin_lock(&_sdf_lock);
  177. if (!hashmap_has(_font_cache, (void *)(scale_height | (font << 16)))) {
  178. tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE);
  179. gfx_context_t * t = init_graphics_sprite(tmp);
  180. draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height);
  181. free(t);
  182. hashmap_set(_font_cache, (void *)(scale_height | (font << 16)), tmp);
  183. } else {
  184. tmp = hashmap_get(_font_cache, (void *)(scale_height | (font << 16)));
  185. }
  186. uint32_t state = 0;
  187. uint32_t c = 0;
  188. int32_t out_width = 0;
  189. gamma = _gamma;
  190. while (*str) {
  191. if (!decode(&state, &c, (unsigned char)*str)) {
  192. int w = draw_sdf_character(ctx,x,y,c,size,color,tmp,font,_font_data, stroke);
  193. out_width += w;
  194. x += w;
  195. }
  196. str++;
  197. }
  198. spin_unlock(&_sdf_lock);
  199. return out_width;
  200. }
  201. 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) {
  202. return draw_sdf_string_stroke(ctx,x,y,str,size,color,font,gamma,0.75);
  203. }
  204. int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, int size, uint32_t color, int font) {
  205. return draw_sdf_string_stroke(ctx,x,y,str,size,color,font,1.7, 0.75);
  206. }
  207. static int char_width(int ch, int font) {
  208. return _select_width(ch, font);
  209. }
  210. int draw_sdf_string_width(const char * str, int size, int font) {
  211. double scale = (double)size / 50.0;
  212. uint32_t state = 0;
  213. uint32_t c = 0;
  214. int32_t out_width = 0;
  215. while (*str) {
  216. if (!decode(&state, &c, (unsigned char)*str)) {
  217. int w = char_width(c,font) * scale;
  218. out_width += w;
  219. } else if (state == UTF8_REJECT) {
  220. state = 0;
  221. }
  222. str++;
  223. }
  224. return out_width;
  225. }