a
    èÝEb÷K  ã                
   @   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
 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�rxddl+Z+ddl,Z,e+ -e, .¡ j/¡ 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)Ú
TTLibErrorN)Ú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 )zb Return 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   úT/home/ja/django-apps/lartica_env/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>Y   ó    z%SFNTReader.__init__.<locals>.<lambda>)Úkey)ÚfileÚcheckChecksumsÚflavorÚ
flavorDataÚSFNTDirectoryEntryÚDirectoryEntryr   r   r   ÚreadTTCHeaderÚnumFontsr   ÚoffsetTableÚsfntDirectorySizeÚlenr   ÚunpackÚsfntDirectoryFormatÚWOFFDirectoryEntryÚwoffDirectorySizeÚwoffDirectoryFormatr   ÚrangeÚ	numTablesÚfromFileÚtagr   ÚsortedÚitemsÚtablesÚWOFFFlavorData)Úselfr#   r$   Z
fontNumberÚheaderr*   Údatar9   r   Úentryr6   r   r   r   Ú__init__.   sT    


zSFNTReader.__init__c                 C   s
   || j v S ©N©r9   ©r;   r6   r   r   r   Úhas_key_   s    zSFNTReader.has_keyc                 C   s
   | j  ¡ S r@   )r9   Úkeys©r;   r   r   r   rD   d   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)	r9   r   ÚloadDatar#   r$   ÚcalcChecksumÚcheckSumÚlogÚwarning)r;   r6   r>   r=   Úchecksumr   r   r   Ú__getitem__g   s    "

zSFNTReader.__getitem__c                 C   s   | j t|ƒ= d S r@   )r9   r   rB   r   r   r   Ú__delitem__y   s    zSFNTReader.__delitem__c                 C   s   | j  ¡  d S r@   )r#   ÚcloserE   r   r   r   rR   |   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#   rS   ÚrbrT   )ÚopenÚpopr#   r   rV   ÚupdaterZ   r   r   r   Ú__setstate__‘   s    zSFNTReader.__setstate__N)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r?   rC   Ú__contains__rD   rP   rQ   rR   r\   ra   r   r   r   r   r      s   
1	r   é   Fr   é   é   rG   é
   é   é   é2   éd   )	r   é   rg   r	   rh   rf   é   rG   é	   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 )a7   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   rp   zBad compression level: %s)Úcompress)ZnumiterationsN)Ú
ValueErrorÚ
USE_ZOPFLIÚzlibrq   Zzopfli.zlibÚZOPFLI_LEVELS)r=   Úlevelrq   r   r   r   rq   ¯   s    
rq   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 )ze Return an instance of the SFNTWriter sub-class which is compatible
		with the specified 'flavor'.
		Nr%   rg   Zwoff2r   )ÚWOFF2Writer)r-   rw   r   rx   r   r   )r   r   r   r%   rx   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#   r4   r   r   r%   r&   r2   ÚdirectoryFormatr1   ÚdirectorySizer0   r(   Ú	signaturer,   ÚsfntDirectoryEntrySizeÚorigNextTableOffsetr/   r'   ÚfontTools.ttLibr{   ÚsearchRangeÚentrySelectorÚ
rangeShiftrY   ÚdirectoryOffsetÚ
formatSizeÚnextTableOffsetr   Úwriter   r9   )r;   r#   r4   r   r%   r&   r{   r   r   r   r?   Ó   s,    

zSFNTWriter.__init__c                 C   s$   || j v rtd| ƒ‚|| j |< d S )Núcannot rewrite '%s' table)r9   r   )r;   r6   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‹   rF   NrG   rH   rI   Tr   rg   éüÿÿÿr}   )r9   r   r(   r6   r‰   r   rK   rL   Ú	headTableÚuncompressedÚsaveDatar#   r%   r‚   Ú
origOffsetÚ
origLengthÚlengthrŠ   rY   rŒ   )r;   r6   r=   r>   r   r   r   Ú__setitem__û   s$    
"

zSFNTWriter.__setitem__c                 C   s
   | j | S r@   rA   rB   r   r   r   rP     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 )zHAll tables must have been written to disk. Now write the
		directory.
		z-wrong number of tables; expected %d, found %dr   r   r   rI   r|   rg   r�   NrŽ   z>HHr	   rG   rn   ú ry   rF   r   )'r7   r9   r8   r-   r4   r   r%   r€   ÚreservedZtotalSfntSizer’   r&   r:   ÚmajorVersionÚminorVersionÚhasattrÚstructr.   rŽ   ÚmetaDataÚmetaOrigLengthr#   r   rY   Ú
metaOffsetrq   Ú
metaLengthrŠ   ÚprivDataÚ
privOffsetÚ
privLengthr“   r   Úpackr~   r‡   r   ÚtoStringÚwriteMasterChecksum)
r;   r9   r6   r>   r=   ZcompressedMetaDataÚoffZ	paddedOffÚ	directoryZseenHeadr   r   r   rR     s`    

 




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   rz   r|   ì   ÿÿ l   º/ac )Úlistr9   rD   r3   r-   ÚappendrL   r(   r'   rƒ   r{   r4   r„   r…   r†   r   r¢   r/   r7   r8   r6   r‘   r   r’   r“   r£   r,   r�   rK   Úsum)r;   r¦   ÚtagsZ	checksumsr   r{   r9   r6   r>   Z	sfntEntryZdirectory_endrO   Úchecksumadjustmentr   r   r   Ú_calcMasterChecksum^  s,    
zSFNTWriter._calcMasterChecksumc                 C   s:   |   |¡}| j | jd jd ¡ | j t d|¡¡ d S )NrF   rG   z>L)r­   r#   r   r9   r   rŠ   rš   r¢   )r;   r¦   r¬   r   r   r   r¤   |  s    
zSFNTWriter.writeMasterChecksumc                 C   s   dS ©NFr   rE   r   r   r   ÚreordersTables‚  s    zSFNTWriter.reordersTables)r   NN)rb   rc   rd   r   r?   rŒ   r”   rP   rR   r­   r¤   r¯   r   r   r   r   rw   À   s     ÿ
"Brw   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�   rE   r   r   r   r?   Ë  s    zDirectoryEntry.__init__c                 C   s   t  | j| | j¡| ¡ d S r@   )r   r.   Úformatr   rˆ   )r;   r#   r   r   r   r5   Î  s    zDirectoryEntry.fromFilec                 C   s   t  | j|| ¡ d S r@   )r   r.   r°   )r;   Ústrr   r   r   Ú
fromStringÑ  s    zDirectoryEntry.fromStringc                 C   s   t  | j| ¡S r@   )r   r¢   r°   rE   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 )Nr6   z<%s '%s' at %x>z
<%s at %x>)r™   Ú	__class__rb   r6   ÚidrE   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   rJ   Ý  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 r@   r   )r;   ÚrawDatar   r   r   r¶   ì  s    zDirectoryEntry.decodeDatac                 C   s   |S r@   r   )r;   r=   r   r   r   r¸   ï  s    zDirectoryEntry.encodeDataN)rb   rc   rd   r?   r5   r²   r£   rµ   rJ   r�   r¶   r¸   r   r   r   r   r(   É  s   r(   c                   @   s   e Zd ZeZeZdS )r'   N)rb   rc   rd   Ú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 )r0   c                    s"   t t| ƒ ¡  ttdƒst| _d S )NÚzlibCompressionLevel)Úsuperr0   r?   r™   ÚZLIB_COMPRESSION_LEVELr»   rE   ©r³   r   r   r?   ü  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   )rt   r“   r’   Ú
decompressr-   )r;   r¹   rt   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 r@   )r-   r’   r�   rq   r»   r“   )r;   r=   ZcompressedDatar¹   r   r   r   r¸     s    


zWOFFDirectoryEntry.encodeData)rb   rc   rd   ÚwoffDirectoryEntryFormatr°   ÚwoffDirectoryEntrySizerˆ   r?   r¶   r¸   Ú__classcell__r   r   r¾   r   r0   ÷  s
   
r0   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 r@   )r—   r˜   r›   rŸ   rž   r#   r   r�   r   r-   Ú_decompressrœ   r¡   r    )r;   Úreaderr¹   r=   r   r   r   r?   "  s&    
zWOFFFlavorData.__init__c                 C   s   dd l }| |¡S r¿   )rt   rÀ   )r;   r¹   rt   r   r   r   rÄ   7  s    zWOFFFlavorData._decompress)N)rb   rc   rd   ZFlavorr?   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 )zû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-   r3   rš   r.   rª   )r=   Ú	remainderÚvalueZ	blockSizer   ÚblockZlongsr   r   r   rK   <  s    rK   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)   S  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Í   rY   rš   )r#   r*   r;   r   r   r   r   ÚwriteTTCHeaderb  s    
&rÐ   Ú__main__)0Ú__doc__Úior   Útypesr   ZfontTools.misc.textToolsr   ZfontTools.miscr   rƒ   r   rš   Úcollectionsr   ÚloggingÚ	getLoggerrb   rM   r   r   r½   rs   ru   rq   rw   rÍ   ÚcalcsizerÌ   r/   r,   rº   r�   r2   r1   rÁ   rÂ   r(   r'   r0   r:   rK   r)   rÐ   ÚsysÚdoctestÚexitÚtestmodÚfailedr   r   r   r   Ú<module>   s\   
}ö I
	


	
)'
