U
    Dvf9                      @   s   d dl mZ d dlZ d dlmZmZmZ d dlmZm	Z	 d dl
mZ d dlZ d dlmZ d dlmZ d dlZd dlZedZeeed	 ejd
ZG dd deZdddgZeddddZedkree  dS )    )ttLibN)computeMegaGlyphOrdercomputeMegaCmaprenameCFFCharStrings)layoutPreMergelayoutPostMerge)Options)Timer)reducezfontTools.mergez.timer)loggerlevelc                   @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )Mergera  Font merger.

    This class merges multiple files into a single OpenType font, taking into
    account complexities such as OpenType layout (``GSUB``/``GPOS``) tables and
    cross-font metrics (e.g. ``hhea.ascent`` is set to the maximum value across
    all the fonts).

    If multiple glyphs map to the same Unicode value, and the glyphs are considered
    sufficiently different (that is, they differ in any of paths, widths, or
    height), then subsequent glyphs are renamed and a lookup in the ``locl``
    feature will be created to disambiguate them. For example, if the arguments
    are an Arabic font and a Latin font and both contain a set of parentheses,
    the Latin glyphs will be renamed to ``parenleft#1`` and ``parenright#1``,
    and a lookup will be inserted into the to ``locl`` feature (creating it if
    necessary) under the ``latn`` script to substitute ``parenleft`` with
    ``parenleft#1`` etc.

    Restrictions:

    - All fonts must have the same units per em.
    - If duplicate glyph disambiguation takes place as described above then the
            fonts must have a ``GSUB`` table.

    Attributes:
            options: Currently unused.
    Nc                 C   s   |s
t  }|| _d S N)r   options)selfr    r   </tmp/pip-unpacked-wheel-qlge9rch/fontTools/merge/__init__.py__init__5   s    zMerger.__init__c                 C   s<   dd |D }t ||D ]\}}||_|d d|_q|S )Nc                 S   s   g | ]}t |qS r   )r   TTFont).0fontfiler   r   r   
<listcomp><   s     z%Merger._openFonts.<locals>.<listcomp>name   )zipZ_merger__fontfileZgetDebugNameZ_merger__name)r   	fontfilesfontsfontr   r   r   r   
_openFonts;   s
    zMerger._openFontsc              
      s  |  |}dd |D }t| | |d j}|  |}t||D ]*\}}|| d|kr@t| ||d  q@dd |D }dd |D | _t| | tj	|d}|| j
 |D ]}| | q|| _ttjdd	 |D t }	|	d
 t|	D ]  | jjkrqtd  t  fdd|D }
td  t }| | |
}|tk	rv|dk	rv|| < td  ntd  W 5 Q R X q| `| `| | |S )a  Merges fonts together.

        Args:
                fontfiles: A list of file names to be merged

        Returns:
                A :class:`fontTools.ttLib.TTFont` object. Call the ``save`` method on
                this to write it out to an OTF file.
        c                 S   s   g | ]}t | qS r   )listZgetGlyphOrderr   r   r   r   r   r   P   s     z Merger.merge.<locals>.<listcomp>r   zCFF c                 S   s   g | ]}|d  qS )Zcmapr   r    r   r   r   r   ]   s     c                 S   s   g | ]}i qS r   r   )r   _r   r   r   r   ^   s     )sfntVersionc                 s   s   | ]}t | V  qd S r   )r   keysr    r   r   r   	<genexpr>i   s     zMerger.merge.<locals>.<genexpr>Z
GlyphOrderz
merge '%s'c                    s   g | ]}|  tqS r   )getNotImplementedr    tagr   r   r   q   s     zMerging '%s'.FzMerged '%s'.zDropped '%s'.)r   r   r"   r   ZsetGlyphOrderr   ZduplicateGlyphsPerFontr   r   r   
glyphOrder	_preMerger   r
   setunionremovesortedr   Zdrop_tablestimerloginfoZgetTableClassmerger&   
_postMerge)r   r   r   ZglyphOrdersr"   r   r)   ZcmapsZmegaZallTagstablesZclazztabler   r'   r   r2   B   sF    








zMerger.mergec                    s   t jt  fdd |D  }|D ] td  z|  }W nJ tk
r   z|d }W n( tk
r   td |jjf Y nX Y nX |tkrq | fdd|D }|tk	r t	| | q |S )Nc                 s   s"   | ]}|t k	rt| V  qd S r   )r&   varsr#   r   r5   r   r   r   r$      s      z&Merger.mergeObjects.<locals>.<genexpr>z %s*z*Don't know how to merge key %s of class %sc                 3   s   | ]}t | tV  qd S r   )getattrr&   r7   keyr   r   r$      s     )
r+   r,   r0   r1   KeyError	Exception	__class____name__r&   setattr)r   ZreturnTableZlogicr4   ZallKeysZ
mergeLogicvaluer   r:   r   mergeObjects   s0    
zMerger.mergeObjectsc                 C   s   t | d S r   )r   r   r   r   r   r   r*      s    zMerger._preMergec                 C   s"   t | d|kr|d | d S )NzOS/2)r   ZrecalcAvgCharWidthrC   r   r   r   r3      s    zMerger._postMerge)N)
r?   
__module____qualname____doc__r   r   r2   rB   r*   r3   r   r   r   r   r      s   
Cr   r   mainz%make one with everything (TOTAL TIME)c              	   C   s  ddl m} | dkr"tjdd } t }|| } g }|jrdt|j}dd | D }W 5 Q R X | D ]}|	| qht
|dk rtdtjd td	tjd td
tjd tdtjd tdtjd tdtjd tdtjd tdtjd tdtjd tdtjd dS ||jr&tjntjd |jrHtjtj ndtj_t|d}||}|jrx||j td ||j W 5 Q R X dS )zMerge multiple fonts into oner   )configLoggerN   c                 S   s"   g | ]}|  d s| qS )#)lstrip
startswithstrip)r   liner   r   r   r      s   zmain.<locals>.<listcomp>ztusage: pyftmerge [font1 ... fontN] [--input-file=filelist.txt] [--output-file=merged.ttf] [--import-file=tables.ttx])filezN                                   [--drop-tables=tags] [--verbose] [--timing] z- font1 ... fontN              Files to merge.zp --input-file=<filename>      Read files to merge from a text file, each path new line. # Comment lines allowed.zM --output-file=<filename>     Specify output file name (default: merged.ttf).za --import-file=<filename>     TTX file to import after merging. This can be used to set metadata.zY --drop-tables=<table tags>   Comma separated list of table tags to skip, case sensitive.z: --verbose                    Output progress information.z5 --timing                     Output progress timing.)r   T)r   zcompile and save font)	fontToolsrH   sysargvr   Z
parse_optsZ
input_fileopen	readlinesappendlenprintstderrverboseloggingINFOWARNINGZtimingr/   r   setLevelDEBUGdisabledr   r2   Zimport_fileZ	importXMLsaveZoutput_file)argsrH   r   r   Z	inputfilegZmergerr   r   r   r   rG      sp    



__main__)N)rQ   r   ZfontTools.merge.baseZfontTools.merge.cmapr   r   r   ZfontTools.merge.layoutr   r   ZfontTools.merge.optionsr   ZfontTools.merge.tablesZfontTools.misc.loggingToolsr	   	functoolsr
   rR   r[   	getLoggerr0   r?   r\   r/   objectr   __all__rG   exitr   r   r   r   <module>   s$   
 
F