text-freetype2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #include "find-font.h"
  22. FT_Library ft2_lib;
  23. OBS_DECLARE_MODULE()
  24. OBS_MODULE_USE_DEFAULT_LOCALE("text-freetype2", "en-US")
  25. MODULE_EXPORT const char *obs_module_description(void)
  26. {
  27. return "FreeType2 text source";
  28. }
  29. uint32_t texbuf_w = 2048, texbuf_h = 2048;
  30. static struct obs_source_info freetype2_source_info_v1 = {
  31. .id = "text_ft2_source",
  32. .type = OBS_SOURCE_TYPE_INPUT,
  33. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CAP_OBSOLETE |
  34. OBS_SOURCE_CUSTOM_DRAW,
  35. .get_name = ft2_source_get_name,
  36. .create = ft2_source_create,
  37. .destroy = ft2_source_destroy,
  38. .update = ft2_source_update,
  39. .get_width = ft2_source_get_width,
  40. .get_height = ft2_source_get_height,
  41. .video_render = ft2_source_render,
  42. .video_tick = ft2_video_tick,
  43. .get_defaults = ft2_source_defaults_v1,
  44. .get_properties = ft2_source_properties,
  45. .icon_type = OBS_ICON_TYPE_TEXT,
  46. };
  47. static struct obs_source_info freetype2_source_info_v2 = {
  48. .id = "text_ft2_source",
  49. .version = 2,
  50. .type = OBS_SOURCE_TYPE_INPUT,
  51. .output_flags = OBS_SOURCE_VIDEO |
  52. #ifdef _WIN32
  53. OBS_SOURCE_DEPRECATED |
  54. #endif
  55. OBS_SOURCE_CUSTOM_DRAW,
  56. .get_name = ft2_source_get_name,
  57. .create = ft2_source_create,
  58. .destroy = ft2_source_destroy,
  59. .update = ft2_source_update,
  60. .get_width = ft2_source_get_width,
  61. .get_height = ft2_source_get_height,
  62. .video_render = ft2_source_render,
  63. .video_tick = ft2_video_tick,
  64. .get_defaults = ft2_source_defaults_v2,
  65. .get_properties = ft2_source_properties,
  66. .missing_files = ft2_missing_files,
  67. .icon_type = OBS_ICON_TYPE_TEXT,
  68. };
  69. static bool plugin_initialized = false;
  70. static void init_plugin(void)
  71. {
  72. if (plugin_initialized)
  73. return;
  74. FT_Init_FreeType(&ft2_lib);
  75. if (ft2_lib == NULL) {
  76. blog(LOG_WARNING, "FT2-text: Failed to initialize FT2.");
  77. return;
  78. }
  79. if (!load_cached_os_font_list())
  80. load_os_font_list();
  81. plugin_initialized = true;
  82. }
  83. bool obs_module_load()
  84. {
  85. char *config_dir = obs_module_config_path(NULL);
  86. if (config_dir) {
  87. os_mkdirs(config_dir);
  88. bfree(config_dir);
  89. }
  90. obs_register_source(&freetype2_source_info_v1);
  91. obs_register_source(&freetype2_source_info_v2);
  92. return true;
  93. }
  94. void obs_module_unload(void)
  95. {
  96. if (plugin_initialized) {
  97. free_os_font_list();
  98. FT_Done_FreeType(ft2_lib);
  99. }
  100. }
  101. static const char *ft2_source_get_name(void *unused)
  102. {
  103. UNUSED_PARAMETER(unused);
  104. return obs_module_text("TextFreetype2");
  105. }
  106. static uint32_t ft2_source_get_width(void *data)
  107. {
  108. struct ft2_source *srcdata = data;
  109. return srcdata->cx + srcdata->outline_width;
  110. }
  111. static uint32_t ft2_source_get_height(void *data)
  112. {
  113. struct ft2_source *srcdata = data;
  114. return srcdata->cy + srcdata->outline_width;
  115. }
  116. static bool from_file_modified(obs_properties_t *props, obs_property_t *prop,
  117. obs_data_t *settings)
  118. {
  119. UNUSED_PARAMETER(prop);
  120. bool from_file = obs_data_get_bool(settings, "from_file");
  121. obs_property_t *text = obs_properties_get(props, "text");
  122. obs_property_t *text_file = obs_properties_get(props, "text_file");
  123. obs_property_set_visible(text, !from_file);
  124. obs_property_set_visible(text_file, from_file);
  125. return true;
  126. }
  127. static obs_properties_t *ft2_source_properties(void *unused)
  128. {
  129. UNUSED_PARAMETER(unused);
  130. obs_properties_t *props = obs_properties_create();
  131. //obs_property_t *prop;
  132. // TODO:
  133. // Scrolling. Can't think of a way to do it with the render
  134. // targets currently being broken. (0.4.2)
  135. // Better/pixel shader outline/drop shadow
  136. // Some way to pull text files from network, I dunno
  137. obs_properties_add_font(props, "font", obs_module_text("Font"));
  138. obs_property_t *from_file = obs_properties_add_list(
  139. props, "from_file", obs_module_text("TextInputMode"),
  140. OBS_COMBO_TYPE_RADIO, OBS_COMBO_FORMAT_BOOL);
  141. obs_property_list_add_bool(
  142. from_file, obs_module_text("TextInputMode.Manual"), false);
  143. obs_property_list_add_bool(
  144. from_file, obs_module_text("TextInputMode.FromFile"), true);
  145. obs_property_set_modified_callback(from_file, from_file_modified);
  146. obs_property_t *text_file = obs_properties_add_path(
  147. props, "text_file", obs_module_text("TextFile"), OBS_PATH_FILE,
  148. obs_module_text("TextFileFilter"), NULL);
  149. obs_property_set_long_description(text_file,
  150. obs_module_text("TextFile.Encoding"));
  151. obs_properties_add_text(props, "text", obs_module_text("Text"),
  152. OBS_TEXT_MULTILINE);
  153. obs_properties_add_bool(props, "antialiasing",
  154. obs_module_text("Antialiasing"));
  155. obs_properties_add_bool(props, "log_mode",
  156. obs_module_text("ChatLogMode"));
  157. obs_properties_add_int(props, "log_lines",
  158. obs_module_text("ChatLogLines"), 1, 1000, 1);
  159. obs_properties_add_color_alpha(props, "color1",
  160. obs_module_text("Color1"));
  161. obs_properties_add_color_alpha(props, "color2",
  162. obs_module_text("Color2"));
  163. obs_properties_add_bool(props, "outline", obs_module_text("Outline"));
  164. obs_properties_add_bool(props, "drop_shadow",
  165. obs_module_text("DropShadow"));
  166. obs_properties_add_int(props, "custom_width",
  167. obs_module_text("CustomWidth"), 0, 4096, 1);
  168. obs_properties_add_bool(props, "word_wrap",
  169. obs_module_text("WordWrap"));
  170. return props;
  171. }
  172. static void ft2_source_destroy(void *data)
  173. {
  174. struct ft2_source *srcdata = data;
  175. if (srcdata->font_face != NULL) {
  176. FT_Done_Face(srcdata->font_face);
  177. srcdata->font_face = NULL;
  178. }
  179. for (uint32_t i = 0; i < num_cache_slots; i++) {
  180. if (srcdata->cacheglyphs[i] != NULL) {
  181. bfree(srcdata->cacheglyphs[i]);
  182. srcdata->cacheglyphs[i] = NULL;
  183. }
  184. }
  185. if (srcdata->font_name != NULL)
  186. bfree(srcdata->font_name);
  187. if (srcdata->font_style != NULL)
  188. bfree(srcdata->font_style);
  189. if (srcdata->text != NULL)
  190. bfree(srcdata->text);
  191. if (srcdata->texbuf != NULL)
  192. bfree(srcdata->texbuf);
  193. if (srcdata->colorbuf != NULL)
  194. bfree(srcdata->colorbuf);
  195. if (srcdata->text_file != NULL)
  196. bfree(srcdata->text_file);
  197. obs_enter_graphics();
  198. if (srcdata->tex != NULL) {
  199. gs_texture_destroy(srcdata->tex);
  200. srcdata->tex = NULL;
  201. }
  202. if (srcdata->vbuf != NULL) {
  203. gs_vertexbuffer_destroy(srcdata->vbuf);
  204. srcdata->vbuf = NULL;
  205. }
  206. if (srcdata->draw_effect != NULL) {
  207. gs_effect_destroy(srcdata->draw_effect);
  208. srcdata->draw_effect = NULL;
  209. }
  210. obs_leave_graphics();
  211. bfree(srcdata);
  212. }
  213. static void ft2_source_render(void *data, gs_effect_t *effect)
  214. {
  215. struct ft2_source *srcdata = data;
  216. if (srcdata == NULL)
  217. return;
  218. if (srcdata->tex == NULL || srcdata->vbuf == NULL)
  219. return;
  220. if (srcdata->text == NULL || *srcdata->text == 0)
  221. return;
  222. gs_reset_blend_state();
  223. if (srcdata->outline_text)
  224. draw_outlines(srcdata);
  225. if (srcdata->drop_shadow)
  226. draw_drop_shadow(srcdata);
  227. draw_uv_vbuffer(srcdata->vbuf, srcdata->tex, srcdata->draw_effect,
  228. (uint32_t)wcslen(srcdata->text) * 6);
  229. UNUSED_PARAMETER(effect);
  230. }
  231. static void ft2_video_tick(void *data, float seconds)
  232. {
  233. struct ft2_source *srcdata = data;
  234. if (srcdata == NULL)
  235. return;
  236. if (!srcdata->from_file || !srcdata->text_file)
  237. return;
  238. if (os_gettime_ns() - srcdata->last_checked >= 1000000000) {
  239. time_t t = get_modified_timestamp(srcdata->text_file);
  240. srcdata->last_checked = os_gettime_ns();
  241. if (srcdata->update_file) {
  242. if (srcdata->log_mode)
  243. read_from_end(srcdata, srcdata->text_file);
  244. else
  245. load_text_from_file(srcdata,
  246. srcdata->text_file);
  247. cache_glyphs(srcdata, srcdata->text);
  248. set_up_vertex_buffer(srcdata);
  249. srcdata->update_file = false;
  250. }
  251. if (srcdata->m_timestamp != t) {
  252. srcdata->m_timestamp = t;
  253. srcdata->update_file = true;
  254. }
  255. }
  256. UNUSED_PARAMETER(seconds);
  257. }
  258. static bool init_font(struct ft2_source *srcdata)
  259. {
  260. FT_Long index;
  261. const char *path = get_font_path(srcdata->font_name, srcdata->font_size,
  262. srcdata->font_style,
  263. srcdata->font_flags, &index);
  264. if (!path)
  265. return false;
  266. if (srcdata->font_face != NULL) {
  267. FT_Done_Face(srcdata->font_face);
  268. srcdata->font_face = NULL;
  269. }
  270. return FT_New_Face(ft2_lib, path, index, &srcdata->font_face) == 0;
  271. }
  272. static void ft2_source_update(void *data, obs_data_t *settings)
  273. {
  274. struct ft2_source *srcdata = data;
  275. obs_data_t *font_obj = obs_data_get_obj(settings, "font");
  276. bool vbuf_needs_update = false;
  277. bool word_wrap = false;
  278. uint32_t color[2];
  279. uint32_t custom_width = 0;
  280. const char *font_name = obs_data_get_string(font_obj, "face");
  281. const char *font_style = obs_data_get_string(font_obj, "style");
  282. uint16_t font_size = (uint16_t)obs_data_get_int(font_obj, "size");
  283. uint32_t font_flags = (uint32_t)obs_data_get_int(font_obj, "flags");
  284. if (!font_obj)
  285. return;
  286. srcdata->outline_width = 0;
  287. srcdata->drop_shadow = obs_data_get_bool(settings, "drop_shadow");
  288. srcdata->outline_text = obs_data_get_bool(settings, "outline");
  289. if (srcdata->outline_text && srcdata->drop_shadow)
  290. srcdata->outline_width = 6;
  291. else if (srcdata->outline_text || srcdata->drop_shadow)
  292. srcdata->outline_width = 4;
  293. word_wrap = obs_data_get_bool(settings, "word_wrap");
  294. color[0] = (uint32_t)obs_data_get_int(settings, "color1");
  295. color[1] = (uint32_t)obs_data_get_int(settings, "color2");
  296. custom_width = (uint32_t)obs_data_get_int(settings, "custom_width");
  297. if (custom_width >= 100) {
  298. if (custom_width != srcdata->custom_width) {
  299. srcdata->custom_width = custom_width;
  300. vbuf_needs_update = true;
  301. }
  302. } else {
  303. if (srcdata->custom_width >= 100)
  304. vbuf_needs_update = true;
  305. srcdata->custom_width = 0;
  306. }
  307. if (word_wrap != srcdata->word_wrap) {
  308. srcdata->word_wrap = word_wrap;
  309. vbuf_needs_update = true;
  310. }
  311. if (color[0] != srcdata->color[0] || color[1] != srcdata->color[1]) {
  312. srcdata->color[0] = color[0];
  313. srcdata->color[1] = color[1];
  314. vbuf_needs_update = true;
  315. }
  316. bool from_file = obs_data_get_bool(settings, "from_file");
  317. bool chat_log_mode = obs_data_get_bool(settings, "log_mode");
  318. uint32_t log_lines = (uint32_t)obs_data_get_int(settings, "log_lines");
  319. if (srcdata->log_lines != log_lines) {
  320. srcdata->log_lines = log_lines;
  321. vbuf_needs_update = true;
  322. }
  323. if (srcdata->log_mode != chat_log_mode) {
  324. srcdata->log_mode = chat_log_mode;
  325. vbuf_needs_update = true;
  326. }
  327. if (ft2_lib == NULL)
  328. goto error;
  329. const size_t texbuf_size = (size_t)texbuf_w * (size_t)texbuf_h;
  330. if (srcdata->draw_effect == NULL) {
  331. char *effect_file = NULL;
  332. char *error_string = NULL;
  333. effect_file = obs_module_file("text_default.effect");
  334. if (effect_file) {
  335. obs_enter_graphics();
  336. srcdata->draw_effect = gs_effect_create_from_file(
  337. effect_file, &error_string);
  338. obs_leave_graphics();
  339. bfree(effect_file);
  340. if (error_string != NULL)
  341. bfree(error_string);
  342. }
  343. }
  344. if (srcdata->font_size != font_size || srcdata->from_file != from_file)
  345. vbuf_needs_update = true;
  346. const bool new_aa_setting = obs_data_get_bool(settings, "antialiasing");
  347. const bool aa_changed = srcdata->antialiasing != new_aa_setting;
  348. if (aa_changed) {
  349. srcdata->antialiasing = new_aa_setting;
  350. if (srcdata->texbuf != NULL) {
  351. memset(srcdata->texbuf, 0, texbuf_size);
  352. }
  353. cache_standard_glyphs(srcdata);
  354. }
  355. srcdata->file_load_failed = false;
  356. srcdata->from_file = from_file;
  357. if (srcdata->font_name != NULL) {
  358. if (strcmp(font_name, srcdata->font_name) == 0 &&
  359. strcmp(font_style, srcdata->font_style) == 0 &&
  360. font_flags == srcdata->font_flags &&
  361. font_size == srcdata->font_size)
  362. goto skip_font_load;
  363. bfree(srcdata->font_name);
  364. bfree(srcdata->font_style);
  365. srcdata->font_name = NULL;
  366. srcdata->font_style = NULL;
  367. srcdata->max_h = 0;
  368. vbuf_needs_update = true;
  369. }
  370. srcdata->font_name = bstrdup(font_name);
  371. srcdata->font_style = bstrdup(font_style);
  372. srcdata->font_size = font_size;
  373. srcdata->font_flags = font_flags;
  374. if (!init_font(srcdata) || srcdata->font_face == NULL) {
  375. blog(LOG_WARNING, "FT2-text: Failed to load font %s",
  376. srcdata->font_name);
  377. goto error;
  378. } else {
  379. FT_Set_Pixel_Sizes(srcdata->font_face, 0, srcdata->font_size);
  380. FT_Select_Charmap(srcdata->font_face, FT_ENCODING_UNICODE);
  381. }
  382. if (srcdata->texbuf != NULL) {
  383. bfree(srcdata->texbuf);
  384. srcdata->texbuf = NULL;
  385. }
  386. srcdata->texbuf = bzalloc(texbuf_size);
  387. if (srcdata->font_face)
  388. cache_standard_glyphs(srcdata);
  389. skip_font_load:
  390. if (from_file) {
  391. const char *tmp = obs_data_get_string(settings, "text_file");
  392. if (!tmp || !*tmp || !os_file_exists(tmp)) {
  393. const char *emptystr = " ";
  394. bfree(srcdata->text);
  395. srcdata->text = NULL;
  396. os_utf8_to_wcs_ptr(emptystr, strlen(emptystr),
  397. &srcdata->text);
  398. blog(LOG_WARNING,
  399. "FT2-text: Failed to open %s for "
  400. "reading",
  401. tmp);
  402. } else {
  403. if (srcdata->text_file != NULL &&
  404. strcmp(srcdata->text_file, tmp) == 0 &&
  405. !vbuf_needs_update)
  406. goto error;
  407. bfree(srcdata->text_file);
  408. srcdata->text_file = bstrdup(tmp);
  409. if (chat_log_mode)
  410. read_from_end(srcdata, tmp);
  411. else
  412. load_text_from_file(srcdata, tmp);
  413. srcdata->last_checked = os_gettime_ns();
  414. }
  415. } else {
  416. const char *tmp = obs_data_get_string(settings, "text");
  417. if (!tmp)
  418. goto error;
  419. if (srcdata->text != NULL) {
  420. bfree(srcdata->text);
  421. srcdata->text = NULL;
  422. }
  423. os_utf8_to_wcs_ptr(tmp, strlen(tmp), &srcdata->text);
  424. }
  425. if (srcdata->font_face) {
  426. cache_glyphs(srcdata, srcdata->text);
  427. set_up_vertex_buffer(srcdata);
  428. }
  429. error:
  430. obs_data_release(font_obj);
  431. }
  432. #ifdef _WIN32
  433. #define DEFAULT_FACE "Arial"
  434. #elif __APPLE__
  435. #define DEFAULT_FACE "Helvetica"
  436. #else
  437. #define DEFAULT_FACE "Sans Serif"
  438. #endif
  439. static void *ft2_source_create(obs_data_t *settings, obs_source_t *source)
  440. {
  441. struct ft2_source *srcdata = bzalloc(sizeof(struct ft2_source));
  442. srcdata->src = source;
  443. init_plugin();
  444. obs_source_update(source, NULL);
  445. UNUSED_PARAMETER(settings);
  446. return srcdata;
  447. }
  448. static void missing_file_callback(void *src, const char *new_path, void *data)
  449. {
  450. struct ft2_source *s = src;
  451. obs_source_t *source = s->src;
  452. obs_data_t *settings = obs_source_get_settings(source);
  453. obs_data_set_string(settings, "text_file", new_path);
  454. obs_source_update(source, settings);
  455. obs_data_release(settings);
  456. UNUSED_PARAMETER(data);
  457. }
  458. static obs_missing_files_t *ft2_missing_files(void *data)
  459. {
  460. struct ft2_source *s = data;
  461. obs_missing_files_t *files = obs_missing_files_create();
  462. obs_source_t *source = s->src;
  463. obs_data_t *settings = obs_source_get_settings(source);
  464. bool read = obs_data_get_bool(settings, "from_file");
  465. const char *path = obs_data_get_string(settings, "text_file");
  466. if (read && strcmp(path, "") != 0) {
  467. if (!os_file_exists(path)) {
  468. obs_missing_file_t *file = obs_missing_file_create(
  469. path, missing_file_callback,
  470. OBS_MISSING_FILE_SOURCE, s->src, NULL);
  471. obs_missing_files_add_file(files, file);
  472. }
  473. }
  474. obs_data_release(settings);
  475. return files;
  476. }
  477. static void ft2_source_defaults(obs_data_t *settings, int ver)
  478. {
  479. const uint16_t font_size = ver == 1 ? 32 : 256;
  480. obs_data_t *font_obj = obs_data_create();
  481. obs_data_set_default_string(font_obj, "face", DEFAULT_FACE);
  482. obs_data_set_default_int(font_obj, "size", font_size);
  483. obs_data_set_default_int(font_obj, "flags", 0);
  484. obs_data_set_default_string(font_obj, "style", "");
  485. obs_data_set_default_obj(settings, "font", font_obj);
  486. obs_data_release(font_obj);
  487. obs_data_set_default_bool(settings, "antialiasing", true);
  488. obs_data_set_default_bool(settings, "word_wrap", false);
  489. obs_data_set_default_bool(settings, "outline", false);
  490. obs_data_set_default_bool(settings, "drop_shadow", false);
  491. obs_data_set_default_int(settings, "log_lines", 6);
  492. obs_data_set_default_int(settings, "color1", 0xFFFFFFFF);
  493. obs_data_set_default_int(settings, "color2", 0xFFFFFFFF);
  494. }
  495. static void ft2_source_defaults_v1(obs_data_t *settings)
  496. {
  497. ft2_source_defaults(settings, 1);
  498. }
  499. static void ft2_source_defaults_v2(obs_data_t *settings)
  500. {
  501. ft2_source_defaults(settings, 2);
  502. }