text-functionality.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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,
  57. srcdata->draw_effect, (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. if (!srcdata->text)
  67. return;
  68. if (srcdata->custom_width >= 100)
  69. srcdata->cx = srcdata->custom_width;
  70. else
  71. srcdata->cx = get_ft2_text_width(srcdata->text, srcdata);
  72. srcdata->cy = srcdata->max_h;
  73. obs_enter_graphics();
  74. if (srcdata->vbuf != NULL) {
  75. gs_vertbuffer_t tmpvbuf = srcdata->vbuf;
  76. srcdata->vbuf = NULL;
  77. gs_vertexbuffer_destroy(tmpvbuf);
  78. }
  79. srcdata->vbuf = create_uv_vbuffer((uint32_t)wcslen(srcdata->text) * 6,
  80. true);
  81. if (srcdata->custom_width <= 100) goto skip_word_wrap;
  82. if (!srcdata->word_wrap) goto skip_word_wrap;
  83. for (uint32_t i = 0; i <= wcslen(srcdata->text); i++) {
  84. if (i == wcslen(srcdata->text)) goto eos_check;
  85. if (srcdata->text[i] != L' ' && srcdata->text[i] != L'\n')
  86. goto next_char;
  87. eos_check:;
  88. if (x + word_width > srcdata->custom_width) {
  89. if (space_pos != 0)
  90. srcdata->text[space_pos] = L'\n';
  91. x = 0;
  92. }
  93. if (i == wcslen(srcdata->text)) goto eos_skip;
  94. x += word_width;
  95. word_width = 0;
  96. if (srcdata->text[i] == L'\n')
  97. x = 0;
  98. if (srcdata->text[i] == L' ')
  99. space_pos = i;
  100. next_char:;
  101. glyph_index = FT_Get_Char_Index(srcdata->font_face,
  102. srcdata->text[i]);
  103. word_width += src_glyph->xadv;
  104. eos_skip:;
  105. }
  106. skip_word_wrap:;
  107. fill_vertex_buffer(srcdata);
  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) return;
  114. struct vec2 *tvarray = (struct vec2 *)vdata->tvarray[0].array;
  115. uint32_t *col = (uint32_t *)vdata->colors;
  116. FT_UInt glyph_index = 0;
  117. uint32_t dx = 0, dy = srcdata->max_h, max_y = dy;
  118. uint32_t cur_glyph = 0;
  119. if (srcdata->colorbuf != NULL) {
  120. bfree(srcdata->colorbuf);
  121. srcdata->colorbuf = NULL;
  122. }
  123. srcdata->colorbuf = bzalloc(sizeof(uint32_t)*wcslen(srcdata->text) * 6);
  124. for (uint32_t i = 0; i < wcslen(srcdata->text) * 6; i++) {
  125. srcdata->colorbuf[i] = 0xFF000000;
  126. }
  127. for (uint32_t i = 0; i < wcslen(srcdata->text); i++) {
  128. add_linebreak:;
  129. if (srcdata->text[i] != L'\n') goto draw_glyph;
  130. dx = 0; i++;
  131. dy += srcdata->max_h + 4;
  132. if (i == wcslen(srcdata->text)) goto skip_glyph;
  133. if (srcdata->text[i] == L'\n') goto add_linebreak;
  134. draw_glyph:;
  135. // Skip filthy dual byte Windows line breaks
  136. if (srcdata->text[i] == L'\r') goto skip_glyph;
  137. glyph_index = FT_Get_Char_Index(srcdata->font_face,
  138. srcdata->text[i]);
  139. if (src_glyph == NULL)
  140. goto skip_glyph;
  141. if (srcdata->custom_width < 100) goto skip_custom_width;
  142. if (dx + src_glyph->xadv > srcdata->custom_width) {
  143. dx = 0;
  144. dy += srcdata->max_h + 4;
  145. }
  146. skip_custom_width:;
  147. set_v3_rect(vdata->points + (cur_glyph * 6),
  148. (float)dx + (float)src_glyph->xoff,
  149. (float)dy - (float)src_glyph->yoff,
  150. (float)src_glyph->w,
  151. (float)src_glyph->h);
  152. set_v2_uv(tvarray + (cur_glyph * 6),
  153. src_glyph->u,
  154. src_glyph->v,
  155. src_glyph->u2,
  156. src_glyph->v2);
  157. set_rect_colors2(col + (cur_glyph * 6),
  158. 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. #define glyph_pos x + (y*slot->bitmap.pitch)
  183. #define buf_pos (dx + x) + ((dy + y) * texbuf_w)
  184. void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
  185. {
  186. FT_GlyphSlot slot;
  187. FT_UInt glyph_index = 0;
  188. if (!srcdata->font_face)
  189. return;
  190. slot = srcdata->font_face->glyph;
  191. uint32_t dx = srcdata->texbuf_x, dy = srcdata->texbuf_y;
  192. uint8_t alpha;
  193. int32_t cached_glyphs = 0;
  194. for (uint32_t i = 0; i < wcslen(cache_glyphs); i++) {
  195. glyph_index = FT_Get_Char_Index(srcdata->font_face,
  196. cache_glyphs[i]);
  197. if (src_glyph != NULL)
  198. goto skip_glyph;
  199. FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
  200. FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
  201. uint32_t g_w = slot->bitmap.width;
  202. uint32_t g_h = slot->bitmap.rows;
  203. if (srcdata->max_h < g_h) srcdata->max_h = g_h;
  204. if (dx + g_w >= texbuf_w) {
  205. dx = 0;
  206. dy += srcdata->max_h + 1;
  207. }
  208. src_glyph = bzalloc(sizeof(struct glyph_info));
  209. src_glyph->u = (float)dx / (float)texbuf_w;
  210. src_glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
  211. src_glyph->v = (float)dy / (float)texbuf_h;
  212. src_glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
  213. src_glyph->w = g_w;
  214. src_glyph->h = g_h;
  215. src_glyph->yoff = slot->bitmap_top;
  216. src_glyph->xoff = slot->bitmap_left;
  217. src_glyph->xadv = slot->advance.x >> 6;
  218. for (uint32_t y = 0; y < g_h; y++) {
  219. for (uint32_t x = 0; x < g_w; x++) {
  220. alpha = slot->bitmap.buffer[glyph_pos];
  221. srcdata->texbuf[buf_pos] =
  222. 0x00FFFFFF ^ (alpha << 24);
  223. }
  224. }
  225. dx += (g_w + 1);
  226. if (dx >= texbuf_w) {
  227. dx = 0;
  228. dy += srcdata->max_h;
  229. }
  230. cached_glyphs++;
  231. skip_glyph:;
  232. }
  233. srcdata->texbuf_x = dx;
  234. srcdata->texbuf_y = dy;
  235. if (cached_glyphs > 0) {
  236. obs_enter_graphics();
  237. if (srcdata->tex != NULL) {
  238. gs_texture_t tmp_texture = NULL;
  239. tmp_texture = srcdata->tex;
  240. srcdata->tex = NULL;
  241. gs_texture_destroy(tmp_texture);
  242. }
  243. srcdata->tex = gs_texture_create(texbuf_w, texbuf_h,
  244. GS_RGBA, 1, (const uint8_t **)&srcdata->texbuf, 0);
  245. obs_leave_graphics();
  246. }
  247. }
  248. time_t get_modified_timestamp(char *filename)
  249. {
  250. struct stat stats;
  251. // stat is apparently terrifying and horrible, but we only call it once
  252. // every second at most.
  253. stat(filename, &stats);
  254. return stats.st_mtime;
  255. }
  256. void load_text_from_file(struct ft2_source *srcdata, const char *filename)
  257. {
  258. FILE *tmp_file = NULL;
  259. uint32_t filesize = 0;
  260. char *tmp_read = NULL;
  261. uint16_t header = 0;
  262. size_t bytes_read;
  263. tmp_file = fopen(filename, "rb");
  264. if (tmp_file == NULL) {
  265. if (!srcdata->file_load_failed) {
  266. blog(LOG_WARNING, "Failed to open file %s", filename);
  267. srcdata->file_load_failed = true;
  268. }
  269. return;
  270. }
  271. fseek(tmp_file, 0, SEEK_END);
  272. filesize = (uint32_t)ftell(tmp_file);
  273. fseek(tmp_file, 0, SEEK_SET);
  274. bytes_read = fread(&header, 2, 1, tmp_file);
  275. if (bytes_read == 2 && header == 0xFEFF) {
  276. // File is already in UTF-16 format
  277. if (srcdata->text != NULL) {
  278. bfree(srcdata->text);
  279. srcdata->text = NULL;
  280. }
  281. srcdata->text = bzalloc(filesize);
  282. bytes_read = fread(srcdata->text, filesize - 2, 1, tmp_file);
  283. srcdata->m_timestamp =
  284. get_modified_timestamp(srcdata->text_file);
  285. bfree(tmp_read);
  286. fclose(tmp_file);
  287. return;
  288. }
  289. fseek(tmp_file, 0, SEEK_SET);
  290. srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
  291. tmp_read = bzalloc(filesize + 1);
  292. bytes_read = fread(tmp_read, filesize, 1, tmp_file);
  293. fclose(tmp_file);
  294. if (srcdata->text != NULL) {
  295. bfree(srcdata->text);
  296. srcdata->text = NULL;
  297. }
  298. srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
  299. os_utf8_to_wcs(tmp_read, strlen(tmp_read),
  300. srcdata->text, (strlen(tmp_read) + 1));
  301. bfree(tmp_read);
  302. }
  303. void read_from_end(struct ft2_source *srcdata, const char *filename)
  304. {
  305. FILE *tmp_file = NULL;
  306. uint32_t filesize = 0, cur_pos = 0;
  307. char *tmp_read = NULL;
  308. uint16_t value = 0, line_breaks = 0;
  309. size_t bytes_read;
  310. char bvalue;
  311. bool utf16 = false;
  312. tmp_file = fopen(filename, "rb");
  313. if (tmp_file == NULL) {
  314. if (!srcdata->file_load_failed) {
  315. blog(LOG_WARNING, "Failed to open file %s", filename);
  316. srcdata->file_load_failed = true;
  317. }
  318. return;
  319. }
  320. bytes_read = fread(&value, 2, 1, tmp_file);
  321. if (bytes_read == 2 && value == 0xFEFF)
  322. utf16 = true;
  323. fseek(tmp_file, 0, SEEK_END);
  324. filesize = (uint32_t)ftell(tmp_file);
  325. cur_pos = filesize;
  326. while (line_breaks <= 6 && cur_pos != 0) {
  327. if (!utf16) cur_pos--;
  328. else cur_pos -= 2;
  329. fseek(tmp_file, cur_pos, SEEK_SET);
  330. if (!utf16) {
  331. bytes_read = fread(&bvalue, 1, 1, tmp_file);
  332. if (bytes_read == 1 && bvalue == '\n')
  333. line_breaks++;
  334. }
  335. else {
  336. bytes_read = fread(&value, 2, 1, tmp_file);
  337. if (bytes_read == 2 && value == L'\n')
  338. line_breaks++;
  339. }
  340. }
  341. if (cur_pos != 0)
  342. cur_pos += (utf16) ? 2 : 1;
  343. fseek(tmp_file, cur_pos, SEEK_SET);
  344. if (utf16) {
  345. if (srcdata->text != NULL) {
  346. bfree(srcdata->text);
  347. srcdata->text = NULL;
  348. }
  349. srcdata->text = bzalloc(filesize - cur_pos);
  350. bytes_read = fread(srcdata->text, (filesize - cur_pos), 1,
  351. tmp_file);
  352. srcdata->m_timestamp =
  353. get_modified_timestamp(srcdata->text_file);
  354. bfree(tmp_read);
  355. fclose(tmp_file);
  356. return;
  357. }
  358. tmp_read = bzalloc((filesize - cur_pos) + 1);
  359. bytes_read = fread(tmp_read, filesize - cur_pos, 1, tmp_file);
  360. fclose(tmp_file);
  361. if (srcdata->text != NULL) {
  362. bfree(srcdata->text);
  363. srcdata->text = NULL;
  364. }
  365. srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
  366. os_utf8_to_wcs(tmp_read, strlen(tmp_read),
  367. srcdata->text, (strlen(tmp_read) + 1));
  368. srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
  369. bfree(tmp_read);
  370. }
  371. uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
  372. {
  373. FT_GlyphSlot slot = srcdata->font_face->glyph;
  374. FT_UInt glyph_index = 0;
  375. uint32_t w = 0, max_w = 0;
  376. if (!text)
  377. return 0;
  378. for (uint32_t i = 0; i < (uint32_t)wcslen(text); i++) {
  379. glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
  380. FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
  381. if (text[i] == L'\n') w = 0;
  382. else {
  383. w += slot->advance.x >> 6;
  384. if (w > max_w) max_w = w;
  385. }
  386. }
  387. return max_w;
  388. }