text-functionality.c 14 KB

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