
    jke                        d dl 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Zd dlZd dlZd dlZ ej                   e      Zd Z G d	 d
ej                        Z G d de      Z G d de      ZdZ G d de      Z G d de      ZdZd Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z!d  Z"d! Z# G d" d#e      Z$ G d$ d%e      Z%eeeee e!e$d&Z&y)'    )	bytesjoinsafeEvalreadHex)getEncoding)getSearchRange)Unicode   )DefaultTableNc                     t        |      t        |      k(  sJ | j                  |      }i }t        |||      D ]  \  }}}|dk(  r|||<    |S Nr   )lengetGlyphNameManyzip)fontcharsgids
glyphNamescmapchargidnames           A/usr/lib/python3/dist-packages/fontTools/ttLib/tables/_c_m_a_p.py	_make_mapr      se    u:T"""&&t,JDudJ7 c4!8T
 K    c                   F    e Zd ZdZd Z	 ddZd Zd ZddZd Z	d Z
d	 Zy
)table__c_m_a_pa  Character to Glyph Index Mapping Table

    This class represents the `cmap <https://docs.microsoft.com/en-us/typography/opentype/spec/cmap>`_
    table, which maps between input characters (in Unicode or other system encodings)
    and glyphs within the font. The ``cmap`` table contains one or more subtables
    which determine the mapping of of characters to glyphs across different platforms
    and encoding systems.

    ``table__c_m_a_p`` objects expose an accessor ``.tables`` which provides access
    to the subtables, although it is normally easier to retrieve individual subtables
    through the utility methods described below. To add new subtables to a font,
    first determine the subtable format (if in doubt use format 4 for glyphs within
    the BMP, format 12 for glyphs outside the BMP, and format 14 for Unicode Variation
    Sequences) construct subtable objects with ``CmapSubtable.newSubtable(format)``,
    and append them to the ``.tables`` list.

    Within a subtable, the mapping of characters to glyphs is provided by the ``.cmap``
    attribute.

    Example::

            cmap4_0_3 = CmapSubtable.newSubtable(4)
            cmap4_0_3.platformID = 0
            cmap4_0_3.platEncID = 3
            cmap4_0_3.language = 0
            cmap4_0_3.cmap = { 0xC1: "Aacute" }

            cmap = newTable("cmap")
            cmap.tableVersion = 0
            cmap.tables = [cmap4_0_3]
    c                 l    | j                   D ]%  }|j                  |k(  s|j                  |k(  s#|c S  y)ag  Returns the first subtable which matches the given platform and encoding.

        Args:
                platformID (int): The platform ID. Use 0 for Unicode, 1 for Macintosh
                        (deprecated for new fonts), 2 for ISO (deprecated) and 3 for Windows.
                encodingID (int): Encoding ID. Interpretation depends on the platform ID.
                        See the OpenType specification for details.

        Returns:
                An object which is a subclass of :py:class:`CmapSubtable` if a matching
                subtable is found within the font, or ``None`` otherwise.
        N)tables
platformID	platEncID)selfr   r    subtables       r   getcmapztable__c_m_a_p.getcmap;   s<      	 H""j0X5G5G95T	  r   c                 \    |D ]'  \  }}| j                  ||      }||j                  c S  y)a(  Returns the 'best' Unicode cmap dictionary available in the font
        or ``None``, if no Unicode cmap subtable is available.

        By default it will search for the following (platformID, platEncID)
        pairs in order::

                        (3, 10), # Windows Unicode full repertoire
                        (0, 6),  # Unicode full repertoire (format 13 subtable)
                        (0, 4),  # Unicode 2.0 full repertoire
                        (3, 1),  # Windows Unicode BMP
                        (0, 3),  # Unicode 2.0 BMP
                        (0, 2),  # Unicode ISO/IEC 10646
                        (0, 1),  # Unicode 1.1
                        (0, 0)   # Unicode 1.0

        This particular order matches what HarfBuzz uses to choose what
        subtable to use by default. This order prefers the largest-repertoire
        subtable, and among those, prefers the Windows-platform over the
        Unicode-platform as the former has wider support.

        This order can be customized via the ``cmapPreferences`` argument.
        N)r#   r   )r!   cmapPreferencesr   r    cmapSubtables        r   getBestCmapztable__c_m_a_p.getBestCmapN   s@    F &5 	)!J	<<
I>L'#(((	) r   c                     i }| j                   D ]^  }|j                         s|j                  j                         D ].  \  }}|j	                  |t                     j                  |       0 ` |S )a"  Builds a reverse mapping dictionary

        Iterates over all Unicode cmap tables and returns a dictionary mapping
        glyphs to sets of codepoints, such as::

                {
                        'one': {0x31}
                        'A': {0x41,0x391}
                }

        The values are sets of Unicode codepoints because
        some fonts map different codepoints to the same glyph.
        For example, ``U+0041 LATIN CAPITAL LETTER A`` and ``U+0391
        GREEK CAPITAL LETTER ALPHA`` are sometimes the same glyph.
        )r   	isUnicoder   items
setdefaultsetadd)r!   resultr"   	codepointr   s        r   buildReversedztable__c_m_a_p.buildReversedw   sp       	BH!!#'/}}':':'< BOIt%%dCE266yAB	B r   c           	      X   t        j                  d|d d       \  }}t        |      | _        g x| _        }i }t        |      D ]E  }t        j                  d|d|dz  z   d|dz   dz  z          \  }}	}
t        |      t        |	      }	}t        j                  d||
|
dz          \  }}|dv r!t        j                  d||
|
dz          \  }}}n#|dv rt        j                  d	||
|
d
z          \  }}|st        j                  d||	||
       t        j                  |      }||_
        |	|_        |j                  ||
|
t        |      z    |       |
|v rd |_        |||
      j                  |_        n|||
<   |j                  |       H |j                   du r| j#                          y y )N>HH   >HHl   r	   )r5   
         z>HHL)   z>HL   zrcmap subtable is reported as having zero length: platformID %s, platEncID %s, format %s offset %s. Skipping table.F)structunpackinttableVersionr   rangelogerrorCmapSubtablenewSubtabler   r    decompileHeaderdatar   appendlazyensureDecompiled)r!   rE   ttFontr>   numSubTablesr   seenOffsetsir   r    offsetformatlengthreservedtables                  r   	decompileztable__c_m_a_p.decompile   s   %+]]5$r(%C"l-!!f|$ #	!A,2MMQQYa!eq[9-)J	6 %(
OS^	J#]]5$v
2KLNFF(+1==D&1*5,(& 4!'ud6FQJ6O!P		I  ,,V4E)E'EO !!$vV0D"EvN$!
#K$78==
&'F#MM% G#	!H ;;%!!#  r   c                 F    | j                   D ]  }|j                           y N)r   rH   )r!   recursests      r   rH   ztable__c_m_a_p.ensureDecompiled   s#     ++ 	"B!	"r   c                 <   | j                   j                          t        | j                         }dd|z  z   }t        j                  d| j
                  |      }d}i }i }| j                   D ]  }|j                  t        |j                              }	|	T|j                  |      }
|j                  |
      }	|	0|t        |      z   x}	x|t        |j                        <   ||
<   ||
z   }|t        j                  d|j                  |j                  |	      z   } ||z   S )Nr3   r5   r2   r   r4   )r   sortr   r;   packr>   getidr   compiler   r    )r!   rI   rJ   totalOffsetrE   	tableDataseendonerQ   rM   chunks              r   r\   ztable__c_m_a_p.compile   s#   4;;'!l**{{5$"3"3\B	 	  	 [[ 
	YEXXbn-F~f-%>BMPS!Q C F T"UZZ.1DK !*E 1I&++fe.>.>QWXXD
	Y ir   c                     |j                  d| j                         |j                          | j                  D ]  }|j	                  ||        y )Nr>   )version)	simpletagr>   newliner   toXML)r!   writerrI   rQ   s       r   rf   ztable__c_m_a_p.toXML   sF    1B1BC[[ 	(EKK'	(r   c                 ^   |dk(  rt        |d         | _        y |d d dk7  ry t        | d      sg | _        t        |dd        }t        j                  |      }t        |d         |_        t        |d         |_        |j                  ||||       | j                  j                  |       y )Nr>   rc   r7   cmap_format_r   r   r    )
r   r>   hasattrr   rB   rC   r   r    fromXMLrF   )r!   r   attrscontentrI   rN   rQ   s          r   rk   ztable__c_m_a_p.fromXML   s    >! (y)9 :D9&tX&DK$rs)$((0#E,$78"5#56dE7F35!r   N)))   r6   )r   r:   )r   r3   )rn   r	   )r   rn   )r      )r   r	   )r   r   F)__name__
__module____qualname____doc__r#   r'   r0   rR   rH   r\   rf   rk    r   r   r   r      s7    @*	
'R.*$X" 2("r   r   c                   p    e Zd ZdZed        Zed        Zd ZddZd Z	d Z
d Zdd
Zd Zd Zd Zd Zy	)rB   a  Base class for all cmap subtable formats.

    Subclasses which handle the individual subtable formats are named
    ``cmap_format_0``, ``cmap_format_2`` etc. Use :py:meth:`getSubtableClass`
    to retrieve the concrete subclass, or :py:meth:`newSubtable` to get a
    new subtable object for a given format.

    The object exposes a ``.cmap`` attribute, which contains a dictionary mapping
    character codepoints to glyph names.
    c                 6    t         j                  | t              S )z'Return the subtable class for a format.)cmap_classesrZ   cmap_format_unknown)rN   s    r   getSubtableClasszCmapSubtable.getSubtableClass   s     (;<<r   c                 <    t         j                  |       } ||       S )zBReturn a new instance of a subtable for the given format
        .)rB   rz   )rN   subtableClasss     r   rC   zCmapSubtable.newSubtable   s     %55f=V$$r   c                 X    || _         d | _        d | _        d | _        d | _        d | _        y rT   )rN   rE   rI   r   r    languager!   rN   s     r   __init__zCmapSubtable.__init__  s/    	 	r   c                 P    | j                   y | j                  d d        d | _         y rT   )rE   rR   )r!   rU   s     r   rH   zCmapSubtable.ensureDecompiled  s&     99tT"	r   c                     |d d dk(  rt        |      | j                  t        |      | j                          t        | |      S )Nro   __)AttributeErrorrE   rH   getattr)r!   attrs     r   __getattr__zCmapSubtable.__getattr__  sI    8t &&99 &&tT""r   c                    t        j                  d|d d       \  }}}t        |      |k(  sJ d|t        |      |fz         t        |      | _        t        |      | _        t        |      | _        |dd  | _        || _        y )N>HHHr:   Acorrupt cmap table format %d (data length: %d, header length: %d))	r;   r<   r   r=   rN   rO   r~   rE   rI   )r!   rE   rI   rN   rO   r~   s         r   rD   zCmapSubtable.decompileHeader$  s    #)==bq#B I	
NIR
 
	
 &k&kHH	r   c                    |j                  | j                  j                  d| j                  fd| j                  fd| j
                  fg       |j                          t        | j                  j                               }| j                  ||       |j                  | j                  j                         |j                          y Nr   r    r~   )begintag	__class__rq   r   r    r~   re   sortedr   r*   _writeCodesendtagr!   rg   rI   codess       r   rf   zCmapSubtable.toXML3  s    NN##t/dnn-T]]+	
 	tyy()'dnn--.r   Nc                 Z    t        | j                  | j                  | j                  |      S )a  Returns the Python encoding name for this cmap subtable based on its platformID,
        platEncID, and language.  If encoding for these values is not known, by default
        ``None`` is returned.  That can be overridden by passing a value to the ``default``
        argument.

        Note that if you want to choose a "preferred" cmap subtable, most of the time
        ``self.isUnicode()`` is what you want as that one only returns true for the modern,
        commonly used, Unicode-compatible triplets, not the legacy ones.
        )r   r   r    r~   )r!   defaults     r   r   zCmapSubtable.getEncodingB  s!     4??DNNDMM7SSr   c                 b    | j                   dk(  xs | j                   dk(  xr | j                  dv S )zEReturns true if the characters are interpreted as Unicode codepoints.r   rn   )r   r	   r6   r   r    r!   s    r   r)   zCmapSubtable.isUnicodeN  s2    !# 
OOq AT^^z%A	
r   c                 B    | j                   dk(  xr | j                  dk(  S )z=Returns true if the subtable is for the Symbol encoding (3,0)rn   r   r   r   s    r   isSymbolzCmapSubtable.isSymbolT  s    !#;!(;;r   c                     | j                         }|D ]L  \  }}|j                  dt        |      |       |r|j                  t        |          |j                          N y )Nmap)coder   )r)   rd   hexcommentr   re   )r!   r   rg   r)   r   r   s         r   r   zCmapSubtable._writeCodesX  sW    NN$	 	JD$UT>wt}-NN		r   c                     t        |t              st        S t        | dd       t        | dd       t        | dd       | j                  f}t        |dd       t        |dd       t        |dd       |j                  f}||k  S r   )
isinstancerB   NotImplementedr   __dict__)r!   other	selfTuple
otherTuples       r   __lt__zCmapSubtable.__lt__`  s    %.!! D,-D+t,D*d+MM	
	 E<.E;-E:t,NN	

 :%%r   rp   rT   )rq   rr   rs   rt   staticmethodrz   rC   r   rH   r   rD   rf   r   r)   r   r   r   ru   r   r   rB   rB      sb    	 = = % %
#
T
<&r   rB   c                       e Zd Zd Zd Zd Zy)cmap_format_0c                 ^   ||| j                  ||       n||J d       | j                  }d| j                  k(  sJ d       t        j                  d      }|j	                  | j                         t        t        t        |                  }t        | j                  ||      | _
        y )N#Need both data and ttFont arguments  z$Format 0 cmap subtable not 262 bytesB)rD   rE   rO   array	frombyteslistr?   r   r   rI   r   )r!   rE   rI   r   	charCodess        r   rR   zcmap_format_0.decompileu  s      2  v. 545/ II 	 dkk!I#II!{{3tyy!s4y)*	dkk9d;	r   c                    | j                   r/t        j                  ddd| j                        | j                   z   S | j                  }t        |j                               j                  t        d            sJ |j                  }t        d      D cg c]  }||v r |||         nd }}t        j                  d|      }t        j                  ddd| j                        |j                         z   }t        |      dk(  sJ |S c c}w )Nr   r   r      r   )rE   r;   rY   r~   r   r,   keysissubsetr?   
getGlyphIDr   tobytesr   )r!   rI   r   r   rL   	valueListr   rE   s           r   r\   zcmap_format_0.compile  s    99;;vq#t}}=		IIyy499;((s444&&
FKCjQAIZQ(1<Q	Q{{3	*{{61c4==9DLLNJ4yC Rs   D	c                     t        |d         | _        t        | d      si | _        | j                  }|D ]3  }t	        |t
              s|\  }}}|dk7  r |d   |t        |d         <   5 y Nr~   r   r   r   r   r   r~   rj   r   r   tupler!   r   rl   rm   rI   r   elements          r   rk   zcmap_format_0.fromXML  sv     z!23tV$DIyy 	:Ggu-#* D%u},1&MD%-()	:r   Nrq   rr   rs   rR   r\   rk   ru   r   r   r   r   t  s    <$:r   r   z>HHhHc                       e Zd Zd Zy)	SubHeaderc                 J    d | _         d | _        d | _        d | _        g | _        y rT   )	firstCode
entryCountidDeltaidRangeOffsetglyphIndexArrayr   s    r   r   zSubHeader.__init__  s'    !!r   N)rq   rr   rs   r   ru   r   r   r   r     s    "r   r   c                   $    e Zd Zd Zd Zd Zd Zy)cmap_format_2c                 P   d|_         |j                  d   }|j                  D ]  }|dk7  s	||k  s|} |dkD  rl|dkD  rd|z
   dz
  |_         n
|dz
  |_         |j                   }t        |j                        D ])  }|j                  |   }|dkD  s||z
  |j                  |<   + y y )Nr   r	   i     )r   r   r?   r   )r!   	subHeaderminGIr   r   rL   s         r   
setIDDeltazcmap_format_2.setIDDelta  s    	))!,,, 	CqsU{	 19v~&-o$6$:	!$)AI	!''G9//0 A//2736=I--a0A r   c                    ||| j                  ||       n||J d       | j                  }g }d}t        j                  d      }|j                  |d d        |dd  }t        j
                  dk7  r|j                          |D cg c]  }|dz  	 }}t        |      }g }d}t        |dz         D ]  }	t               }
t        j                  t        |||dz          \  |
_        |
_        |
_        |
_        |dz  }||
j                   z   dz
  }t        j                  d      }|j                  ||||
j                  dz  z           t        j
                  dk7  r|j                          ||
_        |j%                  |
        d	| _        i }d}t        d
      D ]  }||   }||   }
|dk(  rk||
j                  k  s||
j                  |
j                  z   k\  r>|}||
j                  z
  }|
j"                  |   }|dk7  r||
j                  z   dz  }nw|||<   }|
j                  s|d
z  |
j                  z   }t        |
j                        D ]4  }||z   }|
j"                  |   }|dk7  r||
j                  z   dz  }n0|||<   6  t'        |j)                               }t'        |j+                               }t-        | j.                  ||      | _        y c c}w )Nr   r   Hi   bigr5   r	   ro   r   r   r   )rD   rE   r   r   sys	byteorderbyteswapmaxr?   r   r;   r<   subHeaderFormatr   r   r   r   r   rF   r   valuesr   r   rI   r   )r!   rE   rI   subHeaderKeysmaxSubHeaderindexallKeyskeysubHeaderListposrL   r   	giDataPosgiListr   notdefGI	firstBytesubHeadindexcharCodeoffsetIndexgicharCodeOffsetr   r   s                          r   rR   zcmap_format_2.decompile  s     2  v. 545/ II 	 ++c"$t*%CDz==E!-45c55. (1,- 	,A!I otC#'/BC#$!'1HCi5559I[[%FT)i):N:NQR:R.RST}}%!(.I%  +	,j 	s 	,I(3L%l3Iq 	 3 33!4!4y7K7K!KK(H"+i.A.A"AK"22;?BQw 9#4#44? !#X''%._y7J7J%JN',Y-A-A'B ,#1K#?&66{C7"$y'8'8"8G!CB$)+X,)	,@ DKKM"%	dkk9d;	A 6s   Kc           
         | j                   rCt        j                  d| j                  | j                  | j
                        | j                   z   S d}d}t        | j                  j                               }|D cg c]  }|d   	 }}|D cg c]  }|d   	 }}|j                         }	 |D 	cg c]  }	||	   	 }
}	t        d	      D cg c]  }| }}g }|d   }|d
kD  r7t               }d|_        d|_        d|_        d|_        |j                  |       d}t)        ||
      }|D ]^  \  }}|dk(  r|dz	  }|d
z  }||k7  r|dkD  rm| j+                         |dk(  r.t        |j"                        D ]  }|j                   |z   }d||<    |j"                  t-        |j.                        k(  sJ d       t               }||_        d|_        |j.                  j                  |       |j                  |       t-        |      dz
  ||<   |}|j                   |j"                  z   z
  }t        |      D ]  }|j.                  j                  |        |j.                  j                  |       |j"                  |z   dz   |_        a | j+                         t               }d|_        d|_        d|_        d|_        |j                  |       t-        |      dz
  }t        d	      D ]  }||   |k(  s|||<    t-        |      dz
  dz  dz   }t-        |      dz
  }t        |      D ]  }||   }d|_        t        |      D ]C  }||   }|j.                  |j.                  k(  s"|j&                  ||z
  dz  z
  |_        g |_         n |j&                  dk(  r||_        |dz
  |j"                  dz  z   }|dz
  } ddt-        |      z  z   }|d d D ]  }|t-        |j.                        dz  z   } t        j                  dd|| j
                        g}|D ]*  }|j                  t        j                  d|dz               , |D ]V  }|j                  t        j                  t0        |j                   |j"                  |j$                  |j&                               X |d d D ]8  }|j.                  D ]'  }|j                  t        j                  d|             ) : t3        |      } t-        |       |k(  s+J dt5        t-        |             z   dz   t5        |      z          | S c c}w c c}w c c}	w # t        $ r |j                  d      }	 |D 	cg c]  }	||	   	 nc c}	w }
}	ny# t        $ rm g }
|D ]c  }		 ||	   }nI# t        $ r= 	 |	d d dk(  rt        |	dd        }n|j                  |	      }n#  t        |	      xY wY nw xY w|
j                  |       e Y nw xY wY *w xY wc c}w )Nr   r   r	   Trebuildrn   r   r      r5   zCError - subhead entry count does not match len of glyphID subrange.ro   i  z>Hz?Error: cmap format 2 is not same length as calculated! actual: z calc : )rE   r;   rY   rN   rO   r~   r   r   r*   getReverseGlyphMapKeyErrorr=   r   rF   r?   r   r   r   r   r   r   r   r   r   r   r   str)!r!   rI   kEmptyTwoCharCodeRanger   r*   itemr   namesnameMapr   r   r   xr   r   r   r   lastFirstByte	firstbyte
secondByteindexcodeDiffrL   emptySubheadIndexr   subheadRangeLenjprevSubheadrO   subheaddataListr   rE   s!                                    r   r\   zcmap_format_2.compileC  s   99FDKKdmmLtyyX "$tyy())./T!W/	/%*+Ta++++-	%.34dGDM4D4< -2#J
'("
 
 
 Q<c>!I"#I#$I  !I&'I#  +It$" %	KMHcax AI!F*J ]* 2%OOI. %)%*9+?+?%@ 8E'0':':U'BH67M(38 %//3!114  ]\]  &K	&0	#'(	$))005$$Y/+.}+=+Ai( ) &)<)<y?S?S)STx ?A--44X>?))005'0';';h'F'J	$K%	KP 		" K		 		"#	Y'.23Z 	9EU#'=='8e$	9 " " 	 ?+ 	2E%e,I&'I#5\ 	+A.//93L3LL $11UQY!OC + 13I-	 &&!+*7	'!A%((1,!- !. 1'	2. a#m,,, 	 %Sb) 	GW44599 	 KK64==AB" 	:EOOFKKeai89	:$ 		GOO#%%&&OO))		 %Sb) 	7G-- 7D" 567	7 "4yF" 	
M#d)n &k	
" Q 0+ 5 	%///=G%278$888 %! %D	1%dm# 11#BQx50&)$qr(m&,&7&7&=1"*4.01 KK$%%		%8
s   ;T(T-,T7 0T2<T7 	W,2T7 7W)U,U$#U,+W),W"=VW"	W		(V65W		6WW		W"W			W"W)!W""W)(W)c                     t        |d         | _        t        | d      si | _        | j                  }|D ]3  }t	        |t
              s|\  }}}|dk7  r |d   |t        |d         <   5 y r   r   r   s          r   rk   zcmap_format_2.fromXML  v     z!23tV$DIyy 	:Ggu-#* D%u},1&MD%-()	:r   N)rq   rr   rs   r   rR   r\   rk   ru   r   r   r   r     s    A8u<nqf:r   r   z>7Hc                    | |k(  rg |gfS ||    }| }d }d }g }t        | dz   |dz         D ]5  }||   }	|	dz
  |k(  r	||sd}|}n|rd}|j                  ||f       d }|	}|}7 |r|j                  ||f       ||k(  sJ g }
|D ]?  \  }}|| k(  r||k(  r n0|| k(  s||k(  rd}nd}||z
  dz   |kD  s-|
j                  ||f       A |
}|sg |gfS |d   d   | k7  r|j                  d| |d   d   dz
  f       |d   d   |k7  r|j                  |d   d   dz   |f       d}|t        |      k  rY||dz
     d   dz   ||   d   k7  r.|j                  |||dz
     d   dz   ||   d   dz
  f       |dz   }|dz   }|t        |      k  rYg }g }|D ]'  \  }}|j                  |       |j                  |       ) |j	                  d       t        |      dz   t        |      k(  sJ ||fS )Nr	   r   r3   r5   r   )r?   rF   insertr   pop)	startCodeendCoder   lastIDlastCodeinOrderorderedBegin	subRangesr   glyphID	newRangesbe	thresholdrL   startends                    r   
splitRanger    s   
 GG9})_FHGLI i!mWq[1 t*Q;& g'  ,!9:#  ,12w
 I %1	>a7l	>Q'\IIEAI"aV$% IG9}|A)#Y	!Q!(;<=}Q7")B-*Q.89 	
A
c)n
QUA"il1o5Q1q5!1!!4q!8)A,q/A:M NOAAE	 c)n
 E
C 1Q

1 
IIaLu:>SX%%%#:r   c                       e Zd Zd Zd Zd Zy)cmap_format_4c                    ||| j                  ||       n||J d       | j                  }t        j                  d|d d       \  }}}}|dd  }|dz  }t	        j                  d      }|j                  |       d x| _        }t        j                  dk7  r|j                          |d | }	||dz   d  }|d | }
||d  }|d | }||d  }|d | }||d  }t        |      }g }g }t        t        |
      dz
        D ]  }|
|   }||   }||   }|dz  |z
  |z   t        |      z
  }t        t        |
|   |	|   dz               }|j                  |       |dk(  r&|j                  |D cg c]
  }||z   d	z   c}       |D ]@  }||z   }||k  sJ d
|||fz         ||   dk7  r	||   |z   }nd}|j                  |d	z         B  t        | j                  ||      | _        y c c}w )Nr   z>4Hr5   ro   r   r   r	   r     zIn format 4 cmap, range (%d), the calculated index (%d) into the glyph index array is not less than the length of the array (%d) !)rD   rE   r;   r<   r   r   r   r   r   r   r?   r   extendrF   r   rI   r   )r!   rE   rI   
segCountX2searchRangeentrySelector
rangeShiftsegCountallCodesr  r  r   r   r   
lenGIArrayr   r   rL   r  deltarangeOffsetpartialrangeCharCodesr   r   r  s                             r   rR   zcmap_format_4.decompile_  s     2  v. 545/ II 	 @F}}48@
<[- ABx?;;s#4 	D==E! 9H%HqLN+Yh'	HI&9H%HI& (+"89-)
 	s9~)* 	2AaLEAJE'*K!Q&.2S5GGG!%	!gaj1n"EFN^,aAOPXh&&0P !/ 
2H$w.E :-  ]eZ01- 'u-2"1%"85"@"#KK& 01
2	22 dkk9d;	 Qs   &G!
c                 	   | j                   rCt        j                  d| j                  | j                  | j
                        | j                   z   S t        | j                  j                               }|sdg}dg}n(|j                          |D cg c]  }| j                  |    }}|j                         }	 |D cg c]  }||   	 }	}i }t        ||	      D ]
  \  }}
|
||<    |d   }g }|g}|dd  D ]U  }||dz   k(  r|}t!        |d	   ||      \  }}|j#                  |       |j#                  |       |j                  |       |}W t!        |d	   ||      \  }}|j#                  |       |j#                  |       |j                  d       |j                  d       g }g }g }t%        t'        |      dz
        D ]  }g }t%        ||   ||   dz         D ]  }|j                  |           |t        t%        |d   |d   t'        |      z               k(  r/|j                  |d   ||   z
  d
z         |j                  d       |j                  d       |j                  dt'        |      t'        |      z   |z
  z         |j#                  |        |j                  d       |j                  d       t'        |      }|dz  }t)        |d      \  }}}t+        j*                  d|dgz   |z         }t+        j*                  d|      }t+        j*                  d||z         }t,        j.                  dk7  r|j1                          t,        j.                  dk7  r|j1                          t,        j.                  dk7  r|j1                          |j3                         |j3                         z   |j3                         z   }t        j4                  t6              t'        |      z   }t        j                  t6        | j                  || j
                  ||||      }||z   S c c}w c c}w # t        $ r |j                  d      }	 |D cg c]  }||   	 nc c}w }	}ny# t        $ rm g }	|D ]c  }	 ||   }
nI# t        $ r= 	 |d d dk(  rt        |dd        }
n|j                  |      }
n#  t        |      xY wY nw xY w|	j                  |
       e Y nw xY wY w xY w)Nr   r  Tr   rn   r   r   r	   r   r   ro   r   r   )rE   r;   rY   rN   rO   r~   r   r   r   rX   r   r   r=   r   rF   r   r  r  r?   r   r   r   r   r   r   r   calcsizecmap_format_4_format) r!   rI   r   r  r  r   r   r   r   r   r   r   r  r   r  r  r   r   r   rL   indicesr  r  r  r  r  charCodeArrayidDeltaArray	restArrayrE   rO   headers                                    r   r\   zcmap_format_4.compile  s   99FDKKdmmLtyyX )*	IhGNN1:;TYYt_;E;//1G)278$88* D D1 !	c T
! !|HG!
I% 
$ x!|+'H'	"xF
s  's#  *#
$ $IbM8TBJE3U#NN3V$NN6" s7|a'( 
	0AG!)A,
Q? /tH~./$uWQZc'l1JKLL
Yq\ 9WDE$$Q'q!$$Q#g,_9M*MPQ*Q%RS&&w/
	0 	qQ w<\
1?!1L.]JCA3)BC{{30KK]_%DE	==E!""$==E!!!#==E! $$&)=)=)??)BSBSBUU!56TB KKMM	
 }C < 9 ) 33D3A)6;<dGDM<<D< )D % )	5")$-C' 55#'8u#4*-d12h-C*0*;*;D*AC5&.tn 45 C())	)s   P%:P/ >P*
P/ *P/ /S!Q$QQ$#S!$S5Q;:S;	S	(R.-S	.R;;S	>S S	SS!SS! S!c                     t        |d         | _        t        | d      si | _        | j                  }|D ]9  }t	        |t
              s|\  }}}	|dk7  rJ d       |d   |t        |d         <   ; y )Nr~   r   r   z%Unrecognized keyword in cmap subtabler   r   r   )
r!   r   rl   rm   rI   r   r   r   attrsMapdummyContents
             r   rk   zcmap_format_4.fromXML  s     z!23tV$DIyy 	@Ggu-.5+GX|%AAAq/7/?D(6*+,	@r   Nr   ru   r   r   r  r  ^  s    A<Fm^@r   r  c                       e Zd Zd Zd Zd Zy)cmap_format_6c           	         ||| j                  ||       n||J d       | j                  }t        j                  d|d d       \  }}t	        |      }|dd  }t        j
                  d      }|j                  |d dt	        |      z          t        j                  dk7  r|j                          d x| _        }t        t        ||t        |      z               }t        | j                  ||      | _        y )Nr   r2   r3   r   ro   r   )rD   rE   r;   r<   r=   r   r   r   r   r   r   r?   r   r   rI   r   )r!   rE   rI   r   r   r   r   s          r   rR   zcmap_format_6.decompile!  s      2  v. 545/ II 	 !'eT"1X >	:	N	ABx{{3t1a#j/123==E!MMO	Dy)c$i*?@A	dkk9d;	r   c           
         | j                   rCt        j                  d| j                  | j                  | j
                        | j                   z   S | j                  }t        |j                               }|rt        t        |d   |d   dz               }|d   }|D cg c]  }||v r|j                  ||         nd }}t        j                  d|      }t        j                  dk7  r|j                          |j!                         }nd}d}t        j                  dd	t#        |      d
z   | j
                  |t#        |            }	|	|z   S c c}w )Nr   r   r   r	   r   r   r   z>HHHHHr:   r6   )rE   r;   rY   rN   rO   r~   r   r   r   r   r?   r   r   r   r   r   r   r   )
r!   rI   r   r   r   r   r   r   rE   r+  s
             r   r\   zcmap_format_6.compile;  s%   99FDKKdmmLtyyX yytyy{#uQxrQ78EaIQVIM!!$t*-1DI  ;;sI.D}}%<<>DDIaTR	3u:
 }s   !Ec                     t        |d         | _        t        | d      si | _        | j                  }|D ]3  }t	        |t
              s|\  }}}|dk7  r |d   |t        |d         <   5 y r   r   r   s          r   rk   zcmap_format_6.fromXMLT  r  r   Nr   ru   r   r   r0  r0     s    <42:r   r0  c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)cmap_format_12_or_13c                 <    || _         d| _        d | _        d | _        y r   )rN   rP   rE   rI   r   s     r   r   zcmap_format_12_or_13.__init__d  s    	r   c                 "   t        j                  d|d d       \  }}}}}t        |      d|dz  z   cxk(  r|k(  s"n J d| j                  t        |      |fz         || _        || _        || _        || _        || _        |dd  | _        || _	        y )N>HHLLL   r7   r   )
r;   r<   r   rN   rP   rO   r~   nGroupsrE   rI   )r!   rE   rI   rN   rP   rO   r~   r:  s           r   rD   z$cmap_format_12_or_13.decompileHeaderj  s    6<mmHdSVTVi6X3&(GI"w|+88	
NKKIR
 
	
8   I	r   c           
         ||| j                  ||       n||J d       | j                  }g }g }d}t        | j                        D ]w  }t	        j
                  d|||dz          \  }}}	|dz  }d|z   |z
  }
|j                  t        t        ||dz                      |j                  | j                  |	|
             y d x| _        }t        | j                  ||      | _        y )Nr   r   >LLLr7   r	   )rD   rE   r?   r:  r;   r<   r  r   _computeGIDsr   rI   r   )r!   rE   rI   r   r   r   rL   startCharCodeendCharCoder  lenGroups              r   rR   zcmap_format_12_or_13.decompile{  s     2  v. 545/ II 	 	t||$ 	>A28--S38,3/M; 2IC;6HT%{Q"GHIKK))'8<=	>  	Ddkk9d;	r   c           	         | j                   rYt        j                  d| j                  | j                  | j
                  | j                  | j                        | j                   z   S t        | j                  j                               }t        | j                  j                               }|j                         }	 |D cg c]  }||   	 }}i }t#        ||      D ]
  \  }	}|||	<    |j%                          d}
|d   }||   }|| j&                  z
  }|dz
  }d}g }t)        |      }t+        |      D ]T  }
||
   }||   }| j-                  ||||      s0|j!                  t        j                  d|||             |}|}|dz   }|}|}V |j!                  t        j                  d|||             |dz   }t/        |      }t)        |      d	z   }t)        |      |d
z  cxk(  r	|d	z
  k(  sJ  J t        j                  d| j                  | j                  || j                  |      |z   S c c}w # t        $ r |j                  d      }	 |D cg c]  }||   	 nc c}w }}ny# t        $ rm g }|D ]c  }	 ||   }nI# t        $ r= 	 |d d dk(  rt        |dd        }n|j                  |      }n#  t        |      xY wY nw xY w|j!                  |       e Y nw xY wY w xY w)Nr8  Tr   rn   r   r   r	   r<  r9  r7   )rE   r;   rY   rN   rP   rO   r~   r:  r   r   r   r   r   r   r=   r   rF   r   rX   _format_stepr   r?   _IsInSameRunr   )r!   rI   r   r   r   r   r   r   r   r   r   r>  startGlyphIDlastGlyphIDlastCharCoder:  r   maxIndexr   r  rE   lengthSubtables                         r   r\   zcmap_format_12_or_13.compile  s   99KKMMKKMMLL ))
 )*	TYY%%'(++-	%.34dGDM4D4, Y- 	ID#DJ	 	!!M*"T%6%66$q(y>8_ 	$E 'H8nG$$Wk8\RKK|\R !)&!A+!K#L	$ 	FM<VWA+"TR4yWr\C~/BCCCCCKK 
	
i 5 	%///=G%278$888 %! %D	1%dm# 11#BQx50&)$qr(m&,&7&7&=1"*4.01 KK$%%		%s   =H/ H*H/ *H/ /K!I$II$#K!$K5I;:K;	K	(J.-K	.J;;K	>K K	KK!KK! K!c                    |j                  | j                  j                  d| j                  fd| j                  fd| j
                  fd| j                  fd| j                  fd| j                  fd| j                  fg       |j                          t        | j                  j                               }| j                  ||       |j                  | j                  j                         |j                          y )Nr   r    rN   rP   rO   r~   r:  )r   r   rq   r   r    rN   rP   rO   r~   r:  re   r   r   r*   r   r   r   s       r   rf   zcmap_format_12_or_13.toXML  s    NN##t/dnn-4;;'T]]+4;;'T]]+DLL)	
 	tyy()'dnn--.r   c                 p   t        |d         | _        t        |d         | _        t        |d         | _        t        |d         | _        t        |d         | _        t        | d      si | _        | j                  }|D ]3  }t        |t              s|\  }}}|dk7  r |d   |t        |d	         <   5 y )
NrN   rP   rO   r~   r:  r   r   r   r   )
r   rN   rP   rO   r~   r:  rj   r   r   r   r   s          r   rk   zcmap_format_12_or_13.fromXML  s    uX/ z!23uX/ z!23i 01tV$DIyy 	:Ggu-#* D%u},1&MD%-()	:r   N)	rq   rr   rs   r   rD   rR   r\   rf   rk   ru   r   r   r5  r5  c  s"    "<6O
b&:r   r5  c                   $    e Zd ZdZddZd Zd Zy)cmap_format_12r	   c                 0    t         j                  | |       y rT   r5  r   r   s     r   r   zcmap_format_12.__init__      %%dF3r   c                 2    t        t        |||z               S rT   )r   r?   r!   startingGlyphnumberOfGlyphss      r   r=  zcmap_format_12._computeGIDs  s    E-)GHIIr   c                 &    |d|z   k(  xr |d|z   k(  S Nr	   ru   r!   r  rE  r   rF  s        r   rC  zcmap_format_12._IsInSameRun  s     1{?*NQ=M1MNr   N)r7   rq   rr   rs   rB  r   r=  rC  ru   r   r   rL  rL    s    L4JOr   rL  c                   $    e Zd ZdZddZd Zd Zy)cmap_format_13r   c                 0    t         j                  | |       y rT   rN  r   s     r   r   zcmap_format_13.__init__  rO  r   c                     |g|z  S rT   ru   rQ  s      r   r=  zcmap_format_13._computeGIDs   s    //r   c                      ||k(  xr |d|z   k(  S rU  ru   rV  s        r   rC  zcmap_format_13._IsInSameRun#  s    ;&JX\9I-IJr   N)r8   rW  ru   r   r   rY  rY    s    L40Kr   rY  c                 @    d| z   }t        j                  d|      \  }|S )N    >L)r;   r<   )threeByteStringrE   vals      r   cvtToUVSrb  '  s#    ?"D]]4&FSJr   c                 X    d| cxk  rdk  sJ  J t        j                  d|       }|dd  S )Nr   i   r_  r	   )r;   rY   )ra  fourByteStrings     r   
cvtFromUVSre  -  s8    i[[s+N!"r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)cmap_format_14c                     t        j                  d|d d       \  }}}|dd  | _        || _        || _        || _        d| _        y )N>HLLr6   r   )r;   r<   rE   rO   numVarSelectorRecordsrI   r~   )r!   rE   rI   rN   rO   rj  s         r   rD   zcmap_format_14.decompileHeader4  sJ    06fd3Bi0P--I	%:"r   c           	      :   ||| j                  ||       n||J d       | j                  }i | _        i }d}t        | j                        D ]  }t        j                  d|||dz          \  }}}|dz  }t        |      }	|r|dz
  }
t        j                  d||
|
dz          \  }|
dz  }
t        |      D ]u  }t        j                  d||
|
dz          \  }}|
dz  }
t        |      }|d	z   }t        t        |||z               }d g|z  }t        ||      }	 ||	   j                  |       w |s|dz
  }
t        j                  d||
|
dz          \  }|
dz  }
g }t        |      D ]_  }t        j                  d
||
|
dz          \  }}|
dz  }
t        |      }| j                  j                  |      }|j                  ||f       a 	 ||	   j                  |        || _        y # t        $ r t        |      ||	<   Y Lw xY w# t        $ r	 |||	<   Y w xY w)Nr   r   >3sLL   r6   r_  r3   >3sBr	   >3sH   )rD   rE   r   r?   rj  r;   r<   rb  r   r   r  r   rI   getGlyphNamerF   uvsDict)r!   rE   rI   rr  	recOffsetnuvsdefOVSOffsetnonDefUVSOffsetvarUVSstartOffset	numValuesruvaddtlCntfirstBaseUVcnt
baseUVList	glyphListlocalUVListnumRecsr   	glyphNames                          r   rR   zcmap_format_14.decompile<  s{    2  v. 545/yy  		 	t112 '	2A17i)b.92.C OIc]F*R/%}}T4kTUo3VWq y) <A#)==[;? C$LB  1$K"*2,K"Q,C!%eKs9J&K!LJ!%I"%j)"<K<..{;< -2#]]4kKRSO1TU
q  w 8A$mmFD{UV4WXGB1$K!"B $ 8 8 =I&&I782FO**;7K'	2R ' $ <*.{*;<    2&1GFO2s$   G*H*HHHHc                 L   |j                  | j                  j                  d| j                  fd| j                  fg       |j                          | j                  }t        |j                               }|D ]u  }||   }|j                  d        |D ]V  \  }}dt        |      fdt        |      fg}	||	j                  d|f       |j                  d|	       |j                          X w |j                  | j                  j                         |j                          y )	Nr   r    c                 "    | d   d u| d   | d   fS )Nr	   r   ru   )r   s    r   <lambda>z&cmap_format_14.toXML.<locals>.<lambda>  s    $q'*=tAwQ)P r   )r   r|  ru  r   r   )r   r   rq   r   r    re   rr  r   r   rX   r   rF   rd   r   )
r!   rg   rI   rr  uvsListru  uvListr|  gnamerl   s
             r   rf   zcmap_format_14.toXMLu  s   NN##t/dnn-	
 	,,( 	!CS\FKKPKQ# !	EB5#c(*;<$LL&%1  . !	! 	dnn--.r   c                    d| _         t        | d      si | _        t        | d      si | _        | j                  }d }|D ]  }t	        |t
              s|\  }}}|dk7  r t        |d         }t        |d         }	|j                  d      }
|
dk(  r|d|j                         v }|sd }
	 |   j                  |	|
f        y # t        $ r |	|
fg|<   Y w xY w)	Nr   r   rr  r   ru  r|  r   None)r~   rj   r   rr  r   r   r   rZ   getGlyphOrderrF   r   )r!   r   rl   rm   rI   rr  _hasGlyphNamedNoner   ru  r|  r  s              r   rk   zcmap_format_14.fromXML  s   tV$ I tY'DLllG
 " 	-Ggu-#* D%u}5<(C%+&BIIf%E%-)/63G3G3I)I&) E-##RK0	-   -!#U}-s   )CCCc                 &   | j                   rCt        j                  d| j                  | j                  | j
                        | j                   z   S | j                  }t        |j                               }t        |      | _        d| j
                  dz  z   }g }g }|D ]  }||   }|D 	cg c]
  }	|	d   	|	 }
}	|
r|
D 	cg c]  }	|	d   	 }
}	|}|
j                          |
d   }d}g }|
D ]H  }|dz  }||z   |k7  st        j                  dt        |      |dz
        }|}|j                  |       d}J t        j                  dt        |      |      }|j                  |       t        |      }|j                  t        j                  d|             |j                  |       |d	|d	z  z   z  }nd}|D 	cg c]
  }	|	d   	|	 }}	|r|}|j                          t        |      }|j                  t        j                  d|             |d	|d
z  z   z  }|D ]G  \  }}|j                  |      }t        j                  dt        |      |      }|j                  |       I nd}t        j                  dt        |      ||      }|j                  |        t        |      t        |      z   }dt        |      z   | _        t        j                  d| j                  | j                  | j
                        }||z   S c c}	w c c}	w c c}	w )Nri  r6   rm  r	   r   r   rn  r_  r3   rp  ro  rl  )rE   r;   rY   rN   rO   rj  rr  r   r   r   rX   re  rF   r  r   r   )r!   rI   rr  r  rM   rE   varSelectorRecordsru  	entryListentrydefListrv  lastUVr  defRecsdefEntryrec
numDefRecsndefListrw  numNonDefRecsr|  r  r   ndrecvrec
headerdatas                              r   r\   zcmap_format_14.compile  s   99DKKd6P6P )) ,,(%(\"++b00 	  .	,CI*3HuQx7GuHGH1895899%  '  H1HC1$kk&*V2DcAgN!)s+  kk&*V*<cBs# \
FKKj9:G$!j1n,, +4M%a8LMHM"( #HFKKm<=!ma///!) 'IB ++E2C"KK
2DEKK&'
 #$;;w
3WD%%d+].	,` +,y>3t9n[[DKKd.H.H

 D  g I92 Ns   *
L5LL	;
LLN)rq   rr   rs   rD   rR   rf   rk   r\   ru   r   r   rg  rg  3  s    7r. -DG!r   rg  c                   *    e Zd Zd Zd Zd Zd Zd Zy)ry   c                 R   | j                   j                  d d t        | j                        z   }|j	                  |d| j
                  fd| j                  fg       |j                          |j                  | j                         |j                  |       |j                          y )Nr7   r   r    )r   rq   r   rN   r   r   r    re   dumphexrE   r   )r!   rg   rI   cmapNames       r   rf   zcmap_format_unknown.toXML  s    >>**3B/#dkk2BBt/dnn-	
 	tyy!hr   c                 2    t        |      | _        i | _        y rT   )r   rE   r   )r!   r   rl   rm   rI   s        r   rk   zcmap_format_unknown.fromXML  s    G$		r   c                      d| _         || _        y r   )r~   rE   r!   rE   rI   s      r   rD   z#cmap_format_unknown.decompileHeader  s    	r   c                 H    ||| j                  ||       y ||J d       y )Nr   )rD   r  s      r   rR   zcmap_format_unknown.decompile  s<      2  v. 545/r   c                 4    | j                   r| j                   S y rT   )rE   )r!   rI   s     r   r\   zcmap_format_unknown.compile  s    9999r   N)rq   rr   rs   rf   rk   rD   rR   r\   ru   r   r   ry   ry     s    5r   ry   )r   ro   r3   r:   r7   r8   r9   )'fontTools.misc.textToolsr   r   r   fontTools.misc.encodingToolsr   fontTools.ttLibr   fontTools.unicoder    r
   r   r;   r   logging	getLoggerrq   r@   r   r   objectrB   r   r   r   r   r&  r  r  r0  r5  rL  rY  rb  re  rg  ry   rx   ru   r   r   <module>r     s/   A A 4 * %  
    g!R"\.. R"jB&6 B&J,:L ,:^ " "S:L S:l
  L^@L @D@:L @:Fg:< g:T
O) 
O
K) 
KB!\ B!J%, %R r   