drwl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * drwl - https://codeberg.org/sewn/drwl
  3. *
  4. * Copyright (c) 2023-2024 sewn <sewn@disroot.org>
  5. * Copyright (c) 2024 notchoc <notchoc@disroot.org>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * The UTF-8 Decoder included is from Bjoern Hoehrmann:
  27. * Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  28. * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  29. */
  30. #pragma once
  31. #include <stdlib.h>
  32. #include <fcft/fcft.h>
  33. #include <pixman-1/pixman.h>
  34. enum { ColFg, ColBg, ColBorder }; /* colorscheme index */
  35. typedef struct fcft_font Fnt;
  36. typedef pixman_image_t Img;
  37. typedef struct {
  38. Img *image;
  39. Fnt *font;
  40. uint32_t *scheme;
  41. } Drwl;
  42. #define UTF8_ACCEPT 0
  43. #define UTF8_REJECT 12
  44. #define UTF8_INVALID 0xFFFD
  45. static const uint8_t utf8d[] = {
  46. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  47. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  48. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  49. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  50. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  51. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  52. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  53. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  54. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  55. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  56. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  57. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  58. 12,36,12,12,12,12,12,12,12,12,12,12,
  59. };
  60. static inline uint32_t
  61. utf8decode(uint32_t *state, uint32_t *codep, uint8_t byte)
  62. {
  63. uint32_t type = utf8d[byte];
  64. *codep = (*state != UTF8_ACCEPT) ?
  65. (byte & 0x3fu) | (*codep << 6) :
  66. (0xff >> type) & (byte);
  67. *state = utf8d[256 + *state + type];
  68. return *state;
  69. }
  70. static int
  71. drwl_init(void)
  72. {
  73. fcft_set_scaling_filter(FCFT_SCALING_FILTER_LANCZOS3);
  74. return fcft_init(FCFT_LOG_COLORIZE_AUTO, 0, FCFT_LOG_CLASS_ERROR);
  75. }
  76. static Drwl *
  77. drwl_create(void)
  78. {
  79. Drwl *drwl;
  80. if (!(drwl = calloc(1, sizeof(Drwl))))
  81. return NULL;
  82. return drwl;
  83. }
  84. static void
  85. drwl_setfont(Drwl *drwl, Fnt *font)
  86. {
  87. if (drwl)
  88. drwl->font = font;
  89. }
  90. static void
  91. drwl_setimage(Drwl *drwl, Img *image)
  92. {
  93. if (drwl)
  94. drwl->image = image;
  95. }
  96. static Fnt *
  97. drwl_font_create(Drwl *drwl, size_t count,
  98. const char *names[static count], const char *attributes)
  99. {
  100. Fnt *font = fcft_from_name(count, names, attributes);
  101. if (drwl)
  102. drwl_setfont(drwl, font);
  103. return font;
  104. }
  105. static void
  106. drwl_font_destroy(Fnt *font)
  107. {
  108. fcft_destroy(font);
  109. }
  110. static inline pixman_color_t
  111. convert_color(uint32_t clr)
  112. {
  113. return (pixman_color_t){
  114. ((clr >> 24) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
  115. ((clr >> 16) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
  116. ((clr >> 8) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
  117. (clr & 0xFF) * 0x101
  118. };
  119. }
  120. static void
  121. drwl_setscheme(Drwl *drwl, uint32_t *scm)
  122. {
  123. if (drwl)
  124. drwl->scheme = scm;
  125. }
  126. static Img *
  127. drwl_image_create(Drwl *drwl, unsigned int w, unsigned int h, uint32_t *bits)
  128. {
  129. Img *image;
  130. pixman_region32_t clip;
  131. image = pixman_image_create_bits_no_clear(
  132. PIXMAN_a8r8g8b8, w, h, bits, w * 4);
  133. if (!image)
  134. return NULL;
  135. pixman_region32_init_rect(&clip, 0, 0, w, h);
  136. pixman_image_set_clip_region32(image, &clip);
  137. pixman_region32_fini(&clip);
  138. if (drwl)
  139. drwl_setimage(drwl, image);
  140. return image;
  141. }
  142. static void
  143. drwl_rect(Drwl *drwl,
  144. int x, int y, unsigned int w, unsigned int h,
  145. int filled, int invert)
  146. {
  147. pixman_color_t clr;
  148. if (!drwl || !drwl->scheme || !drwl->image)
  149. return;
  150. clr = convert_color(drwl->scheme[invert ? ColBg : ColFg]);
  151. if (filled)
  152. pixman_image_fill_rectangles(PIXMAN_OP_SRC, drwl->image, &clr, 1,
  153. &(pixman_rectangle16_t){x, y, w, h});
  154. else
  155. pixman_image_fill_rectangles(PIXMAN_OP_SRC, drwl->image, &clr, 4,
  156. (pixman_rectangle16_t[4]){
  157. { x, y, w, 1 },
  158. { x, y + h - 1, w, 1 },
  159. { x, y, 1, h },
  160. { x + w - 1, y, 1, h }});
  161. }
  162. static int
  163. drwl_text(Drwl *drwl,
  164. int x, int y, unsigned int w, unsigned int h,
  165. unsigned int lpad, const char *text, int invert)
  166. {
  167. int ty;
  168. int render = x || y || w || h;
  169. long x_kern;
  170. uint32_t cp = 0, last_cp = 0, state;
  171. pixman_color_t clr;
  172. pixman_image_t *fg_pix = NULL;
  173. int noellipsis = 0;
  174. const struct fcft_glyph *glyph, *eg = NULL;
  175. int fcft_subpixel_mode = FCFT_SUBPIXEL_DEFAULT;
  176. if (!drwl || (render && (!drwl->scheme || !w || !drwl->image)) || !text || !drwl->font)
  177. return 0;
  178. if (!render) {
  179. w = invert ? invert : ~invert;
  180. } else {
  181. clr = convert_color(drwl->scheme[invert ? ColBg : ColFg]);
  182. fg_pix = pixman_image_create_solid_fill(&clr);
  183. drwl_rect(drwl, x, y, w, h, 1, !invert);
  184. x += lpad;
  185. w -= lpad;
  186. }
  187. if (render && (drwl->scheme[ColBg] & 0xFF) != 0xFF)
  188. fcft_subpixel_mode = FCFT_SUBPIXEL_NONE;
  189. if (render)
  190. eg = fcft_rasterize_char_utf32(drwl->font, 0x2026 /* … */, fcft_subpixel_mode);
  191. for (const char *p = text, *pp; pp = p, *p; p++) {
  192. for (state = UTF8_ACCEPT; *p &&
  193. utf8decode(&state, &cp, *p) > UTF8_REJECT; p++)
  194. ;
  195. if (!*p || state == UTF8_REJECT) {
  196. cp = UTF8_INVALID;
  197. if (p > pp)
  198. p--;
  199. }
  200. glyph = fcft_rasterize_char_utf32(drwl->font, cp, fcft_subpixel_mode);
  201. if (!glyph)
  202. continue;
  203. x_kern = 0;
  204. if (last_cp)
  205. fcft_kerning(drwl->font, last_cp, cp, &x_kern, NULL);
  206. last_cp = cp;
  207. ty = y + (h - drwl->font->height) / 2 + drwl->font->ascent;
  208. if (render && !noellipsis && x_kern + glyph->advance.x + eg->advance.x > w &&
  209. *(p + 1) != '\0') {
  210. /* cannot fit ellipsis after current codepoint */
  211. if (drwl_text(drwl, 0, 0, 0, 0, 0, pp, 0) + x_kern <= w) {
  212. noellipsis = 1;
  213. } else {
  214. w -= eg->advance.x;
  215. pixman_image_composite32(
  216. PIXMAN_OP_OVER, fg_pix, eg->pix, drwl->image, 0, 0, 0, 0,
  217. x + eg->x, ty - eg->y, eg->width, eg->height);
  218. }
  219. }
  220. if ((x_kern + glyph->advance.x) > w)
  221. break;
  222. x += x_kern;
  223. if (render && pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)
  224. /* pre-rendered glyphs (eg. emoji) */
  225. pixman_image_composite32(
  226. PIXMAN_OP_OVER, glyph->pix, NULL, drwl->image, 0, 0, 0, 0,
  227. x + glyph->x, ty - glyph->y, glyph->width, glyph->height);
  228. else if (render)
  229. pixman_image_composite32(
  230. PIXMAN_OP_OVER, fg_pix, glyph->pix, drwl->image, 0, 0, 0, 0,
  231. x + glyph->x, ty - glyph->y, glyph->width, glyph->height);
  232. x += glyph->advance.x;
  233. w -= glyph->advance.x;
  234. }
  235. if (render)
  236. pixman_image_unref(fg_pix);
  237. return x + (render ? w : 0);
  238. }
  239. static unsigned int
  240. drwl_font_getwidth(Drwl *drwl, const char *text)
  241. {
  242. if (!drwl || !drwl->font || !text)
  243. return 0;
  244. return drwl_text(drwl, 0, 0, 0, 0, 0, text, 0);
  245. }
  246. static void
  247. drwl_image_destroy(Img *image)
  248. {
  249. pixman_image_unref(image);
  250. }
  251. static void
  252. drwl_destroy(Drwl *drwl)
  253. {
  254. if (drwl->font)
  255. drwl_font_destroy(drwl->font);
  256. if (drwl->image)
  257. drwl_image_destroy(drwl->image);
  258. free(drwl);
  259. }
  260. static void
  261. drwl_fini(void)
  262. {
  263. fcft_fini();
  264. }