sdf.c 6.9 KB

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