
    1yi#                     `    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y)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kD  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heights      K/var/www/api/v1/venv_fitandmore/lib/python3.12/site-packages/segno/utils.pyr   r      s$      ME6
u15A5    c                      ||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
    r   )r   borders     r   r   r       s     '6Q-D[-QQr   Nc                 Z    |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)
    r   r   )r   scaler   r   r   s        r   r	   r	   .   sK      ~(5ME6	QZE
a&jF5=&5.((r   c                 ,    | dk  rt        d|  d      y)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   s    r   r
   r
   F   s&     zI%PQRSS r   c                 N    | #t        |       | k7  s| dk  rt        d|  d      yy)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   s    r   r   r   Q   s<     s6{f4
STZS[[\]^^ 9Cr   c              #      K   ||z  }d}| D ]B  }||}}||z  }|D ]#  }||k7  r|s||f||ff |}|dz  }|s|dz  }|}% |s7||f||ff d}D yw)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H  q'B7""H s   A AAc              #   v  K   t        |       t              t               t        ||      }|\  }dgz  }t	        | |z         t	        | ||z         }}|D ]U  }d|cxk  r|k  rn n| |   n|t        t        j                  fd|D                    }	t        d      D ]  }
|	  W yw)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   \   K   | ]#  }t        d |cxk  rk  rn n|   nd        % yw)r   Nr   ).0jrr   r   s     r   	<genexpr>zmatrix_iter.<locals>.<genexpr>   s-     'l`k[\qA~~qt3PU(V`ks   ),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   z   s      vJEeV,FME6J %vguv~ >vgvX^@_Ka&F1IjE'''l`k'llme$AI % s   B4B9c              #      	
K   ddl m} t        |       t              t	               t        ||      }|\  k(  xr dk  |j                  dd      	|j                  	       	 fd
t        | |z         t        | |z         }}|D ]@  t        t        j                  
fd|D                    }t        d      D ]  }|  B yw)	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                    d| cxk  rk  r.n t         j                  S d|cxk  r	k  rn t         j                  S |    |   }s|    |   }|dk7  r#t         j                  t         j                  f|   S rY	dkD  rT| 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 s*| dk(  rd|cxk  r	dz
  k  s-n |dk(  rd| cxk  rdz
  k  sn r7| dk(  r|dkD  s
|dk(  r(| dkD  r#t         j                  t         j                  f|   S | dk(  r|dk  ss|	d	z
  kD  s|dk(  r2| dk  s
s+| dz
  kD  r#t         j                  t         j                  f|   S | dk  r|dk  ss|	dz
  kD  ss0| dz
  kD  r(|dk  r#t         j                  t         j                  f|   S | dk  r|dk  ss|	dz
  kD  ss| dz
  kD  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?D )))E  !A~~D )))C )A,C 0 3A 6 C'"??AcAcderssA%"*q"<519"< &a <&1* <Q & 9 96;S;STUXYY
?qAv!11116a!.?eai.?Q!VPQTUPbX^abXbPb qAv!a%Q!VA00&2I2IJ3OOQAE(q52:~QAE(q6A:~00&2I2IJ3OO A1q5a%!)m$VaZAE88&:Y:YZ[^__ A1q5a%!)m$!fqj.QU,,,**F,A,AB3GG)))r   c              3   D   K   | ]  }t         |              y w)Nr/   )r0   r1   r]   r:   r   s     r   r3   z&matrix_iter_verbose.<locals>.<genexpr>   s     'Zkwq!}e(Dks    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JEeV,FME6I'URZH**5&%\a*b""#3UFC$* $*L !&vguv~ >vgvX^@_KE'''Zk'ZZ[e$AI % s   CC*)r   N)r   )__doc__	itertoolsr   r    r   __all__r   r   r	   r
   r   r   r   r   r   r   r   <module>rf      sE   
 $ 1
6R)0T_>:Jr   