a
    Sic#                     @   s   d Z ddlmZmZ ddlmZmZ zeZW n e	y>   Y n0 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.
    )absolute_importunicode_literals)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dS dS )z    Returns the default border size (quiet zone) for the provided version.

    :param int version: 1 .. 40 or a Micro QR Code version constant.
    :rtype: int
    r          )versionr   r   G/var/www/html/django/DPS/env/lib/python3.9/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 int version: 1 .. 40 or a Micro QR Code version constant
    :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   sP   |du rt | }| dkr$| d d n| d d d }|d| 7 }||9 }||fS )a      Returns the symbol size (width x height) with the provided border and
    scaling factor.

    :param int version: A version constant.
    :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   	   r   )r   scaler   dimr   r   r   r
   3   s    $r
   c                 C   s   | dkrt 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   z2The scale must not be negative or zero. Got: "{0}"N)
ValueErrorformat)r   r   r   r   r   K   s    r   c                 C   s.   | dur*t | | ks| dk r*td| dS )z    Raises a :py:exc:`ValueError` iff `border` is negative.

    :param int border: Indicating the size of the quiet zone.
    Nr   z<The border must not a non-negative integer value. Got: "{0}")intr   r   )r   r   r   r   r   W   s    r   c           	      c   s   ||8 }d}| D ]t}|| }}||7 }|D ]>}||krP|sP||f||ffV  |}|d7 }|sd|d7 }|}q*|r||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incbyZlast_bitrowx1x2bitr   r   r   r   b   s     
r   c           
      #   s   t | tt t||}t|ddd\}dg }t| | }|D ]^}d|  krj|k rvn n| | n| tt fdd|D }t	dD ]
}	|V  qqRdS )aG      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 int version: A version constant.
    :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   r   r   r   c                 3   s8   | ]0}t d |  krk r(n n | nd V  qdS )r   Nr   .0jrr   widthr   r   	<genexpr>       zmatrix_iter.<locals>.<genexpr>N)
r   r   r   r	   r
   rangetupler   from_iterabler   )
r   r   r   r   heightZ
border_row
size_rangeir"   sr   r+   r   r      s    

$ r   c                 #   s   ddl m} t| tt t|}tddd\dk |jddd |   fddt	| | }|D ]:t
tfd	d
|D }tdD ]
}|V  qqd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 int version: A version constant.
    :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   )encoderr   r&   F)reserve_regions
add_timingc                    s  d|   krk rn nvd|  kr2k rn nZ|  | }s |  | }|dkrlt jt jf| S dkr| dk rd |  k rd k sn d |   k rd k rn n|dk rt jt jf| S | d kr|dkrt jS sB| dkrd|  k rd k spn |dkrBd|   k r@d k spn r| dkr\|dksp|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&s6| d kr6|dk r6t j
t jf| S | dk r^|dk s|s^|d ks|s| d kr|dk rt jS t jt jf| S t jS d S )	Nr   r               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)r5   r*   valZalignment_val)alignment_matrixr3   is_micror   r   r-   r   r   get_bit   sr    8&
V(
(
(
z$matrix_iter_verbose.<locals>.get_bitc                 3   s   | ]}t  |V  qd S )Nr'   r(   )rQ   r5   r   r   r   r.      r/   z&matrix_iter_verbose.<locals>.<genexpr>N)segnor7   r   r   r   r	   r
   make_matrixadd_alignment_patternsr0   r1   r   r2   r   )r   r   r   r   r7   r4   r"   r6   r   )	rO   rQ   r3   r5   rP   r   r   r   r-   r   r      s    
& r   )r   N)r   )r   N)r   N)__doc__
__future__r   r   	itertoolsr   r   xranger0   	NameError r   __all__r   r	   r
   r   r   r   r   r   r   r   r   r   <module>	   s    



