
    KwgPI                        d Z dZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ dZd Zd Z G d dej8                        Z G d dej<                        Z G d dej@                        Z! G d de!      Z"d Z#d Z$e%dk(  rddl&Z&ddlm'Z'  e&jP                  e'       yy)z3.3.0zCID (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)md5)
pdfmetrics)allowedTypeFacesallowedEncodingsCIDFontInfodefaultUnicodeEncodingswidthsByUnichar)Canvas)pdfdoc	escapePDF)CMapSearchPath)isSeqisBytesTc                     t         D ];  }|t        j                  z   | z   }t        j                  j	                  |      s9|c S  t        d| z        )z&Returns full filename, or raises errorz'CMAP file for encodings "%s" not found!)r   osseppathisfileIOError)namedirnamecmapfiles      Q/var/www/horilla/myenv/lib/python3.12/site-packages/reportlab/pdfbase/cidfonts.pyfindCMapFiler      sL    ! RVV#d*77>>(#O	
 ;dB
CC    c                 &   t        | t              r=i }| j                         D ]  \  }}t        |      ||<    t	        j
                  |      S t        |       r8g }| D ]  }|j                  t        |              t	        j                  |      S | S )zCConverts deeply nested structure to PDFdoc dictionary/array objects)	
isinstancedictitemsstructToPDFr   PDFDictionaryr   appendPDFArray)	structurenewDictkvnewListelems         r   r!   r!   '   s    )D!OO% 	(DAq$QGAJ	(##G,,	y	 	.DNN;t,-	.w''r   c                   <    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d Z
y	)CIDEncodingzMulti-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. c                    || _         d | _        g | _        g | _        i | _        d | _        t        s|rddlm}  |d      }t        j                  j                  |t        j                  z   |z   dz         r0| j                  |       |t        j                  z   |z   dz   | _        y | j                  |       d|z   | _        | j                  |       y | j                  |       y y )Nr   )get_rl_tempdir	FastCMAPS.fastmapzCMAP: )r   _mapFileHash_codeSpaceRanges_notDefRanges_cmapsourceDISABLE_CMAPreportlab.lib.utilsr.   r   r   r   r   fastLoadparseCMAPFilefastSave)selfr   useCacher.   
fontmapdirs        r   __init__zCIDEncoding.__init__B   s    	  "
>+K8
77>>*rvv"5"<z"IJMM*-",rvv"5"<z"IDK&&t,"*T/DKMM*-""4( r   c                 X    t               }|j                  |       |j                         S N)r   updatedigest)r;   texthashers      r   _hashzCIDEncoding._hashW   s!    d}}r   c                    t        |      }t        |d      j                         }| j                  |      | _        |j                  d      }|dkD  r+|d| }|j                         }|d   }| j                  |       |j                         }|g k7  rn|d   dk(  rb|dd }|d   dk7  rP|d   |d   |d	d }}	}t        |dd d
      }
t        |	dd d
      }| j                  j                  |
|f       |d   dk7  rSn|d   dk(  rl|dd }|d   dk7  r|dd \  }}	}t        |dd d
      }
t        |	dd d
      }t        |      }| j                  j                  |
||f       |dd }|d   dk7  r^n|d   dk(  r{|dd }|d   dk7  rs|dd \  }}	}t        |dd d
      }
t        |	dd d
      }t        |      }d}|
|z   |k  r#||z   | j                  |
|z   <   |dz   }|
|z   |k  r#|dd }|d   dk7  rmn|dd }|g k7  rmyy)zvThis is a tricky one as CMAP files are Postscript
        ones.  Some refer to others with a 'usecmap'
        commandrusecmapr   begincodespacerange   Nendcodespacerange      beginnotdefrangeendnotdefrange   begincidrangeendcidrange)r   openreadrE   r1   findsplitr9   intr2   r#   r3   r4   )r;   r   r   rawdatausecmap_poschunkwordsotherCMAPNamestrStartstrEndstartendstrValuevalueoffsets                  r   r9   zCIDEncoding.parseCMAPFile\   s   
  %x%**, JJw/ ll9-" Ak*EKKME!"IM}-
 rkQx00ab	Ah"55.3Aha%)efH23EfQrlB/C))00%?	 Ah"55
 q//ab	Ah"2216q.Hfh23EfQrlB/CME&&--uc5.AC!!"IE Ah"22 q_,ab	Ah-/16q.Hfh23EfQrlB/CME F&.C/5:V^

56>2!'!  &.C/ "!"IE Ah-/ ab	E rkr   c                    g }| j                   }d}|D ]u  }|dk7  rt        |      dz  t        |      z   }nt        |      }|}d}| j                  D ].  \  }}	||cxk  r|	k  sn 	 ||   }
|j                  |
       d} n |rd}t|}w |S # t        $ r, d}
| j                  D ]  \  }}}||cxk  r|k  sn |}
 n Y Uw xY w)z$Convert a string into a list of CIDs    r   rK   )r4   ordr2   KeyErrorr3   r#   )r;   rC   outputcmaplastCharcharnumfoundlowhighcidlow2high2notdefs                 r   	translatezCIDEncoding.translate   s   zz 	 D2~(mc)CI5 $iHE!22 	T#t#&"3i MM#&E!" ;	 < ! $ &  373E3E &/D%#c1E1&, %&&s   'B)C7CCc                    t        t        j                  j                  || j                  dz         d      }t        j                  | j                  |       t        j                  | j                  |       t        j                  | j                  |       t        j                  | j                  |       |j                          y )Nr0   wb)rT   r   r   joinr   marshaldumpr1   r2   r3   r4   close)r;   	directoryfs      r   r:   zCIDEncoding.fastSave   s}    iZ)?@$GT&&*T**A.T''+TZZ#		r   c                    t        j                         }t        t        j                  j                  || j                  dz         d      }t        j                  |      | _	        t        j                  |      | _
        t        j                  |      | _        t        j                  |      | _        |j                          t        j                         }y )Nr0   rb)timeclockrT   r   r   ry   r   rz   loadr1   r2   r3   r4   r|   )r;   r}   startedr~   finisheds        r   r8   zCIDEncoding.fastLoad   s    **,iZ)?@$G#LLO 'Q$\\!_\\!_
		::<r   c                 `    | j                   | j                  | j                  | j                  dS )z@Simple persistence helper.  Return a dict with all that matters.)mapFileHashcodeSpaceRangesnotDefRangesrk   )r1   r2   r3   r4   )r;   s    r   getDatazCIDEncoding.getData   s0      ,,#44 ..JJ	 	r   N)rK   )__name__
__module____qualname____doc__r>   rE   r9   rv   r:   r8   r    r   r   r,   r,   6   s,    )*
="B#J r   r,   c                   (    e Zd ZdZd Zd Zd Zd Zy)CIDTypeFacezMulti-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                 f    t         j                  j                  | |       | j                  |       y)zmInitialised from one of the canned dictionaries in allowedEncodings

        Or rather, it will be shortly...N)r   TypeFacer>   _extractDictInfo)r;   r   s     r   r>   zCIDTypeFace.__init__   s(     	$$T40d#r   c                     	 t         |   }|d   d   }|d   d   | _        |d   d   | _        |d   | _        | j                  |d	         | _        y # t        $ r" t        d|z  dz   t        t              z         w xY w)
Nz/Unable to find information on CID typeface '%s'z#Only the following font names work:DescendantFontsr   FontDescriptorAscentDescentDWW)	r   ri   reprr   ascentdescent_defaultWidth_expandWidths_explicitWidths)r;   r   fontDictdescFonts       r   r   zCIDTypeFace._extractDictInfo   s    	\"4(H -.q1/0: 01)<%d^#11(3-@  	\LtSABDHIYDZ[ \ \	\s   	A +A<c                     |dd }i }|rr|d   |dd }}t        |d         r/|d   |dd }}t        t        |            D ]  }||   |||z   <    n(|d   |d   |dd }}}t        ||dz         D ]  }	|||	<   	 |rr|S )a  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
            )
        
        Nr   rK   rM   )r   rangelen)
r;   compactWidthArraydatawidthsr`   r    rd   ra   widthidxs
             r   r   zCIDTypeFace._expandWidths   s    * !#q'484ET!W~"1gtABxt#CJ/ ;F-26]F56>*; $(7DGT!"XDU A. (C"'F3K(  r   c                 N    | j                   j                  || j                        S r@   )r   getr   )r;   characterIds     r   getCharWidthzCIDTypeFace.getCharWidth  s!    ##''T5G5GHHr   N)r   r   r   r   r>   r   r   r   r   r   r   r   r      s    '
$
A !FIr   r   c                   .    e Zd ZdZdZd Zd ZddZd Zy)	CIDFontz%Represents a built-in multi-byte fontrK   c                 d   |t         v sJ d|dt                || _        t        |      | _        |t        v sJ d|dt               || _        t        |      | _        | j                  dz   | j
                  z   | _        | j                  | _	        | j
                  d   dk(  | _
        g | _        y )Nz
TypeFace 'z+' not supported! Use any of these instead: z
Encoding 'z,' not supported!  Use any of these instead: -rI   V)r   faceNamer   facer   encodingNamer,   encodingfontNamer   
isVerticalsubstitutionFonts)r;   r   r   s      r   r>   zCIDFont.__init__$  s    ''hln~)'%	++  	Imu  xH  .I  	I+$#H- +d.?.??MM	  ,,R0C7 "$r   c                     t        |      }|S r@   r   )r;   rC   encodeds      r   formatForPdfzCIDFont.formatForPdf9  s    D/r   Nc                     | j                   j                  |      }| j                  rt        |      |z  S d}|D ]   }|| j                  j                  |      z   }" d|z  |z  S )zJThis presumes non-Unicode input.  UnicodeCIDFont wraps it for that contextr   MbP?)r   rv   r   r   r   r   )r;   rC   sizer   cidlistwrr   s          r   stringWidthzCIDFont.stringWidth>  sj    --))$/?? w<$&&A 4		..s33419t##r   c                 h   dt        t        |j                        dz         z   }t        | j                  j
                     }d|z   |d<   d| j                  z   |d<   t        |      }|j                  ||      }|j                  d   j                  }|||<   d|z   |j                  | j
                  <   y)zThe explicit code in addMinchoObjects and addGothicObjects
        will be replaced by something that pulls the data from
        _cidfontdata.py in the next few days.FrK   /NameEncoding
BasicFontsN)r   r   fontMappingr   r   r   r   r!   	Reference
idToObjectr   )r;   docinternalNamebigDictcidObjrG   r   s          r   
addObjectszCIDFont.addObjectsL  s     T#coo"6q"899diinn-,!D$5$55
 W% MM&,/>>,/44!"%(<%7		"r   r@   )	r   r   r   r   
_multiByter>   r   r   r   r   r   r   r   r      s    +J$*
$8r   r   c                   &    e Zd ZdZddZd ZddZy)UnicodeCIDFontu  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'))
    \000h\000e\000l\000l\000o
    >>> tokyo = u'東䫬'
    >>> print(heisei.formatForPdf(tokyo))
    gqJ\354
    >>> print(heisei.stringWidth(tokyo,10))
    20.0
    >>> print(heisei.stringWidth('hello world',10))
    45.83
    c                 *   	 t         |   \  }}|| _        |d d }|r|dz   }|r|dz   }n|dz   }t        j	                  | ||       |x| _        | _        || _        || _        t        | j
                     | _
        y # t        $ r t        d|z        w xY w)Nz%don't know anything about CID font %srI   zHW-r   H)r   ri   languager   r>   r   r   verticalisHalfWidthr	   unicodeWidths)r;   r   r   r   langdefaultEncodingencs          r   r>   zUnicodeCIDFont.__init__  s    	K$;D$A!D/
  cr"+C)C)C 	tS) %)(	DM"&,TYY71  	KBTIJJ	Ks   A: :Bc                 v    ddl m} t        |      r|j                  d      } ||      d   }t	        |      }|S )Nr   )utf_16_be_encodeutf8)codecsr   r   decoder   )r;   rC   r   utfTextr   s        r   r   zUnicodeCIDFont.formatForPdf  s8    +4=;;v&D"4(+G$r   Nc           
          t        |      r|j                  d      }| j                  }|dz  t        |D cg c]  }|j	                  |d       c}      z  S c c}w )z8Just ensure we do width test on characters, not bytes...r   r   i  )r   r   r   sumr   )r;   rC   r   r   r   uchs         r   r   zUnicodeCIDFont.stringWidth  sO    4=;;v&D##e|cD"IS6::c4#8"IJJJ"Is   A
)FFr@   )r   r   r   r   r>   r   r   r   r   r   r   r   `  s    B8>	 Kr   r   c                    dd l } |j                  |       }|D ]\  }|j                  j                  | |j                  z   |z   dz         r2	 t        |      }|j                  |        t        d|z         ^ y #  t        dz         Y rxY w)Nr   r0   zcannot parse %s, skippingzsaved %s.fastmap)r   listdirr   r   r   r,   printr:   )cmapdirr   filesfiler   s        r   precalculater     s    BJJwE 	)77>>'BFF*T1J>?	d#C 	W 4'(	)
	-34s   A99Bc                  
   t        d      } | j                  dd       | j                  ddd       t        j                  t        dd             t        j                  t        d	d             | j                  d
d       d}| j                  dd|       | j                          t        d       d}t        |      }t        |d|j                  |             t        dd      }t        d|j                  |d      z         y )Nztest_japanese.pdf	Helvetica   d   i  zJapanese Font SupportzHeiseiMin-W3z90ms-RKSJ-HzHeiseiKakuGo-W5zHeiseiMin-W3-90ms-RKSJ-HrN   u'   ±êÍ½¬¾©Å·Bi  zsaved test_japanese.pdfz->zwidth = %0.2f
   )r
   setFont
drawStringr   registerFontr   saver   r,   rv   r   )cmessage1encNamer   r~   s        r   testr     s    "#AIIk2LLS12GN=ABG$5mDE II("- _HLLc8$FFH	
#$ G
g
C	(D#--12}-A	/AMM(B7
78r   __main__)cidfonts))__version__r   r   rz   r   hashlibr   reportlab.pdfbaser   reportlab.pdfbase._cidfontdatar   r   r   r   r	   reportlab.pdfgen.canvasr
   r   reportlab.lib.rl_accelr   reportlab.rl_configr   r7   r   r   r6   r   r!   r   r,   r   r   Fontr   r   r   r   r   doctestr   testmodr   r   r   <module>r     s   
 	?
 
    (. . * $ , . . Df*%% fP@I*%% @ID=8joo =8@WKW WKv)!9d Z*GOOH r   