text-functionality.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /******************************************************************************
  2. Copyright (C) 2014 by Nibbles
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs-module.h>
  15. #include <util/platform.h>
  16. #include <ft2build.h>
  17. #include FT_FREETYPE_H
  18. #include <sys/stat.h>
  19. #include "text-freetype2.h"
  20. #include "obs-convenience.h"
  21. float offsets[16] = {-2.0f, 0.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 0.0f,
  22. 0.0f, 2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 0.0f};
  23. extern uint32_t texbuf_w, texbuf_h;
  24. void draw_outlines(struct ft2_source *srcdata)
  25. {
  26. if (!srcdata->text)
  27. return;
  28. gs_matrix_push();
  29. for (int32_t i = 0; i < 8; i++) {
  30. gs_matrix_translate3f(offsets[i * 2], offsets[(i * 2) + 1], 0.0f);
  31. draw_uv_vbuffer(srcdata->vbuf, srcdata->tex, srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6,
  32. false);
  33. }
  34. gs_matrix_identity();
  35. gs_matrix_pop();
  36. }
  37. void draw_drop_shadow(struct ft2_source *srcdata)
  38. {
  39. if (!srcdata->text)
  40. return;
  41. gs_matrix_push();
  42. gs_matrix_translate3f(4.0f, 4.0f, 0.0f);
  43. draw_uv_vbuffer(srcdata->vbuf, srcdata->tex, srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6, false);
  44. gs_matrix_identity();
  45. gs_matrix_pop();
  46. }
  47. void set_up_vertex_buffer(struct ft2_source *srcdata)
  48. {
  49. FT_UInt glyph_index = 0;
  50. uint32_t x = 0, space_pos = 0, word_width = 0;
  51. size_t len;
  52. if (!srcdata->text)
  53. return;
  54. if (srcdata->custom_width >= 100)
  55. srcdata->cx = srcdata->custom_width;
  56. else
  57. srcdata->cx = get_ft2_text_width(srcdata->text, srcdata);
  58. srcdata->cy = srcdata->max_h;
  59. obs_enter_graphics();
  60. if (srcdata->vbuf != NULL) {
  61. gs_vertbuffer_t *tmpvbuf = srcdata->vbuf;
  62. srcdata->vbuf = NULL;
  63. gs_vertexbuffer_destroy(tmpvbuf);
  64. }
  65. if (*srcdata->text == 0) {
  66. obs_leave_graphics();
  67. return;
  68. }
  69. srcdata->vbuf = create_uv_vbuffer((uint32_t)wcslen(srcdata->text) * 6, true);
  70. if (srcdata->custom_width <= 100)
  71. goto skip_word_wrap;
  72. if (!srcdata->word_wrap)
  73. goto skip_word_wrap;
  74. len = wcslen(srcdata->text);
  75. for (uint32_t i = 0; i <= len; i++) {
  76. if (i == wcslen(srcdata->text))
  77. goto eos_check;
  78. if (srcdata->text[i] != L' ' && srcdata->text[i] != L'\n')
  79. goto next_char;
  80. eos_check:;
  81. if (x + word_width > srcdata->custom_width) {
  82. if (space_pos != 0)
  83. srcdata->text[space_pos] = L'\n';
  84. x = 0;
  85. }
  86. if (i == wcslen(srcdata->text))
  87. goto eos_skip;
  88. x += word_width;
  89. word_width = 0;
  90. if (srcdata->text[i] == L'\n')
  91. x = 0;
  92. if (srcdata->text[i] == L' ')
  93. space_pos = i;
  94. next_char:;
  95. glyph_index = FT_Get_Char_Index(srcdata->font_face, srcdata->text[i]);
  96. if (src_glyph)
  97. word_width += src_glyph->xadv;
  98. eos_skip:;
  99. }
  100. skip_word_wrap:;
  101. fill_vertex_buffer(srcdata);
  102. gs_vertexbuffer_flush(srcdata->vbuf);
  103. obs_leave_graphics();
  104. }
  105. void fill_vertex_buffer(struct ft2_source *srcdata)
  106. {
  107. struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
  108. if (vdata == NULL || !srcdata->text)
  109. return;
  110. struct vec2 *tvarray = (struct vec2 *)vdata->tvarray[0].array;
  111. uint32_t *col = (uint32_t *)vdata->colors;
  112. FT_UInt glyph_index = 0;
  113. uint32_t dx = 0, dy = srcdata->max_h, max_y = dy;
  114. uint32_t cur_glyph = 0;
  115. uint32_t offset = 0;
  116. size_t len = wcslen(srcdata->text);
  117. if (srcdata->outline_text) {
  118. offset = 2;
  119. dx = offset;
  120. }
  121. for (size_t i = 0; i < len; i++) {
  122. add_linebreak:;
  123. if (srcdata->text[i] != L'\n')
  124. goto draw_glyph;
  125. dx = offset;
  126. i++;
  127. dy += srcdata->max_h + 4;
  128. if (i == wcslen(srcdata->text))
  129. goto skip_glyph;
  130. if (srcdata->text[i] == L'\n')
  131. goto add_linebreak;
  132. draw_glyph:;
  133. // Skip filthy dual byte Windows line breaks
  134. if (srcdata->text[i] == L'\r')
  135. goto skip_glyph;
  136. glyph_index = FT_Get_Char_Index(srcdata->font_face, srcdata->text[i]);
  137. if (src_glyph == NULL)
  138. goto skip_glyph;
  139. if (srcdata->custom_width < 100)
  140. goto skip_custom_width;
  141. if (dx + src_glyph->xadv > srcdata->custom_width) {
  142. dx = offset;
  143. dy += srcdata->max_h + 4;
  144. }
  145. skip_custom_width:;
  146. set_v3_rect(vdata->points + (cur_glyph * 6), (float)dx + (float)src_glyph->xoff,
  147. (float)dy - (float)src_glyph->yoff, (float)src_glyph->w, (float)src_glyph->h);
  148. set_v2_uv(tvarray + (cur_glyph * 6), src_glyph->u, src_glyph->v, src_glyph->u2, src_glyph->v2);
  149. set_rect_colors2(col + (cur_glyph * 6), srcdata->color[0], srcdata->color[1]);
  150. dx += src_glyph->xadv;
  151. if (dy - (float)src_glyph->yoff + src_glyph->h > max_y)
  152. max_y = dy - src_glyph->yoff + src_glyph->h;
  153. cur_glyph++;
  154. skip_glyph:;
  155. }
  156. srcdata->cy = max_y;
  157. }
  158. void cache_standard_glyphs(struct ft2_source *srcdata)
  159. {
  160. for (uint32_t i = 0; i < num_cache_slots; i++) {
  161. if (srcdata->cacheglyphs[i] != NULL) {
  162. bfree(srcdata->cacheglyphs[i]);
  163. srcdata->cacheglyphs[i] = NULL;
  164. }
  165. }
  166. srcdata->texbuf_x = 0;
  167. srcdata->texbuf_y = 0;
  168. cache_glyphs(srcdata, L"abcdefghijklmnopqrstuvwxyz"
  169. L"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  170. L"!@#$%^&*()-_=+,<.>/?\\|[]{}`~ \'\"\0");
  171. }
  172. FT_Render_Mode get_render_mode(struct ft2_source *srcdata)
  173. {
  174. return srcdata->antialiasing ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO;
  175. }
  176. void load_glyph(struct ft2_source *srcdata, const FT_UInt glyph_index, const FT_Render_Mode render_mode)
  177. {
  178. const FT_Int32 load_mode = render_mode == FT_RENDER_MODE_MONO ? FT_LOAD_TARGET_MONO : FT_LOAD_DEFAULT;
  179. FT_Load_Glyph(srcdata->font_face, glyph_index, load_mode);
  180. }
  181. struct glyph_info *init_glyph(FT_GlyphSlot slot, const uint32_t dx, const uint32_t dy, const uint32_t g_w,
  182. const uint32_t g_h)
  183. {
  184. struct glyph_info *glyph = bzalloc(sizeof(struct glyph_info));
  185. glyph->u = (float)dx / (float)texbuf_w;
  186. glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
  187. glyph->v = (float)dy / (float)texbuf_h;
  188. glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
  189. glyph->w = g_w;
  190. glyph->h = g_h;
  191. glyph->yoff = slot->bitmap_top;
  192. glyph->xoff = slot->bitmap_left;
  193. glyph->xadv = slot->advance.x >> 6;
  194. return glyph;
  195. }
  196. uint8_t get_pixel_value(const unsigned char *buf_row, FT_Render_Mode render_mode, const uint32_t x)
  197. {
  198. if (render_mode == FT_RENDER_MODE_NORMAL) {
  199. return buf_row[x];
  200. }
  201. const uint32_t byte_index = x / 8;
  202. const uint8_t bit_index = x % 8;
  203. const bool pixel_set = (buf_row[byte_index] >> (7 - bit_index)) & 1;
  204. return pixel_set ? 255 : 0;
  205. }
  206. void rasterize(struct ft2_source *srcdata, FT_GlyphSlot slot, const FT_Render_Mode render_mode, const uint32_t dx,
  207. const uint32_t dy)
  208. {
  209. /**
  210. * The pitch's absolute value is the number of bytes taken by one bitmap
  211. * row, including padding.
  212. *
  213. * Source: https://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html
  214. */
  215. const int pitch = abs(slot->bitmap.pitch);
  216. for (uint32_t y = 0; y < slot->bitmap.rows; y++) {
  217. const uint32_t row_start = y * pitch;
  218. const uint32_t row = (dy + y) * texbuf_w;
  219. for (uint32_t x = 0; x < slot->bitmap.width; x++) {
  220. const uint32_t row_pixel_position = dx + x;
  221. const uint8_t pixel_value = get_pixel_value(&slot->bitmap.buffer[row_start], render_mode, x);
  222. srcdata->texbuf[row_pixel_position + row] = pixel_value;
  223. }
  224. }
  225. }
  226. void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
  227. {
  228. if (!srcdata->font_face || !cache_glyphs)
  229. return;
  230. FT_GlyphSlot slot = srcdata->font_face->glyph;
  231. uint32_t dx = srcdata->texbuf_x;
  232. uint32_t dy = srcdata->texbuf_y;
  233. int32_t cached_glyphs = 0;
  234. const size_t len = wcslen(cache_glyphs);
  235. const FT_Render_Mode render_mode = get_render_mode(srcdata);
  236. for (size_t i = 0; i < len; i++) {
  237. const FT_UInt glyph_index = FT_Get_Char_Index(srcdata->font_face, cache_glyphs[i]);
  238. if (src_glyph != NULL) {
  239. continue;
  240. }
  241. load_glyph(srcdata, glyph_index, render_mode);
  242. FT_Render_Glyph(slot, render_mode);
  243. const uint32_t g_w = slot->bitmap.width;
  244. const uint32_t g_h = slot->bitmap.rows;
  245. if (srcdata->max_h < g_h) {
  246. srcdata->max_h = g_h;
  247. }
  248. if (dx + g_w >= texbuf_w) {
  249. dx = 0;
  250. dy += srcdata->max_h + 1;
  251. }
  252. if (dy + g_h >= texbuf_h) {
  253. blog(LOG_WARNING, "Out of space trying to render glyphs");
  254. break;
  255. }
  256. src_glyph = init_glyph(slot, dx, dy, g_w, g_h);
  257. rasterize(srcdata, slot, render_mode, dx, dy);
  258. dx += (g_w + 1);
  259. if (dx >= texbuf_w) {
  260. dx = 0;
  261. dy += srcdata->max_h;
  262. }
  263. cached_glyphs++;
  264. }
  265. srcdata->texbuf_x = dx;
  266. srcdata->texbuf_y = dy;
  267. if (cached_glyphs > 0) {
  268. obs_enter_graphics();
  269. if (srcdata->tex != NULL) {
  270. gs_texture_t *tmp_texture = srcdata->tex;
  271. srcdata->tex = NULL;
  272. gs_texture_destroy(tmp_texture);
  273. }
  274. srcdata->tex = gs_texture_create(texbuf_w, texbuf_h, GS_A8, 1, (const uint8_t **)&srcdata->texbuf, 0);
  275. obs_leave_graphics();
  276. }
  277. }
  278. time_t get_modified_timestamp(char *filename)
  279. {
  280. struct stat stats;
  281. // stat is apparently terrifying and horrible, but we only call it once
  282. // every second at most.
  283. if (os_stat(filename, &stats) != 0)
  284. return -1;
  285. return stats.st_mtime;
  286. }
  287. static void remove_cr(wchar_t *source)
  288. {
  289. int j = 0;
  290. for (int i = 0; source[i] != '\0'; ++i) {
  291. if (source[i] != L'\r') {
  292. source[j++] = source[i];
  293. }
  294. }
  295. source[j] = '\0';
  296. }
  297. void load_text_from_file(struct ft2_source *srcdata, const char *filename)
  298. {
  299. FILE *tmp_file = NULL;
  300. uint32_t filesize = 0;
  301. char *tmp_read = NULL;
  302. uint16_t header = 0;
  303. size_t bytes_read;
  304. tmp_file = os_fopen(filename, "rb");
  305. if (tmp_file == NULL) {
  306. if (!srcdata->file_load_failed) {
  307. blog(LOG_WARNING, "Failed to open file %s", filename);
  308. srcdata->file_load_failed = true;
  309. }
  310. return;
  311. }
  312. fseek(tmp_file, 0, SEEK_END);
  313. filesize = (uint32_t)ftell(tmp_file);
  314. fseek(tmp_file, 0, SEEK_SET);
  315. bytes_read = fread(&header, 1, 2, tmp_file);
  316. if (bytes_read == 2 && header == 0xFEFF) {
  317. // File is already in UTF-16 format
  318. if (srcdata->text != NULL) {
  319. bfree(srcdata->text);
  320. srcdata->text = NULL;
  321. }
  322. srcdata->text = bzalloc(filesize);
  323. bytes_read = fread(srcdata->text, filesize - 2, 1, tmp_file);
  324. bfree(tmp_read);
  325. fclose(tmp_file);
  326. return;
  327. }
  328. fseek(tmp_file, 0, SEEK_SET);
  329. tmp_read = bzalloc(filesize + 1);
  330. bytes_read = fread(tmp_read, filesize, 1, tmp_file);
  331. fclose(tmp_file);
  332. if (srcdata->text != NULL) {
  333. bfree(srcdata->text);
  334. srcdata->text = NULL;
  335. }
  336. srcdata->text = bzalloc((strlen(tmp_read) + 1) * sizeof(wchar_t));
  337. os_utf8_to_wcs(tmp_read, strlen(tmp_read), srcdata->text, (strlen(tmp_read) + 1));
  338. remove_cr(srcdata->text);
  339. bfree(tmp_read);
  340. }
  341. void read_from_end(struct ft2_source *srcdata, const char *filename)
  342. {
  343. FILE *tmp_file = NULL;
  344. uint32_t filesize = 0, cur_pos = 0, log_lines = 0;
  345. char *tmp_read = NULL;
  346. uint16_t value = 0, line_breaks = 0;
  347. size_t bytes_read;
  348. char bvalue;
  349. bool utf16 = false;
  350. tmp_file = fopen(filename, "rb");
  351. if (tmp_file == NULL) {
  352. if (!srcdata->file_load_failed) {
  353. blog(LOG_WARNING, "Failed to open file %s", filename);
  354. srcdata->file_load_failed = true;
  355. }
  356. return;
  357. }
  358. bytes_read = fread(&value, 1, 2, tmp_file);
  359. if (bytes_read == 2 && value == 0xFEFF)
  360. utf16 = true;
  361. fseek(tmp_file, 0, SEEK_END);
  362. filesize = (uint32_t)ftell(tmp_file);
  363. cur_pos = filesize;
  364. log_lines = srcdata->log_lines;
  365. while (line_breaks <= log_lines && cur_pos != 0) {
  366. if (!utf16)
  367. cur_pos--;
  368. else
  369. cur_pos -= 2;
  370. fseek(tmp_file, cur_pos, SEEK_SET);
  371. if (!utf16) {
  372. bytes_read = fread(&bvalue, 1, 1, tmp_file);
  373. if (bytes_read == 1 && bvalue == '\n')
  374. line_breaks++;
  375. } else {
  376. bytes_read = fread(&value, 1, 2, tmp_file);
  377. if (bytes_read == 2 && value == L'\n')
  378. line_breaks++;
  379. }
  380. }
  381. if (cur_pos != 0)
  382. cur_pos += (utf16) ? 2 : 1;
  383. fseek(tmp_file, cur_pos, SEEK_SET);
  384. if (utf16) {
  385. if (srcdata->text != NULL) {
  386. bfree(srcdata->text);
  387. srcdata->text = NULL;
  388. }
  389. srcdata->text = bzalloc(filesize - cur_pos);
  390. bytes_read = fread(srcdata->text, (filesize - cur_pos), 1, tmp_file);
  391. remove_cr(srcdata->text);
  392. bfree(tmp_read);
  393. fclose(tmp_file);
  394. return;
  395. }
  396. tmp_read = bzalloc((filesize - cur_pos) + 1);
  397. bytes_read = fread(tmp_read, filesize - cur_pos, 1, tmp_file);
  398. fclose(tmp_file);
  399. if (srcdata->text != NULL) {
  400. bfree(srcdata->text);
  401. srcdata->text = NULL;
  402. }
  403. srcdata->text = bzalloc((strlen(tmp_read) + 1) * sizeof(wchar_t));
  404. os_utf8_to_wcs(tmp_read, strlen(tmp_read), srcdata->text, (strlen(tmp_read) + 1));
  405. remove_cr(srcdata->text);
  406. bfree(tmp_read);
  407. }
  408. uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
  409. {
  410. if (!text) {
  411. return 0;
  412. }
  413. FT_GlyphSlot slot = srcdata->font_face->glyph;
  414. uint32_t w = 0, max_w = 0;
  415. const size_t len = wcslen(text);
  416. for (size_t i = 0; i < len; i++) {
  417. const FT_UInt glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
  418. if (text[i] == L'\n')
  419. w = 0;
  420. else {
  421. if (src_glyph) {
  422. // Use the cached values.
  423. w += src_glyph->xadv;
  424. } else {
  425. load_glyph(srcdata, glyph_index, get_render_mode(srcdata));
  426. w += slot->advance.x >> 6;
  427. }
  428. if (w > max_w)
  429. max_w = w;
  430. }
  431. }
  432. return max_w;
  433. }