U
    f=                     @   s   d dl Z e jd  dkr(d dlZed d dlmZ d dlm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mZ d dlmZ d dlZe ZddddddddddddddgZdd ZG dd dZdS )    N   ZTkAgg)TextPath)	PathPatch)Affine2DBbox)FontManagerFontProperties)to_rgb)Axes)checkhandle_errors)get_rgbZ
ultralightZlightnormalZregularbookZmediumromanZsemiboldZdemiboldZdemiboldZheavyz
extra boldblackc                  C   s6   t dd tjD } t|  }|d |  |S )a  
    Returns a list of valid font_name options for use in Glyph or
    Logo constructors.

    parameters
    ----------
    None.

    returns
    -------
    fontnames: (list)
        List of valid font_name names. This will vary from system to system.

    c                 S   s   g | ]}|j |jfqS  )namefname).0fr   r   :/tmp/pip-target-lpfmz8o1/lib/python/logomaker/src/Glyph.py
<listcomp>,   s     z#list_font_names.<locals>.<listcomp>sans)dictfont_managerZttflistlistkeysappendsort)Zfontnames_dictZ	fontnamesr   r   r   list_font_names   s
    
r!   c                   @   s>   e Zd ZdZedddZdd Zdd Zdd Zdd Z	dS )Glypha
  
    A Glyph represents a character, drawn on a specified axes at a specified
    position, rendered using specified styling such as color and font_name.

    attributes
    ----------

    p: (number)
        x-coordinate value on which to center the Glyph.

    c: (str)
        The character represented by the Glyph.

    floor: (number)
        y-coordinate value where the bottom of the Glyph extends to.
        Must be < ceiling.

    ceiling: (number)
        y-coordinate value where the top of the Glyph extends to.
        Must be > floor.

    ax: (matplotlib Axes object)
        The axes object on which to draw the Glyph.

    width: (number > 0)
        x-coordinate span of the Glyph.

    vpad: (number in [0,1])
        Amount of whitespace to leave within the Glyph bounding box above
        and below the actual Glyph. Specifically, in a glyph with
        height h = ceiling-floor, a margin of size h*vpad/2 will be left blank
        both above and below the rendered character.

    font_name: (str)
        The name of the font to use when rendering the Glyph. This is
        the value passed as the 'family' parameter when calling the
        matplotlib.font_manager.FontProperties constructor.

    font_weight: (str or number)
        The font weight to use when rendering the Glyph. Specifically, this is
        the value passed as the 'weight' parameter in the
        matplotlib.font_manager.FontProperties constructor.
        From matplotlib documentation: "weight: A numeric
        value in the range 0-1000 or one of 'ultralight', 'light',
        'normal', 'regular', 'book', 'medium', 'roman', 'semibold',
        'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'."

    color: (matplotlib color)
        Color to use for Glyph face.

    edgecolor: (matplotlib color)
        Color to use for Glyph edge.

    edgewidth: (number >= 0)
        Width of Glyph edge.

    dont_stretch_more_than: (str)
        This parameter limits the amount that a character will be
        horizontally stretched when rendering the Glyph. Specifying a
        wide character such as 'W' corresponds to less potential stretching,
        while specifying a narrow character such as '.' corresponds to more
        stretching.

    flip: (bool)
        If True, the Glyph will be rendered upside down.

    mirror: (bool)
        If True, a mirror image of the Glyph will be rendered.

    zorder: (number)
        Placement of Glyph within the z-stack of ax.

    alpha: (number in [0,1])
        Opacity of the rendered Glyph.

    figsize: ([float, float]):
        The default figure size for the rendered glyph; only used if ax is
        not supplied by the user.
    Nffffff?        r   r   grayr   EF   r'   r'   c                 C   s   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|
| _|| _|| _|| _|	| _|| _|   | jd krtjdd| jd\}}|| _|   d S )Nr'   )figsize)pcfloorceilingaxwidthvpadflipmirrorzorderdont_stretch_more_thanalphacolor	edgecolor	edgewidth	font_namefont_weightr)   _input_checkspltZsubplots_make_patch)selfr*   r+   r,   r-   r.   r/   r0   r9   r:   r6   r7   r8   r4   r1   r2   r3   r5   r)   Zfigr   r   r   __init__   s.    
zGlyph.__init__c                 K   sX   | j dk	r | j jdk	r | j   | D ]"\}}|dkr@t|}|| j|< q(|   dS )z
        Safe way to set the attributes of a Glyph object

        parameters
        ----------
        **kwargs:
            Attributes and their values.
        N)r6   r7   )patchZaxesremoveitemsr	   __dict__r=   )r>   kwargskeyvaluer   r   r   set_attributes   s    
zGlyph.set_attributesc                 C   s   | j dk	r| j| j  dS )z
        Draws Glyph given current parameters.

        parameters
        ----------
        None.

        returns
        -------
        None.
        N)r@   r.   	add_patch)r>   r   r   r   draw   s    
z
Glyph.drawc                 C   s  | j | j }|dkrd| _dS | j| jd  }| j| j| d  }| j}|| j|  }t||||}t| j	| j
d}td| jd|d}td| jd|d}	| jrt jddd	}
|
|}| jrt jddd	}
|
|}| }|	 }|j|j }|j|j }t||}||j }|j| d }|j|j }t j|j |j d
j||d	j|j| |jd
}
|
|}t|| j| j| j| j| jd| _| j | j dS )zb
        Returns an appropriately scaled patch object corresponding to
        the Glyph.
        r$   Ng       @)familyweight)r   r   r'   )sizeprop)sxZsy)Ztxty)Z	facecolorr3   r5   r7   Z	linewidth)!r-   r,   r@   r*   r/   r0   r   Zfrom_boundsr   r9   r:   r   r+   r4   r1   r   scaleZtransform_pathr2   Zget_extentsminheight	translateZxminZyminr   r6   r3   r5   r7   r8   r.   rH   )r>   rS   Z	char_xminZ	char_ymin
char_widthchar_heightZbboxZfont_propertiesZtmp_pathZmsc_pathZtransformationZtmp_bboxZmsc_bboxZhstretch_tmpZhstretch_mscZhstretchZ
char_shiftZvstretchZ	char_pathr   r   r   r=      sn    





   
zGlyph._make_patchc                 C   s  ddl m} ttt| jttfdt| j  tt| jt	dt| j  tt| j
ttfdt| j
  t| j
| _
tt| jttfdt| j  t| j| _t| j
| jkd| j
| jf  t| jdkpt| jtd	 tt| jttfd
t| j  t| jdkd| j  tt| jttfdt| j  td| j  koLdk n  d| j  tt| jt	dt| j  tt| jt	tfdt| j  t| jt	rt| jtkdt  n0t| jtrtd| j  kodkn  d t| j| _t| j| _tt| jttfdt| j  t| j| _t| jdkd| j  tt| jt	dt| j  tt| jdkdt| j  tt| jttjfdt| j  t| j| _tt| jttjfdt| j  t| j| _| jdk	rtt| jttfdt| j  tt| jttfdt| j  t| j| _td| j  koLdkn  d tt| j t!t"tj#fdt| j   t!| j | _ tt| j dkd  tt$d!d" | j D d# dS )$zP
        check input parameters in the Logo constructor for correctness
        r   )Numberztype(p) = %s must be a numberz"type(c) = %s; must be of type str z!type(floor) = %s must be a numberz#type(ceiling) = %s must be a numberz;must have floor <= ceiling. Currently, floor=%f, ceiling=%fNz3ax must be either a matplotlib Axes object or None.z/type(width) = %s; must be of type float or int zwidth = %d must be > 0 z.type(vpad) = %s; must be of type float or int r'   zvpad = %d must be >= 0 and < 1 z(type(font_name) = %s must be of type strz:type(font_weight) = %s should either be a string or an intzfont_weight must be one of %si  z%font_weight must be in range [0,1000]z%type(edgewidth) = %s must be a numberz edgewidth must be >= 0; is %fz7type(dont_stretch_more_than) = %s; must be of type str zSdont_stretch_more_than must have length 1; currently len(dont_stretch_more_than)=%dz&type(flip) = %s; must be of type bool z(type(mirror) = %s; must be of type bool z0type(zorder) = %s; must be of type float or int z'type(alpha) = %s must be a float or intg      ?z-alpha must be between 0.0 and 1.0 (inclusive)z/type(figsize) = %s; figsize must be array-like.r   zfigsize must have length two.c                 S   s"   g | ]}t |ttfo|d kqS )r   )
isinstanceintfloat)r   nr   r   r   r     s   z'Glyph._input_checks.<locals>.<listcomp>z2all elements of figsize array must be numbers > 0.)%numbersrW   r   rX   rY   r*   rZ   typer+   strr,   r-   r.   r
   r/   r0   r9   r:   VALID_FONT_WEIGHT_STRINGSr   r6   r7   r8   r4   lenr1   boolnpZbool_r2   r3   r5   r)   tupler   Zndarrayall)r>   rW   r   r   r   r;   D  s    



zGlyph._input_checks)Nr#   r$   r   r   r%   r   r$   r&   FFNr'   r(   )
__name__
__module____qualname____doc__r   r?   rG   rI   r=   r;   r   r   r   r   r"   3   s*   P              3`r"   ) sysversion_infoZ
matplotlibZuseZmatplotlib.textpathr   Zmatplotlib.patchesr   Zmatplotlib.transformsr   r   Zmatplotlib.font_managerr   r   Zmatplotlib.colorsr	   Zmatplotlib.pyplotZpyplotr<   Zmatplotlib.axesr
   Zlogomaker.src.error_handlingr   r   Zlogomaker.src.colorsr   numpyrb   r   r_   r!   r"   r   r   r   r   <module>   s>   
           