o
    l^Uf'                     @   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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j0d"fed>d6gej1d"fed?d gej2d"fed@dAd)gej3d"fedBdCd)gej4d"fedDdEd)edFdGd)giZ5eej6j7dHf Z8dIej6dJedee8 fdKdLZ9eee8 ee8 gd"f Z:dIej6dJedMe:dee8 fdNdOZ;dPe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
NamedTupleTupleUnionZ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   =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   Z	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_attr1   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   	BaseTabler1   r   r   r   r   r.   ;   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$   r:   lenr   r%   
getGlyphID)r0   r/   r&   Zcoverager   Zcoverage_entryr   r   r   r1   J   s"   
zReorderCoverage.apply)r2   r3   r4   r5   r:   r	   str__annotations___COVERAGE_ATTRr;   r   r6   r   r7   r1   r   r   r   r   r9   B   s
   
 r9   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/   r0   r   r   r   p   s    z#ReorderList.apply.<locals>.<lambda>r   )r-   rD   
isinstancer$   sort)r0   r/   r&   Zlstr   rF   r   r1   m   s   zReorderList.apply)r2   r3   r4   r5   r@   rA   r   r6   r   r7   r1   r   r   r   r   rC   b   s
   
 rC         Z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_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)rM   rN   r   r   r   _bfs_base_table   s   
rS   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   r7   SubTableEntrypopleftr&   ZiterSubTables)rM   rN   rT   rP   pathZcurrentZnew_entriesZsubtable_entryr   r   r   rR      s   

rR   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 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GSUBZGDEFzfont["z"]rU   ZFormat)ZgetGlyphOrderr>   
ValueErrorsetZensureDecompiledr!   keysZsetGlyphOrderrS   tabler&   r=   r,   _REORDER_RULESgetr1   )
r/   rZ   Zold_glyph_orderZ
not_loadedZcoverage_containerstagrY   r&   Zreorder_keyZreorderr   r[   r   reorderGlyphs   s>   	

rc   )>r5   
__author__Z	fontToolsr   ZfontTools.ttLib.tablesr   r   Zotabcr   r   Zdataclassesr   collectionsr   typingr	   r
   r   r   r   r   r   r   r   rB   r@   intr%   r-   r.   r9   rC   Z	SinglePosZPairPosrK   Z
CursivePosZMarkBasePosZ
MarkLigPosZMarkMarkPosZ
ContextPosZChainContextPosZContextSubstZChainContextSubstZReverseChainSingleSubstZ
AttachListZLigCaretListZMarkGlyphSetsDefZMathGlyphInfoZMathItalicsCorrectionInfoZMathTopAccentAttachmentZMathKernInfoZMathVariantsr`   r7   rW   ZSubTablePathrS   ZAddToFrontierFnrR   r6   rc   r   r   r   r   <module>   s    ,


!"#$%&,-.]
	
