find-font-windows.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include <util/dstr.h>
  2. #include <util/darray.h>
  3. #include <util/crc32.h>
  4. #include "find-font.h"
  5. #include "text-freetype2.h"
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <shellapi.h>
  9. #include <shlobj.h>
  10. extern DARRAY(struct font_path_info) font_list;
  11. extern void save_font_list(void);
  12. struct mac_font_mapping {
  13. unsigned short encoding_id;
  14. unsigned short language_id;
  15. unsigned int code_page;
  16. };
  17. #define TT_MAC_LANGID_ANY 0xFFFF
  18. static const struct mac_font_mapping mac_codes[] = {
  19. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_ENGLISH, 10000},
  20. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_ICELANDIC, 10079},
  21. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_TURKISH, 10081},
  22. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_POLISH, 10029},
  23. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_ROMANIAN, 10010},
  24. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_CZECH, 10029},
  25. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_SLOVAK, 10029},
  26. {TT_MAC_ID_ROMAN, TT_MAC_LANGID_ANY, 10000},
  27. {TT_MAC_ID_JAPANESE, TT_MAC_LANGID_JAPANESE, 932},
  28. {TT_MAC_ID_JAPANESE, TT_MAC_LANGID_ANY, 932},
  29. {TT_MAC_ID_TRADITIONAL_CHINESE,TT_MAC_LANGID_CHINESE_SIMPLIFIED, 950},
  30. {TT_MAC_ID_TRADITIONAL_CHINESE,TT_MAC_LANGID_ANY, 950},
  31. {TT_MAC_ID_KOREAN, TT_MAC_LANGID_KOREAN, 51949},
  32. {TT_MAC_ID_KOREAN, TT_MAC_LANGID_ANY, 51949},
  33. {TT_MAC_ID_ARABIC, TT_MAC_LANGID_ARABIC, 10004},
  34. {TT_MAC_ID_ARABIC, TT_MAC_LANGID_URDU, 0},
  35. {TT_MAC_ID_ARABIC, TT_MAC_LANGID_FARSI, 0},
  36. {TT_MAC_ID_ARABIC, TT_MAC_LANGID_ANY, 10004},
  37. {TT_MAC_ID_HEBREW, TT_MAC_LANGID_HEBREW, 10005},
  38. {TT_MAC_ID_HEBREW, TT_MAC_LANGID_ANY, 10005},
  39. {TT_MAC_ID_GREEK, TT_MAC_LANGID_ANY, 10006},
  40. {TT_MAC_ID_RUSSIAN, TT_MAC_LANGID_ANY, 10007},
  41. {TT_MAC_ID_DEVANAGARI, TT_MAC_LANGID_ANY, 0},
  42. {TT_MAC_ID_GURMUKHI, TT_MAC_LANGID_ANY, 0},
  43. {TT_MAC_ID_GUJARATI, TT_MAC_LANGID_ANY, 0},
  44. {TT_MAC_ID_SIMPLIFIED_CHINESE, TT_MAC_LANGID_CHINESE_SIMPLIFIED, 936},
  45. {TT_MAC_ID_SIMPLIFIED_CHINESE, TT_MAC_LANGID_ANY, 936}
  46. };
  47. unsigned int iso_codes[] = {
  48. 20127,
  49. 0,
  50. 28591
  51. };
  52. unsigned int ms_codes[] = {
  53. 1201,
  54. 1201,
  55. 932,
  56. 0,
  57. 950,
  58. 0,
  59. 0,
  60. 0,
  61. 0,
  62. 0,
  63. 1201
  64. };
  65. static const size_t mac_code_count = sizeof(mac_codes) / sizeof(mac_codes[0]);
  66. static const size_t iso_code_count = sizeof(iso_codes) / sizeof(iso_codes[0]);
  67. static const size_t ms_code_count = sizeof(ms_codes) / sizeof(ms_codes[0]);
  68. static unsigned int get_mac_code(uint16_t encoding_id, uint16_t language_id)
  69. {
  70. for (size_t i = 0; i < mac_code_count; i++) {
  71. const struct mac_font_mapping *mac_code = &mac_codes[i];
  72. if (mac_code->encoding_id == encoding_id &&
  73. mac_code->language_id == language_id)
  74. return mac_code->code_page;
  75. }
  76. return 0;
  77. }
  78. static unsigned int get_code_page_for_font(uint16_t platform_id,
  79. uint16_t encoding_id, uint16_t language_id)
  80. {
  81. unsigned int ret;
  82. switch (platform_id) {
  83. case TT_PLATFORM_APPLE_UNICODE:
  84. return 1201;
  85. case TT_PLATFORM_MACINTOSH:
  86. ret = get_mac_code(encoding_id, language_id);
  87. if (!ret)
  88. ret = get_mac_code(encoding_id, TT_MAC_LANGID_ANY);
  89. return ret;
  90. case TT_PLATFORM_ISO:
  91. if (encoding_id < iso_code_count)
  92. return iso_codes[encoding_id];
  93. break;
  94. case TT_PLATFORM_MICROSOFT:
  95. if (encoding_id < ms_code_count)
  96. return ms_codes[encoding_id];
  97. break;
  98. }
  99. return 0;
  100. }
  101. static char *wide_to_utf8(const wchar_t *str, size_t len)
  102. {
  103. size_t utf8_len;
  104. char *utf8_str = NULL;
  105. utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0, str, (int)len,
  106. NULL, 0, NULL, false);
  107. if (utf8_len) {
  108. utf8_str = bzalloc(utf8_len + 1);
  109. utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0,
  110. str, (int)len,
  111. utf8_str, (int)utf8_len + 1, NULL, false);
  112. if (!utf8_len) {
  113. bfree(utf8_str);
  114. utf8_str = NULL;
  115. }
  116. }
  117. return utf8_str;
  118. }
  119. static char *convert_utf16_be_to_utf8(FT_SfntName *sfnt_name)
  120. {
  121. size_t utf16_len = sfnt_name->string_len / 2;
  122. wchar_t *utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
  123. char *utf8_str = NULL;
  124. utf16_str[utf16_len] = 0;
  125. /* convert to little endian */
  126. for (size_t i = 0; i < utf16_len; i++) {
  127. size_t pos = i * 2;
  128. wchar_t ch = *(wchar_t *)&sfnt_name->string[pos];
  129. utf16_str[i] = ((ch >> 8) & 0xFF) | ((ch << 8) & 0xFF00);
  130. }
  131. utf8_str = wide_to_utf8(utf16_str, utf16_len);
  132. free(utf16_str);
  133. return utf8_str;
  134. }
  135. char *sfnt_name_to_utf8(FT_SfntName *sfnt_name)
  136. {
  137. unsigned int code_page = get_code_page_for_font(
  138. sfnt_name->platform_id,
  139. sfnt_name->encoding_id,
  140. sfnt_name->language_id);
  141. char *utf8_str = NULL;
  142. wchar_t *utf16_str;
  143. size_t utf16_len;
  144. if (code_page == 1201)
  145. return convert_utf16_be_to_utf8(sfnt_name);
  146. else if (code_page == 0)
  147. return NULL;
  148. utf16_len = MultiByteToWideChar(code_page, 0,
  149. (char*)sfnt_name->string, sfnt_name->string_len,
  150. NULL, 0);
  151. if (utf16_len) {
  152. utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
  153. utf16_len = MultiByteToWideChar(code_page, 0,
  154. (char*)sfnt_name->string, sfnt_name->string_len,
  155. utf16_str, (int)utf16_len);
  156. if (utf16_len) {
  157. utf16_str[utf16_len] = 0;
  158. utf8_str = wide_to_utf8(utf16_str, utf16_len);
  159. }
  160. free(utf16_str);
  161. }
  162. return utf8_str;
  163. }
  164. uint32_t get_font_checksum(void)
  165. {
  166. uint32_t checksum = 0;
  167. struct dstr path = {0};
  168. HANDLE handle;
  169. WIN32_FIND_DATAA wfd;
  170. dstr_reserve(&path, MAX_PATH);
  171. HRESULT res = SHGetFolderPathA(NULL, CSIDL_FONTS, NULL,
  172. SHGFP_TYPE_CURRENT, path.array);
  173. if (res != S_OK) {
  174. blog(LOG_WARNING, "Error finding windows font folder");
  175. return 0;
  176. }
  177. path.len = strlen(path.array);
  178. dstr_cat(&path, "\\*.*");
  179. handle = FindFirstFileA(path.array, &wfd);
  180. if (handle == INVALID_HANDLE_VALUE)
  181. goto free_string;
  182. dstr_resize(&path, path.len - 4);
  183. do {
  184. checksum = calc_crc32(checksum, &wfd.ftLastWriteTime,
  185. sizeof(FILETIME));
  186. checksum = calc_crc32(checksum, wfd.cFileName,
  187. strlen(wfd.cFileName));
  188. } while (FindNextFileA(handle, &wfd));
  189. FindClose(handle);
  190. free_string:
  191. dstr_free(&path);
  192. return checksum;
  193. }
  194. void load_os_font_list(void)
  195. {
  196. struct dstr path = {0};
  197. HANDLE handle;
  198. WIN32_FIND_DATAA wfd;
  199. dstr_reserve(&path, MAX_PATH);
  200. HRESULT res = SHGetFolderPathA(NULL, CSIDL_FONTS, NULL,
  201. SHGFP_TYPE_CURRENT, path.array);
  202. if (res != S_OK) {
  203. blog(LOG_WARNING, "Error finding windows font folder");
  204. return;
  205. }
  206. path.len = strlen(path.array);
  207. dstr_cat(&path, "\\*.*");
  208. handle = FindFirstFileA(path.array, &wfd);
  209. if (handle == INVALID_HANDLE_VALUE)
  210. goto free_string;
  211. dstr_resize(&path, path.len - 4);
  212. do {
  213. struct dstr full_path = {0};
  214. FT_Face face;
  215. FT_Long idx = 0;
  216. FT_Long max_faces = 1;
  217. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  218. continue;
  219. dstr_copy_dstr(&full_path, &path);
  220. dstr_cat(&full_path, "\\");
  221. dstr_cat(&full_path, wfd.cFileName);
  222. while (idx < max_faces) {
  223. FT_Error ret = FT_New_Face(ft2_lib, full_path.array,
  224. idx, &face);
  225. if (ret != 0)
  226. break;
  227. build_font_path_info(face, idx++, full_path.array);
  228. max_faces = face->num_faces;
  229. FT_Done_Face(face);
  230. }
  231. dstr_free(&full_path);
  232. } while (FindNextFileA(handle, &wfd));
  233. FindClose(handle);
  234. save_font_list();
  235. free_string:
  236. dstr_free(&path);
  237. }