"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.23/src/reportlab/pdfbase/_can_cmap_data.py" (26 Mar 2014, 1827 Bytes) of package /linux/privat/reportlab-3.5.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "_can_cmap_data.py" see the Fossies "Dox" file reference documentation.

    1 #
    2 """
    3 This is a utility to 'can' the widths data for certain CID fonts.
    4 Now we're using Unicode, we don't need 20 CMAP files for each Asian
    5 language, nor the widths of the non-normal characters encoded in each
    6 font.  we just want a dictionary of the character widths in a given
    7 font which are NOT 1000 ems wide, keyed on Unicode character (not CID).
    8 
    9 Running off CMAP files we get the following widths...::
   10 
   11     >>> font = UnicodeCIDFont('HeiseiMin-W3')
   12     >>> font.stringWidth(unicode(','), 10)
   13     2.5
   14     >>> font.stringWidth(unicode('m'), 10)
   15     7.7800000000000002
   16     >>> font.stringWidth(u'\u6771\u4EAC', 10)
   17     20.0
   18     >>> 
   19 
   20 """
   21 
   22 from pprint import pprint as pp
   23 
   24 from reportlab.pdfbase._cidfontdata import defaultUnicodeEncodings
   25 from reportlab.pdfbase.cidfonts import UnicodeCIDFont
   26 
   27 
   28 def run():
   29 
   30     buf = []
   31     buf.append('widthsByUnichar = {}')
   32     for fontName, (language, encName) in defaultUnicodeEncodings.items():
   33         print('handling %s : %s : %s' % (fontName, language, encName))
   34 
   35         #this does just about all of it for us, as all the info
   36         #we need is present.
   37         font = UnicodeCIDFont(fontName)
   38 
   39         widthsByCID = font.face._explicitWidths
   40         cmap = font.encoding._cmap
   41         nonStandardWidthsByUnichar = {}
   42         for codePoint, cid in cmap.items():
   43             width = widthsByCID.get(cid, 1000)
   44             if width != 1000:
   45                 nonStandardWidthsByUnichar[chr(codePoint)] = width
   46         
   47 
   48         
   49         print('created font width map (%d items).  ' % len(nonStandardWidthsByUnichar))
   50 
   51         buf.append('widthsByUnichar["%s"] = %s' % (fontName, repr(nonStandardWidthsByUnichar)))
   52         
   53         
   54     src = '\n'.join(buf) + '\n'
   55     open('canned_widths.py','w').write(src)
   56     print('wrote canned_widths.py')
   57 
   58 if __name__=='__main__':
   59     run()
   60