o
    l^UfF!                    @   s  d dl mZmZmZmZm	Z
mZ d dlmZ d dlmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZ ddlmZmZmZm Z m!Z! dd	l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z-m.Z/m0Z0 d d
l1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8Z8d dl9Z9d dl:m;Z; d dl<Z<e<=e>Z?dd Z@dd ZAG dd deBZCG dd deCZDG dd deDZEG dd deDZFG dd deFZGG dd deFZHG dd  d eHZIG d!d" d"eEeHZJG d#d$ d$eFZKG d%d& d&eFZLG d'd( d(eFZMG d)d* d*eFZNG d+d, d,eFZOG d-d. d.eFZPG d/d0 d0ePeNZQG d1d2 d2ePeLZRG d3d4 d4ePeHZSG d5d6 d6eDZTG d7d8 d8eDZUG d9d: d:eUZVG d;d< d<eLZWG d=d> d>eLZXG d?d@ d@eDZYG dAdB dBeYZZG dCdD dDeYZ[G dEdF dFe[Z\G dGdH dHe[Z]G dIdJ dJe]Z^G dKdL dLe^Z_G dMdN dNeDZ`G dOdP dPeDZaG dQdR dReCZbG dSdT dTebZcG dUdV dVebZdG dWdX dXedZeG dYdZ dZedZfG d[d\ d\ebZgG d]d^ d^edZhG d_d` d`eeehZiG dadb dbedZjG dcdd ddeFZkG dedf dfekZlG dgdh dheCZmG didj djeCZnG dkdl dleCZoG dmdn dneCZpG dodp dpeCZqG dqdr dreCZrG dsdt dteCZsG dudv dveCZtG dwdx dxeCZuG dydz dzZvG d{d| d|eCZwG d}d~ d~eLZxG dd deNZyG dd deyZ,G dd deyZ.i deMdeKdeNdeLdeOdeHdead eId"eJdNe`d6eTd8eUd:eVd<eWdBeZdFe\dHe]i dJe^dLe_debdeddeedefdfeldtesdvetdxeud~exde,de.d>eXde5ewevdde5ewe$ddpeqerecececeodd dd dd dd dd dd dZzdS )    )fixedToFloatfloatToFixedfloatToFixedToStrstrToFixedToFloatensureVersionIsLongversionToFixed)TupleVariation)nearestMultipleShortestReprotRound)	bytesjointobytestostrpadsafeEval)LazyList)getSearchRange   )CountReferenceFormatSwitchingBaseTableOTTableReaderOTTableWriterValueRecordFactory)lookupTypesVarCompositeGlyphAATStateTableAATState	AATActionContextualMorphActionLigatureMorphActionInsertionMorphActionMorxSubtable
ExtendModeCompositeModeNO_VARIATION_INDEX)zip_longest
accumulate)partial)SimpleNamespaceN)Optionalc                 C   s
   t | tS N)
isinstancetuple)t r-   Clib/python3.10/site-packages/fontTools/ttLib/tables/otConverters.py<lambda>-   s   
 r/   c                 C   s  g }i }| D ]\}}}}}|}	| dr|dksJ t}
nH|ds&|dv r/tttd| }
n6|dkr6t}
n/|dkr=t}
n(|dkrDt}
n!|d	krKt	}
n|d
v rRt
}
n|tvr_d|vr_|}	t}
nt||t}
|
||||d}|jrt|j}n|dv r~||}n||	}|js||_|dv r|d |_|j D ]}| D ]}t||||||j< qq|d	kr|d |_|d	 |_|j D ]}t||||||j< q|| ||vsJ ||||< q||fS )zGiven a table spec from otData.py, build a converter object for each
    field of the table. This is called for each table in otData.py, and
    the results are assigned to the corresponding class in otTables.py.ValueFormatuint16Count)StructLength	MorphType)uint8r1   uint32SubTableExtSubTable	SubStructFeatureParams)ZCIDGlyphMappingZGlyphCIDMapping(description)	MortChainMortSubtable	MorxChain)r7   r8   r9   r   featureParamTypes)
startswithr0   endswithComputedUInt8ComputedUShortComputedULongr7   r8   r9   r:   StructWithLengthconverterMappingStructeval
tableClassgetr   valuesTable__name__rA   defaultFeatureParamsappend)Z	tableSpecZtableNamespaceZ
convertersZconvertersByNametpnamerepeatauxZdescrZ	tableNameZconverterClassconvrK   r,   clsr-   r-   r.   buildConverters0   sf   






rX   c                   @   sv   e Zd ZdZdddddZdd Zd	d
 Zdd Zdd ZdddZ	dd Z
dd ZedZdee fddZdS )BaseConverterz\Base class for converter objects. Apart from the constructor, this
    is an abstract class.N r<   c                C   sp   || _ || _|| _| jr| jst| jdd| _|| _|dp"|dv | _|dp,|dk| _|dv | _|| _	d S )Nz<string>rJ   r2   )DesignAxisRecordSizeValueRecordSizeZ
LookupTyper4   )Z
ClassCountZClass2CountZ
FeatureTagZSettingsCountVarRegionCountMappingCountZRegionAxisCountZDesignAxisCountr[   ZAxisValueCountr\   Z	AxisCountZBaseGlyphRecordCountZLayerRecordCountZAxisIndicesList)
rS   rT   rU   compilerK   rC   ZisCountZisLookupTypeisPropagatedr=   selfrS   rT   rU   rK   r=   r-   r-   r.   __init__t   s   

zBaseConverter.__init__c           	         s    j o|dk}|rtu rd}|s+g }t|D ]}| | q|S  fdd}| tfddt|D }|  |S )z(Read an array of values from the reader.   Fc                     s&     j  fdd} | S )Nc                    s     |     i S r)   )seekread)i)fontposreader_copy
recordSizerb   r-   r.   	read_item   s   zABaseConverter.readArray.<locals>.get_read_item.<locals>.read_itemcopyri   rl   )rh   readerrk   rb   )ri   rj   r.   get_read_item   s   z.BaseConverter.readArray.<locals>.get_read_itemc                 3   s    | ]} V  qd S r)   r-   ).0rg   ro   r-   r.   	<genexpr>   s    z*BaseConverter.readArray.<locals>.<genexpr>)lazygetRecordSizeNotImplementedrangerQ   rf   r   Zadvance)	rb   rp   rh   	tableDictcountrt   lrg   rq   r-   )rh   rl   rp   rk   rb   r.   	readArray   s   

zBaseConverter.readArrayc                 C   s   t | dr| jS tS )N
staticSize)hasattrr|   rv   rb   rp   r-   r-   r.   ru      s   
zBaseConverter.getRecordSizec                 C      t | )zRead a value from the reader.NotImplementedErrorrb   rp   rh   rx   r-   r-   r.   rf         zBaseConverter.readc              
   C   sT   zt |D ]\}}| ||||| qW d S  ty) } z|j|f |_ d }~ww r)   )	enumeratewrite	Exceptionargs)rb   writerrh   rx   rM   rg   valueer-   r-   r.   
writeArray   s   zBaseConverter.writeArrayc                 C   r   )zWrite a value to the writer.r   rb   r   rh   rx   r   repeatIndexr-   r-   r.   r      r   zBaseConverter.writec                 C   r   )zRead a value from XML.r   rb   attrscontentrh   r-   r-   r.   xmlRead   r   zBaseConverter.xmlReadc                 C   r   )zWrite a value to XML.r   rb   	xmlWriterrh   r   rS   r   r-   r-   r.   xmlWrite   r   zBaseConverter.xmlWritezVarIndexBase\s*\+\s*(\d+)returnc                 C   s$   | j | j}|sdS t|dS )zJIf description has `VarIndexBase + {offset}`, return the offset else None.Nr   )varIndexBasePlusOffsetREsearchr=   intgroup)rb   mr-   r-   r.   getVarIndexOffset   s   zBaseConverter.getVarIndexOffsetr)   )rO   
__module____qualname____doc__rc   r{   ru   rf   r   r   r   r   rer_   r   r(   r   r   r-   r-   r-   r.   rY   p   s    

rY   c                   @   s4   e Zd Zedd Zedd Zdd Zdd Zd	S )
SimpleValuec                 C      | S r)   r-   r   r-   r-   r.   toString      zSimpleValue.toStringc                 C   r   r)   r-   r   r-   r-   r.   
fromString   r   zSimpleValue.fromStringc                 C   s(   | ||d| |fg  |  d S Nr   )	simpletagr   newliner   r-   r-   r.   r      s   zSimpleValue.xmlWritec                 C   s   |  |d S r   )r   r   r-   r-   r.   r         zSimpleValue.xmlReadN)rO   r   r   staticmethodr   r   r   r   r-   r-   r-   r.   r      s    

r   c                   @   s    e Zd ZdZdd Zdd ZdS )OptionalValueNc                 C   s6   || j kr|d| |f ||| |  d S r   )DEFAULTrQ   r   r   r   r   r-   r-   r.   r      s   
zOptionalValue.xmlWritec                 C   s   d|v r|  |d S | jS r   )r   r   r   r-   r-   r.   r      s   zOptionalValue.xmlRead)rO   r   r   r   r   r   r-   r-   r-   r.   r      s    r   c                   @      e Zd Zedd ZdS )IntValuec                 C   
   t | dS Nr   )r   r   r-   r-   r.   r         
zIntValue.fromStringNrO   r   r   r   r   r-   r-   r-   r.   r          r   c                   @   2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )Long   c                 C      |  S r)   readLongr   r-   r-   r.   rf        z	Long.readc                 C   
   | |S r)   )readLongArrayrb   rp   rh   rx   ry   r-   r-   r.   r{        
zLong.readArrayNc                 C      | | d S r)   )	writeLongr   r-   r-   r.   r     r   z
Long.writec                 C   r   r)   )writeLongArrayrb   r   rh   rx   rM   r-   r-   r.   r   
  r   zLong.writeArrayr)   rO   r   r   r|   rf   r{   r   r   r-   r-   r-   r.   r          
r   c                   @   r   )ULongr   c                 C   r   r)   	readULongr   r-   r-   r.   rf     r   z
ULong.readc                 C   r   r)   )readULongArrayr   r-   r-   r.   r{     r   zULong.readArrayNc                 C   r   r)   
writeULongr   r-   r-   r.   r     r   zULong.writec                 C   r   r)   )writeULongArrayr   r-   r-   r.   r     r   zULong.writeArrayr)   r   r-   r-   r-   r.   r     r   r   c                   @   r   )Flags32c                 C      d|  S )Nz0x%08Xr-   r   r-   r-   r.   r     r   zFlags32.toStringN)rO   r   r   r   r   r-   r-   r-   r.   r     r   r   c                   @      e Zd ZeZdS )VarIndexN)rO   r   r   r#   r   r-   r-   r-   r.   r   $      r   c                   @   r   )Short   c                 C   r   r)   )	readShortr   r-   r-   r.   rf   +  r   z
Short.readc                 C   r   r)   )readShortArrayr   r-   r-   r.   r{   .  r   zShort.readArrayNc                 C   r   r)   )
writeShortr   r-   r-   r.   r   1  r   zShort.writec                 C   r   r)   )writeShortArrayr   r-   r-   r.   r   4  r   zShort.writeArrayr)   r   r-   r-   r-   r.   r   (  r   r   c                   @   r   )UShortr   c                 C   r   r)   
readUShortr   r-   r-   r.   rf   ;  r   zUShort.readc                 C   r   r)   )readUShortArrayr   r-   r-   r.   r{   >  r   zUShort.readArrayNc                 C   r   r)   writeUShortr   r-   r-   r.   r   A  r   zUShort.writec                 C   r   r)   )writeUShortArrayr   r-   r-   r.   r   D  r   zUShort.writeArrayr)   r   r-   r-   r-   r.   r   8  r   r   c                   @   r   )Int8r   c                 C   r   r)   )ZreadInt8r   r-   r-   r.   rf   K  r   z	Int8.readc                 C   r   r)   )readInt8Arrayr   r-   r-   r.   r{   N  r   zInt8.readArrayNc                 C   r   r)   )Z	writeInt8r   r-   r-   r.   r   Q  r   z
Int8.writec                 C   r   r)   )writeInt8Arrayr   r-   r-   r.   r   T  r   zInt8.writeArrayr)   r   r-   r-   r-   r.   r   H  r   r   c                   @   r   )UInt8r   c                 C   r   r)   )	readUInt8r   r-   r-   r.   rf   [  r   z
UInt8.readc                 C   r   r)   )readUInt8Arrayr   r-   r-   r.   r{   ^  r   zUInt8.readArrayNc                 C   r   r)   )
writeUInt8r   r-   r-   r.   r   a  r   zUInt8.writec                 C   r   r)   )writeUInt8Arrayr   r-   r-   r.   r   d  r   zUInt8.writeArrayr)   r   r-   r-   r-   r.   r   X  r   r   c                   @   "   e Zd ZdZdd ZdddZdS )UInt24   c                 C   r   r)   Z
readUInt24r   r-   r-   r.   rf   k  r   zUInt24.readNc                 C   r   r)   ZwriteUInt24r   r-   r-   r.   r   n  r   zUInt24.writer)   rO   r   r   r|   rf   r   r-   r-   r-   r.   r   h      r   c                   @      e Zd Zdd ZdS )ComputedIntc                 C   s*   |d ur| d||f  |  d S d S )Nz%s=%s)commentr   r   r-   r-   r.   r   s  s   zComputedInt.xmlWriteNrO   r   r   r   r-   r-   r-   r.   r   r      r   c                   @      e Zd ZdS )rD   NrO   r   r   r-   r-   r-   r.   rD   y      rD   c                   @   r   )rE   Nr   r-   r-   r-   r.   rE   }  r   rE   c                   @   r   )rF   Nr   r-   r-   r-   r.   rF     r   rF   c                   @   r   )Tagr   c                 C   r   r)   )ZreadTagr   r-   r-   r.   rf     r   zTag.readNc                 C   r   r)   )ZwriteTagr   r-   r-   r.   r     r   z	Tag.writer)   r   r-   r-   r-   r.   r     r   r   c                   @   s6   e Zd ZdZdZdd Zdd Zdd Zdd
dZd	S )GlyphIDr   Hc                 C   s   | || j| j|S r)   )getGlyphNameManyr{   typecoder|   r   r-   r-   r.   r{     s   zGlyphID.readArrayc                 C   s   | || j| jS r)   )getGlyphNameZ	readValuer   r|   r   r-   r-   r.   rf        zGlyphID.readc                 C      | | j|| d S r)   )r   r   ZgetGlyphIDManyr   r-   r-   r.   r        zGlyphID.writeArrayNc                 C   r   r)   )Z
writeValuer   
getGlyphIDr   r-   r-   r.   r     r   zGlyphID.writer)   )	rO   r   r   r|   r   r{   rf   r   r   r-   r-   r-   r.   r     s    r   c                   @   s   e Zd ZdZdZdS )	GlyphID32r   LN)rO   r   r   r|   r   r-   r-   r-   r.   r     s    r   c                   @   r   )NameIDc                 C   st   | ||d|fg  |r4|r4|d}|r4||}|d |r(|| n|d td|  |  d S )Nr   rS     zmissing from name tablez"name id %d missing from name table)r   rL   ZgetDebugNamer   r   logwarningr   )rb   r   rh   r   rS   r   Z	nameTabler-   r-   r.   r     s   



zNameID.xmlWriteNr   r-   r-   r-   r.   r    r   r  c                   @   r   )	STATFlagsc                 C   sh   | ||d|fg  g }|d@ r|d |d@ r|d |r.|d |d| |  d S )Nr   r   ZOlderSiblingFontAttributer   ZElidableAxisValueNamer   )r   rQ   r   r   joinr   rb   r   rh   r   rS   r   flagsr-   r-   r.   r     s   


zSTATFlags.xmlWriteNr   r-   r-   r-   r.   r    r   r  c                   @   r   )
FloatValuec                 C      t | S r)   )floatr   r-   r-   r.   r     r   zFloatValue.fromStringNr   r-   r-   r-   r.   r
    r   r
  c                   @   r   )
DeciPointsr   c                 C   s   |  d S N
   r   r   r-   r-   r.   rf        zDeciPoints.readNc                 C   s   | t|d  d S r  )r   roundr   r-   r-   r.   r     r   zDeciPoints.writer)   r   r-   r-   r-   r.   r    r   r  c                   @   s^   e Zd ZeZeZeZeZdd ZdddZ	e
dd Ze
dd	 Ze
d
d Ze
dd ZdS )BaseFixedValuec                 C   s   |  t|| j S r)   )fromIntgetattrreaderMethodr   r-   r-   r.   rf     s   zBaseFixedValue.readNc                 C   s   t || j| | d S r)   )r  writerMethodtoIntr   r-   r-   r.   r     s   zBaseFixedValue.writec                 C      t || jS r)   )fi2flprecisionBitsrW   r   r-   r-   r.   r       zBaseFixedValue.fromIntc                 C   r  r)   )fl2fir  r  r-   r-   r.   r    r  zBaseFixedValue.toIntc                 C   r  r)   )str2flr  r  r-   r-   r.   r     r  zBaseFixedValue.fromStringc                 C   r  r)   )fl2strr  r  r-   r-   r.   r     r  zBaseFixedValue.toStringr)   )rO   r   r   rv   r|   r  r  r  rf   r   classmethodr  r  r   r   r-   r-   r-   r.   r    s    



r  c                   @      e Zd ZdZdZdZdZdS )Fixedr      r   r   NrO   r   r   r|   r  r  r  r-   r-   r-   r.   r"    
    r"  c                   @   r!  )F2Dot14r      r   r   Nr$  r-   r-   r-   r.   r&    r%  r&  c                       sP   e Zd ZdZdZe fddZe fddZedd Zed	d
 Z	  Z
S )Angleg        g     ?c                    s   t  || j d S N   )superr  biasr  	__class__r-   r.   r       zAngle.fromIntc                    s   t  |d | j S r)  )r+  r  r,  r  r-  r-   r.   r  
  r/  zAngle.toIntc                 C   s   t t|| j | j S r)   )r
   r  factorr  r-   r-   r.   r     s   zAngle.fromStringc                 C   r  r)   )r	   r0  r  r-   r-   r.   r     r  zAngle.toString)rO   r   r   r,  r0  r   r  r  r   r   __classcell__r-   r-   r-  r.   r(     s    
r(  c                   @   s   e Zd ZdZdS )BiasedAngleg      ?N)rO   r   r   r,  r-   r-   r-   r.   r2    s    r2  c                   @   sF   e Zd ZdZdd ZdddZedd Zed	d
 Zedd Z	dS )Versionr   c                 C   s   |  }|S r)   r   )rb   rp   rh   rx   r   r-   r-   r.   rf   !  s   zVersion.readNc                 C   s   t |}|| d S r)   )fi2ver   r   r-   r-   r.   r   %  s   zVersion.writec                 C   r  r)   )ve2fir   r-   r-   r.   r   )  r   zVersion.fromStringc                 C   r   )N0x%08xr-   r   r-   r-   r.   r   -  r   zVersion.toStringc                 C   r   )Nr#  )r  )vr-   r-   r.   	fromFloat1  r   zVersion.fromFloatr)   )
rO   r   r   r|   rf   r   r   r   r   r8  r-   r-   r-   r.   r3    s    


r3  c                   @   s&   e Zd ZdZdZdd ZdddZdS )	Char64zAn ASCII string with up to 64 characters.

    Unused character positions are filled with 0x00 bytes.
    Used in Apple AAT fonts in the `gcid` table.
    @   c                 C   s\   | | j}|d}|dkr|d | }t|ddd}|t|dddkr,td|  |S )N    r   asciireplaceencodingerrorsignorez%replaced non-ASCII characters in "%s")readDatar|   findr   r  r  )rb   rp   rh   rx   dataZzeroPossr-   r-   r.   rf   ?  s   
zChar64.readNc                 C   sv   t |ddd}|t |dddkrtd|  t|| jkr(td|| jf  |d| j  d | j }|| d S )Nr<  r=  r>  rA  z&replacing non-ASCII characters in "%s"z$truncating overlong "%s" to %d bytesr;  )r   r  r  lenr|   	writeData)rb   r   rh   rx   r   r   rD  r-   r-   r.   r   I  s   zChar64.writer)   )rO   r   r   r   r|   rf   r   r-   r-   r-   r.   r9  6  s
    
r9  c                   @   s>   e Zd Zdd Zdd ZdddZdd	 Zd
d Zdd ZdS )rI   c                 C   s   | j o| j |S r)   )rK   ru   r~   r-   r-   r.   ru   V     zStruct.getRecordSizec                 C   s   |   }||| |S r)   )rK   	decompile)rb   rp   rh   rx   tabler-   r-   r.   rf   Y  s   zStruct.readNc                 C   s   | || d S r)   r_   r   r-   r-   r.   r   ^     zStruct.writec                 C   sF   |d u r|r| ||dg  |  d S 	 d S |j||||d d S )N)emptyr   )rS   )r   r   toXMLr   r-   r-   r.   r   a  s   zStruct.xmlWritec                 C   s4  d|v rt |d rd S |  }|d}|d urt||_t|d }|r\d}| D ]/}|jr[d}t|ds;i |_|j}	|j	|	vsJJ |j	|	ft
||j	d  t|j|j	|	|j	< q,|D ]}
t|
trs|
\}}}||||| q^	 q^|jt|dd d |r|r| D ]}|jr|j}	|	|j	= |	s|`q|S )NrM  FormatZpostReadFT_propagator)
propagator)r   rK   rL   r   rO  r}   getConvertersr`   rP  rS   setattrr   __dict__r*   r+   fromXMLZpopulateDefaultsr  )rb   r   r   rh   rJ  rO  Z
noPostReadZcleanPropagationrV   rQ  elementrS   r-   r-   r.   r   n  sF   




zStruct.xmlReadc                 C   s   dt | j S )Nz
Struct of )reprrK   )rb   r-   r-   r.   __repr__  r   zStruct.__repr__r)   )	rO   r   r   ru   rf   r   r   r   rX  r-   r-   r-   r.   rI   U  s    
*rI   c                   @      e Zd Zdd ZdddZdS )rG   c                 C   s.   |j }|  }||| |||j  |S r)   )ri   rK   rI  re   r3   )rb   rp   rh   rx   ri   rJ  r-   r-   r.   rf     s
   zStructWithLength.readNc                 C   s   t | D ]\}}|jdkr nqt|j| }t|tr"|d7 }dddd|j }	| }
|	|_	|
|| | |
 }| }||||| |j| dd |j ksXJ | |j|< d S )Nr3   r      i     >[= )r   r   r      ޭ)r   rR  rS   rF  itemsr*   r   r|   getDataLengthr3   r_   getSubWriterr   
getAllData)rb   r   rh   rx   r   r   Z	convIndexrV   lengthIndexZdeadbeefbeforelengthZlengthWriterr-   r-   r.   r     s    

zStructWithLength.writer)   rO   r   r   rf   r   r-   r-   r-   r.   rG     s    rG   c                   @   s2   e Zd ZdZdd Zdd Zdd Zdd	d
ZdS )rN   r   c                 C   r   r)   r   r~   r-   r-   r.   
readOffset  r   zTable.readOffsetc                 C      | d d S r   r   rb   r   r-   r-   r.   writeNullOffset  r   zTable.writeNullOffsetc                 C   sN   |  |}|dkrd S |  }||}|jr||_||_|S ||| |S r   )re  rK   getSubReaderrt   rp   rh   rI  )rb   rp   rh   rx   offsetrJ  r-   r-   r.   rf     s   

z
Table.readNc                 C   sT   |d u r|  | d S | }| j|_|d ur||_|j|| jd ||| d S )NZ
offsetSize)rh  r_  rS   r   writeSubTabler|   r_   )rb   r   rh   rx   r   r   	subWriterr-   r-   r.   r     s   zTable.writer)   )rO   r   r   r|   re  rh  rf   r   r-   r-   r-   r.   rN     s    rN   c                   @       e Zd ZdZdd Zdd ZdS )LTabler   c                 C   r   r)   r   r~   r-   r-   r.   re    r   zLTable.readOffsetc                 C   rf  r   r   rg  r-   r-   r.   rh    r   zLTable.writeNullOffsetNrO   r   r   r|   re  rh  r-   r-   r-   r.   ro        ro  c                   @   rn  )Table24r   c                 C   r   r)   r   r~   r-   r-   r.   re    r   zTable24.readOffsetc                 C   rf  r   r   rg  r-   r-   r.   rh    r   zTable24.writeNullOffsetNrp  r-   r-   r-   r.   rr    rq  rr  c                       $   e Zd Zdd Z fddZ  ZS )r9   c                 C   $   | j | | }| | j| j| j|S r)   r   r.  rS   rT   rU   rb   Z	tableTypeZ
lookupTyperK   r-   r-   r.   getConverter     zSubStruct.getConverterc                       t t| |||d | d S r)   )r+  r9   r   r   r-  r-   r.   r        zSubStruct.xmlWriterO   r   r   rw  r   r1  r-   r-   r-  r.   r9         r9   c                       rs  )r7   c                 C   rt  r)   ru  rv  r-   r-   r.   rw    rx  zSubTable.getConverterc                    ry  r)   )r+  r7   r   r   r-  r-   r.   r     rz  zSubTable.xmlWriter{  r-   r-   r-  r.   r7     r|  r7   c                   @   s   e Zd ZdddZdS )r8   Nc                 C   s   d|_ t| ||||| d S )NT)Z	ExtensionrN   r   r   r-   r-   r.   r     s   zExtSubTable.writer)   )rO   r   r   r   r-   r-   r-   r.   r8     s    r8   c                   @   r   )r:   c                 C   s&   | j || j}| | j| j| j|S r)   )rA   rL   rP   r.  rS   rT   rU   )rb   Z
featureTagrK   r-   r-   r.   rw  
     zFeatureParams.getConverterN)rO   r   r   rw  r-   r-   r-   r.   r:   	  r   r:   c                   @   s2   e Zd ZdZdddddZdd Zdd	d
ZdS )r0   r   NrZ   r<   c                C   s4   t j| |||||d d|d dkrdnd | _d S )Nr<   r0   21)rY   rc   whichra   r-   r-   r.   rc     s   zValueFormat.__init__c                 C   s   |  }t||| j< |S r)   )r   r   r  rb   rp   rh   rx   formatr-   r-   r.   rf     s   zValueFormat.readc                 C   s   | | t||| j< d S r)   )r   r   r  )rb   r   rh   rx   r  r   r-   r-   r.   r     s   
zValueFormat.writer)   )rO   r   r   r|   rc   rf   r   r-   r-   r-   r.   r0     s
    r0   c                   @   s6   e Zd Zdd Zdd ZdddZdd	 Zd
d ZdS )ValueRecordc                 C   s   dt || j  S Nr   )rF  r  r~   r-   r-   r.   ru   #  rH  zValueRecord.getRecordSizec                 C   s   || j  ||S r)   )r  ZreadValueRecordr   r-   r-   r.   rf   &  rH  zValueRecord.readNc                 C   s   || j  ||| d S r)   )r  ZwriteValueRecordr   r-   r-   r.   r   )  r   zValueRecord.writec                 C   s"   |d u rd S | ||| j| d S r)   )rN  rS   r   r-   r-   r.   r   ,  s   zValueRecord.xmlWritec                 C   s&   ddl m} | }|d ||| |S )Nr   )r  )otBaser  rU  )rb   r   r   rh   r  r   r-   r-   r.   r   2  s   zValueRecord.xmlReadr)   )rO   r   r   ru   rf   r   r   r   r-   r-   r-   r.   r  "  s    
r  c                   @   s   e Zd ZdZddddZdd Zd+d	d
Zedd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdS ),	AATLookupr  rZ   r<   c                C   sP   t j| |||||d t| jtr| jdd d d| _d S tdd d | jd| _d S )Nr<   ZValue)rS   rT   rU   )rS   rT   rU   rK   )rY   rc   
issubclassrK   r   	converterrN   ra   r-   r-   r.   rc   =  s   
zAATLookup.__init__c                 C   sx   |  }|dkr| ||S |dkr| ||S |dkr"| ||S |dkr,| ||S |dkr6| ||S J d| )Nr   r   r      rd   Fzunsupported lookup format: %d)r   readFormat0readFormat2readFormat4readFormat6readFormat8r  r-   r-   r.   rf   H  s   zAATLookup.readNc                    s   t t fdd| D }t ttd | | || | || | || | |g}|d \}}	}
| }|
  | | }||ksQJ d|	||f d S )Nc                    s   g | ]\}}  ||fqS r-   r   )rr   glyphvalrh   r-   r.   
<listcomp>Y  s    z#AATLookup.write.<locals>.<listcomp>r   z;AATLookup format %d claimed to write %d bytes, but wrote %d)	listsortedr]  filterbuildFormat0buildFormat2buildFormat6buildFormat8r^  )rb   r   rh   rx   r   r   rM   formatsZdataSizeZlookupFormatZwriteMethodri   Z
actualSizer-   r  r.   r   W  s4   
zAATLookup.writec                 C   sH   |  | |  | t||d\}}}|  | |  | |  | d S )N)nZitemSize)r   r   )r   numUnitsunitSizeZsearchRangeZentrySelectorZ
rangeShiftr-   r-   r.   writeBinSearchHeaderw  s   



zAATLookup.writeBinSearchHeaderc                    sD   t   }t |krd S jj}d||  d fddfS )Nr   r   c                           S r)   )writeFormat0r-   rh   rb   rM   r   r-   r.   r/         z(AATLookup.buildFormat0.<locals>.<lambda>)rF  getGlyphOrderr  r|   )rb   r   rh   rM   	numGlyphs	valueSizer-   r  r.   r    s   
zAATLookup.buildFormat0c                 C   s2   | d |D ]\}}| jj||d |d d qd S )Nr   rx   r   r   )r   r  r   )rb   r   rh   rM   ZglyphID_r   r-   r-   r.   r    s   

zAATLookup.writeFormat0c                    s   |d \}}|}g |dd  D ]\}}||d ks||kr- |||f | }}|}q|}q |||f jj}	td |	d }
}dj |
|  d fddfS )Nr   r   r   r   c                      s     S r)   )writeFormat2r-   rh   segmentsrb   r   r-   r.   r/     r  z(AATLookup.buildFormat2.<locals>.<lambda>)rQ   r  r|   rF  BIN_SEARCH_HEADER_SIZE)rb   r   rh   rM   ZsegStartZsegValueZsegEndglyphIDZcurValuer  r  r  r-   r  r.   r    s    zAATLookup.buildFormat2c           
      C   s   | d | jj}t||d }}| ||| |D ]\}}}	| | | | | jj||d |	d d q| d | d |d|  d S )Nr   r   r    r;  r   r  r|   rF  r  r   rG  )
rb   r   rh   r  r  r  r  Z
firstGlyphZ	lastGlyphr   r-   r-   r.   r    s   





zAATLookup.writeFormat2c                    sD   j j}t|d }}dj |d |  d fddfS )Nr   r   r  c                      r  r)   )writeFormat6r-   r  r-   r.   r/     r  z(AATLookup.buildFormat6.<locals>.<lambda>)r  r|   rF  r  )rb   r   rh   rM   r  r  r  r-   r  r.   r    s   zAATLookup.buildFormat6c           	      C   s|   | d | jj}t||d }}| ||| |D ]\}}| | | jj||d |d d q| d |d|  d S )Nr  r   r  r  r;  r  )	rb   r   rh   rM   r  r  r  r  r   r-   r-   r.   r    s   



zAATLookup.writeFormat6c                    s^   d d d d }}t || d krd S jj}dt |  d fddfS )Nr   r~  r   r  rd   c                      r  r)   )writeFormat8r-   r  r-   r.   r/     r  z(AATLookup.buildFormat8.<locals>.<lambda>)rF  r  r|   )rb   r   rh   rM   Z
minGlyphIDZ
maxGlyphIDr  r-   r  r.   r    s   zAATLookup.buildFormat8c                 C   sV   |d d }| d | | | t| |D ]\}}| jj||d |d d qd S )Nr   rd   r  )r   rF  r  r   )rb   r   rh   rM   ZfirstGlyphID_r   r-   r-   r.   r    s   


zAATLookup.writeFormat8c                    s6   t   }| jj| d |d} fddt|D S )Nrx   ry   c                    s   i | ]
\}}  ||qS r-   r   rr   kr   r  r-   r.   
<dictcomp>      z)AATLookup.readFormat0.<locals>.<dictcomp>)rF  r  r  r{   r   )rb   rp   rh   r  rD  r-   r  r.   r    s   zAATLookup.readFormat0c                 C   s   i }|j d }| | }}|d| jj ksJ |t|D ]3}||||  d  | }| }	| jj||d d}
|dkrSt|	|d D ]	}|
|||< qIq |S )Nr   r      rx   r  r   ri   r   r  r|   rw   re   rf   r   )rb   rp   rh   mappingri   r  r  rg   lastfirstr   r  r-   r-   r.   r    s   
zAATLookup.readFormat2c                 C   s   i }|j d }| }|dksJ |t| D ]I}||||  d  | }| }| }	|dkrb|d}
|
||	  | jj|
|d || d d}t|D ]\}}||||| < qTq|S )Nr   r  r  r  r   r   r  )	ri   r   rw   re   ri  r  r{   r   r   )rb   rp   rh   r  ri   r  rg   r  r  rj  
dataReaderrD  r  r7  r-   r-   r.   r    s&   

zAATLookup.readFormat4c           	      C   s   i }|j d }| }|d| jj ksJ |t| D ]%}||||  d  | }| jj||d d}|dkrB||||< q|S )Nr   r  r  r  r  )	rb   rp   rh   r  ri   r  rg   r  r   r-   r-   r.   r  
  s   
zAATLookup.readFormat6c                    s<   |   |  }| jj|d |d} fddt|D S )Nr  c                    s    i | ]\}}  | |qS r-   r  r  r  rh   r-   r.   r    s     z)AATLookup.readFormat8.<locals>.<dictcomp>)r   r  r{   r   )rb   rp   rh   ry   rD  r-   r  r.   r    s   zAATLookup.readFormat8c           	      C   sF   i }|D ]}t |tr |\}}}|dkr | j|||||d < q|S )NLookupr  )r*   r+   r  r   )	rb   r   r   rh   r   rV  rS   a
eltContentr-   r-   r.   r     s   

zAATLookup.xmlReadc                 C   s\   | || |  t| D ]\}}| jj|||dd|fgd q|| |  d S )Nr  r  )r   rS   r   )begintagr   r  r]  r  r   endtag)rb   r   rh   r   rS   r   r  r-   r-   r.   r   &  s   
zAATLookup.xmlWriter)   )rO   r   r   r  rc   rf   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r-   r-   r-   r.   r  :  s,    
 

	
	r  c                   @   .   e Zd Zdd Zd
ddZdd Zdd	 ZdS )AATLookupWithDataOffsetc                 C   sz   |  }|  }||}tdd d t}||||}i }	| D ]\}
}||| }|  }||| ||	|
< q!|	S NDataOffsets)r   ri  r  r   rf   r]  rK   rI  )rb   rp   rh   rx   ZlookupOffsetZ
dataOffsetlookupReaderlookupoffsetsresultr  rj  r  itemr-   r-   r.   rf   9  s   

zAATLookupWithDataOffset.readNc                 C   s   i i d}}}g }	t ||jdD ]0}
t }||
 || | }||d }|d kr=|}|t| }|||< |	| |||
< q| }t	dd d t
}|||||d  | }|j|dd |j|dd |	D ]}|| qjd S )Nr   )keyr  r   rk  )r  r   r   r_   r`  rL   rF  rQ   r_  r  r   r   rl  rG  )rb   r   rh   rx   r   r   ZoffsetByGlyphZoffsetByDataZdataLenZcompiledDatar  rm  rD  rj  lookupWriterr  Z
dataWriterdr-   r-   r.   r   G  s,   


zAATLookupWithDataOffset.writec                 C   s   t dd d | j}||||S r  )r  rK   r   )rb   r   r   rh   r  r-   r-   r.   r   j  s   zAATLookupWithDataOffset.xmlReadc                 C   s&   t dd d | j}|||||| d S r  )r  rK   r   )rb   r   rh   r   rS   r   r  r-   r-   r.   r   n  r}  z AATLookupWithDataOffset.xmlWriter)   rO   r   r   rf   r   r   r   r-   r-   r-   r.   r  8  s
    
#r  c                   @   sf   e Zd ZdddddZdd e D Zdd	d
ddZdd Zdd Zdd Z	dd Z
dddZdS )MorxSubtableConverterZLayoutOrderZReversedLayoutOrderZLogicalOrderZReversedLogicalOrder))FF)TFFT)TTc                 C   s   i | ]\}}||qS r-   r-   )rr   r  r  r-   r-   r.   r  |      z MorxSubtableConverter.<dictcomp>NrZ   r<   c                C   s   t j| |||||d d S Nr<   )rY   rc   ra   r-   r-   r.   rc   ~  s   
zMorxSubtableConverter.__init__c                 C   s6   |d@ dkrd|_ d S |d@ dkrd|_ d S d|_ d S )N    r   Any   Vertical
Horizontal)TextDirection)rb   r	  Zsubtabler-   r-   r.   "_setTextDirectionFromCoverageFlags  s
   


z8MorxSubtableConverter._setTextDirectionFromCoverageFlagsc                 C   s  |j }t }| |_| }|d@ dk|d@ dkf}| j| |_| || | |_	| j	|d@ d> O  _	| |_
| |_td |j
}|d u rTJ d|j
 |j | }	|j|j |j |j |	  }
t|
|j|	 ksrJ t|
|jd}| |_|j|| |||j  |S )	Nr:  r   r#     ZmorxFz!unsupported 'morx' lookup type %s)rD  tableTag)ri   r    r   r3   r   _PROCESSING_ORDERSProcessingOrderr  r   Reservedr4   SubFeatureFlagsr   rL   rD  rF  r   r  r9   rI  re   )rb   rp   rh   rx   ri   r   r	  orderKeyrK   ZheaderLengthrD  Z	subReaderr-   r-   r.   rf     s,   




zMorxSubtableConverter.readc                 C   s   | || |  |d|j  |  |jd|jd |  |jd|jd |  |jdkrA|jdd|j d |  |d|j  |  |jd	d
|j	 d |  |j
|| || |  d S )NzStructLength=%dr  r   r  r   r  z0x%04xzMorphType=%dr  r6  )r  r   r   r3   r   r  r  r  r4   r  r9   rN  r  r   r-   r-   r.   r     s$   

zMorxSubtableConverter.xmlWritec           
      C   s0  t  }d}d|_tt|D ]~\}}}|dkr5t|d }|d@ dk|d@ dkf}	| j|	 |_| || q|dkrL|d |_|j| jv sKJ d|j q|dkrb|d |_	|j	d	v saJ d
|j	 q|dkrnt|d |_q|dkrzt|d |_
q|dr||||| qJ ||d@ d> |jB |_|S )Nr   CoverageFlagsr   r:  r#  r  zunknown ProcessingOrder: %sr  >   r  r  r  zunknown TextDirection %sr  r  ZMorphFr  )r    r  r  istupler   r  r  r  _PROCESSING_ORDERS_REVERSEDr  r  rC   rU  )
rb   r   r   rh   r   covFlagseltNameeltAttrsr  r  r-   r-   r.   r     s8   


zMorxSubtableConverter.xmlReadc                 C   s   |j d@ d? }| j|j \}}||jdkrdndO }||rdndO }||jdkr*dndO }||r2dndO }||_t|j}	| }
d	|_|j }|j d
@ |_ |	|| ||_ |j|	 dks_J | |
 }t
d||j|	< d S )Ni   r#  r  r  r   r:  r  r  r[  r  r\  z>L)r  r  r  r  r  rF  r]  r^  r3   r_   structZpack)rb   r   rh   rx   r   r   r  ZreverseOrderZlogicalOrderra  rb  ZorigReservedrc  r-   r-   r.   r     s&   
zMorxSubtableConverter.writer)   )rO   r   r   r  r]  r  rc   r  rf   r   r   r   r-   r-   r-   r.   r  s  s     r  c                   @   s   e Zd ZddddZdd Zdd Zd	d
 Zdd Zdd Zd&ddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdS )'	STXHeaderrZ   r<   c                C   s`   t j| |||||d t| jtsJ tdd d t| _t| jtr+tdd d t	| _
d S d | _
d S )Nr<   GlyphClassesPerGlyphLookup)rY   rc   r  rK   r   r  r   classLookupr   r   perGlyphLookupra   r-   r-   r.   rc     s   
zSTXHeader.__init__c                 C   s  t  }|j}|d}|d}|d}d }	d }
| |_|||   |||   |||   | jd urL|d}|||   t| jt	r|d}	|	||   |d}|||   |d}
|
||   |
j|j d }|dksJ |
||_| |
||_nt| jtr|d}	|	||   | j||||_t|j|j |jd  }t|D ]"}t }|j| t|jD ]}| }| ||||	|j|< qq| jd ur| ||||_|S )Nr   r   )r   ri   ri  r   GlyphClassCountre   r  r  rK   r   r   LigComponents_readLigatures	Ligaturesr   r  rf   r  r   rw   r   StatesrQ   r   _readTransitionTransitions_readPerGlyphLookupsPerGlyphLookups)rb   rp   rh   rx   rJ  ri   ZclassTableReaderZstateArrayReaderZentryTableReaderactionReaderZligaturesReaderZperGlyphTableReaderZligComponentReaderZnumLigComponentsZ	numStates
stateIndexstate
glyphClass
entryIndexr-   r-   r.   rf      s\   










zSTXHeader.readc                 C   s0   |   }||j||j  }|||| |S r)   )rK   ri  ri   r|   rI  )rb   rp   r  rh   r  
transitionZentryReaderr-   r-   r.   r  /  s   zSTXHeader._readTransitionc                 C   s(   t |j}||j d }|||S r  )rF  rD  ri   r   r   )rb   rp   rh   limitZnumLigatureGlyphsr-   r-   r.   r  7  s   
zSTXHeader._readLigaturesc                 C   sb   d}|j D ])}|j D ]!}t|tr-|jdkr t||jd }|jdkr-t||jd }qq|S )Nr   r  r   )r  r  rM   r*   r   Z	MarkIndexmaxZCurrentIndex)rb   rJ  
numLookupsr  r,   r-   r-   r.   _countPerGlyphLookups<  s   



zSTXHeader._countPerGlyphLookupsc                 C   sT   |j }g }t| |D ]}|d}|||   || j||i  q|S r   )	ri   rw   r  ri  re   r   rQ   r  rf   )rb   rJ  rp   rh   ri   Zlookupsr  r  r-   r-   r.   r  K  s   
zSTXHeader._readPerGlyphLookupsNc                 C   s  t  }| jj||||jd d t| d}t|j d }d}	| jd ur*|	d7 }	|	| j	j
7 }	| j	||j\}
}| ||j||\}}|	t| }|t| }|t| }t| ||d}|
d urk|t| }nd }d\}}| ||}| ||}|d urt|dksJ |t|
 }|t| }|| ||	 || || | jd ur|| |d ur|| |d ur|| || || || || || |
d ur||
 |d ur|| |d ur|| d S d S )N)r   r   r   r#  r   )NNr   )r   r  r   r  r   r`  r  rM   r  rK   ZactionHeaderSizeZcompileActionsr  _compileStatesrF  _compilePerGlyphLookups_compileLigComponents_compileLigaturesr   rG  )rb   r   rh   rx   r   r   ZglyphClassWriterZglyphClassDataglyphClassCountZglyphClassTableOffsetZ
actionDataactionIndexstateArrayDataentryTableDataZstateArrayOffsetZentryTableOffsetZperGlyphOffsetZperGlyphDataZactionOffsetZligaturesOffsetZligComponentsOffsetZligComponentsDataZligaturesDatar-   r-   r.   r   T  sd   
















zSTXHeader.writec                 C   s   t  }g i }}|D ]L}t|D ]E}	|j|	 }
t  }|
||| | }t||
jks:J dt|
|
jt|f ||}|d u rPt|}|||< |	| |
| qq
t| d}tt|d}||fS )Nz1%s has staticSize %d, but actually wrote %d bytesr   )r   rw   r  r_   r`  rF  r|   rW  rL   rQ   r   r   r   )rb   rh   Zstatesr  r	  ZstateArrayWriterentriesZentryIDsr  r  r  ZentryWriterZ	entryDatar  r
  r  r-   r-   r.   r    s2   



zSTXHeader._compileStatesc                 C   s   | j d u rdS | |}t|j|ksJ dt|j|f t }|jD ]}| }| j ||i |d  |j|dd q$| S )N    zVlen(AATStateTable.PerGlyphLookups) is %d, but the actions inside the table refer to %dr   rk  )	r  r  rF  r  r   r_  r   rl  r`  )rb   rJ  rh   r  r   r  r  r-   r-   r.   r    s   


z!STXHeader._compilePerGlyphLookupsc                 C   s2   t |dsd S t }|jD ]}|| q| S )Nr  )r}   r   r  r   r`  )rb   rJ  rh   r   Z	componentr-   r-   r.   r    s   

zSTXHeader._compileLigComponentsc                 C   s8   t |dsd S t }|jD ]
}||| q| S )Nr  )r}   r   r  r   r   r`  )rb   rJ  rh   r   Z	glyphNamer-   r-   r.   r    s   

zSTXHeader._compileLigaturesc                 C   s  | || |  |d|j  |  t|j D ]\}}|jd||d |  qt|j	D ]/\}}	|j d|d |  t|	j
 D ]\}
}|j||d|
idd qI|d |  q3t|jD ]/\}}|j d	|d |  t| D ]\}}|jd
||d |  q}|d	 |  qht|dr| d |  tt|dD ]\}}|jd||d |  q|d |  | ||||| || |  d S )NzGlyphClassCount=%s
GlyphClassr  r   State)indexonGlyphClass
Transition)rh   r   rS   r  r  r  LigComponent)r  r   )r  r   r   r  r  r  r]  r   r   r  r  rN  r  r  r}   r  _xmlWriteLigatures)rb   r   rh   r   rS   r   gklassr  r  r  Ztransrg   r  r  r  r-   r-   r.   r     sL   










zSTXHeader.xmlWritec                 C   sf   t |dsd S |d |  tt|dD ]\}}|jd||d |  q|d |  d S )Nr  Ligature)r  r  )r}   r  r   r   r  r   r  )rb   r   rh   r   rS   r   rg   r  r-   r-   r.   r    s   



zSTXHeader._xmlWriteLigaturesc                 C   s   t  }tt|D ]W\}}}|dkr!|d }|d }	t|	|j|< q|dkr3| |||}
|j|
 q|dkrF| j	|||}|j
| q|dkrS| ||||_q|dkr_| ||||_qt|j d |_|S )	Nr  r  r   r  r  r  r  r   )r   r  r  r   r  _xmlReadStater  rQ   r  r   r  _xmlReadLigComponentsr  _xmlReadLigaturesr  r  rM   r  )rb   r   r   rh   rJ  r  r  r  r  r   r  r  r-   r-   r.   r     s*   zSTXHeader.xmlReadc           
      C   sV   t  }tt|D ] \}}}|dkr(t|d }|  }	|	|||| |	|j|< q|S )Nr  r  )r   r  r  r   rK   rU  r  )
rb   r   r   rh   r  r  r  r  r  r  r-   r-   r.   r    s   
zSTXHeader._xmlReadStatec                 C   s8   g }t t|D ]\}}}|dkr|t|d  q|S )Nr  r   )r  r  rQ   r   )rb   r   r   rh   ZligComponentsr  r  _eltContentr-   r-   r.   r    s   zSTXHeader._xmlReadLigComponentsc                 C   s4   g }t t|D ]\}}}|dkr||d  q|S )Nr  r  )r  r  rQ   )rb   r   r   rh   Zligsr  r  r  r-   r-   r.   r    s   zSTXHeader._xmlReadLigaturesr)   )rO   r   r   rc   rf   r  r  r  r  r   r  r  r  r  r   r  r   r  r  r  r-   r-   r-   r.   r    s$    /
	7(
r  c                   @   r  )CIDGlyphMapc                 C   s>   |  }i }t||D ]\}}|dkr||||< q|S Nr  )r   r   r   r   )rb   rp   rh   rx   ZnumCIDsr  cidr  r-   r-   r.   rf   (  s   zCIDGlyphMap.readNc           	         X    fdd|  D }|rt|d nd}|| t|D ]}|||d qd S )Nc                    s   i | ]
\}}|  |qS r-   r  )rr   r  r  r  r-   r.   r  1  r  z%CIDGlyphMap.write.<locals>.<dictcomp>r   r   r  r]  r  r   rw   rL   )	rb   r   rh   rx   r   r   r]  ry   r  r-   r  r.   r   0  s   
zCIDGlyphMap.writec                 C   s>   i }t t|D ]\}}}|dkr|d  |t|d < q|S )NCIDr  r  )r  r  stripr   rb   r   r   rh   r  ZeNameZeAttrsZ	_eContentr-   r-   r.   r   7  s   zCIDGlyphMap.xmlReadc                 C   h   | || |  t| D ]\}}|d ur(|dkr(|jd||d |  q|| |  d S )Nr  r"  )r  r  r  r   r  r]  r   r  )rb   r   rh   r   rS   r   r  r  r-   r-   r.   r   >     
zCIDGlyphMap.xmlWriter)   r  r-   r-   r-   r.   r  '  s
    
r  c                   @   r  )GlyphCIDMapc           
      C   s|   |  }| }||}|t|krtd|t|f  i }ttt|t|D ]}|| }	|	dkr;|	||| < q+|S )NzOGlyphCIDMap has %d elements, but the font has only %d glyphs; ignoring the restr  )r  r   r   rF  r  r  rw   min)
rb   rp   rh   rx   Z
glyphOrderry   Zcidsr  r  r  r-   r-   r.   rf   J  s    

zGlyphCIDMap.readNc           	         r   )Nc                    s,   i | ]\}}|d ur|dkr  ||qS r  r  )rr   r  r  r  r-   r.   r  \  s
    
z%GlyphCIDMap.write.<locals>.<dictcomp>r   r   r  r!  )	rb   r   rh   rx   r   r   r]  ry   r  r-   r  r.   r   [  s   

zGlyphCIDMap.writec                 C   s:   i }t t|D ]\}}}|dkrt|d ||d < q|S )Nr"  r   r  )r  r  r   r$  r-   r-   r.   r   f  s   zGlyphCIDMap.xmlReadc                 C   r%  )Nr  r"  r  r&  )rb   r   rh   r   rS   r   r  r  r-   r-   r.   r   m  r'  zGlyphCIDMap.xmlWriter)   r  r-   r-   r-   r.   r(  I  s
    
r(  c                   @   r  )
DeltaValuec                 C   s   |d }|d }|d }|dv sJ d|| d }d|> }d|> }	d|> d }
d|d > }g }d\}}t |D ]$}|dkrE| d	}}|| }||? |
@ }||@ rW||	 }|| q8|S )
N	StartSizeEndSizeDeltaFormatr   r   r   illegal DeltaFormatr   )r   r   r   r#  )rw   r   rQ   )rb   rp   rh   rx   r+  r,  r-  nItemsnBitsZminusOffsetmaskZsignMaskr*  tmpshiftrg   r   r-   r-   r.   rf   y  s(   zDeltaValue.readNc                 C   s   |d }|d }|d }|}	|dv sJ d|| d }
d|> }t |	|
ks(J d|> d }d\}}|	D ]}|| }|||@ |> B }|dkrO|| d\}}q4|d	kr[|| d S d S )
Nr+  r,  r-  r.  r/  r   )r   r#  r   r#  )rF  r   )rb   r   rh   rx   r   r   r+  r,  r-  r*  r0  r1  r2  r3  r4  r-   r-   r.   r     s(   
zDeltaValue.writec                 C   "   | ||d|fg  |  d S r   r   r   r   r-   r-   r.   r        zDeltaValue.xmlWritec                 C      t |d S r   r   r   r-   r-   r.   r     r  zDeltaValue.xmlReadr)   rO   r   r   rf   r   r   r   r-   r-   r-   r.   r*  x  s
    
r*  c                   @   rY  )VarIdxMapValuec           	         s|   |d }|d }d|d@  }d|> d  d  d| d|d@ d?  }|j |j|j|jd	| } fd
d||D S )NEntryFormatr^   r   r  l    r#  0   r   r   r   r   r   c                    s    g | ]}|@ > | @ B qS r-   r-   )rr   raw	innerMaskZ	outerMask
outerShiftr-   r.   r        z'VarIdxMapValue.read.<locals>.<listcomp>r   r   ZreadUInt24Arrayr   )	rb   rp   rh   rx   fmtr0  	innerBits	entrySizer{   r-   r@  r.   rf     s"   zVarIdxMapValue.readNc                    s   |d }|}|d  t| d|d@  }d|> d  d| d|d@ d?  }	|j|j|j|jd|	 }
|
 fd	d
|D  d S )Nr<  r^   r   r  r#  r=  r   r>  c                    s    g | ]}|d @ ? | @ B qS )l      r-   )rr   idxrA  rB  r-   r.   r    rC  z(VarIdxMapValue.write.<locals>.<listcomp>)ZsetValuerF  r   r   writeUInt24Arrayr   )rb   r   rh   rx   r   r   rE  r  rF  rG  r   r-   rI  r.   r     s&   zVarIdxMapValue.writer)   rd  r-   r-   r-   r.   r;    s    r;  c                   @   r  )VarDataValuec                 C   s   g }|d }|d }t |d@ }|d@ }|r|j|j}}	n|j|j}}	t||t||}
}|||
 ||	||
  ||krI||d = |S )Nr]   	NumShorts     )boolr   r   r   r)  r  extend)rb   rp   rh   rx   rM   regionCount	wordCount	longWordsZreadBigArrayZreadSmallArrayn1n2r-   r-   r.   rf     s   
zVarDataValue.readNc                 C   s   |d }|d }t |d@ }|d@ }|j|jf|j|jfd| \}	}
t||t||}}|	|d |  |
|||  ||krN|dg||   d S d S )Nr]   rL  rM  rN  r  r   )rO  r   r   r   r)  r  writeSmallArray)rb   r   rh   rx   rM   r   rQ  rR  rS  ZwriteBigArrayrV  rT  rU  r-   r-   r.   r     s   

zVarDataValue.writec                 C   r5  r   r6  r   r-   r-   r.   r     r7  zVarDataValue.xmlWritec                 C   r8  r   r9  r   r-   r-   r.   r     r  zVarDataValue.xmlReadr)   r:  r-   r-   r-   r.   rK    s
    
rK  c                   @   r  )TupleValuesc                 C   s   t d |d S r   )r   ZdecompileDeltas_)rb   rD  rh   r-   r-   r.   rf     rL  zTupleValues.readNc                 C   s   t t|S r)   )bytesr   ZcompileDeltaValues_)rb   r   rh   rx   rM   r   r-   r-   r.   r     r   zTupleValues.writec                 C   r8  r   r9  r   r-   r-   r.   r     r  zTupleValues.xmlReadc                 C   r5  r   r6  r   r-   r-   r.   r     r7  zTupleValues.xmlWriter)   r  r-   r-   r-   r.   rW    s
    
rW  c                   @   sD   e Zd Z	dddddddZdd Zddd	Zd
d Zdd ZdS )	CFF2IndexNrZ   )	itemClassitemConverterClassr=   c                C   s:   t j| |||||d || _|d ur| | _d S d | _d S r  )rY   rc   
_itemClass
_converter)rb   rS   rT   rU   rK   rZ  r[  r=   r-   r-   r.   rc      s   zCFF2Index.__init__c                    s      dkr
g S  dd }jduo dk}|sq| d }g }|d}|d  |D ]6}	||	ks@J |	| }
jd ur[ }||
j |}
njd urgj	|
}
|
|
 |	}q8|S  fdd}| }t|g  }|S )	Nr   c                 S   s   | j | j| j| jd| S )Nr>  rD  )rp   offSizer-   r-   r.   getReadArray9  s   z$CFF2Index.read.<locals>.getReadArrayFrd   r   c                     sH     jd   d   	fdd} | S )Nr   c                    s    |    d}  |d   |d |d  }jd ur6 }||j |}|S jd urBj|}|S )Nr   r   r   )re   rB  r\  rI  
localStater]  rf   )rg   r  r  obj)data_posrh   r^  
offset_posr{   rj   rb   r-   r.   rl   `  s   

z8CFF2Index.read.<locals>.get_read_item.<locals>.read_itemrm   ro   ry   rh   r_  r^  rp   rb   )rb  rc  r{   rj   r.   rq   Z  s   
z%CFF2Index.read.<locals>.get_read_item)r   r   rt   poprB  r\  rI  r`  r]  rf   rQ   r   )rb   rp   rh   rx   r{   rt   r  r]  
lastOffsetrj  r  ra  rq   rl   rz   r-   rd  r.   rf   3  s8   




zCFF2Index.readc                    s   |} t| t|sd S jd ur fdd|D }njd ur1 fddt|D }dd |D }tt|dd}|d }|dk rJdn|d	k rPd
n|dk rVdnd}	|	 jj	j
jd|	 }
|
| |D ]}| qpd S )Nc                    s   g | ]}|  qS r-   rK  rr   r  r  r-   r.   r    r  z#CFF2Index.write.<locals>.<listcomp>c              	      s$   g | ]\}}j  ||qS r-   )r]  r   )rr   rg   r  rh   rb   rx   r   r-   r.   r    s    c                 S   s   g | ]}t |qS r-   )rF  rg  r-   r-   r.   r    s    r   )initialr~     i   r   i   r   r   r>  )r   rF  r\  r]  r   r  r%   r   r   r   rJ  r   rG  )rb   r   rh   rx   rM   r   r]  r  rf  r^  r   r  r-   rh  r.   r   w  s:   


zCFF2Index.writec                 C   sF   | j d ur|   }|d ||| |S | jd ur | j|||S t r)   )r\  rU  r]  r   r   )rb   r   r   rh   ra  r-   r-   r.   r     s   

zCFF2Index.xmlReadc              
   C   sz   | j d urt|D ]\}}|||d|fg| q	d S | jd ur:t|D ]\}}| j|||||d|fg  q$d S t )Nr  )r\  r   rN  r]  r   r   )rb   r   rh   r   rS   r   rg   r  r-   r-   r.   r     s   

zCFF2Index.xmlWriter)   )rO   r   r   rc   rf   r   r   r   r-   r-   r-   r.   rY    s    
D%
rY  c                   @   r   )
LookupFlagc                 C   s   | ||d|fg  g }|d@ r|d |d@ r|d |d@ r(|d |d@ r1|d	 |d
@ r:|d |d@ rG|d|d?   |rQ|d| |  d S )Nr   r   ZrightToLeftr   ZignoreBaseGlyphsr   ZignoreLigaturesrd   ZignoreMarksr#  ZuseMarkFilteringSeti   zmarkAttachmentType[%i]r  )r   rQ   r   r  r   r  r-   r-   r.   r     s"   




zLookupFlag.xmlWriteNr   r-   r-   r-   r.   rk    r   rk  c                       s8   e Zd ZeZ fddZedd Zedd Z  Z	S )
_UInt8Enumc                    s   |  t |||S r)   )	enumClassr+  rf   r   r-  r-   r.   rf     r   z_UInt8Enum.readc                 C   s   t | j| S r)   )r  rm  upperr  r-   r-   r.   r        z_UInt8Enum.fromStringc                 C   s   |  |j S r)   )rm  rS   lowerr  r-   r-   r.   r     ro  z_UInt8Enum.toString)
rO   r   r   rv   rm  rf   r   r   r   r1  r-   r-   r-  r.   rl    s    
rl  c                   @   r   )r!   N)rO   r   r   _ExtendModerm  r-   r-   r-   r.   r!     r   r!   c                   @   r   )r"   N)rO   r   r   _CompositeModerm  r-   r-   r-   r.   r"     r   r"   Zint8Zint16r5   r1   Zuint24r6   Zchar64r  ZOffsetZLOffsetZOffset24Z	TupleList)r[  ZVarCompositeGlyphList)rZ  c                 C      t t| dS N)rK   )r&   r  Cr-   r-   r.   r/   
      c                 C   rs  rt  )r&   r  ru  r-   r-   r.   r/     rw  c                 C   rs  rt  )r&   r  ru  r-   r-   r.   r/     rw  c                 C   rs  rt  )r&   rN   ru  r-   r-   r.   r/     rw  c                 C   rs  rt  )r&   ro  ru  r-   r-   r.   r/     rw  c                 C   rs  rt  )r&   rr  ru  r-   r-   r.   r/     rw  )r(  r>   r?   r@   r    r  r  r  ZOffsetToZ	LOffsetToZLOffset24To){ZfontTools.misc.fixedToolsr   r  r   r  r   r  r   r  r   r4  r   r5  Z%fontTools.ttLib.tables.TupleVariationr   ZfontTools.misc.roundToolsr	   r
   ZfontTools.misc.textToolsr   r   r   r   r   ZfontTools.misc.lazyToolsr   ZfontTools.ttLibr   r  r   r   r   r   r   ZotTablesr   r   r   r   r   r   r   r   r    r!   rq  r"   rr  r#   	itertoolsr$   r%   	functoolsr&   typesr'   r   r  typingr(   ZloggingZ	getLoggerrO   r  r  rX   objectrY   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   rE   rF   r   r   r   r  r  r
  r  r  r"  r&  r(  r2  r3  r9  rI   rG   rN   ro  rr  r9   r7   r8   r:   r0   r  r  r  r  r  r  r(  r*  r;  rK  rW  rY  rk  rl  rH   r-   r-   r-   r.   <module>   sB    8
@h


G"		 ;   5"/503 	
 !"$%