<commit_before>--- libass/libass/ass_fontselect.c.orig	2016-04-07 14:00:48.412620215 +0200
+++ libass/libass/ass_fontselect.c	2016-04-07 14:51:29.220686538 +0200
@@ -47,6 +47,9 @@
 #include "ass_font.h"
 #include "ass_string.h"
 
+#include <windows.h>
+#include <tchar.h>
+
 #define ABS(x) ((x) < 0 ? -(x) : (x))
 #define MAX_FULLNAME 100
 
@@ -161,28 +164,61 @@
     .destroy_font      = destroy_font_ft,
 };
 
+static inline char *FromWide (const wchar_t *wide)
+{
+    size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
+    if (len == 0)
+        return NULL;
+
+    char *out = (char *)malloc (len);
+
+    if (out)
+        WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
+    return out;
+}
+
+static inline wchar_t *ToWide (const char *utf8)
+{
+    int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
+    if (len == 0)
+        return NULL;
+
+    wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
+
+    if (out)
+        MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
+    return out;
+}
+
 static void load_fonts_from_dir(ASS_Library *library, const char *dir)
 {
-    DIR *d = opendir(dir);
+    wchar_t *dirw = ToWide(dir);
+    _TDIR *d = _topendir(dirw);
+    free(dirw);
     if (!d)
         return;
     while (1) {
-        struct dirent *entry = readdir(d);
+        struct _tdirent *entry = _treaddir(d);
         if (!entry)
             break;
+        char* d_name = FromWide(entry->d_name);
         if (entry->d_name[0] == '.')
+        {
+            free(d_name);
             continue;
+        }
         char fullname[4096];
-        snprintf(fullname, sizeof(fullname), "%s/%s", dir, entry->d_name);
+        _snprintf(fullname, sizeof(fullname), "%s/%s", dir, 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);
+            ass_add_font(library, d_name, data, bufsize);
             free(data);
         }
+        free(d_name);
     }
-    closedir(d);
+    _tclosedir(d);
 }
 
 /**<commit_msg>Contribs: fix libass compilation for WIndows<commit_after>--- libass-0.13.4/libass/ass_fontselect.c.orig	2016-07-11 23:29:00.000000000 +0200
+++ libass-0.13.4/libass/ass_fontselect.c	2016-10-11 11:14:24.387661958 +0200
@@ -47,6 +47,9 @@
 #include "ass_font.h"
 #include "ass_string.h"
 
+#include <windows.h>
+#include <tchar.h>
+
 #define ABS(x) ((x) < 0 ? -(x) : (x))
 #define MAX_FULLNAME 100
 
@@ -161,28 +164,63 @@
     .destroy_font      = destroy_font_ft,
 };
 
+#ifdef _WIN32
+static inline char *FromWide (const wchar_t *wide)
+{
+    size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
+    if (len == 0)
+        return NULL;
+
+    char *out = (char *)malloc (len);
+
+    if (out)
+        WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
+    return out;
+}
+
+static inline wchar_t *ToWide (const char *utf8)
+{
+    int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
+    if (len == 0)
+        return NULL;
+
+    wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
+
+    if (out)
+        MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
+    return out;
+}
+#endif
+
 static void load_fonts_from_dir(ASS_Library *library, const char *dir)
 {
-    DIR *d = opendir(dir);
+    wchar_t *dirw = ToWide(dir);
+    _TDIR *d = _topendir(dirw);
+    free(dirw);
     if (!d)
         return;
     while (1) {
-        struct dirent *entry = readdir(d);
+        struct _tdirent *entry = _treaddir(d);
         if (!entry)
             break;
+        char* d_name = FromWide(entry->d_name);
         if (entry->d_name[0] == '.')
+        {
+            free(d_name);
             continue;
+        }
         char fullname[4096];
-        snprintf(fullname, sizeof(fullname), "%s/%s", dir, entry->d_name);
+        _snprintf(fullname, sizeof(fullname), "%s/%s", dir, 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);
+            ass_add_font(library, d_name, data, bufsize);
             free(data);
         }
+        free(d_name);
     }
-    closedir(d);
+    _tclosedir(d);
 }
 
 /**
{'fraction_non_alphanumeric': 0.16896705253784505, 'fraction_numerical': 0.04674977738201247, 'mean_word_length': 2.480247869868319, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1989030', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1754, 'n_words': 394}