From 571185341f3e58bd57a823070dde30c66491a4ee Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 28 Aug 2015 19:32:46 +0200 Subject: fontselect: reimplement ass_set_fonts_dir() functionality ass_set_fonts_dir() is supposed to enable all fonts in a specific directory. The implementation for it was dropped with the commit introducing the new fontselect code. Some users were relying on it, so we need it back. It used to be implemented using a single fontconfig call. But since this has to work even if fontconfig support is not even compiled, a new implementation is needed. This commit adds very simple and low-effort support for it. It loads all files into memory, and then lets the memory font code do the rest. A more efficient implementation would be possible, for example by implementing a new font provider, which serves get_data requests from open file handles. Anyone who wants to do this is welcome to try, and this commit is just the minimum to restore the lost feature. --- libass/ass.c | 2 +- libass/ass_fontselect.c | 29 +++++++++++++++++++++++++++++ libass/ass_library.h | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) (limited to 'libass') diff --git a/libass/ass.c b/libass/ass.c index 01dd2eb..b51cf2e 100644 --- a/libass/ass.c +++ b/libass/ass.c @@ -1031,7 +1031,7 @@ out: * \param bufsize out: file size * \return pointer to file contents. Caller is responsible for its deallocation. */ -static char *read_file(ASS_Library *library, char *fname, size_t *bufsize) +char *read_file(ASS_Library *library, char *fname, size_t *bufsize) { int res; long sz; diff --git a/libass/ass_fontselect.c b/libass/ass_fontselect.c index 0514d84..6441caa 100644 --- a/libass/ass_fontselect.c +++ b/libass/ass_fontselect.c @@ -28,7 +28,10 @@ #include #include #include +#include #include +#include +#include #include FT_FREETYPE_H #include FT_SFNT_NAMES_H #include FT_TRUETYPE_IDS_H @@ -156,6 +159,28 @@ static ASS_FontProviderFuncs ft_funcs = { NULL }; +static void load_fonts_from_dir(ASS_Library *library, const char *dir) +{ + DIR *d = opendir(dir); + if (!d) + return; + while (1) { + struct dirent *entry = readdir(d); + if (!entry) + break; + char fullname[PATH_MAX]; + snprintf(fullname, sizeof(fullname), "%s/%s", dir, entry->d_name); + size_t bufsize = 0; + ass_msg(library, MSGL_WARN, "Loading font file '%s'", fullname); + void *data = read_file(library, fullname, &bufsize); + if (data) { + ass_add_font(library, entry->d_name, data, bufsize); + free(data); + } + } + closedir(d); +} + /** * \brief Create a bare font provider. * \param selector parent selector. The provider will be attached to it. @@ -870,6 +895,10 @@ ass_embedded_fonts_add_provider(ASS_Library *lib, ASS_FontSelector *selector, if (priv == NULL) return NULL; + if (lib->fonts_dir && lib->fonts_dir[0]) { + load_fonts_from_dir(lib, lib->fonts_dir); + } + for (i = 0; i < lib->num_fontdata; ++i) process_fontdata(priv, lib, ftlib, i); diff --git a/libass/ass_library.h b/libass/ass_library.h index 8faf15e..8144f8e 100644 --- a/libass/ass_library.h +++ b/libass/ass_library.h @@ -38,4 +38,6 @@ struct ass_library { void *msg_callback_data; }; +char *read_file(struct ass_library *library, char *fname, size_t *bufsize); + #endif /* LIBASS_LIBRARY_H */ -- cgit v1.2.3