
    h=                        S SK Jr  S SKJrJr  S SKrSSKJrJr  SSK	J
r
  SSKJr  SSKJrJr   " S	 S
5      rSS jrSS jrSS jrSSS jjrSSS jjrS rS rg)    )annotations)IteratorIterableN   )Vec2NULLVEC2)RGB)Plotter)Command	pe_decodec                  v   \ rS rSrSrS%S jrS&S jrS'S jrS(S jrS)S jr	S)S jr
S)S	 jrS)S
 jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jrS)S jr\S)S j5       rS)S jrS)S jr\S)S j5       rS)S jrS)S jr \S)S j5       r!S)S jr"S*S  jr#S*S! jr$S+S" jr%S#r&g$),Interpreter   a!  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                Z    / U l         [        5       U l        [        5       U l        Xl        g N)errorssetnot_implemented_commands_disabled_commandsplotter)selfr   s     P/var/www/html/env/lib/python3.13/site-packages/ezdxf/addons/hpgl2/interpreter.py__init__Interpreter.__init__$   s"    !#25%%,/E    c                :    U R                   R                  U5        g r   )r   append)r   errors     r   	add_errorInterpreter.add_error*   s    5!r   c                   U Hy  u  p#X R                   ;   a  M  [        U SUR                  5        3S5      nU(       a
  U" U5        ME  US   [        R                  ;   d  M^  U R
                  R                  U5        M{     g)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   runInterpreter.run-   sj     #JD...TT$**,#8$?FtaF000--11$7 #r   c                :    U R                   R                  U5        g)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_commandsInterpreter.disable_commands:   s    
 	&&x0r   c                    SnSnSn[        [        U5      5      n[        U5      nUS:  a  [        US   5      nUS:  a  US   nUS:  a  US   nU R                  R                  X#U5        g)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_ftInterpreter.cmd_ftB   sp    	y'K	q=F1IIq=QiGq=1IE""9u=r   c                    [        [        U5      5      n[        U5      S:X  a,  Uu  p4pVU R                  R	                  U[        XEU5      5        gU R                  S5        g)zSet pen color as RGB tuple.   z invalid arguments for PC commandN)listto_intsr7   r   set_pen_colorr	   r   )r   r*   r=   indexrgbs          r   cmd_pcInterpreter.cmd_pcQ   sL    gdm$v;!#NEaLL&&uc!l;NN=>r   c                    [        U5      nU(       a  [        US   S5      nOU R                  S5        gSnUS:  a  [        US   U5      nU R                  R                  XC5        g)zSet pen width.r   gffffff?z invalid arguments for PW commandNr   )r7   to_floatr   to_intr   set_pen_width)r   r*   r>   widthrF   s        r   cmd_pwInterpreter.cmd_pwZ   s\    I	T!Wd+ENN=>q=47E*E""50r   c                v    [        U5      (       a)  U R                  R                  [        US   S5      5        gg)zSelect pen.r   r   N)r7   r   set_current_penrO   r   r*   s     r   cmd_spInterpreter.cmd_spg   s-    t99LL((Q);< r   c                v    [        U5      (       a)  U R                  R                  [        US   S5      5        gg)zSet number of pens.r   r4   N)r7   r   set_max_pen_countrO   rV   s     r   cmd_npInterpreter.cmd_npl   s-    t99LL**6$q'1+=> r   c                   [        U5      S:X  a  U R                  R                  5         g[        [	        U5      5      n[        U5      S:  a#  U R                  R                  US   US   5        gU R                  S5        g)z$Set input points p1 and p2 absolute.r   Nr    invalid arguments for IP command)r7   r   reset_scaling	to_pointsr6   set_scaling_pointsr   )r   r*   pointss      r   cmd_ipInterpreter.cmd_ipq   sb    t9>LL&&(9T?+v;?LL++F1IvayANN=>r   c                :   [        U5      S:X  a  U R                  R                  5         g[        [	        U5      5      n[        U5      S:X  aC  [        US   SS5      n[        US   SS5      nU R                  R                  US-  US-  5        g[        U5      S:X  ak  [        US   SS5      n[        US   SS5      n[        US   SS5      n[        US   SS5      nU R                  R                  US-  US-  US-  US-  5        gU R                  S	5        g)
z6Set input points p1 and p2 in percentage of page size.r   Nr4   r3         Y@r   rB      r^   )	r7   r   r_   rC   r6   clampset_scaling_points_relative_1set_scaling_points_relative_2r   )r   r*   r=   xp1yp1xp2yp2s          r   cmd_ir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                   [        U5      S:X  a  U R                  R                  5         g [        [	        U5      5      n[        U5      S:  a  U R                  S5        g Sn[        U5      S:  a  [        US   5      nUS:X  ao  Sn[        U5      S:  a  [        US   SS5      nSn[        U5      S	:  a  [        US	   SS5      nU R                  R                  US   US   US
   US   UU5        g US
:X  a4  U R                  R                  [        US   US
   5      US   US   5        g U R                  R                  US   US   US
   US   5        g )Nr   rB   z invalid arguments for SC commandr   g      I@   r3   rf      r4   rg   )r7   r   r_   rC   r6   r   r8   rh   set_isotropic_scalingset_point_factorr   set_anisotropic_scaling)r   r*   r=   scaling_typeleftbottoms         r   cmd_sc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                    Sn[        U5      (       a4  [        US   U5      nU R                  R                  [	        U5      5        g g Nr   )r7   rO   r   set_merge_controlboolr   r*   statuss      r   cmd_mcInterpreter.cmd_mc   s;    t99DGV,FLL**4<8 r   c                    SnSn[        U5      nU(       a  [        US   U5      n[        US-  5      nUS:  a  [        US   U5      nU R                  R	                  X#5        g )Ni  iI  r   g      ?r   )r7   rO   r8   r   
setup_page)r   r*   lengthheightcounts        r   cmd_psInterpreter.cmd_ps   s_    D	DGV,F#&F19DGV,F/r   c                    U R                   R                  5         [        U5      (       a.  U R                   R                  [	        [        U5      5      5        gg)zLower pen down and plot lines.N)r   pen_downr7   plot_polyliner`   r6   rV   s     r   cmd_pdInterpreter.cmd_pd   s;    t99LL&&y4'AB r   c                    U R                   R                  5         [        U5      (       a.  U R                   R                  [	        [        U5      5      5        gg)zLift pen up and move pen.N)r   pen_upr7   r   r`   r6   rV   s     r   cmd_puInterpreter.cmd_pu   s;    t99LL&&y4'AB r   c                    U R                   R                  5         [        U5      (       a.  U R                   R                  [	        [        U5      5      5        gg)z3Place pen absolute. Plots polylines if pen is down.N)r   set_absolute_moder7   r   r`   r6   rV   s     r   cmd_paInterpreter.cmd_pa   ;    &&(t99LL&&y4'AB r   c                    U R                   R                  5         [        U5      (       a.  U R                   R                  [	        [        U5      5      5        gg)z2Place pen relative.Plots polylines if pen is down.N)r   set_relative_moder7   r   r`   r6   rV   s     r   cmd_prInterpreter.cmd_pr   r   r   c                j   [        U5      nU(       d  U R                  S5        gU R                  R                  5         U R                  R	                  5         [        US   S5      nSnUS:  a  [        US   U5      nU R                  R                  X45        U R                  R                  5         g)zPlot full circle.z invalid arguments for CI commandNr   g      ?      @r   )r7   r   r   push_pen_stater   rN   plot_abs_circlepop_pen_state)r   r*   r>   radiuschord_angles        r   cmd_ciInterpreter.cmd_ci   s    I	NN=>##%$q'3'q="47K8K$$V9""$r   c                    [        U5      S:  a  U R                  S5        gU R                  XR                  R                  5        g)zPlot arc absolute.rg    invalid arguments for AR commandN)r7   r   _arc_outr   plot_abs_arcrV   s     r   cmd_aaInterpreter.cmd_aa   3    t9q=NN=>dLL556r   c                    [        U5      S:  a  U R                  S5        gU R                  XR                  R                  5        g)zPlot arc relative.rg   r   N)r7   r   r   r   plot_rel_arcrV   s     r   cmd_arInterpreter.cmd_ar   r   r   c                    [        U 5      nUS:  a  g[        U S   5      n[        U S   5      n[        U S   5      nSnUS:  a  [        U S   U5      nU" [        X45      XV5        g)zPlot arcrg   Nr   r   r4   r   )r7   rN   r   )r*   output_methodr>   xysweep_angler   s          r   r   Interpreter._arc_out   sk     I	q=T!WT!WtAw'q="47K8Kd1j+;r   c                    [        U5      S:  a  U R                  S5        gU R                  XR                  R                  5        g)z$Plot arc absolute from three points.rB   z invalid arguments for AT commandN)r7   r   _arc_3p_outr   plot_abs_arc_three_pointsrV   s     r   cmd_atInterpreter.cmd_at  5    t9q=NN=>||EEFr   c                    [        U5      S:  a  U R                  S5        gU R                  XR                  R                  5        g)z$Plot arc relative from three points.rB   z invalid arguments for RT commandN)r7   r   r   r   plot_rel_arc_three_pointsrV   s     r   cmd_rtInterpreter.cmd_rt  r   r   c                    [        U 5      nUS:  a  g[        [        U 5      5      n[        U5      S:  a  gSnUS:  a  [        U S   U5      n U" US   US   U5        g! [         a     gf = f)zPlot arc from three pointsrB   Nr4   r   r   r   )r7   r`   r6   rN   ZeroDivisionError)r*   r   r>   rb   r   s        r   r   Interpreter._arc_3p_out  sz     I	q=9T?+v;?q="47K8K	&)VAY<  		s   A   
A-,A-c                N    U R                  XR                  R                  5        g)z8Plot cubic Bezier curves with absolute user coordinates.N)_bezier_outr   plot_abs_cubic_bezierrV   s     r   cmd_bzInterpreter.cmd_bz,      ||AABr   c                N    U R                  XR                  R                  5        g)z8Plot cubic Bezier curves with relative user coordinates.N)r   r   plot_rel_cubic_bezierrV   s     r   cmd_brInterpreter.cmd_br0  r   r   c                    Sn[         n[         n[        [        U 5      5       H.  nUS:X  a  UnOUS:X  a  UnOUS:X  a  UnU" X4U5        US-   S-  nM0     g )Nr   r   r4   rg   )r   r`   r6   )r*   r   kindctrl1ctrl2pointends          r   r   Interpreter._bezier_out4  s_    y/EqyeC01H>D 0r   c                   [        U5      (       a  US   nOU R                  S5        gU R                  nSnSnSnSnSn[        U5      n	/ n
X:  Ga  X(   nUS;   a  US-  nUS	:X  aV  [        X'US
9u  pUR	                  [        US   5      5        [        U5      S:  a  U
R                  [        USS 5      5        OUS:X  a  SnOUS:X  aG  [        X'US
9u  p[        US   5      n[        U5      S:  a  U
R                  [        USS 5      5        O8US:X  a  SnO/US:X  a  SnO&[        X&XxS9u  pU
R                  [        U5      5        U
(       a  UR                  5         U(       a  U
S   UR                  -
  U
S'   U(       d9  U
R                  S5      nUR                  U5        U
(       d  UR                  5         U
(       a!  UR                  U
5        U
R                  5         SnSnX:  a  GM  gg)zPlot Polyline Encoded.r   z invalid arguments for PE commandNTF@   s   :<>=7r   :   )basestart<   >   =   7       )	frac_bitsr   r   )r7   r   r   r   rU   r8   extendr`   r   user_locationpopmove_to_relr   plot_rel_polylineclear)r   r*   datar   r   absolutefrac_bin_bitsr   rF   r   point_queuecharr=   targets                 r   cmd_peInterpreter.cmd_peC  s   t997DNN=>,,T"$n;Dx
2:$-dU$KMF++Cq	N;6{Q#**9VABZ+@ARZ$HRZ$-dU$KMF$'q	NM6{Q#**9VABZ+@ARZ#HRZD )! ""9V#45  "%0^g6K6K%KKN(__Q/F''/&(--k:%%' Q nnr   c                    Sn[        U5      (       a  [        US   U5      nUS:X  a  U R                  R                  5         gU R                  R	                  U5        g)zEnter/Exit polygon mode.r   r4   N)r7   rO   r   exit_polygon_modeenter_polygon_moder   s      r   cmd_pmInterpreter.cmd_pm  sI    t99DGV,FQ;LL**,LL++F3r   c                    Sn[        U5      (       a  [        [        US   U5      S5      nU R                  R	                  U5        g)zPlot filled polygon.r   )r   r   N)r7   one_ofrO   r   fill_polygon)r   r*   fill_methods      r   cmd_fpInterpreter.cmd_fp  s9    t99 Q!=vFK!!+.r   c                8    U R                   R                  5         g)zPlot edged polygon.N)r   edge_polygon)r   _s     r   cmd_epInterpreter.cmd_ep  s    !!#r   )r   r   r   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____firstlineno____doc__r   r   r,   r0   r?   rJ   rR   rW   r[   rc   ro   rz   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   __static_attributes__ 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              #  \   #    U  H  n [        U5      v   M     g ! [         a     M#  f = f7fr   float
ValueErrorr*   args     r   r6   r6     s1     	*   		   ,,
),),c              #  \   #    U  H  n [        U5      v   M     g ! [         a     M#  f = f7fr   r8   r  r  s     r   rD   rD     s0     	c(N   		r  c                v    / nSnSnU  H,  nU(       a  UR                  [        X45      5        SnM(  UnSnM.     U$ )NFr3   T)r   r   )r=   rb   append_pointbuffervalues        r   r`   r`     sE    FLFMM$v-. LFL  Mr   c                >     [        U 5      $ ! [         a    Us $ f = fr   r  sdefaults     r   rN   rN     s$    Qx    
 c                >     [        U 5      $ ! [         a    Us $ f = fr   r  r  s     r   rO   rO     s$    1v r  c                ,    [        [        X 5      U5      $ r   )maxmin)vv_minv_maxs      r   rh   rh     s    s5}e$$r   c                    X;   a  U $ US   $ r}   r	  )r  choices     r   r   r     s    !9r   )r*   Iterable[bytes]r   zIterator[float])r*   r%  r   zIterator[int])r=   zIterable[float]r   z
list[Vec2])r3   )r  bytesr   r  )r   )r  r&  r   r8   )
__future__r   typingr   r   r%   depsr   r   
propertiesr	   r   r
   	tokenizerr   r   r   r6   rD   r`   rN   rO   rh   r   r	  r   r   <module>r,     sL    # %      )G$ G$T%r   