-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get subtitles can load fonts from fonts_dir (#6)
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Dependencies/patches/libass-unicode-fontdir-path-win.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- a/libass/ass_fontselect.c 2017-10-27 01:34:18.000000000 +0800 | ||
+++ b/libass/ass_fontselect.c 2020-05-04 19:50:58.926098100 +0800 | ||
@@ -47,6 +47,9 @@ | ||
#include "ass_font.h" | ||
#include "ass_string.h" | ||
|
||
+#include <stringapiset.h> | ||
+#include <windows.h> | ||
+ | ||
#define ABS(x) ((x) < 0 ? -(x) : (x)) | ||
#define MAX_FULLNAME 100 | ||
|
||
@@ -163,26 +166,78 @@ static ASS_FontProviderFuncs ft_funcs = | ||
|
||
static void load_fonts_from_dir(ASS_Library *library, const char *dir) | ||
{ | ||
- DIR *d = opendir(dir); | ||
- if (!d) | ||
+ wchar_t wpath[PATH_MAX]; | ||
+ if (!MultiByteToWideChar(CP_UTF8, 0, dir, -1, wpath, PATH_MAX)) | ||
+ { | ||
+ ass_msg(library, MSGL_WARN, | ||
+ "Error obtaining unicode path for font dir: '%s' (code: %d)", dir, GetLastError()); | ||
return; | ||
- while (1) { | ||
- struct dirent *entry = readdir(d); | ||
- if (!entry) | ||
- break; | ||
- if (entry->d_name[0] == '.') | ||
- continue; | ||
- char fullname[4096]; | ||
- snprintf(fullname, sizeof(fullname), "%s/%s", dir, entry->d_name); | ||
- size_t bufsize = 0; | ||
- ass_msg(library, MSGL_INFO, "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); | ||
+ wchar_t wpath_wc[PATH_MAX]; | ||
+ wcscpy(wpath_wc, wpath); | ||
+ wcscat_s(wpath_wc, PATH_MAX, L"\\*.*"); | ||
+ HANDLE hFind; | ||
+ WIN32_FIND_DATAW FindFileData; | ||
+ if ((hFind = FindFirstFileW(wpath_wc, &FindFileData)) != INVALID_HANDLE_VALUE) { | ||
+ do { | ||
+ if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { | ||
+ HANDLE hFile; | ||
+ wchar_t fullName[PATH_MAX]; | ||
+ wcscpy(fullName, wpath); | ||
+ wcscat_s(fullName, PATH_MAX, L"\\"); | ||
+ wcscat_s(fullName, PATH_MAX, FindFileData.cFileName); | ||
+ hFile = CreateFileW(fullName, | ||
+ GENERIC_READ, | ||
+ FILE_SHARE_READ, | ||
+ NULL, | ||
+ OPEN_EXISTING, | ||
+ FILE_ATTRIBUTE_NORMAL, | ||
+ NULL); | ||
+ if (hFile == INVALID_HANDLE_VALUE) { | ||
+ char tmp[PATH_MAX]; | ||
+ WideCharToMultiByte(CP_UTF8, 0, &fullName, -1, tmp, PATH_MAX, NULL, NULL); | ||
+ ass_msg(library, MSGL_WARN, | ||
+ "Error opening font file: '%s' (code: %d)", tmp, GetLastError()); | ||
+ continue; | ||
+ } | ||
+ DWORD fileSize; | ||
+ char fname[PATH_MAX]; | ||
+ WideCharToMultiByte(CP_UTF8, 0, &FindFileData.cFileName, -1, fname, PATH_MAX, NULL, NULL); | ||
+ ass_msg(library, MSGL_INFO, "Loading font file '%s'", fname); | ||
+ fileSize = GetFileSize(hFile, NULL); | ||
+ if (fileSize == INVALID_FILE_SIZE) { | ||
+ char tmp[PATH_MAX]; | ||
+ WideCharToMultiByte(CP_UTF8, 0, &fullName, -1, tmp, PATH_MAX, NULL, NULL); | ||
+ ass_msg(library, MSGL_WARN, | ||
+ "Error opening font file: '%s' (code: %d)", tmp, GetLastError()); | ||
+ CloseHandle(hFile); | ||
+ continue; | ||
+ } | ||
+ char *data = malloc(fileSize + 1); | ||
+ if (data == NULL) { | ||
+ char tmp[PATH_MAX]; | ||
+ WideCharToMultiByte(CP_UTF8, 0, &fullName, -1, tmp, PATH_MAX, NULL, NULL); | ||
+ ass_msg(library, MSGL_WARN, | ||
+ "Error reading font file: '%s', can not allocate memory for reading, is your system running out of memory?", tmp); | ||
+ CloseHandle(hFile); | ||
+ continue; | ||
+ } | ||
+ DWORD dwBytesRead = 0; | ||
+ if (!ReadFile(hFile, data, fileSize, &dwBytesRead, NULL)) { | ||
+ char tmp[PATH_MAX]; | ||
+ WideCharToMultiByte(CP_UTF8, 0, &fullName, -1, tmp, PATH_MAX, NULL, NULL); | ||
+ ass_msg(library, MSGL_WARN, | ||
+ "Error reading font file: '%s' (code: %d)", tmp, GetLastError()); | ||
+ CloseHandle(hFile); | ||
+ continue; | ||
+ } | ||
+ CloseHandle(hFile); | ||
+ ass_add_font(library, fname, data, dwBytesRead); | ||
+ free(data); | ||
+ } | ||
+ } while (FindNextFileW(hFind, &FindFileData)); | ||
+ FindClose(hFind); | ||
+ } | ||
} | ||
|
||
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters