Source

pygame / lib / font.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
##    pygame - Python Game Library
##    Copyright (C) 2000-2003  Pete Shinners
##
##    This library is free software; you can redistribute it and/or
##    modify it under the terms of the GNU Library General Public
##    License as published by the Free Software Foundation; either
##    version 2 of the License, or (at your option) any later version.
##
##    This library is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
##    Library General Public License for more details.
##
##    You should have received a copy of the GNU Library General Public
##    License along with this library; if not, write to the Free
##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##

import glob, os, sys, subprocess

#
# font styles consist of (fullname, filetype, bold, italic)
#

_fonts = {}
_aliases = {}
_families = {}

# Fonts on windows
# Info taken from:
# http://www.microsoft.com/typography/fonts/winxp.htm
# with extra files added from:
# http://www.ampsoft.net/webdesign-l/windows-fonts-by-version.html
# File name, family, (Bold, Italic)
_win32_fontfiles = [
    ('ahronbd.ttf', 'Aharoni', True, False),
    ('andlso.ttf', 'Andalus', False, False),
    ('angsa.ttf', 'Angsana New', False, False),
    ('angsab.ttf', 'Angsana New', True, False),
    ('angsai.ttf', 'Angsana New', False, True),
    ('angsaz.ttf', 'Angsana New', True, True),
    ('angsau.ttf', 'AngsanaUPC', False, False),
    ('angsaub.ttf', 'AngsanaUPC', True, False),
    ('angsaui.ttf', 'AngsanaUPC', False, True),
    ('angsauz.ttf', 'AngsanaUPC', True, True),
    ('artro.ttf', 'Arabic Transparent', False, False),
    ('artrbdo.ttf', 'Arabic Transparent', True, False),
    ('agatha.ttf', 'Agatha', False, False),
    ('arial.ttf', 'Arial', False, False),
    ('arialbd.ttf', 'Arial', True, False),
    ('ariali.ttf', 'Arial', False, True),
    ('arialbi.ttf', 'Arial', True, True),
    ('ariblk.ttf', 'Arial Black', False, False),
    ('browa.ttf', 'Browallia New', False, False),
    ('browab.ttf', 'Browallia New', True, False),
    ('browai.ttf', 'Browallia New', False, True),
    ('browaz.ttf', 'Browallia New', True, True),
    ('browau.ttf', 'BrowalliaUPC', False, False),
    ('browaub.ttf', 'BrowalliaUPC', True, False),
    ('browaui.ttf', 'BrowalliaUPC', False, True),
    ('browauz.ttf', 'BrowalliaUPC', True, True),
    ('comic.ttf', 'Comic Sans MS', False, False),
    ('comicbd.ttf', 'Comic Sans MS', True, False),
    ('cordia.ttf', 'Cordia New', False, False),
    ('cordiab.ttf', 'Cordia New', True, False),
    ('cordiai.ttf', 'Cordia New', False, True),
    ('cordiaz.ttf', 'Cordia New', True, True),
    ('cordiau.ttf', 'CordiaUPC', False, False),
    ('cordiaub.ttf', 'CordiaUPC', True, False),
    ('cordiaui.ttf', 'CordiaUPC', False, True),
    ('cordiauz.ttf', 'CordiaUPC', True, True),
    ('cour.ttf', 'Courier New', False, False),
    ('courbd.ttf', 'Courier New', True, False),
    ('couri.ttf', 'Courier New', False, True),
    ('courbi.ttf', 'Courier New', True, True),
    ('david.ttf', 'David', False, False),
    ('davidbd.ttf', 'David', True, False),
    ('davidtr.ttf', 'David Transparent', False, False),
    ('upcdl.ttf', 'DilleniaUPC', False, False),
    ('upcdb.ttf', 'DilleniaUPC', True, False),
    ('upcdi.ttf', 'DilleniaUPC', False, True),
    ('upcdbi.ttf', 'DilleniaUPC', True, True),
    ('estre.ttf', 'Estrangelo Edessa', False, False),
    ('upcel.ttf', 'EucrosialUPC', False, False),
    ('upceb.ttf', 'EucrosialUPC', True, False),
    ('upcei.ttf', 'EucrosialUPC', False, True),
    ('upcebi.ttf', 'EucrosialUPC', True, True),
    ('mriamfx.ttf', 'Fixed Miriam Transparent', False, False),
    ('framd.ttf', 'Franklin Gothic Medium', False, False),
    ('framdit.ttf', 'Franklin Gothic Medium', False, True),
    ('frank.ttf', 'FrankRuehl', False, False),
    ('upcfl.ttf', 'FreesialUPC', False, False),
    ('upcfb.ttf', 'FreesialUPC', True, False),
    ('upcfi.ttf', 'FreesialUPC', False, True),
    ('upcfbi.ttf', 'FreesialUPC', True, True),
    ('gautami.ttf', 'Gautami', False, False),
    ('georgia.ttf', 'Georgia', False, False),
    ('georgiab.ttf', 'Georgia', True, False),
    ('georgiai.ttf', 'Georgia', False, True),
    ('georgiaz.ttf', 'Georgia', True, True),
    ('impact.ttf', 'Impact', False, False),
    ('upcil.ttf', 'IrisUPC', False, False),
    ('upcib.ttf', 'IrisUPC', True, False),
    ('upcii.ttf', 'IrisUPC', False, True),
    ('upcibi.ttf', 'IrisUPC', True, True),
    ('upcjl.ttf', 'JasmineUPC', False, False),
    ('upcjb.ttf', 'JasmineUPC', True, False),
    ('upcji.ttf', 'JasmineUPC', False, True),
    ('upcjbi.ttf', 'JasmineUPC', True, True),
    ('upckl.ttf', 'KodchiangUPC', False, False),
    ('upckb.ttf', 'KodchiangUPC', True, False),
    ('upcki.ttf', 'KodchiangUPC', False, True),
    ('upckbi.ttf', 'KodchiangUPC', True, True),
    ('latha.ttf', 'Latha', False, False),
    ('lvnm.ttf', 'Levenim MT', False, False),
    ('lvnmbd.ttf', 'Levenim MT', True, False),
    ('upcll.ttf', 'LilyUPC', False, False),
    ('upclb.ttf', 'LilyUPC', True, False),
    ('upcli.ttf', 'LilyUPC', False, True),
    ('upclbi.ttf', 'LilyUPC', True, True),
    ('lucon.ttf', 'Lucida Console', False, False),
    ('l_10646.ttf', 'Lucida Sans Unicode', False, False),
    ('mangal.ttf', 'Mangal', False, False),
    ('marlett.ttf', 'Marlett', False, False),
    ('micross.ttf', 'Microsoft Sans Serif', False, False),
    ('mriam.ttf', 'Miriam', False, False),
    ('mriamc.ttf', 'Miriam Fixed', False, False),
    ('mriamtr.ttf', 'Miriam Transparent', False, False),
    ('mvboli.ttf', 'MV Boli', False, False),
    ('nrkis.ttf', 'Narkisim', False, False),
    ('pala.ttf', 'Falatino Linotype', False, False),
    ('palab.ttf', 'Falatino Linotype', True, False),
    ('palai.ttf', 'Falatino Linotype', False, True),
    ('palabi.ttf', 'Falatino Linotype', True, True),
    ('raavi.ttf', 'Raavi', False, False),
    ('rod.ttf', 'Rod', False, False),
    ('rodtr.ttf', 'Rod Transparent', False, False),
    ('shruti.ttf', 'Shruti', False, False),
    ('simpo.ttf', 'Simplified Arabic', False, False),
    ('simpbdo.ttf', 'Simplified Arabic', True, False),
    ('simpfxo.ttf', 'Simplified Arabic Fixed', False, False),
    ('sylfaen.ttf', 'Sylfaen', False, False),
    ('symbol.ttf', 'Symbol', False, False),
    ('tahoma.ttf', 'Tahoma', False, False),
    ('tahomabd.ttf', 'Tahoma', True, False),
    ('times.ttf', 'Times New Roman', False, False),
    ('timesbd.ttf', 'Times New Roman', True, False),
    ('timesi.ttf', 'Times New Roman', False, True),
    ('timesbi.ttf', 'Times New Roman', True, True),
    ('trado.ttf', 'Traditional Arabic', False, False),
    ('tradbdo.ttf', 'Traditional Arabic', True, False),
    ('Trebuc.ttf', 'Trebuchet MS', False, False),
    ('Trebucbd.ttf', 'Trebuchet MS', True, False),
    ('Trebucit.ttf', 'Trebuchet MS', False, True),
    ('Trebucbi.ttf', 'Trebuchet MS', True, True),
    ('tunga.ttf', 'Tunga', False, False),
    ('verdana.ttf', 'Verdana', False, False),
    ('verdanab.ttf', 'Verdana', True, False),
    ('verdanai.ttf', 'Verdana', False, True),
    ('verdanaz.ttf', 'Verdana', True, True),
    ('webdings.ttf', 'Webdings', False, False),
    ('wingding.ttf', 'Wingdings', False, False),
    ('simhei.ttf', 'SimHei', False, False),
    ('simfang.ttf', 'FangSong_GB2312', False, False),
    ('kaiu.ttf', 'DFKai-SB', False, False),
    ('simkai.ttf', 'KaiTi_GB2312', False, False),
    ('msgothic.ttc', 'MS Gothic', False, False),
    ('msmincho.ttc', 'MS Mincho', False, False),
    ('gulim.ttc', 'Gulim', False, False),
    ('mingliu.ttc', 'Mingliu', False, False),
    ('simsun.ttc', 'Simsun', False, False),
    ('batang.ttc', 'Batang', False, False),
    ]

def _simplename (name):
    """_simplename (name) -> str

    Simplifies a font name, removing any characters not being alphanumeric.
    """
    return ''.join ([c.lower () for c in name if c.isalnum ()])

def _addfont (filename, name, ftype, family, bold, italic):
    """_addfont (filename, name, ftype, family, bold, italic) -> None

    Adds a font and its family to the internal font file caches.
    """
    family = _simplename (family)
    if family not in _families:
        _families[family] = []
    if name in _fonts:
        print ("*** Font %s will be overwritten" % name)
    _fonts[filename] = (name, ftype, bold, italic)
    _families[family].append (filename)

def _gettype (filename):
    """_gettype (filename) -> str

    Gets the type of the font file, based on its file extension.
    """
    ftype = os.path.splitext (filename)[1].lstrip (".")
    if filename.endswith (".pcf.gz"):
        ftype = "pcf"
    return ftype.lower ()

def _getstyle (style):
    """_getstyle (style) -> bool, bool

    Gets the bold and italic style properties from the passed style.
    """
    bold = italic = oblique = False
    if sys.platform == "win32":
        pass
    else:
        bold = style.find ("Bold") >= 0
        italic = style.find ("Italic") >= 0
        oblique = style.find ("Oblique") >= 0
    return bold, italic or oblique

def _initwin32 ():
    """_initwin32 () -> None
    
    Initializes the win32-based font cache.
    """
    fontdir = "C:\\Windows\\Fonts"
    if "WINDIR" in os.environ:
        fontdir = os.path.join (os.environ["WINDIR"], "Fonts")
    elif "windir" in os.environ:
        fontdir = os.path.join (os.environ["windir"], "Fonts")
    
    # Build the lookup table.
    lookups = dict ([(fname.lower (), (_simplename(name), bold, italic))
                     for fname, name, bold, italic in _win32_fontfiles])
    
    # Walk over the windows font file directory first.
    files = glob.glob (os.path.join (fontdir, "*"))
    for font in files:
        font = font.lower ()
        filename = os.path.basename (font)
        name = os.path.splitext (filename)[0]
        ftype = _gettype (filename)
        family = ""
        bold = italic = False
        if filename in lookups:
            name, bold, italic = lookups[filename]
        _addfont (font, name, ftype, family, bold, italic)

    # Lookup any mappings existing in the registry.
    try:
        import _winreg
    except ImportError:
        try:
            import winreg as _winreg
        except ImportError:
            return

    possible_keys = [
        r"SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts",
        r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
        r"SOFTWARE\Microsoft\Windows[NT]\CurrentVersion\Fonts",
        ]
    keys = []
    for key_name in possible_keys:
        try:
            key = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE, key_name)
            keys.append (key)
        except WindowsError:
            pass

    for key in keys:
        fontdict = {}
        for i in range (_winreg.QueryInfoKey (key)[1]):
            name, font, t = None, None, None
            try:
                name, font, t = _winreg.EnumValue (key, i)
            except EnvironmentError:
                break

            # try and handle windows unicode strings for some file names.
            
            # here are two documents with some information about it:
            # http://www.python.org/peps/pep-0277.html
            # https://www.microsoft.com/technet/archive/interopmigration/linux/mvc/lintowin.mspx#ECAA
            try:
                font = str (font)
            except UnicodeEncodeError:
                # MBCS is the windows encoding for unicode file names.
                try:
                    font = font.encode ('MBCS')
                except:
                    # no goodness with str or MBCS encoding... skip this font.
                    continue
            if os.path.sep not in font:
                font = os.path.join (fontdir, font)
            font = font.lower ()
            if font in _fonts:
                continue # Skip the font, if already detected.
            if name.find ("(") != -1:
                name = name[:name.find ("(")].rstrip ()
            name = name.lower ()

def _initunix ():
    """_initunix () -> None

    Initializes the unix-based font cache using the fontconfig utilities.
    """
    output = ""
    try:
        p = subprocess.Popen \
		("fc-list : file family style fullname fullnamelang",
                 shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = p.communicate()[0]
        retcode = p.returncode
        if sys.version_info[0] >= 3:
            output = str (output, "ascii")
    except OSError:
        _fonts[None] = None

    try:
        for line in output.split ("\n"):
            if line.strip () == "":
               continue 
            values = line.split (":")
            fullnames, fulllang, name = None, None, None
            filename, family, style = values[0:3]
            if len (values) > 3:
                pos = -1
                fullnames, fulllang = values[3:]
                langs = fulllang.split (",")
                if "fullnamelang=en" in langs:
                    pos = langs.index ("fullnamelang=en")
                else:
                    pos = langs.index ("en")
                if pos != -1:
                    name = fullnames.split (",")[pos].lstrip ("fullname=")
                    
            else:
                name = os.path.splitext (os.path.basename (filename))[0]
                if name.endswith (".pcf"):
                    name = name[:-4]
                name = _simplename (name)
            ftype = _gettype (filename)
            bold, italic = _getstyle (style)
            _addfont (filename, name, ftype, family, bold, italic)
    except Exception:
        _fonts[None] = None

def _initfonts ():
    """_initfonts () -> None

    Initializes the internal font caches.
    """
    if _fonts:
        return
    if sys.platform == "win32":
        _initwin32 ()
    elif sys.platform == "darwin":
        # TODO
        pass
    else:
        _initunix ()

def get_families ():
    """get_families () -> [str, str, str, ...]

    Gets the list of available font families.
    """
    if not _fonts:
        _initfonts ()
    return list (_families.keys ())

def find_font (name, bold=False, italic=False, ftype=None):
    """find_font (name, bold=False, italic=False, ftype=None) -> str, bool, bool

    Finds a font matching a certain family or font filename.

    Tries to find a font that matches the passed requirements best. The
    *name* argument denotes a specific font or font family name. If
    multiple fonts match that name, the *bold* and *italic* arguments
    are used to find a font that matches the requirements best. *ftype*
    is an optional font filetype argument to request specific font file
    types, such as bdf or ttf fonts.
    """
    if not _fonts:
        _initfonts ()

    if ftype:
        ftype = ftype.lower ()

    candidates = []
    #
    # font styles consist of (fullname, filetype, bold, italic)
    #
    for fname in _families.get (name, []):
        fullname, filetype, fbold, fitalic = _fonts[fname]
        if ftype and ftype != filetype:
            # The user requires a certain font filetype.
            continue

        if bold == fbold and italic == fitalic:
            # Exact style match
            candidates.append ((fname, fbold, fitalic, 0))
        elif italic and italic == fitalic:
            # Italic matches
            candidates.append ((fname, fbold, fitalic, 1))
        elif bold and bold == fbold:
            # Bold matches
            candidates.append ((fname, fbold, fitalic, 2))
        else:
            # None matches
            candidates.append ((fname, fbold, fitalic, 3))
    if candidates:
        candidates.sort(key=lambda x: x[3])
        return candidates[0][0], candidates[0][1], candidates[0][2]

    for items in _fonts.items():
        fname = items[0]
        fullname, filetype, fbold, fitalic = items[1]
        if fullname != name and name not in fname:
            nl = name.lower ()
            fl = fullname.lower ()
            if nl not in fl and nl not in fname.lower():
                continue
        if ftype and ftype != filetype:
            # The user requires a certain font filetype.
            continue

        if bold == fbold and italic == fitalic:
            # Exact style match
            candidates.append ((fname, fbold, fitalic, 0))
        elif italic and italic == fitalic:
            # Italic matches
            candidates.append ((fname, fbold, fitalic, 1))
        elif bold and bold == fbold:
            # Bold matches
            candidates.append ((fname, fbold, fitalic, 2))
        else:
            # None matches
            candidates.append ((fname, fbold, fitalic, 3))
    if candidates:
        candidates.sort(key=lambda x: x[3])
        return candidates[0][0], candidates[0][1], candidates[0][2]
    return None