o
    i(                     @   s
  d Z dZddlmZ ddlmZ ddlmZ ddl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mZmZ d
Zdeegef dee deee  fddZdededefddZG dd de	ZeddG dd deZeddG dd deZi ejdfe gejdfedd gej dfed!d gej!d"fed#d$d%gej dfe gej"dfed&d gej#dfed'd(d)ed*d+d)gej$dfed'd(d)ed,d-d)gej%dfed.d/d)ed0d1d)gej&dfed2d gej&dfe gej&d3fe gej'dfed4d gej'dfe gej'd3fed5d6ed7d6ed8d6gej(dfed9d gej(dfe gej(d3fe gej)dfed:d gej)dfe gej)d3fed5d6ed7d6ed8d6gej*dfed;d ed5d6ed8d6gej+d"fed<d gej,d"fed=d gej-d"fe gej.d"fed>d6gej/d"fed?d gej0d"fed@dAd)gej1d"fedBdCd)gej2d"fedDdEd)edFdGd)giZ3eej4j5dHf Z6dIej4dJedee6 fdKdLZ7eee6 ee6 gd"f Z8dIej4dJedMe8dee6 fdNdOZ9dPej:dQee fdRdSZ;d"S )TzReorder glyphs in a font.zRod Sheeter    )ttLib)otBase)otTables)ABCabstractmethod)	dataclass)deque)OptionalAnyCallableDequeIterableListTupleZCoverageget_glyph_idglyphsparallel_listc                    sd   |r$t dd t||D  fddd}ttt| \}}||d d < nt | d}||d d < d S )Nc                 s   s    | ]	\}}||fV  qd S N ).0ger   r   l/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/fontTools/ttLib/reorderGlyphs.py	<genexpr>$   s    z_sort_by_gid.<locals>.<genexpr>c                    s    | d S )Nr   r   )tr   r   r   <lambda>%   s    z_sort_by_gid.<locals>.<lambda>key)sortedzipmaplist)r   r   r   	reorderedZsorted_glyphsZsorted_parallel_listr   r   r   _sort_by_gid   s   
r$   valuedotted_attrreturnc                 C   s2   | d}|s	J |r|d}t| |} |s| S )N.r   )splitpopgetattr)r%   r&   Z
attr_namesZ	attr_namer   r   r   _get_dotted_attr/   s   


r,   c                   @   s.   e Zd ZdZedejdejddfddZ	dS )ReorderRulezEA rule to reorder something in a font to match the fonts glyph order.fontr%   r'   Nc                 C   s   d S r   r   )selfr.   r%   r   r   r   apply<   s   zReorderRule.apply)
__name__
__module____qualname____doc__r   r   TTFontr   	BaseTabler0   r   r   r   r   r-   9   s     r-   T)frozenc                   @   sH   e Zd ZU dZdZee ed< eZ	eed< de
jdejddfdd	ZdS )
ReorderCoveragezNReorder a Coverage table, and optionally a list that is sorted parallel to it.Nparallel_list_attrcoverage_attrr.   r%   r'   c                 C   s   t || j}t|tur<d }| jr2t || j}t|tu s%J | j dt|t|jks2J dt|j|j| d S | jrFJ d|  |D ]
}t|j|jd  qHd S )N should be a listzNothing makes sensez2Can't have multiple coverage AND a parallel list; )	r,   r:   typer"   r9   lenr   r$   
getGlyphID)r/   r.   r%   Zcoverager   Zcoverage_entryr   r   r   r0   H   s"   
zReorderCoverage.apply)r1   r2   r3   r4   r9   r	   str__annotations___COVERAGE_ATTRr:   r   r5   r   r6   r0   r   r   r   r   r8   @   s
   
 r8   c                   @   s<   e Zd ZU dZeed< eed< dejdej	ddfdd	Z
dS )
ReorderLista7  Reorder the items within a list to match the updated glyph order.

    Useful when a list ordered by coverage itself contains something ordered by a gid.
    For example, the PairSet table of https://docs.microsoft.com/en-us/typography/opentype/spec/gpos#lookup-type-2-pair-adjustment-positioning-subtable.
    	list_attrr   r.   r%   r'   Nc                    s@   t |j}t|tsJ j d|j fddd d S )Nr;   c                    s     t| jS r   )r>   r+   r   )vr.   r/   r   r   r   n   s    z#ReorderList.apply.<locals>.<lambda>r   )r,   rC   
isinstancer"   sort)r/   r.   r%   lstr   rE   r   r0   k   s   zReorderList.apply)r1   r2   r3   r4   r?   r@   r   r5   r   r6   r0   r   r   r   r   rB   `   s
   
 rB         Value)r9   PairSetNZPairValueRecordZSecondGlyphr   ZEntryExitRecordZMarkCoveragezMarkArray.MarkRecord)r:   r9   ZBaseCoveragezBaseArray.BaseRecordZLigatureCoveragezLigatureArray.LigatureAttachZMark1CoveragezMark1Array.MarkRecordZMark2CoveragezMark2Array.Mark2RecordZ
PosRuleSet   ZChainPosRuleSetZBacktrackCoverage)r:   ZInputCoverageZLookAheadCoverageZ
SubRuleSetZChainSubRuleSetZ
SubstituteZAttachPointZLigGlyphZExtendedShapeCoverageZItalicsCorrectionZTopAccentCoverageZTopAccentAttachmentZMathKernCoverageZMathKernInfoRecordsZVertGlyphCoverageZVertGlyphConstructionZHorizGlyphCoverageZHorizGlyphConstruction.rootroot_accessorc                 c   s    t | |dd E d H  d S )Nc                 S   s
   |  |S r   )extend)frontiernewr   r   r   r      s   
 z!_bfs_base_table.<locals>.<lambda>)_traverse_ot_data)rN   rO   r   r   r   _bfs_base_table   s   
rT   add_to_frontier_fnc                 c   sr    t  }|tj|| f |r7| }|d j}|V  g }| D ]
}|||f  q#||| |sd S d S )N)r   appendr   r6   SubTableEntrypopleftr%   ZiterSubTables)rN   rO   rU   rQ   pathcurrentZnew_entriesZsubtable_entryr   r   r   rS      s   

rS   r.   new_glyph_orderc                    s    }t|t|krtdt| dt| dt|t|kr9tdt|t|  dt|t|    tfdd D }|rStd| | h d	}|D ]5}| v rt| j	d
| dD ] }|d j
}t|t|dd f}t|g D ]}	|	| qqrq^dD ](}|v r| }
|
jjd jj ||
jjd _ fdd|D |
jjd j_qd S )NzNew glyph order contains z glyphs, but font has z glyphszTNew glyph order does not contain the same set of glyphs as the font:
* only in new: z
* only in old: c                 3   s    | ]
}  |s|V  qd S r   )ZisLoaded)r   r   )r.   r   r   r     s    z reorderGlyphs.<locals>.<genexpr>z/Everything should be loaded, following aren't: >   ZMATHZGPOSZGDEFZGSUBzfont["z"]rV   ZFormat)zCFF ZCFF2r   c                    s   i | ]}|  |qS r   )get)r   k)charstringsr   r   
<dictcomp>  s    z!reorderGlyphs.<locals>.<dictcomp>)ZgetGlyphOrderr=   
ValueErrorsetZensureDecompiledr   keysZsetGlyphOrderrT   tabler%   r<   r+   _REORDER_RULESr]   r0   ZcffZtopDictIndexZCharStringsZcharStringscharset)r.   r\   Zold_glyph_orderZ
not_loadedZcoverage_containerstagrZ   r%   Zreorder_keyZreorderZ	cff_tabler   )r_   r.   r   reorderGlyphs   sP   	


rh   )<r4   
__author__Z	fontToolsr   ZfontTools.ttLib.tablesr   r   Zotabcr   r   dataclassesr   collectionsr   typingr	   r
   r   r   r   r   r   rA   r?   intr$   r,   r-   r8   rB   Z	SinglePosZPairPosrL   Z
CursivePosZMarkBasePosZ
MarkLigPosZMarkMarkPosZ
ContextPosZChainContextPosZContextSubstZChainContextSubstZReverseChainSingleSubstZ
AttachListZLigCaretListZMarkGlyphSetsDefZMathGlyphInfoZMathItalicsCorrectionInfoZMathTopAccentAttachmentZMathKernInfoZMathVariantsre   r6   rX   ZSubTablePathrT   ZAddToFrontierFnrS   r5   rh   r   r   r   r   <module>   s    $


!"#$%&,-.]
	
