o
    gij4                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddl	mZ dd	lmZ dd
lmZ G dd deZdS )z
This class represents a collection of shqpes (objects of type ConnectedShape or DisconnectedShape).
It has convenience methods to calculate width and height, perform scaling, etc
    N)Decimal)Color)HexColor)	Rectangle)	Alignment)LayoutElement)ConnectedShape)DisconnectedShapec                3       s  e Zd ZdZddeddededededddeddejdededededededededdejfde	j
e	jeef  de	je d	ed
ededededededededede	je dede	je de	je de	je de	je de	je dedededede	je def2 fd d!Zd"ed#efd$d%Zd&d'd(ed#dfd)d*Zd#efd+d,Zd#efd-d.Zd/ed0ed#d fd1d2Z	3d;d4ed5ed6ed#d fd7d8Z	3d;d4ed5ed6ed#d fd9d:Z  ZS )<Shapesz
    This class represents a collection of shapes (objects of type ConnectedShape or DisconnectedShape).
    It has convenience methods to calculate width and height, perform scaling, etc
    NF000000r      shapesbackground_colorborder_bottomborder_colorborder_leftborder_radius_bottom_leftborder_radius_bottom_rightborder_radius_top_leftborder_radius_top_rightborder_right
border_topborder_width
fill_colorhorizontal_alignment
line_widthmargin_bottommargin_leftmargin_right
margin_toppadding_bottompadding_leftpadding_rightpadding_topstroke_colorvertical_alignmentc                    s  t t| jdi d|d|d|d|d|d|d|d|	d	|
d
|d|dddtddtdd|d|d|d|d|d|d|d|d|d| || _| jD ](}|d urd||_t|trw|d urp||_	|d urw||_
t|tr|d ur||_
q[d S )Nr   r   r   r   r   r   r   r   r   r   r   font	Helvetica
font_colorz#000000	font_size   r   r   r   r   r   r    r!   r"   r#   r%    )superr
   __init__r   r   _shapes_line_width
isinstancer   _fill_color_stroke_colorr	   )selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   s	__class__r+   W/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/shape/shapes.pyr-      s   	



zShapes.__init__available_spacereturnc                 C   s.   t | | |  |   |  |  S )N)r   get_xget_y
get_height	get_width)r3   r8   r+   r+   r7   _get_content_boxf   s   zShapes._get_content_boxpagePagecontent_boxc           
      C   s  |  | | |  |    d}| jD ]}t|tr|jp$td	 }|j
p-td	 }|dt|jt|jt|jt|jt|jt|jt|jf 7 }|dt|jd d t|jd d f 7 }|jdd  D ]}|dt|d t|d f 7 }qod	}|jd u rd
}|j
d u rd}|d| 7 }|d7 }t|tr|jptd	 }|dt|jt|jt|jt|jf 7 }|jD ]$}	|dt|	d d t|	d d t|	d d t|	d d f 7 }q|d7 }q|| d S )N r   ffffffz! q %f %f %f RG  %f %f %f rg %f w z%f %f m r   r   z	 %f %f l BFSz %s z Q z q %f %f %f RG %d w z %f %f m %f %f l z S Q )move_tor:   r;   r<   r.   r0   r   r2   r   to_rgbr1   floatredgreenbluer/   _pointsr	   _linesappend_to_content_stream)
r3   r?   rA   contentcs
stroke_rgbfill_rgbpoperatorlr+   r+   r7   _paint_content_boxn   sb   

	"




zShapes._paint_content_boxc                 C      d}d}| j D ]B}g }t|trdd |jD }t|tr-dd |jD dd |jD  }|du s7t||k r;t|}|du sEt||krIt|}q|dusPJ |dusVJ || S )z_
        This function returns the height of this Shape
        :return:    the height
        Nc                 S      g | ]\}}|qS r+   r+   .0xyr+   r+   r7   
<listcomp>       z%Shapes.get_height.<locals>.<listcomp>c                 S      g | ]\}}|d  qS r   r+   r[   srcdstr+   r+   r7   r^          c                 S      g | ]\}}|d  qS ra   r+   rb   r+   r+   r7   r^          r.   r0   r   rM   r	   rN   minmax)r3   min_ymax_yrQ   ysr+   r+   r7   r<      $   


zShapes.get_heightc                 C   rX   )z]
        This function returns the width of this Shape
        :return:    the width
        Nc                 S      g | ]\}}|qS r+   r+   rZ   r+   r+   r7   r^      r_   z$Shapes.get_width.<locals>.<listcomp>c                 S   r`   r   r+   rb   r+   r+   r7   r^      re   c                 S   rf   rp   r+   rb   r+   r+   r7   r^      rg   rh   )r3   min_xmax_xrQ   xsr+   r+   r7   r=      rn   zShapes.get_widthlower_left_xlower_left_yc                    sD  d}d}| j D ]\}g }g }t|tr"dd |jD }dd |jD }t|trGdd |jD dd |jD  }dd |jD dd |jD  }|du sQt||k rUt|}|du s_t||k rct|}q|dusjJ |duspJ ||  || | j D ]$}t|tr fd	d|jD |_t|tr fd
d|jD |_q{| S )a  
        This method translates this Shape so its lower left corner aligns with the given coordinates
        :param lower_left_x:    the desired lower left x-coordinate
        :param lower_left_y:    the desired lower left y-coordinate
        :return:    self
        Nc                 S   ro   r+   r+   rZ   r+   r+   r7   r^      r_   z"Shapes.move_to.<locals>.<listcomp>c                 S   rY   r+   r+   rZ   r+   r+   r7   r^      r_   c                 S   r`   rp   r+   rb   r+   r+   r7   r^      re   c                 S   rf   rp   r+   rb   r+   r+   r7   r^      rg   c                 S   r`   ra   r+   rb   r+   r+   r7   r^      re   c                 S   rf   ra   r+   rb   r+   r+   r7   r^      rg   c                    s$   g | ]}|d    |d  fqS r   r   r+   r[   r\   delta_xdelta_yr+   r7   r^      s   $ c                    sL   g | ]"}|d  d    |d  d  f|d d    |d d  ffqS rv   r+   rw   rx   r+   r7   r^      s    )r.   r0   r   rM   r	   rN   ri   )r3   rt   ru   rq   rk   rQ   rs   rm   r+   rx   r7   rG      sB   





	zShapes.move_toT	max_width
max_heightpreserve_aspect_ratioc                    s   ||    ||    |rt  dk r?| jD ]"}t|tr.fdd|jD |_t|tr>fdd|jD |_q dk ri| jD ]"}t|trX fdd|jD |_t|trh fdd|jD |_qF| S )a_  
        This method scales this Shape down to fit a given max. width / height
        :param max_width:               the maximum width
        :param max_height:              the maximum height
        :param preserve_aspect_ratio:   True if the aspect ratio should be preserved, False otherwise
        :return:                        self
        r   c                        g | ]}|d    |d fqS rv   r+   rw   w_scaler+   r7   r^          z%Shapes.scale_down.<locals>.<listcomp>c                    D   g | ]}|d  d    |d  d f|d d    |d d ffqS rv   r+   rw   r   r+   r7   r^   !      6c                        g | ]}|d  |d   fqS rv   r+   rw   h_scaler+   r7   r^   (  r   c                    D   g | ]}|d  d  |d  d   f|d d  |d d   ffqS rv   r+   rw   r   r+   r7   r^   *  r   	r=   r<   ri   r.   r0   r   rM   r	   rN   r3   r{   r|   r}   r4   r+   r   r   r7   
scale_down
  0   








zShapes.scale_downc                    s   ||    ||    |rt  dkr?| jD ]"}t|tr.fdd|jD |_t|tr>fdd|jD |_q dkri| jD ]"}t|trX fdd|jD |_t|trh fdd|jD |_qF| S )a]  
        This method scales this Shape up to fit a given max. width / height
        :param max_width:               the maximum width
        :param max_height:              the maximum height
        :param preserve_aspect_ratio:   True if the aspect ratio should be preserved, False otherwise
        :return:                        self
        r   c                    r~   rv   r+   rw   r   r+   r7   r^   E  r   z#Shapes.scale_up.<locals>.<listcomp>c                    r   rv   r+   rw   r   r+   r7   r^   G  r   c                    r   rv   r+   rw   r   r+   r7   r^   N  r   c                    r   rv   r+   rw   r   r+   r7   r^   P  r   r   r   r+   r   r7   scale_up0  r   zShapes.scale_up)T)__name__
__module____qualname____doc__r   r   r   LEFTTOPtypingListUnionr   r	   Optionalr   boolr-   r   r>   rW   r<   r=   rG   r   r   __classcell__r+   r+   r5   r7   r
      s    	
I=5
*r
   )r   r   decimalr   borb.pdf.canvas.color.colorr   r   "borb.pdf.canvas.geometry.rectangler   %borb.pdf.canvas.layout.layout_elementr   r   ,borb.pdf.canvas.layout.shape.connected_shaper   /borb.pdf.canvas.layout.shape.disconnected_shaper	   r
   r+   r+   r+   r7   <module>   s   