src/reportlab/pdfbase/_can_cmap_data.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 4709 1dcf7b3f7127
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:
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     1
#
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     2
"""
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     3
This is a utility to 'can' the widths data for certain CID fonts.
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     4
Now we're using Unicode, we don't need 20 CMAP files for each Asian
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     5
language, nor the widths of the non-normal characters encoded in each
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     6
font.  we just want a dictionary of the character widths in a given
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     7
font which are NOT 1000 ems wide, keyed on Unicode character (not CID).
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
     8
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     9
Running off CMAP files we get the following widths...::
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    10
3376
9f1f91de9b3e reportlab: fix doctests
rgbecker
parents: 3326
diff changeset
    11
    >>> font = UnicodeCIDFont('HeiseiMin-W3')
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    12
    >>> font.stringWidth(unicode(','), 10)
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    13
    2.5
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    14
    >>> font.stringWidth(unicode('m'), 10)
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    15
    7.7800000000000002
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    16
    >>> font.stringWidth(u'\u6771\u4EAC', 10)
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    17
    20.0
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    18
    >>> 
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    19
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    20
"""
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    21
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    22
from pprint import pprint as pp
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    23
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    24
from reportlab.pdfbase._cidfontdata import defaultUnicodeEncodings
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    25
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    26
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    27
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    28
def run():
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    29
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    30
    buf = []
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    31
    buf.append('widthsByUnichar = {}')
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    32
    for fontName, (language, encName) in defaultUnicodeEncodings.items():
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3376
diff changeset
    33
        print('handling %s : %s : %s' % (fontName, language, encName))
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    34
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    35
        #this does just about all of it for us, as all the info
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    36
        #we need is present.
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    37
        font = UnicodeCIDFont(fontName)
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    38
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    39
        widthsByCID = font.face._explicitWidths
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    40
        cmap = font.encoding._cmap
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    41
        nonStandardWidthsByUnichar = {}
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    42
        for codePoint, cid in cmap.items():
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    43
            width = widthsByCID.get(cid, 1000)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3032
diff changeset
    44
            if width != 1000:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3376
diff changeset
    45
                nonStandardWidthsByUnichar[chr(codePoint)] = width
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    46
        
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    47
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    48
        
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3376
diff changeset
    49
        print('created font width map (%d items).  ' % len(nonStandardWidthsByUnichar))
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    50
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    51
        buf.append('widthsByUnichar["%s"] = %s' % (fontName, repr(nonStandardWidthsByUnichar)))
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    52
        
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    53
        
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    54
    src = '\n'.join(buf) + '\n'
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    55
    open('canned_widths.py','w').write(src)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3376
diff changeset
    56
    print('wrote canned_widths.py')
2771
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    57
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    58
if __name__=='__main__':
11f5967f7dd3 Add missing svn:eol-style properties
jjlee
parents: 2629
diff changeset
    59
    run()
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
    60