
    'j}                       d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ e	rd dlmZ d d	lmZ d
ZdZdZdZdZdZ dZ!dZ"dZ#dZ$ej%        Z&dZ'dZ(dZ)dZ*dZ+dZ,dZ- e
dd          Z. G d d          Z/ G d d          Z0 G d d           Z1 G d! d"          Z2 G d# d$          Z3 G d% d&          Z4 G d' d          Z5e5Z6 G d( d)e5          Z7 G d* d+e5          Z8	 d4d5d3Z9dS )6    )annotations)AnyCallableIterableIteratorOptionalSequenceTYPE_CHECKINGTypeVar)deepcopy)const)MTextEntityAlignmentMAP_MTEXT_ALIGN_TO_FLAGS)MTextSurrogate)UVecVec2)BlockLayout)GenericLayoutTypeTABLEBACKGROUNDTABLECONTENT	TABLEGRIDSTANDARD      ?g      @gffffff?g      ?Ng?   BYLAYERT2   TCell)boundc                     e Zd ZdZeedfd?dZd@dZd@dZ	 	 dAdBdZ		 	 	 dCdDdZ
edEd            ZedEd            ZdFd"ZdGd$ZdHd%Z	 	 	 dIdJd*ZdKd-Zeej        dd.d/ej        fdLd5            ZdKd6ZdMd:ZdNdOd>ZdS )PTablePaintera  The TablePainter class renders tables build from DXF primitives.

    The TablePainter instance contains all the data cells.

    Args:
        insert: insert location as or :class:`~ezdxf.math.UVec`
        nrows: row count
        ncols: column count
        cell_width: default cell width in drawing units
        cell_height: default cell height in drawing units
        default_grid: draw a grid of solid lines if ``True``, otherwise
            draw only explicit defined borders, the default grid has a
            priority of 50.

    Tinsertr   nrowsintncolsc                   t          |          | _        || _        || _        |g|z  | _        |g|z  | _        t          | _        t          | _	        t          | _        dt                      i| _        |s-|                     d          }|                    dddd           i | _        g | _        t%          |           | _        d S )NdefaultF)r   r"   r#   r%   row_heights
col_widthsDEFAULT_TABLE_BG_LAYERbg_layer_nameDEFAULT_TABLE_FG_LAYERfg_layer_nameDEFAULT_TABLE_GRID_LAYERgrid_layer_name	CellStylestylesget_cell_styleset_border_status_cellsframesr   
empty_cell)selfr"   r#   r%   
cell_widthcell_heightdefault_griddefault_styles           S/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/tablepainter.py__init__zTablePainter.__init__D   s     6ll

)4(=(2|e';"8"8$<-6	,D 	H //	::M++E5%GGG35#%t**    indexvaluefloatc                4    t          |          | j        |<   dS )zSet column width in drawing units of the given column index.

        Args:
            index: zero based column index
            value: new column width in drawing units

        N)rA   r)   r7   r?   r@   s      r<   set_col_widthzTablePainter.set_col_width^   s     "'ur>   c                4    t          |          | j        |<   dS )zSet row height in drawing units of the given row index.

        Args:
            index: zero based row index
            value: new row height in drawing units
        N)rA   r(   rC   s      r<   set_row_heightzTablePainter.set_row_heighth   s     #(,,r>      rH   r'   rowcoltextstrspantuple[int, int]returnTextCellc                V    t          | |||          }|                     |||          S )zFactory method to create a new text cell at location (row, col),
        with `text` as content, the `text` can be a line breaks ``'\n'``.
        The final cell can spread over several cells defined by the argument
        `span`.

        )stylerM   )rP   set_cell)r7   rI   rJ   rK   rM   rR   cells          r<   	text_cellzTablePainter.text_cellq   s0     d%d;;;}}S#t,,,r>   Nblockdefr   	BlockCellc                `    |i }t          | ||||          }|                     |||          S )aA  Factory method to Create a new block cell at position (row, col).

        Content is a block reference inserted by an INSERT entity,
        attributes will be added if the block definition contains ATTDEF.
        Assignments are defined by attribs-key to attdef-tag association.

        Example: attribs = {'num': 1} if an ATTDEF with tag=='num' in
        the block definition exists, an attrib with text=str(1) will be
        created and added to the insert entity.

        The cell spans over 'span' cells and has the cell style with the
        name 'style'.
        N)rR   attribsrM   )rW   rS   )r7   rI   rJ   rV   rM   rY   rR   rT   s           r<   
block_cellzTablePainter.block_cell   sE    , ?G(%t
 
 
 }}S#t,,,r>   c                *    t          | j                  S )zReturns the total table width.)sumr)   r7   s    r<   table_widthzTablePainter.table_width   s     4?###r>   c                *    t          | j                  S )zReturns the total table height.)r\   r(   r]   s    r<   table_heightzTablePainter.table_height   s     4#$$$r>   rT   r   c                P    |                      ||          \  }}|| j        ||f<   |S )z%Insert a cell at position (row, col).)validate_indexr4   r7   rI   rJ   rT   s       r<   rS   zTablePainter.set_cell   s0    &&sC00S $CHr>   r   c                    |                      ||          \  }}	 | j        ||f         S # t          $ r
 | j        cY S w xY w)z Get cell at location (row, col).)rb   r4   KeyErrorr6   r7   rI   rJ   s      r<   get_cellzTablePainter.get_cell   sZ    &&sC00S	#;sCx(( 	# 	# 	#?"""	#s   * >>c                    t          |          }t          |          }|dk     s|| j        k    s|dk     s|| j        k    rt          d          ||fS )Nr   zcell index out of range)r$   r#   r%   
IndexErrorrf   s      r<   rb   zTablePainter.validate_index   sY    #hh#hh77cTZ''377cTZ6G6G6777Cxr>   rH   widthheightFramec                h    t          | ||f||f|          }| j                            |           |S )zCreates a frame around the give cell area, starting at (row, col) and
        covering `width` columns and `height` rows. The `style` argument is the
        name of a :class:`CellStyle`.
        )posrM   rR   )rl   r5   append)r7   rI   rJ   rj   rk   rR   frames          r<   rp   zTablePainter.frame   s=     dc
&%NNN5!!!r>   namer0   c                    t          |t                    r|dk    s
J d            t          |                     d                    }|                    |           || j        |<   |S )zFactory method to create a new :class:`CellStyle` object, overwrites
        an already existing cell style.

        Args:
            name: style name as string
            kwargs: see attributes of class :class:`CellStyle`

         z!name has to be a non-empty stringr'   )
isinstancerL   r   r2   updater1   )r7   rq   kwargsrR   s       r<   new_cell_stylezTablePainter.new_cell_style   sl     tS!!	/&*bjjj. '1j0#D$7$7	$B$BCCV!Dr>   d   r   colorprioritylinetype
lineweightBorderStylec                h    t                      }| |_        ||_        ||_        ||_        ||_        |S )ao  Factory method to create a new border style.

        Args:
            status: ``True`` for visible, ``False`` for invisible
            color: :ref:`ACI`
            linetype: linetype name, default is "BYLAYER"
            lineweight: lineweight as int, default is by layer
            priority: drawing priority, higher priorities cover lower priorities

        )r}   ry   r{   r|   statusrz   )ry   r   rz   r{   r|   border_styles         r<   new_border_stylezTablePainter.new_border_style   s;    $ #}}" (",$ (r>   c                    | j         |         S )zGet cell style by name.)r1   )r7   rq   s     r<   r2   zTablePainter.get_cell_style   s    {4  r>   visibility_mapVisibilityMapIterator[tuple[int, int, Cell]]c                       fd|D             S )zIterate over all visible cellsc              3  P   K   | ] \  }}||                     ||          fV  !d S N)rg   .0rI   rJ   r7   s      r<   	<genexpr>z2TablePainter.iter_visible_cells.<locals>.<genexpr>  sM       
 
4<CS#t}}S#../
 
 
 
 
 
r>    )r7   r   s   ` r<   iter_visible_cellszTablePainter.iter_visible_cells   s.    
 
 
 
@N
 
 
 	
r>   layoutr   Optional[UVec]c                N   | j         }|t          |          | _         t          |           }t          |           }|                     |          D ]6\  }}}|                    ||||           |                    ||||           7|                    ||           || _         dS )zRender table to layout.N)r"   r   r   Gridr   render_cell_backgroundrender_cell_contentrender_lines)	r7   r   r"   insert_backupr   gridrI   rJ   rT   s	            r<   renderzTablePainter.render  s    v,,DK&t,,Dzz"55nEE 	= 	=NCd''S$???$$VS#t<<<<&.111#r>   )r"   r   r#   r$   r%   r$   )r?   r$   r@   rA   )rG   r'   )
rI   r$   rJ   r$   rK   rL   rM   rN   rO   rP   )rG   Nr'   )
rI   r$   rJ   r$   rV   r   rM   rN   rO   rW   )rO   rA   )rI   r$   rJ   r$   rT   r   rO   r   )rI   r$   rJ   r$   rO   r   )rI   r$   rJ   r$   rO   rN   )rH   rH   r'   )
rI   r$   rJ   r$   rj   r$   rk   r$   rO   rl   )rq   rL   rO   r0   )
ry   r$   rz   r$   r{   rL   r|   r$   rO   r}   )r   r   rO   r   r   )r   r   r"   r   )__name__
__module____qualname____doc__DEFAULT_CELL_WIDTHDEFAULT_CELL_HEIGHTr=   rD   rF   rU   rZ   propertyr^   r`   rS   rg   rb   rp   rw   staticmethodr   r   LINEWEIGHT_BYLAYERr   r2   r   r   r   r>   r<   r!   r!   3   s        * &'% % % % %4. . . ./ / / / !'- - - - -, !'- - - - -: $ $ $ X$ % % % X%   # # # #            " ]!2    \2! ! ! !
 
 
 
$ $ $ $ $ $ $r>   r!   c                  P    e Zd ZdZddZd ZddZddZddZddZ	ddZ
ddZdS )r   z)Stores the visibility of the table cells.tabler!   c                b    || _         t                      | _        |                                  dS )z$Create the visibility map for table.N)r   set_hidden_cells_create_visibility_mapr7   r   s     r<   r=   zVisibilityMap.__init__  s-    #(
3655##%%%%%r>   c                    t          |           D ]<\  }}| j                            ||          }|                     |||j                   =dS )z&Set visibility for all existing cells.N)iterr   rg   _set_span_visibilityrM   rc   s       r<   r   z$VisibilityMap._create_visibility_map  sY    T

 	; 	;HC:&&sC00D%%c3	::::	; 	;r>   rI   r$   rJ   rM   rN   c                    |dk    rE|\  }}t          |          D ]0}t          |          D ]}|                     ||z   ||z              1|                     ||           dS )zSet the visibility of the given cell.

        The cell itself is visible, all other cells in the span-range
        (tuple: width, height) are invisible, they are covered by the
        main cell (row, col).
        rG   N)rangehideshow)r7   rI   rJ   rM   r#   r%   rowxcolxs           r<   r   z"VisibilityMap._set_span_visibility"  s     6>>LE5e 6 6!%LL 6 6DIIcDj#*55556 			#sr>   c                b    	 | j                             ||f           dS # t          $ r Y dS w xY w)zShow cell (row, col).N)r   removere   rf   s      r<   r   zVisibilityMap.show3  sJ    	%%sCj11111 	 	 	DD	s     
..rO   Nonec                >    | j                             ||f           dS )zHide cell (row, col).N)r   addrf   s      r<   r   zVisibilityMap.hide:  s#    Sz*****r>   Iterator[tuple[int, int]]c              #     K   t          | j        j                  D ]$}t          | j        j                  D ]}||fV  	%dS )z8Iterate over all cell indices, yields (row, col) tuples.N)r   r   r#   r%   rf   s      r<   iter_all_cellszVisibilityMap.iter_all_cells>  s\      )** 	 	CTZ-..  3h	 	r>   boolc                    ||f| j         vS )z0True if cell (row, col)  is visible, else False.)r   rf   s      r<   is_visible_cellzVisibilityMap.is_visible_cellD  s    Sz!333r>   c                D      fd                                  D             S )zIterate over all visible cells.c              3  R   K   | ]!\  }}                     ||          ||fV  "d S r   )r   r   s      r<   r   z)VisibilityMap.__iter__.<locals>.<genexpr>J  sU       
 
c##C--
#J
 
 
 
 
 
r>   )r   r]   s   `r<   __iter__zVisibilityMap.__iter__H  s:    
 
 
 
"1133
 
 
 	
r>   Nr   r!   )rI   r$   rJ   r$   rM   rN   )rI   r$   rJ   r$   )rI   r$   rJ   r$   rO   r   )rO   r   )rI   r$   rJ   r$   rO   r   )r   r   r   r   r=   r   r   r   r   r   r   r   r   r>   r<   r   r     s        33& & & &; ; ;   "   + + + +   4 4 4 4
 
 
 
 
 
r>   r   c                  h    e Zd ZdZdddZdd
ZddZddZddZ	 dddZ	e
dd            ZddZdS ) r0   zCell style object.

    .. important::

        Always instantiate new styles by the factory method:
        :meth:`TablePainter.new_cell_style`

    NdataOptional[dict[str, Any]]c                   d| _         t          | _        t          | _        t
          | _        t          | _        t          | _
        d| _        d| _        t          j        | _        t           | _        t$          | _        t(          | _        t-                      | _        t-                      | _        t-                      | _        t-                      | _        |r|                     |           d S d S )Nr           F)
text_styleDEFAULT_CELL_CHAR_HEIGHTchar_heightDEFAULT_CELL_LINE_SPACINGline_spacingDEFAULT_CELL_X_SCALEscale_xDEFAULT_CELL_Y_SCALEscale_yDEFAULT_CELL_TEXTCOLOR
text_colorrotationstackedr   
TOP_CENTERalignDEFAULT_CELL_X_MARGINmargin_xDEFAULT_CELL_Y_MARGINmargin_yDEFAULT_CELL_BG_COLORbg_colorr}   lefttoprightbottomru   )r7   r   s     r<   r=   zCellStyle.__init__[  s    $35++0)4
---MM	== ]]
!mm 	KK	 	r>   krL   rO   r   c                    | j         |         S r   )__dict__)r7   r   s     r<   __getitem__zCellStyle.__getitem__  s    }Qr>   vc                r    || j         v r| j                             ||           d S t          d|           )Nzinvalid attribute name: )r   __setitem__re   )r7   r   r   s      r<   r   zCellStyle.__setitem__  sD    M%%a+++++9a99:::r>   dict[str, Any]c                    |                                 D ]\  }}|                     ||           t          | j        t                    s
J d            d S )NzBenum ezdxf.enums.MTextEntityAlignment for text alignments required)itemsr   rt   r   r   )r7   r   r   r   s       r<   ru   zCellStyle.update  sw    JJLL 	# 	#DAqQ""""J,
 
 	P 	PO	P 	P 
 	P 	Pr>   Tc                f    || j         _        || j        _        || j        _        || j        _        dS )z'Set status of all cell borders at once.N)r   r   r   r   r   )r7   r   r   r   r   s        r<   r3   zCellStyle.set_border_status  s/    	!
#r>   rR   r}   c                <    d|fd|fd|fd|ffD ]\  }}|r|| |<   dS )z.Set border styles of all cell borders at once.r   r   r   r   Nr   )r7   rR   r   r   r   r   borderr   s           r<   set_border_stylezCellStyle.set_border_style  sU    
 TNeCLv	
 	% 	%NFF  %$V	% 	%r>   c                     t                      S r   )r}   r   r>   r<   get_default_border_stylez"CellStyle.get_default_border_style  s    }}r>   rN   c                &    t           | j                 S r   )r   r   r]   s    r<   get_text_align_flagszCellStyle.get_text_align_flags  s    '
33r>   r   )r   r   )r   rL   rO   r   )r   rL   r   r   )r   r   )TTTT)rR   r}   )rO   r}   rO   rN   )r   r   r   r   r=   r   r   ru   r3   r   r   r   r   r   r>   r<   r0   r0   Q  s         " " " " "H       ; ; ; ;P P P P$ $ $ $ KO% % % % %    \4 4 4 4 4 4r>   r0   c                  0    e Zd ZdZeeeej        e	fdd	Z
d
S )r}   zBorder style class.

    .. important::

        Always instantiate new border styles by the factory method:
        :meth:`TablePainter.new_border_style`

    r   r   ry   r$   r{   rL   rz   c                L    || _         || _        || _        || _        || _        d S r   )r   ry   r{   r|   rz   )r7   r   ry   r{   r|   rz   s         r<   r=   zBorderStyle.__init__  s+     
 $ r>   N)r   r   ry   r$   r{   rL   rz   r$   )r   r   r   r   DEFAULT_BORDER_STATUSDEFAULT_BORDER_COLORDEFAULT_BORDER_LINETYPEr   r   DEFAULT_BORDER_PRIORITYr=   r   r>   r<   r}   r}     sK          -)/+/! ! ! ! ! ! !r>   r}   c                      e Zd ZdZd7dZd8dZd9dZd:dZd:dZd;dZ	d<dZ
d<dZd=dZd>dZd>dZd?dZd@d"Zd@d#ZdAd&ZdBd)ZdCd.ZdDd1ZdEd4ZdFd5Zd6S )Gr   z8Grid contains the graphical representation of the table.r   r!   c                    || _         |                                 | _        |                                 | _        g | _        g | _        t          dddd          | _        d S )NFi  r   r   )r   rz   r{   ry   )	r   _calc_col_poscol_pos_calc_row_posrow_pos
_x_borders
_y_bordersr}   	no_borderr   s     r<   r=   zGrid.__init__  sd    #(
$($6$6$8$8$($6$6$8$8 .0
 .0$3!
 
 
r>   x_borderr}   y_borderc                p    | j         j        dz   | j         j        dz   z  }|g|z  | _        |g|z  | _        dS )z@Init the _hborders with  <hborder> and _vborders with <vborder>.rH   N)r   r#   r%   r   r   )r7   r   r  border_counts       r<   _init_borderszGrid._init_borders  sB     "Z-1dj6F6JK#*|3#*|3r>   rI   r$   rJ   rO   c                ,    || j         j        dz   z  |z   S )zCCalculate linear index for border arrays _x_borders and _y_borders.rH   )r   r%   rf   s      r<   _border_indexzGrid._border_index  s    dj&*+c11r>   r   c                <    |                      | j        |||          S )zYSet <border_style> for the horizontal border element above
        <row>, <col>.
        )_set_border_styler   r7   rI   rJ   r   s       r<   set_x_borderzGrid.set_x_border        %%dosCNNNr>   c                <    |                      | j        |||          S )zYSet <border_style> for the vertical border element left of
        <row>, <col>.
        )r  r   r	  s       r<   set_y_borderzGrid.set_y_border  r  r>   borderslist[BorderStyle]c                p    |                      ||          }||         }|j        |j        k    r|||<   dS dS )z1Set <border_style> for <row>, <col> in <borders>.N)r  rz   )r7   r  rI   rJ   r   border_indexactual_borderstyles          r<   r  zGrid._set_border_style  sO     ))#s33$\2 $6$???$0GL!!! @?r>   c                :    |                      | j        ||          S )zGet the horizontal border element above <row>, <col>.
        Last grid line (below <nrows>) is the element above of <nrows+1>.
        )_get_borderr   rf   s      r<   get_x_borderzGrid.get_x_border       c:::r>   c                :    |                      | j        ||          S )zGet the vertical border element left of <row>, <col>.
        Last grid line (right of <ncols>) is the element left of <ncols+1>.
        )r  r   rf   s      r<   get_y_borderzGrid.get_y_border  r  r>   c                :    ||                      ||                   S )z2Get border element at <row>, <col> from <borders>.)r  )r7   r  rI   rJ   s       r<   r  zGrid._get_border  s     t))#s3344r>   list[float]c                l    g }| j         j        j        }t          || j         j        |j                   |S )zBCalculate the x-axis coords of the grid lines between the columns.)r   r"   x
sum_fieldsr)   ro   )r7   r   start_xs      r<   r   zGrid._calc_col_pos  s2    !*,7DJ17>BBBr>   c                n    g }| j         j        j        }t          || j         j        |j        d           |S )z?Calculate the y-axis coords of the grid lines between the rows.g      )r   r"   yr  r(   ro   )r7   r   start_ys      r<   r   zGrid._calc_row_pos$  s4    !*,7DJ2GNDIIIr>   rM   rN   !tuple[float, float, float, float]c                    | j         |         }| j         ||d         z            }| j        |         }| j        ||d         z            }||||fS )zGet the coordinates of the cell <row>,<col> as absolute drawing units.

        :return: a tuple (left, right, top, bottom)
        r   rH   )r   r   )r7   rI   rJ   rM   r   r   r   r   s           r<   cell_coordszGrid.cell_coords+  sS     l3cDGm,|C S47]+UC''r>   r   r   rT   r   c                    |j         }|j        dS |                     |||j                  \  }}}}	|                    ||f||	f||f||	ff|j        | j        j        d           dS )z:Render the cell background for (row, col) as SOLID entity.N)ry   layer)points
dxfattribs)rR   r   r$  rM   	add_solidr   r+   )
r7   r   rI   rJ   rT   rR   r   r   r   r   s
             r<   r   zGrid.render_cell_background8  s     
>!F#'#3#3Cdi#H#H eS&3K$%vO1  	 	
 	
 	
 	
 	
r>   c                    |                      |||j                  }|                    ||| j        j                   dS )z;Render the cell content for <row>,<col> into layout object.N)r$  rM   r   r   r-   )r7   r   rI   rJ   rT   coordss         r<   r   zGrid.render_cell_contentI  s>    
 !!#sDI66FFDJ$<=====r>   vmr   c                P   | j                             d          }|j        }|j        }|                     ||           |                     | j         j                   |                     | j                             |                     | 	                    || j                    dS )z)Render all grid lines into layout object.r'   N)
r   r2   r   r   r  _set_framesr5   _set_bordersr   _render_borders)r7   r   r,  r;   r   r  s         r<   r   zGrid.render_linesQ  s     
11)<< $ %8X...*+++$*77;;<<<VTZ00000r>   visible_cellsIterable[tuple[int, int, Cell]]c                    |D ]b\  }}}||j         d         z   }||j         d         z   }|                     |||||j                   |                     ||||| j                   cdS )z!Set borders of the visible cells.r   rH   N)rM   _set_rect_bordersrR   _set_inner_bordersr   )r7   r1  rI   rJ   rT   
bottom_row	right_cols          r<   r/  zGrid._set_borders\  s    + 	 	NCdty|+Jdil*I""3
CDJOOO##Zi   		 	r>   top_rowr6  left_colr7  c                .   ||z
  dk    r@t          ||          D ]/}t          |dz   |          D ]}|                     |||           0||z
  dk    r@t          ||          D ]1}t          |dz   |          D ]}|                     |||           0dS dS )zdSet `border_style` to the inner borders of the rectangle (top_row,
        bottom_row, ...)
        rH   N)r   r
  r  )r7   r8  r6  r9  r7  r   rJ   rI   s           r<   r5  zGrid._set_inner_bordersf  s     !##Xy11 > > 1j99 > >C%%c3====>x!##Wj11 > > Ay99 > >C%%c3====> $#> >r>   rR   r0   c                2   t          ||          D ]:}|                     |||j                   |                     |||j                   ;t          ||          D ]:}|                     |||j                   |                     |||j                   ;dS )a  Set border `style` to the rectangle (top_row, bottom_row, ...)

        The values describing the grid lines between the cells, see doc-strings
        for methods set_x_border() and set_y_border() and see comments for
        self._x_borders and self._y_borders.
        N)r   r
  r   r   r  r   r   )r7   r8  r6  r9  r7  rR   rJ   rI   s           r<   r4  zGrid._set_rect_bordersz  s     9-- 	= 	=CgsEI666j#u|<<<<*-- 	; 	;Cc8UZ888c9ek::::	; 	;r>   r5   Iterable[Frame]c                    |D ]Z}|j         d         }|j         d         }||j        d         z   }||j        d         z   }|                     |||||j                   [dS )z#Set borders for all defined frames.r   rH   N)rn   rM   r4  rR   )r7   r5   rp   r8  r9  r6  r7  s          r<   r.  zGrid._set_frames  s{     	 	EilGy|H 5:a=0J 5:a=0I""Xy%+   	 	r>   c                p     d
fd fd} fd}j          |              |             d	S )z:Render the grid lines as LINE entities into layout object.startr   endrR   r}   c           	     r    |j         r.                    | ||j        |j        |j        d           dS dS )z*Render the LINE entity into layout object.)r&  ry   r{   r|   )r?  r@  r(  N)r   add_linery   r{   r|   )r?  r@  rR   r&  r   s      r<   render_linez)Grid._render_borders.<locals>.render_line  s_    | 
!&!&$)N&+&6	      	 	 	 	 	
 
r>   c            	     <   t          j        dz             D ]} j        |          }t          j                  D ]^}j        |         }j        |dz            }                    | |          } t          ||          t          ||          |           _dS )zDraw the horizontal grid lines.rH   N)r   r#   r   r%   r   r  r   )	rI   r   rJ   r   r   rR   rC  r7   r   s	         r<   render_x_bordersz.Grid._render_borders.<locals>.render_x_borders  s    U[1_-- F FL% -- F FC<,D Lq1E --c377EKT1tE1~~uEEEE	FF Fr>   c            	     <   t          j        dz             D ]} j        |          }t          j                  D ]^}j        |         }j        |dz            }                    ||           } t          ||          t          ||          |           _dS )zDraw the vertical grid lines.rH   N)r   r%   r   r#   r   r  r   )	rJ   r  rI   r   r   rR   rC  r7   r   s	         r<   render_y_bordersz.Grid._render_borders.<locals>.render_y_borders  s    U[1_-- F FL% -- F FC,s+C!\#'2F --c377EKQd1foouEEEE	FF Fr>   N)r?  r   r@  r   rR   r}   )r/   )r7   r   r   rE  rG  r&  rC  s   ```  @@r<   r0  zGrid._render_borders  s    	 	 	 	 	 	 		F 	F 	F 	F 	F 	F 	F	F 	F 	F 	F 	F 	F 	F %r>   Nr   )r   r}   r  r}   )rI   r$   rJ   r$   rO   r$   )rI   r$   rJ   r$   r   r}   )r  r  rI   r$   rJ   r$   r   r}   )rI   r$   rJ   r$   rO   r}   )r  r  rI   r$   rJ   r$   rO   r}   )rO   r  )rI   r$   rJ   r$   rM   rN   rO   r"  )r   r   rI   r$   rJ   r$   rT   r   )r   r   r,  r   )r1  r2  )
r8  r$   r6  r$   r9  r$   r7  r$   r   r}   )
r8  r$   r6  r$   r9  r$   r7  r$   rR   r0   )r5   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/  r5  r4  r.  r0  r   r>   r<   r   r     s       BB
 
 
 
.	4 	4 	4 	42 2 2 2O O O OO O O O1 1 1 1; ; ; ;; ; ; ;5 5 5 5      ( ( ( (
 
 
 
"> > > >	1 	1 	1 	1   > > > >(; ; ; ;*	 	 	 	' ' ' ' ' 'r>   r   c                  :    e Zd ZdZ	 	 	 ddd
Zedd            ZdS )rl   a0  Represent a rectangle cell area enclosed by borderlines.

    Args:
         table: the assigned data table
         pos: tuple (row, col), border goes left and top of pos
         span: count of cells that Frame covers, border goes right and below of this cells
         style: style name as string
    r   r   rG   r'   r   r!   rn   rN   rM   c                >    || _         || _        || _        || _        d S r   )r   rn   rM   	stylename)r7   r   rn   rM   rR   s        r<   r=   zFrame.__init__  s$     
	r>   rO   r0   c                @    | j                             | j                  S r   r   r2   rK  r]   s    r<   rR   zFrame.style  s    z((888r>   N)rI  rG   r'   )r   r!   rn   rN   rM   rN   rO   r0   )r   r   r   r   r=   r   rR   r   r>   r<   rl   rl     sb           & &
 
 
 
 
 9 9 9 X9 9 9r>   rl   c                      e Zd ZdZ	 	 dddZedd
            Zej        dd            Zedd            ZddZ	ddZ
dS )r   a  Base class for table cells.

    Args:
        table: assigned data table
        style: style name as string
        span: tuple(spanrows, spancols), count of cells that cell covers

    A cell doesn't know its own position in the data table, because a cell can
    be used multiple times in the same or in different tables, therefore the
    cell itself can not determine if the cell-range reaches beyond the table
    borders.
    r'   rG   r   r!   rM   rN   c                0    || _         || _        || _        d S r   )r   rK  rM   )r7   r   rR   rM   s       r<   r=   zCell.__init__  s     
			r>   rO   c                    | j         S )zGet/set table span parameters.)_spanr]   s    r<   rM   z	Cell.span  s     zr>   r@   c                h    t          d|d                   t          d|d                   f| _        dS )z4Ensures that span values are >= 1 in each direction.rH   r   N)maxrR  )r7   r@   s     r<   rM   z	Cell.span  s0     !U1X&&AuQx(8(89


r>   r0   c                @    | j                             | j                  S )z*Returns the associated :class:`CellStyle`.rM  r]   s    r<   rR   z
Cell.style  s     z((888r>   r   r   r+  Sequence[float]r&  rL   c                    dS )z1Renders the cell content into the given `layout`.Nr   )r7   r   r+  r&  s       r<   r   zCell.render  s	     	r>   c                    | j         j        }| j         j        }|d         |z   |d         |z
  |d         |z
  |d         |z   fS )zCReduces the cell-coords about the margin_x and the margin_y values.r   rH         )rR   r   r   )r7   r+  r   r   s       r<   get_workspace_coordszCell.get_workspace_coords  sP    :&:&1I 1I 1I 1I 	
 	
r>   Nr'   rG   )r   r!   rM   rN   r   )r@   rN   rN  r   r   r+  rV  r&  rL   )r+  rV  rO   rV  )r   r   r   r   r=   r   rM   setterrR   r   r[  r   r>   r<   r   r     s            &		 	 	 	 	    X 
[: : : [: 9 9 9 X9   	
 	
 	
 	
 	
 	
r>   c                  2     e Zd ZdZ	 	 dd fd
ZddZ xZS )rP   a  Implements a cell type containing a multi-line text. Uses the
    :class:`~ezdxf.addons.MTextSurrogate` add-on to render the multi-line
    text, therefore the content of these cells is compatible to DXF R12.

    Args:
        table: assigned data table
        text: multi line text, lines separated by the new line character ``"\n"``
        style: cell style name as string
        span: tuple(rows, cols) area of cells to cover

    r'   rG   r   r!   rK   rL   rM   rN   c                j    t          t          |                               |||           || _        d S r   )superrP   r=   rK   )r7   r   rK   rR   rM   	__class__s        r<   r=   zTextCell.__init__)  s2     	h&&ueT:::			r>   r   r   r+  rV  r&  c                   t          | j                  sdS |                     |          \  }}}}| j        }|                                \  }	}
| j        j        }| j        }|j        r:d}d                    d | j                            dd          D                       }|t          ||z             dz  |f|	         }|t          ||z             dz  |f|
dz
           }t          |||f| j        j        | j        j        | j        j        || j        j        |j        | j        j        |
  
        }|                    |           dS )	zText cell.

        Args:
            layout: target layout
            coords: tuple of border-coordinates: left, right, top, bottom
            layer: target layer name as string

        Nr   
c              3     K   | ]}|V  d S r   r   )r   chars     r<   r   z"TextCell.render.<locals>.<genexpr>H  s"      LLtdLLLLLLr>           @rH   )r   rR   r   r   width_factorr   ry   r&  )lenrK   r[  rR   r   r   r   joinreplacerA   r   r   r   r   r   r   r   r   )r7   r   r+  r&  r   r   r   r   rR   h_alignv_alignrotatedrK   xposyposmtexts                   r<   r   zTextCell.render3  sX    49~~ 	F#'#<#<V#D#D eS&
 5577*%y= 	NG99LLty/@/@s/K/KLLLMMDeD5L))C/7@fsl++c137!D4L0*'
.++*'
 
 
 	Vr>   r\  )r   r!   rK   rL   rM   rN   r]  r   r   r   r   r=   r   __classcell__rb  s   @r<   rP   rP     sh        
 
   &      $ $ $ $ $ $ $ $r>   rP   c                  4     e Zd ZdZ	 	 	 dd fdZddZ xZS )rW   a<  Implements a cell type containing a block reference.

    Args:
        table: table object
        blockdef: :class:`ezdxf.layouts.BlockLayout` instance
        attribs: BLOCK attributes as (tag, value) dictionary
        style: cell style name as string
        span: tuple(rows, cols) area of cells to cover

    r'   NrG   r   r!   rV   r   rM   rN   c                    |i }t          t          |                               |||           |j        | _        || _        d S r   )ra  rW   r=   rq   
block_namerY   )r7   r   rV   rR   rY   rM   rb  s         r<   r=   zBlockCell.__init__f  sE     ?Gi''ud;;;"-r>   r   r   r+  rV  r&  rL   c           
     f   |                      |          \  }}}}| j        }|                                \  }	}
|t          ||z             dz  |f|	         }|t          ||z             dz  |f|
dz
           }|                    | j        ||f| j        |j        |j        |j	        |d           dS )zCreate the cell content as INSERT-entity with trailing ATTRIB-Entities.

        Args:
            layout: target layout
            coords: tuple of border-coordinates : left, right, top, bottom
            layer: target layer name as string

        rh  rH   )xscaleyscaler   r&  )rq   r"   valuesr(  N)
r[  rR   r   rA   add_auto_blockrefrx  rY   r   r   r   )r7   r   r+  r&  r   r   r   r   rR   rm  rn  rp  rq  s                r<   r   zBlockCell.rendert  s     $(#<#<V#D#D eS&
 5577eD5L))C/7@fsl++c137!D  $<<--!N	 	 	! 
	
 
	
 
	
 
	
 
	
r>   )r'   NrG   )r   r!   rV   r   rM   rN   r]  rs  ru  s   @r<   rW   rW   Z  sk        	 	  &      
 
 
 
 
 
 
 
r>   rW   start_valuerA   fieldsr  ro   Callable[[float], None]signc                P    | } ||           |D ]}|||z  z  } ||           dS )zAdds step-by-step the fields-values, starting with <start_value>,
    and appends the resulting values to another object with the
    append-method.
    Nr   )r~  r  ro   r  positionelements         r<   r  r    sS     H
F8  GdN"x r>   )r   )r~  rA   r  r  ro   r  r  rA   ):
__future__r   typingr   r   r   r   r   r	   r
   r   copyr   ezdxf.lldxfr   ezdxf.enumsr   r   ezdxf.addonsr   
ezdxf.mathr   r   ezdxf.layoutsr   ezdxf.eztypesr   r*   r,   r.   DEFAULT_TEXT_STYLEr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r0   r}   r   rl   r   
CustomCellrP   rW   r  r   r>   r<   <module>r     sa   # " " " " "	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                    ( ' ' ' ' ' ! ! ! ! ! ! ! ! 0))))))//////* ' &             #    GCv]$ ]$ ]$ ]$ ]$ ]$ ]$ ]$@;
 ;
 ;
 ;
 ;
 ;
 ;
 ;
|W4 W4 W4 W4 W4 W4 W4 W4t! ! ! ! ! ! ! !<x x x x x x x xv9 9 9 9 9 9 9 967
 7
 7
 7
 7
 7
 7
 7
t 
; ; ; ; ;t ; ; ;|4
 4
 4
 4
 4
 4
 4
 4
v 	      r>   