ext_freetype_fonts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 fallbacks[] = {FONT_JAPANESE, FONT_SYMBOLA, -1};
  33. #define SGFX(CTX,x,y,WIDTH) *((uint32_t *)&CTX[((WIDTH) * (y) + (x)) * 4])
  34. static void _load_font(int i, char * name) {
  35. uint8_t * font;
  36. size_t s = 0;
  37. int error;
  38. char tmp[100];
  39. snprintf(tmp, 100, "sys.%s%s", SERVER_NAME, name);
  40. font = (void*)syscall_shm_obtain(tmp, &s);
  41. error = FT_New_Memory_Face(library, font, s, 0, &faces[i]);
  42. if (error) {
  43. fprintf(stderr, "[freetype backend] encountered error\n");
  44. }
  45. error = FT_Set_Pixel_Sizes(faces[i], FONT_SIZE, FONT_SIZE);
  46. if (error) {
  47. fprintf(stderr, "[freetype backend] encountered error\n");
  48. }
  49. }
  50. static void _load_font_f(int i, char * path) {
  51. int error;
  52. error = FT_New_Face(library, path, 0, &faces[i]);
  53. if (error) {
  54. fprintf(stderr, "[freetype backend] encountered error\n");
  55. }
  56. error = FT_Set_Pixel_Sizes(faces[i], FONT_SIZE, FONT_SIZE);
  57. if (error) {
  58. fprintf(stderr, "[freetype backend] encountered error\n");
  59. }
  60. }
  61. static void _load_fonts() {
  62. _load_font(FONT_SANS_SERIF, ".fonts.sans-serif");
  63. _load_font(FONT_SANS_SERIF_BOLD, ".fonts.sans-serif.bold");
  64. _load_font(FONT_SANS_SERIF_ITALIC, ".fonts.sans-serif.italic");
  65. _load_font(FONT_SANS_SERIF_BOLD_ITALIC, ".fonts.sans-serif.bolditalic");
  66. _load_font(FONT_MONOSPACE, ".fonts.monospace");
  67. _load_font(FONT_MONOSPACE_BOLD, ".fonts.monospace.bold");
  68. _load_font(FONT_MONOSPACE_ITALIC, ".fonts.monospace.italic");
  69. _load_font(FONT_MONOSPACE_BOLD_ITALIC, ".fonts.monospace.bolditalic");
  70. _load_font_f(FONT_JAPANESE, "/usr/share/fonts/VLGothic.ttf");
  71. _load_font_f(FONT_SYMBOLA, "/usr/share/fonts/Symbola.ttf");
  72. }
  73. void freetype_set_font_face(int font) {
  74. selected_face = font;
  75. }
  76. static int _old_size = -1;
  77. void freetype_set_font_size(int size) {
  78. if (size == _old_size) return;
  79. _old_size = size;
  80. for (int i = 0; i < FONTS_TOTAL; ++i) {
  81. FT_Set_Pixel_Sizes(faces[i], size, size);
  82. }
  83. }
  84. static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context_t * ctx) {
  85. int i, j, p, q;
  86. int x_max = x + bitmap->width;
  87. int y_max = y + bitmap->rows;
  88. for (j = y, q = 0; j < y_max; j++, q++) {
  89. if (j < 0 || j >= ctx->height) continue;
  90. for ( i = x, p = 0; i < x_max; i++, p++) {
  91. uint32_t a = _ALP(fg);
  92. a = (a * bitmap->buffer[q * bitmap->width + p]) / 255;
  93. uint32_t tmp = premultiply(rgba(_RED(fg),_GRE(fg),_BLU(fg),a));
  94. if (i < 0 || i >= ctx->width) continue;
  95. SGFX(ctx->backbuffer,i,j,ctx->width) = alpha_blend_rgba(SGFX(ctx->backbuffer,i,j,ctx->width),tmp);
  96. }
  97. }
  98. }
  99. void freetype_draw_char(gfx_context_t * ctx, int x, int y, uint32_t fg, uint32_t o) {
  100. int pen_x = x, pen_y = y;
  101. int error;
  102. slot = faces[selected_face]->glyph;
  103. FT_UInt glyph_index;
  104. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  105. if (glyph_index) {
  106. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  107. if (error) {
  108. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  109. return;
  110. }
  111. slot = (faces[selected_face])->glyph;
  112. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  113. error = FT_Render_Glyph((faces[selected_face])->glyph, FT_RENDER_MODE_NORMAL);
  114. if (error) {
  115. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  116. return;
  117. }
  118. }
  119. } else {
  120. int i = 0;
  121. while (!glyph_index && fallbacks[i] != -1) {
  122. int fallback = fallbacks[i++];
  123. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  124. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  125. if (error) {
  126. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  127. return;
  128. }
  129. slot = (faces[fallback])->glyph;
  130. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  131. error = FT_Render_Glyph((faces[fallback])->glyph, FT_RENDER_MODE_NORMAL);
  132. if (error) {
  133. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, ctx);
  140. }
  141. void freetype_draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
  142. slot = faces[selected_face]->glyph;
  143. int pen_x = x, pen_y = y;
  144. int error;
  145. uint8_t * s = (uint8_t *)string;
  146. uint32_t codepoint;
  147. uint32_t state = 0;
  148. while (*s) {
  149. uint32_t o = 0;
  150. while (*s) {
  151. if (!decode(&state, &codepoint, (uint8_t)*s)) {
  152. o = (uint32_t)codepoint;
  153. s++;
  154. goto finished;
  155. } else if (state == UTF8_REJECT) {
  156. state = 0;
  157. }
  158. s++;
  159. }
  160. finished:
  161. if (!o) continue;
  162. FT_UInt glyph_index;
  163. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  164. if (glyph_index) {
  165. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  166. if (error) {
  167. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  168. continue;
  169. }
  170. slot = (faces[selected_face])->glyph;
  171. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  172. error = FT_Render_Glyph((faces[selected_face])->glyph, FT_RENDER_MODE_NORMAL);
  173. if (error) {
  174. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  175. continue;
  176. }
  177. }
  178. } else {
  179. int i = 0;
  180. while (!glyph_index && fallbacks[i] != -1) {
  181. int fallback = fallbacks[i++];
  182. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  183. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  184. if (error) {
  185. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  186. continue;
  187. }
  188. slot = (faces[fallback])->glyph;
  189. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  190. error = FT_Render_Glyph((faces[fallback])->glyph, FT_RENDER_MODE_NORMAL);
  191. if (error) {
  192. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  193. continue;
  194. }
  195. }
  196. }
  197. }
  198. draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, ctx);
  199. pen_x += slot->advance.x >> 6;
  200. pen_y += slot->advance.y >> 6;
  201. }
  202. }
  203. __attribute__((constructor)) static void init_lib(void) {
  204. FT_Init_FreeType(&library);
  205. _load_fonts();
  206. selected_face = FONT_SANS_SERIF;
  207. }