
    ^j	                    d   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	 d dl
mZmZ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mZ g dZ ed       G d d             Z ed       G d d             ZddZ	 d	 	 	 	 	 ddZ G d de      Z G d dej>                        Z  G d de      Z!y)    )annotations)SequenceOptional)	dataclass)Matrix44Vec2)TextMTextget_font_name)fonts)text_layout)MTextContext)AbstractMTextRenderer)estimate_mtext_extentsvalid_text_height)	text_size
mtext_sizeTextSize	MTextSizeWordSizeDetectorr   T)frozenc                  ,    e Zd ZU ded<   ded<   ded<   y)r   floatwidth
cap_heighttotal_heightN)__name__
__module____qualname____annotations__     N/opt/ringagent/.cad-venv/lib/python3.12/site-packages/ezdxf/tools/text_size.pyr   r      s    Lr"   r   c                  R    e Zd ZU ded<   ded<   ded<   ded<   ded<   ed
d       Zy	)r   r   total_widthr   column_widthgutter_widthzSequence[float]column_heightsc                ,    t        | j                        S N)lenr(   selfs    r#   column_countzMTextSize.column_count.   s    4&&''r"   N)returnint)r   r   r   r    propertyr.   r!   r"   r#   r   r   #   s4    ##
 ( (r"   r   c                   | j                   j                  d      }d}t        | j                   j                  d            }t        j                  ||      }| j
                  "t        |       }t        j                  |||      }|j                  j                  }| j                         }|r|j                  |      }t        |||      S )a  Returns the measured text width, the font cap-height and the font
    total-height for a :class:`~ezdxf.entities.Text` entity.
    This function uses the optional `Matplotlib` package if available to measure
    the final rendering width and font-height for the :class:`Text` entity as
    close as possible. This function does not measure the real char height!
    Without access to the `Matplotlib` package the
    :class:`~ezdxf.tools.fonts.MonospaceFont` is used and the measurements are
    very inaccurate.

    See the :mod:`~ezdxf.addons.text2path` add-on for more tools to work
    with the text path objects created by the `Matplotlib` package.

    r           height)dxfget_defaultr   r   MonospaceFontdocr   	make_fontmeasurementsr   
plain_text
text_widthr   )textwidth_factorr<   r   font	font_namer   contents           r#   r   r   3   s     ((..w7LJ)$((*>*>x*HIJ$22:|LDxx!$'	y*lC$$11LooG__W-
J
L99r"   Nc           	     v   |xs
 t               }dg}d}d}| j                  r[t        |j                  |             }t	        |      r6|d   }|j
                  }|j                  }|D cg c]  }|j                   }}t	        |      }t        ||z  ||dz
  z  z   t        |      ||t        |            S c c}w )aZ  Returns the total-width, -height and columns information for a
    :class:`~ezdxf.entities.MText` entity.

    This function uses the optional `Matplotlib` package if available to do
    font measurements and the internal text layout engine to determine the final
    rendering size for the :class:`MText` entity as close as possible.
    Without access to the `Matplotlib` package the :class:`~ezdxf.tools.fonts.MonospaceFont`
    is used and the measurements are very inaccurate.

    Attention: The required full layout calculation is slow!

    The first call to this function with `Matplotlib` support is very slow,
    because `Matplotlib` lookup all available fonts on the system. To speedup
    the calculation and accepting inaccurate results you can disable the
    `Matplotlib` support manually::

        ezdxf.option.use_matplotlib = False

    r3   r      )r%   r   r&   r'   r(   )MTextSizeDetectorr=   listmeasurer+   r%   gutterr   r   maxtuple)	mtexttoolr(   r'   r&   columnsfirst_columncolumncounts	            r#   r   r   P   s    , &$&D#&%NLLzz#'U(;#<w<"1:L'33L'..L@GHff11HNHE 5(<519+EE(!!^,  Is   (B6c                  L     e Zd Z fdZd ZddZd	dZd
dZddZddZ	 xZ
S )rD   c                v    t         |           t        j                         | _        | j                  | _        y r*   )super__init__tlDoNothingRenderer
do_nothingrenderer)r-   	__class__s    r#   rS   zMTextSizeDetector.__init__~   s)    ..0r"   c                     y r*   r!   r,   s    r#   resetzMTextSizeDetector.reset   s    r"   c                    t        j                  | j                  |      j                  |      |j                  t        j
                  |j                        | j                        S )N)r   r4   valignrW   )rT   r	   get_fontr<   r   CellAlignmentalignrW   )r-   r=   ctxs      r#   wordzMTextSizeDetector.word   sJ    ww--$//5>>##CII.]]
 	
r"   c                    |\  }}}|rRt        j                  | j                  ||      | j                  ||      | j                  |      | j                        S | j                  ||      S )N)topbottomstackingrW   )rT   Fractionra   get_stackingrW   )r-   datar`   uprlwrtype_s         r#   fractionzMTextSizeDetector.fraction   se    S%;;IIc3'yyc***51	  99S#&&r"   c                ,    t        j                  |      S r*   )r   get_entity_font_facer-   rJ   s     r#   get_font_facezMTextSizeDetector.get_font_face   s    ))%00r"   c                    | j                   S r*   )rV   ro   s     r#   make_bg_rendererz"MTextSizeDetector.make_bg_renderer   s    r"   c                h    | j                          | j                  |      }|j                          |S r*   )rZ   layout_engineplace)r-   rJ   layouts      r#   rF   zMTextSizeDetector.measure   s)    

##E*r"   )r=   strr`   r   r/   tl.ContentCell)rh   rI   r`   r   r/   rx   )rJ   r
   r/   zfonts.FontFace)rJ   r
   r/   ztl.ContentRendererrJ   r
   r/   z	tl.Layout)r   r   r   rS   rZ   ra   rl   rp   rr   rF   __classcell__rX   s   @r#   rD   rD   }   s&    (


'1r"   rD   c                  :    e Zd ZdZddZ	 d	 	 	 	 	 	 	 	 	 	 	 ddZy)WordSizeCollectorzzCollects word sizes as tuples of the lower left corner and the upper
    right corner as Vec2 objects, ignores lines.
    c                    g | _         y r*   )
word_boxesr,   s    r#   rS   zWordSizeCollector.__init__   s	    35r"   Nc                f    | j                   j                  t        ||      t        ||      f       y r*   )r   appendr   )r-   leftrd   rightrc   ms         r#   renderzWordSizeCollector.render   s)     	T6 2D4DEFr"   )r/   Noner*   )r   r   rd   r   r   r   rc   r   r   zOptional[Matrix44]r/   r   )r   r   r   __doc__rS   r   r!   r"   r#   r}   r}      sW    6 !%GG G 	G
 G G 
Gr"   r}   c                  .     e Zd Zd Zd fdZddZ xZS )r   c                "    t               | _        y r*   )r}   rW   r,   s    r#   rZ   zWordSizeDetector.reset   s    )+r"   c                F    t         |   |      }|j                          |S r*   )rR   rF   r   )r-   rJ   rv   rX   s      r#   rF   zWordSizeDetector.measure   s    'r"   c                .    | j                   j                  S r*   )rW   r   r,   s    r#   r   zWordSizeDetector.word_boxes   s    }}'''r"   ry   )r/   zlist[tuple[Vec2, Vec2]])r   r   r   rZ   rF   r   rz   r{   s   @r#   r   r      s    ,
(r"   r   )r=   r	   r/   r   r*   )rJ   r
   rK   zOptional[MTextSizeDetector]r/   r   )"
__future__r   typingr   r   dataclassesr   
ezdxf.mathr   r   ezdxf.entitiesr	   r
   r   ezdxf.fontsr   ezdxf.toolsr   rT   ezdxf.tools.textr   $ezdxf.render.abstract_mtext_rendererr   r   r   __all__r   r   r   r   rD   rU   r}   r   r!   r"   r#   <module>r      s    # % ! % 5 5  ) ) F F	 $   $( ( (:< 7;**3**Z(- (VG,, G&
(( 
(r"   