
    Ph7                        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           jC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\fontTools/ttLib/tables/_c_m_a_p.py	_make_mapr      s`    u:T"""&&t,JDudJ7c4!8T
 8 K    c                   L    e Zd ZdZd Z	 ddZ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]

    See also https://learn.microsoft.com/en-us/typography/opentype/spec/cmap
    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=   s7     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.getBestCmapP   s;    F &5!J	<<
I>L'#((( &5 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.buildReversedy   sb      H!!#'/}}':':'<OIt%%dCE266yA (= $ r   c                     i }| j                   D ]Q  }|j                         s|j                  j                         D ]!  \  }}||v rt	        ||   |      ||<   |||<   # S |S N)r   r)   r   r*   minr.   s        r   buildReversedMinztable__c_m_a_p.buildReversedMin   sg    H!!#'/}}':':'<OItv~'*6$<'Ct'0t	 (= $ 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	   )r:   
         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!   rJ   ttFontrC   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 r3   )r   rM   )r!   recursests      r   rM   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 )Nr8   r:   r7   r   r9   )r   sortr   r@   packrC   getidr   compiler   r    )r!   rN   rO   totalOffsetrJ   	tableDataseendonerV   rR   chunks              r   r`   ztable__c_m_a_p.compile   s   4;;'!l**{{5$"3"3\B	 	  	 [[EXXbn-F~f-%>BMPS!Q C F T"UZZ.1DK !*E 1I&++fe.>.>QWXXD ! ir   c                     |j                  d| j                         |j                          | j                  D ]  }|j	                  ||        y )NrC   )version)	simpletagrC   newliner   toXML)r!   writerrN   rV   s       r   rj   ztable__c_m_a_p.toXML   sA    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 )NrC   rg   r<   cmap_format_r   r   r    )
r   rC   hasattrr   rG   rH   r   r    fromXMLrK   )r!   r   attrscontentrN   rS   rV   s          r   ro   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)))   r;   )r   r?   )r   r8   )rr   r	   )r   rr   )r      )r   r	   )r   r   F)__name__
__module____qualname____doc__r#   r'   r1   r5   rW   rM   r`   rj   ro    r   r   r   r      s<     D*	
'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	)rG   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_classesr^   cmap_format_unknown)rS   s    r   getSubtableClasszCmapSubtable.getSubtableClass  s     (;<<r   c                 <    t         j                  |       } ||       S )zBReturn a new instance of a subtable for the given format
        .)rG   r~   )rS   subtableClasss     r   rH   zCmapSubtable.newSubtable  s     %55f=V$$r   c                 X    || _         d | _        d | _        d | _        d | _        d | _        y r3   )rS   rJ   rN   r   r    languager!   rS   s     r   __init__zCmapSubtable.__init__  s/    	 	r   c                 P    | j                   y | j                  d d        d | _         y r3   )rJ   rW   )r!   rY   s     r   rM   zCmapSubtable.ensureDecompiled  s&     99tT"	r   c                     |d d dk(  rt        |      | j                  t        |      | j                          t        | |      S )Nrs   __)AttributeErrorrJ   rM   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@   rA   r   rB   rS   rT   r   rJ   rN   )r!   rJ   rN   rS   rT   r   s         r   rI   zCmapSubtable.decompileHeader1  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__ru   r   r    r   ri   sortedr   r*   _writeCodesendtagr!   rk   rN   codess       r   rj   zCmapSubtable.toXML@  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.getEncodingO  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   rr   )r   r	   r;   r   r    r!   s    r   r)   zCmapSubtable.isUnicode[  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)rr   r   r   r   s    r   isSymbolzCmapSubtable.isSymbola  s    !#;!(;;r   c                     | j                         }|D ]L  \  }}|j                  dt        |      |       |r|j                  t        |          |j                          N y )Nmap)coder   )r)   rh   hexcommentr   ri   )r!   r   rk   r)   r   r   s         r   r   zCmapSubtable._writeCodese  sR    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   )
isinstancerG   NotImplementedr   __dict__)r!   other	selfTuple
otherTuples       r   __lt__zCmapSubtable.__lt__m  s    %.!! D,-D+t,D*d+MM	
	 E<.E;-E:t,NN	

 :%%r   rt   r3   )ru   rv   rw   rx   staticmethodr~   rH   r   rM   r   rI   rj   r   r)   r   r   r   ry   r   r   rG   rG      sb    	 = = % %
#
T
<&r   rG   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)rI   rJ   rT   array	frombyteslistrD   r   r   rN   r   )r!   rJ   rN   r   	charCodess        r   rW   zcmap_format_0.decompile  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   )rJ   r@   r]   r   r   r,   keysissubsetrD   
getGlyphIDr   tobytesr   )r!   rN   r   r   rQ   	valueListr   rJ   s           r   r`   zcmap_format_0.compile  s    99;;vq#t}}=		IIyy499;((s444&&
FKCjQjAIZQ(1<j	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   rn   r   r   tupler!   r   rp   rq   rN   r   elements          r   ro   zcmap_format_0.fromXML  sq     z!23tV$DIyyGgu-#* D%u},1&MD%-() r   Nru   rv   rw   rW   r`   ro   ry   r   r   r   r     s    <$:r   r   z>HHhHc                       e Zd Zd Zy)	SubHeaderc                 J    d | _         d | _        d | _        d | _        g | _        y r3   )	firstCode
entryCountidDeltaidRangeOffsetglyphIndexArrayr   s    r   r   zSubHeader.__init__  s'    !!r   N)ru   rv   rw   r   ry   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   rD   r   )r!   	subHeaderminGIr   r   rQ   s         r   
setIDDeltazcmap_format_2.setIDDelta  s    	))!,,,CqsU{ - 19v~&-o$6$:	!$)AI	!''G9//0//2736=I--a0 1 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   bigr:   r	   rs   r   r   r   )rI   rJ   r   r   sys	byteorderbyteswapmaxrD   r   r@   rA   subHeaderFormatr   r   r   r   r   rK   r   valuesr   r   rN   r   )r!   rJ   rN   subHeaderKeysmaxSubHeaderindexallKeyskeysubHeaderListposrQ   r   	giDataPosgiListr   notdefGI	firstBytesubHeadindexcharCodeoffsetIndexgicharCodeOffsetr   r   s                          r   rW   zcmap_format_2.decompile  s     2  v. 545/ II 	 ++c"$t*%CDz==E!-45WcW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 (C) $@ 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rebuildrr   r   r      r:   zCError - subhead entry count does not match len of glyphID subrange.rs   i  z>Hz?Error: cmap format 2 is not same length as calculated! actual: z calc : )rJ   r@   r]   rS   rT   r   r   r   r*   getReverseGlyphMapKeyErrorrB   r   rK   rD   r   r   r   r   r   r   r   r   r   r   r   str)!r!   rN   kEmptyTwoCharCodeRanger   r*   itemr   namesnameMapr   r   r   xr   r   r   r   lastFirstByte	firstbyte
secondByteindexcodeDiffrQ   emptySubheadIndexr   subheadRangeLenjprevSubheadrT   subheaddataListr   rJ   s!                                    r   r`   zcmap_format_2.compileP  sZ   99FDKKdmmLtyyX "$tyy())./T!W	/%*+UTaU+++-	%.34edGDMeD4< -2#J
,6q"J 	 
 
 Q<c>!I"#I#$I  !I&'I#  +It$"MHcax AI!F*J ]* 2%OOI. %)%*9+?+?%@E'0':':U'BH67M(3 &A %//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 #P 		" K		 		"#	Y'.23ZEU#'=='8e$   " " 	 ?+E%e,I&'I#5\+A.//93L3LL $11UQY!OC + 13I- " &&!+*7	'!A%((1,!- !. 1' ,. a#m,,, 	 %Sb)GW44599  * KK64==AB"EOOFKKeai89 #$GOO#%%&&OO)) % %Sb)G--D" 56 . * "4yF" 	
M#d)n &k	
" Q 0+ 5 	%///=G%27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   ro   zcmap_format_2.fromXML  q     z!23tV$DIyyGgu-#* D%u},1&MD%-() r   N)ru   rv   rw   r   rW   r`   ro   ry   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   r8   r:   r   )rD   rK   insertr   pop)	startCodeendCoder   lastIDlastCodeinOrderorderedBegin	subRangesr   glyphID	newRangesbe	thresholdrQ   startends                    r   
splitRanger    s   
 GG9})_FHGLI i!mWq[1t*Q;& g'  ,!9:# 2  ,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>4Hr:   rs   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) !)rI   rJ   r@   rA   r   r   r   r   r   r   rD   r   extendrK   r   rN   r   )r!   rJ   rN   
segCountX2searchRangeentrySelector
rangeShiftsegCountallCodesr
  r	  r   r   r   
lenGIArrayr   r   rQ   r  deltarangeOffsetpartialrangeCharCodesr   r   r  s                             r   rW   zcmap_format_4.decompilel  s~     2  v. 545/ II 	 @F}}48@
<[- ABx?;;s#4 	D==E! 9H%HqLN+Yh'	HI&9H%HI& (+"89-)
 	s9~)*AaLEAJE'*K!Q&.2S5GGG!%	!gaj1n"EFN^,aAOPXh&&0P !/H$w.E :-  ]eZ01- 'u-2"1%"85"@"#KK& 01 !/ +2 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   rr   r   r   r	   r   r   rs   r   r   )rJ   r@   r]   rS   rT   r   r   r   r   r\   r   r   rB   r   rK   r   r  r  rD   r   r   r   r   r   r   r   calcsizecmap_format_4_format) r!   rN   r   r	  r
  r   r   r   r   r   r   r   r  r   r  r  r   r   r   rQ   indicesr!  r  r  r  r   charCodeArrayidDeltaArray	restArrayrJ   rT   headers                                    r   r`   zcmap_format_4.compile  s   99FDKKdmmLtyyX )*	IhGNN1:;TYYt_E;//1G)278%$%8* D D1	c T
 2 !|HG!
I% x!|+'H'	"xF
s  's#  *# $IbM8TBJE3U#NN3V$NN6" s7|a'(AG!)A,
Q?tH~. @$uWQZc'l1JKLL
Yq\ 9WDE$$Q'q!$$Q#g,_9M*MPQ*Q%RS&&w/ ) 	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;<edGDMe<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   rp   rq   rN   r   r   r   attrsMapdummyContents
             r   ro   zcmap_format_4.fromXML  s|     z!23tV$DIyyGgu-.5+GX|%AAAq/7/?D(6*+, r   Nr   ry   r   r   r  r  k  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   r7   r8   r   rs   r   )rI   rJ   r@   rA   rB   r   r   r   r   r   r   rD   r   r   rN   r   )r!   rJ   rN   r   r   r   r   s          r   rW   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?   r;   )rJ   r@   r]   rS   rT   r   r   r   r   r   rD   r   r   r   r   r   r   r   )
r!   rN   r   r   r   r   r   r   rJ   r/  s
             r   r`   zcmap_format_6.compileH  s(   99FDKKdmmLtyyX yytyy{#uQxrQ78EaIQVQV!!$t*-1DQV   ;;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   ro   zcmap_format_6.fromXMLa  r  r   Nr   ry   r   r   r4  r4  -  s    <42:r   r4  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   )rS   rU   rJ   rN   r   s     r   r   zcmap_format_12_or_13.__init__q  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   r<   r   )
r@   rA   r   rS   rU   rT   r   nGroupsrJ   rN   )r!   rJ   rN   rS   rU   rT   r   r>  s           r   rI   z$cmap_format_12_or_13.decompileHeaderw  s    6<mmHdSVTVi6X3&(GI"w|+88	
NKKIR
 
	
8   I	r   c                 F   ||| j                  ||       n||J d       | j                  }g }g }d}t        j                  d|d | j                  dz         }t        j
                  dk7  r|j                          t        | j                        D ]g  }||dz     }||dz  dz      }	||dz  dz      }
d|	z   |z
  }|j                  t        ||	dz                |j                  | j                  |
|             i d x| _        }t        | j                  ||      | _        y )	Nr   r   Ir<   r   rr   r	   rs   )rI   rJ   r   r>  r   r   r   rD   r  _computeGIDsr   rN   r   )r!   rJ   rN   r   r   r   groupsrQ   startCharCodeendCharCoder  lenGroups               r   rW   zcmap_format_12_or_13.decompile  s6     2  v. 545/ II 	 	S$':):";<==E!OOt||$A"1q5MM Q+KQUQY'G;6HU=+/BC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)Nr<  Tr   rr   r   r   r	   z>LLLr=  r<   )rJ   r@   r]   rS   rU   rT   r   r>  r   r   r   r   r   r   rB   r   rK   r   r\   _format_stepr   rD   _IsInSameRunr   )r!   rN   r   r   r   r   r   r   r   r   r   rC  startGlyphIDlastGlyphIDlastCharCoder>  r  maxIndexr   r  rJ   lengthSubtables                         r   r`   zcmap_format_12_or_13.compile  s   99KKMMKKMMLL ))
 )*	TYY%%'(++-	%.34edGDMeD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 %!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    rS   rU   rT   r   r>  )r   r   ru   r   r    rS   rU   rT   r   r>  ri   r   r   r*   r   r   r   s       r   rj   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 )
NrS   rU   rT   r   r>  r   r   r   r   )
r   rS   rU   rT   r   r>  rn   r   r   r   r   s          r   ro   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)	ru   rv   rw   r   rI   rW   r`   rj   ro   ry   r   r   r9  r9  p  s"    "<:O
b&:r   r9  c                   $    e Zd ZdZddZd Zd Zy)cmap_format_12r	   c                 0    t         j                  | |       y r3   r9  r   r   s     r   r   zcmap_format_12.__init__      %%dF3r   c                      t        |||z         S r3   )rD   r!   startingGlyphnumberOfGlyphss      r   rA  zcmap_format_12._computeGIDs"  s    ]MN$BCCr   c                 &    |d|z   k(  xr |d|z   k(  S Nr	   ry   r!   r  rJ  r   rK  s        r   rH  zcmap_format_12._IsInSameRun%  s     1{?*NQ=M1MNr   N)r<   ru   rv   rw   rG  r   rA  rH  ry   r   r   rQ  rQ    s    L4DOr   rQ  c                   $    e Zd ZdZddZd Zd Zy)cmap_format_13r   c                 0    t         j                  | |       y r3   rS  r   s     r   r   zcmap_format_13.__init__,  rT  r   c                     |g|z  S r3   ry   rV  s      r   rA  zcmap_format_13._computeGIDs/  s    //r   c                      ||k(  xr |d|z   k(  S rZ  ry   r[  s        r   rH  zcmap_format_13._IsInSameRun2  s    ;&JX\9I-IJr   N)r=   r\  ry   r   r   r^  r^  )  s    L40Kr   r^  c                 @    d| z   }t        j                  d|      \  }|S )N    >L)r@   rA   )threeByteStringrJ   vals      r   cvtToUVSrg  6  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   rd  r	   )r@   r]   )rf  fourByteStrings     r   
cvtFromUVSrj  <  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>HLLr;   r   )r@   rA   rJ   rT   numVarSelectorRecordsrN   r   )r!   rJ   rN   rS   rT   ro  s         r   rI   zcmap_format_14.decompileHeaderC  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   r;   rd  r8   >3sBr	   >3sH   )rI   rJ   r   rD   ro  r@   rA   rg  r   r   r  r   rN   getGlyphNamerK   uvsDict)r!   rJ   rN   rw  	recOffsetnuvsdefOVSOffsetnonDefUVSOffsetvarUVSstartOffset	numValuesruvaddtlCntfirstBaseUVcnt
baseUVList	glyphListlocalUVListnumRecsr   	glyphNames                          r   rW   zcmap_format_14.decompileK  sl    2  v. 545/yy  		 	t11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A$mmFD{UV4WXGB1$K!"B $ 8 8 =I&&I7 (2FO**;7K 3R ' $ <*.{*;<    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   ry   )r   s    r   <lambda>z&cmap_format_14.toXML.<locals>.<lambda>  s    $q'*=tAwQ)Pr   )r   r  rz  r   r   )r   r   ru   r   r    ri   rw  r   r   r\   r   rK   rh   r   )
r!   rk   rN   rw  uvsListrz  uvListr  gnamerp   s
             r   rj   zcmap_format_14.toXML  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   rw  r   rz  r  r   None)r   rn   r   rw  r   r   r   r^   getGlyphOrderrK   r   )r!   r   rp   rq   rN   rw  _hasGlyphNamedNoner   rz  r  r  s              r   ro   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 )Nrn  r;   rr  r	   r   r   rs  rd  r8   ru  rt  rq  )rJ   r@   r]   rS   rT   ro  rw  r   r   r   r\   rj  rK   r  r   r   )r!   rN   rw  r  rR   rJ   varSelectorRecordsrz  	entryListentrydefListr{  lastUVr  defRecsdefEntryrec
numDefRecsndefListr|  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)GH189589%  'H1HC1$kk&*V2DcAgN!)s+ !( kk&*V*<cBs# \
FKKj9:G$!j1n,, +4M9%a8L9H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)ru   rv   rw   rI   rW   rj   ro   r`   ry   r   r   rl  rl  B  s    7r. -DG!r   rl  c                   *    e Zd Zd Zd Zd Zd Zd Zy)r}   c                 R   | j                   j                  d d t        | j                        z   }|j	                  |d| j
                  fd| j                  fg       |j                          |j                  | j                         |j                  |       |j                          y )Nr<   r   r    )r   ru   r   rS   r   r   r    ri   dumphexrJ   r   )r!   rk   rN   cmapNames       r   rj   zcmap_format_unknown.toXML  s    >>**3B/#dkk2BBt/dnn-	
 	tyy!hr   c                 2    t        |      | _        i | _        y r3   )r   rJ   r   )r!   r   rp   rq   rN   s        r   ro   zcmap_format_unknown.fromXML  s    G$		r   c                      d| _         || _        y r   )r   rJ   r!   rJ   rN   s      r   rI   z#cmap_format_unknown.decompileHeader  s    	r   c                 H    ||| j                  ||       y ||J d       y )Nr   )rI   r  s      r   rW   zcmap_format_unknown.decompile  s<      2  v. 545/r   c                 4    | j                   r| j                   S y r3   )rJ   )r!   rN   s     r   r`   zcmap_format_unknown.compile(  s    9999r   N)ru   rv   rw   rj   ro   rI   rW   r`   ry   r   r   r}   r}     s    5r   r}   )r   rs   r8   r?   r<   r=   r>   )'fontTools.misc.textToolsr   r   r   fontTools.misc.encodingToolsr   fontTools.ttLibr   fontTools.unicoder    r
   r   r@   r   logging	getLoggerru   rE   r   r   objectrG   r   r   r   r   r*  r  r  r4  r9  rQ  r^  rg  rj  rl  r}   r|   ry   r   r   <module>r     s/   A A 4 * %  
    g!_"\.. _"DB&6 B&J,:L ,:^ " "S:L S:l
  L^@L @D@:L @:Fi:< i:X
O) 
O
K) 
KB!\ B!J%, %R r   