o
    -hP                     @   s   d Z ddlmZ ddl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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   s6   t | d}t|W  d    S 1 sw   Y  d S )Nrb)openr   )fnamefh r   W/var/www/vscode/kcb/lib/python3.10/site-packages/matplotlib/backends/_backend_pdf_ps.py_cached_get_afm_from_fname   s   $r   c                 C   sb   t jddd}| jg d7  _| drd|_t | |}t j|d}|j|d | | |S )a  
    Subset a TTF font

    Reads the named fontfile and restricts the font to the characters.

    Parameters
    ----------
    fontfile : str
        Path to the font file
    characters : str
        Continuous set of characters to include in subset

    Returns
    -------
    fontTools.ttLib.ttFont.TTFont
        An open font object representing the subset, which needs to
        be closed by the caller.
    T)glyph_namesrecommended_glyphs)FFTMPfEdBDFmetaMERGTSIVZapfbdatbloccidgfdscfeatfmtxfondjustkerxltagmorxtrakxrefz.ttcr   )options)text)r   Optionsdrop_tablesendswithfont_number	load_font	Subsetterpopulate)fontfile
charactersr'   font	subsetterr   r   r   get_glyphs_subset   s   

r4   c                 C   s   t  }| j|dd |S )z
    Convert a TTFont object into a file-like object.

    Parameters
    ----------
    font : fontTools.ttLib.ttFont.TTFont
        A font object

    Returns
    -------
    BytesIO
        A file object with the font saved into it
    F)reorderTables)r   save)r2   r   r   r   r   font_as_fileO   s   r7   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__j   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)_get_fontmapitemsr:   
setdefaultr   setaddord)r<   r2   schar_to_font_c_fr   r   r   trackm   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   rA   rB   )r<   r2   glyphr   r   r   track_glyphs   s   zCharacterTracker.track_glyphN)__name__
__module____qualname____doc__r=   rH   rJ   r   r   r   r   r8   b   s
    r8   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 r9   )superr=   widthheight)r<   rQ   rR   	__class__r   r   r=   }   s   

zRendererPDFPSBase.__init__c                 C      dS )NFr   r;   r   r   r   flipy      zRendererPDFPSBase.flipyc                 C   rU   )NTr   r;   r   r   r   option_scale_image   rW   z$RendererPDFPSBase.option_scale_imagec                 C   s   t jd  S )Nzimage.composite_image)mplrcParamsr;   r   r   r   option_image_nocomposite   s   z*RendererPDFPSBase.option_image_nocompositec                 C   s   | j d | jd fS )Ng      R@)rQ   rR   r;   r   r   r   get_canvas_width_height   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
 rK| |}||\}}}}	}
| d }||9 }|	|9 }	|
|9 }
||	|
fS | |}|j|dtjjd | \}}	| }
d}||9 }|	|9 }	|
|9 }
||	|
fS )NTeXH   i  g        )flagsg      ?)rP   get_text_width_height_descent
_text2pathmathtext_parserparserQ   rR   depthrY   rZ   _use_afm_rc_name_get_font_afmget_str_bbox_and_descentget_size_in_points_get_font_ttfset_textr   	LoadFlags
NO_HINTINGget_width_heightget_descent)r<   rD   propismathrc   r2   lbwhdscalerS   r   r   r`      s,   



z/RendererPDFPSBase.get_text_width_height_descentc                 C   s   t j|d| jd}t|S )Nafm)fontext	directory)r   findfont_afm_font_dirr   )r<   ro   r   r   r   r   rf      s   zRendererPDFPSBase._get_font_afmc                 C   sX   t j|}zt |}|  || d |W S  ty+   t	t
d|  w )Nr^   zMThe PostScript/PDF backend does not currently support the selected font (%s).)r   fontManager_find_fonts_by_propsget_fontclearset_sizerh   RuntimeErrorlogging	getLoggerrK   warning)r<   ro   fnamesr2   r   r   r   ri      s   

zRendererPDFPSBase._get_font_ttf)rK   rL   rM   r=   rV   rX   r[   r\   r`   rf   ri   __classcell__r   r   rS   r   rO   x   s    rO   )rN   ior   	functoolsr   	fontToolsr   
matplotlibrY    r   r   _afmr   backend_basesr   	lru_cacher   r4   r7   r8   rO   r   r   r   r   <module>   s    
8