ext_freetype_fonts.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * Extension library for freetype font rendering.
  7. */
  8. #include <syscall.h>
  9. #include <toaru/yutani.h>
  10. #include <toaru/graphics.h>
  11. #include <toaru/decodeutf8.h>
  12. #include <ft2build.h>
  13. #include FT_FREETYPE_H
  14. #include FT_CACHE_H
  15. #define SERVER_NAME "fonts"
  16. #define FONT_SANS_SERIF 0
  17. #define FONT_SANS_SERIF_BOLD 1
  18. #define FONT_SANS_SERIF_ITALIC 2
  19. #define FONT_SANS_SERIF_BOLD_ITALIC 3
  20. #define FONT_MONOSPACE 4
  21. #define FONT_MONOSPACE_BOLD 5
  22. #define FONT_MONOSPACE_ITALIC 6
  23. #define FONT_MONOSPACE_BOLD_ITALIC 7
  24. #define FONT_JAPANESE 8
  25. #define FONT_SYMBOLA 9
  26. #define FONTS_TOTAL 10
  27. #define FONT_SIZE 12
  28. static FT_Library library;
  29. static FT_Face faces[FONTS_TOTAL]; /* perhaps make this an array ? */
  30. static FT_GlyphSlot slot;
  31. static int selected_face = 0;
  32. static int _font_size = 12;
  33. static int fallbacks[] = {FONT_JAPANESE, FONT_SYMBOLA, -1};
  34. #define SGFX(CTX,x,y,WIDTH) *((uint32_t *)&CTX[((WIDTH) * (y) + (x)) * 4])
  35. static void _load_font(int i, char * name) {
  36. uint8_t * font;
  37. size_t s = 0;
  38. int error;
  39. char tmp[100];
  40. snprintf(tmp, 100, "sys.%s%s", SERVER_NAME, name);
  41. font = (void*)syscall_shm_obtain(tmp, &s);
  42. error = FT_New_Memory_Face(library, font, s, 0, &faces[i]);
  43. if (error) {
  44. fprintf(stderr, "[freetype backend] encountered error\n");
  45. }
  46. error = FT_Set_Pixel_Sizes(faces[i], FONT_SIZE, FONT_SIZE);
  47. if (error) {
  48. fprintf(stderr, "[freetype backend] encountered error\n");
  49. }
  50. }
  51. static void _load_font_f(int i, char * path) {
  52. int error;
  53. error = FT_New_Face(library, path, 0, &faces[i]);
  54. if (error) {
  55. fprintf(stderr, "[freetype backend] encountered error\n");
  56. }
  57. error = FT_Set_Pixel_Sizes(faces[i], FONT_SIZE, FONT_SIZE);
  58. if (error) {
  59. fprintf(stderr, "[freetype backend] encountered error\n");
  60. }
  61. }
  62. static void _load_fonts() {
  63. _load_font(FONT_SANS_SERIF, ".fonts.sans-serif");
  64. _load_font(FONT_SANS_SERIF_BOLD, ".fonts.sans-serif.bold");
  65. _load_font(FONT_SANS_SERIF_ITALIC, ".fonts.sans-serif.italic");
  66. _load_font(FONT_SANS_SERIF_BOLD_ITALIC, ".fonts.sans-serif.bolditalic");
  67. _load_font(FONT_MONOSPACE, ".fonts.monospace");
  68. _load_font(FONT_MONOSPACE_BOLD, ".fonts.monospace.bold");
  69. _load_font(FONT_MONOSPACE_ITALIC, ".fonts.monospace.italic");
  70. _load_font(FONT_MONOSPACE_BOLD_ITALIC, ".fonts.monospace.bolditalic");
  71. _load_font_f(FONT_JAPANESE, "/usr/share/fonts/VLGothic.ttf");
  72. _load_font_f(FONT_SYMBOLA, "/usr/share/fonts/Symbola.ttf");
  73. }
  74. void freetype_set_font_face(int font) {
  75. selected_face = font;
  76. }
  77. void freetype_set_font_size(int size) {
  78. _font_size = size;
  79. for (int i = 0; i < FONTS_TOTAL; ++i) {
  80. FT_Set_Pixel_Sizes(faces[i], size, size);
  81. }
  82. }
  83. static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context_t * ctx) {
  84. int i, j, p, q;
  85. int x_max = x + bitmap->width;
  86. int y_max = y + bitmap->rows;
  87. for (j = y, q = 0; j < y_max; j++, q++) {
  88. if (j < 0 || j >= ctx->height) continue;
  89. for ( i = x, p = 0; i < x_max; i++, p++) {
  90. uint32_t a = _ALP(fg);
  91. a = (a * bitmap->buffer[q * bitmap->width + p]) / 255;
  92. uint32_t tmp = premultiply(rgba(_RED(fg),_GRE(fg),_BLU(fg),a));
  93. if (i < 0 || i >= ctx->width) continue;
  94. SGFX(ctx->backbuffer,i,j,ctx->width) = alpha_blend_rgba(SGFX(ctx->backbuffer,i,j,ctx->width),tmp);
  95. }
  96. }
  97. }
  98. void freetype_draw_char(gfx_context_t * ctx, int x, int y, uint32_t fg, uint32_t o) {
  99. int pen_x = x, pen_y = y;
  100. int error;
  101. slot = faces[selected_face]->glyph;
  102. FT_UInt glyph_index;
  103. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  104. if (glyph_index) {
  105. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  106. if (error) {
  107. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  108. return;
  109. }
  110. slot = (faces[selected_face])->glyph;
  111. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  112. error = FT_Render_Glyph((faces[selected_face])->glyph, FT_RENDER_MODE_NORMAL);
  113. if (error) {
  114. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  115. return;
  116. }
  117. }
  118. } else {
  119. int i = 0;
  120. while (!glyph_index && fallbacks[i] != -1) {
  121. int fallback = fallbacks[i++];
  122. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  123. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  124. if (error) {
  125. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  126. return;
  127. }
  128. slot = (faces[fallback])->glyph;
  129. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  130. error = FT_Render_Glyph((faces[fallback])->glyph, FT_RENDER_MODE_NORMAL);
  131. if (error) {
  132. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  133. return;
  134. }
  135. }
  136. }
  137. }
  138. draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, ctx);
  139. }
  140. int freetype_draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
  141. slot = faces[selected_face]->glyph;
  142. int pen_x = x, pen_y = y;
  143. int error;
  144. uint8_t * s = (uint8_t *)string;
  145. uint32_t codepoint;
  146. uint32_t state = 0;
  147. while (*s) {
  148. uint32_t o = 0;
  149. while (*s) {
  150. if (!decode(&state, &codepoint, (uint8_t)*s)) {
  151. o = (uint32_t)codepoint;
  152. s++;
  153. goto finished;
  154. } else if (state == UTF8_REJECT) {
  155. state = 0;
  156. }
  157. s++;
  158. }
  159. finished:
  160. if (!o) continue;
  161. FT_UInt glyph_index;
  162. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  163. if (glyph_index) {
  164. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  165. if (error) {
  166. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  167. continue;
  168. }
  169. slot = (faces[selected_face])->glyph;
  170. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  171. error = FT_Render_Glyph((faces[selected_face])->glyph, FT_RENDER_MODE_NORMAL);
  172. if (error) {
  173. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  174. continue;
  175. }
  176. }
  177. } else {
  178. int i = 0;
  179. while (!glyph_index && fallbacks[i] != -1) {
  180. int fallback = fallbacks[i++];
  181. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  182. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  183. if (error) {
  184. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  185. continue;
  186. }
  187. slot = (faces[fallback])->glyph;
  188. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  189. error = FT_Render_Glyph((faces[fallback])->glyph, FT_RENDER_MODE_NORMAL);
  190. if (error) {
  191. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  192. continue;
  193. }
  194. }
  195. }
  196. }
  197. draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, ctx);
  198. pen_x += slot->advance.x >> 6;
  199. pen_y += slot->advance.y >> 6;
  200. }
  201. return pen_x - x;
  202. }
  203. int freetype_draw_string_width(char * string) {
  204. slot = faces[selected_face]->glyph;
  205. int pen_x = 0;
  206. int error;
  207. uint8_t * s = (uint8_t *)string;
  208. uint32_t codepoint;
  209. uint32_t state = 0;
  210. while (*s) {
  211. uint32_t o = 0;
  212. while (*s) {
  213. if (!decode(&state, &codepoint, (uint8_t)*s)) {
  214. o = (uint32_t)codepoint;
  215. s++;
  216. goto finished_width;
  217. } else if (state == UTF8_REJECT) {
  218. state = 0;
  219. }
  220. s++;
  221. }
  222. finished_width:
  223. if (!o) continue;
  224. FT_UInt glyph_index;
  225. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  226. if (glyph_index) {
  227. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  228. if (error) {
  229. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  230. continue;
  231. }
  232. slot = (faces[selected_face])->glyph;
  233. } else {
  234. int i = 0;
  235. while (!glyph_index && fallbacks[i] != -1) {
  236. int fallback = fallbacks[i++];
  237. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  238. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  239. if (error) {
  240. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  241. continue;
  242. }
  243. slot = (faces[fallback])->glyph;
  244. }
  245. }
  246. pen_x += slot->advance.x >> 6;
  247. }
  248. return pen_x;
  249. }
  250. __attribute__((constructor)) static void init_lib(void) {
  251. FT_Init_FreeType(&library);
  252. _load_fonts();
  253. selected_face = FONT_SANS_SERIF;
  254. }
  255. char * freetype_font_name(int i) {
  256. return ((FT_FaceRec *)faces[i])->family_name;
  257. }
  258. FT_Face freetype_get_active_font_face(void) {
  259. return faces[selected_face];
  260. }
  261. void freetype_draw_string_shadow(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string, uint32_t shadow_color, int darkness, int offset_x, int offset_y, double radius) {
  262. #define OFFSET_X 5
  263. #define OFFSET_Y 5
  264. #define WIDTH_PAD 15
  265. #define HEIGHT_PAD 15
  266. gfx_context_t * out_c;
  267. sprite_t * out_s;
  268. size_t width = freetype_draw_string_width(string) + WIDTH_PAD;
  269. size_t height = _font_size + HEIGHT_PAD;
  270. out_s = create_sprite(width, height, ALPHA_EMBEDDED);
  271. out_c = init_graphics_sprite(out_s);
  272. draw_fill(out_c, rgba(0,0,0,0));
  273. freetype_draw_string(out_c, OFFSET_X + offset_x, OFFSET_Y + offset_y + _font_size, shadow_color, string);
  274. /* Two should work okay? */
  275. blur_context_box(out_c, radius);
  276. blur_context_box(out_c, radius);
  277. freetype_draw_string(out_c, OFFSET_X, OFFSET_Y + _font_size, fg, string);
  278. for (int i = 0; i < darkness; ++i) {
  279. draw_sprite(ctx, out_s, x - OFFSET_X, y - OFFSET_Y - _font_size);
  280. }
  281. sprite_free(out_s);
  282. free(out_c);
  283. }