text-functionality.c 13 KB

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