| 1 |
## pygame - Python Game Library |
| 2 |
## Copyright (C) 2000-2003 Pete Shinners |
| 3 |
## |
| 4 |
## This library is free software; you can redistribute it and/or |
| 5 |
## modify it under the terms of the GNU Library General Public |
| 6 |
## License as published by the Free Software Foundation; either |
| 7 |
## version 2 of the License, or (at your option) any later version. |
| 8 |
## |
| 9 |
## This library is distributed in the hope that it will be useful, |
| 10 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
## Library General Public License for more details. |
| 13 |
## |
| 14 |
## You should have received a copy of the GNU Library General Public |
| 15 |
## License along with this library; if not, write to the Free |
| 16 |
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
## |
| 18 |
## Pete Shinners |
| 19 |
## [email protected] |
| 20 |
import glob |
| 21 |
|
| 22 |
"sysfont, used in the font module to find system fonts" |
| 23 |
|
| 24 |
import os, sys |
| 25 |
|
| 26 |
#Python 3 compatibility |
| 27 |
try: |
| 28 |
bytes |
| 29 |
except NameError: |
| 30 |
def toascii(raw): |
| 31 |
return raw.decode('ascii', 'ignore').encode('ascii') |
| 32 |
else: |
| 33 |
def toascii(raw): |
| 34 |
return raw.decode('ascii', 'ignore') |
| 35 |
|
| 36 |
#create simple version of the font name |
| 37 |
def _simplename(name): |
| 38 |
return ''.join([c.lower() for c in name if c.isalnum()]) |
| 39 |
|
| 40 |
|
| 41 |
#insert a font and style into the font dictionary |
| 42 |
def _addfont(name, bold, italic, font, fontdict): |
| 43 |
if name not in fontdict: |
| 44 |
fontdict[name] = {} |
| 45 |
fontdict[name][bold, italic] = font |
| 46 |
|
| 47 |
|
| 48 |
#read the fonts on windows |
| 49 |
# Info taken from: |
| 50 |
# http://www.microsoft.com/typography/fonts/winxp.htm |
| 51 |
# with extra files added from: |
| 52 |
# http://www.ampsoft.net/webdesign-l/windows-fonts-by-version.html |
| 53 |
# File name, family, (Bold, Italic) |
| 54 |
_XP_default_font_files = [ |
| 55 |
('ahronbd.ttf', 'Aharoni', True, False), |
| 56 |
('andlso.ttf', 'Andalus', False, False), |
| 57 |
('angsa.ttf', 'Angsana New', False, False), |
| 58 |
('angsab.ttf', 'Angsana New', True, False), |
| 59 |
('angsai.ttf', 'Angsana New', False, True), |
| 60 |
('angsaz.ttf', 'Angsana New', True, True), |
| 61 |
('angsau.ttf', 'AngsanaUPC', False, False), |
| 62 |
('angsaub.ttf', 'AngsanaUPC', True, False), |
| 63 |
('angsaui.ttf', 'AngsanaUPC', False, True), |
| 64 |
('angsauz.ttf', 'AngsanaUPC', True, True), |
| 65 |
('artro.ttf', 'Arabic Transparent', False, False), |
| 66 |
('artrbdo.ttf', 'Arabic Transparent', True, False), |
| 67 |
('agatha.ttf', 'Agatha', False, False), |
| 68 |
('arial.ttf', 'Arial', False, False), |
| 69 |
('arialbd.ttf', 'Arial', True, False), |
| 70 |
('ariali.ttf', 'Arial', False, True), |
| 71 |
('arialbi.ttf', 'Arial', True, True), |
| 72 |
('ariblk.ttf', 'Arial Black', False, False), |
| 73 |
('browa.ttf', 'Browallia New', False, False), |
| 74 |
('browab.ttf', 'Browallia New', True, False), |
| 75 |
('browai.ttf', 'Browallia New', False, True), |
| 76 |
('browaz.ttf', 'Browallia New', True, True), |
| 77 |
('browau.ttf', 'BrowalliaUPC', False, False), |
| 78 |
('browaub.ttf', 'BrowalliaUPC', True, False), |
| 79 |
('browaui.ttf', 'BrowalliaUPC', False, True), |
| 80 |
('browauz.ttf', 'BrowalliaUPC', True, True), |
| 81 |
('comic.ttf', 'Comic Sans MS', False, False), |
| 82 |
('comicbd.ttf', 'Comic Sans MS', True, False), |
| 83 |
('cordia.ttf', 'Cordia New', False, False), |
| 84 |
('cordiab.ttf', 'Cordia New', True, False), |
| 85 |
('cordiai.ttf', 'Cordia New', False, True), |
| 86 |
('cordiaz.ttf', 'Cordia New', True, True), |
| 87 |
('cordiau.ttf', 'CordiaUPC', False, False), |
| 88 |
('cordiaub.ttf', 'CordiaUPC', True, False), |
| 89 |
('cordiaui.ttf', 'CordiaUPC', False, True), |
| 90 |
('cordiauz.ttf', 'CordiaUPC', True, True), |
| 91 |
('cour.ttf', 'Courier New', False, False), |
| 92 |
('courbd.ttf', 'Courier New', True, False), |
| 93 |
('couri.ttf', 'Courier New', False, True), |
| 94 |
('courbi.ttf', 'Courier New', True, True), |
| 95 |
('david.ttf', 'David', False, False), |
| 96 |
('davidbd.ttf', 'David', True, False), |
| 97 |
('davidtr.ttf', 'David Transparent', False, False), |
| 98 |
('upcdl.ttf', 'DilleniaUPC', False, False), |
| 99 |
('upcdb.ttf', 'DilleniaUPC', True, False), |
| 100 |
('upcdi.ttf', 'DilleniaUPC', False, True), |
| 101 |
('upcdbi.ttf', 'DilleniaUPC', True, True), |
| 102 |
('estre.ttf', 'Estrangelo Edessa', False, False), |
| 103 |
('upcel.ttf', 'EucrosialUPC', False, False), |
| 104 |
('upceb.ttf', 'EucrosialUPC', True, False), |
| 105 |
('upcei.ttf', 'EucrosialUPC', False, True), |
| 106 |
('upcebi.ttf', 'EucrosialUPC', True, True), |
| 107 |
('mriamfx.ttf', 'Fixed Miriam Transparent', False, False), |
| 108 |
('framd.ttf', 'Franklin Gothic Medium', False, False), |
| 109 |
('framdit.ttf', 'Franklin Gothic Medium', False, True), |
| 110 |
('frank.ttf', 'FrankRuehl', False, False), |
| 111 |
('upcfl.ttf', 'FreesialUPC', False, False), |
| 112 |
('upcfb.ttf', 'FreesialUPC', True, False), |
| 113 |
('upcfi.ttf', 'FreesialUPC', False, True), |
| 114 |
('upcfbi.ttf', 'FreesialUPC', True, True), |
| 115 |
('gautami.ttf', 'Gautami', False, False), |
| 116 |
('georgia.ttf', 'Georgia', False, False), |
| 117 |
('georgiab.ttf', 'Georgia', True, False), |
| 118 |
('georgiai.ttf', 'Georgia', False, True), |
| 119 |
('georgiaz.ttf', 'Georgia', True, True), |
| 120 |
('impact.ttf', 'Impact', False, False), |
| 121 |
('upcil.ttf', 'IrisUPC', False, False), |
| 122 |
('upcib.ttf', 'IrisUPC', True, False), |
| 123 |
('upcii.ttf', 'IrisUPC', False, True), |
| 124 |
('upcibi.ttf', 'IrisUPC', True, True), |
| 125 |
('upcjl.ttf', 'JasmineUPC', False, False), |
| 126 |
('upcjb.ttf', 'JasmineUPC', True, False), |
| 127 |
('upcji.ttf', 'JasmineUPC', False, True), |
| 128 |
('upcjbi.ttf', 'JasmineUPC', True, True), |
| 129 |
('upckl.ttf', 'KodchiangUPC', False, False), |
| 130 |
('upckb.ttf', 'KodchiangUPC', True, False), |
| 131 |
('upcki.ttf', 'KodchiangUPC', False, True), |
| 132 |
('upckbi.ttf', 'KodchiangUPC', True, True), |
| 133 |
('latha.ttf', 'Latha', False, False), |
| 134 |
('lvnm.ttf', 'Levenim MT', False, False), |
| 135 |
('lvnmbd.ttf', 'Levenim MT', True, False), |
| 136 |
('upcll.ttf', 'LilyUPC', False, False), |
| 137 |
('upclb.ttf', 'LilyUPC', True, False), |
| 138 |
('upcli.ttf', 'LilyUPC', False, True), |
| 139 |
('upclbi.ttf', 'LilyUPC', True, True), |
| 140 |
('lucon.ttf', 'Lucida Console', False, False), |
| 141 |
('l_10646.ttf', 'Lucida Sans Unicode', False, False), |
| 142 |
('mangal.ttf', 'Mangal', False, False), |
| 143 |
('marlett.ttf', 'Marlett', False, False), |
| 144 |
('micross.ttf', 'Microsoft Sans Serif', False, False), |
| 145 |
('mriam.ttf', 'Miriam', False, False), |
| 146 |
('mriamc.ttf', 'Miriam Fixed', False, False), |
| 147 |
('mriamtr.ttf', 'Miriam Transparent', False, False), |
| 148 |
('mvboli.ttf', 'MV Boli', False, False), |
| 149 |
('nrkis.ttf', 'Narkisim', False, False), |
| 150 |
('pala.ttf', 'Falatino Linotype', False, False), |
| 151 |
('palab.ttf', 'Falatino Linotype', True, False), |
| 152 |
('palai.ttf', 'Falatino Linotype', False, True), |
| 153 |
('palabi.ttf', 'Falatino Linotype', True, True), |
| 154 |
('raavi.ttf', 'Raavi', False, False), |
| 155 |
('rod.ttf', 'Rod', False, False), |
| 156 |
('rodtr.ttf', 'Rod Transparent', False, False), |
| 157 |
('shruti.ttf', 'Shruti', False, False), |
| 158 |
('simpo.ttf', 'Simplified Arabic', False, False), |
| 159 |
('simpbdo.ttf', 'Simplified Arabic', True, False), |
| 160 |
('simpfxo.ttf', 'Simplified Arabic Fixed', False, False), |
| 161 |
('sylfaen.ttf', 'Sylfaen', False, False), |
| 162 |
('symbol.ttf', 'Symbol', False, False), |
| 163 |
('tahoma.ttf', 'Tahoma', False, False), |
| 164 |
('tahomabd.ttf', 'Tahoma', True, False), |
| 165 |
('times.ttf', 'Times New Roman', False, False), |
| 166 |
('timesbd.ttf', 'Times New Roman', True, False), |
| 167 |
('timesi.ttf', 'Times New Roman', False, True), |
| 168 |
('timesbi.ttf', 'Times New Roman', True, True), |
| 169 |
('trado.ttf', 'Traditional Arabic', False, False), |
| 170 |
('tradbdo.ttf', 'Traditional Arabic', True, False), |
| 171 |
('Trebuc.ttf', 'Trebuchet MS', False, False), |
| 172 |
('Trebucbd.ttf', 'Trebuchet MS', True, False), |
| 173 |
('Trebucit.ttf', 'Trebuchet MS', False, True), |
| 174 |
('Trebucbi.ttf', 'Trebuchet MS', True, True), |
| 175 |
('tunga.ttf', 'Tunga', False, False), |
| 176 |
('verdana.ttf', 'Verdana', False, False), |
| 177 |
('verdanab.ttf', 'Verdana', True, False), |
| 178 |
('verdanai.ttf', 'Verdana', False, True), |
| 179 |
('verdanaz.ttf', 'Verdana', True, True), |
| 180 |
('webdings.ttf', 'Webdings', False, False), |
| 181 |
('wingding.ttf', 'Wingdings', False, False), |
| 182 |
('simhei.ttf', 'SimHei', False, False), |
| 183 |
('simfang.ttf', 'FangSong_GB2312', False, False), |
| 184 |
('kaiu.ttf', 'DFKai-SB', False, False), |
| 185 |
('simkai.ttf', 'KaiTi_GB2312', False, False), |
| 186 |
('msgothic.ttc', 'MS Gothic', False, False), |
| 187 |
('msmincho.ttc', 'MS Mincho', False, False), |
| 188 |
('gulim.ttc', 'Gulim', False, False), |
| 189 |
('mingliu.ttc', 'Mingliu', False, False), |
| 190 |
('simsun.ttc', 'Simsun', False, False), |
| 191 |
('batang.ttc', 'Batang', False, False), |
| 192 |
] |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
def initsysfonts_win32(): |
| 198 |
try: |
| 199 |
import _winreg |
| 200 |
except ImportError: |
| 201 |
import winreg as _winreg |
| 202 |
|
| 203 |
if 'WINDIR' in os.environ: |
| 204 |
windir = os.environ['WINDIR'] |
| 205 |
elif 'windir' in os.environ: |
| 206 |
windir = os.environ['windir'] |
| 207 |
else: |
| 208 |
windir = "C:\\Windows\\" |
| 209 |
|
| 210 |
|
| 211 |
fonts = {} |
| 212 |
mods = 'demibold', 'narrow', 'light', 'unicode', 'bt', 'mt' |
| 213 |
fontdir = os.path.join(windir, "Fonts") |
| 214 |
|
| 215 |
#this is a list of registry keys containing information |
| 216 |
#about fonts installed on the system. |
| 217 |
keys = [] |
| 218 |
|
| 219 |
#add recognized fonts from the fonts directory because the default |
| 220 |
#fonts may not be entered in the registry. |
| 221 |
win_font_files_mapping = dict( |
| 222 |
[(file_name.lower(), (_simplename(name), bold, italic)) |
| 223 |
for file_name, name, bold, italic in _XP_default_font_files]) |
| 224 |
|
| 225 |
font_dir_path = os.path.join(windir, 'fonts') |
| 226 |
try: |
| 227 |
font_file_paths = glob.glob(os.path.join(font_dir_path, '*.tt?')) |
| 228 |
except Exception: |
| 229 |
pass |
| 230 |
else: |
| 231 |
for font in font_file_paths: |
| 232 |
file_name = os.path.basename(font) |
| 233 |
try: |
| 234 |
name, bold, italic = win_font_files_mapping[file_name] |
| 235 |
except KeyError: |
| 236 |
pass |
| 237 |
else: |
| 238 |
_addfont(name, bold, italic, font, fonts) |
| 239 |
|
| 240 |
#add additional fonts entered in the registry. |
| 241 |
|
| 242 |
#find valid registry keys containing font information. |
| 243 |
possible_keys = [ |
| 244 |
r"SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts", |
| 245 |
r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", |
| 246 |
r"SOFTWARE\Microsoft\Windows[NT]\CurrentVersion\Fonts", |
| 247 |
] |
| 248 |
|
| 249 |
for key_name in possible_keys: |
| 250 |
try: |
| 251 |
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key_name) |
| 252 |
keys.append(key) |
| 253 |
except WindowsError: # @UndefinedVariable |
| 254 |
pass |
| 255 |
|
| 256 |
for key in keys: |
| 257 |
fontdict = {} |
| 258 |
for i in range(_winreg.QueryInfoKey(key)[1]): |
| 259 |
try: name, font, t = _winreg.EnumValue(key,i) |
| 260 |
except EnvironmentError: break |
| 261 |
|
| 262 |
# try and handle windows unicode strings for some file names. |
| 263 |
|
| 264 |
# here are two documents with some information about it: |
| 265 |
# http://www.python.org/peps/pep-0277.html |
| 266 |
# https://www.microsoft.com/technet/archive/interopmigration/linux/mvc/lintowin.mspx#ECAA |
| 267 |
try: |
| 268 |
font = str(font) |
| 269 |
except UnicodeEncodeError: |
| 270 |
# MBCS is the windows encoding for unicode file names. |
| 271 |
try: |
| 272 |
font = font.encode('MBCS') |
| 273 |
except: |
| 274 |
# no goodness with str or MBCS encoding... skip this font. |
| 275 |
continue |
| 276 |
|
| 277 |
if font[-4:].lower() not in [".ttf", ".ttc", ".otf"]: |
| 278 |
continue |
| 279 |
if os.sep not in font: |
| 280 |
font = os.path.join(fontdir, font) |
| 281 |
|
| 282 |
if name[-10:] == '(TrueType)': |
| 283 |
name = name[:-11] |
| 284 |
name = name.lower().split() |
| 285 |
|
| 286 |
bold = italic = 0 |
| 287 |
for m in mods: |
| 288 |
if m in name: |
| 289 |
name.remove(m) |
| 290 |
if 'bold' in name: |
| 291 |
name.remove('bold') |
| 292 |
bold = 1 |
| 293 |
if 'italic' in name: |
| 294 |
name.remove('italic') |
| 295 |
italic = 1 |
| 296 |
name = ''.join(name) |
| 297 |
|
| 298 |
name=_simplename(name) |
| 299 |
|
| 300 |
_addfont(name, bold, italic, font, fonts) |
| 301 |
|
| 302 |
return fonts |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
_OSX_default_font_files = { |
| 309 |
'albayan': {(False, False): '/Library/Fonts/AlBayan.ttf', |
| 310 |
(True, False): '/Library/Fonts/AlBayanBold.ttf'}, |
| 311 |
'andalemono': {(False, False): '/Library/Fonts/Andale Mono.ttf'}, |
| 312 |
'applebraille': {(False, False): '/System/Library/Fonts/Apple Braille Outline 6 Dot.ttf'}, |
| 313 |
'applegothic': {(False, False): '/System/Library/Fonts/AppleGothic.ttf'}, |
| 314 |
'applesymbols': {(False, False): '/System/Library/Fonts/Apple Symbols.ttf'}, |
| 315 |
'arial': {(False, False): '/Library/Fonts/Arial.ttf', |
| 316 |
(False, True): '/Library/Fonts/Arial Italic.ttf', |
| 317 |
(True, False): '/Library/Fonts/Arial Bold.ttf', |
| 318 |
(True, True): '/Library/Fonts/Arial Bold Italic.ttf'}, |
| 319 |
'arialblack': {(False, False): '/Library/Fonts/Arial Black.ttf'}, |
| 320 |
'arialhebrew': {(False, False): '/Library/Fonts/ArialHB.ttf', |
| 321 |
(True, False): '/Library/Fonts/ArialHBBold.ttf'}, |
| 322 |
'arialnarrow': {(False, False): '/Library/Fonts/Arial Narrow.ttf', |
| 323 |
(False, True): '/Library/Fonts/Arial Narrow Italic.ttf', |
| 324 |
(True, False): '/Library/Fonts/Arial Narrow Bold.ttf', |
| 325 |
(True, True): '/Library/Fonts/Arial Narrow Bold Italic.ttf'}, |
| 326 |
'arialroundedmtbold': {(False, False): '/Library/Fonts/Arial Rounded Bold.ttf'}, |
| 327 |
'arialunicodems': {(False, False): '/Library/Fonts/Arial Unicode.ttf'}, |
| 328 |
'ayuthaya': {(False, False): '/Library/Fonts/Ayuthaya.ttf'}, |
| 329 |
'baghdad': {(False, False): '/Library/Fonts/Baghdad.ttf'}, |
| 330 |
'brushscriptmt': {(False, True): '/Library/Fonts/Brush Script.ttf'}, |
| 331 |
'chalkboard': {(False, False): '/Library/Fonts/Chalkboard.ttf', |
| 332 |
(True, False): '/Library/Fonts/ChalkboardBold.ttf'}, |
| 333 |
'comicsansms': {(False, False): '/Library/Fonts/Comic Sans MS.ttf', |
| 334 |
(True, False): '/Library/Fonts/Comic Sans MS Bold.ttf'}, |
| 335 |
'corsivahebrew': {(False, False): '/Library/Fonts/Corsiva.ttf', |
| 336 |
(True, False): '/Library/Fonts/CorsivaBold.ttf'}, |
| 337 |
'couriernew': {(False, False): '/Library/Fonts/Courier New.ttf', |
| 338 |
(False, True): '/Library/Fonts/Courier New Italic.ttf', |
| 339 |
(True, False): '/Library/Fonts/Courier New Bold.ttf', |
| 340 |
(True, True): '/Library/Fonts/Courier New Bold Italic.ttf'}, |
| 341 |
'decotypenaskh': {(False, False): '/Library/Fonts/DecoTypeNaskh.ttf'}, |
| 342 |
'devanagarimt': {(False, False): '/Library/Fonts/DevanagariMT.ttf', |
| 343 |
(True, False): '/Library/Fonts/DevanagariMTBold.ttf'}, |
| 344 |
'euphemiaucas': {(False, False): '/Library/Fonts/EuphemiaCASRegular.ttf', |
| 345 |
(False, True): '/Library/Fonts/EuphemiaCASItalic.ttf', |
| 346 |
(True, False): '/Library/Fonts/EuphemiaCASBold.ttf'}, |
| 347 |
'gb18030bitmap': {(False, False): '/Library/Fonts/NISC18030.ttf'}, |
| 348 |
'geezapro': {(False, False): '/System/Library/Fonts/Geeza Pro.ttf', |
| 349 |
(True, False): '/System/Library/Fonts/Geeza Pro Bold.ttf'}, |
| 350 |
'georgia': {(False, False): '/Library/Fonts/Georgia.ttf', |
| 351 |
(False, True): '/Library/Fonts/Georgia Italic.ttf', |
| 352 |
(True, False): '/Library/Fonts/Georgia Bold.ttf', |
| 353 |
(True, True): '/Library/Fonts/Georgia Bold Italic.ttf'}, |
| 354 |
'gujaratimt': {(False, False): '/Library/Fonts/GujaratiMT.ttf', |
| 355 |
(True, False): '/Library/Fonts/GujaratiMTBold.ttf'}, |
| 356 |
'gurmukhimt': {(False, False): '/Library/Fonts/Gurmukhi.ttf'}, |
| 357 |
'impact': {(False, False): '/Library/Fonts/Impact.ttf'}, |
| 358 |
'inaimathi': {(False, False): '/Library/Fonts/InaiMathi.ttf'}, |
| 359 |
'kailasa': {(False, False): '/Library/Fonts/Kailasa.ttf'}, |
| 360 |
'kokonor': {(False, False): '/Library/Fonts/Kokonor.ttf'}, |
| 361 |
'krungthep': {(False, False): '/Library/Fonts/Krungthep.ttf'}, |
| 362 |
'kufistandardgk': {(False, False): '/Library/Fonts/KufiStandardGK.ttf'}, |
| 363 |
'liheipro': {(False, False): '/System/Library/Fonts/ Pro.ttf'}, |
| 364 |
'lisongpro': {(False, False): '/Library/Fonts/ Pro.ttf'}, |
| 365 |
'microsoftsansserif': {(False, False): '/Library/Fonts/Microsoft Sans Serif.ttf'}, |
| 366 |
'mshtakan': {(False, False): '/Library/Fonts/MshtakanRegular.ttf', |
| 367 |
(False, True): '/Library/Fonts/MshtakanOblique.ttf', |
| 368 |
(True, False): '/Library/Fonts/MshtakanBold.ttf', |
| 369 |
(True, True): '/Library/Fonts/MshtakanBoldOblique.ttf'}, |
| 370 |
'nadeem': {(False, False): '/Library/Fonts/Nadeem.ttf'}, |
| 371 |
'newpeninimmt': {(False, False): '/Library/Fonts/NewPeninimMT.ttf', |
| 372 |
(True, False): '/Library/Fonts/NewPeninimMTBoldInclined.ttf'}, |
| 373 |
'plantagenetcherokee': {(False, False): '/Library/Fonts/PlantagenetCherokee.ttf'}, |
| 374 |
'raanana': {(False, False): '/Library/Fonts/Raanana.ttf', |
| 375 |
(True, False): '/Library/Fonts/RaananaBold.ttf'}, |
| 376 |
'sathu': {(False, False): '/Library/Fonts/Sathu.ttf'}, |
| 377 |
'silom': {(False, False): '/Library/Fonts/Silom.ttf'}, |
| 378 |
'stfangsong': {(False, False): '/Library/Fonts/.ttf'}, |
| 379 |
'stheiti': {(False, False): '/System/Library/Fonts/.ttf'}, |
| 380 |
'stkaiti': {(False, False): '/Library/Fonts/.ttf'}, |
| 381 |
'stsong': {(False, False): '/Library/Fonts/.ttf'}, |
| 382 |
'tahoma': {(False, False): '/Library/Fonts/Tahoma.ttf', |
| 383 |
(True, False): '/Library/Fonts/Tahoma Bold.ttf'}, |
| 384 |
'thonburi': {(False, False): '/System/Library/Fonts/Thonburi.ttf', |
| 385 |
(True, False): '/System/Library/Fonts/ThonburiBold.ttf'}, |
| 386 |
'timesnewroman': {(False, False): '/Library/Fonts/Times New Roman.ttf', |
| 387 |
(False, True): '/Library/Fonts/Times New Roman Italic.ttf', |
| 388 |
(True, False): '/Library/Fonts/Times New Roman Bold.ttf', |
| 389 |
(True, True): '/Library/Fonts/Times New Roman Bold Italic.ttf'}, |
| 390 |
'trebuchetms': {(False, False): '/Library/Fonts/Trebuchet MS.ttf', |
| 391 |
(False, True): '/Library/Fonts/Trebuchet MS Italic.ttf', |
| 392 |
(True, False): '/Library/Fonts/Trebuchet MS Bold.ttf', |
| 393 |
(True, True): '/Library/Fonts/Trebuchet MS Bold Italic.ttf'}, |
| 394 |
'verdana': {(False, False): '/Library/Fonts/Verdana.ttf', |
| 395 |
(False, True): '/Library/Fonts/Verdana Italic.ttf', |
| 396 |
(True, False): '/Library/Fonts/Verdana Bold.ttf', |
| 397 |
(True, True): '/Library/Fonts/Verdana Bold Italic.ttf'}, |
| 398 |
'webdings': {(False, False): '/Library/Fonts/Webdings.ttf'}, |
| 399 |
'wingdings': {(False, False): '/Library/Fonts/Wingdings.ttf'}, |
| 400 |
'wingdings2': {(False, False): '/Library/Fonts/Wingdings 2.ttf'}, |
| 401 |
'wingdings3': {(False, False): '/Library/Fonts/Wingdings 3.ttf'}} |
| 402 |
|
| 403 |
|
| 404 |
def _search_osx_font_paths(fonts): |
| 405 |
|
| 406 |
for name, details in _OSX_default_font_files.items(): |
| 407 |
for k, apath in details.items(): |
| 408 |
if os.path.exists(apath): |
| 409 |
bold, italic = k |
| 410 |
_addfont(name, bold, italic, apath, fonts) |
| 411 |
|
| 412 |
|
| 413 |
def initsysfonts_darwin(): |
| 414 |
""" read the fonts on OSX. |
| 415 |
""" |
| 416 |
# if the X11 binary exists... try and use that. |
| 417 |
# Not likely to be there on pre 10.4.x ... |
| 418 |
# so still need to do other OSX specific method below. |
| 419 |
# if os.path.exists("/usr/X11/bin/fc-list"): |
| 420 |
# fonts = initsysfonts_unix("/usr/X11/bin/fc-list") |
| 421 |
# else: |
| 422 |
|
| 423 |
# On recent OSX, the above code will ask the user to install X11. Since |
| 424 |
# that's crazy, we disable it, and use the default font list. |
| 425 |
fonts = {} |
| 426 |
|
| 427 |
# we look for the default paths. |
| 428 |
_search_osx_font_paths(fonts) |
| 429 |
|
| 430 |
return fonts |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
paths = ['/Library/Fonts', |
| 437 |
'~/Library/Fonts', |
| 438 |
'/Local/Library/Fonts', |
| 439 |
'/Network/Library/Fonts'] |
| 440 |
fonts = {} |
| 441 |
for p in paths: |
| 442 |
if os.path.isdir(p): |
| 443 |
pass |
| 444 |
#os.path.walk(p, _fontwalk, fonts) |
| 445 |
return fonts |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
#read the fonts on unix |
| 451 |
def initsysfonts_unix(path = "fc-list"): |
| 452 |
import subprocess |
| 453 |
|
| 454 |
fonts = {} |
| 455 |
|
| 456 |
# we use the fc-list from fontconfig to get a list of fonts. |
| 457 |
|
| 458 |
try: |
| 459 |
# note, we capture stderr so if fc-list isn't there to stop stderr printing. |
| 460 |
flout, flerr = subprocess.Popen('%s : file family style' % path, shell=True, |
| 461 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 462 |
close_fds=True).communicate() |
| 463 |
except Exception: |
| 464 |
return fonts |
| 465 |
|
| 466 |
entries = toascii(flout) |
| 467 |
try: |
| 468 |
for line in entries.split('\n'): |
| 469 |
try: |
| 470 |
filename, family, style = line.split(':', 2) |
| 471 |
if filename[-4:].lower() in ['.ttf', '.ttc', '.otf']: |
| 472 |
bold = style.find('Bold') >= 0 |
| 473 |
italic = style.find('Italic') >= 0 |
| 474 |
oblique = style.find('Oblique') >= 0 |
| 475 |
for name in family.split(','): |
| 476 |
if name: |
| 477 |
break |
| 478 |
else: |
| 479 |
name = os.path.splitext(os.path.basename(filename))[0] |
| 480 |
_addfont(_simplename(name), |
| 481 |
bold, italic or oblique, filename, fonts) |
| 482 |
except Exception: |
| 483 |
# try the next one. |
| 484 |
pass |
| 485 |
except Exception: |
| 486 |
pass |
| 487 |
|
| 488 |
return fonts |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
#create alias entries |
| 493 |
def create_aliases(): |
| 494 |
aliases = ( |
| 495 |
('monospace', 'misc-fixed', 'courier', 'couriernew', 'console', |
| 496 |
'fixed', 'mono', 'freemono', 'bitstreamverasansmono', |
| 497 |
'verasansmono', 'monotype', 'lucidaconsole'), |
| 498 |
('sans', 'arial', 'helvetica', 'swiss', 'freesans', |
| 499 |
'bitstreamverasans', 'verasans', 'verdana', 'tahoma'), |
| 500 |
('serif', 'times', 'freeserif', 'bitstreamveraserif', 'roman', |
| 501 |
'timesroman', 'timesnewroman', 'dutch', 'veraserif', |
| 502 |
'georgia'), |
| 503 |
('wingdings', 'wingbats'), |
| 504 |
) |
| 505 |
for set in aliases: |
| 506 |
found = None |
| 507 |
fname = None |
| 508 |
for name in set: |
| 509 |
if name in Sysfonts: |
| 510 |
found = Sysfonts[name] |
| 511 |
fname = name |
| 512 |
break |
| 513 |
if not found: |
| 514 |
continue |
| 515 |
for name in set: |
| 516 |
if name not in Sysfonts: |
| 517 |
Sysalias[name] = found |
| 518 |
|
| 519 |
|
| 520 |
Sysfonts = {} |
| 521 |
Sysalias = {} |
| 522 |
|
| 523 |
#initialize it all, called once |
| 524 |
def initsysfonts(): |
| 525 |
if sys.platform == 'win32': |
| 526 |
fonts = initsysfonts_win32() |
| 527 |
elif sys.platform == 'darwin': |
| 528 |
fonts = initsysfonts_darwin() |
| 529 |
else: |
| 530 |
fonts = initsysfonts_unix() |
| 531 |
Sysfonts.update(fonts) |
| 532 |
create_aliases() |
| 533 |
if not Sysfonts: #dummy so we don't try to reinit |
| 534 |
Sysfonts[None] = None |
| 535 |
|
| 536 |
|
| 537 |
# pygame.font specific declarations |
| 538 |
def font_constructor(fontpath, size, bold, italic): |
| 539 |
import pygame_sdl2.font |
| 540 |
|
| 541 |
font = pygame_sdl2.font.Font(fontpath, size) |
| 542 |
if bold: |
| 543 |
font.set_bold(1) |
| 544 |
if italic: |
| 545 |
font.set_italic(1) |
| 546 |
|
| 547 |
return font |
| 548 |
|
| 549 |
|
| 550 |
#the exported functions |
| 551 |
|
| 552 |
def SysFont(name, size, bold=False, italic=False, constructor=None): |
| 553 |
"""pygame.font.SysFont(name, size, bold=False, italic=False, constructor=None) -> Font |
| 554 |
create a pygame Font from system font resources |
| 555 |
|
| 556 |
This will search the system fonts for the given font |
| 557 |
name. You can also enable bold or italic styles, and |
| 558 |
the appropriate system font will be selected if available. |
| 559 |
|
| 560 |
This will always return a valid Font object, and will |
| 561 |
fallback on the builtin pygame font if the given font |
| 562 |
is not found. |
| 563 |
|
| 564 |
Name can also be a comma separated list of names, in |
| 565 |
which case set of names will be searched in order. Pygame |
| 566 |
uses a small set of common font aliases, if the specific |
| 567 |
font you ask for is not available, a reasonable alternative |
| 568 |
may be used. |
| 569 |
|
| 570 |
if optional contructor is provided, it must be a function with |
| 571 |
signature constructor(fontpath, size, bold, italic) which returns |
| 572 |
a Font instance. If None, a pygame.font.Font object is created. |
| 573 |
""" |
| 574 |
if constructor is None: |
| 575 |
constructor = font_constructor |
| 576 |
|
| 577 |
if not Sysfonts: |
| 578 |
initsysfonts() |
| 579 |
|
| 580 |
gotbold = gotitalic = False |
| 581 |
fontname = None |
| 582 |
if name: |
| 583 |
allnames = name |
| 584 |
for name in allnames.split(','): |
| 585 |
name = _simplename(name) |
| 586 |
styles = Sysfonts.get(name) |
| 587 |
if not styles: |
| 588 |
styles = Sysalias.get(name) |
| 589 |
if styles: |
| 590 |
plainname = styles.get((False, False)) |
| 591 |
fontname = styles.get((bold, italic)) |
| 592 |
if not fontname and not plainname: |
| 593 |
# Neither requested style, nor plain font exists, so |
| 594 |
# return a font with the name requested, but an |
| 595 |
# arbitrary style. |
| 596 |
(style, fontname) = list(styles.items())[0] |
| 597 |
# Attempt to style it as requested. This can't |
| 598 |
# unbold or unitalicize anything, but it can |
| 599 |
# fake bold and/or fake italicize. |
| 600 |
if bold and style[0]: |
| 601 |
gotbold = True |
| 602 |
if italic and style[1]: |
| 603 |
gotitalic = True |
| 604 |
elif not fontname: |
| 605 |
fontname = plainname |
| 606 |
elif plainname != fontname: |
| 607 |
gotbold = bold |
| 608 |
gotitalic = italic |
| 609 |
if fontname: |
| 610 |
break |
| 611 |
|
| 612 |
set_bold = set_italic = False |
| 613 |
if bold and not gotbold: |
| 614 |
set_bold = True |
| 615 |
if italic and not gotitalic: |
| 616 |
set_italic = True |
| 617 |
|
| 618 |
return constructor(fontname, size, set_bold, set_italic) |
| 619 |
|
| 620 |
|
| 621 |
def get_fonts(): |
| 622 |
"""pygame.font.get_fonts() -> list |
| 623 |
get a list of system font names |
| 624 |
|
| 625 |
Returns the list of all found system fonts. Note that |
| 626 |
the names of the fonts will be all lowercase with spaces |
| 627 |
removed. This is how pygame internally stores the font |
| 628 |
names for matching. |
| 629 |
""" |
| 630 |
if not Sysfonts: |
| 631 |
initsysfonts() |
| 632 |
return list(Sysfonts.keys()) |
| 633 |
|
| 634 |
|
| 635 |
def match_font(name, bold=0, italic=0): |
| 636 |
"""pygame.font.match_font(name, bold=0, italic=0) -> name |
| 637 |
find the filename for the named system font |
| 638 |
|
| 639 |
This performs the same font search as the SysFont() |
| 640 |
function, only it returns the path to the TTF file |
| 641 |
that would be loaded. The font name can be a comma |
| 642 |
separated list of font names to try. |
| 643 |
|
| 644 |
If no match is found, None is returned. |
| 645 |
""" |
| 646 |
if not Sysfonts: |
| 647 |
initsysfonts() |
| 648 |
|
| 649 |
fontname = None |
| 650 |
allnames = name |
| 651 |
for name in allnames.split(','): |
| 652 |
name = _simplename(name) |
| 653 |
styles = Sysfonts.get(name) |
| 654 |
if not styles: |
| 655 |
styles = Sysalias.get(name) |
| 656 |
if styles: |
| 657 |
while not fontname: |
| 658 |
fontname = styles.get((bold, italic)) |
| 659 |
if italic: |
| 660 |
italic = 0 |
| 661 |
elif bold: |
| 662 |
bold = 0 |
| 663 |
elif not fontname: |
| 664 |
fontname = list(styles.values())[0] |
| 665 |
if fontname: break |
| 666 |
return fontname |
| 667 |
|
| 668 |
|