
    Pf#                     b    d Z ddlmZmZ ddlmZ dZd Zd Zdd	Z	d
 Z
d ZddZddZ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                 ,    | \  }}|dk    r||k    rdn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heights      =/var/www/html/env/lib/python3.11/site-packages/segno/utils.pyr   r      s'      ME6

u11A5    c                 (    ||n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
    r   )r   borders     r   r   r   !   s     '66-D[-Q-QQr   Nc                 b    |t          |           }| \  }}|d|z  z  }|d|z  z  }||z  ||z  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   s        r   r	   r	   /   sN      ~(55ME6	QZE
a&jF5=&5.((r   c                 V    | 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   s    r   r
   r
   G   s2     zzMTTUZ[[\\\ zr   c                     | ;t          |           | k    s| dk     r$t          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   z<The border must not a non-negative integer value. Got: "{0}")intr   r    )r   s    r   r   r   R   sL     s6{{f44

W^^_effggg 

r   c              #      K   ||z  }d}| D ]@}||}}||z  }|D ]$}||k    r|s||f||ffV  |}|dz  }|s|dz  }|}%|r||f||ffV  d}Ad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bits	            r   r   r   \   s       JAH  AB	U
 	 	C3s1gAw&&&&!GB aHH 	q'B7""""H r   c              #     K   t          |           t                    t                     t          ||          }|\  }dgz  }t	          | |z             t	          | ||z             }}|D ]b}d|cxk    r|k     rn n| |         n|t          t          j        fd|D                                 }	t          d          D ]}
|	V  cd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   h   K   | ],}t          d |cxk    rk     rn n|         nd           V  -dS )r   Nr   ).0jrr   r   s     r   	<genexpr>zmatrix_iter.<locals>.<genexpr>   sO      'l'l[\qA~~~~~~~~~qtt3PU(V(V'l'l'l'l'l'lr   N)	r   r"   r
   r   rangetupler   from_iterabler   )r$   r   r   r   r   
border_rowwidth_rangeheight_rangeir)   sr2   r   s     `        @@r   r   r   {   s      vJJEeV,,FME6J %vguv~ > >vgvX^@_@_K  a&F1IIjE''l'l'l'l'l'l`k'l'l'lllmme$$ 	 	AIIII	 r   c              #   "   	
K   ddl m} t          |           t                    t	                     t          ||          }|\  k    odk     |                    dd          	|                    	           	 fd
t          | |z             t          | |z             }}|D ]Ht          t          j        
fd|D                                 }t          d          D ]}|V  Id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                 t   d| cxk    rk     rn nd|cxk    r	k     rn n|          |         }s|          |         }|dk    rt           j        t           j        f|         S r[	dk    rU| dk     r	dz
  |cxk     r		dz
  k     sn dz
  | cxk     r	dz
  k     r'n n$|dk     rt           j        t           j        f|         S | dz
  k    r|dk    rt           j        S s0| dk    rd|cxk     r		dz
  k     s4n |dk    rd| cxk     r	dz
  k     sn r6| dk    r|dk    s|dk    r$| dk    rt           j        t           j        f|         S | dk    r|dk     s"s	|	d	z
  k    s|dk    r/| dk     ss'| dz
  k    rt           j        t           j	        f|         S | dk     r|dk     ss	|	dz
  k    ss-| dz
  k    r$|dk     rt           j
        t           j        f|         S | dk     r|dk     ss	|	dz
  k    ss| dz
  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)
r:   r1   valalignment_valalignment_matrixr   is_micro	is_squarer$   r   s
       r   get_bitz$matrix_iter_verbose.<locals>.get_bit   s   ????F?????qA~~~~~~~~~)A,C 2 0 3A 6 C''"?Acderss Z1uua!;!;!;!;%!)!;!;!;!;%{Q;;;;!;;;;;A & 96;STUXYY
??qAvv!11 P!q&&Q->->->->UQY->->->->AFFqSTOaOaOaOaW]`aWaOaOaOaOa Pb%&!VVAaAEE0&2IJ3OOAvv1q555a%"*nnAvv1q555Q!^^0&2IJ3OO 1uu!a%%%Q]]# 6C()FQJ1q558&:YZ[^__ 1uu!a%%%Q]]# 6C)*VaZAEE,,*F,AB3GG))r   c              3   L   K   | ]}t           |                    V  d S )Nr/   )r0   r1   r]   r:   r   s     r   r3   z&matrix_iter_verbose.<locals>.<genexpr>   s7      'Z'Zwwq!}}e(D(D'Z'Z'Z'Z'Z'Zr   N)segnor=   r   r"   r
   r   make_matrixadd_alignment_patternsr4   r5   r   r6   r   )r$   r   r   r   r=   r8   r9   r)   r;   rZ   r]   r   r:   r[   r\   r   s   ` `      @@@@@@@r   r   r      s     ( vJJEeV,,FME6I'URZH**5&%\a*bb""#3UFCCC$* $* $* $* $* $* $* $* $* $*L !&vguv~ > >vgvX^@_@_K  E''Z'Z'Z'Z'Z'Zk'Z'Z'ZZZ[[e$$ 	 	AIIII	 r   )r   N)r   )__doc__	itertoolsr   r    r   __all__r   r   r	   r
   r   r   r   r   r   r   r   <module>rf      s    
 $ # # # # # # #      1
6 6 6R R R) ) ) )0] ] ]h h h   >   :J J J J J Jr   