3
Tc\9                 @   s   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j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 )    )TextPath)	PathPatch)Affine2DBbox)FontManagerFontProperties)to_rgbN)Axes)checkhandle_errors)get_rgb
ultralightlightnormalregularbookmediumromansemibolddemibolddemiboldheavyz
extra boldblackc              C   s6   t dd tjD } t| j }|jd |j  |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   ../../logomaker/src/Glyph.py
<listcomp>%   s    z#list_font_names.<locals>.<listcomp>sans)dictfont_manager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   c             C   s   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|
| _|| _|| _|| _|	| _|| _| j  | jd krtjdd| jd\}}|| _| j  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_weightr0   _input_checkspltsubplots_make_patch)selfr1   r2   r3   r4   r5   r6   r7   r@   rA   r=   r>   r?   r;   r8   r9   r:   r<   r0   figr   r   r   __init__}   s.    
zGlyph.__init__c             K   s\   | j dk	r | j jdk	r | j j  x.|j D ]"\}}|dkrBt|}|| j|< q*W | j  dS )z
        Safe way to set the attributes of a Glyph object

        parameters
        ----------
        **kwargs:
            Attributes and their values.
        Nr=   r>   )r=   r>   )patchaxesremoveitemsr   __dict__rE   )rF   kwargskeyvaluer   r   r   set_attributes   s    
zGlyph.set_attributesc             C   s   | j dk	r| jj| j  dS )z
        Draws Glyph given current parameters.

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

        returns
        -------
        None.
        N)rI   r5   	add_patch)rF   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j||||}t| j	| j
d}td| jd|d}td| jd|d}	| jrt jddd}
|
j|}| jrt jddd}
|
j|}|j }|	j }|j|j }|j|j }t||}||j }|j| d }|j|j }t j|j |j d	j||dj|j| |jd	}
|
j|}t|| j| j| j| j| jd
| _| jj | j dS )zb
        Returns an appropriately scaled patch object corresponding to
        the Glyph.
        g        Ng       @)familyweightr   r/   )sizeprop)sxsy)txty)	facecolorr:   r<   r>   	linewidth)r   r   )r   r   r^   )!r4   r3   rI   r1   r6   r7   r   from_boundsr   r@   rA   r   r2   r;   r8   r   scaletransform_pathr9   get_extentsminheight	translatexminyminr   r=   r:   r<   r>   r?   r5   rR   )rF   rd   Z	char_xminZ	char_yminZ
char_widthZchar_heightbboxZfont_propertiesZtmp_pathZmsc_pathZtransformationZtmp_bboxZmsc_bboxZhstretch_tmpZhstretch_mscZhstretchZ
char_shiftZvstretchZ	char_pathr   r   r   rE      sX    








zGlyph._make_patchc             C   s<  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 | jd	k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	rt t| jttfdt| j  t t| jttfdt| j  t| j| _t d	| j  ko.dkn  d dS )zP
        check input parameters in the Logo constructor for correctness
        z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 r   zwidth = %d must be >= 0 z.type(vpad) = %s; must be of type float or int zvpad = %d must be >= 0 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 r/   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))r
   
isinstanceintr1   floattyper2   strr3   r4   r5   r	   r6   r7   r@   rA   VALID_FONT_WEIGHT_STRINGSr   r=   r>   r?   r;   lenr8   boolnpbool_r9   r:   r<   )rF   r   r   r   rB   =  s    



zGlyph._input_checksr/   r/   )Nr+   r,   r!   r   r-   r   r,   r.   FFNr/   rs   )
__name__
__module____qualname____doc__r   rH   rQ   rS   rE   rB   r   r   r   r   r*   ,   s(   O             !`r*   )Zmatplotlib.textpathr   matplotlib.patchesr   matplotlib.transformsr   r   matplotlib.font_managerr   r   matplotlib.colorsr   matplotlib.pyplotpyplotrC   matplotlib.axesr	   logomaker.src.error_handlingr
   r   logomaker.src.colorsr   numpyrq   r#   rn   r)   r*   r   r   r   r   <module>   s   


