text-freetype2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. if (srcdata->log_mode != chat_log_mode) {
  305. srcdata->log_mode = chat_log_mode;
  306. vbuf_needs_update = true;
  307. }
  308. if (ft2_lib == NULL)
  309. goto error;
  310. const size_t texbuf_size = (size_t)texbuf_w * (size_t)texbuf_h;
  311. if (srcdata->draw_effect == NULL) {
  312. char *effect_file = NULL;
  313. char *error_string = NULL;
  314. effect_file = obs_module_file("text_default.effect");
  315. if (effect_file) {
  316. obs_enter_graphics();
  317. srcdata->draw_effect = gs_effect_create_from_file(
  318. effect_file, &error_string);
  319. obs_leave_graphics();
  320. bfree(effect_file);
  321. if (error_string != NULL)
  322. bfree(error_string);
  323. }
  324. }
  325. if (srcdata->font_size != font_size || srcdata->from_file != from_file)
  326. vbuf_needs_update = true;
  327. const bool new_aa_setting = obs_data_get_bool(settings, "antialiasing");
  328. const bool aa_changed = srcdata->antialiasing != new_aa_setting;
  329. if (aa_changed) {
  330. srcdata->antialiasing = new_aa_setting;
  331. if (srcdata->texbuf != NULL) {
  332. memset(srcdata->texbuf, 0, texbuf_size);
  333. }
  334. cache_standard_glyphs(srcdata);
  335. }
  336. srcdata->file_load_failed = false;
  337. srcdata->from_file = from_file;
  338. if (srcdata->font_name != NULL) {
  339. if (strcmp(font_name, srcdata->font_name) == 0 &&
  340. strcmp(font_style, srcdata->font_style) == 0 &&
  341. font_flags == srcdata->font_flags &&
  342. font_size == srcdata->font_size)
  343. goto skip_font_load;
  344. bfree(srcdata->font_name);
  345. bfree(srcdata->font_style);
  346. srcdata->font_name = NULL;
  347. srcdata->font_style = NULL;
  348. srcdata->max_h = 0;
  349. vbuf_needs_update = true;
  350. }
  351. srcdata->font_name = bstrdup(font_name);
  352. srcdata->font_style = bstrdup(font_style);
  353. srcdata->font_size = font_size;
  354. srcdata->font_flags = font_flags;
  355. if (!init_font(srcdata) || srcdata->font_face == NULL) {
  356. blog(LOG_WARNING, "FT2-text: Failed to load font %s",
  357. srcdata->font_name);
  358. goto error;
  359. } else {
  360. FT_Set_Pixel_Sizes(srcdata->font_face, 0, srcdata->font_size);
  361. FT_Select_Charmap(srcdata->font_face, FT_ENCODING_UNICODE);
  362. }
  363. if (srcdata->texbuf != NULL) {
  364. bfree(srcdata->texbuf);
  365. srcdata->texbuf = NULL;
  366. }
  367. srcdata->texbuf = bzalloc(texbuf_size);
  368. if (srcdata->font_face)
  369. cache_standard_glyphs(srcdata);
  370. skip_font_load:
  371. if (from_file) {
  372. const char *tmp = obs_data_get_string(settings, "text_file");
  373. if (!tmp || !*tmp || !os_file_exists(tmp)) {
  374. const char *emptystr = " ";
  375. bfree(srcdata->text);
  376. srcdata->text = NULL;
  377. os_utf8_to_wcs_ptr(emptystr, strlen(emptystr),
  378. &srcdata->text);
  379. blog(LOG_WARNING,
  380. "FT2-text: Failed to open %s for "
  381. "reading",
  382. tmp);
  383. } else {
  384. if (srcdata->text_file != NULL &&
  385. strcmp(srcdata->text_file, tmp) == 0 &&
  386. !vbuf_needs_update)
  387. goto error;
  388. bfree(srcdata->text_file);
  389. srcdata->text_file = bstrdup(tmp);
  390. if (chat_log_mode)
  391. read_from_end(srcdata, tmp);
  392. else
  393. load_text_from_file(srcdata, tmp);
  394. srcdata->last_checked = os_gettime_ns();
  395. }
  396. } else {
  397. const char *tmp = obs_data_get_string(settings, "text");
  398. if (!tmp)
  399. goto error;
  400. if (srcdata->text != NULL) {
  401. bfree(srcdata->text);
  402. srcdata->text = NULL;
  403. }
  404. os_utf8_to_wcs_ptr(tmp, strlen(tmp), &srcdata->text);
  405. }
  406. if (srcdata->font_face) {
  407. cache_glyphs(srcdata, srcdata->text);
  408. set_up_vertex_buffer(srcdata);
  409. }
  410. error:
  411. obs_data_release(font_obj);
  412. }
  413. #ifdef _WIN32
  414. #define DEFAULT_FACE "Arial"
  415. #elif __APPLE__
  416. #define DEFAULT_FACE "Helvetica"
  417. #else
  418. #define DEFAULT_FACE "Sans Serif"
  419. #endif
  420. static void *ft2_source_create(obs_data_t *settings, obs_source_t *source)
  421. {
  422. struct ft2_source *srcdata = bzalloc(sizeof(struct ft2_source));
  423. srcdata->src = source;
  424. init_plugin();
  425. obs_source_update(source, NULL);
  426. UNUSED_PARAMETER(settings);
  427. return srcdata;
  428. }
  429. static void missing_file_callback(void *src, const char *new_path, void *data)
  430. {
  431. struct ft2_source *s = src;
  432. obs_source_t *source = s->src;
  433. obs_data_t *settings = obs_source_get_settings(source);
  434. obs_data_set_string(settings, "text_file", new_path);
  435. obs_source_update(source, settings);
  436. obs_data_release(settings);
  437. UNUSED_PARAMETER(data);
  438. }
  439. static obs_missing_files_t *ft2_missing_files(void *data)
  440. {
  441. struct ft2_source *s = data;
  442. obs_missing_files_t *files = obs_missing_files_create();
  443. obs_source_t *source = s->src;
  444. obs_data_t *settings = obs_source_get_settings(source);
  445. bool read = obs_data_get_bool(settings, "from_file");
  446. const char *path = obs_data_get_string(settings, "text_file");
  447. if (read && strcmp(path, "") != 0) {
  448. if (!os_file_exists(path)) {
  449. obs_missing_file_t *file = obs_missing_file_create(
  450. path, missing_file_callback,
  451. OBS_MISSING_FILE_SOURCE, s->src, NULL);
  452. obs_missing_files_add_file(files, file);
  453. }
  454. }
  455. obs_data_release(settings);
  456. return files;
  457. }
  458. static void ft2_source_defaults(obs_data_t *settings, int ver)
  459. {
  460. const uint16_t font_size = ver == 1 ? 32 : 256;
  461. obs_data_t *font_obj = obs_data_create();
  462. obs_data_set_default_string(font_obj, "face", DEFAULT_FACE);
  463. obs_data_set_default_int(font_obj, "size", font_size);
  464. obs_data_set_default_int(font_obj, "flags", 0);
  465. obs_data_set_default_string(font_obj, "style", "");
  466. obs_data_set_default_obj(settings, "font", font_obj);
  467. obs_data_release(font_obj);
  468. obs_data_set_default_bool(settings, "antialiasing", true);
  469. obs_data_set_default_bool(settings, "word_wrap", false);
  470. obs_data_set_default_bool(settings, "outline", false);
  471. obs_data_set_default_bool(settings, "drop_shadow", false);
  472. obs_data_set_default_int(settings, "log_lines", 6);
  473. obs_data_set_default_int(settings, "color1", 0xFFFFFFFF);
  474. obs_data_set_default_int(settings, "color2", 0xFFFFFFFF);
  475. }
  476. static void ft2_source_defaults_v1(obs_data_t *settings)
  477. {
  478. ft2_source_defaults(settings, 1);
  479. }
  480. static void ft2_source_defaults_v2(obs_data_t *settings)
  481. {
  482. ft2_source_defaults(settings, 2);
  483. }