U
    #vhL(                  @   @   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eegef ee eee  dddZeeed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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d,d-d%gej&dfed.dgej&dfe gej&d/fe gej'dfed0dgej'dfe gej'd/fed1d2ed3d2ed4d2gej(dfed5dgej(dfe gej(d/fe gej)dfed6dgej)dfe gej)d/fed1d2ed3d2ed4d2gej*dfed7ded1d2ed4d2gej+dfed8dgej,dfed9dgej-dfe gej.dfed:d2gej/dfed;dgej0dfed<d=d%gej1dfed>d?d%gej2dfed@dAd%edBdCd%giZ3eej4j5dDf Z6ej4eee6 dEdFdGZ7eee6 ee6 gdf Z8ej4ee8ee6 dHdIdJZ9ej:ee dKdLdMZ;dS )Nz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   |rHt 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   A/tmp/pip-unpacked-wheel-1ufboor8/fontTools/ttLib/reorderGlyphs.py	<genexpr>$   s     z_sort_by_gid.<locals>.<genexpr>c                    s    | d S )Nr   r   )tr   r   r   <lambda>%       z_sort_by_gid.<locals>.<lambda>key)sortedzipmaplist)r   r   r   Z	reorderedZsorted_glyphsZsorted_parallel_listr   r   r   _sort_by_gid   s    
r$   )valuedotted_attrreturnc                 C   s0   | d}|st|r,|d}t| |} q| S )N.r   )splitAssertionErrorpopgetattr)r%   r&   Z
attr_names	attr_namer   r   r   _get_dotted_attr/   s    

r.   c                   @   s*   e Zd ZdZeejejddddZ	dS )ReorderRulezEA rule to reorder something in a font to match the fonts glyph order.Nfontr%   r'   c                 C   s   d S r   r   )selfr1   r%   r   r   r   apply<   s    zReorderRule.apply)
__name__
__module____qualname____doc__r   r   TTFontr   	BaseTabler3   r   r   r   r   r/   9   s   r/   T)frozenc                   @   sD   e Zd ZU dZdZee ed< eZ	eed< e
jejddddZdS )ReorderCoveragezNReorder a Coverage table, and optionally a list that is sorted parallel to it.Nparallel_list_attrcoverage_attrr0   c                 C   s   t || j}t|tk	rvd }| jrdt || j}t|tksJt| j dt|t|jksdtdt|j	|j| n.| jrtd|  |D ]}t|j	|jd  qd S )N should be a listzNothing makes sensez2Can't have multiple coverage AND a parallel list; )
r.   r=   typer#   r<   r*   lenr   r$   
getGlyphID)r2   r1   r%   Zcoverager   Zcoverage_entryr   r   r   r3   H   s"    

zReorderCoverage.apply)r4   r5   r6   r7   r<   r	   str__annotations___COVERAGE_ATTRr=   r   r8   r   r9   r3   r   r   r   r   r;   @   s   
r;   c                   @   s8   e Zd ZU dZeed< eed< ejej	dd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   Nr0   c                    s@   t |j}t|ts&tj d|j fddd d S )Nr>   c                    s     t| jS r   )rA   r,   r   )vr1   r2   r   r   r   n   r   z#ReorderList.apply.<locals>.<lambda>r   )r.   rF   
isinstancer#   r*   sort)r2   r1   r%   lstr   rH   r   r3   k   s    zReorderList.apply)r4   r5   r6   r7   rB   rC   r   r8   r   r9   r3   r   r   r   r   rE   `   s   
rE         Value)r<   PairSetNZPairValueRecordZSecondGlyphr   ZEntryExitRecordZMarkCoveragezMarkArray.MarkRecord)r=   r<   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_accessorr'   c                 c   s   t | |dd E d H  d S )Nc                 S   s
   |  |S r   )extend)frontiernewr   r   r   r      r   z!_bfs_base_table.<locals>.<lambda>)_traverse_ot_data)rQ   rR   r   r   r   _bfs_base_table   s
      rW   )rQ   rR   add_to_frontier_fnr'   c                 c   sj   t  }|tj|| f |rf| }|d j}|V  g }| D ]}|||f  qD||| qd S )N)r   appendr   r9   SubTableEntrypopleftr%   ZiterSubTables)rQ   rR   rX   rT   pathcurrentZnew_entriesZsubtable_entryr   r   r   rV      s    
rV   )r1   new_glyph_orderc                    s    }t|t|kr6tdt| dt| dt|t|krrtdt|t|  dt|t|    tfdd D }|rtd| | d	d
ddh}|D ]l}| krt| j	d| dD ]B}|d j
}t|t|dd f}t|g D ]}	|	| qqqdkr|d }
|
jjd jj ||
jjd _ fdd|D |
jjd j_d 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   )r1   r   r   r     s     
 z reorderGlyphs.<locals>.<genexpr>z/Everything should be loaded, following aren't: ZGDEFZGPOSZGSUBZMATHzfont["z"]rY   ZFormatzCFF r   c                    s   i | ]}|  |qS r   )get)r   k)charstringsr   r   
<dictcomp>  s     z!reorderGlyphs.<locals>.<dictcomp>)ZgetGlyphOrderr@   
ValueErrorsetZensureDecompiledr    keysZsetGlyphOrderrW   tabler%   r?   r,   _REORDER_RULESr`   r3   ZcffZtopDictIndexZCharStringsZcharStringscharset)r1   r_   Zold_glyph_orderZ
not_loadedZcoverage_containerstagr]   r%   Zreorder_keyZreorderZ	cff_tabler   )rb   r1   r   reorderGlyphs   s:    &	



rk   )<r7   
__author__Z	fontToolsr   ZfontTools.ttLib.tablesr   r   Zotabcr   r   dataclassesr   collectionsr   typingr	   r
   r   r   r   r   r   rD   rB   intr$   r.   r/   r;   rE   Z	SinglePosZPairPosrO   Z
CursivePosZMarkBasePosZ
MarkLigPosZMarkMarkPosZ
ContextPosZChainContextPosZContextSubstZChainContextSubstZReverseChainSingleSubstZ
AttachListZLigCaretListZMarkGlyphSetsDefZMathGlyphInfoZMathItalicsCorrectionInfoZMathTopAccentAttachmentZMathKernInfoZMathVariantsrh   r9   r[   ZSubTablePathrW   ZAddToFrontierFnrV   r8   rk   r   r   r   r   <module>   s  $

  
 
   
   	   
   
  
   
  
 
  
  ] 	  