
    Og=                        d dl mZ d dlmZmZ d dlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZ  G d	 d
      ZddZddZddZdddZdddZd Zd Zy)    )annotations)IteratorIterableN   )Vec2NULLVEC2)RGB)Plotter)Command	pe_decodec                  .   e Zd Z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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ed'd       Zd'dZd'dZed'd       Zd'dZd'dZed'd       Z d'dZ!d(dZ"d(d Z#d)d!Z$y")*Interpreteraa  The :class:`Interpreter` is the frontend for the :class:`Plotter` class.
    The :meth:`run` methods interprets the low level HPGL commands from the
    :func:`hpgl2_commands` parser and sends the commands to the virtual plotter
    device, which sends his output to a low level :class:`Backend` class.

    Most CAD application send a very restricted subset of commands to plotters,
    mostly just polylines and filled polygons. Implementing the whole HPGL/2 command set
    is not worth the effort - unless reality proofs otherwise.

    Not implemented commands:

        - the whole character group - text is send as filled polygons or polylines
        - configuration group: IN, DF, RO, IW - the plotter is initialized by creating a
          new plotter and page rotation is handled by the add-on itself
        - polygon group: EA, ER, EW, FA, RR, WG, the rectangle and wedge commands
        - line and fill attributes group: LA, RF, SM, SV, TR, UL, WU, linetypes and
          hatch patterns are decomposed into simple lines by CAD applications

    Args:
        plotter: virtual :class:`Plotter` device

    c                \    g | _         t               | _        t               | _        || _        y N)errorssetnot_implemented_commands_disabled_commandsplotter)selfr   s     c/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/addons/hpgl2/interpreter.py__init__zInterpreter.__init__$   s$    !#25%%,/E    c                :    | j                   j                  |       y r   )r   append)r   errors     r   	add_errorzInterpreter.add_error*   s    5!r   c                    |D ]n  \  }}|| j                   v rt        | d|j                          d      }|r	 ||       >|d   t        j                  v sT| j
                  j                  |       p y)zInterprets the low level HPGL commands from the :func:`hpgl2_commands` parser
        and sends the commands to the virtual plotter device.
        cmd_Nr   )r   getattrlowerstringascii_lettersr   add)r   commandsnameargsmethods        r   runzInterpreter.run-   ss     # 	8JD$t...TT$**,#8$?FtaF000--11$7	8r   c                :    | j                   j                  |       y)zDisable commands manually, like the scaling command ["SC", "IP", "IR"].
        This is a feature for experts, because disabling commands which changes the pen
        location may distort or destroy the plotter output.
        N)r   update)r   r%   s     r   disable_commandszInterpreter.disable_commands:   s    
 	&&x0r   c                    d}d}d}t        t        |            }t        |      }|dkD  rt        |d         }|dkD  r|d   }|dkD  r|d   }| j                  j                  |||       y)zSet fill type.r           r      N)tuple	to_floatslenintr   set_fill_type)r   r'   	fill_typespacinganglevalues	arg_counts          r   cmd_ftzInterpreter.cmd_ftB   sr    	y'K	q=F1IIq=QiGq=1IE""9gu=r   c                    t        t        |            }t        |      dk(  r/|\  }}}}| j                  j	                  |t        |||             y| j                  d       y)zSet pen color as RGB tuple.   z invalid arguments for PC commandN)listto_intsr2   r   set_pen_colorr	   r   )r   r'   r8   indexrgbs          r   cmd_pczInterpreter.cmd_pcQ   sR    gdm$v;!#NE1aLL&&uc!Ql;NN=>r   c                    t        |      }|rt        |d   d      }n| j                  d       yd}|dkD  rt        |d   |      }| j                  j                  ||       y)zSet pen width.r   gffffff?z invalid arguments for PW commandNr   )r2   to_floatr   to_intr   set_pen_width)r   r'   r9   widthr@   s        r   cmd_pwzInterpreter.cmd_pwZ   s^    I	T!Wd+ENN=>q=47E*E""5%0r   c                l    t        |      r)| j                  j                  t        |d   d             yy)zSelect pen.r   r   N)r2   r   set_current_penrH   r   r'   s     r   cmd_spzInterpreter.cmd_spg   s+    t9LL((Q);< r   c                l    t        |      r)| j                  j                  t        |d   d             yy)zSet number of pens.r   r/   N)r2   r   set_max_pen_countrH   rN   s     r   cmd_npzInterpreter.cmd_npl   s+    t9LL**6$q'1+=> r   c                   t        |      dk(  r| j                  j                          yt        t	        |            }t        |      dkD  r#| j                  j                  |d   |d          y| j                  d       y)z$Set input points p1 and p2 absolute.r   Nr    invalid arguments for IP command)r2   r   reset_scaling	to_pointsr1   set_scaling_pointsr   )r   r'   pointss      r   cmd_ipzInterpreter.cmd_ipq   sb    t9>LL&&(9T?+v;?LL++F1IvayANN=>r   c                4   t        |      dk(  r| j                  j                          yt        t	        |            }t        |      dk(  rCt        |d   dd      }t        |d   dd      }| j                  j                  |dz  |dz         yt        |      dk(  rkt        |d   dd      }t        |d   dd      }t        |d   dd      }t        |d   dd      }| j                  j                  |dz  |dz  |dz  |dz         y| j                  d	       y)
z6Set input points p1 and p2 in percentage of page size.r   Nr/   r.         Y@r   r<      rT   )	r2   r   rU   r=   r1   clampset_scaling_points_relative_1set_scaling_points_relative_2r   )r   r'   r8   xp1yp1xp2yp2s          r   cmd_irzInterpreter.cmd_ir}   s   t9>LL&&(io&v;!q	3.Cq	3.CLL66sU{C%KP[Aq	3.Cq	3.Cq	3.Cq	3.CLL66eS5[#+sU{ NN=>r   c                   t        |      dk(  r| j                  j                          y t        t	        |            }t        |      dk  r| j                  d       y d}t        |      dkD  rt        |d         }|dk(  rmd}t        |      dkD  rt        |d   dd      }d}t        |      d	kD  rt        |d	   dd      }| j                  j                  |d   |d   |d
   |d   ||       y |d
k(  r4| j                  j                  t        |d   |d
         |d   |d          y | j                  j                  |d   |d   |d
   |d          y )Nr   r<   z invalid arguments for SC commandr   g      I@   r.   r[      r/   r\   )r2   r   rU   r=   r1   r   r3   r]   set_isotropic_scalingset_point_factorr   set_anisotropic_scaling)r   r'   r8   scaling_typeleftbottoms         r   cmd_sczInterpreter.cmd_sc   sM   t9>LL&&(io&v;?NN=>v;?vay>L1D6{QVAYU3F6{Qvay#u5LL..q	q	q	q	 QLL))VAYq	*F1Ivay LL00q	6!9fQir   c                    d}t        |      r4t        |d   |      }| j                  j                  t	        |             y y Nr   )r2   rH   r   set_merge_controlboolr   r'   statuss      r   cmd_mczInterpreter.cmd_mc   s9    t9DGV,FLL**4<8 r   c                    d}d}t        |      }|rt        |d   |      }t        |dz        }|dkD  rt        |d   |      }| j                  j	                  ||       y )Ni  iI  r   g      ?r   )r2   rH   r3   r   
setup_page)r   r'   lengthheightcounts        r   cmd_pszInterpreter.cmd_ps   sa    D	DGV,F#&F19DGV,F/r   c                    | j                   j                          t        |      r.| j                   j                  t	        t        |                   yy)zLower pen down and plot lines.N)r   pen_downr2   plot_polylinerV   r1   rN   s     r   cmd_pdzInterpreter.cmd_pd   s9    t9LL&&y4'AB r   c                    | j                   j                          t        |      r.| j                   j                  t	        t        |                   yy)zLift pen up and move pen.N)r   pen_upr2   r~   rV   r1   rN   s     r   cmd_puzInterpreter.cmd_pu   s9    t9LL&&y4'AB r   c                    | j                   j                          t        |      r.| j                   j                  t	        t        |                   yy)z3Place pen absolute. Plots polylines if pen is down.N)r   set_absolute_moder2   r~   rV   r1   rN   s     r   cmd_pazInterpreter.cmd_pa   9    &&(t9LL&&y4'AB r   c                    | j                   j                          t        |      r.| j                   j                  t	        t        |                   yy)z2Place pen relative.Plots polylines if pen is down.N)r   set_relative_moder2   r~   rV   r1   rN   s     r   cmd_przInterpreter.cmd_pr   r   r   c                `   t        |      }|s| j                  d       y| j                  j                          | j                  j	                          t        |d   d      }d}|dkD  rt        |d   |      }| j                  j                  ||       | j                  j                          y)zPlot full circle.z invalid arguments for CI commandNr   g      ?      @r   )r2   r   r   push_pen_stater}   rG   plot_abs_circlepop_pen_state)r   r'   r9   radiuschord_angles        r   cmd_cizInterpreter.cmd_ci   s    I	NN=>##%$q'3'q="47K8K$$V[9""$r   c                    t        |      dk  r| j                  d       y| j                  || j                  j                         y)zPlot arc absolute.r\    invalid arguments for AR commandN)r2   r   _arc_outr   plot_abs_arcrN   s     r   cmd_aazInterpreter.cmd_aa   5    t9q=NN=>dDLL556r   c                    t        |      dk  r| j                  d       y| j                  || j                  j                         y)zPlot arc relative.r\   r   N)r2   r   r   r   plot_rel_arcrN   s     r   cmd_arzInterpreter.cmd_ar   r   r   c                    t        |       }|dk  ryt        | d         }t        | d         }t        | d         }d}|dkD  rt        | d   |      } |t        ||      ||       y)zPlot arcr\   Nr   r   r/   r   )r2   rG   r   )r'   output_methodr9   xysweep_angler   s          r   r   zInterpreter._arc_out   so     I	q=T!WT!WtAw'q="47K8Kd1aj+{;r   c                    t        |      dk  r| j                  d       y| j                  || j                  j                         y)z$Plot arc absolute from three points.r<   z invalid arguments for AT commandN)r2   r   _arc_3p_outr   plot_abs_arc_three_pointsrN   s     r   cmd_atzInterpreter.cmd_at  7    t9q=NN=>t||EEFr   c                    t        |      dk  r| j                  d       y| j                  || j                  j                         y)z$Plot arc relative from three points.r<   z invalid arguments for RT commandN)r2   r   r   r   plot_rel_arc_three_pointsrN   s     r   cmd_rtzInterpreter.cmd_rt  r   r   c                    t        |       }|dk  ryt        t        |             }t        |      dk  ryd}|dkD  rt        | d   |      }	  ||d   |d   |       y# t        $ r Y yw xY w)zPlot arc from three pointsr<   Nr/   r   r   r   )r2   rV   r1   rG   ZeroDivisionError)r'   r   r9   rX   r   s        r   r   zInterpreter._arc_3p_out  sz     I	q=9T?+v;?q="47K8K	&)VAY<  		s   A 	A)(A)c                P    | j                  || j                  j                         y)z8Plot cubic Bezier curves with absolute user coordinates.N)_bezier_outr   plot_abs_cubic_bezierrN   s     r   cmd_bzzInterpreter.cmd_bz,      t||AABr   c                P    | j                  || j                  j                         y)z8Plot cubic Bezier curves with relative user coordinates.N)r   r   plot_rel_cubic_bezierrN   s     r   cmd_brzInterpreter.cmd_br0  r   r   c                    d}t         }t         }t        t        |             D ]+  }|dk(  r|}n|dk(  r|}n|dk(  r|} ||||       |dz   dz  }- y )Nr   r   r/   r\   )r   rV   r1   )r'   r   kindctrl1ctrl2pointends          r   r   zInterpreter._bezier_out4  sf    y/ 	"EqyeUC01H>D	"r   c                   t        |      r|d   }n| j                  d       y| j                  }d}d}d}d}d}t        |      }	g }
||	k  r||   }|dv r|dz  }|d	k(  rZt        |||
      \  }}|j	                  t        |d                t        |      dkD  r|
j                  t        |dd              n|dk(  rd}n|dk(  rKt        |||
      \  }}t        |d         }t        |      dkD  rZ|
j                  t        |dd              n<|dk(  rd}n4|dk(  r/d}n,t        ||||      \  }}|
j                  t        |             |
r|j                          |r|
d   |j                  z
  |
d<   |s4|
j                  d      }|j                  |       |
s|j                          |
r!|j                  |
       |
j                          d}d}||	k  ryy)zPlot Polyline Encoded.r   z invalid arguments for PE commandNTF@   s   :<>=7r   :   )basestart<   >   =   7       )	frac_bitsr   r   )r2   r   r   r   rM   r3   extendrV   r}   user_locationpopmove_to_relr   plot_rel_polylineclear)r   r'   datar   r}   absolutefrac_bin_bitsr   r@   rx   point_queuecharr8   targets                 r   cmd_pezInterpreter.cmd_peC  s   t97DNN=>,,T"$fn;Dx
2:$-dU$KMFE++Cq	N;6{Q#**9VABZ+@ARZ$HRZ$-dU$KMFE$'q	NM6{Q#**9VABZ+@ARZ#HRZD )ME! ""9V#45  "%0^g6K6K%KKN(__Q/F''/&(--k:%%' Q fnr   c                    d}t        |      rt        |d   |      }|dk(  r| j                  j                          y| j                  j	                  |       y)zEnter/Exit polygon mode.r   r/   N)r2   rH   r   exit_polygon_modeenter_polygon_moders   s      r   cmd_pmzInterpreter.cmd_pm  sG    t9DGV,FQ;LL**,LL++F3r   c                    d}t        |      rt        t        |d   |      d      }| j                  j	                  |       y)zPlot filled polygon.r   )r   r   N)r2   one_ofrH   r   fill_polygon)r   r'   fill_methods      r   cmd_fpzInterpreter.cmd_fp  s7    t9 Q!=vFK!!+.r   c                8    | j                   j                          y)zPlot edged polygon.N)r   edge_polygon)r   _s     r   cmd_epzInterpreter.cmd_ep  s    !!#r   N)r   r
   returnNone)r   strr   r   )r%   zlist[Command]r   r   )r%   zIterable[str]r   r   )r'   list[bytes])r'   r   r   r   )r   r   )%__name__
__module____qualname____doc__r   r   r)   r,   r:   rD   rK   rO   rR   rY   rd   rn   ru   r{   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r    r   r   r   r      s    ,"81>?1=
?

??,!F9	0CCCC% 77 < <GG   CC " ";!|4/$r   r   c              #  T   K   | D ]  }	 t        |        y # t        $ r Y w xY wwr   float
ValueErrorr'   args     r   r1   r1     s7      	*  		   ((	%(%(c              #  T   K   | D ]  }	 t        |        y # t        $ r Y w xY wwr   r3   r   r   s     r   r>   r>     s6      	c(N  		r   c                h    g }d}d}| D ]&  }|r|j                  t        ||             d}#|}d}( |S )NFr.   T)r   r   )r8   rX   append_pointbuffervalues        r   rV   rV     sL    FLF  MM$vu-. LFL  Mr   c                <    	 t        |       S # t        $ r |cY S w xY wr   r   sdefaults     r   rG   rG     s$    Qx    
 c                <    	 t        |       S # t        $ r |cY S w xY wr   r   r   s     r   rH   rH     s$    1v r   c                .    t        t        ||       |      S r   )maxmin)vv_minv_maxs      r   r]   r]     s    s5!}e$$r   c                    | |v r| S |d   S rp   r   )r   choices     r   r   r     s    !9r   )r'   Iterable[bytes]r   zIterator[float])r'   r  r   zIterator[int])r8   zIterable[float]r   z
list[Vec2])r.   )r   bytesr   r   )r   )r   r  r   r3   )
__future__r   typingr   r   r"   depsr   r   
propertiesr	   r   r
   	tokenizerr   r   r   r1   r>   rV   rG   rH   r]   r   r   r   r   <module>r	     sL    # %      )G$ G$T%r   