B
    Scn                 @   s   d 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 ddlZddlZddlZeeZdd	d
Zdd ZdddZdd Zdd Zdd Zdd Zdd Zdd Zd ddZedkrddlZee  dS )!z;Visualize DesignSpaceDocument and resulting VariationModel.    )VariationModelsupportScalar)DesignSpaceDocument)pyplot)axes3d)cycleN
   c                sD   | \  fddt D fddt D  g S )Nc                s    g | ]}   |   qS  r	   ).0i)abcountr	   4lib/python3.7/site-packages/fontTools/varLib/plot.py
<listcomp>   s    zstops.<locals>.<listcomp>c                s    g | ]}   |   qS r	   r	   )r
   r   )r   cr   r	   r   r      s    )range)supportr   r	   )r   r   r   r   r   stops   s    
2r   c             K   s   xt | ttjjjD ]\}}t|dkrV|j||d dgdgdfd|i| qt|dkr|j||d dg||d dgdgdfd|i| qt	t|qW d S )N   r   g      ?ocolor   )
zipr   r   cmSet1colorslenplotgetAssertionError)	locationsaxessubplotkwargslocr   r	   r	   r   _plotLocationsDots   s"    

r&   c                s   t | }t|d }t|| }d kr:d gt |  t|   fddtt D t| d  }t |dkrt |d |||fdi| n2t |dkrt ||||fdi| nt	dd S )	Ng      ?c                s   g | ]} j |  qS r	   )ZreverseMapping)r
   r   )modelnamesr	   r   r   7   s    z!plotLocations.<locals>.<listcomp>r   r   r(   r   zOnly 1 or 2 axes are supported)
r   mathZceilr   r   sortedkeys_plotLocations2D_plotLocations3D
ValueError)r!   figr(   r$   ncolsrowsr"   r	   )r'   r(   r   plotLocations.   s     r3   c             K   s   | d}xtt| jttjjjt|D ]\}\}	}
}|d k	rL|	| |
| tdd |	|d}g g  }}x2t|D ]&}t||i|	}|| || qW |j||fd|
i| t| j|g| q,W d S )No   g      g      ?)g      g        g      ?r   )add_subplot	enumerater   supportsr   r   r   r   r   	set_title
set_xlabelxlimr   r   r   appendr   r&   r!   )r'   axisr/   r1   r2   r(   r$   r#   r   r   r   nameXsXYxyr	   r	   r   r,   F   s    
,



r,   c             K   s  |\}}|j ddd}	xtt| jttjjjt|D ]n\}
\}}}|d k	r\|		| |	
| |	| tdd tdd ||d}||d}xxt|D ]l}g g g   }}}x<|D ]4}t||||i|}|| || || qW |	j|||fd|i| qW x|t|D ]p}g g g   }}}x>|D ]6}t||||i|}|| || || q>W |	j|||fd|i| q$W t| j||g|	 q:W d S )Nr4   3d)
projectiong      g      ?)g      g        g      ?r   )r5   r6   r   r7   r   r   r   r   r   r8   r9   Z
set_ylabelr:   Zylimr   r   r   r;   r   r&   r!   )r'   r"   r/   r2   r1   r(   r$   Zax1Zax2axis3Dr   r   r   r=   r>   ZYsrA   r?   r@   ZrB   zr	   r	   r   r-   [   s:    .








 r-   c             K   s<   |    dd | jD }dd | jD }t|||f| d S )Nc             S   s   g | ]
}|j qS r	   )location)r
   sr	   r	   r   r      s    z plotDocument.<locals>.<listcomp>c             S   s   g | ]
}|j qS r	   )r=   )r
   rI   r	   r	   r   r      s    )Z	normalizeZsourcesr3   )docr/   r$   r!   r(   r	   r	   r   plotDocument   s    rK   c                s   t | jdkst| jd  t fdd| jD }t fdd| jD }dd l}||||| d }g }x*|D ]"}	 |	i}
| |
|}|	| qtW |
d}|j||df| d S )	Nr   r   c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )r<   r	   r   	<genexpr>   s    z*_plotModelFromMasters2D.<locals>.<genexpr>c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )r<   r	   r   rL      s    d   r4   -)r   	axisOrderr    minr!   maxnumpyarangeinterpolateFromMastersr;   r5   r   )r'   masterValuesr/   r$   Zaxis_minZaxis_maxnpr?   r@   rA   r%   vr#   r	   )r<   r   _plotModelFromMasters2D   s    


rX   c                s`  t | jdkst| jd | jd   t fdd| jD }t fdd| jD }tfdd| jD }tfdd| jD }dd l}||||| d	 }	||||| d	 }
||	|
\}	}
g }x`t	|	|
D ]R\}}g }|
| x:t	||D ],\}} ||i}| ||}|
|  qW qW ||}|jd
dd}|j|	|
|f| d S )Nr   r   r   c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )axis1r	   r   rL      s    z*_plotModelFromMasters3D.<locals>.<genexpr>c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )rY   r	   r   rL      s    c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )axis2r	   r   rL      s    c             3   s   | ]}|  d V  qdS )r   N)r   )r
   r%   )rZ   r	   r   rL      s    rM   r4   rC   )rD   )r   rO   r    rP   r!   rQ   rR   rS   Zmeshgridr   r;   rT   Zarrayr5   Zplot_surface)r'   rU   r/   r$   Z	axis1_minZ	axis1_maxZ	axis2_minZ	axis2_maxrV   r?   r@   rF   Zrow_xZrow_yZz_rowrA   rB   r%   rW   rE   r	   )rY   rZ   r   _plotModelFromMasters3D   s*    

r[   c             K   sL   t | jdkr t| ||f| n(t | jdkr@t| ||f| ntddS )zPlot a variation model and set of master values corresponding
	to the locations to the model into a pyplot figure.  Variation
	model must have axisOrder of size 1 or 2.r   r   zOnly 1 or 2 axes are supportedN)r   rO   rX   r[   r.   )r'   rU   r/   r$   r	   r	   r   plotModelFromMasters   s
    r\   c          
      s  ddl m} | d kr"tjdd  } |  t| dk rxtdtjd td tdtjd td tdtjd td t	 }|
d	 t| dkr| d d
rt }|| d  t|| ndd ttdtdd D  d| d kr fdd| D }t|| n~g }g }xN| D ]F}|d\}}	|tt dd |dD  |t|	 qW t| d t|d  }
t|
|| t  d S )Nr   )configLoggerr   z/usage: fonttools varLib.plot source.designspace)filez  orz4usage: fonttools varLib.plot location1 location2 ...zBusage: fonttools varLib.plot location1=value1 location2=value2 ...Tz.designspacec             S   s   g | ]}t |qS r	   )chr)r
   r   r	   r	   r   r      s    zmain.<locals>.<listcomp>ArF   =c          	      s*   g | ]"}t t d d |dD qS )c             s   s   | ]}t |V  qd S )N)float)r
   rW   r	   r	   r   rL      s    z"main.<locals>.<listcomp>.<genexpr>,)dictr   split)r
   rI   )r"   r	   r   r      s    c             s   s   | ]}t |V  qd S )N)rb   )r
   rW   r	   r	   r   rL      s    zmain.<locals>.<genexpr>rc   )Z	fontToolsr]   sysargvr   printstderrexitr   ZfigureZset_tight_layoutendswithr   readrK   r   ordr3   re   r;   rd   r   rb   r   r\   Zshow)argsr]   r/   rJ   Zlocsr!   rU   argr%   rW   r'   r	   )r"   r   main   s<    

 
$rp   __main__)r   )N)N)__doc__ZfontTools.varLib.modelsr   r   ZfontTools.designspaceLibr   Z
matplotlibr   Zmpl_toolkits.mplot3dr   	itertoolsr   r)   Zloggingrf   Z	getLogger__name__logr   r&   r3   r,   r-   rK   rX   r[   r\   rp   rj   r	   r	   r	   r   <module>   s,   


$
,