;
0sDc           @   s  d  Z  d Z d k Z d k l Z l Z l Z d k l Z l	 Z	 l
 Z
 d k Z d k Z d k Z d k Z d k l Z d k l Z l Z l Z l Z l Z d k l Z d k l Z d	 k l Z d
 k l Z e Z d   Z  d   Z! d e i" f d     YZ# d e i$ f d     YZ% d e i& f d     YZ' d e' f d     YZ( d   Z) d   Z* e+ d j o# d k, Z, d k- Z- e, i. e-  n d S(   s3    $Id: cidfonts.py 2905 2006-05-23 14:49:28Z andy $ s   CID (Asian multi-byte) font support.

This defines classes to represent CID fonts.  They know how to calculate
their own width and how to write themselves into PDF files.N(   s   ListTypes	   TupleTypes   DictType(   s   finds   splits   strip(   s
   pdfmetrics(   s   allowedTypeFacess   allowedEncodingss   CIDFontInfos   defaultUnicodeEncodingss   widthsByUnichar(   s   Canvas(   s   pdfdoc(   s   _escape(   s   CMapSearchPathc         C   sN   x: t  D]2 } | t i |  } t i i |  o | Sq q Wt d |   d S(   s&   Returns full filename, or raises errors'   CMAP file for encodings "%s" not found!N(	   s   CMapSearchPaths   dirnames   oss   seps   names   cmapfiles   paths   isfiles   IOError(   s   names   cmapfiles   dirname(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   findCMapFile!   s      c         C   s   t  |   t j oD h  } x* |  i   D] \ } } t |  | | <q& Wt i	 |  SnY t  |   t
 t f j o; g  } x! |  D] } | i t |   q} Wt i |  Sn |  Sd S(   sC   Converts deeply nested structure to PDFdoc dictionary/array objectsN(   s   types	   structures   DictTypes   newDicts   itemss   ks   vs   structToPDFs   pdfdocs   PDFDictionarys   ListTypes	   TupleTypes   newLists   elems   appends   PDFArray(   s	   structures   newDicts   ks   elems   newLists   v(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   structToPDF*   s       s   CIDEncodingc           B   sP   t  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(	   s   Multi-byte encoding.  These are loaded from CMAP files.

    A CMAP file is like a mini-codec.  It defines the correspondence
    between code points in the (multi-byte) input data and Character
    IDs. i   c         C   s   | |  _  t |  _ g  |  _ g  |  _ h  |  _ t |  _ t o | o d k
 l } | d  } t i i | t i | d  o) |  i |  | t i | d |  _ q |  i |  d | |  _ |  i |  q |  i |  n d  S(   N(   s   get_rl_tempdirs	   FastCMAPSs   .fastmaps   CMAP: (   s   names   selfs   Nones   _mapFileHashs   _codeSpaceRangess   _notDefRangess   _cmaps   sources   DISABLE_CMAPs   useCaches   reportlab.lib.utilss   get_rl_tempdirs
   fontmapdirs   oss   paths   isfiles   seps   fastLoads   parseCMAPFiles   fastSave(   s   selfs   names   useCaches   get_rl_tempdirs
   fontmapdir(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   __init__E   s"    						"c         C   s'   t  i   } | i |  | i   Sd  S(   N(   s   md5s   news   hashers   updates   texts   digest(   s   selfs   texts   hasher(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   _hashZ   s    c         C   s  t  |  } t | d  i   }	 |  i |	  |  _ t	 |	 d  } | d j o4 |	 d | !} t |  }
 |
 d } |  i |  n t |	  }
 x1|
 g  j o#|
 d d j o |
 d }
 x|
 d d j oj |
 d |
 d |
 d f \ } } }
 t | d d !d	  } t | d d !d	  } |  i i | | f  q Wq |
 d d
 j o |
 d }
 xc|
 d d j ou |
 d d !\ } } } t | d d !d	  } t | d d !d	  } t |  } |  i i | | | f  |
 d }
 q[Wq |
 d d j o |
 d }
 x |
 d d j o |
 d d !\ } } } t | d d !d	  } t | d d !d	  } t |  } d } x5 | | | j o# | | |  i | | <| d } qlW|
 d }
 qWq |
 d }
 q Wd S(   sv   This is a tricky one as CMAP files are Postscript
        ones.  Some refer to others with a 'usecmap'
        commands   rs   usecmapii    s   begincodespacerangei   s   endcodespacerangei   i   s   beginnotdefranges   endnotdefrangei   s   begincidranges   endcidrangeN(   s   findCMapFiles   names   cmapfiles   opens   reads   rawdatas   selfs   _hashs   _mapFileHashs   finds   usecmap_poss   chunks   splits   wordss   otherCMAPNames   parseCMAPFiles   strStarts   strEnds   ints   starts   ends   _codeSpaceRangess   appends   strValues   values   _notDefRangess   offsets   _cmap(   s   selfs   names   usecmap_poss   chunks   cmapfiles   strEnds   values   starts   strValues   rawdatas   wordss   offsets   ends   strStarts   otherCMAPName(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   parseCMAPFile_   s\     
 
 $
 
  c         C   sH  g  } |  i } d } x(| D] } | d j o t |  d t |  } n t |  } | } d } x |  i
 D] \ }
 } |
 | j  o
 | j  n o y | | } Wn_ t j
 oS d } xH |  i D]8 \ }	 } } |	 | j  o
 | j  n o | } Pq q Wn X| i |  d } Pqo qo W| o
 d } q | } q W| Sd S(   s$   Convert a string into a list of CIDss    i   i    i   N(   s   outputs   selfs   _cmaps   cmaps   lastChars   texts   chars   ords   nums   founds   _codeSpaceRangess   lows   highs   cids   KeyErrors   _notDefRangess   low2s   high2s   notdefs   append(   s   selfs   texts   highs   nums   cids   high2s   notdefs   chars   cmaps   low2s   lows   founds   outputs   lastChar(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys	   translate   s<     	 
 
 	

c         C   s   t  t i i | |  i d  d  } t i	 |  i
 |  t i	 |  i |  t i	 |  i |  t i	 |  i |  | i   d  S(   Ns   .fastmaps   wb(   s   opens   oss   paths   joins	   directorys   selfs   names   fs   marshals   dumps   _mapFileHashs   _codeSpaceRangess   _notDefRangess   _cmaps   close(   s   selfs	   directorys   f(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   fastSave   s    %c         C   s   t  i   } t t i i | |  i	 d  d  } t i |  |  _ t i |  |  _ t i |  |  _ t i |  |  _ | i   t  i   } d  S(   Ns   .fastmaps   rb(   s   times   clocks   starteds   opens   oss   paths   joins	   directorys   selfs   names   fs   marshals   loads   _mapFileHashs   _codeSpaceRangess   _notDefRangess   _cmaps   closes   finished(   s   selfs	   directorys   fs   starteds   finished(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   fastLoad   s    %
c         C   s8   h  d |  i <d |  i <d |  i <d |  i <Sd S(   s@   Simple persistence helper.  Return a dict with all that matters.s   mapFileHashs   codeSpaceRangess   notDefRangess   cmapN(   s   selfs   _mapFileHashs   _codeSpaceRangess   _notDefRangess   _cmap(   s   self(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   getData   s     (
   s   __name__s
   __module__s   __doc__s   __init__s   _hashs   parseCMAPFiles	   translates   fastSaves   fastLoads   getData(    (    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   CIDEncoding9   s    		A	%		s   CIDTypeFacec           B   s2   t  Z d  Z d   Z d   Z d   Z d   Z RS(   s   Multi-byte type face.

    Conceptually similar to a single byte typeface,
    but the glyphs are identified by a numeric Character
    ID (CID) and not a glyph name. c         C   s$   t  i i |  |  |  i |  d S(   sm   Initialised from one of the canned dictionaries in allowedEncodings

        Or rather, it will be shortly...N(   s
   pdfmetricss   TypeFaces   __init__s   selfs   names   _extractDictInfo(   s   selfs   name(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   __init__   s     c         C   s   y t  | } Wn. t j
 o" t d | d t t   n X| d d } | d d |  _ | d d |  _	 | d |  _
 |  i | d	  |  _ d  S(
   Ns/   Unable to find information on CID typeface '%s's#   Only the following font names work:s   DescendantFontsi    s   FontDescriptors   Ascents   Descents   DWs   W(   s   CIDFontInfos   names   fontDicts   KeyErrors   reprs   allowedTypeFacess   descFonts   selfs   ascents   descents   _defaultWidths   _expandWidthss   _explicitWidths(   s   selfs   names   descFonts   fontDict(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   _extractDictInfo   s     c   
      C   s   | }	 h  } x |	 o |	 d |	 d f \ } }	 t |	 d  t t f j oM |	 d |	 d f \ } }	 x| t t	 |   D] } | | | | | <q{ Wq |	 d |	 d |	 d f \ } } }	 x% t | | d  D] } | | | <q Wq W| Sd S(   s  Expands Adobe nested list structure to get a dictionary of widths.

        Here is an example of such a structure.
        (
            # starting at character ID 1, next n  characters have the widths given.
            1,  (277,305,500,668,668,906,727,305,445,445,508,668,305,379,305,539),
            # all Characters from ID 17 to 26 are 668 em units wide
            17, 26, 668,
            27, (305, 305, 668, 668, 668, 566, 871, 727, 637, 652, 699, 574, 555,
                 676, 687, 242, 492, 664, 582, 789, 707, 734, 582, 734, 605, 605,
                 641, 668, 727, 945, 609, 609, 574, 445, 668, 445, 668, 668, 590,
                 555, 609, 547, 602, 574, 391, 609, 582, 234, 277, 539, 234, 895,
                 582, 605, 602, 602, 387, 508, 441, 582, 562, 781, 531, 570, 555,
                 449, 246, 449, 668),
            # these must be half width katakana and the like.
            231, 632, 500
        )
        i    i   i   N(   s   compactWidthArrays   datas   widthss   starts   types   ListTypes	   TupleTypes   itemss   ranges   lens   offsets   ends   widths   idx(
   s   selfs   compactWidthArrays   ends   idxs   widths   widthss   offsets   itemss   starts   data(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   _expandWidths   s        $ c         C   s   |  i i | |  i  Sd  S(   N(   s   selfs   _explicitWidthss   gets   characterIds   _defaultWidth(   s   selfs   characterId(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   getCharWidth!  s    (   s   __name__s
   __module__s   __doc__s   __init__s   _extractDictInfos   _expandWidthss   getCharWidth(    (    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   CIDTypeFace   s
    			!s   CIDFontc           B   s5   t  Z d  Z d   Z d   Z e d  Z d   Z RS(   s%   Represents a built-in multi-byte fontc         C   s   d |  _ | t j p t d | t f  | |  _ t |  |  _ | t j p t d | t f  | |  _	 t
 |  |  _ |  i d |  i	 |  _ |  i |  _ |  i	 d d j |  _ g  |  _ d  S(   Ni   s9   TypeFace '%s' not supported! Use any of these instead: %ss:   Encoding '%s' not supported!  Use any of these instead: %ss   -is   V(   s   selfs
   _multiBytes   faces   allowedTypeFacess   AssertionErrors   faceNames   CIDTypeFaces   encodings   allowedEncodingss   encodingNames   CIDEncodings   fontNames   names
   isVerticals   substitutionFonts(   s   selfs   faces   encoding(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   __init__&  s    	!	!	c         C   s   t  |  } | Sd  S(   N(   s   _escapes   texts   encoded(   s   selfs   texts   encoded(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   formatForPdf=  s    c         C   sk   |  i i |  } |  i o t |  | Sn: d } x$ | D] } | |  i
 i |  } q; Wd | | Sd S(   sJ   This presumes non-Unicode input.  UnicodeCIDFont wraps it for that contexti    f0.001N(   s   selfs   encodings	   translates   texts   cidlists
   isVerticals   lens   sizes   ws   cids   faces   getCharWidth(   s   selfs   texts   sizes   encodings   cidlists   cids   w(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   stringWidthB  s     
 c         C   s   d t  t | i  d  } t |  i i } d | | d <d |  i
 | d <t |  } | i | |  } | i d i } | | | <d | | i |  i <d S(   s   The explicit code in addMinchoObjects and addGothicObjects
        will be replaced by something that pulls the data from
        _cidfontdata.py in the next few days.s   Fi   s   /s   Names   Encodings
   BasicFontsN(   s   reprs   lens   docs   fontMappings   internalNames   CIDFontInfos   selfs   faces   names   bigDicts   encodingNames   structToPDFs   cidObjs	   References   rs
   idToObjects   dicts   fontDict(   s   selfs   docs   internalNames   cidObjs   bigDicts   rs   fontDict(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys
   addObjectsP  s     
(   s   __name__s
   __module__s   __doc__s   __init__s   formatForPdfs   Nones   stringWidths
   addObjects(    (    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   CIDFont$  s
    		s   UnicodeCIDFontc           B   s2   t  Z d  Z e e d  Z d   Z e d  Z RS(   sX  Wraps up CIDFont to hide explicit encoding choice;
    encodes text for output as UTF16.

    lang should be one of 'jpn',chs','cht','kor' for now.
    if vertical is set, it will select a different widths array
    and possibly glyphs for some punctuation marks.

    halfWidth is only for Japanese.


    >>> dodgy = UnicodeCIDFont('nonexistent')
    Traceback (most recent call last):
    ...
    KeyError: "don't know anything about CID font nonexistent"
    >>> heisei = UnicodeCIDFont('HeiseiMin-W3')
    >>> heisei.name
    'HeiseiMin-W3'
    >>> heisei.language
    'jpn'
    >>> heisei.encoding.name
    'UniJIS-UCS2-H'
    >>> #This is how PDF data gets encoded.
    >>> print heisei.formatForPdf('hello')
    \377\376h\000e\000l\000l\000o\000
    >>> tokyo = u'\u6771\u4AEC'
    >>> print heisei.formatForPdf(tokyo)
    \377\376qg\354J
    
    c         C   s   y t  | \ } } Wn# t j
 o t d |   n X| |  _ | d  } | o | d } n | o | d } n | d } t
 i |  | |  | |  _ |  _ | |  _ | |  _ t |  i |  _ d  S(   Ns%   don't know anything about CID font %sis   HW-s   Vs   H(   s   defaultUnicodeEncodingss   faces   langs   defaultEncodings   KeyErrors   selfs   languages   encs   isHalfWidths
   isVerticals   CIDFonts   __init__s   names   fontNames   verticals   widthsByUnichars   unicodeWidths(   s   selfs   faces
   isVerticals   isHalfWidths   langs   encs   defaultEncoding(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   __init__  s     	

		c         C   sW   d k  l } t |  t j	 o | i d  } n | |  d } t |  } | Sd  S(   N(   s   utf_16_be_encodes   utf8i    (	   s   codecss   utf_16_be_encodes   types   texts   unicodes   decodes   utfTexts   _escapes   encoded(   s   selfs   texts   utf_16_be_encodes   utfTexts   encoded(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   formatForPdf  s    c         C   sx   t  |  t  d  j o | i d  } n |  i } | d t g  i } | D] } | | i | d   qM ~  Sd S(   s8   Just ensure we do width test on characters, not bytes...s    s   utf8f0.001i  N(   s   types   texts   decodes   selfs   unicodeWidthss   widthss   sizes   sums   appends   _[1]s   uchs   get(   s   selfs   texts   sizes   encodings   _[1]s   widthss   uch(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   stringWidth  s
     	(   s   __name__s
   __module__s   __doc__s   Falses   __init__s   formatForPdfs   Nones   stringWidth(    (    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   UnicodeCIDFontd  s    	c         C   s   d  k  } | i |   } xv | D]n } | i i |  | i t i	 d  o q n y t
 |  } Wn d | GHq n X| i |   d | GHq Wd  S(   Ns   .fastmaps   cannot parse %s, skippings   saved %s.fastmap(   s   oss   listdirs   cmapdirs   filess   files   paths   isfiles   seps   selfs   names   CIDEncodings   encs   fastSave(   s   cmapdirs   filess   encs   files   os(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   precalculate  s    	 %	c          C   s   t  d  }  |  i d d  |  i d d d  t i t d d   t i t d	 d   |  i d
 d  d } |  i d d |  |  i   d GHd } t
 |  } | Gd G| i |  GHt d d  } d | i | d  GHd  S(   Ns   test_japanese.pdfs	   Helveticai   id   i  s   Japanese Font Supports   HeiseiMin-W3s   90ms-RKSJ-Hs   HeiseiKakuGo-W5s   HeiseiMin-W3-90ms-RKSJ-Hi   s   ͕łBi  s   saved test_japanese.pdfs   ->s   width = %0.2fi
   (   s   Canvass   cs   setFonts
   drawStrings
   pdfmetricss   registerFonts   CIDFonts   message1s   saves   encNames   CIDEncodings   encs	   translates   fs   stringWidth(   s   cs   encs   encNames   fs   message1(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   test  s    

s   __main__(/   s   __version__s   __doc__s   oss   typess   ListTypes	   TupleTypes   DictTypes   strings   finds   splits   strips   marshals   md5s   times	   reportlabs   reportlab.pdfbases
   pdfmetricss   reportlab.pdfbase._cidfontdatas   allowedTypeFacess   allowedEncodingss   CIDFontInfos   defaultUnicodeEncodingss   widthsByUnichars   reportlab.pdfgen.canvass   Canvass   pdfdocs   reportlab.pdfbase.pdfutilss   _escapes   reportlab.rl_configs   CMapSearchPaths   Trues   DISABLE_CMAPs   findCMapFiles   structToPDFs   Encodings   CIDEncodings   TypeFaces   CIDTypeFaces   Fonts   CIDFonts   UnicodeCIDFonts   precalculates   tests   __name__s   doctests   cidfontss   testmod("   s   Canvass   CIDFonts   CMapSearchPaths   CIDTypeFaces   precalculates   strips   finds   allowedEncodingss   defaultUnicodeEncodingss	   reportlabs   structToPDFs   cidfontss   splits   UnicodeCIDFonts   tests   __version__s   __doc__s   DictTypes   CIDEncodings
   pdfmetricss   ListTypes   allowedTypeFacess	   TupleTypes   _escapes   md5s   CIDFontInfos   findCMapFiles   widthsByUnichars   DISABLE_CMAPs   doctests   pdfdocs   times   oss   marshal(    (    sD   /home/packages/reportlab/reportlab_2_0/reportlab/pdfbase/cidfonts.pys   ?   s4   					%			A@X		2