a
    ¥Q•hÛ,  ã                   @   s  d Z ddlmZ ddlmZ ddlZddlZdd„ Zefdd„Z	e
e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d0d&d'„ZG d(d)„ d)eƒZd1d+d,„Zd-d.„ Zed/k�rddlZddlZe  e !¡ j"¡ dS )2zTRoutines for calculating bounding boxes, point in rectangle calculations and
so on.
é    )ÚotRound)ÚVectorNc                 C   s@   | sdS dd„ | D ƒ}dd„ | D ƒ}t |ƒt |ƒt|ƒt|ƒfS )zØCalculate the bounding rectangle of a 2D points array.

    Args:
        array: A sequence of 2D tuples.

    Returns:
        A four-item tuple representing the bounding rectangle ``(xMin, yMin, xMax, yMax)``.
    ©r   r   r   r   c                 S   s   g | ]\}}|‘qS © r   ©Ú.0ÚxÚyr   r   úR/var/www/sistema_ama/venv/lib/python3.9/site-packages/fontTools/misc/arrayTools.pyÚ
<listcomp>   ó    zcalcBounds.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r   r   r   r   r   r
   r      r   ©ÚminÚmax)ÚarrayÚxsÚysr   r   r
   Ú
calcBounds   s
    	r   c                    s   t ‡ fdd„t| ƒD ƒƒS )aû  Calculate the integer bounding rectangle of a 2D points array.

    Values are rounded to closest integer towards ``+Infinity`` using the
    :func:`fontTools.misc.fixedTools.otRound` function by default, unless
    an optional ``round`` function is passed.

    Args:
        array: A sequence of 2D tuples.
        round: A rounding function of type ``f(x: float) -> int``.

    Returns:
        A four-item tuple of integers representing the bounding rectangle:
        ``(xMin, yMin, xMax, yMax)``.
    c                 3   s   | ]}ˆ |ƒV  qd S )Nr   )r   Úv©Úroundr   r
   Ú	<genexpr>*   r   z calcIntBounds.<locals>.<genexpr>)Útupler   )r   r   r   r   r
   ÚcalcIntBounds   s    r   c           
      C   sL   |\}}| du r||||fS | \}}}}	|||ƒ|||ƒ|||ƒ||	|ƒfS )a_  Add a point to a bounding rectangle.

    Args:
        bounds: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax), or None``.
        p: A 2D tuple representing a point.
        min,max: functions to compute the minimum and maximum.

    Returns:
        The updated bounding rectangle ``(xMin, yMin, xMax, yMax)``.
    Nr   )
ÚboundsÚpr   r   r   r	   ÚxMinÚyMinÚxMaxÚyMaxr   r   r
   ÚupdateBounds-   s
    r    c                 C   sD   | \}}|\}}}}||  ko&|kn  oB||  ko>|kS   S )a'  Test if a point is inside a bounding rectangle.

    Args:
        p: A 2D tuple representing a point.
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        ``True`` if the point is inside the rectangle, ``False`` otherwise.
    r   )r   Úrectr   r	   r   r   r   r   r   r   r
   ÚpointInRect@   s    r"   c                    s4   t | ƒdk rg S |\‰‰‰ ‰‡ ‡‡‡fdd„| D ƒS )a  Determine which points are inside a bounding rectangle.

    Args:
        array: A sequence of 2D tuples.
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        A list containing the points inside the rectangle.
    é   c                    s@   g | ]8\}}ˆ|  koˆ kn  o:ˆ|  ko6ˆkn  ‘qS r   r   r   ©r   r   r   r   r   r
   r   ^   r   z pointsInRect.<locals>.<listcomp>)Úlen)r   r!   r   r$   r
   ÚpointsInRectP   s    r&   c                 C   s   | \}}t  |d |d  ¡S )z�Calculate the length of the given vector.

    Args:
        vector: A 2D tuple.

    Returns:
        The Euclidean length of the vector.
    é   )ÚmathÚsqrt)Úvectorr   r	   r   r   r
   ÚvectorLengtha   s    	r+   c                 C   s   dd„ | D ƒS )z˜Round a list of floats to 16-bit signed integers.

    Args:
        array: List of float values.

    Returns:
        A list of rounded integers.
    c                 S   s   g | ]}t t |d  ¡ƒ‘qS )g      à?)Úintr(   Úfloor)r   Úir   r   r
   r   w   r   zasInt16.<locals>.<listcomp>r   )r   r   r   r
   ÚasInt16n   s    	r/   c                 C   s0   | \}}}}t ||ƒt ||ƒt||ƒt||ƒfS )aP  Normalize a bounding box rectangle.

    This function "turns the rectangle the right way up", so that the following
    holds::

        xMin <= xMax and yMin <= yMax

    Args:
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        A normalized bounding rectangle.
    r   ©r!   r   r   r   r   r   r   r
   ÚnormRectz   s    r1   c                 C   s(   | \}}}}|| || || || fS )a:  Scale a bounding box rectangle.

    Args:
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.
        x: Factor to scale the rectangle along the X axis.
        Y: Factor to scale the rectangle along the Y axis.

    Returns:
        A scaled bounding rectangle.
    r   )r!   r   r	   r   r   r   r   r   r   r
   Ú	scaleRect�   s    r2   c                 C   s(   | \}}}}|| || || || fS )a@  Offset a bounding box rectangle.

    Args:
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.
        dx: Amount to offset the rectangle along the X axis.
        dY: Amount to offset the rectangle along the Y axis.

    Returns:
        An offset bounding rectangle.
    r   ©r!   ÚdxÚdyr   r   r   r   r   r   r
   Ú
offsetRect�   s    r6   c                 C   s(   | \}}}}|| || || || fS )aI  Inset a bounding box rectangle on all sides.

    Args:
        rect: A bounding rectangle expressed as a tuple
            ``(xMin, yMin, xMax, yMax)``.
        dx: Amount to inset the rectangle along the X axis.
        dY: Amount to inset the rectangle along the Y axis.

    Returns:
        An inset bounding rectangle.
    r   r3   r   r   r
   Ú	insetRect­   s    r7   c                 C   sh   | \}}}}|\}}}}	t ||ƒt ||ƒt||ƒt||	ƒf\}
}}}|
|ksT||krXdS d|
|||ffS )a¸  Test for rectangle-rectangle intersection.

    Args:
        rect1: First bounding rectangle, expressed as tuples
            ``(xMin, yMin, xMax, yMax)``.
        rect2: Second bounding rectangle.

    Returns:
        A boolean and a rectangle.
        If the input rectangles intersect, returns ``True`` and the intersecting
        rectangle. Returns ``False`` and ``(0, 0, 0, 0)`` if the input
        rectangles don't intersect.
    )Fr   T)r   r   ©Zrect1Zrect2ZxMin1ZyMin1ZxMax1ZyMax1ZxMin2ZyMin2ZxMax2ZyMax2r   r   r   r   r   r   r
   ÚsectRect½   s    ür9   c                 C   sP   | \}}}}|\}}}}	t ||ƒt ||ƒt||ƒt||	ƒf\}
}}}|
|||fS )a0  Determine union of bounding rectangles.

    Args:
        rect1: First bounding rectangle, expressed as tuples
            ``(xMin, yMin, xMax, yMax)``.
        rect2: Second bounding rectangle.

    Returns:
        The smallest rectangle in which both input rectangles are fully
        enclosed.
    r   r8   r   r   r
   Ú	unionRectØ   s    ür:   c                 C   s$   | \}}}}|| d || d fS )zãDetermine rectangle center.

    Args:
        rect: Bounding rectangle, expressed as tuples
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        A 2D tuple representing the point at the center of the rectangle.
    r'   r   r0   r   r   r
   Ú
rectCenterï   s    
r;   c                 C   s   | \}}}}|| ||  S )zºDetermine rectangle area.

    Args:
        rect: Bounding rectangle, expressed as tuples
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        The area of the rectangle.
    r   r0   r   r   r
   ÚrectAreaý   s    
r<   c                 C   sP   | \}}}}t t |¡ƒ}t t |¡ƒ}t t |¡ƒ}t t |¡ƒ}||||fS )a  Round a rectangle to integer values.

    Guarantees that the resulting rectangle is NOT smaller than the original.

    Args:
        rect: Bounding rectangle, expressed as tuples
            ``(xMin, yMin, xMax, yMax)``.

    Returns:
        A rounded bounding rectangle.
    )r,   r(   r-   Úceilr0   r   r   r
   ÚintRect  s    r>   r#   c                 C   sz   |dk rt d|›�ƒ‚t| ƒ\}}}}tt || ¡| ƒtt || ¡| ƒtt || ¡| ƒtt || ¡| ƒfS )zð
    >>> bounds = (72.3, -218.4, 1201.3, 919.1)
    >>> quantizeRect(bounds)
    (72, -219, 1202, 920)
    >>> quantizeRect(bounds, factor=10)
    (70, -220, 1210, 920)
    >>> quantizeRect(bounds, factor=100)
    (0, -300, 1300, 1000)
    r#   z*Expected quantization factor >= 1, found: )Ú
ValueErrorr1   r,   r(   r-   r=   )r!   Úfactorr   r   r   r   r   r   r
   ÚquantizeRect  s    
ürA   c                   @   s   e Zd Zdd„ ZdS )r   c                 O   s   t  dt¡ d S )NzffontTools.misc.arrayTools.Vector has been deprecated, please use fontTools.misc.vector.Vector instead.)ÚwarningsÚwarnÚDeprecationWarning)ÚselfÚargsÚkwargsr   r   r
   Ú__init__5  s    ýzVector.__init__N)Ú__name__Ú
__module__Ú__qualname__rH   r   r   r   r
   r   4  s   r   Fc                 c   sR   | sdS |rt | ƒ}nt| ƒ}t|dƒ}|}|D ]}||fV  |}q0||fV  dS )aÉ  Iterate over current and next items in iterable.

    Args:
        iterable: An iterable
        reverse: If true, iterate in reverse order.

    Returns:
        A iterable yielding two elements per iteration.

    Example:

        >>> tuple(pairwise([]))
        ()
        >>> tuple(pairwise([], reverse=True))
        ()
        >>> tuple(pairwise([0]))
        ((0, 0),)
        >>> tuple(pairwise([0], reverse=True))
        ((0, 0),)
        >>> tuple(pairwise([0, 1]))
        ((0, 1), (1, 0))
        >>> tuple(pairwise([0, 1], reverse=True))
        ((1, 0), (0, 1))
        >>> tuple(pairwise([0, 1, 2]))
        ((0, 1), (1, 2), (2, 0))
        >>> tuple(pairwise([0, 1, 2], reverse=True))
        ((2, 1), (1, 0), (0, 2))
        >>> tuple(pairwise(['a', 'b', 'c', 'd']))
        (('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a'))
        >>> tuple(pairwise(['a', 'b', 'c', 'd'], reverse=True))
        (('d', 'c'), ('c', 'b'), ('b', 'a'), ('a', 'd'))
    N)ÚreversedÚiterÚnext)ÚiterableÚreverseÚitÚfirstÚaÚbr   r   r
   Úpairwise=  s    !


rU   c                   C   s   dS )a  
    >>> import math
    >>> calcBounds([])
    (0, 0, 0, 0)
    >>> calcBounds([(0, 40), (0, 100), (50, 50), (80, 10)])
    (0, 10, 80, 100)
    >>> updateBounds((0, 0, 0, 0), (100, 100))
    (0, 0, 100, 100)
    >>> pointInRect((50, 50), (0, 0, 100, 100))
    True
    >>> pointInRect((0, 0), (0, 0, 100, 100))
    True
    >>> pointInRect((100, 100), (0, 0, 100, 100))
    True
    >>> not pointInRect((101, 100), (0, 0, 100, 100))
    True
    >>> list(pointsInRect([(50, 50), (0, 0), (100, 100), (101, 100)], (0, 0, 100, 100)))
    [True, True, True, False]
    >>> vectorLength((3, 4))
    5.0
    >>> vectorLength((1, 1)) == math.sqrt(2)
    True
    >>> list(asInt16([0, 0.1, 0.5, 0.9]))
    [0, 0, 1, 1]
    >>> normRect((0, 10, 100, 200))
    (0, 10, 100, 200)
    >>> normRect((100, 200, 0, 10))
    (0, 10, 100, 200)
    >>> scaleRect((10, 20, 50, 150), 1.5, 2)
    (15.0, 40, 75.0, 300)
    >>> offsetRect((10, 20, 30, 40), 5, 6)
    (15, 26, 35, 46)
    >>> insetRect((10, 20, 50, 60), 5, 10)
    (15, 30, 45, 50)
    >>> insetRect((10, 20, 50, 60), -5, -10)
    (5, 10, 55, 70)
    >>> intersects, rect = sectRect((0, 10, 20, 30), (0, 40, 20, 50))
    >>> not intersects
    True
    >>> intersects, rect = sectRect((0, 10, 20, 30), (5, 20, 35, 50))
    >>> intersects
    1
    >>> rect
    (5, 20, 20, 30)
    >>> unionRect((0, 10, 20, 30), (0, 40, 20, 50))
    (0, 10, 20, 50)
    >>> rectCenter((0, 0, 100, 200))
    (50.0, 100.0)
    >>> rectCenter((0, 0, 100, 199.0))
    (50.0, 99.5)
    >>> intRect((0.9, 2.9, 3.1, 4.1))
    (0, 2, 4, 5)
    Nr   r   r   r   r
   Ú_testl  s    rV   Ú__main__)r#   )F)#Ú__doc__ZfontTools.misc.roundToolsr   ÚfontTools.misc.vectorr   Z_Vectorr(   rB   r   r   r   r   r    r"   r&   r+   r/   r1   r2   r6   r7   r9   r:   r;   r<   r>   rA   rU   rV   rI   ÚsysÚdoctestÚexitÚtestmodÚfailedr   r   r   r
   Ú<module>   s8   
	
/8
