o
    l^UfF                     @   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rnddlZee  dS 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                        g | ]}   |   qS  r
   .0i)abcountr
   5lib/python3.10/site-packages/fontTools/varLib/plot.py
<listcomp>        zstops.<locals>.<listcomp>c                    r	   r
   r
   r   )r   cr   r
   r   r      r   )range)supportr   r
   )r   r   r   r   r   stops   s   
r   c                 K   s   t | ttjjjD ]I\}}t|dkr*|j||d dgdgdfd|i| q
t|dkrN|j||d dg||d dgdgdfd|i| q
t	t|d S )N   r         ?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 u rd gt |  t|   fddtt D t| d  }t |dkrOt |d |||fdi| d S t |dkret ||||fdi| d S t	d)	Ng      ?c                    s   g | ]	} j |  qS r
   )ZreverseMappingr   modelnamesr
   r   r   3   s    z!plotLocations.<locals>.<listcomp>r   r   r-   r   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   plotLocations*   s   $ r9   c                 K   s   | d}tt| jttjjjt|D ]R\}\}	}
}|d ur%|	| |
| tdd |	|d}g g }}t|D ]}t||i|	}|| || q?|j||fd|
i| t| j|g| qd S )No         r   r;   g        r   r   )add_subplot	enumerater   supportsr   r   r   r   r    	set_title
set_xlabelxlimr#   r   r   appendr"   r*   r%   )r,   axisr5   r7   r8   r-   r(   r'   r   r   r   nameXsXYxyr
   r
   r   r2   >   s"   




r2   c                 K   s  |\}}|j ddd}	tt| jttjjjt|D ]\}
\}}}|d ur+|		| |	
| |	| tdd tdd ||d}||d}t|D ]4}g g g }}}|D ]}t||||i|}|| || || q]|	j|||fd|i| qQt|D ]4}g g g }}}|D ]}t||||i|}|| || || q|	j|||fd|i| qt| j||g|	 qd S )Nr:   3dZ
projectionr;   r   r<   r   )r=   r>   r   r?   r   r   r   r   r    r@   rA   Z
set_ylabelrB   Zylimr#   r   r   rC   r"   r*   r%   )r,   r&   r5   r8   r7   r-   r(   Zax1Zax2axis3Dr   r   r   rE   rF   ZYsrI   rG   rH   ZrJ   zr
   r
   r   r3   S   s>   






r3   c                 K   s@   |    dd | jD }dd | jD }t|||fi | d S )Nc                 S      g | ]}|j qS r
   )locationr   sr
   r
   r   r   y       z plotDocument.<locals>.<listcomp>c                 S   rP   r
   )rE   rR   r
   r
   r   r   z   rT   )	normalizeZsourcesr9   )docr5   r(   r%   r-   r
   r
   r   plotDocumentw   s   rW   c                    s   t | jdks	J | jd  t fdd| jD }t fdd| jD }dd l}||||| d }g }|D ]}	 |	i}
| |
|}|| q9|	d}|j
||dfi | d S )	Nr   r   c                 3       | ]	}|  d V  qdS r   Nr#   r   r)   rD   r
   r   	<genexpr>       z*_plotModelFromMasters2D.<locals>.<genexpr>c                 3   rX   rY   rZ   r[   r\   r
   r   r]      r^   d   r:   -)r!   	axisOrderminr%   maxnumpyarangeinterpolateFromMastersrC   r=   r"   )r,   masterValuesr5   r(   Zaxis_minZaxis_maxnprG   rH   rI   r)   vr'   r
   r\   r   _plotModelFromMasters2D~   s   

rj   c                    sZ  t | jdks	J | 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 }t|	|
D ]&\}}g }|	| t||D ]\}} ||i}| 
||}|	| q}qm||}|jd
dd}|j|	|
|fi | d S )Nr   r   r   c                 3   rX   rY   rZ   r[   axis1r
   r   r]      r^   z*_plotModelFromMasters3D.<locals>.<genexpr>c                 3   rX   rY   rZ   r[   rk   r
   r   r]      r^   c                 3   rX   rY   rZ   r[   axis2r
   r   r]      r^   c                 3   rX   rY   rZ   r[   rm   r
   r   r]      r^   r_   r:   rK   rL   )r!   ra   rb   r%   rc   rd   re   Zmeshgridr   rC   rf   Zarrayr=   Zplot_surface)r,   rg   r5   r(   Z	axis1_minZ	axis1_maxZ	axis2_minZ	axis2_maxrh   rG   rH   rN   Zrow_xZrow_yZz_rowrI   rJ   r)   ri   rM   r
   )rl   rn   r   _plotModelFromMasters3D   s,   

ro   c                 K   sT   t | jdkrt| ||fi | dS t | jdkr&t| ||fi | dS td)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   r.   N)r!   ra   rj   ro   r4   )r,   rg   r5   r(   r
   r
   r   plotModelFromMasters   s
   rp   c              
      s  ddl m} | d u rtjdd  } |  t| dk r<t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rb| d d
rbt }|| d  t|| nadd ttdtdd D  d| d vr fdd| D }t|| n<g }g }| D ]"}|d\}}	|tt dd |dD  |t|	 qt| 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>ArN   =c              	      s*   g | ]}t t d d |dD qS )c                 s       | ]}t |V  qd S Nfloatr   ri   r
   r
   r   r]          z"main.<locals>.<listcomp>.<genexpr>,)dictr   splitrR   r&   r
   r   r      s   * c                 s   rv   rw   rx   rz   r
   r
   r   r]      r{   zmain.<locals>.<genexpr>r|   )Z	fontToolsrq   sysargvr!   printstderrexitr   ZfigureZset_tight_layoutendswithr   readrW   r   ordr9   r~   rC   r}   r   ry   r   rp   Zshow)argsrq   r5   rV   Zlocsr%   rg   argr)   ri   r,   r
   r   r   main   sB   

 $r   __main__)r   rw   )__doc__ZfontTools.varLib.modelsr   r   ZfontTools.designspaceLibr   Z
matplotlibr   Zmpl_toolkits.mplot3dr   	itertoolsr   r/   Zloggingr   Z	getLogger__name__logr   r*   r9   r2   r3   rW   rj   ro   rp   r   r   r
   r
   r
   r   <module>   s0    



$
/