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-- |
93 | 1 |
#!/bin/env python |
4330 | 2 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
494 | 3 |
#see license.txt for license details |
2332 | 4 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/fonts.py |
4252 | 5 |
__version__='3.3.0' |
3029 | 6 |
__doc__='''Utilities to associate bold and italic versions of fonts into families |
7 |
||
8 |
Bold, italic and plain fonts are usually implemented in separate disk files; |
|
9 |
but non-trivial apps want <b>this</b> to do the right thing. We therefore |
|
10 |
need to keep 'mappings' between the font family name and the right group |
|
11 |
of up to 4 implementation fonts to use. |
|
12 |
||
13 |
Most font-handling code lives in pdfbase, and this probably should too. |
|
14 |
||
15 |
''' |
|
2815 | 16 |
import sys, os |
93 | 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 | 19 |
############################################################################### |
20 |
# |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1159
diff
changeset
|
21 |
# Font Mappings |
93 | 22 |
# The brute force approach to finding the correct postscript font name; |
23 |
# much safer than the rule-based ones we tried. |
|
24 |
# preprocessor to reduce font face names to the shortest list |
|
25 |
# possible. Add any aliases you wish; it keeps looking up |
|
26 |
# until it finds no more translations to do. Any input |
|
27 |
# will be lowercased before checking. |
|
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 | 34 |
#maps a piddle font to a postscript one. |
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 | 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 | 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 | 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 | 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 | 64 |
|
65 |
_ps2tt_map={} |
|
4128 | 66 |
for k in sorted(_tt2ps_map.keys()): |
67 |
v = _tt2ps_map[k].lower() |
|
68 |
if v not in _ps2tt_map: |
|
69 |
_ps2tt_map[v] = k |
|
70 |
v = k[0].lower() |
|
71 |
if v not in _ps2tt_map: |
|
72 |
_ps2tt_map[v] = k |
|
93 | 73 |
|
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 | 76 |
psfn = psfn.lower() |
3326 | 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 | 80 |
|
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 | 83 |
K = (fn.lower(),b,i) |
3326 | 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 | 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 | 92 |
|
93 |
def addMapping(face, bold, italic, psname): |
|
2216 | 94 |
'allow a custom font to be put in the mapping' |
2815 | 95 |
k = face.lower(), bold, italic |
2216 | 96 |
_tt2ps_map[k] = psname |
2815 | 97 |
_ps2tt_map[psname.lower()] = k |