a
    ëÝEbi  ã                   @   s�   d Z ddlmZ ddlZddlmZ ddlZddlm	Z	m
Z
 ddlmZ ddlmZ e d	¡d
d„ ƒZdd„ ZG dd„ dƒZG dd„ deƒZdS )z7
Common functionality between the PDF and PS backends.
é    )ÚBytesION)Úsubseté   )Úfont_managerÚft2font)ÚAFM)ÚRendererBaseé2   c                 C   s6   t | dƒ�}t|ƒW  d   ƒ S 1 s(0    Y  d S )NÚrb)Úopenr   )ÚfnameÚfh© r   úc/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/matplotlib/backends/_backend_pdf_ps.pyÚ_cached_get_afm_from_fname   s    r   c                 C   sŠ   t jddd�}| jddg7  _t  | |¡�J}t j|d�}|j|d� |  |¡ tƒ }|j|dd� |W  d	  ƒ S 1 s|0    Y  d	S )
aD  
    Subset a TTF font

    Reads the named fontfile and restricts the font to the characters.
    Returns a serialization of the subset font as file-like object.

    Parameters
    ----------
    symbol : str
        Path to the font file
    characters : str
        Continuous set of characters to include in subset
    T)Zglyph_namesZrecommended_glyphsZFFTMZPfEd)Úoptions)ÚtextF)ZreorderTablesN)r   ÚOptionsZdrop_tablesZ	load_fontZ	SubsetterZpopulater   Úsave)ZfontfileÚ
charactersr   ÚfontZ	subsetterr   r   r   r   Úget_glyphs_subset   s    
r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCharacterTrackerz²
    Helper for font subsetting by the pdf and ps backends.

    Maintains a mapping of font paths to the set of character codepoints that
    are being used from that font.
    c                 C   s
   i | _ d S ©N)Úused©Úselfr   r   r   Ú__init__;   s    zCharacterTracker.__init__c                 C   s"   | j  |jtƒ ¡ tt|ƒ¡ dS )z:Record that string *s* is being typeset using font *font*.N)r   Ú
setdefaultr   ÚsetÚupdateÚmapÚord)r   r   Úsr   r   r   Útrack>   s    zCharacterTracker.trackc                 C   s   | j  |jtƒ ¡ |¡ dS )zARecord that codepoint *glyph* is being typeset using font *font*.N)r   r   r   r   Úadd)r   r   Zglyphr   r   r   Útrack_glyphB   s    zCharacterTracker.track_glyphN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r&   r   r   r   r   r   3   s   r   c                       sT   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚRendererPDFPSBasec                    s   t ƒ  ¡  || _|| _d S r   )Úsuperr   ÚwidthÚheight)r   r-   r.   ©Ú	__class__r   r   r   L   s    
zRendererPDFPSBase.__init__c                 C   s   dS )NFr   r   r   r   r   ÚflipyQ   s    zRendererPDFPSBase.flipyc                 C   s   dS )NTr   r   r   r   r   Úoption_scale_imageU   s    z$RendererPDFPSBase.option_scale_imagec                 C   s   t jd  S )Nzimage.composite_image)ÚmplÚrcParamsr   r   r   r   Úoption_image_nocompositeY   s    z*RendererPDFPSBase.option_image_nocompositec                 C   s   | j d | jd fS )Ng      R@)r-   r.   r   r   r   r   Úget_canvas_width_height^   s    z)RendererPDFPSBase.get_canvas_width_heightc                 C   s0  |dkr8|   ¡ }| ¡ }|j||| d�\}}}|||fS |r~ddlm}	 | jjj|d|t| |	ƒoht	j
d d�}
|
j|
j|
jfS t	j
| j rÖ|  |¡}| |¡\}}}}}| ¡ d }||9 }||9 }||9 }|||fS |  |¡}|j|d	tjd
� | ¡ \}}| ¡ }d}||9 }||9 }||9 }|||fS d S )NÚTeX)Úrendererr   )Ú
RendererPSéH   z	ps.useafm)Z_force_standard_ps_fontsiè  g        )Úflagsg      �?)Zget_texmanagerÚget_size_in_pointsÚget_text_width_height_descentZmatplotlib.backends.backend_psr9   Z
_text2pathZmathtext_parserÚparseÚ
isinstancer3   r4   r-   r.   ÚdepthZ_use_afm_rc_nameÚ_get_font_afmZget_str_bbox_and_descentÚ_get_font_ttfZset_textr   ZLOAD_NO_HINTINGZget_width_heightZget_descent)r   r#   ÚpropZismathZ
texmanagerZfontsizeÚwÚhÚdr9   r>   r   ÚlÚbÚscaler   r   r   r=   b   s@    ÿ

ý


z/RendererPDFPSBase.get_text_width_height_descentc                 C   s   t j|d| jd�}t|ƒS )NÚafm)ZfontextÚ	directory)r   ÚfindfontZ_afm_font_dirr   )r   rC   r   r   r   r   rA   …   s    ÿzRendererPDFPSBase._get_font_afmc                 C   s0   t  |¡}t  |¡}| ¡  | | ¡ d¡ |S )Nr:   )r   rL   Zget_fontÚclearZset_sizer<   )r   rC   r   r   r   r   r   rB   Š   s
    

zRendererPDFPSBase._get_font_ttf)r'   r(   r)   r   r1   r2   r5   r6   r=   rA   rB   Ú__classcell__r   r   r/   r   r+   G   s   #r+   )r*   Úior   Ú	functoolsZ	fontToolsr   Z
matplotlibr3   Ú r   r   rJ   r   Zbackend_basesr   Ú	lru_cacher   r   r   r+   r   r   r   r   Ú<module>   s   
