
    jke,                     8   d Z ddlmZ ddlmZ ddlZddlZd ZefdZ	e
efdZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZ G d de      ZddZd Zedk(  r4ddlZddlZ ej@                   ejB                         jD                         yy)zTRoutines for calculating bounding boxes, point in rectangle calculations and
so on.
    )otRound)VectorNc                     | sy| D cg c]  \  }}|	 }}}| D cg c]  \  }}|	 }}}t        |      t        |      t        |      t        |      fS c c}}w c c}}w )zCalculate 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   minmax)arrayxyxsyss        ;/usr/lib/python3/dist-packages/fontTools/misc/arrayTools.py
calcBoundsr      s_     	1!	B		1!	B	r7CGSWc"g-- 
	s
   AAc                 >    t        f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   .   K   | ]  } |        y w)N ).0vrounds     r   	<genexpr>z calcIntBounds.<locals>.<genexpr>*   s     5aq5s   )tupler   )r
   r   s    `r   calcIntBoundsr      s     5:e#4555    c                 n    |\  }}| ||||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)``.
    r   )
boundspr   r	   r   r   xMinyMinxMaxyMaxs
             r   updateBoundsr"   -   sU     FQ~!Qz#D$dtQ<T1s4|Sq\AAr   c                 ^    | \  }}|\  }}}}||cxk  xr |k  nc xr ||cxk  xr |k  S c 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!   s           r   pointInRectr%   @   s@     FQ!D$dA6DA$5$56$56r   c                     t        |       dk  rg S |\  }}}}| D cg c]'  \  }}||cxk  xr |k  nc xr ||cxk  xr |k  nc ) c}}S c c}}w )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.
       )len)r
   r$   r   r   r    r!   r   r   s           r   pointsInRectr)   P   sV     5zA~	!D$dDIJDAqTQ$7TQ%6$%67JJJs   ,Ac                 H    | \  }}t        j                  |dz  |dz  z         S )zCalculate the length of the given vector.

    Args:
        vector: A 2D tuple.

    Returns:
        The Euclidean length of the vector.
       )mathsqrt)vectorr   r   s      r   vectorLengthr/   a   s(     DAq99QTAqD[!!r   c           	      j    | D cg c]#  }t        t        j                  |dz               % c}S c c}w )zRound a list of floats to 16-bit signed integers.

    Args:
        array: List of float values.

    Returns:
        A list of rounded integers.
    g      ?)intr,   floor)r
   is     r   asInt16r4   n   s*     /44C

1s7#$444s   (0c                 l    | \  }}}}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!   s        r   normRectr7   z   s9      $T4ttT?CdOSt_c$oMMr   c                 4    | \  }}}}||z  ||z  ||z  ||z  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!   s          r   	scaleRectr9      s1      $T4t!8TAXtax11r   c                 4    | \  }}}}||z   ||z   ||z   ||z   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!   s          r   
offsetRectr>      1      $T4t"9dRiD2I55r   c                 4    | \  }}}}||z   ||z   ||z
  ||z
  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   r;   s          r   	insetRectrA      r?   r   c                     | \  }}}}|\  }}}}	t        ||      t        ||      t        ||      t        ||	      f\  }
}}}|
|k\  s||k\  ry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   rect1rect2xMin1yMin1xMax1yMax1xMin2yMin2xMax2yMax2r   r   r    r!   s                 r   sectRectrN      s~     $) UE5%#( UE5%E5E5E5E5	D$d t|tt|"$dD)))r   c                     | \  }}}}|\  }}}}	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   rC   s                 r   	unionRectrP      si     $) UE5%#( UE5%E5E5E5E5	D$d $d##r   c                 0    | \  }}}}||z   dz  ||z   dz  fS )zDetermine 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   r6   s        r   
rectCenterrR      s/      $T4t4K1td{a///r   c                 &    | \  }}}}||z
  ||z
  z  S )zDetermine rectangle area.

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

    Returns:
        The area of the rectangle.
    r   r6   s        r   rectArearT      s%      $T4t4KD4K((r   c                    | \  }}}}t        t        j                  |            }t        t        j                  |            }t        t        j                  |            }t        t        j                  |            }||||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.
    )r1   r,   r2   ceilr6   s        r   intRectrW     sk      $T4ttzz$ Dtzz$ DtyyDtyyD$d##r   c           	      d   |dk  rt        d|      t        |       \  }}}}t        t        j                  ||z        |z        t        t        j                  ||z        |z        t        t        j
                  ||z        |z        t        t        j
                  ||z        |z        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: )
ValueErrorr7   r1   r,   r2   rV   )r$   factorr   r   r    r!   s         r   quantizeRectr[     s     zEfZPQQ%d^D$dDJJtf}%./DJJtf}%./DIIdVm$v-.DIIdVm$v-.	 r   c                       e Zd Zd Zy)r   c                 8    t        j                  dt               y )NzffontTools.misc.arrayTools.Vector has been deprecated, please use fontTools.misc.vector.Vector instead.)warningswarnDeprecationWarning)selfargskwargss      r   __init__zVector.__init__5  s    4	
r   N)__name__
__module____qualname__rd   r   r   r   r   r   4  s    
r   r   c              #      K   | sy|rt        |       }nt        |       }t        |d      }|}|D ]
  }||f |} ||f yw)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bs         r   pairwiserr   =  s^     B h(^TNEA !f e*s   AAc                       y)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   _testrt   l  s    r   __main__)r'   )F)#__doc__fontTools.misc.roundToolsr   fontTools.misc.vectorr   _Vectorr,   r^   r   r   r   r	   r"   r%   r)   r/   r4   r7   r9   r>   rA   rN   rP   rR   rT   rW   r[   rr   rt   re   sysdoctestexittestmodfailedr   r   r   <module>r      s    . 3  .   ' 6$ !$ B&7 K"
"	5N&2 6 6 *6$.0)$(*
W 
,^5p zCHH_W__%%&	 r   