
    Q?hPI                        S r SrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
JrJrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SrS rS r " S S\R8                  5      r " S S\R<                  5      r " S S\R@                  5      r! " S S\!5      r"S r#S r$\%S:X  a  SSK&r&SSKJ'r'  \&RP                  " \'5        gg)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                     [          HA  nU[        R                  -   U -   n[        R                  R	                  U5      (       d  M?  Us  $    [        SU -  5      e)z&Returns full filename, or raises errorz'CMAP file for encodings "%s" not found!)r   osseppathisfileIOError)namedirnamecmapfiles      L/var/www/html/env/lib/python3.13/site-packages/reportlab/pdfbase/cidfonts.pyfindCMapFiler      sJ    !RVV#d*77>>(##O	 "
 ;dB
CC    c                 B   [        U [        5      (       a>  0 nU R                  5        H  u  p#[        U5      X'   M     [        R
                  " U5      $ [        U 5      (       a;  / nU  H  nUR                  [        U5      5        M     [        R                  " U5      $ U $ )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%DA$QGJ &##G,,	y		DNN;t,- w''r   c                   F    \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
rg)CIDEncoding6   zMulti-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                    Xl         S U l        / U l        / U l        0 U l        S U l        [        (       d  U(       a  SSKJn  U" S5      n[        R                  R                  U[        R                  -   U-   S-   5      (       a0  U R                  U5        U[        R                  -   U-   S-   U l        g U R                  U5        SU-   U l        U R                  U5        g U R                  U5        g g )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__CIDEncoding.__init__B   s    	  "
|>+K8
77>>*rvv"5"<z"IJJMM*-",rvv"5"<z"IDK&&t,"*T/DKMM*-""4( r   c                 X    [        5       nUR                  U5        UR                  5       $ N)r   updatedigest)r<   texthashers      r   _hashCIDEncoding._hashW   s!    d}}r   c                    [        U5      n[        US5      R                  5       nU R                  U5      U l        UR                  S5      nUS:  a+  USU nUR                  5       nUS   nU R                  U5        UR                  5       nU/ :w  Gav  US   S:X  ac  USS nUS   S:w  aS  US   US   US	S pin[        USS S
5      n
[        U	SS S
5      nU R                  R                  X45        US   S:w  a  MS  GO US   S:X  am  USS nUS   S:w  a^  USS u  pn[        USS S
5      n
[        U	SS S
5      n[        U5      nU R                  R                  XU45        USS nUS   S:w  a  M^  OUS   S:X  a|  USS nUS   S:w  am  USS u  pn[        USS S
5      n
[        U	SS S
5      n[        U5      nSnX-   U::  a"  X-   U R                  X-   '   US-   nX-   U::  a  M"  USS nUS   S:w  a  Mm  OUSS nU/ :w  a  GMu  gg)zfThis 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readrG   r2   findsplitr:   intr3   r#   r4   r5   )r<   r   r   rawdatausecmap_poschunkwordsotherCMAPNamestrStartstrEndstartendstrValuevalueoffsets                  r   r:   CIDEncoding.parseCMAPFile\   s   
  %x%**, JJw/ ll9-" Ak*EKKME!"IM}-
 rkQx00ab	Ah"55.3Aha%)eH23EfQrlB/C))00%?	 Ah"55
 q//ab	Ah"2216q.Hh23EfQrlB/CME&&--u5.AC!!"IE Ah"22 q_,ab	Ah-/16q.Hh23EfQrlB/CME F.C/5:^

5>2!'!  .C/ "!"IE Ah-/ ab	E rkkr   c                    / nU R                   nSnU H  nUS:w  a  [        U5      S-  [        U5      -   nO[        U5      nUnSnU R                   H/  u  pXs=:  a  U	:  d  M  O  M   X6   n
UR                  U
5        Sn  O   U(       a  SnM}  UnM     U$ ! [         a1    Sn
U R                   H  u  pnXs=:  a  U:  d  M  O  M  Un
   M`      Ndf = f)z$Convert a string into a list of CIDs    r   rN   )r5   ordr3   KeyErrorr4   r#   )r<   rE   outputcmaplastCharcharnumfoundlowhighcidlow2high2notdefs                 r   	translateCIDEncoding.translate   s    zzD2~(mc)CI5 $iHE!22	#t##&"i MM#&E! 3" ; < ! $ &  373E3E/D#1E11&, % 4F&s   *B(CCCCc                    [        [        R                  R                  XR                  S-   5      S5      n[
        R                  " U R                  U5        [
        R                  " U R                  U5        [
        R                  " U R                  U5        [
        R                  " U R                  U5        UR                  5         g )Nr1   wb)rW   r   r   joinr   marshaldumpr2   r3   r4   r5   close)r<   	directoryfs      r   r;   CIDEncoding.fastSave   s{    iZ)?@$GT&&*T**A.T''+TZZ#		r   c                    [         R                  " 5       n[        [        R                  R                  XR                  S-   5      S5      n[        R                  " U5      U l	        [        R                  " U5      U l
        [        R                  " U5      U l        [        R                  " U5      U l        UR                  5         [         R                  " 5       ng )Nr1   rb)timeclockrW   r   r   r~   r   r   loadr2   r3   r4   r5   r   )r<   r   startedr   finisheds        r   r9   CIDEncoding.fastLoad   s    **,iZ)?@$G#LLO 'Q$\\!_\\!_
		::<r   c                 `    U R                   U R                  U R                  U R                  S.$ )z@Simple persistence helper.  Return a dict with all that matters.)mapFileHashcodeSpaceRangesnotDefRangesro   )r2   r3   r4   r5   )r<   s    r   getDataCIDEncoding.getData   s0      ,,#44 ..JJ	 	r   )r5   r3   r2   r4   r   r6   N)rN   )__name__
__module____qualname____firstlineno____doc__r?   rG   r:   rz   r;   r9   r   __static_attributes__ r   r   r,   r,   6   s,    )*
="B#J r   r,   c                   0    \ rS rSrSrS rS rS rS rSr	g)	CIDTypeFace   zMulti-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                 d    [         R                  R                  X5        U R                  U5        g)zeInitialised from one of the canned dictionaries in allowedEncodings

Or rather, it will be shortly...N)r   TypeFacer?   _extractDictInfo)r<   r   s     r   r?   CIDTypeFace.__init__   s&     	$$T0d#r   c                      [         U   nUS   S   nUS   S   U l        US   S   U l        US   U l        U R                  US	   5      U l        g ! [         a"    [        SU-  S-   [        [        5      -   5      ef = f)
Nz/Unable to find information on CID typeface '%s'z#Only the following font names work:DescendantFontsr   FontDescriptorAscentDescentDWW)	r   rm   reprr   ascentdescent_defaultWidth_expandWidths_explicitWidths)r<   r   fontDictdescFonts       r   r   CIDTypeFace._extractDictInfo   s    	\"4(H -.q1/0: 01)<%d^#11(3-@  	\LtSABDHIYDZ[ \ \	\s   	A ,A=c                    USS n0 nU(       az  US   USS p$[        US   5      (       a.  US   USS p%[        [        U5      5       H  nXV   X4U-   '   M     O'US   US   USS p(n[        XGS-   5       H  n	XU	'   M	     U(       a  Mz  U$ )a8  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   rN   rP   )r   rangelen)
r<   compactWidthArraydatawidthsrc   r    rg   rd   widthidxs
             r   r   CIDTypeFace._expandWidths   s    * !#q'484T!W~~"1gtABxt#CJ/F-2]F6>* 0 $(7DGT!"XD A.C"'3K / d r   c                 L    U R                   R                  XR                  5      $ rB   )r   getr   )r<   characterIds     r   getCharWidthCIDTypeFace.getCharWidth  s    ##''5G5GHHr   )r   r   r   r   N)
r   r   r   r   r   r?   r   r   r   r   r   r   r   r   r      s    '
$
A !FIr   r   c                   8    \ rS rSrSrSrS rS rS
S jrS r	S	r
g)CIDFonti   z%Represents a built-in multi-byte fontrN   c                 p   U[         ;   d   SU< S[         < 35       eXl        [        U5      U l        U[        ;   d   SU< S[        < 35       eX l        [        U5      U l        U R                  S-   U R
                  -   U l        U R                  U l	        U R
                  S   S:H  U l
        / U l        g )Nz
TypeFace 'z+' not supported! Use any of these instead: z
Encoding 'z,' not supported!  Use any of these instead: -rL   V)r   faceNamer   facer   encodingNamer,   encodingfontNamer   
isVerticalsubstitutionFonts)r<   r   r   s      r   r?   CIDFont.__init__$  s    ''hln~)'%	++  	Imu  xH  .I  	I+$#H- +d.?.??MM	  ,,R0C7 "$r   c                     [        U5      nU$ rB   r   )r<   rE   encodeds      r   formatForPdfCIDFont.formatForPdf9  s    D/r   Nc                     U R                   R                  U5      nU R                  (       a  [        U5      U-  $ SnU H   nXPR                  R                  U5      -   nM"     SU-  U-  $ )zJThis presumes non-Unicode input.  UnicodeCIDFont wraps it for that contextr   MbP?)r   rz   r   r   r   r   )r<   rE   sizer   cidlistwrv   s          r   stringWidthCIDFont.stringWidth>  sc    --))$/?? w<$&&A		..s33 19t##r   c                 d   S[        [        UR                  5      S-   5      -   n[        U R                  R
                     nSU-   US'   SU R                  -   US'   [        U5      nUR                  XB5      nUR                  S   R                  nXVU'   SU-   UR                  U R
                  '   g)zThe explicit code in addMinchoObjects and addGothicObjects
will be replaced by something that pulls the data from
_cidfontdata.py in the next few days.FrN   /NameEncoding
BasicFontsN)r   r   fontMappingr   r   r   r   r!   	Reference
idToObjectr   )r<   docinternalNamebigDictcidObjrJ   r   s          r   
addObjects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   r   r   r   rB   )r   r   r   r   r   
_multiByter?   r   r   r   r   r   r   r   r   r      s    +J$*
$8r   r   c                   2    \ rS rSrSrSS jrS rS	S jrSrg)
UnicodeCIDFonti`  uF  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                 6    [         U   u  pEX@l        US S nU(       a  US-   nU(       a  US-   nOUS-   n[        R	                  XU5        U=U l        U l        X l        X0l        [        U R
                     U l
        g ! [         a    [        SU-  5      ef = f)Nz%don't know anything about CID font %srL   zHW-r   H)r   rm   languager   r?   r   r   verticalisHalfWidthr	   unicodeWidths)r<   r   r   r   langdefaultEncodingencs          r   r?   UnicodeCIDFont.__init__  s    	K$;D$A!D
  cr"+C)C)C 	S) %)(	DM"&,TYY71  	KBTIJJ	Ks   A? ?Bc                     SSK Jn  [        U5      (       a  UR                  S5      nU" U5      S   n[	        U5      nU$ )Nr   )utf_16_be_encodeutf8)codecsr   r   decoder   )r<   rE   r   utfTextr   s        r   r   UnicodeCIDFont.formatForPdf  s:    +4==;;v&D"4(+G$r   Nc           
          [        U5      (       a  UR                  S5      nU R                  nUS-  [        U Vs/ s H  oTR	                  US5      PM     sn5      -  $ s  snf )z8Just ensure we do width test on characters, not bytes...r   r   i  )r   r   r   sumr   )r<   rE   r   r   r   uchs         r   r   UnicodeCIDFont.stringWidth  sS    4==;;v&D##e|cD"IDS::c4#8D"IJJJ"Is   A
)r   r   r   r   r   r   )FFrB   )	r   r   r   r   r   r?   r   r   r   r   r   r   r   r   `  s    B8>	 Kr   r   c                 ,   SS K nUR                  " U 5      nU Hb  nUR                  R                  XR                  -   U-   S-   5      (       a  M7   [        U5      nUR                  U 5        [        SU-  5        Md     g !   [        SW-  5         Mz  = f)Nr   r1   zcannot parse %s, skippingzsaved %s.fastmap)r   listdirr   r   r   r,   printr;   )cmapdirr   filesfiler   s        r   precalculater    s    JJwE77>>'FF*T1J>??	d#C 	W 4'( 
	-34s   B  Bc                     [        S5      n U R                  SS5        U R                  SSS5        [        R                  " [        SS5      5        [        R                  " [        S	S5      5        U R                  S
S5        SnU R                  SSU5        U R                  5         [        S5        Sn[        U5      n[        USUR                  U5      5        [        SS5      n[        SUR                  US5      -  5        g )Nztest_japanese.pdf	Helvetica   d   i  zJapanese Font SupportzHeiseiMin-W3z90ms-RKSJ-HzHeiseiKakuGo-W5zHeiseiMin-W3-90ms-RKSJ-HrQ   u'   ±êÍ½¬¾©Å·Bi  zsaved test_japanese.pdfz->zwidth = %0.2f
   )r
   setFont
drawStringr   registerFontr   saver   r,   rz   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   r   r   hashlibr   reportlab.pdfbaser   reportlab.pdfbase._cidfontdatar   r   r   r   r	   reportlab.pdfgen.canvasr
   r   reportlab.lib.rl_accelr   reportlab.rl_configr   r8   r   r   r7   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*OOH r   