find-font-windows.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. unsigned int iso_codes[] = {20127, 0, 28591};
  47. unsigned int ms_codes[] = {1201, 1201, 932, 0, 950, 0, 0, 0, 0, 0, 1201};
  48. static const size_t mac_code_count = sizeof(mac_codes) / sizeof(mac_codes[0]);
  49. static const size_t iso_code_count = sizeof(iso_codes) / sizeof(iso_codes[0]);
  50. static const size_t ms_code_count = sizeof(ms_codes) / sizeof(ms_codes[0]);
  51. static unsigned int get_mac_code(uint16_t encoding_id, uint16_t language_id)
  52. {
  53. for (size_t i = 0; i < mac_code_count; i++) {
  54. const struct mac_font_mapping *mac_code = &mac_codes[i];
  55. if (mac_code->encoding_id == encoding_id && mac_code->language_id == language_id)
  56. return mac_code->code_page;
  57. }
  58. return 0;
  59. }
  60. static unsigned int get_code_page_for_font(uint16_t platform_id, uint16_t encoding_id, uint16_t language_id)
  61. {
  62. unsigned int ret;
  63. switch (platform_id) {
  64. case TT_PLATFORM_APPLE_UNICODE:
  65. return 1201;
  66. case TT_PLATFORM_MACINTOSH:
  67. ret = get_mac_code(encoding_id, language_id);
  68. if (!ret)
  69. ret = get_mac_code(encoding_id, TT_MAC_LANGID_ANY);
  70. return ret;
  71. case TT_PLATFORM_ISO:
  72. if (encoding_id < iso_code_count)
  73. return iso_codes[encoding_id];
  74. break;
  75. case TT_PLATFORM_MICROSOFT:
  76. if (encoding_id < ms_code_count)
  77. return ms_codes[encoding_id];
  78. break;
  79. }
  80. return 0;
  81. }
  82. static char *wide_to_utf8(const wchar_t *str, size_t len)
  83. {
  84. size_t utf8_len;
  85. char *utf8_str = NULL;
  86. utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0, str, (int)len, NULL, 0, NULL, false);
  87. if (utf8_len) {
  88. utf8_str = bzalloc(utf8_len + 1);
  89. utf8_len = (size_t)WideCharToMultiByte(CP_UTF8, 0, str, (int)len, utf8_str, (int)utf8_len + 1, NULL,
  90. false);
  91. if (!utf8_len) {
  92. bfree(utf8_str);
  93. utf8_str = NULL;
  94. }
  95. }
  96. return utf8_str;
  97. }
  98. static char *convert_utf16_be_to_utf8(FT_SfntName *sfnt_name)
  99. {
  100. size_t utf16_len = sfnt_name->string_len / 2;
  101. wchar_t *utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
  102. char *utf8_str = NULL;
  103. utf16_str[utf16_len] = 0;
  104. /* convert to little endian */
  105. for (size_t i = 0; i < utf16_len; i++) {
  106. size_t pos = i * 2;
  107. wchar_t ch = *(wchar_t *)&sfnt_name->string[pos];
  108. utf16_str[i] = ((ch >> 8) & 0xFF) | ((ch << 8) & 0xFF00);
  109. }
  110. utf8_str = wide_to_utf8(utf16_str, utf16_len);
  111. free(utf16_str);
  112. return utf8_str;
  113. }
  114. char *sfnt_name_to_utf8(FT_SfntName *sfnt_name)
  115. {
  116. unsigned int code_page =
  117. get_code_page_for_font(sfnt_name->platform_id, sfnt_name->encoding_id, sfnt_name->language_id);
  118. char *utf8_str = NULL;
  119. wchar_t *utf16_str;
  120. size_t utf16_len;
  121. if (code_page == 1201)
  122. return convert_utf16_be_to_utf8(sfnt_name);
  123. else if (code_page == 0)
  124. return NULL;
  125. utf16_len = MultiByteToWideChar(code_page, 0, (char *)sfnt_name->string, sfnt_name->string_len, NULL, 0);
  126. if (utf16_len) {
  127. utf16_str = malloc((utf16_len + 1) * sizeof(wchar_t));
  128. utf16_len = MultiByteToWideChar(code_page, 0, (char *)sfnt_name->string, sfnt_name->string_len,
  129. utf16_str, (int)utf16_len);
  130. if (utf16_len) {
  131. utf16_str[utf16_len] = 0;
  132. utf8_str = wide_to_utf8(utf16_str, utf16_len);
  133. }
  134. free(utf16_str);
  135. }
  136. return utf8_str;
  137. }
  138. uint32_t get_font_checksum(void)
  139. {
  140. uint32_t checksum = 0;
  141. struct dstr path = {0};
  142. HANDLE handle;
  143. WIN32_FIND_DATAA wfd;
  144. dstr_reserve(&path, MAX_PATH);
  145. HRESULT res = SHGetFolderPathA(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, path.array);
  146. if (res != S_OK) {
  147. blog(LOG_WARNING, "Error finding windows font folder");
  148. return 0;
  149. }
  150. path.len = strlen(path.array);
  151. dstr_cat(&path, "\\*.*");
  152. handle = FindFirstFileA(path.array, &wfd);
  153. if (handle == INVALID_HANDLE_VALUE)
  154. goto free_string;
  155. dstr_resize(&path, path.len - 4);
  156. do {
  157. checksum = calc_crc32(checksum, &wfd.ftLastWriteTime, sizeof(FILETIME));
  158. checksum = calc_crc32(checksum, wfd.cFileName, strlen(wfd.cFileName));
  159. } while (FindNextFileA(handle, &wfd));
  160. FindClose(handle);
  161. free_string:
  162. dstr_free(&path);
  163. return checksum;
  164. }
  165. void load_os_font_list(void)
  166. {
  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, SHGFP_TYPE_CURRENT, path.array);
  172. if (res != S_OK) {
  173. blog(LOG_WARNING, "Error finding windows font folder");
  174. return;
  175. }
  176. path.len = strlen(path.array);
  177. dstr_cat(&path, "\\*.*");
  178. handle = FindFirstFileA(path.array, &wfd);
  179. if (handle == INVALID_HANDLE_VALUE)
  180. goto free_string;
  181. dstr_resize(&path, path.len - 4);
  182. do {
  183. struct dstr full_path = {0};
  184. FT_Face face;
  185. FT_Long idx = 0;
  186. FT_Long max_faces = 1;
  187. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  188. continue;
  189. dstr_copy_dstr(&full_path, &path);
  190. dstr_cat(&full_path, "\\");
  191. dstr_cat(&full_path, wfd.cFileName);
  192. while (idx < max_faces) {
  193. FT_Error ret = FT_New_Face(ft2_lib, full_path.array, idx, &face);
  194. if (ret != 0)
  195. break;
  196. build_font_path_info(face, idx++, full_path.array);
  197. max_faces = face->num_faces;
  198. FT_Done_Face(face);
  199. }
  200. dstr_free(&full_path);
  201. } while (FindNextFileA(handle, &wfd));
  202. FindClose(handle);
  203. save_font_list();
  204. free_string:
  205. dstr_free(&path);
  206. }