1
0

text-functionality.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. tmp = vdata->colors;
  30. vdata->colors = srcdata->colorbuf;
  31. gs_matrix_push();
  32. for (int32_t i = 0; i < 8; i++) {
  33. gs_matrix_translate3f(offsets[i * 2], offsets[(i * 2) + 1],
  34. 0.0f);
  35. draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
  36. srcdata->draw_effect,
  37. (uint32_t)wcslen(srcdata->text) * 6);
  38. }
  39. gs_matrix_identity();
  40. gs_matrix_pop();
  41. vdata->colors = tmp;
  42. }
  43. void draw_drop_shadow(struct ft2_source *srcdata)
  44. {
  45. // Horrible (hopefully temporary) solution for drop shadow.
  46. uint32_t *tmp;
  47. struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
  48. tmp = vdata->colors;
  49. vdata->colors = srcdata->colorbuf;
  50. gs_matrix_push();
  51. gs_matrix_translate3f(4.0f, 4.0f, 0.0f);
  52. draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
  53. srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6);
  54. gs_matrix_identity();
  55. gs_matrix_pop();
  56. vdata->colors = tmp;
  57. }
  58. void set_up_vertex_buffer(struct ft2_source *srcdata)
  59. {
  60. FT_UInt glyph_index = 0;
  61. uint32_t x = 0, space_pos = 0, word_width = 0;
  62. if (srcdata->custom_width >= 100)
  63. srcdata->cx = srcdata->custom_width;
  64. else
  65. srcdata->cx = get_ft2_text_width(srcdata->text, srcdata);
  66. srcdata->cy = srcdata->max_h;
  67. obs_enter_graphics();
  68. if (srcdata->vbuf != NULL) {
  69. gs_vertbuffer_t tmpvbuf = srcdata->vbuf;
  70. srcdata->vbuf = NULL;
  71. gs_vertexbuffer_destroy(tmpvbuf);
  72. }
  73. srcdata->vbuf = create_uv_vbuffer((uint32_t)wcslen(srcdata->text) * 6,
  74. true);
  75. if (srcdata->custom_width <= 100) goto skip_word_wrap;
  76. if (!srcdata->word_wrap) goto skip_word_wrap;
  77. for (uint32_t i = 0; i <= wcslen(srcdata->text); i++) {
  78. if (i == wcslen(srcdata->text)) goto eos_check;
  79. if (srcdata->text[i] != L' ' && srcdata->text[i] != L'\n')
  80. goto next_char;
  81. eos_check:;
  82. if (x + word_width > srcdata->custom_width) {
  83. if (space_pos != 0)
  84. srcdata->text[space_pos] = L'\n';
  85. x = 0;
  86. }
  87. if (i == wcslen(srcdata->text)) 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,
  96. srcdata->text[i]);
  97. word_width += src_glyph->xadv;
  98. eos_skip:;
  99. }
  100. skip_word_wrap:;
  101. fill_vertex_buffer(srcdata);
  102. obs_leave_graphics();
  103. }
  104. void fill_vertex_buffer(struct ft2_source *srcdata)
  105. {
  106. struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
  107. if (vdata == NULL) return;
  108. struct vec2 *tvarray = (struct vec2 *)vdata->tvarray[0].array;
  109. uint32_t *col = (uint32_t *)vdata->colors;
  110. FT_UInt glyph_index = 0;
  111. uint32_t dx = 0, dy = srcdata->max_h, max_y = dy;
  112. uint32_t cur_glyph = 0;
  113. if (srcdata->colorbuf != NULL) {
  114. bfree(srcdata->colorbuf);
  115. srcdata->colorbuf = NULL;
  116. }
  117. srcdata->colorbuf = bzalloc(sizeof(uint32_t)*wcslen(srcdata->text) * 6);
  118. for (uint32_t i = 0; i < wcslen(srcdata->text) * 6; i++) {
  119. srcdata->colorbuf[i] = 0xFF000000;
  120. }
  121. for (uint32_t i = 0; i < wcslen(srcdata->text); i++) {
  122. add_linebreak:;
  123. if (srcdata->text[i] != L'\n') goto draw_glyph;
  124. dx = 0; i++;
  125. dy += srcdata->max_h + 4;
  126. if (i == wcslen(srcdata->text)) goto skip_glyph;
  127. if (srcdata->text[i] == L'\n') goto add_linebreak;
  128. draw_glyph:;
  129. // Skip filthy dual byte Windows line breaks
  130. if (srcdata->text[i] == L'\r') goto skip_glyph;
  131. glyph_index = FT_Get_Char_Index(srcdata->font_face,
  132. srcdata->text[i]);
  133. if (src_glyph == NULL)
  134. goto skip_glyph;
  135. if (srcdata->custom_width < 100) goto skip_custom_width;
  136. if (dx + src_glyph->xadv > srcdata->custom_width) {
  137. dx = 0;
  138. dy += srcdata->max_h + 4;
  139. }
  140. skip_custom_width:;
  141. set_v3_rect(vdata->points + (cur_glyph * 6),
  142. (float)dx + (float)src_glyph->xoff,
  143. (float)dy - (float)src_glyph->yoff,
  144. (float)src_glyph->w,
  145. (float)src_glyph->h);
  146. set_v2_uv(tvarray + (cur_glyph * 6),
  147. src_glyph->u,
  148. src_glyph->v,
  149. src_glyph->u2,
  150. src_glyph->v2);
  151. set_rect_colors2(col + (cur_glyph * 6),
  152. srcdata->color[0],
  153. srcdata->color[1]);
  154. dx += src_glyph->xadv;
  155. if (dy - (float)src_glyph->yoff + src_glyph->h > max_y)
  156. max_y = dy - src_glyph->yoff + src_glyph->h;
  157. cur_glyph++;
  158. skip_glyph:;
  159. }
  160. srcdata->cy = max_y;
  161. }
  162. void cache_standard_glyphs(struct ft2_source *srcdata)
  163. {
  164. for (uint32_t i = 0; i < num_cache_slots; i++) {
  165. if (srcdata->cacheglyphs[i] != NULL) {
  166. bfree(srcdata->cacheglyphs[i]);
  167. srcdata->cacheglyphs[i] = NULL;
  168. }
  169. }
  170. srcdata->texbuf_x = 0;
  171. srcdata->texbuf_y = 0;
  172. cache_glyphs(srcdata, L"abcdefghijklmnopqrstuvwxyz" \
  173. L"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" \
  174. L"!@#$%^&*()-_=+,<.>/?\\|[]{}`~ \'\"\0");
  175. }
  176. #define glyph_pos x + (y*slot->bitmap.pitch)
  177. #define buf_pos (dx + x) + ((dy + y) * texbuf_w)
  178. void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
  179. {
  180. FT_GlyphSlot slot = srcdata->font_face->glyph;
  181. FT_UInt glyph_index = 0;
  182. uint32_t dx = srcdata->texbuf_x, dy = srcdata->texbuf_y;
  183. uint8_t alpha;
  184. int32_t g_pitch = 0;
  185. int32_t cached_glyphs = 0;
  186. for (uint32_t i = 0; i < wcslen(cache_glyphs); i++) {
  187. glyph_index = FT_Get_Char_Index(srcdata->font_face,
  188. cache_glyphs[i]);
  189. if (src_glyph != NULL)
  190. goto skip_glyph;
  191. FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
  192. FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
  193. uint32_t g_w = slot->bitmap.width;
  194. uint32_t g_h = slot->bitmap.rows;
  195. g_pitch = slot->bitmap.pitch;
  196. if (srcdata->max_h < g_h) srcdata->max_h = g_h;
  197. if (dx + g_w >= texbuf_w) {
  198. dx = 0;
  199. dy += srcdata->max_h + 1;
  200. }
  201. src_glyph = bzalloc(sizeof(struct glyph_info));
  202. src_glyph->u = (float)dx / (float)texbuf_w;
  203. src_glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
  204. src_glyph->v = (float)dy / (float)texbuf_h;
  205. src_glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
  206. src_glyph->w = g_w;
  207. src_glyph->h = g_h;
  208. src_glyph->yoff = slot->bitmap_top;
  209. src_glyph->xoff = slot->bitmap_left;
  210. src_glyph->xadv = slot->advance.x >> 6;
  211. for (uint32_t y = 0; y < g_h; y++) {
  212. for (uint32_t x = 0; x < g_w; x++) {
  213. alpha = slot->bitmap.buffer[glyph_pos];
  214. srcdata->texbuf[buf_pos] =
  215. 0x00FFFFFF ^ (alpha << 24);
  216. }
  217. }
  218. dx += (g_w + 1);
  219. if (dx >= texbuf_w) {
  220. dx = 0;
  221. dy += srcdata->max_h;
  222. }
  223. cached_glyphs++;
  224. skip_glyph:;
  225. }
  226. srcdata->texbuf_x = dx;
  227. srcdata->texbuf_y = dy;
  228. if (cached_glyphs > 0) {
  229. obs_enter_graphics();
  230. if (srcdata->tex != NULL) {
  231. gs_texture_t tmp_texture = NULL;
  232. tmp_texture = srcdata->tex;
  233. srcdata->tex = NULL;
  234. gs_texture_destroy(tmp_texture);
  235. }
  236. srcdata->tex = gs_texture_create(texbuf_w, texbuf_h,
  237. GS_RGBA, 1, (const uint8_t **)&srcdata->texbuf, 0);
  238. obs_leave_graphics();
  239. }
  240. }
  241. time_t get_modified_timestamp(char *filename)
  242. {
  243. struct stat stats;
  244. // stat is apparently terrifying and horrible, but we only call it once
  245. // every second at most.
  246. stat(filename, &stats);
  247. return stats.st_mtime;
  248. }
  249. void load_text_from_file(struct ft2_source *srcdata, const char *filename)
  250. {
  251. FILE *tmp_file = NULL;
  252. uint32_t filesize = 0;
  253. char *tmp_read = NULL;
  254. uint16_t header = 0;
  255. tmp_file = fopen(filename, "rb");
  256. if (tmp_file == NULL) {
  257. blog(LOG_WARNING, "Failed to open file %s", filename);
  258. return;
  259. }
  260. fseek(tmp_file, 0, SEEK_END);
  261. filesize = (uint32_t)ftell(tmp_file);
  262. fseek(tmp_file, 0, SEEK_SET);
  263. fread(&header, 2, 1, tmp_file);
  264. if (header == 0xFEFF) {
  265. // File is already in UTF-16 format
  266. if (srcdata->text != NULL) {
  267. bfree(srcdata->text);
  268. srcdata->text = NULL;
  269. }
  270. srcdata->text = bzalloc(filesize);
  271. fread(srcdata->text, filesize - 2, 1, tmp_file);
  272. srcdata->m_timestamp =
  273. get_modified_timestamp(srcdata->text_file);
  274. bfree(tmp_read);
  275. fclose(tmp_file);
  276. return;
  277. }
  278. fseek(tmp_file, 0, SEEK_SET);
  279. srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
  280. tmp_read = bzalloc(filesize + 1);
  281. fread(tmp_read, filesize, 1, tmp_file);
  282. fclose(tmp_file);
  283. if (srcdata->text != NULL) {
  284. bfree(srcdata->text);
  285. srcdata->text = NULL;
  286. }
  287. srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
  288. os_utf8_to_wcs(tmp_read, strlen(tmp_read),
  289. srcdata->text, (strlen(tmp_read) + 1));
  290. bfree(tmp_read);
  291. }
  292. void read_from_end(struct ft2_source *srcdata, const char *filename)
  293. {
  294. FILE *tmp_file = NULL;
  295. uint32_t filesize = 0, cur_pos = 0;
  296. char *tmp_read = NULL;
  297. uint16_t value = 0, line_breaks = 0;
  298. char bvalue;
  299. bool utf16 = false;
  300. tmp_file = fopen(filename, "rb");
  301. if (tmp_file == NULL) {
  302. blog(LOG_WARNING, "Failed to open file %s", filename);
  303. return;
  304. }
  305. fread(&value, 2, 1, tmp_file);
  306. if (value == 0xFEFF)
  307. utf16 = true;
  308. fseek(tmp_file, 0, SEEK_END);
  309. filesize = (uint32_t)ftell(tmp_file);
  310. cur_pos = filesize;
  311. while (line_breaks <= 6 && cur_pos != 0) {
  312. if (!utf16) cur_pos--;
  313. else cur_pos -= 2;
  314. fseek(tmp_file, cur_pos, SEEK_SET);
  315. if (!utf16) {
  316. fread(&bvalue, 1, 1, tmp_file);
  317. if (bvalue == '\n')
  318. line_breaks++;
  319. }
  320. else {
  321. fread(&value, 2, 1, tmp_file);
  322. if (value == L'\n')
  323. line_breaks++;
  324. }
  325. }
  326. if (cur_pos != 0)
  327. cur_pos += (utf16) ? 2 : 1;
  328. fseek(tmp_file, cur_pos, SEEK_SET);
  329. if (utf16) {
  330. if (srcdata->text != NULL) {
  331. bfree(srcdata->text);
  332. srcdata->text = NULL;
  333. }
  334. srcdata->text = bzalloc(filesize - cur_pos);
  335. fread(srcdata->text, (filesize - cur_pos), 1, tmp_file);
  336. srcdata->m_timestamp =
  337. get_modified_timestamp(srcdata->text_file);
  338. bfree(tmp_read);
  339. fclose(tmp_file);
  340. return;
  341. }
  342. tmp_read = bzalloc((filesize - cur_pos) + 1);
  343. fread(tmp_read, filesize - cur_pos, 1, tmp_file);
  344. fclose(tmp_file);
  345. if (srcdata->text != NULL) {
  346. bfree(srcdata->text);
  347. srcdata->text = NULL;
  348. }
  349. srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
  350. os_utf8_to_wcs(tmp_read, strlen(tmp_read),
  351. srcdata->text, (strlen(tmp_read) + 1));
  352. srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
  353. bfree(tmp_read);
  354. }
  355. uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
  356. {
  357. FT_GlyphSlot slot = srcdata->font_face->glyph;
  358. FT_UInt glyph_index = 0;
  359. uint32_t w = 0, max_w = 0;
  360. for (uint32_t i = 0; i < (uint32_t)wcslen(text); i++) {
  361. glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
  362. FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
  363. if (text[i] == L'\n') w = 0;
  364. else {
  365. w += slot->advance.x >> 6;
  366. if (w > max_w) max_w = w;
  367. }
  368. }
  369. return max_w;
  370. }