
    Og2                       d dl mZ d dlZd dlmZ d dl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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mZ dZdZ G d de       Z! G d de!      Z" G d de!      Z# G d dejH                        Z%d ddejL                  e%jN                  d	 	 	 	 	 	 	 	 	 	 	 d%dZ(d dde%jN                  d	 	 	 	 	 	 	 	 	 d&dZ)d dde%jN                  d	 	 	 	 	 	 	 	 	 d'dZ*d dde%jN                  ddd	 	 	 	 	 	 	 	 	 	 	 	 	 d(d Z+d dde%jN                  d!dd	 	 	 	 	 	 	 	 	 	 	 d)d"Z,d*d#Z-	 	 	 	 	 	 d+d$Z.y),    )annotationsN)ElementTree)Drawing)zoomcolors)xplayer)svglayoutpymupdfdxf)	ColorMode   )hpgl2_commands)Plotter)Interpreter)Recorderplacement_matrixPlayerFs   %1Bc                      e Zd ZdZy)
Hpgl2Errorz+Base exception for the :mod:`hpgl2` add-on.N__name__
__module____qualname____doc__     [/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/addons/hpgl2/api.pyr   r      s    5r   r   c                      e Zd ZdZy)Hpgl2DataNotFoundzSNo HPGL/2 data was found, maybe the "Enter HPGL/2 mode" escape sequence is missing.Nr   r   r   r   r    r       s    ]r   r    c                      e Zd ZdZy)EmptyDrawingz/The HPGL/2 commands do not produce any content.Nr   r   r   r   r"   r"   $   s    9r   r"   c                      e Zd ZdZdZdZy)MergeControlr   r      N)r   r   r   NONE	LUMINANCEAUTOr   r   r   r$   r$   *   s    DIDr   r$   )rotationmirror_xmirror_y
color_modemerge_controlc               T   |dvrt        d      t        j                         }	 t        | |      }|j                         }t        ||rdnd|rdnd|      }	|j                  |	       |j                         }|j                         }
t        j                  |
|      }t        j                  ddd      }|t        j                  k(  rD|j                  j                  d       t        j                   |
||      }d|j                  _        t%        j&                  |||j)                         	       ~|j*                  rWt-        j.                  |
|j0                  |j2                         t        j4                  ||       t        j6                  ||       |S # t        $ r |cY S w xY w)
a  
    Exports the HPGL/2 commands of the byte stream `b` as a DXF document.

    The page content is created at the origin of the modelspace and 1 drawing unit is 1
    plot unit (1 plu = 0.025mm) unless scaling values are provided.

    The content of HPGL files is intended to be plotted on white paper, therefore a white
    filling will be added as background in color mode :attr:`RGB`.

    All entities are assigned to a layer according to the pen number with the name scheme
    ``PEN_<###>``. In order to be able to process the file better, it is also possible to
    assign the :term:`ACI` color by layer by setting the argument `color_mode` to
    :attr:`ColorMode.ACI`, but then the RGB color is lost because the RGB color has
    always the higher priority over the :term:`ACI`.

    The first paperspace layout "Layout1" of the DXF document is set up to print the entire
    modelspace on one sheet, the size of the page is the size of the original plot file in
    millimeters.

    HPGL/2's merge control works at the pixel level and cannot be replicated by DXF,
    but to prevent fillings from obscuring text, the filled polygons are
    sorted by luminance - this can be forced or disabled by the argument `merge_control`,
    see also :class:`MergeControl` enum.

    Args:
        b: plot file content as bytes
        rotation: rotation angle of 0, 90, 180 or 270 degrees
        mirror_x: mirror in x-axis direction
        mirror_y:  mirror in y-axis direction
        color_mode: the color mode controls how color values are assigned to DXF entities,
            see :class:`ColorMode`
        merge_control: how to order filled polygons, see :class:`MergeControl`

    Returns: DXF document as instance of class :class:`~ezdxf.document.Drawing`

    r   Z      i  )rotation angle must be 0, 90, 180, or 270r   )r,      
BACKGROUND)colorbg_color)
ValueErrorezdxfnewrecord_plotter_outputr   bboxr   	transform
modelspacer   
DXFBackendr   RGBr   layersaddadd_backgroundlayerr   hpgl2_to_drawingto_hexhas_datar   windowextminextmaxupdate_extentssetup_paperspace)br)   r*   r+   r,   r-   docplayerr=   mmspdxf_backendr8   bgs                 r   to_dxfrU   0   sW   Z ((DEE ))+C&q-8 ;;=DXr1Hb!XVA
Q;;=D
..
C..<Kzz#sC(HY]]"

|$T:#
 V[8??;LM}}Cdkk23%S$'J5  
s   F F'&F')r)   r*   r+   r-   c                  |dvrt        d      	 t        | |      }|j                         }|j                  }|j
                  dz  }|j                  dz  }	|dv rt        j                  ||	      }
nt        j                  |	|      }
|dz  }t        t        |j
                  |j                              }t        j                  |      }t        j                  ||rdnd	|rd	nd||
|j                  
      }|j                  |       t        j                   |
|      }t#        j$                  ||d       ~|j'                         }t)        j*                  |dd      S # t        $ r Y yw xY w)a<  
    Exports the HPGL/2 commands of the byte stream `b` as SVG string.

    The plot units are mapped 1:1 to ``viewBox`` units and the size of image is the size
    of the original plot file in millimeters.

    HPGL/2's merge control works at the pixel level and cannot be replicated by the
    backend, but to prevent fillings from obscuring text, the filled polygons are
    sorted by luminance - this can be forced or disabled by the argument `merge_control`,
    see also :class:`MergeControl` enum.

    Args:
        b: plot file content as bytes
        rotation: rotation angle of 0, 90, 180 or 270 degrees
        mirror_x: mirror in x-axis direction
        mirror_y:  mirror in y-axis direction
        merge_control: how to order filled polygons, see :class:`MergeControl`

    Returns: SVG content as ``str``

    r/   r2    (   r   r1   r1   output_coordinate_spacer3   r   sxsyr)   pager[   #ffffffr7   unicodeT)encodingxml_declaration)r9   r<   r   r=   sizexyr
   PageroundmaxSettingsr   r[   r>   r	   SVGRenderBackendr   rF   get_xml_root_elementETtostring)rN   r)   r*   r+   r-   rP   r=   rd   width_in_mmheight_in_mmr_   max_plot_unitssettingsrQ   svg_backendxmls                   r   to_svgru      sU   : ((DEE&q-8
 ;;=D99D &&2+K66B;L8{{;5{{<5C 3tvvtvv./N~FH2q1b ( @ @	A Q &&tX6KV[9E

*
*
,C;;sYEEI  s   E! !	E-,E-c               $    t        | ||||d      S )a  
    Exports the HPGL/2 commands of the byte stream `b` as PDF data.

    The plot units (1 plu = 0.025mm) are converted to PDF units (1/72 inch) so the image
    has the size of the original plot file.

    HPGL/2's merge control works at the pixel level and cannot be replicated by the
    backend, but to prevent fillings from obscuring text, the filled polygons are
    sorted by luminance - this can be forced or disabled by the argument `merge_control`,
    see also :class:`MergeControl` enum.

    Python module PyMuPDF is required: https://pypi.org/project/PyMuPDF/

    Args:
        b: plot file content as bytes
        rotation: rotation angle of 0, 90, 180 or 270 degrees
        mirror_x: mirror in x-axis direction
        mirror_y:  mirror in y-axis direction
        merge_control: how to order filled polygons, see :class:`MergeControl`

    Returns: PDF content as ``bytes``

    pdf)r)   r*   r+   r-   fmt)_pymupdf)rN   r)   r*   r+   r-   s        r   to_pdfrz      s#    > 	# r   png`   r)   r*   r+   r-   rx   dpic          	         |j                         }|t        j                  vrt        d| d      t	        | ||||||      S )a  
    Exports the HPGL/2 commands of the byte stream `b` as pixel image.

    Supported image formats:

        === =========================
        png Portable Network Graphics
        ppm Portable Pixmap
        pbm Portable Bitmap
        === =========================

    The plot units (1 plu = 0.025mm) are converted to dot per inch (dpi) so the image
    has the size of the original plot file.

    HPGL/2's merge control works at the pixel level and cannot be replicated by the
    backend, but to prevent fillings from obscuring text, the filled polygons are
    sorted by luminance - this can be forced or disabled by the argument `merge_control`,
    see also :class:`MergeControl` enum.

    Python module PyMuPDF is required: https://pypi.org/project/PyMuPDF/

    Args:
        b: plot file content as bytes
        rotation: rotation angle of 0, 90, 180 or 270 degrees
        mirror_x: mirror in x-axis direction
        mirror_y:  mirror in y-axis direction
        merge_control: how to order filled polygons, see :class:`MergeControl`
        fmt: image format
        dpi: output resolution in dots per inch

    Returns: image content as ``bytes``

    zimage format 'z' not supportedr}   )lowerr   SUPPORTED_IMAGE_FORMATSr9   ry   )rN   r)   r*   r+   r-   rx   r~   s          r   	to_pixmapr      sT    V ))+C
'111>#o>??	# r   rw   c                  t         j                  st        d       y|dvrt        d      	 t	        | |      }|j                         }|j                  }	|	j                  dz  }
|	j                  dz  }|dv rt        j                  |
|      }nt        j                  ||
      }|dz  }t        |	j                  |	j                        dz  d	z  }t        j                  |
      }t        j                  ||rdnd|rdnd|||j                        }|j!                  |       t        j"                         }t%        j&                  ||d       ~|dk(  r|j)                  ||      S |j+                  ||||      S # t
        $ r Y yw xY w)NzDPython module PyMuPDF is required: https://pypi.org/project/PyMuPDF/r   r/   r2   rX   rY   r1   i  H   rZ   r3   r   r\   r`   r7   rw   )rr   )rx   rr   r~   )r   is_pymupdf_installedprintr9   r<   r   r=   rd   re   rf   r
   rg   ri   rj   r   r[   r>   PyMuPdfBackendr   rF   get_pdf_bytesget_pixmap_bytes)rN   r)   r*   r+   r-   rx   r~   rP   r=   rd   ro   rp   r_   rq   rr   rQ   pymupdf_backends                    r   ry   ry   ,  s    ''TU((DEE&q-8
 ;;=D99D &&2+K66B;L8{{;5{{<5C (4/"4N~FH2q2q ( @ @	A Q ,,.OV_yI
e|,,TH,EE//cH# 0 
 	
M  s   E= =	F	F	c                    t        d       t        d| j                          | j                  r(t        d       | j                  D ]  }t        |        y y )NzHPGL/2 interpreter log:zunsupported commands: zparsing errors:)r   not_implemented_commandserrors)interpretererrs     r   print_interpreter_logr   j  sU    	
#$	";#G#G"H
IJ %% 	C#J	 r   c                   t        |       }t        |      dk(  rt        d       t        t	               }t        |      }t        |      }|j                  |       t        rt        |       |j                         }|j                         }|j                  st        |t        j                  k(  r|j                   rt        j"                  }|t        j"                  k(  r!t        rt        d       |j%                          |S )Nr   zHPGL2 data not found.z6merge control on: sorting filled polygons by luminance)r   lenr   r    r   r   r   runDEBUGr   rP   r=   rH   r"   r$   r(   has_merge_controlr'   sort_filled_paths)rN   r-   commandsrecorderplotterr   rP   r=   s           r   r<   r<   s  s     a H
8}%&zHhGg&KOOHk*__F;;=D==)))$$(22M...JK  "Mr   )rN   bytesr)   intr*   boolr+   r   r-   r$   returnr   )
rN   r   r)   r   r*   r   r+   r   r   str)
rN   r   r)   r   r*   r   r+   r   r   r   )rN   r   r)   r   r*   r   r+   r   rx   r   r~   r   r   r   )rN   r   r)   r   r*   r   r+   r   rx   r   r   r   )r   r   r   None)rN   r   r-   r$   r   r   )/
__future__r   enum	xml.etreer   rm   r:   ezdxf.documentr   r   r   ezdxf.addonsr   ezdxf.addons.drawingr	   r
   r   r   ezdxf.addons.drawing.dxfr   	tokenizerr   r   r   r   r   backendr   r   r   r   ENTER_HPGL2_MODE	Exceptionr   r    r"   IntEnumr$   rA   r(   rU   ru   rz   r   ry   r   r<   r   r   r   <module>r      sY   #  '  "    : : . %  $ 7 7 	 	 		
 		: 	4<<  }}"."3"3NN N 	N
 N  N Nh ##FFFF FF 	FF
 FF 	FFX ##&& & 	&
 & &X ##66 6 	6
 6 
6 
6 6x ##
;
;
 ;
 	;

 ;
 
;
 ;
| r   