src/reportlab/lib/fonts.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
     1
#!/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 93
diff changeset
     3
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2216
diff changeset
     4
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/fonts.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4128
diff changeset
     5
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     6
__doc__='''Utilities to associate bold and italic versions of fonts into families
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     7
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     8
Bold, italic and plain fonts are usually implemented in separate disk files;
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     9
but non-trivial apps want <b>this</b> to do the right thing.   We therefore
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    10
need to keep 'mappings' between the font family name and the right group
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    11
of up to 4 implementation fonts to use.
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    12
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    13
Most font-handling code lives in pdfbase, and this probably should too.
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    14
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    15
'''
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    16
import sys, os
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    17
###############################################################################
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    18
#   A place to put useful font stuff
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    19
###############################################################################
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    20
#
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    21
#      Font Mappings
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    22
# The brute force approach to finding the correct postscript font name;
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    23
# much safer than the rule-based ones we tried.
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    24
# preprocessor to reduce font face names to the shortest list
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    25
# possible.  Add any aliases you wish; it keeps looking up
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    26
# until it finds no more translations to do.  Any input
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    27
# will be lowercased before checking.
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    28
_family_alias = {
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    29
            'serif':'times',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    30
            'sansserif':'helvetica',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    31
            'monospaced':'courier',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    32
            'arial':'helvetica'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    33
            }
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    34
#maps a piddle font to a postscript one.
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    35
_tt2ps_map = {
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    36
            #face, bold, italic -> ps name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    37
            ('times', 0, 0) :'Times-Roman',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    38
            ('times', 1, 0) :'Times-Bold',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    39
            ('times', 0, 1) :'Times-Italic',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    40
            ('times', 1, 1) :'Times-BoldItalic',
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    41
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    42
            ('courier', 0, 0) :'Courier',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    43
            ('courier', 1, 0) :'Courier-Bold',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    44
            ('courier', 0, 1) :'Courier-Oblique',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    45
            ('courier', 1, 1) :'Courier-BoldOblique',
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    46
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    47
            ('helvetica', 0, 0) :'Helvetica',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    48
            ('helvetica', 1, 0) :'Helvetica-Bold',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    49
            ('helvetica', 0, 1) :'Helvetica-Oblique',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    50
            ('helvetica', 1, 1) :'Helvetica-BoldOblique',
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 1782
diff changeset
    51
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    52
            # there is only one Symbol font
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    53
            ('symbol', 0, 0) :'Symbol',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    54
            ('symbol', 1, 0) :'Symbol',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    55
            ('symbol', 0, 1) :'Symbol',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    56
            ('symbol', 1, 1) :'Symbol',
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    57
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    58
            # ditto for dingbats
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    59
            ('zapfdingbats', 0, 0) :'ZapfDingbats',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    60
            ('zapfdingbats', 1, 0) :'ZapfDingbats',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    61
            ('zapfdingbats', 0, 1) :'ZapfDingbats',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    62
            ('zapfdingbats', 1, 1) :'ZapfDingbats',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    63
            }
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    64
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    65
_ps2tt_map={}
4128
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    66
for k in sorted(_tt2ps_map.keys()):
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    67
    v = _tt2ps_map[k].lower()
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    68
    if v not in _ps2tt_map:
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    69
        _ps2tt_map[v] = k
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    70
    v = k[0].lower()
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    71
    if v not in _ps2tt_map:
309d3bc30482 fonts.py: force family name into _ps2tt_map
robin
parents: 3723
diff changeset
    72
        _ps2tt_map[v] = k
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    73
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    74
def ps2tt(psfn):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    75
    'ps fontname to family name, bold, italic'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    76
    psfn = psfn.lower()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    77
    if psfn in _ps2tt_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    78
        return _ps2tt_map[psfn]
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3326
diff changeset
    79
    raise ValueError("Can't map determine family/bold/italic for %s" % psfn)
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    80
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    81
def tt2ps(fn,b,i):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    82
    'family name + bold & italic to ps font name'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    83
    K = (fn.lower(),b,i)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    84
    if K in _tt2ps_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    85
        return _tt2ps_map[K]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    86
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    87
        fn, b1, i1 = ps2tt(K[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    88
        K = fn, b1|b, i1|i
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    89
        if K in _tt2ps_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    90
            return _tt2ps_map[K]
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3326
diff changeset
    91
    raise ValueError("Can't find concrete font for family=%s, bold=%d, italic=%d" % (fn, b, i))
1075
1decdb11bdaa revert to Andy's version
rgbecker
parents: 1074
diff changeset
    92
1decdb11bdaa revert to Andy's version
rgbecker
parents: 1074
diff changeset
    93
def addMapping(face, bold, italic, psname):
2216
aadcd9dc9480 Font reregistration cleanup
andy_robinson
parents: 2200
diff changeset
    94
    'allow a custom font to be put in the mapping'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    95
    k = face.lower(), bold, italic
2216
aadcd9dc9480 Font reregistration cleanup
andy_robinson
parents: 2200
diff changeset
    96
    _tt2ps_map[k] = psname
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    97
    _ps2tt_map[psname.lower()] = k