text-freetype2.c 17 KB

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