U
    wh                     @   sH   d Z ddlmZ ddlmZ ddlmZmZmZ G dd deejZ	dS )	a]   TSI{0,1,2,3,5} are private tables used by Microsoft Visual TrueType (VTT)
tool to store its hinting source data.

TSI1 contains the text of the glyph programs in the form of low-level assembly
code, as well as the 'extra' programs 'fpgm', 'ppgm' (i.e. 'prep'), and 'cvt'.

See also https://learn.microsoft.com/en-us/typography/tools/vtt/tsi-tables
   )DefaultTable    )LogMixin)strjointobytestostrc                   @   s>   e Zd ZdddddZdZdd Zd	d
 Zdd Zdd ZdS )table_T_S_I__1ZppgmcvtreservedZfpgm)i  i  i  i  ZTSI0c                 C   sz  t |}|| j }t|j|jfdD ]N\}}i }t|D ]"\}\}	}
}|rZ| j|	 }n
||	}||kr~| j	d|  q:|
dk rn|
dkr|t |d k}|r|r|}q|jd d }n||d  d }||kst
d||kr| j	d|  |}|| }
ndd	lm} |d
||
f ||||
  }t ||
ksFt
t|dd}|r:|||< q:|rn|| _q$|| _q$d S )N)FTz$textOffset > totalLength; %r skipped   r   r      zentries not sorted by offsetz*nextTextOffset > totalLength; %r truncated)ttLibz&%r textLength (%d) must not be > 32768utf-8encoding)len
indextablezipindicesextra_indices	enumerateextrasZgetGlyphNamelogwarningAssertionErrorZ	fontToolsr   Z
TTLibErrorr   extraProgramsglyphPrograms)selfdatattFontZtotalLengthr   r   ZisExtraZprogramsiZglyphID
textLengthZ
textOffsetnameZisLastZnextTextOffsetr   text r$   C/tmp/pip-unpacked-wheel-cdq4c5o5/fontTools/ttLib/tables/T_S_I__1.py	decompile   sT    

 




ztable_T_S_I__1.decompilec                 C   sZ  t | dsi | _i | _d}|| j }| }g }tt|D ]r}t|d rT|d }|| }|| jkrzt| j| dd}nd}t|}	|	dkrd}	|||	t|f || }q<g }
t	| j
 }tt|D ]z}t|d r|d }|| \}}|| jkrt| j| dd}nd}t|}	|	dkr,d}	|
||	t|f || }q|||
 |S )Nr       r      r   r   r   )hasattrr   r   r   ZgetGlyphOrderranger   r   appendsortedr   itemsset)r   r   r   r   Z
glyphNamesr   r    r"   r#   r!   r   codescoder$   r$   r%   compileU   sJ    





ztable_T_S_I__1.compilec                 C   s   t | j }|  |D ]^}| j| }|s.q|jd|d |  ||dd |  |d |  |  qt | j }|D ]^}| j| }|sq|jd|d |  ||dd |  |d |  |  qd S )NglyphProgram)r"   
extraProgram)	r,   r   keysnewlineZbegintagZwrite_noindentreplaceZendtagr   )r   writerr   namesr"   r#   Zextra_namesr$   r$   r%   toXML   s2    




ztable_T_S_I__1.toXMLc                 C   sp   t | dsi | _i | _t|ddd}d|dd }|dkrV|| j|d < n|dkrl|| j|d < d S )	Nr   r3   r4   r   r2   r"   r5   )r)   r   r   r   r8   splitjoin)r   r"   attrscontentr   linesr#   r$   r$   r%   fromXML   s    
ztable_T_S_I__1.fromXMLN)	__name__
__module____qualname__r   r   r&   r1   r;   rB   r$   r$   r$   r%   r      s   A-r   N)
__doc__ r   ZfontTools.misc.loggingToolsr   ZfontTools.misc.textToolsr   r   r   r   r$   r$   r$   r%   <module>   s   	