a
    ¥Q•h0Y  ã                
   @   s€  d Z ddlm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ZddlmZ ddlZe e¡ZG d	d
„ d
eƒZdZdZddddddddddœ	Zefdd„ZG dd„ deƒZdZe e¡ZdZe e¡ZdZe e¡Z dZ!e e!¡Z"dZ#e e#¡Z$G d d!„ d!eƒZ%G d"d#„ d#e%ƒZ&G d$d%„ d%e%ƒZ'G d&d'„ d'ƒZ(d(d)„ Z)d*d+„ Z*d,d-„ Z+ed.k�r|ddl,Z,ddl-Z-e, .e- /¡ j0¡ dS )/a¯  ttLib/sfnt.py -- low-level module to deal with the sfnt file format.

Defines two public classes:

- SFNTReader
- SFNTWriter

(Normally you don't have to use these classes explicitly; they are
used automatically by ttLib.TTFont.)

The reading and writing of sfnt files is separated in two distinct
classes, since whenever the number of tables changes or whenever
a table's length changes you need to rewrite the whole file anyway.
é    )ÚBytesIO)ÚSimpleNamespace)ÚTag)Ússtruct)Ú
TTLibErrorÚTTLibFileIsCollectionErrorN)ÚOrderedDictc                   @   sZ   e Zd Zdd„ Zddd„Zdd„ ZeZd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )Ú
SFNTReaderc                 O   s^   |rT| t u rT|d }| d¡ t| d¡ƒ}| d¡ |dkrTddlm} t |¡S t | ¡S )zmReturn an instance of the SFNTReader sub-class which is compatible
        with the input file type.
        r   é   ZwOF2)ÚWOFF2Reader)r	   Úseekr   ÚreadÚfontTools.ttLib.woff2r   ÚobjectÚ__new__)ÚclsÚargsÚkwargsÚinfileÚsfntVersionr   © r   úM/var/www/sistema_ama/venv/lib/python3.9/site-packages/fontTools/ttLib/sfnt.pyr      s    


zSFNTReader.__new__r   éÿÿÿÿc                 C   sÖ  || _ || _d | _d | _t| _| j  d¡ | j  d¡| _| j  d¡ | jdkrÌt	| j ƒ}|j
}d|  krr|k s„n td|d  ƒ‚|| _
| j  |j| ¡ | j  t¡}t|ƒtkr¼tdƒ‚t t|| ¡ nz| jdk�rd| _t| _| j  t¡}t|ƒtk�rtd	ƒ‚t t|| ¡ n0| j  t¡}t|ƒtk�r8td
ƒ‚t t|| ¡ t| jƒ| _| jdv�rftdƒ‚i }t| jƒD ],}|  ¡ }	|	 | j ¡ t|	jƒ}
|	||
< �qttt| ¡ dd„ d�ƒ| _| jdk�rÒt| ƒ| _d S )Nr   r
   s   ttcfz2specify a font number between 0 and %d (inclusive)é   ú'Not a Font Collection (not enough data)ó   wOFFÚwoffz!Not a WOFF font (not enough data)z1Not a TrueType or OpenType font (not enough data))ú   ZOTTOÚtruez1Not a TrueType or OpenType font (bad sfntVersion)c                 S   s
   | d j S )Nr   )Úoffset)Úir   r   r   Ú<lambda>]   ó    z%SFNTReader.__init__.<locals>.<lambda>)Úkey) ÚfileÚcheckChecksumsÚflavorÚ
flavorDataÚSFNTDirectoryEntryÚDirectoryEntryr   r   r   ÚreadTTCHeaderÚnumFontsr   ÚoffsetTableÚsfntDirectorySizeÚlenr   r   ÚunpackÚsfntDirectoryFormatÚWOFFDirectoryEntryÚwoffDirectorySizeÚwoffDirectoryFormatr   ÚrangeÚ	numTablesÚfromFileÚtagr   ÚsortedÚitemsÚtablesÚWOFFFlavorData)Úselfr$   r%   Ú
fontNumberÚheaderr+   Údatar:   r    Úentryr7   r   r   r   Ú__init__/   s\    

ÿÿ
zSFNTReader.__init__c                 C   s
   || j v S ©N©r:   ©r<   r7   r   r   r   Úhas_keyc   s    zSFNTReader.has_keyc                 C   s
   | j  ¡ S rB   )r:   Úkeys©r<   r   r   r   rF   h   s    zSFNTReader.keysc                 C   sŽ   | j t|ƒ }| | j¡}| jrŠ|dkrJt|dd… d |dd…  ƒ}nt|ƒ}| jdkrt||jksŠJ d| ƒ‚n||jkrŠt d|¡ |S )zFetch the raw table data.ÚheadNé   ó       é   r   zbad checksum for '%s' table)	r:   r   ÚloadDatar$   r%   ÚcalcChecksumÚcheckSumÚlogÚwarning)r<   r7   r@   r?   Úchecksumr   r   r   Ú__getitem__k   s    "

zSFNTReader.__getitem__c                 C   s   | j t|ƒ= d S rB   )r:   r   rD   r   r   r   Ú__delitem__}   s    zSFNTReader.__delitem__c                 C   s   | j  ¡  d S rB   )r$   ÚcloserG   r   r   r   rT   €   s    zSFNTReader.closec                 C   s@   t | jtƒr| jS | j ¡ }|d= | jj|d< | j ¡ |d< |S )Nr$   Ú	_filenameÚ_filepos)Ú
isinstancer$   r   Ú__dict__ÚcopyÚnameÚtell©r<   Ústater   r   r   Ú__getstate__‰   s    
zSFNTReader.__getstate__c                 C   s<   d|vr,t | d¡dƒ| _| j | d¡¡ | j |¡ d S )Nr$   rU   ÚrbrV   )ÚopenÚpopr$   r   rX   Úupdater\   r   r   r   Ú__setstate__•   s    zSFNTReader.__setstate__N)r   r   )Ú__name__Ú
__module__Ú__qualname__r   rA   rE   Ú__contains__rF   rR   rS   rT   r^   rc   r   r   r   r   r	      s   
4	r	   é   Fr   é   é   rI   é
   é   é   é2   éd   )	r   é   ri   r
   rj   rh   é   rI   é	   c                 C   sd   d|  krdks"n t d| ƒ‚tr.|dkrDddlm} || |ƒS ddlm} || t| d�S dS )aE  Compress 'data' to Zlib format. If 'USE_ZOPFLI' variable is True,
    zopfli is used instead of the zlib module.
    The compression 'level' must be between 0 and 9. 1 gives best speed,
    9 gives best compression (0 gives no compression at all).
    The default value is a compromise between speed and compression (6).
    r   rr   zBad compression level: %s)Úcompress)ZnumiterationsN)Ú
ValueErrorÚ
USE_ZOPFLIÚzlibrs   Zzopfli.zlibÚZOPFLI_LEVELS)r?   Úlevelrs   r   r   r   rs   ³   s    
rs   c                   @   sV   e Zd Zdd„ Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú
SFNTWriterc                 O   sb   d}|rd|v r|d }n|r2t |ƒdkr2|d }| tu rX|dkrXddlm} t |¡S t | ¡S )zpReturn an instance of the SFNTWriter sub-class which is compatible
        with the specified 'flavor'.
        Nr&   ri   Úwoff2r   )ÚWOFF2Writer)r.   ry   r   r{   r   r   )r   r   r   r&   r{   r   r   r   r   Ç   s    

zSFNTWriter.__new__r   Nc                 C   sø   || _ || _t|ƒ| _|| _|| _| jdkrTt| _t| _	t
| _d| _t|t  | _nH| jrhJ d| j ƒ‚t| _t| _	t| _ddlm} ||dƒ\| _| _| _| j  ¡ | _| j| j	 || jj  | _| j  | j¡ | j  d| j| j  ¡   ¡ tƒ | _d S )Nr   ZwOFFúUnknown flavor '%s'r   ©ÚgetSearchRangeé   ó    )r$   r5   r   r   r&   r'   r3   ÚdirectoryFormatr2   ÚdirectorySizer1   r)   Ú	signaturer-   ÚsfntDirectoryEntrySizeÚorigNextTableOffsetr0   r(   ÚfontTools.ttLibr~   ÚsearchRangeÚentrySelectorÚ
rangeShiftr[   ÚdirectoryOffsetÚ
formatSizeÚnextTableOffsetr   Úwriter   r:   )r<   r$   r5   r   r&   r'   r~   r   r   r   rA   Ù   s<    


ÿÿÿ
þÿzSFNTWriter.__init__c                 C   s$   || j v rtd| ƒ‚|| j |< d S )Núcannot rewrite '%s' table)r:   r   )r<   r7   r@   r   r   r   ÚsetEntry
  s    
zSFNTWriter.setEntryc                 C   sü   || j v rtd| ƒ‚|  ¡ }||_| j|_|dkrdt|dd… d |dd…  ƒ|_|| _d|_	n
t|ƒ|_| 
| j|¡ | jdkr¦| j|_|  j|jd	 d
@ 7  _| j|jd	 d
@  | _| j d| j| j ¡   ¡ | j| j ¡ ksìJ ‚|  ||¡ dS )zWrite raw table data to disk.rŽ   rH   NrI   rJ   rK   Tr   ri   éüÿÿÿr€   )r:   r   r)   r7   rŒ   r   rM   rN   Ú	headTableÚuncompressedÚsaveDatar$   r&   r…   Ú
origOffsetÚ
origLengthÚlengthr�   r[   r�   )r<   r7   r?   r@   r   r   r   Ú__setitem__  s$    
"

zSFNTWriter.__setitem__c                 C   s
   | j | S rB   rC   rD   r   r   r   rR   .  s    zSFNTWriter.__getitem__c           
      C   s€  t | j ¡ ƒ}t|ƒ| jkr2td| jt|ƒf ƒ‚| jdk�rìd| _d| _d| _	|  j	dt|ƒ 7  _	|D ] \}}|  j	|j
d d@ 7  _	qj| jr˜| jntƒ }|jd	urÄ|jd	urÄ|j| _|j| _n6t| d
ƒrît d| jdd… ¡\| _| _nd | _| _|j�rJt|jƒ| _| j dd¡ | j ¡ | _t|jƒ}t|ƒ| _| j |¡ nd | _ | _| _|j�rÄ| j dd¡ | j ¡ }|d d@ }| j d||  ¡ | j ¡ | _t|jƒ| _| j |j¡ nd | _| _| j dd¡ | j ¡ | _n| j�rJ d| j ƒ‚t  !| j"| ¡}| j | j#| j$ ¡ d}	|D ]$\}}|dk�rBd}	|| %¡  }�q,|	�rb|  &|¡ | j | j#¡ | j |¡ d	S )zTAll tables must have been written to disk. Now write the
        directory.
        z-wrong number of tables; expected %d, found %dr   r   r   rK   r   ri   r�   Nr‘   z>HHr
   rI   rp   r€   r|   rH   r   )'r8   r:   r9   r.   r5   r   r&   rƒ   ÚreservedZtotalSfntSizer•   r'   r;   ÚmajorVersionÚminorVersionÚhasattrÚstructr/   r‘   ÚmetaDataÚmetaOrigLengthr$   r   r[   Ú
metaOffsetrs   Ú
metaLengthr�   ÚprivDataÚ
privOffsetÚ
privLengthr–   r   Úpackr�   rŠ   r‚   ÚtoStringÚwriteMasterChecksum)
r<   r:   r7   r@   r?   ZcompressedMetaDataÚoffZ	paddedOffÚ	directoryZseenHeadr   r   r   rT   1  sl    ÿÿ

ÿ




zSFNTWriter.closec                 C   s  t | j ¡ ƒ}g }tt|ƒƒD ]}| | j||  j¡ q| jtkrÂddl	m
} || jdƒ\| _| _| _t t| ¡}t| j ¡ ƒ}|D ]:\}}tƒ }	|j|	_|j|	_|j|	_|j|	_||	 ¡  }q†tt| jƒt  }
|
t|ƒksäJ ‚| t|ƒ¡ t|ƒd@ }d| d@ }|S )Nr   r}   r   ì   ÿÿ l   º/ac )Úlistr:   rF   r4   r.   ÚappendrN   r)   r(   r†   r~   r5   r‡   rˆ   r‰   r   r¤   r0   r8   r9   r7   r”   r   r•   r–   r¥   r-   r„   rM   Úsum)r<   r¨   ÚtagsZ	checksumsr    r~   r:   r7   r@   Z	sfntEntryZdirectory_endrQ   Úchecksumadjustmentr   r   r   Ú_calcMasterChecksumx  s0    
ÿzSFNTWriter._calcMasterChecksumc                 C   s:   |   |¡}| j | jd jd ¡ | j t d|¡¡ d S )NrH   rI   z>L)r¯   r$   r   r:   r   r�   rœ   r¤   )r<   r¨   r®   r   r   r   r¦   ™  s    
zSFNTWriter.writeMasterChecksumc                 C   s   dS ©NFr   rG   r   r   r   ÚreordersTablesŸ  s    zSFNTWriter.reordersTables)r   NN)rd   re   rf   r   rA   r�   r—   rR   rT   r¯   r¦   r±   r   r   r   r   ry   Æ   s      ú
1G!ry   a  
		> # big endian
		TTCTag:                  4s # "ttcf"
		Version:                 L  # 0x00010000 or 0x00020000
		numFonts:                L  # number of fonts
		# OffsetTable[numFonts]: L  # array with offsets from beginning of file
		# ulDsigTag:             L  # version 2.0 only
		# ulDsigLength:          L  # version 2.0 only
		# ulDsigOffset:          L  # version 2.0 only
zä
		> # big endian
		sfntVersion:    4s
		numTables:      H    # number of tables
		searchRange:    H    # (max2 <= numTables)*16
		entrySelector:  H    # log2(max2 <= numTables)
		rangeShift:     H    # numTables*16-searchRange
zc
		> # big endian
		tag:            4s
		checkSum:       L
		offset:         L
		length:         L
ab  
		> # big endian
		signature:      4s   # "wOFF"
		sfntVersion:    4s
		length:         L    # total woff file size
		numTables:      H    # number of tables
		reserved:       H    # set to 0
		totalSfntSize:  L    # uncompressed size
		majorVersion:   H    # major version of WOFF file
		minorVersion:   H    # minor version of WOFF file
		metaOffset:     L    # offset to metadata block
		metaLength:     L    # length of compressed metadata
		metaOrigLength: L    # length of uncompressed metadata
		privOffset:     L    # offset to private data block
		privLength:     L    # length of private data block
zº
		> # big endian
		tag:            4s
		offset:         L
		length:         L    # compressed length
		origLength:     L    # original length
		checkSum:       L    # original checksum
c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )r)   c                 C   s
   d| _ d S r°   )r’   rG   r   r   r   rA   ç  s    zDirectoryEntry.__init__c                 C   s   t  | j| | j¡| ¡ d S rB   )r   r/   Úformatr   r‹   )r<   r$   r   r   r   r6   ê  s    zDirectoryEntry.fromFilec                 C   s   t  | j|| ¡ d S rB   )r   r/   r²   )r<   Ústrr   r   r   Ú
fromStringí  s    zDirectoryEntry.fromStringc                 C   s   t  | j| ¡S rB   )r   r¤   r²   rG   r   r   r   r¥   ð  s    zDirectoryEntry.toStringc                 C   s:   t | dƒr"d| jj| jt| ƒf S d| jjt| ƒf S d S )Nr7   z<%s '%s' at %x>z
<%s at %x>)r›   Ú	__class__rd   r7   ÚidrG   r   r   r   Ú__repr__ó  s    
zDirectoryEntry.__repr__c                 C   sD   |  | j¡ | | j¡}t|ƒ| jks*J ‚t| jdƒr@|  |¡}|S )NÚ
decodeData)r   r   r   r–   r.   r›   rµ   r¸   ©r<   r$   r?   r   r   r   rL   ù  s    
zDirectoryEntry.loadDatac                 C   s:   t | jdƒr|  |¡}t|ƒ| _| | j¡ | |¡ d S )NÚ
encodeData)r›   rµ   rº   r.   r–   r   r   r�   r¹   r   r   r   r“     s
    

zDirectoryEntry.saveDatac                 C   s   |S rB   r   )r<   ÚrawDatar   r   r   r¸     s    zDirectoryEntry.decodeDatac                 C   s   |S rB   r   )r<   r?   r   r   r   rº     s    zDirectoryEntry.encodeDataN)rd   re   rf   rA   r6   r´   r¥   r·   rL   r“   r¸   rº   r   r   r   r   r)   æ  s   r)   c                   @   s   e Zd ZeZeZdS )r(   N)rd   re   rf   ÚsfntDirectoryEntryFormatr²   r„   r‹   r   r   r   r   r(     s   r(   c                       s4   e Zd ZeZeZ‡ fdd„Zdd„ Zdd„ Z	‡  Z
S )r1   c                    s"   t t| ƒ ¡  ttdƒst| _d S )NÚzlibCompressionLevel)Úsuperr1   rA   r›   ÚZLIB_COMPRESSION_LEVELr½   rG   ©rµ   r   r   rA     s    
zWOFFDirectoryEntry.__init__c                 C   sJ   dd l }| j| jkr|}n,| j| jk s*J ‚| |¡}t|ƒ| jksFJ ‚|S ©Nr   )rv   r–   r•   Ú
decompressr.   )r<   r»   rv   r?   r   r   r   r¸   #  s    
zWOFFDirectoryEntry.decodeDatac                 C   sP   t |ƒ| _| jst|| jƒ}| js0t |ƒ| jkr>|}| j| _n|}t |ƒ| _|S rB   )r.   r•   r’   rs   r½   r–   )r<   r?   ZcompressedDatar»   r   r   r   rº   .  s    


zWOFFDirectoryEntry.encodeData)rd   re   rf   ÚwoffDirectoryEntryFormatr²   ÚwoffDirectoryEntrySizer‹   rA   r¸   rº   Ú__classcell__r   r   rÀ   r   r1     s
   r1   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )r;   r   Nc                 C   sÀ   d | _ d | _d | _d | _|r¼|j | _ |j| _|jr‚|j |j¡ |j |j¡}t	|ƒ|jks`J ‚|  
|¡}t	|ƒ|jks|J ‚|| _|jr¼|j |j¡ |j |j¡}t	|ƒ|jks¶J ‚|| _d S rB   )r™   rš   r�   r¡   r    r$   r   rŸ   r   r.   Ú_decompressrž   r£   r¢   )r<   Úreaderr»   r?   r   r   r   rA   ?  s&    
zWOFFFlavorData.__init__c                 C   s   dd l }| |¡S rÁ   )rv   rÂ   )r<   r»   rv   r   r   r   rÆ   T  s    zWOFFFlavorData._decompress)N)rd   re   rf   ZFlavorrA   rÆ   r   r   r   r   r;   <  s   
r;   c                 C   sŠ   t | ƒd }|r | dd|  7 } d}d}|d dks8J ‚tdt | ƒ|ƒD ]<}| ||| … }t dt |ƒd  |¡}|t|ƒ d@ }qH|S )a,  Calculate the checksum for an arbitrary block of data.

    If the data length is not a multiple of four, it assumes
    it is to be padded with null byte.

            >>> print(calcChecksum(b"abcd"))
            1633837924
            >>> print(calcChecksum(b"abcdxyz"))
            3655064932
    r
   r€   r   i   ú>%dLr©   )r.   r4   rœ   r/   r¬   )r?   Ú	remainderÚvalueZ	blockSizer    ÚblockZlongsr   r   r   rM   Z  s    rM   c                 C   sž   |   d¡ |  t¡}t|ƒtkr(tdƒ‚tƒ }t t||¡ |j	dkrNtdƒ‚|j
dksp|j
dkspJ d|j
 ƒ‚t d|j |  |jd	 ¡¡|_|j
dkrš|S )
Nr   r   ÚttcfzNot a Font Collectioné   i   zunrecognized TTC version 0x%08xrÈ   r
   )r   r   ÚttcHeaderSizer.   r   r   r   r/   ÚttcHeaderFormatÚTTCTagÚVersionrœ   r+   r,   )r$   r?   r<   r   r   r   r*   r  s     


ÿÿ
r*   c                 C   sf   t ƒ }d|_d|_||_|  d¡ |  t t|¡¡ |  	¡ }|  t
jd|j gdg|j ¢R Ž ¡ |S )NrÌ   rÍ   r   rÈ   )r   rÐ   rÑ   r+   r   r�   r   r¤   rÏ   r[   rœ   )r$   r+   r<   r   r   r   r   ÚwriteTTCHeader†  s    
&rÒ   Ú__main__)1Ú__doc__Úior   Útypesr   ÚfontTools.misc.textToolsr   ÚfontTools.miscr   r†   r   r   rœ   Úcollectionsr   ÚloggingÚ	getLoggerrd   rO   r   r	   r¿   ru   rw   rs   ry   rÏ   ÚcalcsizerÎ   r0   r-   r¼   r„   r3   r2   rÃ   rÄ   r)   r(   r1   r;   rM   r*   rÒ   ÚsysÚdoctestÚexitÚtestmodÚfailedr   r   r   r   Ú<module>   s^   
 ö `
	


	
)(
