find-font-windows.c 6.5 KB

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