text-freetype2.c 15 KB

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