U
    ®&^`  ã                   @   s–   d dl Z ddlmZmZ ddlmZmZmZ	m
ZmZ dZdZdZdZdZd	Zd
ZdZdZdZdd„ dd„ dd„ dd„ gZdd„ ZG dd„ dejƒZdS )é    Né   )ÚFontFileÚImage)Úi8Úi16beÚi16leÚi32beÚi32leifcpé   é   é   é   é    é@   é€   é   c                 C   s   | d d? S )Né   é   © ©Úbitsr   r   ú3/tmp/pip-install-a1j0c_p1/Pillow/PIL/PcfFontFile.pyÚ<lambda>(   ó    r   c                 C   s   | d d? d@ S )Né   r   éþÿÿÿr   r   r   r   r   r   )   r   c                 C   s   | d d? d@ S )Né   r   éüÿÿÿr   r   r   r   r   r   *   r   c                 C   s   | d d? d@ S )Né?   r   iøÿÿÿr   r   r   r   r   r   +   r   c                 C   s   | ||   d|¡… S )Nó    )Úindex)ÚsÚor   r   r   Úsz/   s    r#   c                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )ÚPcfFontFileÚnamec              	      s   t | d¡ƒ}|tkrtdƒ‚tƒ  ¡  t | d¡ƒ}i | _t|ƒD ]@}t | d¡ƒ}t | d¡ƒt | d¡ƒt | d¡ƒf| j|< qD|| _|  	¡ | _
|  ¡ }|  |¡}|  ¡ }tdƒD ]b}	||	 }
|
d k	r¸||
 \}}}}}}}}|df||| || |fdd||f||
 f}|| j|	< q¸d S )Nr   znot a PCF filer   r   )Úl32ÚreadÚ	PCF_MAGICÚSyntaxErrorÚsuperÚ__init__ÚtocÚrangeÚfpÚ_load_propertiesÚinfoÚ_load_metricsÚ_load_bitmapsÚ_load_encodingÚglyph)Úselfr.   ÚmagicÚcountÚiÚtypeÚmetricsÚbitmapsÚencodingÚchÚixÚxÚyÚlÚrÚwÚaÚdÚfr4   ©Ú	__class__r   r   r+   ;   s(    
0

,zPcfFontFile.__init__c                 C   sX   | j | \}}}| j}| |¡ t| d¡ƒ}|d@ rBtt }}n
tt }}||||fS )Nr   )r,   r.   Úseekr&   r'   Úb16Úb32Úl16)r5   ÚtagÚformatÚsizeÚoffsetr.   Úi16Úi32r   r   r   Ú
_getformat[   s    

zPcfFontFile._getformatc              	   C   sÊ   i }|   t¡\}}}}|| d¡ƒ}g }t|ƒD ]2}| || d¡ƒt| d¡ƒ|| d¡ƒf¡ q0|d@ r‚| d|d@  tj¡ | || d¡ƒ¡}	|D ]*\}
}}t	|	|
ƒ}
|r¼t	|	|ƒ}|||
< qš|S )Nr   r   r   )
rS   ÚPCF_PROPERTIESr'   r-   Úappendr   rI   ÚioÚSEEK_CURr#   )r5   Z
propertiesr.   rN   rQ   rR   ZnpropsÚpr8   ÚdataÚkr!   Úvr   r   r   r/   k   s    0


zPcfFontFile._load_propertiesc              
   C   s\  g }|   t¡\}}}}|j}|d@ dkrÄt|| d¡ƒƒD ]†}t| d¡ƒd }t| d¡ƒd }	t| d¡ƒd }
t| d¡ƒd }t| d¡ƒd }|	| }|| }|||||	|
||dfƒ q:n”t|| d¡ƒƒD ]€}|| d¡ƒ}|| d¡ƒ}	|| d¡ƒ}
|| d¡ƒ}|| d¡ƒ}|| d¡ƒ}|	| }|| }|||||	|
|||fƒ qÖ|S )Ni ÿ  r   r
   r   r   r   r   )rS   ÚPCF_METRICSrU   r-   r'   r   )r5   r:   r.   rN   rQ   rR   rU   r8   ÚleftÚrightÚwidthZascentZdescentZxsizeZysizeÚ
attributesr   r   r   r1   ‡   s0    zPcfFontFile._load_metricsc                 C   s.  g }|   t¡\}}}}|| d¡ƒ}|t|ƒkr8tdƒ‚g }t|ƒD ]}	| || d¡ƒ¡ qDg }
tdƒD ]}	|
 || d¡ƒ¡ qj|d@ }|d@ }|
| }| |¡ | |¡}t| }d}|rÄd}t|ƒD ]\}	||	 \}}}}}}}}||	 ||	d   }}| t 	d||f|||… d|||ƒ¡¡ qÌ|S )	Nr   zWrong number of bitmapsr   r   z1;RÚ1r   Úraw)
rS   ÚPCF_BITMAPSr'   ÚlenÚOSErrorr-   rU   ÚBYTES_PER_ROWr   Z	frombytes)r5   r:   r;   r.   rN   rQ   rR   ZnbitmapsÚoffsetsr8   ZbitmapSizesZbitorderZpadindexZ
bitmapsizerY   ÚpadÚmoder?   r@   rA   rB   rC   rD   rE   rF   ÚbÚer   r   r   r2   ¯   s2    

,zPcfFontFile._load_bitmapsc              	   C   sÒ   d gd }|   t¡\}}}}|| d¡ƒ|| d¡ƒ }}|| d¡ƒ|| d¡ƒ }}	|| d¡ƒ || d |	| d  }
t|
ƒD ]F}|| d¡ƒ}|dkr†z|||| < W q† tk
rÊ   Y  qÎY q†X q†|S )Nr   r
   r   iÿÿ  )rS   ÚPCF_BDF_ENCODINGSr'   r-   Ú
IndexError)r5   r<   r.   rN   rQ   rR   ZfirstColZlastColZfirstRowZlastRowZ	nencodingr8   ZencodingOffsetr   r   r   r3   Ú   s    
zPcfFontFile._load_encoding)Ú__name__Ú
__module__Ú__qualname__r%   r+   rS   r/   r1   r2   r3   Ú__classcell__r   r   rG   r   r$   7   s    (+r$   )rV   Ú r   r   Ú_binaryr   r   rJ   r   rL   r   rK   r	   r&   r(   rT   ZPCF_ACCELERATORSr\   rc   ZPCF_INK_METRICSrl   ZPCF_SWIDTHSZPCF_GLYPH_NAMESZPCF_BDF_ACCELERATORSrf   r#   r$   r   r   r   r   Ú<module>   s&   ü