o
    gimZ                     @   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 G d
d deZG dd deZdS )zs
This class represents a common base for all LayoutElement implementations
that attempt to represent tabular data.
    N)Decimal)Color)HexColor)	Rectangle)	Alignment)LayoutElement)Pagec                )       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dedededdddfde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dededededeje deje def( fddZdedefdd Zd!d"deddfd#d$Zd%edd fd&d'Zdefd(d)Zdef fd*d+Zdefd,d-Zdeje fd.d/Zdeje fd0d1Zdeje fd2d3Zdeje fd4d5Zdeje fd6d7Zdeje fd8d9Zdeje fd:d;Zdefd<d=Zdejejeef  fd>d?Z  ZS )@	TableCellz8
    This class represents a single cell of a table
    NT000000r      layout_element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column_spanpadding_bottompadding_leftpadding_rightpadding_toppreferred_heightpreferred_widthrow_spanc                    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tjdtddtddtddtdd|d|d|d|dtj || _|dkshJ |dksnJ || _	|| _
g | _d | _d | _|| _d | _d | _|| _d | _d S )Nr   r   r   r   r   r   r   r   r   r   r   font	Helvetica
font_colorz#000000	font_size   horizontal_alignmentmargin_bottomr   margin_leftmargin_right
margin_topr   r   r   r   vertical_alignmentr    )superr	   __init__r   r   r   	JUSTIFIEDTOP_layout_element	_row_span_column_span_table_coordinates
_min_width
_max_width_preferred_width_min_height_max_height_preferred_height_forced_layout_box)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	__class__r+   V/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/table/table.pyr-      s|   	

zTableCell.__init__available_spacereturnc                 C   s   | j d urttd}| jr|| j7 }| jr|| j7 }td}| jr%|| j7 }| jr-|| j7 }t| j  | j	 | jr;| jntd | j 
 | j | jrL| jntd ttd| j  | j	 | j | ttd| j  | j | j | S | j|S )Nr   )r:   r   _border_left_border_width_border_right_border_top_border_bottomr   get_x_padding_leftget_y_padding_bottommax	get_width_padding_right
get_height_padding_topr0   get_layout_box)r;   r?   horizontal_border_widthvertical_border_widthr+   r+   r>   _get_content_boxi   sX   




zTableCell._get_content_boxpager   c                 C   s   | j || d S N)r0   paint)r;   rS   r?   r+   r+   r>   _paint_content_box   s   zTableCell._paint_content_box
layout_boxc                 C   s
   || _ | S rT   )r:   )r;   rW   r+   r+   r>   _set_layout_box   s   zTableCell._set_layout_boxc                 C      | j S )z
        This function returns the column span. This specifies the number of columns a cell should span.
        :return:    The column span
        )r2   r;   r+   r+   r>   get_column_span      zTableCell.get_column_spanc                    s    | j dur| j S tt| |S )z
        This function returns the previous result of layout
        :return:    the Rectangle that was the result of the previous layout operation
        N)r:   r,   r	   rO   )r;   r?   r<   r+   r>   rO      s   
zTableCell.get_layout_boxc                 C   rY   )z
        This function returns the LayoutElement in this TableCell
        :return:    the LayoutElement inside this TableCell
        )r0   rZ   r+   r+   r>   get_layout_element   r\   zTableCell.get_layout_elementc                 C   rY   )a  
        This function returns the maximum width this TableCell (determined by the LayoutElement inside it).
        This function returns None if the maximum width has not yet been determined.
        :return:    the maximum width of this TableCell
        )r5   rZ   r+   r+   r>   get_max_width      zTableCell.get_max_widthc                 C   rY   )a  
        This function returns the maximum height this TableCell (determined by the LayoutElement inside it).
        This function returns None if the maximum height has not yet been determined.
        :return:    the maximum height of this TableCell
        )r8   rZ   r+   r+   r>   get_max_height   r_   zTableCell.get_max_heightc                 C   rY   )a  
        This function returns the minimum width this TableCell (determined by the LayoutElement inside it).
        This function returns None if the minimum width has not yet been determined.
        :return:    the minimum width of this TableCell
        )r4   rZ   r+   r+   r>   get_min_width   r_   zTableCell.get_min_widthc                 C   rY   )a  
        This function returns the minimum height this TableCell (determined by the LayoutElement inside it).
        This function returns None if the minimum height has not yet been determined.
        :return:    the minimum height of this TableCell
        )r7   rZ   r+   r+   r>   get_min_height   r_   zTableCell.get_min_heightc                 C   rY   )a   
        This function returns the preferred height of this TableCell (determined by a previous run of the LayoutElement framework).
        This function returns None if the preferred height has not yet been determined.
        :return:    the preferred height of this TableCell
        )r9   rZ   r+   r+   r>   get_preferred_height   r_   zTableCell.get_preferred_heightc                 C   rY   )a  
        This function returns the preferred width of this TableCell (determined by a previous run of the LayoutElement framework).
        This function returns None if the preferred width has not yet been determined.
        :return:    the preferred width of this TableCell
        )r6   rZ   r+   r+   r>   get_preferred_width   r_   zTableCell.get_preferred_widthc                 C   s
   | j  S )z
        This function returns the previous result of layout of this LayoutElement
        :return:    the Rectangle that was the result of the previous layout operation
        )r0   get_previous_layout_boxrZ   r+   r+   r>   re      s   
z!TableCell.get_previous_layout_boxc                 C   rY   )z
        This function returns the row span. This specifies the number of rows a cell should span.
        :return:    The row span
        )r1   rZ   r+   r+   r>   get_row_span   r\   zTableCell.get_row_spanc                 C   rY   )a  
        This function returns the coordinates (in the Table) of this TableCell.
        These coordinates may be a single typing.Tuple[int, int] or a typing.List of such
        tuples in case of row/column span.
        :return:    the table coordinates
        )r3   rZ   r+   r+   r>   get_table_coordinates   s   zTableCell.get_table_coordinates) __name__
__module____qualname____doc__r   r   r   typingOptionalr   boolintr-   r   rR   rV   rX   r[   rO   r]   r^   r`   ra   rb   rc   rd   re   rf   ListTuplerg   __classcell__r+   r+   r<   r>   r	      s    	
M(	&r	   c                /       s4  e Zd ZdZddeddededededddedejededededededededejfde	de	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dededededededededef. fddZd e	d!e	d"e
je fd#d$Zd%ed"d fd&d'Z	dId(ed)ed*e
je d"d fd+d,Zd!e	d"e
je fd-d.Zd e	d"e
je fd/d0Zd"e	fd1d2Zd"e	fd3d4ZdJd5d6ZdJd7d8ZdJd9d:Zed;fd<ed"d fd=d>Zd	ed"d fd?d@Zded"d fdAdBZded"d fdCdDZdeded
eded"d f
dEdFZ dedededed"d f
dGdHZ!  Z"S )KTablez
    This class represents a common base for all LayoutElement implementations
    that attempt to represent tabular data.
    NFr
   r   r   number_of_rowsnumber_of_columnsr   r   r   r   r   r   r   r   r   r   r   r%   r&   r'   r(   r)   r   r   r   r   r*   c                    s   t t| jdi d|d|d|d|d|d|d|	d|
d	|d
|d|dtdd|d|d|d|d|d|d|d|d|d| |dksSJ |dksYJ || _|| _g | _d S )Nr   r   r   r   r   r   r   r   r   r   r   r#   r$   r%   r&   r'   r(   r)   r   r   r   r   r*   r   r+   )r,   rs   r-   r   _number_of_rows_number_of_columns_content)r;   rt   ru   r   r   r   r   r   r   r   r   r   r   r   r%   r&   r'   r(   r)   r   r   r   r   r*   r<   r+   r>   r-     sd   	

zTable.__init__rowcolumnr@   c                    s8   | j D ]}t fdd| D dkr|  S qd S )Nc                    s(   g | ]}|d  kr|d  kr|qS r   r   r+   .0prz   ry   r+   r>   
<listcomp>E  s
    z'Table._get_cells_at.<locals>.<listcomp>r   )rx   lenrg   )r;   ry   rz   tr+   r   r>   _get_cells_atA  s   
	
zTable._get_cells_atr   c                    s   t |ts	t|}t |tsJ j| | }jdu r$| _tfddtdj	D  g 
 D ]} fdd| D  q:tfddtdjD }td| D ]}td| D ]}|j | || f qjqaS )z
        This function adds the given LayoutElement to this Table.
        This function returns self.
        :param layout_element:  the LayoutElement to be added
        :return:                self
        Nc                    s.   g | ]}t d d  |D  jk r|qS )c                 S   s   g | ]}|  qS r+   )r[   )r}   yr+   r+   r>   r   n  s    z(Table.add.<locals>.<listcomp>.<listcomp>)sumget_cells_at_rowrw   r}   xrZ   r+   r>   r   k  s    zTable.add.<locals>.<listcomp>r   c                    s    g | ]}|d   kr|d qS r{   r+   r   )first_incomplete_rowr+   r>   r   v  s
    c                    s   g | ]}| vr|qS r+   r+   r   )occupied_cols_in_rowr+   r>   r   ~  s
    )
isinstancer	   rx   appendr]   
_font_sizeget_font_sizeminrangerv   r   extendrg   rw   rf   r[   r3   )r;   r   inner_layout_elementcfirst_empty_columnijr+   )r   r   r;   r>   addT  s>   
	



	


	z	Table.addeven_row_colorodd_row_colorheader_row_colorc                 C   sh   |du r|}|dusJ t d| jD ]}| |D ]}|dkr#||_q|d dkr-||_q||_qq| S )a  
        This function colors the Table with the classic "zebra stripes"
        e.a. one color for all even rows, and a contrasting color for the odd rows.
        This function returns self.
        :param even_row_color:      the Color to be used for even rows
        :param odd_row_color:       the Color to be used for odd rows
        :param header_row_color:    the Color to be used for the header row, if None is specified the even_row_color will be used
        :return:                    self
        Nr      )r   rv   r   _background_color)r;   r   r   r   rtcr+   r+   r>   even_odd_row_colors  s   zTable.even_odd_row_colorsc                    <   g }| j D ]}t fdd| D dkr|| q|S )z
        This function returns all TableCell elements at a given column
        :param column:  the specified column
        :return:        all TableCell elements at the given column
        c                       g | ]
}|d   kr|qS )r   r+   r|   rz   r+   r>   r         z-Table.get_cells_at_column.<locals>.<listcomp>r   rx   r   rg   r   )r;   rz   outr   r+   r   r>   get_cells_at_column     

zTable.get_cells_at_columnc                    r   )z
        This function returns all TableCell elements at a given row
        :param row:  the specified row
        :return:        all TableCell elements at the given row
        c                    r   )r   r+   r|   ry   r+   r>   r     r   z*Table.get_cells_at_row.<locals>.<listcomp>r   r   )r;   ry   r   r   r+   r   r>   r     r   zTable.get_cells_at_rowc                 C   rY   )zu
        This function returns the number of columns in this Table
        :return:    the number of columns
        )rw   rZ   r+   r+   r>   get_number_of_columns  r\   zTable.get_number_of_columnsc                 C   rY   )zo
        This function returns the number of rows in this Table
        :return:    the number of rows
        )rv   rZ   r+   r+   r>   get_number_of_rows  r\   zTable.get_number_of_rowsc                 C   s   | j D ]}d|_d|_d|_d|_q| dD ]}d|_q| | jd D ]}d|_q%| dD ]}d|_q0| | jd D ]}d|_q>| S )z
        This method sets the border(s) on all TableCell objects in this Table
        except for the borders that form the outside edge of the Table
        :return:    self
        Tr   Fr   )	rx   rD   rC   rE   rA   r   rv   r   rw   )r;   r   r   r+   r+   r>   internal_borders  s   
zTable.internal_bordersc                 C   s   |  dddd | S )zr
        This method unsets the border(s) on all TableCell objects in this Table
        :return:    self
        F)set_borders_on_all_cellsrZ   r+   r+   r>   
no_borders  s   zTable.no_bordersc                 C   sp   |    | dD ]}d|_q	| | jd D ]}d|_q| dD ]}d|_q"| | jd D ]}d|_q0| S )z
        This method unsets the border(s) on all TableCell objects in this Table
        except for the borders that form the outside edge of the Table
        :return:    self
        r   Tr   )	r   r   rD   rv   rE   r   rA   rw   rC   )r;   r   r+   r+   r>   outer_borders  s   zTable.outer_borders   border_radiusc                 C   s   |  dd}|dur||_|  d| jd }|dur||_|  | jd | jd }|dur0||_|  | jd d}|dur@||_| S )z
        This function sets rounded borders on the outside of this Table
        :param border_radius:   the desired border radius
        :return:                self
        r   Nr   )r   _border_radius_top_leftrw   _border_radius_top_rightrv   _border_radius_bottom_right_border_radius_bottom_left)r;   r   ulurlrllr+   r+   r>   outer_borders_rounded   s"   
zTable.outer_borders_roundedc                 C      | j D ]}||_q| S )z
        This method sets the background Color on all TableCell objects in this Table
        :param background_color:    the background Color
        :return:    self
        )rx   r   )r;   r   er+   r+   r>   !set_background_color_on_all_cells     
z'Table.set_background_color_on_all_cellsc                 C   r   )z
        This method sets the border color on all TableCell objects in this Table
        :param border_color:    the border Color
        :return:                self
        )rx   _border_color)r;   r   r   r+   r+   r>   set_border_color_on_all_cells'  r   z#Table.set_border_color_on_all_cellsc                 C   s"   |dksJ | j D ]}||_q	| S )z
        This method sets the border width on all TableCell objects in this Table
        :param border_width:    the border width
        :return:                self
        r   )rx   rB   )r;   r   r   r+   r+   r>   set_border_width_on_all_cells1  s   
z#Table.set_border_width_on_all_cellsc                 C   (   | j D ]}||_||_||_||_q| S )a  
        This method sets the border(s) on all TableCell objects in this Table
        :param border_top:          True if a top border should be drawn, False otherwise
        :param border_right:        True if a right border should be drawn, False otherwise
        :param border_bottom:       True if a bottom border should be drawn, False otherwise
        :param border_left:         True if a left border should be drawn, False otherwise
        :return:                    self
        )rx   rD   rC   rE   rA   )r;   r   r   r   r   r   r+   r+   r>   r   <     
zTable.set_borders_on_all_cellsc                 C   r   )a@  
        This method sets the padding on all TableCell objects in this Table
        :param padding_top:     the top padding
        :param padding_right:   the right padding
        :param padding_bottom:  the bottom padding
        :param padding_left:    the left padding
        :return:                self
        )rx   rN   rL   rI   rG   )r;   r   r   r   r   r   r+   r+   r>   set_padding_on_all_cellsR  r   zTable.set_padding_on_all_cellsrT   )r@   rs   )#rh   ri   rj   rk   r   r   r   LEFTr/   ro   rl   rm   r   rn   r-   r	   r   r   r   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   rr   r+   r+   r<   r>   rs      s    	
<?






rs   )rk   rl   decimalr   borb.pdf.canvas.color.colorr   r   "borb.pdf.canvas.geometry.rectangler   %borb.pdf.canvas.layout.layout_elementr   r   borb.pdf.page.pager   r	   rs   r+   r+   r+   r>   <module>   s    i