a
    w=ic3                     @   s(  d Z ddlmZ ddlmZ g dZG dd deZG dd deZG d	d
 d
Z	G dd de	Z
G dd dee	ZG dd deZG dd deZG dd deZdd Zdd ZG dd deZedkr$edZed ed edd d!d e  edZeddd"d#d e  dS )$a  fontTools.pens.basePen.py -- Tools and base classes to build pen objects.

The Pen Protocol

A Pen is a kind of object that standardizes the way how to "draw" outlines:
it is a middle man between an outline and a drawing. In other words:
it is an abstraction for drawing outlines, making sure that outline objects
don't need to know the details about how and where they're being drawn, and
that drawings don't need to know the details of how outlines are stored.

The most basic pattern is this::

	outline.draw(pen)  # 'outline' draws itself onto 'pen'

Pens can be used to render outlines to the screen, but also to construct
new outlines. Eg. an outline object can be both a drawable object (it has a
draw() method) as well as a pen itself: you *build* an outline using pen
methods.

The AbstractPen class defines the Pen protocol. It implements almost
nothing (only no-op closePath() and endPath() methods), but is useful
for documentation purposes. Subclassing it basically tells the reader:
"this class implements the Pen protocol.". An examples of an AbstractPen
subclass is :py:class:`fontTools.pens.transformPen.TransformPen`.

The BasePen class is a base implementation useful for pens that actually
draw (for example a pen renders outlines using a native graphics engine).
BasePen contains a lot of base functionality, making it very easy to build
a pen that fully conforms to the pen protocol. Note that if you subclass
BasePen, you *don't* override moveTo(), lineTo(), etc., but _moveTo(),
_lineTo(), etc. See the BasePen doc string for details. Examples of
BasePen subclasses are fontTools.pens.boundsPen.BoundsPen and
fontTools.pens.cocoaPen.CocoaPen.

Coordinates are usually expressed as (x, y) tuples, but generally any
sequence of length 2 will do.
    )Tuple)LogMixin)AbstractPenNullPenBasePenPenErrordecomposeSuperBezierSegmentdecomposeQuadraticSegmentc                   @   s   e Zd ZdZdS )r   z#Represents an error during penning.N__name__
__module____qualname____doc__ r   r   g/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/fontTools/pens/basePen.pyr   /   s   r   c                   @   s   e Zd ZdS )OpenContourErrorN)r   r   r   r   r   r   r   r   2   s   r   c                   @   s   e Zd Zeeef ddddZeeef ddddZeeef dddd	Zeeef ddd
dZddddZ	ddddZ
eeeeeeeef ddddZdS )r   N)ptreturnc                 C   s   t dS )zBegin a new sub path, set the current point to 'pt'. You must
		end each sub path with a call to pen.closePath() or pen.endPath().
		NNotImplementedErrorselfr   r   r   r   moveTo8   s    zAbstractPen.moveToc                 C   s   t dS )z4Draw a straight line from the current point to 'pt'.Nr   r   r   r   r   lineTo>   s    zAbstractPen.lineTo)pointsr   c                 G   s   t dS )aC  Draw a cubic bezier with an arbitrary number of control points.

		The last point specified is on-curve, all others are off-curve
		(control) points. If the number of control points is > 2, the
		segment is split into multiple bezier segments. This works
		like this:

		Let n be the number of control points (which is the number of
		arguments to this call minus 1). If n==2, a plain vanilla cubic
		bezier is drawn. If n==1, we fall back to a quadratic segment and
		if n==0 we draw a straight line. It gets interesting when n>2:
		n-1 PostScript-style cubic segments will be drawn as if it were
		one curve. See decomposeSuperBezierSegment().

		The conversion algorithm used for n>2 is inspired by NURB
		splines, and is conceptually equivalent to the TrueType "implied
		points" principle. See also decomposeQuadraticSegment().
		Nr   r   r   r   r   r   curveToB   s    zAbstractPen.curveToc                 G   s   t dS )a   Draw a whole string of quadratic curve segments.

		The last point specified is on-curve, all others are off-curve
		points.

		This method implements TrueType-style curves, breaking up curves
		using 'implied points': between each two consequtive off-curve points,
		there is one implied point exactly in the middle between them. See
		also decomposeQuadraticSegment().

		The last argument (normally the on-curve point) may be None.
		This is to support contours that have NO on-curve points (a rarely
		seen feature of TrueType outlines).
		Nr   r   r   r   r   qCurveToW   s    zAbstractPen.qCurveTo)r   c                 C   s   dS )zkClose the current sub path. You must call either pen.closePath()
		or pen.endPath() after each sub path.
		Nr   r   r   r   r   	closePathh   s    zAbstractPen.closePathc                 C   s   dS )z}End the current sub path, but don't close it. You must call
		either pen.closePath() or pen.endPath() after each sub path.
		Nr   r   r   r   r   endPathn   s    zAbstractPen.endPath)	glyphNametransformationr   c                 C   s   t dS )zAdd a sub glyph. The 'transformation' argument must be a 6-tuple
		containing an affine transformation, or a Transform object from the
		fontTools.misc.transform module. More precisely: it should be a
		sequence containing 6 numbers.
		Nr   r   r!   r"   r   r   r   addComponentt   s    
zAbstractPen.addComponent)r   r   r   r   floatr   r   r   r   r   r    strr$   r   r   r   r   r   6   s   r   c                   @   sH   e Zd Z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 )r   zA pen that does nothing.
	c                 C   s   d S Nr   r   r   r   r   r      s    zNullPen.moveToc                 C   s   d S r'   r   r   r   r   r   r      s    zNullPen.lineToc                 G   s   d S r'   r   r   r   r   r   r      s    zNullPen.curveToc                 G   s   d S r'   r   r   r   r   r   r      s    zNullPen.qCurveToc                 C   s   d S r'   r   r   r   r   r   r      s    zNullPen.closePathc                 C   s   d S r'   r   r   r   r   r   r       s    zNullPen.endPathc                 C   s   d S r'   r   r#   r   r   r   r$      s    zNullPen.addComponentN)r   r   r   r   r   r   r   r   r   r    r$   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )
LoggingPenzJA pen with a ``log`` property (see fontTools.misc.loggingTools.LogMixin)
	Nr
   r   r   r   r   r(      s   r(   c                   @   s   e Zd ZdZdS )MissingComponentErrorzGIndicates a component pointing to a non-existent glyph in the glyphset.Nr
   r   r   r   r   r)      s   r)   c                       s,   e Zd ZdZdZ fddZdd Z  ZS )DecomposingPena   Implements a 'addComponent' method that decomposes components
	(i.e. draws them onto self as simple contours).
	It can also be used as a mixin class (e.g. see ContourRecordingPen).

	You must override moveTo, lineTo, curveTo and qCurveTo. You may
	additionally override closePath, endPath and addComponent.

	By default a warning message is logged when a base glyph is missing;
	set the class variable ``skipMissingComponents`` to False if you want
	to raise a :class:`MissingComponentError` exception.
	Tc                    s   t t|   || _dS )z Takes a single 'glyphSet' argument (dict), in which the glyphs
		that are referenced as components are looked up by their name.
		N)superr*   __init__glyphSetr   r-   	__class__r   r   r,      s    zDecomposingPen.__init__c                 C   sd   ddl m} z| j| }W n0 tyJ   | js6t|| jd|  Y n0 || |}|| dS )zA Transform the points of the base glyph and draw it onto self.
		r   )TransformPenz,glyph '%s' is missing from glyphSet; skippedN)	ZfontTools.pens.transformPenr1   r-   KeyErrorskipMissingComponentsr)   logwarningZdraw)r   r!   r"   r1   ZglyphZtPenr   r   r   r$      s    

zDecomposingPen.addComponent)r   r   r   r   r3   r,   r$   __classcell__r   r   r/   r   r*      s   r*   c                       s   e Zd ZdZd f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d Zdd Zdd Zdd Zdd Z  ZS ) r   zBase class for drawing pens. You must override _moveTo, _lineTo and
	_curveToOne. You may additionally override _closePath, _endPath,
	addComponent and/or _qCurveToOne. You should not override any other
	methods.
	Nc                    s   t t| | d | _d S r'   )r+   r   r,   _BasePen__currentPointr.   r/   r   r   r,      s    zBasePen.__init__c                 C   s   t d S r'   r   r   r   r   r   _moveTo   s    zBasePen._moveToc                 C   s   t d S r'   r   r   r   r   r   _lineTo   s    zBasePen._lineToc                 C   s   t d S r'   r   )r   pt1pt2pt3r   r   r   _curveToOne   s    zBasePen._curveToOnec                 C   s   d S r'   r   r   r   r   r   
_closePath   s    zBasePen._closePathc                 C   s   d S r'   r   r   r   r   r   _endPath   s    zBasePen._endPathc                 C   st   | j \}}|\}}|\}}|d||   }	|d||   }
|d||   }|d||   }| |	|
f||f| dS )zThis method implements the basic quadratic curve type. The
		default implementation delegates the work to the cubic curve
		function. Optionally override with a native implementation.
		gUUUUUU?N)r7   r=   )r   r:   r;   Zpt0xZpt0yZpt1xZpt1yZpt2xZpt2yZmid1xZmid1yZmid2xZmid2yr   r   r   _qCurveToOne   s    
zBasePen._qCurveToOnec                 C   s   | j S )zfReturn the current point. This is not part of the public
		interface, yet is useful for subclasses.
		)r7   r   r   r   r   _getCurrentPoint   s    zBasePen._getCurrentPointc                 C   s   |    d | _d S r'   )r>   r7   r   r   r   r   r     s    zBasePen.closePathc                 C   s   |    d | _d S r'   )r?   r7   r   r   r   r   r      s    zBasePen.endPathc                 C   s   |  | || _d S r'   )r8   r7   r   r   r   r   r   
  s    
zBasePen.moveToc                 C   s   |  | || _d S r'   )r9   r7   r   r   r   r   r     s    
zBasePen.lineToc                 G   s   t |d }|dksJ |dkr6| j|  |d | _nj|dkrl| j}t|D ]\}}}|||| || _qLn4|dkr| j|  n |dkr| |d  ntdd S )N   r      zcan't get there from here)lenr=   r7   r   r   r   AssertionError)r   r   nr=   r:   r;   r<   r   r   r   r     s    

zBasePen.curveToc                 G   s   t |d }|dksJ |d d u rv|d \}}|d \}}d||  d||  f}|| _| | |d d |f }|dkr| j}t|D ]\}	}
||	|
 |
| _qn| |d  d S )NrB   r   rD         ?)rE   r7   r8   r@   r	   r   )r   r   rG   xynxnyZimpliedStartPointr@   r:   r;   r   r   r   r   /  s    


zBasePen.qCurveTo)N)r   r   r   r   r,   r8   r9   r=   r>   r?   r@   rA   r   r    r   r   r   r   r6   r   r   r/   r   r      s   r   c                 C   s2  t | d }|dksJ g }| d dd  }}}td|d D ]}t|d|| d }td|D ]}|| }	| |d  }
| |d  }|d |	|
d |d    |d |	|
d |d    f}|du r|}q`d|d |d   d|d |d   f}||||f |dd  }}}q`q>||| d | d f |S )	a  Split the SuperBezier described by 'points' into a list of regular
	bezier segments. The 'points' argument must be a sequence with length
	3 or greater, containing (x, y) coordinates. The last point is the
	destination on-curve point, the rest of the points are off-curve points.
	The start point should not be supplied.

	This function returns a list of (pt1, pt2, pt3) tuples, which each
	specify a regular curveto-style bezier segment.
	rB   r   NrC      rI   rH   rD   )rE   rangeminappend)r   rG   ZbezierSegmentsr:   r;   r<   iZ
nDivisionsjfactorZtemp1Ztemp2tempr   r   r   r   L  s,    
r   c           	      C   s   t | d }|dksJ g }t|d D ]J}| | \}}| |d  \}}d||  d||  f}|| | |f q(|| d | d f |S )a  Split the quadratic curve segment described by 'points' into a list
	of "atomic" quadratic segments. The 'points' argument must be a sequence
	with length 2 or greater, containing (x, y) coordinates. The last point
	is the destination on-curve point, the rest of the points are off-curve
	points. The start point should not be supplied.

	This function returns a list of (pt1, pt2) tuples, which each specify a
	plain quadratic bezier segment.
	rB   r   rI   rH   rD   )rE   rO   rQ   )	r   rG   ZquadSegmentsrR   rJ   rK   rL   rM   Z	impliedPtr   r   r   r	   n  s    
r	   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_TestPenz,Test class that prints PostScript to stdout.c                 C   s   t d|d |d f  d S )Nz%s %s movetor   rB   printr   r   r   r   r8     s    z_TestPen._moveToc                 C   s   t d|d |d f  d S )Nz%s %s linetor   rB   rW   r   r   r   r   r9     s    z_TestPen._lineToc              	   C   s4   t d|d |d |d |d |d |d f  d S )Nz%s %s %s %s %s %s curvetor   rB   rW   )r   Zbcp1Zbcp2r   r   r   r   r=     s    z_TestPen._curveToOnec                 C   s   t d d S )NZ	closepathrW   r   r   r   r   r>     s    z_TestPen._closePathN)r   r   r   r   r8   r9   r=   r>   r   r   r   r   rV     s
   rV   __main__N)r   r   )r   d   )2   K   )<   r[   )r[      )rZ   rZ   )rZ   r   )r   typingr   ZfontTools.misc.loggingToolsr   __all__	Exceptionr   r   r   r   r(   r2   r)   r*   r   r   r	   rV   r   penr   r   r   r   r   r   r   r   r   <module>   s.   &K'"


