text-freetype2.c 17 KB

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