U
    :vh/                     @   s   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZmZmZ ddlmZmZmZ erhddlmZ G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z0
Handles the creation of patterns and gradients
    )ABC)ListOptionalTYPE_CHECKINGTupleUnion   )
DeviceCMYK
DeviceGray	DeviceRGBconvert_to_device_color)NamePDFArray	PDFObject)FPDFc                       s6   e Zd ZdZed d fddZedd Z  ZS )Patternz
    Represents a PDF Pattern object.

    Currently, this class supports only "shading patterns" (pattern_type 2),
    using either a linear or radial gradient. Tiling patterns (pattern_type 1)
    are not yet implemented.
    )LinearGradientRadialGradient)shadingc                    s$   t    td| _d| _|| _d S )Nr      )super__init__r   typeZpattern_type_shading)selfr   	__class__ 0/tmp/pip-unpacked-wheel-dvf6lv8i/fpdf/pattern.pyr      s    

zPattern.__init__c                 C   s   | j  j dS )N 0 R)r   get_shading_objectidr   r   r   r   r      s    zPattern.shading)	__name__
__module____qualname____doc__r   r   propertyr   __classcell__r   r   r   r   r      s   r   c                       s    e Zd ZdZ fddZ  ZS )Type2FunctionzTransition between 2 colorsc                    s`   t    d| _d| _dddd |jD  d| _dddd |jD  d| _d	| _d S )
Nr   [0 1][ c                 s   s   | ]}|d V  qdS .2fNr   .0cr   r   r   	<genexpr>,   s     z)Type2Function.__init__.<locals>.<genexpr>]c                 s   s   | ]}|d V  qdS r-   r   r/   r   r   r   r2   -   s     r   )	r   r   function_typedomainjoincolorsZc0Zc1n)r   Zcolor_1Zcolor_2r   r   r   r   '   s    
  zType2Function.__init__)r#   r$   r%   r&   r   r(   r   r   r   r   r)   $   s   r)   c                       s,   e Zd ZdZ fddZedd Z  ZS )Type3FunctionzWhen multiple colors are used, a type 3 function is necessary to stitch type 2 functions together
    and define the bounds between each color transitionc                    sb   t    d| _d| _|| _dddd |D  d| _dddd |D  d| _d	| _d S )
N   r*   r+   r,   c                 s   s   | ]}|d V  qdS r-   r   )r0   boundr   r   r   r2   ;   s     z)Type3Function.__init__.<locals>.<genexpr>r3   c                 s   s   | ]
}d V  qdS )z0 1Nr   )r0   _r   r   r   r2   <   s     r   )	r   r   r4   r5   
_functionsr6   boundsencoder8   )r   	functionsr>   r   r   r   r   5   s    
zType3Function.__init__c                 C   s   dd dd | jD  dS )Nr+   r,   c                 s   s   | ]}|j  d V  qdS )r   N)r!   )r0   fr   r   r   r2   A   s     z*Type3Function.functions.<locals>.<genexpr>r3   )r6   r=   r"   r   r   r   r@   ?   s    zType3Function.functions)r#   r$   r%   r&   r   r'   r@   r(   r   r   r   r   r9   1   s   
r9   c                       sH   e Zd Zeeeeeef  e	e
e eeef eed fddZ  ZS )Shadingshading_type
backgroundcolor_spacecoordsfunctionextend_beforeextend_afterc                    s~   t    || _|r2dddd |jD  dnd | _t|| _|| _|j	 d| _
d|r`dnd d|rndnd d| _d S )	Nr+   r,   c                 s   s   | ]}|d V  qdS r-   r   r/   r   r   r   r2   R   s     z#Shading.__init__.<locals>.<genexpr>r3   r   truefalse)r   r   rD   r6   r7   rE   r   rF   rG   r!   rH   extend)r   rD   rE   rF   rG   rH   rI   rJ   r   r   r   r   E   s    

 
zShading.__init__)r#   r$   r%   intr   r   r   r
   r	   strr   r)   r9   boolr   r(   r   r   r   r   rB   D   s   
rB   c                   @   sF   e Zd Zdd Zeeeef dddZdd Z	dd	 Z
d
d ZdS )Gradientc                    s     |\ _ _d  _|r@t|ttttfr6t	|nt	|  _ jr^ jj
j jkr^td| _| _|rr|n fddtt jd D  _t jt jd krtd   _t  _d  _d  _d _d S )NzDThe background color must be of the same color space as the gradientc                    s"   g | ]}|d  t  jd   qS )r   )lenr7   )r0   ir"   r   r   
<listcomp>o   s     z%Gradient.__init__.<locals>.<listcomp>r   z>Bounds array length must be two less than the number of colorsr   )_convert_colorsrF   r7   rE   
isinstancerO   r
   r   r	   r   r   r#   
ValueErrorrI   rJ   rangerR   r>   _generate_functionsr@   r   pattern_shading_objectrG   rD   )r   r7   rE   rI   rJ   r>   r   r"   r   r   ]   s4    


zGradient.__init__)returnc                 C   s   g }t |dk rtdt }|D ]@}t|ttttfr@t|nt| }|	| |
t|j q"t |dkr|| |fS d|krtdg }|D ]2}t|tr|	t|j|j|j q|	| qd|fS )Nr   (A gradient must have at least two colorsr   r	   z'Can't mix CMYK with other color spaces.r   )rR   rW   setrV   rO   r
   r   r	   r   appendaddr   r#   popg)clsr7   Z
color_listZcolor_spacescolorZcurrent_colorZ	convertedr   r   r   rU   {   s*    


zGradient._convert_colorsc                 C   s   t | jdk rtdt | jdkr<t| jd | jd gS t | j}g }t|d D ]$}|t| j| | j|d   qV|t|d d  | j |S )Nr   r]   r   r   )rR   r7   rW   r)   rX   r_   r9   r>   )r   Znumber_of_colorsr@   rS   r   r   r   rY      s    
"zGradient._generate_functionsc              	   C   s:   | j s4t| j| j| jt| j| jd | j| j	d| _ | j S )NrC   )
r[   rB   rD   rE   rF   r   rG   r@   rI   rJ   r"   r   r   r   r       s    	zGradient.get_shading_objectc                 C   s   | j S )N)rZ   r"   r   r   r   get_pattern   s    zGradient.get_patternN)r#   r$   r%   r   classmethodr   rO   r   rU   rY   r    rf   r   r   r   r   rQ   \   s   rQ   c                       s8   e Zd Zddeeeeeeeee d	 fddZ  ZS )r   NFr   )	fpdffrom_xfrom_yto_xto_yr7   rI   rJ   r>   c                    sJ   t  ||||	|
 | j| | j| g} fdd|D | _d| _dS )a  
        A shading pattern that creates a linear (axial) gradient in a PDF.

        The gradient is defined by two points: (from_x, from_y) and (to_x, to_y),
        along which the specified colors are interpolated. Optionally, you can set
        a background color, extend the gradient beyond its start or end, and
        specify custom color stop positions via `bounds`.

        Args:
            fpdf (FPDF): The FPDF instance used for PDF generation.
            from_x (int or float): The x-coordinate of the starting point of the gradient,
                in user space units.
            from_y (int or float): The y-coordinate of the starting point of the gradient,
                in user space units.
            to_x (int or float): The x-coordinate of the ending point of the gradient,
                in user space units.
            to_y (int or float): The y-coordinate of the ending point of the gradient,
                in user space units.
            colors (List[str or Tuple[int, int, int]]): A list of colors along which the gradient
                will be interpolated. Colors may be given as hex strings (e.g., "#FF0000") or
                (R, G, B) tuples.
            background (str or Tuple[int, int, int], optional): A background color to use
                if the gradient does not fully cover the region it is applied to.
                Defaults to None (no background).
            extend_before (bool, optional): Whether to extend the first color beyond the
                starting point (from_x, from_y). Defaults to False.
            extend_after (bool, optional): Whether to extend the last color beyond the
                ending point (to_x, to_y). Defaults to False.
            bounds (List[float], optional): An optional list of floats in the range (0, 1)
                that represent gradient stops for color transitions. The number of bounds
                should be two less than the number of colors (for multi-color gradients).
                Defaults to None, which evenly distributes color stops.
        c                    s   g | ]} j | d qS r.   kr/   rh   r   r   rT      s     z+LinearGradient.__init__.<locals>.<listcomp>r   Nr   r   hrG   rD   )r   rh   ri   rj   rk   rl   r7   rE   rI   rJ   r>   rG   r   rp   r   r      s    .zLinearGradient.__init__)NFFNr#   r$   r%   rN   r   rP   r   r(   r   r   r   r   r      s   	    r   c                       s<   e Zd Zddeeeeeeeeeee d fddZ  ZS )r   NFr   )rh   start_circle_xstart_circle_ystart_circle_radiusend_circle_xend_circle_yend_circle_radiusr7   rI   rJ   r>   c                    sN   t  ||	|
|| | j| || j| |g} fdd|D | _d| _dS )a  
        A shading pattern that creates a radial (or circular/elliptical) gradient in a PDF.

        The gradient is defined by two circles (start and end). Colors are blended from the
        start circle to the end circle, forming a radial gradient. You can optionally set a
        background color, extend the gradient beyond its circles, and provide custom color
        stop positions via `bounds`.

        Args:
            fpdf (FPDF): The FPDF instance used for PDF generation.
            start_circle_x (int or float): The x-coordinate of the inner circle's center,
                in user space units.
            start_circle_y (int or float): The y-coordinate of the inner circle's center,
                in user space units.
            start_circle_radius (int or float): The radius of the inner circle, in user space units.
            end_circle_x (int or float): The x-coordinate of the outer circle's center,
                in user space units.
            end_circle_y (int or float): The y-coordinate of the outer circle's center,
                in user space units.
            end_circle_radius (int or float): The radius of the outer circle, in user space units.
            colors (List[str or Tuple[int, int, int]]): A list of colors along which the gradient
                will be interpolated. Colors may be given as hex strings (e.g., "#FF0000") or
                (R, G, B) tuples.
            background (str or Tuple[int, int, int], optional): A background color to display
                if the gradient does not fully cover the region it's applied to. Defaults to None
                (no background).
            extend_before (bool, optional): Whether to extend the gradient beyond the start circle.
                Defaults to False.
            extend_after (bool, optional): Whether to extend the gradient beyond the end circle.
                Defaults to False.
            bounds (List[float], optional): An optional list of floats in the range (0, 1) that
                represent gradient stops for color transitions. The number of bounds should be one
                less than the number of colors (for multi-color gradients). Defaults to None,
                which evenly distributes color stops.
        c                    s   g | ]} j | d qS rm   rn   r/   rp   r   r   rT   $  s     z+RadialGradient.__init__.<locals>.<listcomp>r:   Nrq   )r   rh   rt   ru   rv   rw   rx   ry   r7   rE   rI   rJ   r>   rG   r   rp   r   r      s    2zRadialGradient.__init__)NFFNrs   r   r   r   r   r      s"       r   N)r&   abcr   typingr   r   r   r   r   Zdrawingr	   r
   r   r   Zsyntaxr   r   r   rh   r   r   r)   r9   rB   rQ   r   r   r   r   r   r   <module>   s   W5