text-freetype2.c 15 KB

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