text-functionality.c 14 KB

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