U
    FvfZ                     @   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)RendererBase2   c              
   C   s*   t | d}t|W  5 Q R  S Q R X d S )Nrb)openr   )fnamefh r   G/tmp/pip-unpacked-wheel-7vhvci0g/matplotlib/backends/_backend_pdf_ps.py_cached_get_afm_from_fname   s    r   c              
   C   s   t jddd}| jddddg7  _| dr4d|_t | |H}t j|d	}|j|d
 | | t }|j	|dd |W  5 Q R  S Q R X 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PfEdZBDFmetaz.ttcr   )options)textF)ZreorderTablesN)
r   OptionsZdrop_tablesendswithZfont_numberZ	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__D   s    zCharacterTracker.__init__c                 C   s<   | |}| D ]$\}}| j|jt t| qdS )z:Record that string *s* is being typeset using font *font*.N)Z_get_fontmapitemsr   
setdefaultr   setaddord)r   r   sZchar_to_fontZ_c_fr   r   r   trackG   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"   r#   )r   r   Zglyphr   r   r   track_glyphM   s    zCharacterTracker.track_glyphN)__name__
__module____qualname____doc__r   r'   r(   r   r   r   r   r   <   s   r   c                       sX   e Zd Z fddZdd Zdd Zdd Zd	d
 Z fddZdd Z	dd Z
  ZS )RendererPDFPSBasec                    s   t    || _|| _d S r   )superr   widthheight)r   r/   r0   	__class__r   r   r   W   s    
zRendererPDFPSBase.__init__c                 C   s   dS )NFr   r   r   r   r   flipy\   s    zRendererPDFPSBase.flipyc                 C   s   dS )NTr   r   r   r   r   option_scale_image`   s    z$RendererPDFPSBase.option_scale_imagec                 C   s   t jd  S )Nzimage.composite_image)mplrcParamsr   r   r   r   option_image_nocomposited   s    z*RendererPDFPSBase.option_image_nocompositec                 C   s   | j d | jd fS )Ng      R@)r/   r0   r   r   r   r   get_canvas_width_heighti   s    z)RendererPDFPSBase.get_canvas_width_heightc                    s   |dkrt  |||S |r>| jj|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 )NZTeXH   i  g        )flagsg      ?)r.   get_text_width_height_descentZ
_text2pathZmathtext_parserparser/   r0   depthr5   r6   Z_use_afm_rc_name_get_font_afmZget_str_bbox_and_descentget_size_in_points_get_font_ttfZset_textr   ZLOAD_NO_HINTINGZget_width_heightZget_descent)r   r%   propZismathr<   r   lbwhdZscaler1   r   r   r;   m   s,    


z/RendererPDFPSBase.get_text_width_height_descentc                 C   s   t j|d| jd}t|S )NZafm)Zfontext	directory)r   ZfindfontZ_afm_font_dirr   )r   rA   r   r   r   r   r>      s      zRendererPDFPSBase._get_font_afmc                 C   s2   t j|}t |}|  || d |S )Nr9   )r   ZfontManagerZ_find_fonts_by_propsZget_fontclearZset_sizer?   )r   rA   fnamesr   r   r   r   r@      s
    
zRendererPDFPSBase._get_font_ttf)r)   r*   r+   r   r3   r4   r7   r8   r;   r>   r@   __classcell__r   r   r1   r   r-   R   s   r-   )r,   ior   	functoolsZ	fontToolsr   Z
matplotlibr5    r   r   Z_afmr   Zbackend_basesr   	lru_cacher   r   r   r-   r   r   r   r   <module>   s   
&