sdf.c 6.1 KB

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