脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法

查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法

2021-08-22 00:12mighty13 Python

这篇文章主要介绍了查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

问题来源

如何在matplotlib中使用中文字体是老问题了,相关文章非常多。
前几天有人问我如何知道中文字体名称和实际文件的对应关系时,才想起来原来没思考过这个问题,只能让他记住字体与文件的对应关系或者去fonts目录查看。
难道真的就没有稍微自动化、智能化的查看支持matplotlib的字体名称与文件的对应关系的方法?

问题解决步骤

matplotlib与字体相关的模块是font_manager,观察源码font_manager.py可知:

1. matplotlib支持哪种类型的字体?

  1. def get_fontext_synonyms(fontext):
  2. """
  3. Return a list of file extensions extensions that are synonyms for
  4. the given file extension *fileext*.
  5. """
  6. return {
  7. 'afm': ['afm'],
  8. 'otf': ['otf', 'ttc', 'ttf'],
  9. 'ttc': ['otf', 'ttc', 'ttf'],
  10. 'ttf': ['otf', 'ttc', 'ttf'],
  11. }[fontext]

由此可知matplotlib只支持ttfafm字体。

  • ‘ttf': TrueType and OpenType fonts (.ttf, .ttc, .otf)
  • ‘afm': Adobe Font Metrics (.afm)

2. matplotlib如何查找系统字体?

findSystemFonts模块函数的作用是查找系统字体。
def findSystemFonts(fontpaths=None, fontext='ttf'): -->list
参数fontext默认值为'ttf',另外还支持值'afm'
参数fontpaths默认值是None
对于fontpaths有两种种情况。

fontpathsNone

函数会判断操作系统,如果是Windows,调用函数win32InstalledFonts,如果不是Windows,调用函数get_fontconfig_fonts

对于Windows,函数win32InstalledFonts到以下路径查找。

  1. # OS Font paths
  2. MSFolders = \
  3. r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
  4. MSFontDirectories = [
  5. r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts',
  6. r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts']
  7. MSUserFontDirectories = [
  8. str(Path.home() / 'AppData/Local/Microsoft/Windows/Fonts'),
  9. str(Path.home() / 'AppData/Roaming/Microsoft/Windows/Fonts'),
  10. ]

对于非Windows操作系统,函数get_fontconfig_fonts主要依托fontconfig包(即fc-list命令)查找字体,要求fontconfig>=2.7

fontpaths不为None

fontpaths不为None时,通过list_fonts()函数查找字体。
list_fonts()函数其实一个通用的按路径、扩展名递归遍历文件路径的函数。

  1. def list_fonts(directory, extensions):
  2. """
  3. Return a list of all fonts matching any of the extensions, found
  4. recursively under the directory.
  5. """
  6. extensions = ["." + ext for ext in extensions]
  7. return [os.path.join(dirpath, filename)
  8. # os.walk ignores access errors, unlike Path.glob.
  9. for dirpath, _, filenames in os.walk(directory)
  10. for filename in filenames
  11. if Path(filename).suffix.lower() in extensions]

3.matplotlib如何查找matplotlib自带字体?

安装matplotlib时,会在site-packages\matplotlib\mpl-data\fonts目录放置一系列字体。

通过源码可知fonts目录下有'ttf', 'afm', 'pdfcorefonts'3个子目录。

  1. paths = [cbook._get_data_path('fonts', subdir)
  2. for subdir in ['ttf', 'afm', 'pdfcorefonts']]
  1. In [1]: import matplotlib.cbook as cbook
  2. In [2]: paths = [cbook._get_data_path('fonts', subdir) for subdir in ['ttf', 'afm', 'pdfcorefonts']]
  3. In [3]: paths
  4. Out[4]:
  5. [WindowsPath('c:/users/administrator/appdata/local/programs/python/python37/lib/site-packages/matplotlib/mpl-data/fonts/ttf'),
  6. WindowsPath('c:/users/administrator/appdata/local/programs/python/python37/lib/site-packages/matplotlib/mpl-data/fonts/afm'),
  7. WindowsPath('c:/users/administrator/appdata/local/programs/python/python37/lib/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts')]

4.matplotlib如何生成字体列表?

FontManager类是matplotlib管理字体的重要类,是一个单例类。FontManager实例在构造后会创建一个ttf字体列表和一个afm字体列表,并会缓存他们的字体属性。
下面简单看看ttflistafmlist列表的元素的属性。

  1. In [1]: import matplotlib.font_manager as mf
  2. In [2]: ttflist=mf.FontManager().ttflist
  3. In [3]: vars(ttflist[0])
  4. Out[3]:
  5. {'fname': 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSansMono-BoldOblique.ttf',
  6. 'name': 'DejaVu Sans Mono',
  7. 'style': 'oblique',
  8. 'variant': 'normal',
  9. 'weight': 700,
  10. 'stretch': 'normal',
  11. 'size': 'scalable'}
  12. In [4]: len(ttflist)
  13. Out[4]: 252
  14. In [5]: afmlist=mf.FontManager().afmlist
  15. In [6]: vars(afmlist[0])
  16. Out[6]:
  17. {'fname': 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\afm\\pagko8a.afm',
  18. 'name': 'ITC Avant Garde Gothic',
  19. 'style': 'italic',
  20. 'variant': 'normal',
  21. 'weight': 'book',
  22. 'stretch': 'normal',
  23. 'size': 'scalable'}
  24. In [7]: len(afmlist)
  25. Out[7]: 60

5.matplotlib的字体属性缓存在哪里?

前面了解到matplotlib会缓存字体属性那在什么位置呢?
根据源码可知,字体缓存的所在目录是.matplotlib,缓存文件是fontlist-v330.json(文件名与版本有关)。在某些老版本的matplotlib中字体缓存文件名是fontList.cache

  1. _fmcache = os.path.join(
  2. mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
  1. In [1]: import matplotlib
  2. In [2]: matplotlib.get_cachedir()
  3. Out[2]: 'C:\\Users\\Administrator\\.matplotlib'

6. 怎么知道哪些字体是中文字体?

虽然在前面已经知道matplotlib会把系统字体和自带字体信息存放在ttflistafmlist中,但是在这些字体信息中也不容易确定哪些是中文字体。
通过资料查找有3种方法:

查看Windows的fonts系统目录中显示的字体名称和文件名属性。这种方法效率太低!

通过注册表项[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts]查看字体名和字体文件名称映射。这种方法一些系统字体仍然看不到汉字名称。

查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法

根据第1种方法想到了去查字体文件的元数据,Python相关的库有两个:fonttools,作者是老爹Guido的弟弟Just van RossumTTFQuery,基于fonttools的TTF包,只能查看TTF文件而且最后更新日期是2012年。

TTFQery项目地址https://pypi.org/project/TTFQuery/
源码中有些小问题,源码不算复杂,直接修改。

  1. import sys
  2. from fontTools.ttLib import TTFont
  3. from fontTools.ttLib.ttCollection import TTCollection
  4.  
  5. UNICODE_ENCODINGS = {0: 'Unicode 1.0 semantics',
  6. 1: 'Unicode 1.1 semantics',
  7. 2: 'Unicode 1.1 semantics',
  8. 3: 'Unicode 2.0 and onwards semantics, Unicode BMP only (cmap subtable formats 0, 4, 6).',
  9. 4: 'Unicode 2.0 and onwards semantics, Unicode full repertoire (cmap subtable formats 0, 4, 6, 10, 12).',
  10. 5: 'Unicode Variation Sequences (cmap subtable format 14).',
  11. 6: 'Unicode Variation Sequences (cmap subtable format 14).'}
  12.  
  13. WINDOWS_ENCODINGS = {0: 'Symbol',
  14. 1: 'Unicode BMP(UCS-2)',
  15. 2: 'ShiftJIS',
  16. 3: 'PRC',
  17. 4: 'Big5',
  18. 5: 'Wansung',
  19. 6: 'Johab',
  20. 7: 'Reserved',
  21. 8: 'Reserved',
  22. 9: 'Reserved',
  23. 10: 'Unicode UCS-4'}
  24.  
  25. WINDOWS_LANGUAGES = {1025: 'Arabic/Saudi Arabia',
  26. 1026: 'Bulgarian/Bulgaria',
  27. 1027: 'Catalan/Catalan',
  28. 1028: 'Chinese/Taiwan',
  29. 1029: 'Czech/Czech Republic',
  30. 1030: 'Danish/Denmark',
  31. 1031: 'German/Germany',
  32. 1032: 'Greek/Greece',
  33. 1033: 'English/United States',
  34. 1034: 'Spanish (Traditional Sort)/Spain',
  35. 1035: 'Finnish/Finland',
  36. 1036: 'French/France',
  37. 1037: 'Hebrew/Israel',
  38. 1038: 'Hungarian/Hungary',
  39. 1039: 'Icelandic/Iceland',
  40. 1040: 'Italian/Italy',
  41. 1041: 'Japanese/Japan',
  42. 1042: 'Korean/Korea',
  43. 1043: 'Dutch/Netherlands',
  44. 1044: 'Norwegian (Bokmal)/Norway',
  45. 1045: 'Polish/Poland',
  46. 1046: 'Portuguese/Brazil',
  47. 1047: 'Romansh/Switzerland',
  48. 1048: 'Romanian/Romania',
  49. 1049: 'Russian/Russia',
  50. 1050: 'Croatian/Croatia',
  51. 1051: 'Slovak/Slovakia',
  52. 1052: 'Albanian/Albania',
  53. 1053: 'Swedish/Sweden',
  54. 1054: 'Thai/Thailand',
  55. 1055: 'Turkish/Turkey',
  56. 1056: 'Urdu/Islamic Republic of Pakistan',
  57. 1057: 'Indonesian/Indonesia',
  58. 1058: 'Ukrainian/Ukraine',
  59. 1059: 'Belarusian/Belarus',
  60. 1060: 'Slovenian/Slovenia',
  61. 1061: 'Estonian/Estonia',
  62. 1062: 'Latvian/Latvia',
  63. 1063: 'Lithuanian/Lithuania',
  64. 1064: 'Tajik (Cyrillic)/Tajikistan',
  65. 1066: 'Vietnamese/Vietnam',
  66. 1067: 'Armenian/Armenia',
  67. 1068: 'Azeri (Latin)/Azerbaijan',
  68. 1069: 'Basque/Basque',
  69. 1070: 'Upper Sorbian/Germany',
  70. 1071: 'Macedonian (FYROM)/Former Yugoslav Republic of Macedonia',
  71. 1074: 'Setswana/South Africa',
  72. 1076: 'isiXhosa/South Africa',
  73. 1077: 'isiZulu/South Africa',
  74. 1078: 'Afrikaans/South Africa',
  75. 1079: 'Georgian/Georgia',
  76. 1080: 'Faroese/Faroe Islands',
  77. 1081: 'Hindi/India',
  78. 1082: 'Maltese/Malta',
  79. 1083: 'Sami (Northern)/Norway',
  80. 1086: 'Malay/Malaysia',
  81. 1087: 'Kazakh/Kazakhstan',
  82. 1088: 'Kyrgyz/Kyrgyzstan',
  83. 1089: 'Kiswahili/Kenya',
  84. 1090: 'Turkmen/Turkmenistan',
  85. 1091: 'Uzbek (Latin)/Uzbekistan',
  86. 1092: 'Tatar/Russia',
  87. 1093: 'Bengali/India',
  88. 1094: 'Punjabi/India',
  89. 1095: 'Gujarati/India',
  90. 1096: 'Odia (formerly Oriya)/India',
  91. 1097: 'Tamil/India',
  92. 1098: 'Telugu/India',
  93. 1099: 'Kannada/India',
  94. 1100: 'Malayalam/India',
  95. 1101: 'Assamese/India',
  96. 1102: 'Marathi/India',
  97. 1103: 'Sanskrit/India',
  98. 1104: 'Mongolian (Cyrillic)/Mongolia',
  99. 1105: 'Tibetan/PRC',
  100. 1106: 'Welsh/United Kingdom',
  101. 1107: 'Khmer/Cambodia',
  102. 1108: 'Lao/Lao P.D.R.',
  103. 1110: 'Galician/Galician',
  104. 1111: 'Konkani/India',
  105. 1114: 'Syriac/Syria',
  106. 1115: 'Sinhala/Sri Lanka',
  107. 1117: 'Inuktitut/Canada',
  108. 1118: 'Amharic/Ethiopia',
  109. 1121: 'Nepali/Nepal',
  110. 1122: 'Frisian/Netherlands',
  111. 1123: 'Pashto/Afghanistan',
  112. 1124: 'Filipino/Philippines',
  113. 1125: 'Divehi/Maldives',
  114. 1128: 'Hausa (Latin)/Nigeria',
  115. 1130: 'Yoruba/Nigeria',
  116. 1131: 'Quechua/Bolivia',
  117. 1132: 'Sesotho sa Leboa/South Africa',
  118. 1133: 'Bashkir/Russia',
  119. 1134: 'Luxembourgish/Luxembourg',
  120. 1135: 'Greenlandic/Greenland',
  121. 1136: 'Igbo/Nigeria',
  122. 1144: 'Yi/PRC',
  123. 1146: 'Mapudungun/Chile',
  124. 1148: 'Mohawk/Mohawk',
  125. 1150: 'Breton/France',
  126. 1152: 'Uighur/PRC',
  127. 1153: 'Maori/New Zealand',
  128. 1154: 'Occitan/France',
  129. 1155: 'Corsican/France',
  130. 1156: 'Alsatian/France',
  131. 1157: 'Yakut/Russia',
  132. 1158: "K'iche/Guatemala",
  133. 1159: 'Kinyarwanda/Rwanda',
  134. 1160: 'Wolof/Senegal',
  135. 1164: 'Dari/Afghanistan',
  136. 2049: 'Arabic/Iraq',
  137. 2052: "Chinese/People's Republic of China",
  138. 2055: 'German/Switzerland',
  139. 2057: 'English/United Kingdom',
  140. 2058: 'Spanish/Mexico',
  141. 2060: 'French/Belgium',
  142. 2064: 'Italian/Switzerland',
  143. 2067: 'Dutch/Belgium',
  144. 2068: 'Norwegian (Nynorsk)/Norway',
  145. 2070: 'Portuguese/Portugal',
  146. 2074: 'Serbian (Latin)/Serbia',
  147. 2077: 'Sweden/Finland',
  148. 2092: 'Azeri (Cyrillic)/Azerbaijan',
  149. 2094: 'Lower Sorbian/Germany',
  150. 2107: 'Sami (Northern)/Sweden',
  151. 2108: 'Irish/Ireland',
  152. 2110: 'Malay/Brunei Darussalam',
  153. 2115: 'Uzbek (Cyrillic)/Uzbekistan',
  154. 2117: 'Bengali/Bangladesh',
  155. 2128: "Mongolian (Traditional)/People's Republic of China",
  156. 2141: 'Inuktitut (Latin)/Canada',
  157. 2143: 'Tamazight (Latin)/Algeria',
  158. 2155: 'Quechua/Ecuador',
  159. 3073: 'Arabic/Egypt',
  160. 3076: 'Chinese/Hong Kong S.A.R.',
  161. 3079: 'German/Austria',
  162. 3081: 'English/Australia',
  163. 3082: 'Spanish (Modern Sort)/Spain',
  164. 3084: 'French/Canada',
  165. 3098: 'Serbian (Cyrillic)/Serbia',
  166. 3131: 'Sami (Northern)/Finland',
  167. 3179: 'Quechua/Peru',
  168. 4097: 'Arabic/Libya',
  169. 4100: 'Chinese/Singapore',
  170. 4103: 'German/Luxembourg',
  171. 4105: 'English/Canada',
  172. 4106: 'Spanish/Guatemala',
  173. 4108: 'French/Switzerland',
  174. 4122: 'Croatian (Latin)/Bosnia and Herzegovina',
  175. 4155: 'Sami (Lule)/Norway',
  176. 5121: 'Arabic/Algeria',
  177. 5124: 'Chinese/Macao S.A.R.',
  178. 5127: 'German/Liechtenstein',
  179. 5129: 'English/New Zealand',
  180. 5130: 'Spanish/Costa Rica',
  181. 5132: 'French/Luxembourg',
  182. 5146: 'Bosnian (Latin)/Bosnia and Herzegovina',
  183. 5179: 'Sami (Lule)/Sweden',
  184. 6145: 'Arabic/Morocco',
  185. 6153: 'English/Ireland',
  186. 6154: 'Spanish/Panama',
  187. 6156: 'French/Principality of Monaco',
  188. 6170: 'Serbian (Latin)/Bosnia and Herzegovina',
  189. 6203: 'Sami (Southern)/Norway',
  190. 7169: 'Arabic/Tunisia',
  191. 7177: 'English/South Africa',
  192. 7178: 'Spanish/Dominican Republic',
  193. 7194: 'Serbian (Cyrillic)/Bosnia and Herzegovina',
  194. 7227: 'Sami (Southern)/Sweden',
  195. 8193: 'Arabic/Oman',
  196. 8201: 'English/Jamaica',
  197. 8202: 'Spanish/Venezuela',
  198. 8218: 'Bosnian (Cyrillic)/Bosnia and Herzegovina',
  199. 8251: 'Sami (Skolt)/Finland',
  200. 9217: 'Arabic/Yemen',
  201. 9225: 'English/Caribbean',
  202. 9226: 'Spanish/Colombia',
  203. 9275: 'Sami (Inari)/Finland',
  204. 10241: 'Arabic/Syria',
  205. 10249: 'English/Belize',
  206. 10250: 'Spanish/Peru',
  207. 11265: 'Arabic/Jordan',
  208. 11273: 'English/Trinidad and Tobago',
  209. 11274: 'Spanish/Argentina',
  210. 12289: 'Arabic/Lebanon',
  211. 12297: 'English/Zimbabwe',
  212. 12298: 'Spanish/Ecuador',
  213. 13313: 'Arabic/Kuwait',
  214. 13321: 'English/Republic of the Philippines',
  215. 13322: 'Spanish/Chile',
  216. 14337: 'Arabic/U.A.E.',
  217. 14346: 'Spanish/Uruguay',
  218. 15361: 'Arabic/Bahrain',
  219. 15370: 'Spanish/Paraguay',
  220. 16385: 'Arabic/Qatar',
  221. 16393: 'English/India',
  222. 16394: 'Spanish/Bolivia',
  223. 17417: 'English/Malaysia',
  224. 17418: 'Spanish/El Salvador',
  225. 18441: 'English/Singapore',
  226. 18442: 'Spanish/Honduras',
  227. 19466: 'Spanish/Nicaragua',
  228. 20490: 'Spanish/Puerto Rico',
  229. 21514: 'Spanish/United States'}
  230.  
  231. MACINTOSH_ENCODINGS = {0: 'Roman',
  232. 1: 'Japanese',
  233. 2: 'Chinese',
  234. 3: 'Korean',
  235. 4: 'Arabic',
  236. 5: 'Hebrew',
  237. 6: 'Greek',
  238. 7: 'Russian',
  239. 8: 'RSymbol',
  240. 9: 'Devanagari',
  241. 10: 'Gurmukhi',
  242. 11: 'Gujarati',
  243. 12: 'Oriya',
  244. 13: 'Bengali',
  245. 14: 'Tamil',
  246. 15: 'Telugu',
  247. 16: 'Kannada',
  248. 17: 'Malayalam',
  249. 18: 'Sinhalese',
  250. 19: 'Burmese',
  251. 20: 'Khmer',
  252. 21: 'Thai',
  253. 22: 'Laotian',
  254. 23: 'Georgian',
  255. 24: 'Armenian',
  256. 25: 'Chinese',
  257. 26: 'Tibetan',
  258. 27: 'Mongolian',
  259. 28: 'Geez',
  260. 29: 'Slavic',
  261. 30: 'Vietnamese',
  262. 31: 'Sindhi',
  263. 32: 'Uninterpreted'}
  264.  
  265. MACINTOSH_LANGUAGES = {0: 'English',
  266. 1: 'French',
  267. 2: 'German',
  268. 3: 'Italian',
  269. 4: 'Dutch',
  270. 5: 'Swedish',
  271. 6: 'Spanish',
  272. 7: 'Danish',
  273. 8: 'Portuguese',
  274. 9: 'Norwegian',
  275. 10: 'Hebrew',
  276. 11: 'Japanese',
  277. 12: 'Arabic',
  278. 13: 'Finnish',
  279. 14: 'Inuktitut',
  280. 15: 'Icelandic',
  281. 16: 'Maltese',
  282. 17: 'Turkish',
  283. 18: 'Croatian',
  284. 19: 'Chinese (Traditional)',
  285. 20: 'Urdu',
  286. 21: 'Hindi',
  287. 22: 'Thai',
  288. 23: 'Korean',
  289. 24: 'Lithuanian',
  290. 25: 'Polish',
  291. 26: 'Hungarian',
  292. 27: 'Estonian',
  293. 28: 'Latvian',
  294. 29: 'Sami',
  295. 30: 'Faroese',
  296. 31: 'Farsi/Persian',
  297. 32: 'Russian',
  298. 33: 'Chinese (Simplified)',
  299. 34: 'Flemish',
  300. 35: 'Irish Gaelic',
  301. 36: 'Albanian',
  302. 37: 'Romanian',
  303. 38: 'Czech',
  304. 39: 'Slovak',
  305. 40: 'Slovenian',
  306. 41: 'Yiddish',
  307. 42: 'Serbian',
  308. 43: 'Macedonian',
  309. 44: 'Bulgarian',
  310. 45: 'Ukrainian',
  311. 46: 'Byelorussian',
  312. 47: 'Uzbek',
  313. 48: 'Kazakh',
  314. 49: 'Azerbaijani (Cyrillic script)',
  315. 50: 'Azerbaijani (Arabic script)',
  316. 51: 'Armenian',
  317. 52: 'Georgian',
  318. 53: 'Moldavian',
  319. 54: 'Kirghiz',
  320. 55: 'Tajiki',
  321. 56: 'Turkmen',
  322. 57: 'Mongolian (Mongolian script)',
  323. 58: 'Mongolian (Cyrillic script)',
  324. 59: 'Pashto',
  325. 60: 'Kurdish',
  326. 61: 'Kashmiri',
  327. 62: 'Sindhi',
  328. 63: 'Tibetan',
  329. 64: 'Nepali',
  330. 65: 'Sanskrit',
  331. 66: 'Marathi',
  332. 67: 'Bengali',
  333. 68: 'Assamese',
  334. 69: 'Gujarati',
  335. 70: 'Punjabi',
  336. 71: 'Oriya',
  337. 72: 'Malayalam',
  338. 73: 'Kannada',
  339. 74: 'Tamil',
  340. 75: 'Telugu',
  341. 76: 'Sinhalese',
  342. 77: 'Burmese',
  343. 78: 'Khmer',
  344. 79: 'Lao',
  345. 80: 'Vietnamese',
  346. 81: 'Indonesian',
  347. 82: 'Tagalong',
  348. 83: 'Malay (Roman script)',
  349. 84: 'Malay (Arabic script)',
  350. 85: 'Amharic',
  351. 86: 'Tigrinya',
  352. 87: 'Galla',
  353. 88: 'Somali',
  354. 89: 'Swahili',
  355. 90: 'Kinyarwanda/Ruanda',
  356. 91: 'Rundi',
  357. 92: 'Nyanja/Chewa',
  358. 93: 'Malagasy',
  359. 94: 'Esperanto',
  360. 128: 'Welsh',
  361. 129: 'Basque',
  362. 130: 'Catalan',
  363. 131: 'Latin',
  364. 132: 'Quenchua',
  365. 133: 'Guarani',
  366. 134: 'Aymara',
  367. 135: 'Tatar',
  368. 136: 'Uighur',
  369. 137: 'Dzongkha',
  370. 138: 'Javanese (Roman script)',
  371. 139: 'Sundanese (Roman script)',
  372. 140: 'Galician',
  373. 141: 'Afrikaans',
  374. 142: 'Breton',
  375. 144: 'Scottish Gaelic',
  376. 145: 'Manx Gaelic',
  377. 146: 'Irish Gaelic (with dot above)',
  378. 147: 'Tongan',
  379. 148: 'Greek (polytonic)',
  380. 149: 'Greenlandic',
  381. 150: 'Azerbaijani (Roman script)'}
  382.  
  383. ISO_IDS = {
  384. 0: '7-bit ASCII',
  385. 1: 'ISO 10646',
  386. 2: 'ISO 8859-1'
  387. }
  388.  
  389. CUSTOMS = {}
  390.  
  391. PLATFORMS = {0: {'name': 'Unicode',
  392. 'encodings': UNICODE_ENCODINGS,
  393. 'languages': UNICODE_ENCODINGS},
  394. 1: {'name': 'Macintosh',
  395. 'encodings': MACINTOSH_ENCODINGS,
  396. 'languages': MACINTOSH_LANGUAGES},
  397. 2: {'name': 'ISO [deprecated]',
  398. 'encodings': ISO_IDS,
  399. 'languages': ISO_IDS},
  400. 3: {'name': 'Windows',
  401. 'encodings': WINDOWS_ENCODINGS,
  402. 'languages': WINDOWS_LANGUAGES},
  403. 4: {'name': 'Custom',
  404. 'encodings': CUSTOMS,
  405. 'languages': CUSTOMS}}
  406.  
  407. NAME_TABLE = {0: 'Copyright Notice',
  408. 1: 'Font Family',
  409. 2: 'SubFamily',
  410. 3: 'Unique Font Identifier',
  411. 4: 'Full Font Name',
  412. 5: 'Version',
  413. 6: 'PostScript Name',
  414. 7: 'Trademark',
  415. 8: 'Manufacturer Name',
  416. 9: 'Designer',
  417. 10: 'Description',
  418. 11: 'Vendor URL',
  419. 12: 'Designer URL',
  420. 13: 'License Description',
  421. 14: 'License Info URL',
  422. 15: 'Reserved',
  423. 16: 'Typographic Family',
  424. 17: 'Typographic SubFamily',
  425. 18: 'Compatible Full',
  426. 19: 'Sample Text',
  427. 20: 'PostScript CID findfont name',
  428. 21: 'WWS Family Name',
  429. 22: 'WWS SubFamily Name',
  430. 23: 'Light Background Pallete',
  431. 24: 'Dark Background Pallete',
  432. 25: 'Variations PostScript Name Prefix'}
  433.  
  434. ENCODINGS = {
  435. "Roman": 'latin_1'
  436. }
  437.  
  438. def parse_meta(font):
  439. """The main meta parsing function. Thanks to Fonttools library."""
  440. data = {}
  441. for nti in font['name'].names:
  442. key = nti.nameID
  443. platform_data = PLATFORMS[nti.platformID]
  444. if platform_data['name'] == 'Custom':
  445. encoding = {'id': 0, 'value': 'Custom'}
  446. language = {'id': 0, 'value': 'Custom'}
  447. else:
  448. encoding = {'id': nti.platEncID,
  449. 'value': platform_data['encodings'].get(nti.platEncID, "Unknown")}
  450. language = {'id': nti.langID,
  451. 'value': platform_data['languages'].get(nti.langID, "Unknown")}
  452. name_str = nti.toStr()
  453. field = NAME_TABLE.get(nti.nameID, False)
  454. if not field:
  455. if 26 <= nti.nameID <= 255:
  456. field = 'Reserved [{}]'.format(nti.nameID)
  457. elif 256 <= nti.nameID:
  458. field = 'Font Specific[{}]'.format(nti.nameID)
  459.  
  460. data[key] = {"field": field,
  461. "value": name_str,
  462. "encoding": encoding,
  463. "language": language
  464. }
  465. return data
  466.  
  467. def get_font_name(data, language=None):
  468. language_name = data.get(4).get('language').get('value')
  469. font_full_name = data.get(4).get('value')
  470.  
  471. if language is None:
  472. return font_full_name
  473. else:
  474. if language in language_name:
  475. return font_full_name
  476.  
  477. if __name__ == '__main__':
  478. from pathlib import Path
  479. import matplotlib.font_manager as fm
  480.  
  481. ttflist=fm.FontManager().ttflist
  482.  
  483. for f in ttflist:
  484. if Path(f.fname).suffix.lower()=='.ttf':
  485. # 输出字体元数据中语言包含有中文的TTF字体
  486. data = parse_meta(TTFont(f.fname))
  487. out_ttf = get_font_name(data,'Chinese')
  488. if out_ttf:
  489. print(f.fname,f.name,out_ttf)
  490. elif Path(f.fname).suffix.lower()=='.ttc':
  491. # 输出所有TTC字体中包含的字体名称信息
  492. ttc = TTCollection(f.fname)
  493. for ttf in ttc:
  494. ttc_data = parse_meta(ttf)
  495. out_ttc = get_font_name(ttc_data,'Chinese')
  496. if out_ttc:
  497. print(f.fname,f.name,out_ttc)

ttc文件很特殊,它是多个ttf文件集合,常用字体宋体微软雅黑等在Windows10中都是ttc文件,而且奇怪的是很多语言信息不是中文的ttc字体也可以显示中文。

  1. C:\Windows\Fonts\STHUPO.TTF STHupo 华文琥珀
  2. C:\Windows\Fonts\mingliub.ttc MingLiU-ExtB 細明體-ExtB
  3. C:\Windows\Fonts\mingliub.ttc MingLiU-ExtB 新細明體-ExtB
  4. C:\Windows\Fonts\mingliub.ttc MingLiU-ExtB 細明體_HKSCS-ExtB
  5. C:\Windows\Fonts\simkai.ttf KaiTi 楷体
  6. C:\Windows\Fonts\STXIHEI.TTF STXihei 华文细黑
  7. C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\仿_GB2312.ttf FangSong_GB2312 仿宋_GB2312
  8. C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\楷_GB2312.ttf KaiTi_GB2312 楷体_GB2312
  9. C:\Windows\Fonts\STXINGKA.TTF STXingkai 华文行楷
  10. C:\Windows\Fonts\STCAIYUN.TTF STCaiyun 华文彩云
  11. C:\Windows\Fonts\simsun.ttc SimSun 宋体
  12. C:\Windows\Fonts\simsun.ttc SimSun 新宋体
  13. C:\Windows\Fonts\STKAITI.TTF STKaiti 华文楷体
  14. C:\Windows\Fonts\msjhl.ttc Microsoft JhengHei 微軟正黑體 Light
  15. C:\Windows\Fonts\msjhl.ttc Microsoft JhengHei Microsoft JhengHei UI Light
  16. C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\华文中宋.ttf STZhongsong 华文中宋
  17. C:\Windows\Fonts\FZYTK.TTF FZYaoTi 方正姚体
  18. C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\方正卡通简体.ttf FZKaTong-M19S 方正卡通简体
  19. C:\Windows\Fonts\simhei.ttf SimHei 黑体
  20. C:\Windows\Fonts\方正粗黑宋简体.ttf FZCuHeiSongS-B-GB 方正粗黑宋简体
  21. C:\Windows\Fonts\Deng.ttf DengXian 等线
  22. C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\FZXBSJW.TTF FZXiaoBiaoSong-B05S 方正小标宋简体
  23. C:\Windows\Fonts\Dengl.ttf DengXian 等线 Light
  24. C:\Windows\Fonts\msjh.ttc Microsoft JhengHei 微軟正黑體
  25. C:\Windows\Fonts\msjh.ttc Microsoft JhengHei Microsoft JhengHei UI
  26. C:\Windows\Fonts\msyh.ttc Microsoft YaHei 微软雅黑
  27. C:\Windows\Fonts\msyh.ttc Microsoft YaHei Microsoft Yahei UI
  28. C:\Windows\Fonts\STFANGSO.TTF STFangsong 华文仿宋
  29. C:\Windows\Fonts\simfang.ttf FangSong 仿宋
  30. C:\Windows\Fonts\SIMLI.TTF LiSu 隶书
  31. C:\Windows\Fonts\SIMYOU.TTF YouYuan 幼圆
  32. C:\Windows\Fonts\STLITI.TTF STLiti 华文隶书
  33. C:\Windows\Fonts\Dengb.ttf DengXian 等线 Bold
  34. C:\Windows\Fonts\msyhl.ttc Microsoft YaHei 微软雅黑 Light
  35. C:\Windows\Fonts\msyhl.ttc Microsoft YaHei Microsoft YaHei UI Light
  36. C:\Windows\Fonts\STSONG.TTF STSong 华文宋体
  37. C:\Windows\Fonts\STXINWEI.TTF STXinwei 华文新魏
  38. C:\Windows\Fonts\FZSTK.TTF FZShuTi 方正舒体

参考

https://matplotlib.org/api/font_manager_api.html
http://ttfquery.sourceforge.net/
https://fonttools.readthedocs.io/en/latest/ttLib/index.html

到此这篇关于查找适用于matplotlib的中文字体名称与实际文件名对应关系的方法的文章就介绍到这了,更多相关matplotlib中文字体名称与实际文件名对应内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mighty13/article/details/111824296

延伸 · 阅读

精彩推荐