o
    Z#a                     @   s   d dl 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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   1/usr/lib/python3/dist-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   .   r   c                 C   r   )N?   r   ir   r   r   r   r   r   /   r   c                 C   s   | ||  d| S )N    )index)sor   r   r   sz3   s   r$   c                       sN   e Zd ZdZdZd fdd	Zdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )PcfFontFilez(Font file plugin for the X11 PCF format.name	iso8859-1c              	      s&  || _ t|d}|tkrtdt   t|d}i | _t|D ] }t|d}t|dt|dt|df| j|< q%|| _	| 
 | _|  }| |}|  }	tdD ]1}
|	|
 }|d ur|| \}}}}}}}}|df||| || |fdd||f|| f}|| j|
< q_d S )Nr   znot a PCF filer   r   )charset_encodingl32read	PCF_MAGICSyntaxErrorsuper__init__tocrangefp_load_propertiesinfo_load_metrics_load_bitmaps_load_encodingglyph)selfr1   r(   magiccountitypemetricsbitmapsencodingchixxylrwadfr7   	__class__r   r   r.   <   s.   
0

,
zPcfFontFile.__init__c                 C   sX   | j | \}}}| j}|| t|d}|d@ r!tt}}ntt}}||||fS )Nr   )r/   r1   seekr)   r*   b16b32l16)r8   tagformatsizeoffsetr1   i16i32r   r   r   
_getformat^   s   

zPcfFontFile._getformatc              	   C   s   i }|  t\}}}}||d}g }t|D ]}|||dt|d||df q|d@ rA|d|d@  tj |||d}	|D ]\}
}}t	|	|
}
|r^t	|	|}|||
< qM|S )Nr   r   r   )
rV   PCF_PROPERTIESr*   r0   appendr   rL   ioSEEK_CURr$   )r8   
propertiesr1   rQ   rT   rU   npropspr;   datakr"   vr   r   r   r2   n   s   0


zPcfFontFile._load_propertiesc              
   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   )rV   PCF_METRICSrX   r0   r*   r   )r8   r=   r1   rQ   rT   rU   rX   r;   leftrightwidthascentdescentxsizeysize
attributesr   r   r   r4      s2   zPcfFontFile._load_metricsc                 C   s.  g }|  t\}}}}||d}|t|krtdg }t|D ]}	|||d q"g }
tdD ]}	|
||d q5|d@ }|d@ }|
| }|| ||}t| }d}|rbd}t|D ].}	||	 \}}}}}}}}||	 ||	d  }}|t	d||f||| d||| qf|S )	Nr   zWrong number of bitmapsr   r   z1;R1r   raw)
rV   PCF_BITMAPSr*   lenOSErrorr0   rX   BYTES_PER_ROWr   	frombytes)r8   r=   r>   r1   rQ   rT   rU   nbitmapsoffsetsr;   bitmapSizesbitorderpadindex
bitmapsizer^   padmoderB   rC   rD   rE   rF   rG   rH   rI   ber   r   r   r5      s2   

,zPcfFontFile._load_bitmapsc              	      s   d gd }|  t\ }} d d}} d d}} d || d || d  } fddt|D }	t|t|D ]"}
z|	tt|
g| j }|dkrj|||
< W qR t	yt   Y qRw |S )Nr   r
   r   c                    s   g | ]	}  d qS )r
   )r*   ).0_r1   rT   r   r   
<listcomp>   s    z.PcfFontFile._load_encoding.<locals>.<listcomp>i  )
rV   PCF_BDF_ENCODINGSr*   r0   rm   ord	bytearraydecoder(   UnicodeDecodeError)r8   r?   rQ   rU   firstCollastColfirstRowlastRow	nencodingencodingOffsetsr;   encodingOffsetr   r}   r   r6      s&   
zPcfFontFile._load_encoding)r'   )__name__
__module____qualname____doc__r&   r.   rV   r2   r4   r5   r6   __classcell__r   r   rJ   r   r%   7   s    "(+r%   )rY    r   r   _binaryr   r   rM   r   rO   r   rN   r	   r)   r+   rW   PCF_ACCELERATORSra   rl   PCF_INK_METRICSr   PCF_SWIDTHSPCF_GLYPH_NAMESPCF_BDF_ACCELERATORSro   r$   r%   r   r   r   r   <module>   s0   