src/reportlab/lib/fonts.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 4128 309d3bc30482
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
     1
#!/bin/env python
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3368
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2012
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
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2216
diff changeset
     5
__version__=''' $Id$ '''
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={}
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    66
for k,v in _tt2ps_map.items():
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    67
    if k not in _ps2tt_map:
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    68
        _ps2tt_map[v.lower()] = k
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    69
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    70
def ps2tt(psfn):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    71
    'ps fontname to family name, bold, italic'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    72
    psfn = psfn.lower()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    73
    if psfn in _ps2tt_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    74
        return _ps2tt_map[psfn]
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3326
diff changeset
    75
    raise ValueError("Can't map determine family/bold/italic for %s" % psfn)
93
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    76
ac4b5a84f307 Fonts utilities initial version
rgbecker
parents:
diff changeset
    77
def tt2ps(fn,b,i):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    78
    'family name + bold & italic to ps font name'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    79
    K = (fn.lower(),b,i)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    80
    if K in _tt2ps_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    81
        return _tt2ps_map[K]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    82
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    83
        fn, b1, i1 = ps2tt(K[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    84
        K = fn, b1|b, i1|i
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    85
        if K in _tt2ps_map:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1159
diff changeset
    86
            return _tt2ps_map[K]
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3326
diff changeset
    87
    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
    88
1decdb11bdaa revert to Andy's version
rgbecker
parents: 1074
diff changeset
    89
def addMapping(face, bold, italic, psname):
2216
aadcd9dc9480 Font reregistration cleanup
andy_robinson
parents: 2200
diff changeset
    90
    'allow a custom font to be put in the mapping'
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    91
    k = face.lower(), bold, italic
2216
aadcd9dc9480 Font reregistration cleanup
andy_robinson
parents: 2200
diff changeset
    92
    _tt2ps_map[k] = psname
2815
0a9baa55d61f reportlab: added registerFontFamily
rgbecker
parents: 2332
diff changeset
    93
    _ps2tt_map[psname.lower()] = k