o
    Wg#                     @   sp   d Z ddlmZmZ ddlmZ dZdd Zdd	 ZdddZ	dd Z
dd ZdddZdddZdddZd
S )z]Utility functions useful for writers or QR Code objects.

DOES NOT belong to the public API.
    )chainrepeat   )consts)get_default_border_size
get_borderget_symbol_sizecheck_valid_scalecheck_valid_bordermatrix_to_linesmatrix_itermatrix_iter_verbosec                 C   s    | \}}|dkr||krdS dS )z    Returns the default border size (quiet zone) for the provided version.

    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :rtype: int
              )matrix_sizewidthheightr   r   H/var/www/rescue_company/venv/lib/python3.10/site-packages/segno/utils.pyr      s   r   c                 C   s   |dur|S t | S )a&      Returns `border` if not ``None``, otherwise the default border size for
    the provided QR Code.

    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :param border: The size of the quiet zone or ``None``.
    :type border: int or None

    :rtype: int
    Nr   )r   borderr   r   r   r       s   r   Nc                 C   s@   |du rt | }| \}}|d| 7 }|d| 7 }|| || fS )a      Returns the symbol size (width x height) with the provided border and
    scaling factor.

    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :param scale: Indicates the size of a single module (default: 1).
            The size of a module depends on the used output format; i.e.
            in a PNG context, a scaling factor of 2 indicates that a module
            has a size of 2 x 2 pixel. Some outputs (i.e. SVG) accept
            floating point values.
    :type scale: int or float
    :param int border: The border size or ``None`` to specify the
            default quiet zone (4 for QR Codes, 2 for Micro QR Codes).
    :rtype: tuple (width, height)
    Nr   r   )r   scaler   r   r   r   r   r   r   .   s   r   c                 C   s   | dkrt d|  ddS )z    Raises a :py:exc:`ValueError` iff `scale` is negative or zero.

    :param scale: Scaling factor.
    :type scale: float or int
    r   z.The scale must not be negative or zero. Got: ""N)
ValueError)r   r   r   r   r	   F   s   r	   c                 C   s4   | durt | | ks| dk rtd|  ddS dS )z    Raises a :py:exc:`ValueError` iff `border` is negative.

    :param int border: Indicating the size of the quiet zone.
    Nr   z8The border must not a non-negative integer value. Got: "r   )intr   )r   r   r   r   r
   Q   s   r
   c           	      c   s    ||8 }d}| D ]:}||}}||7 }|D ]}||kr)|s)||f||ffV  |}|d7 }|s3|d7 }|}q|rC||f||ffV  d}q	dS )a      Converts the `matrix` into an iterable of ((x1, y1), (x2, y2)) tuples which
    represent a sequence (horizontal line) of dark modules.

    The path starts at the 1st row of the matrix and moves down to the last row.

    :param matrix: An iterable of bytearrays.
    :param x: Initial position on the x-axis.
    :param y: Initial position on the y-axis.
    :param incby: Value to move along the y-axis (default: 1).
    :rtype: iterable of (x1, y1), (x2, y2) tuples
    r   r   Nr   )	matrixxyincbylast_bitrowx1x2bitr   r   r   r   [   s&   
r   c                 #   s    t | tt t||}|\}dg }t| | t| || }}|D ]/}d|  kr:|k r@n n| | n| tt fdd|D }	tdD ]}
|	V  qWq.dS )al      Returns an iterator / generator over the provided matrix which includes
    the border and the scaling factor.

    If either the `scale` or `border` value is invalid, a :py:exc:`ValueError`
    is raised.

    :param matrix: An iterable of bytearrays.
    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :param int scale: The scaling factor (default: ``1``).
    :param int border: The border size or ``None`` to specify the
            default quiet zone (4 for QR Codes, 2 for Micro QR Codes).
    :raises: :py:exc:`ValueError` if an illegal scale or border value is provided
    r   c                 3   s:    | ]}t d |  krk rn n | nd V  qdS )r   Nr   .0jrr   r   r   r   	<genexpr>   s   8 zmatrix_iter.<locals>.<genexpr>N)	r
   r   r	   r   rangetupler   from_iterabler   )r   r   r   r   r   
border_rowwidth_rangeheight_rangeir!   sr   r)   r   r   z   s   

"$ r   c           	      #   s    ddl m} t| tt t||}|\ko%dk |jddd |   fddt| | t| | }}|D ]t	t
fdd	|D }td
D ]}|V  qkqTd
S )a      Returns an iterator / generator over the provided matrix which includes
    the border and the scaling factor.

    This iterator / generator returns different values for dark / light modules
    and therefor the different parts (like the finder patterns, alignment patterns etc.)
    are distinguishable. If this information isn't necessary, use the
    :py:func:`matrix_iter()` function because it is much cheaper and faster.

    If either the `scale` or `border` value is invalid, a py:exc:`ValueError`
    is raised.

    :param matrix: An iterable of bytearrays.
    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :param int scale: The scaling factor (default: ``1``).
    :param int border: The border size or ``None`` to specify the
            default quiet zone (4 for QR Codes, 2 for Micro QR Codes).
    :raises: :py:exc:`ValueError` if an illegal scale or border value is provided
    r   )encoder   F)reserve_regions
add_timingc                    sp  d|   krk r5n t jS d|  krk r5n t jS |  | }sz |  | }|dkr8t jt jf| S rmdkrm| dk rQd |  k rPd k sen d |   k r_d k rmn n|dk rmt jt jf| S | d krz|dkrzt jS s| dkrd|  k rd k sn |dkrd|   k rd k sn r| dkr|dks|dkr| dkrt jt jf| S | dkr|dk s؈s|d	 ks|dkr| dk s؈s| d krt jt j	f| S | dk r|dk ss|d kss| d kr|dk rt j
t jf| S | dk r|dk s*s|d ks*s-| d kr-|dk r-t jS t jt jf| S t jS )
Nr   r   )               	   
   )r   TYPE_ALIGNMENT_PATTERN_LIGHTTYPE_ALIGNMENT_PATTERN_DARKTYPE_VERSION_LIGHTTYPE_VERSION_DARKTYPE_DARKMODULETYPE_TIMING_LIGHTTYPE_TIMING_DARKTYPE_FORMAT_LIGHTTYPE_FORMAT_DARKTYPE_FINDER_PATTERN_LIGHTTYPE_FINDER_PATTERN_DARKTYPE_SEPARATORTYPE_DATA_LIGHTTYPE_DATA_DARKTYPE_QUIET_ZONE)r2   r(   valalignment_val)alignment_matrixr   is_micro	is_squarer   r   r   r   get_bit   sF   ""&(H    (z$matrix_iter_verbose.<locals>.get_bitc                 3   s     | ]}t  |V  qd S )Nr%   r&   )rS   r2   r   r   r   r+      s    z&matrix_iter_verbose.<locals>.<genexpr>N)segnor4   r
   r   r	   r   make_matrixadd_alignment_patternsr,   r-   r   r.   r   )	r   r   r   r   r4   r0   r1   r!   r3   r   )	rP   rS   r   r2   rQ   rR   r   r   r   r   r      s&   
"& r   )r   N)r   )__doc__	itertoolsr   r    r   __all__r   r   r   r	   r
   r   r   r   r   r   r   r   <module>   s   



