o
    Ô¦/fë  ã                   @  sÞ   U d dl mZ d dlZd dlmZmZ ddlmZ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	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ed< d$d d!„ZG d"d#„ d#ejƒZ dS )%é    )ÚannotationsN)ÚBinaryIOÚCallableé   )ÚFontFileÚImage)Úi8)Úi16be)Úi16le)Úi32be)Úi32leifcpé   é   é   é   é    é@   é€   é   c                 C  s   | d d? S )Né   é   © ©Úbitsr   r   ú/lib/python3.10/site-packages/PIL/PcfFontFile.pyÚ<lambda>.   s    r   c                 C  ó   | d d? d@ S )Né   r   éþÿÿÿr   r   r   r   r   r   /   ó    c                 C  r   )Né   r   éüÿÿÿr   r   r   r   r   r   0   r   c                 C  r   )Né?   r   iøÿÿÿr   r   r   r   r   r   1   r   zlist[Callable[[int], int]]ÚBYTES_PER_ROWÚsÚbytesÚoÚintÚreturnc                 C  s   | ||   d|¡… S )Nó    )Úindex)r$   r&   r   r   r   Úsz5   s   r+   c                      sZ   e Zd ZdZdZdd‡ fdd	„Zddd„Zd dd„Zd!dd„Zd"dd„Z	d#dd„Z
‡  ZS )$ÚPcfFontFilez(Font file plugin for the X11 PCF format.Únameú	iso8859-1Úfpr   Úcharset_encodingÚstrc              	     s"  || _ t| d¡ƒ}|tkrd}t|ƒ‚tƒ  ¡  t| d¡ƒ}i | _t|ƒD ] }t| d¡ƒ}t| d¡ƒt| d¡ƒt| d¡ƒf| j|< q'|| _	|  
¡ | _|  ¡ }|  |¡}	|  ¡ }
t|
ƒD ]-\}}|d urŽ|| \}}}}}}}}|df||| || |fdd||f|	| f| j|< qad S )Nr   znot a PCF filer   )r0   Úl32ÚreadÚ	PCF_MAGICÚSyntaxErrorÚsuperÚ__init__ÚtocÚranger/   Ú_load_propertiesÚinfoÚ_load_metricsÚ_load_bitmapsÚ_load_encodingÚ	enumerateZglyph)Úselfr/   r0   ÚmagicÚmsgÚcountÚiÚtypeÚmetricsÚbitmapsÚencodingZchZixÚxsizeÚysizeÚleftÚrightÚwidthÚascentÚdescentÚ
attributes©Ú	__class__r   r   r7   >   sF   
0


÷

ü€ôzPcfFontFile.__init__Útagr'   r(   úDtuple[BinaryIO, int, Callable[[bytes], int], Callable[[bytes], int]]c                 C  sX   | j | \}}}| j}| |¡ t| d¡ƒ}|d@ r!tt}}ntt}}||||fS )Nr   )r8   r/   Úseekr2   r3   Úb16Úb32Úl16)r@   rS   ÚformatÚsizeÚoffsetr/   Úi16Úi32r   r   r   Ú
_getformatl   s   

zPcfFontFile._getformatúdict[bytes, bytes | int]c                   s¢   i }|   t¡\‰ }}‰ˆˆ  d¡ƒ}‡ ‡fdd„t|ƒD ƒ}|d@ r-ˆ  d|d@  tj¡ ˆ  ˆˆ  d¡ƒ¡}|D ]\}}}	|rEt||	ƒn|	}
|
|t||ƒ< q9|S )Nr   c                   s4   g | ]}ˆˆ   d ¡ƒtˆ   d¡ƒˆˆ   d ¡ƒf‘qS )r   r   )r3   r   ©Ú.0Ú_©r/   r]   r   r   Ú
<listcomp>ˆ   s   4 z0PcfFontFile._load_properties.<locals>.<listcomp>r   )r^   ÚPCF_PROPERTIESr3   r9   rU   ÚioÚSEEK_CURr+   )r@   Z
propertiesrY   r\   ZnpropsÚpÚdataÚkr$   ÚvZproperty_valuer   rc   r   r:   }   s   zPcfFontFile._load_propertiesú3list[tuple[int, int, int, int, int, int, int, int]]c              
   C  s^  g }|   t¡\}}}}|j}|d@ dkrct|| d¡ƒƒD ]C}t| d¡ƒd }t| d¡ƒd }	t| d¡ƒd }
t| d¡ƒd }t| d¡ƒd }|	| }|| }|||||	|
||dfƒ q|S t|| d¡ƒƒD ]@}|| d¡ƒ}|| d¡ƒ}	|| d¡ƒ}
|| d¡ƒ}|| d¡ƒ}|| d¡ƒ}|	| }|| }|||||	|
|||fƒ ql|S )Ni ÿ  r   r   r   r   r   r   )r^   ÚPCF_METRICSÚappendr9   r3   r   )r@   rF   r/   rY   r\   r]   rn   rD   rK   rL   rM   rN   rO   rI   rJ   rP   r   r   r   r<   •   s2   õzPcfFontFile._load_metricsrF   úlist[Image.Image]c                   s  |   t¡\‰ }}‰ˆˆ  d¡ƒ}|t|ƒkrd}t|ƒ‚‡ ‡fdd„t|ƒD ƒ}‡ ‡fdd„tdƒD ƒ}|d@ }|d@ }	||	 }
| |
¡ ˆ  |
¡}t|	 }d}|rTd	}g }t|ƒD ]+}|| d d
… \}}|||d
 … \}}| t 	d	||f|||… d|||ƒ¡¡ qZ|S )Nr   zWrong number of bitmapsc                   ó   g | ]	}ˆˆ   d ¡ƒ‘qS ©r   ©r3   r`   rc   r   r   rd   È   ó    z-PcfFontFile._load_bitmaps.<locals>.<listcomp>c                   rp   rq   rr   r`   rc   r   r   rd   Ê   rs   r   r   z1;RÚ1r   Úraw)
r^   ÚPCF_BITMAPSr3   ÚlenÚOSErrorr9   rn   r#   r   Z	frombytes)r@   rF   rY   r\   ZnbitmapsrB   ZoffsetsZbitmap_sizesZbitorderZpadindexZ
bitmapsizeri   ZpadÚmoderG   rD   rI   rJ   ÚbÚer   rc   r   r=   º   s0   

"ÿzPcfFontFile._load_bitmapsúlist[int | None]c              	     sô   |   t¡\‰ }‰}ˆˆ  d¡ƒˆˆ  d¡ƒ}}ˆˆ  d¡ƒˆˆ  d¡ƒ}}ˆˆ  d¡ƒ || d || d  }d gtd|ƒ }‡ ‡fdd„t|ƒD ƒ}	t|t|ƒƒD ]"}
z|	tt|
gƒ | j	¡ƒ }|dkrm|||
< W qU t
yw   Y qUw |S )Nr   r   r   c                   rp   )r   rr   r`   ©r/   r\   r   r   rd   ñ   rs   z.PcfFontFile._load_encoding.<locals>.<listcomp>iÿÿ  )r^   ÚPCF_BDF_ENCODINGSr3   Úminr9   rw   ÚordÚ	bytearrayÚdecoder0   ÚUnicodeDecodeError)r@   rY   r]   Z	first_colZlast_colZ	first_rowZlast_rowZ	nencodingrH   Zencoding_offsetsrD   Zencoding_offsetr   r}   r   r>   ä   s&   ÿ€þzPcfFontFile._load_encoding)r.   )r/   r   r0   r1   )rS   r'   r(   rT   )r(   r_   )r(   rl   )rF   rl   r(   ro   )r(   r|   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r7   r^   r:   r<   r=   r>   Ú__classcell__r   r   rQ   r   r,   9   s    
.


%*r,   )r$   r%   r&   r'   r(   r%   )!Z
__future__r   rf   Útypingr   r   Ú r   r   Z_binaryr   r	   rV   r
   rX   r   rW   r   r2   r4   re   ZPCF_ACCELERATORSrm   rv   ZPCF_INK_METRICSr~   ZPCF_SWIDTHSZPCF_GLYPH_NAMESZPCF_BDF_ACCELERATORSr#   Ú__annotations__r+   r,   r   r   r   r   Ú<module>   s4   ü
