U
    æ‹Ñh¼  ã                   @   sð   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Z
ddlmZmZ dZzd dlZW nD ek
r¬   zd dlZW n" ek
r¦ Z zW 5 dZ[X Y nX Y nX e d¡Zdd„ Zd	d
„ Zddd„Zdd„ Zdd„ Zddd„ZdS )é    N)Úclosing)Úpartialé   )Úfont_to_quadraticÚfonts_to_quadraticzfontTools.cu2quc                   C   s&   z
t  ¡ W S  tk
r    Y dS X d S )Nr   )ÚmpÚ	cpu_countÚNotImplementedError© r
   r
   úA/tmp/user/2202/pip-unpacked-wheel-pq9wd1e4/fontTools/cu2qu/cli.pyÚ
_cpu_count   s    
r   c                 C   s"   t tjdƒrtj | ¡S t | ¡S )NÚopen)ÚhasattrÚ
ufo_moduleZFontr   )Úpathr
   r
   r   Úopen_ufo    s    r   c                 K   sX   t | ƒ}t d| ¡ t|f|ŽrFt d|¡ |r<| |¡ qT| ¡  n|rTt| |ƒ d S )NzConverting curves for %sú	Saving %s)r   ÚloggerÚinfor   ÚsaveÚ	_copytree)Ú
input_pathÚoutput_pathÚkwargsÚufor
   r
   r   Ú_font_to_quadratic&   s    
r   c                 C   s@   t j t j t j | ¡¡¡} t j t j t j |¡¡¡}| |kS )N)Úosr   ÚnormcaseÚabspathÚrealpath)Úpath1Úpath2r
   r
   r   Ú	_samepath3   s    r"   c                 C   s>   t | |ƒrt d¡ d S tj |¡r.t |¡ t | |¡ d S )Nz6input and output paths are the same file; skipped copy)	r"   r   Údebugr   r   ÚexistsÚshutilÚrmtreeÚcopytree)r   r   r
   r
   r   r   :   s    


r   c              
      s,  t jdd�}|jddtjd� |jdddd	d
� |jddddd� |jddtdddd� |jdddddd� |jddddd � | ¡ }|jd!d"dd#d$� |jd%d&td'd(tƒ d)d*d+� | ¡ }|jd,d-dd.d/d0� |jd1d2dd3d4d0� | 	| ¡}t
dk�r| d5¡ |j�sd6}n|jd(k�r"d7}nd8}tj|d9� t|jƒd(k�rT|j�rT| d:¡ |j�r®|j‰ tj ˆ ¡�s|t ˆ ¡ ntj ˆ ¡�s˜| d;ˆ  ¡ ‡ fd<d=„|jD ƒ}n"|j�rÀ|jg}ndgt|jƒ }t|jdk|j|j|j�rîdnd>d?�}|j�rŽt d@¡ dAd=„ |jD ƒ}t|f|Ž�rdt ||ƒD ]0\}	}
t dB|
¡ |
�rV|	 !|
¡ n|	 !¡  �q0n(t |j|ƒD ]\}}
|
�rpt"||
ƒ �qpnš|j#d(k�r¬t$t|jƒ|j#ƒnd(}|d(k�rt%t&f|Ž}t dC|¡ t't( )|¡ƒ�}| *|t |j|ƒ¡ W 5 Q R X n&t |j|ƒD ]\}}
t&||
f|Ž �qdS )Dz1Convert a UFO font from cubic to quadratic curvesZcu2qu)Úprogz	--versionÚversion)Úactionr)   Úinfilesú+ZINPUTz%one or more input UFO source file(s).)ÚnargsÚmetavarÚhelpz-vz	--verboseÚcountr   )r*   Údefaultz-ez--conversion-errorÚERRORNz<maxiumum approximation error measured in EM (default: 0.001))Útyper.   r1   r/   z-mz--mixedFÚ
store_truez0whether to used mixed quadratic and cubic curves)r1   r*   r/   z--keep-directionÚreverse_directionÚstore_falsez$do not reverse the contour direction)Údestr*   r/   z-iz--interpolatablez@whether curve conversion should keep interpolation compatibility)r*   r/   z-jz--jobsú?r   ÚNz9Convert using N multiple processes (default: %(default)s))r3   r-   r1   Úconstr.   r/   z-oz--output-fileZOUTPUTzsoutput filename for the converted UFO. By default fonts are modified in place. This only works with a single input.)r1   r.   r/   z-dz--output-dirZ	DIRECTORYz-output directory where to save converted UFOsz9Either ufoLib2 or defcon are required to run this script.ÚWARNINGÚINFOÚDEBUG)Úlevelz2-o/--output-file can't be used with multile inputsz'%s' is not a directoryc                    s"   g | ]}t j ˆ t j |¡¡‘qS r
   )r   r   ÚjoinÚbasename)Ú.0Úp©Ú
output_dirr
   r   Ú
<listcomp>Ÿ   s    z_main.<locals>.<listcomp>T)Z
dump_statsZ
max_err_emr5   Zall_quadraticzConverting curves compatiblyc                 S   s   g | ]}t |ƒ‘qS r
   )r   )rA   Úinfiler
   r
   r   rE   ±   s     r   zRunning %d parallel processes)+ÚargparseÚArgumentParserÚadd_argumentÚ	fontToolsÚ__version__ÚfloatÚadd_mutually_exclusive_groupÚintr   Ú
parse_argsr   ÚerrorÚverboseÚloggingÚbasicConfigÚlenr+   Zoutput_filerD   r   r   r$   ÚmkdirÚisdirÚdictZconversion_errorr5   ÚmixedZinterpolatabler   r   r   Úzipr   r   ÚjobsÚminr   r   r   r   ZPoolÚstarmap)ÚargsÚparserZmode_parserZoutput_parserÚoptionsr>   Zoutput_pathsr   Zufosr   r   r   rZ   ÚfuncÚpoolr
   rC   r   Ú_mainC   sÞ    üúûüüøú
û




ÿ
ü
"
 rb   )N)N)r   rG   rR   r%   Úmultiprocessingr   Ú
contextlibr   Ú	functoolsr   rJ   r   r   r   r   ZufoLib2ÚImportErrorZdefconÚeÚ	getLoggerr   r   r   r   r"   r   rb   r
   r
   r
   r   Ú<module>   s.   

	