a
    ZXh(                  @   @   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   b/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/fontTools/ttLib/reorderGlyphs.py	<genexpr>$       z_sort_by_gid.<locals>.<genexpr>c                    s    | d S )Nr   r   )tr   r   r   <lambda>%   r   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J |r,|d}t| |} q| S )N.r   )split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   )selfr0   r%   r   r   r   apply<   s    zReorderRule.apply)
__name__
__module____qualname____doc__r   r   TTFontr   	BaseTabler2   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_attrr/   c                 C   s   t || j}t|turvd }| jrdt || j}t|tu sJJ | j dt|t|jksdJ dt|j|j| n.| jrJ 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;   lenr   r$   
getGlyphID)r1   r0   r%   Zcoverager   Zcoverage_entryr   r   r   r2   H   s"    

zReorderCoverage.apply)r3   r4   r5   r6   r;   r	   str__annotations___COVERAGE_ATTRr<   r   r7   r   r8   r2   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   Nr/   c                    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r0   r1   r   r   r   n   r   z#ReorderList.apply.<locals>.<lambda>r   )r-   rE   
isinstancer#   sort)r1   r0   r%   lstr   rG   r   r2   k   s    zReorderList.apply)r3   r4   r5   r6   rA   rB   r   r7   r   r8   r2   r   r   r   r   rD   `   s   
rD         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)rP   rQ   r   r   r   _bfs_base_table   s    
rV   )rP   rQ   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   r8   SubTableEntrypopleftr%   ZiterSubTables)rP   rQ   rW   rS   pathcurrentZnew_entriesZsubtable_entryr   r   r   rU      s    
rU   )r0   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| | h d	}|D ]l}| v rt| j	d
| dD ]B}|d j
}t|t|dd f}t|g D ]}	|	| qqqdD ]T}|v r.| }
|
jjd jj ||
jjd _ fdd|D |
jjd j_q.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   )r0   r   r   r     r   z reorderGlyphs.<locals>.<genexpr>z/Everything should be loaded, following aren't: >   ZGPOSZGSUBZGDEFZMATHzfont["z"]rX   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setGlyphOrderrV   tabler%   r>   r+   _REORDER_RULESr_   r2   ZcffZtopDictIndexZCharStringsZcharStringscharset)r0   r^   Zold_glyph_orderZ
not_loadedZcoverage_containerstagr\   r%   Zreorder_keyZreorderZ	cff_tabler   )ra   r0   r   reorderGlyphs   sH    	



rj   )<r6   
__author__Z	fontToolsr   ZfontTools.ttLib.tablesr   r   Zotabcr   r   Zdataclassesr   collectionsr   typingr	   r
   r   r   r   r   r   rC   rA   intr$   r-   r.   r:   rD   Z	SinglePosZPairPosrN   Z
CursivePosZMarkBasePosZ
MarkLigPosZMarkMarkPosZ
ContextPosZChainContextPosZContextSubstZChainContextSubstZReverseChainSingleSubstZ
AttachListZLigCaretListZMarkGlyphSetsDefZMathGlyphInfoZMathItalicsCorrectionInfoZMathTopAccentAttachmentZMathKernInfoZMathVariantsrg   r8   rZ   ZSubTablePathrV   ZAddToFrontierFnrU   r7   rj   r   r   r   r   <module>   s   $

	]	