U
    Dvf(!                     @   s   d dl mZ d dlmZ efddZG dd deZddd	Zed
krd dl	Z	e
e	jdkrtd dlZe	e j e	e  dS )    )Callable)BasePenc                    s   d  fdd| D S )N c                 3   s   | ]} |V  qd S N ).0intosr   =/tmp/pip-unpacked-wheel-qlge9rch/fontTools/pens/svgPathPen.py	<genexpr>   s     z pointToString.<locals>.<genexpr>)join)ptr
   r   r	   r   pointToString   s    r   c                   @   sl   e Zd ZdZefeegef dddZdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd ZdS )
SVGPathPenai  Pen to draw SVG path d commands.

    Example::
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((0, 0))
        >>> pen.lineTo((1, 1))
        >>> pen.curveTo((2, 2), (3, 3), (4, 4))
        >>> pen.closePath()
        >>> pen.getCommands()
        'M0 0 1 1C2 2 3 3 4 4Z'

    Args:
        glyphSet: a dictionary of drawable glyph objects keyed by name
            used to resolve component references in composite glyphs.
        ntos: a callable that takes a number and returns a string, to
            customize how numbers are formatted (default: str).

    Note:
        Fonts have a coordinate system where Y grows up, whereas in SVG,
        Y grows down.  As such, rendering path data from this pen in
        SVG typically results in upside-down glyphs.  You can fix this
        by wrapping the data from this pen in an SVG group element with
        transform, or wrap this pen in a transform pen.  For example:

            spen = svgPathPen.SVGPathPen(glyphset)
            pen= TransformPen(spen , (1, 0, 0, -1, 0, 0))
            glyphset[glyphname].draw(pen)
            print(tpen.getCommands())
    r	   c                 C   s.   t | | g | _d | _d | _d | _|| _d S r   )r   __init__	_commands_lastCommand_lastX_lastY_ntos)selfZglyphSetr
   r   r   r   r   (   s    zSVGPathPen.__init__c                 C   s   | j dkr| jd dS )z
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((0, 0))
        >>> pen.moveTo((10, 10))
        >>> pen._commands
        ['M10 10']
        MN)r   r   popr   r   r   r   _handleAnchor0   s    
zSVGPathPen._handleAnchorc                 C   s:   |    dt|| j }| j| d| _|\| _| _dS )aV  
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((0, 0))
        >>> pen._commands
        ['M0 0']

        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((10, 0))
        >>> pen._commands
        ['M10 0']

        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((0, 10))
        >>> pen._commands
        ['M0 10']
        zM%sr   N)r   r   r   r   appendr   r   r   )r   r   tr   r   r   _moveTo;   s
    zSVGPathPen._moveToc                 C   s   |\}}|| j kr || jkr dS || j kr:d}| |}nJ|| jkrTd}| |}n0| jdkrtd}dt|| j }nd}t|| j}d}|r||7 }|| _||7 }| j| |\| _ | _dS )aU  
        # duplicate point
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((10, 10))
        >>> pen.lineTo((10, 10))
        >>> pen._commands
        ['M10 10']

        # vertical line
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((10, 10))
        >>> pen.lineTo((10, 0))
        >>> pen._commands
        ['M10 10', 'V0']

        # horizontal line
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((10, 10))
        >>> pen.lineTo((0, 10))
        >>> pen._commands
        ['M10 10', 'H0']

        # basic
        >>> pen = SVGPathPen(None)
        >>> pen.lineTo((70, 80))
        >>> pen._commands
        ['L70 80']

        # basic following a moveto
        >>> pen = SVGPathPen(None)
        >>> pen.moveTo((0, 0))
        >>> pen.lineTo((10, 10))
        >>> pen._commands
        ['M0 0', ' 10 10']
        NVHr   r   L )r   r   r   r   r   r   r   )r   r   xycmdZptsr   r   r   r   _lineToR   s*    $


zSVGPathPen._lineToc                 C   s^   d}|t || jd 7 }|t || jd 7 }|t || j7 }| j| d| _|\| _| _dS )z
        >>> pen = SVGPathPen(None)
        >>> pen.curveTo((10, 20), (30, 40), (50, 60))
        >>> pen._commands
        ['C10 20 30 40 50 60']
        Cr   N)r   r   r   r   r   r   r   )r   pt1pt2Zpt3r   r   r   r   _curveToOne   s    zSVGPathPen._curveToOnec                 C   sV   |dk	st d}|t|| jd 7 }|t|| j7 }| j| d| _|\| _| _dS )aw  
        >>> pen = SVGPathPen(None)
        >>> pen.qCurveTo((10, 20), (30, 40))
        >>> pen._commands
        ['Q10 20 30 40']
        >>> from fontTools.misc.roundTools import otRound
        >>> pen = SVGPathPen(None, ntos=lambda v: str(otRound(v)))
        >>> pen.qCurveTo((3, 3), (7, 5), (11, 4))
        >>> pen._commands
        ['Q3 3 5 4', 'Q7 5 11 4']
        NQr   )AssertionErrorr   r   r   r   r   r   r   )r   r)   r*   r   r   r   r   _qCurveToOne   s    zSVGPathPen._qCurveToOnec                 C   s"   | j d d| _d | _| _dS )zp
        >>> pen = SVGPathPen(None)
        >>> pen.closePath()
        >>> pen._commands
        ['Z']
        ZN)r   r   r   r   r   r   r   r   r   
_closePath   s    zSVGPathPen._closePathc                 C   s   d| _ d | _| _dS )zk
        >>> pen = SVGPathPen(None)
        >>> pen.endPath()
        >>> pen._commands
        []
        N)r   r   r   r   r   r   r   _endPath   s    zSVGPathPen._endPathc                 C   s   d | jS )Nr#   )r   r   r   r   r   r   getCommands   s    zSVGPathPen.getCommandsN)__name__
__module____qualname____doc__strr   floatr   r   r   r'   r+   r.   r0   r1   r2   r   r   r   r   r   	   s   B
r   Nc                    s
  | dkrddl }|jdd } ddlm} ddl}|jddd}|jdd	d
d |jddddd |jdddd |jddtdd |jddddd || }|j	dk	rt
|j	nd}||j|d}|j}|j}	i }
|j D ].}|d}|d  }t|d }||
|< q|d }|j|j }}|j|
d}|d    |	dk	rZ|dk	rZtd!|	dkr|d" fd#d$|D }	|	 }	d}d}|	D ]D}|| }t|}|| | }|d%|||f 7 }||j7 }qtd& td'||| f  t|dd( td) dS )*z-Generate per-character SVG from font and textNr      )TTFontzfonttools pens.svgPathPenzGenerate SVG from text)descriptionfontzfont.ttfz
Font file.)metavarhelptext?zText string.)r=   nargsr>   z-yz<number>z1Face index into a collection to open. Zero based.z--glyphsz(whitespace-separated list of glyph namesz*Glyphs to show. Exclusive with text option)r=   typer>   z--variationszAXIS=LOCr#   zList of space separated locations. A location consist in the name of a variation axis, followed by '=' and a number. E.g.: wght=700 wdth=80. The default is the location of the base master.)r=   defaultr>   )
fontNumber=hhea)locationcmapz)Options --glyphs and --text are exclusiver   c                 3   s   | ]} t | V  qd S r   )ord)r   urH   r   r   r     s     zmain.<locals>.<genexpr>z?<g transform="translate(%d %d) scale(1 -1)"><path d="%s"/></g>
z&<?xml version="1.0" encoding="UTF-8"?>z?<svg width="%d" height="%d" xmlns="http://www.w3.org/2000/svg">)endz</svg>)sysargvZfontTools.ttLibr:   argparseArgumentParseradd_argumentr7   
parse_argsr%   intr<   r?   glyphsZ
variationssplitstripr8   ascentdescentZgetGlyphSetZgetBestCmap
ValueErrorr   r   Zdrawr2   widthprint)argsrM   r:   rO   parseroptionsrD   r<   r?   rT   rG   Ztag_vfieldstagvrF   rW   rX   ZglyphsetsrZ   gZglyphZpencommandsr   rK   r   main   s     	





re   __main__r9   )N)typingr   ZfontTools.pens.basePenr   r7   r   r   re   r3   rM   lenrN   doctestexittestmodfailedr   r   r   r   <module>   s    H
[