ext_freetype_fonts.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 set_font_face(int font) {
  74. selected_face = font;
  75. }
  76. void set_font_size(int size) {
  77. for (int i = 0; i < FONTS_TOTAL; ++i) {
  78. FT_Set_Pixel_Sizes(faces[i], size, size);
  79. }
  80. }
  81. static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context_t * ctx) {
  82. int i, j, p, q;
  83. int x_max = x + bitmap->width;
  84. int y_max = y + bitmap->rows;
  85. for (j = y, q = 0; j < y_max; j++, q++) {
  86. if (j < 0 || j >= ctx->height) continue;
  87. for ( i = x, p = 0; i < x_max; i++, p++) {
  88. uint32_t a = _ALP(fg);
  89. a = (a * bitmap->buffer[q * bitmap->width + p]) / 255;
  90. uint32_t tmp = premultiply(rgba(_RED(fg),_GRE(fg),_BLU(fg),a));
  91. if (i < 0 || i >= ctx->width) continue;
  92. SGFX(ctx->backbuffer,i,j,ctx->width) = alpha_blend_rgba(SGFX(ctx->backbuffer,i,j,ctx->width),tmp);
  93. }
  94. }
  95. }
  96. void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
  97. slot = faces[selected_face]->glyph;
  98. int pen_x = x, pen_y = y;
  99. int error;
  100. uint8_t * s = (uint8_t *)string;
  101. uint32_t codepoint;
  102. uint32_t state = 0;
  103. while (*s) {
  104. uint32_t o = 0;
  105. while (*s) {
  106. if (!decode(&state, &codepoint, (uint8_t)*s)) {
  107. o = (uint32_t)codepoint;
  108. s++;
  109. goto finished;
  110. } else if (state == UTF8_REJECT) {
  111. state = 0;
  112. }
  113. s++;
  114. }
  115. finished:
  116. if (!o) continue;
  117. FT_UInt glyph_index;
  118. glyph_index = FT_Get_Char_Index( faces[selected_face], o);
  119. if (glyph_index) {
  120. error = FT_Load_Glyph(faces[selected_face], glyph_index, FT_LOAD_DEFAULT);
  121. if (error) {
  122. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  123. continue;
  124. }
  125. slot = (faces[selected_face])->glyph;
  126. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  127. error = FT_Render_Glyph((faces[selected_face])->glyph, FT_RENDER_MODE_NORMAL);
  128. if (error) {
  129. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  130. continue;
  131. }
  132. }
  133. } else {
  134. int i = 0;
  135. while (!glyph_index && fallbacks[i] != -1) {
  136. int fallback = fallbacks[i++];
  137. glyph_index = FT_Get_Char_Index( faces[fallback], o);
  138. error = FT_Load_Glyph(faces[fallback], glyph_index, FT_LOAD_DEFAULT);
  139. if (error) {
  140. fprintf(stderr, "Error loading glyph for '%lu'\n", o);
  141. continue;
  142. }
  143. slot = (faces[fallback])->glyph;
  144. if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
  145. error = FT_Render_Glyph((faces[fallback])->glyph, FT_RENDER_MODE_NORMAL);
  146. if (error) {
  147. fprintf(stderr, "Error rendering glyph for '%lu'\n", o);
  148. continue;
  149. }
  150. }
  151. }
  152. }
  153. draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, ctx);
  154. pen_x += slot->advance.x >> 6;
  155. pen_y += slot->advance.y >> 6;
  156. }
  157. }
  158. __attribute__((constructor)) static void init_lib(void) {
  159. FT_Init_FreeType(&library);
  160. _load_fonts();
  161. selected_face = FONT_SANS_SERIF;
  162. }