
    Ng;                       d Z ddl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 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mZmZmZmZmZ ddlmZ  ej         e!          Z" edg d          Z# e#dddd          Z$ e#dddd          Z% e#dddd          Z& e#dddd          Z' e#dddd          Z( e#dddd          Z) e#dddd          Z* e#dddd          Z+ G d de          Z, G d de          Z- G d de-          Z. G d d          Z/e G d d                      Z0 G d d          Z1 G d d          Z2 G d  d!          Z3dS )"z*Module for generating PDF content streams.    )annotationsN)ABCabstractmethod)
namedtuple)contextmanager)	dataclass)Enum)BytesIO)Path)Image)ArrayContentStreamInstruction
DictionaryMatrixNameOperatorPdfunparse_content_stream)StringColor)redgreenbluealpha   g      ?c                      e Zd ZdZdZdZdS )TextDirectionzEnumeration for text direction.r      N)__name__
__module____qualname____doc__LTRRTL     J/var/www/html/ai-engine/env/lib/python3.11/site-packages/pikepdf/canvas.pyr   r   .   s        ))
C
CCCr&   r   c                  B    e Zd ZdZedd            Zedd            ZdS )FontzBase class for fonts.textstrfontsizefloatreturnc                    dS )FEstimate the width of a text string when rendered with the given font.Nr%   selfr*   r,   s      r'   
text_widthzFont.text_width8         r&   pdfr   r   c                    dS )a  Register the font.

        Create several data structures in the Pdf to describe the font. While it create
        the data, a reference should be set in at least one page's /Resources dictionary
        to retain the font in the output PDF and ensure it is usable on that page.

        The returned Dictionary should be created as an indirect object, using
        ``pdf.make_indirect()``.

        Returns a Dictionary suitable for insertion into a /Resources /Font dictionary.
        Nr%   r2   r5   s     r'   registerzFont.register<   r4   r&   Nr*   r+   r,   r-   r.   r-   r5   r   r.   r   )r   r    r!   r"   r   r3   r8   r%   r&   r'   r)   r)   5   s\        U U U ^U    ^  r&   r)   c                  "    e Zd ZdZddZddZdS )	HelveticazHelvetica font.r*   r+   r,   r-   r.   c                    t                      )r0   )NotImplementedErrorr1   s      r'   r3   zHelvetica.text_widthN   s    !###r&   r5   r   r   c                    |                     t          t          j        t          j        t          j                            S )zRegister the font.)BaseFontTypeSubtype)make_indirectr   r   r<   r)   Type1r7   s     r'   r8   zHelvetica.registerR   s;      Y
  
 
 	
r&   Nr9   r:   )r   r    r!   r"   r3   r8   r%   r&   r'   r<   r<   K   sB        $ $ $ $
 
 
 
 
 
r&   r<   c                      e Zd ZdZd Zd<dZd=dZd Zd	 Zd>dZ	d Z
d Zd?dZd@dZd ZdAdZd>dZdBdZdCdZdDd!Zd" Zd# Zd$ ZdEd)ZdFd-ZdFd.Zd/ ZdGd4ZdHd7ZdId9ZdJd;Zd5S )KContentStreamBuilderzContent stream builder.c                    d| _         dS )Initialize.r&   N_streamr2   s    r'   __init__zContentStreamBuilder.__init__`   s    r&   instr   c                H    | xj         t          |g          dz   z  c_         d S )N   
)rJ   r   r2   rM   s     r'   _appendzContentStreamBuilder._appendd   s&    .v66>>r&   otherc                0    | xj         |j         z  c_         dS )zAppend another content stream.NrI   )r2   rR   s     r'   extendzContentStreamBuilder.extendg   s    %r&   c                j    t          g t          d                    }|                     |           | S )Save the graphics state.qr   r   rQ   rP   s     r'   pushzContentStreamBuilder.pushk   .    'HSMM::Tr&   c                j    t          g t          d                    }|                     |           | S )zRestore the graphics state.QrX   rP   s     r'   popzContentStreamBuilder.popq   rZ   r&   matrixr   c                t    t          |j        t          d                    }|                     |           | S )zConcatenate matrix.cmr   	shorthandr   rQ   r2   r^   rM   s      r'   r`   zContentStreamBuilder.cmw   1    '(8(4..IITr&   c                j    t          g t          d                    }|                     |           | S )zBegin text object.BTrX   rP   s     r'   
begin_textzContentStreamBuilder.begin_text}   .    'HTNN;;Tr&   c                j    t          g t          d                    }|                     |           | S )zEnd text object.ETrX   rP   s     r'   end_textzContentStreamBuilder.end_text   rh   r&   mctyper   mcidintc                    t          |t          |          gt          d                    }|                     |           | S )Begin marked content sequence.)MCIDBDC)r   r   r   rQ   )r2   rl   rm   rM   s       r'   begin_marked_content_proplistz2ContentStreamBuilder.begin_marked_content_proplist   sE    'ZT***+Xe__
 
 	Tr&   c                l    t          |gt          d                    }|                     |           | S )rp   BMCrX   )r2   rl   rM   s      r'   begin_marked_contentz)ContentStreamBuilder.begin_marked_content   s0    '(5//BBTr&   c                j    t          g t          d                    }|                     |           | S )zEnd marked content sequence.EMCrX   rP   s     r'   end_marked_contentz'ContentStreamBuilder.end_marked_content   s.    'HUOO<<Tr&   fontsizec                n    t          ||gt          d                    }|                     |           | S )zSet text font and size.TfrX   )r2   rz   r{   rM   s       r'   set_text_fontz"ContentStreamBuilder.set_text_font   s2    'thtnnEETr&   c                t    t          |j        t          d                    }|                     |           | S )Set text matrix.Tmra   rc   s      r'   set_text_matrixz$ContentStreamBuilder.set_text_matrix   rd   r&   modec                l    t          |gt          d                    }|                     |           | S )Set text rendering mode.TrrX   )r2   r   rM   s      r'   set_text_renderingz'ContentStreamBuilder.set_text_rendering   s0    '??Tr&   scaler-   c                l    t          |gt          d                    }|                     |           | S )Set text horizontal scaling.TzrX   )r2   r   rM   s      r'   set_text_horizontal_scalingz0ContentStreamBuilder.set_text_horizontal_scaling   s0    '$@@Tr&   encodedbytesc                    t          t          t          |          g          gt          d                    }|                     |           | S )z^Show text.

        The text must be encoded in character codes expected by the font.
        TJ)r   r   r   r   rQ   )r2   r   rM   s      r'   	show_textzContentStreamBuilder.show_text   sD     (w/@)A)A(BHTNNSSTr&   c                n    t          ||gt          d                    }|                     |           | S )Move cursor.TdrX   )r2   dxdyrM   s       r'   move_cursorz ContentStreamBuilder.move_cursor   s2    'R(4..AATr&   c                j    t          g t          d                    }|                     |           | S )Stroke and close path.srX   rP   s     r'   stroke_and_closez%ContentStreamBuilder.stroke_and_close   rZ   r&   c                j    t          g t          d                    }|                     |           | S )r   frX   rP   s     r'   fillzContentStreamBuilder.fill   rZ   r&   xywhc                r    t          ||||gt          d                    }|                     |           | S )zAppend rectangle to path.rerX   )r2   r   r   r   r   rM   s         r'   append_rectanglez%ContentStreamBuilder.append_rectangle   s6    'Aq!htnnEETr&   rgbc                p    t          |||gt          d                    }|                     |           | S )zSet RGB stroke color.RGrX   r2   r   r   r   rM   s        r'   set_stroke_colorz%ContentStreamBuilder.set_stroke_color   4    'Aq	8D>>BBTr&   c                p    t          |||gt          d                    }|                     |           | S )zSet RGB fill color.rgrX   r   s        r'   set_fill_colorz#ContentStreamBuilder.set_fill_color   r   r&   c                l    t          |gt          d                    }|                     |           | S )Set line width.r   rX   )r2   widthrM   s      r'   set_line_widthz#ContentStreamBuilder.set_line_width   s0    '#??Tr&   x1y1x2y2c                    t          ||gt          d                    t          ||gt          d                    g}|                     |d                    |                     |d                    | S )z
Draw line.mlr   r   rX   )r2   r   r   r   r   instss         r'   linezContentStreamBuilder.line   sl     %b"Xx}}==$b"Xx}}==
 	U1XU1Xr&   Nr   c                    |g }t          |t          t          f          r||f}d}t          ||gt	          d                    }|                     |           | S )Set dashes.Nr   d)
isinstancern   r-   r   r   rQ   )r2   arrayphaserM   s       r'   
set_dasheszContentStreamBuilder.set_dashes   s`    =Eec5\** 	ENEE'FFTr&   namec                l    t          |gt          d                    }|                     |           | S )zDraw XObject.

        Add instructions to render an XObject. The XObject must be
        defined in the document.

        Args:
            name: Name of XObject
        DorX   )r2   r   rM   s      r'   draw_xobjectz!ContentStreamBuilder.draw_xobject   s2     (??Tr&   r.   c                    | j         S )zBuild content stream.rI   rK   s    r'   buildzContentStreamBuilder.build  s
    |r&   )rM   r   )rR   rF   r^   r   )rl   r   rm   rn   )rl   r   )rz   r   r{   rn   )r   rn   )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   r-   Nr   )r   r   )r.   r   )r   r    r!   r"   rL   rQ   rT   rY   r]   r`   rg   rk   rs   rv   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r&   r'   rF   rF   ]   s       !!  ? ? ? ?& & & &                               	 	 	 	                    	 	 	 	        r&   rF   c                  (    e Zd ZU dZded<   ded<   dS )LoadedImagezLoaded image.r   r   zImage.ImageimageN)r   r    r!   r"   __annotations__r%   r&   r'   r   r     s-         JJJr&   r   c                      e Zd ZdZdd dZd!dZd!d	Zd
 Zd Zd"dZ	d#dZ
d$dZd Zd Zd Zeddd%d            Zd&dZdS )'_CanvasAccessorz;Contains all drawing methods class for drawing on a Canvas.NcsrF   c                8    || _         ||ng | _        d| _        d S r   )_cs_images_stack_depth)r2   r   imagess      r'   rL   z_CanvasAccessor.__init__  s'    !'!3vvr&   colorr   c                h    |j         |j        |j        }}}| j                            |||           | S )zSet stroke color.)r   r   r   r   r   r2   r   r   r   r   s        r'   stroke_colorz_CanvasAccessor.stroke_color!  s4    )U[%*a1!!!Q***r&   c                h    |j         |j        |j        }}}| j                            |||           | S )zSet fill color.)r   r   r   r   r   r   s        r'   
fill_colorz_CanvasAccessor.fill_color'  s4    )U[%*a11a(((r&   c                :    | j                             |           | S )r   )r   r   )r2   r   s     r'   
line_widthz_CanvasAccessor.line_width-  s    &&&r&   c                r    | j                             ||||           | j                                          | S )z"Draw line from (x1,y1) to (x2,y2).)r   r   r   )r2   r   r   r   r   s        r'   r   z_CanvasAccessor.line2  s5    b"b"%%%!!###r&   r   boolc                    | j                             ||||           |r| j                                          n| j                                          | S )zDDraw optionally filled rectangle at (x,y) with width w and height h.)r   r   r   r   )r2   r   r   r   r   r   s         r'   rectz_CanvasAccessor.rect8  sP    !!!Q1--- 	(HMMOOOOH%%'''r&   r   Path | str | Image.Imagec           
     @   |                      t          |dd|||                    5  t          |t          t          f          rt          j        |          }|                                 |j        dk    r|	                    d          }|j        dvrt          d|j                   t          j        d          }t          ||          }| j                            |           | j                            |           d	d	d	           n# 1 swxY w Y   | S )
z.Draw image at (x,y) with width w and height h.r   r`   PRGB)1Lr   zUnsupported image mode: Im)prefixN)
save_stater   r   r   r+   r   openloadr   convert
ValueErrorr   randomr   r   appendr   r   )r2   r   r   r   r   heightr   lis           r'   
draw_imagez_CanvasAccessor.draw_imageA  sH   __uaFAq A A_BB 	( 	(%$-- *
5))JJLLLzS  e,,z!222 !HEJ!H!HIII;d+++DT5))BL###H!!$'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( s   CDDDr*   Textc                v    | j                             |j                    | j                                          | S )zDraw text object.)r   rT   rk   )r2   r*   s     r'   	draw_textz_CanvasAccessor.draw_textQ  s1    !!!r&   c                $     | j         j        |  | S )r   )r   r   )r2   argss     r'   dashesz_CanvasAccessor.dashesW  s    T""r&   c                X    | j                                          | xj        dz  c_        | S )rV   r   )r   rY   r   rK   s    r'   rY   z_CanvasAccessor.push\  s*    Qr&   c                X    | j                                          | xj        dz  c_        | S )z$Restore the previous graphics state.r   )r   r]   r   rK   s    r'   r]   z_CanvasAccessor.popb  s*    Qr&   r   r`   Matrix | Nonec             #     K   |                                   ||                     |           | V  |                                  dS )zSave the graphics state and restore it on exit.

        Optionally, concatenate a transformation matrix. Implements
        the commonly used pattern of:
            q cm ... Q
        N)rY   r`   r]   )r2   r`   s     r'   r   z_CanvasAccessor.save_stateh  sC       			>GGBKKK







r&   r^   r   c                :    | j                             |           | S )z>Concatenate a new transformation matrix to the current matrix.)r   r`   r2   r^   s     r'   r`   z_CanvasAccessor.cmv  s    Fr&   N)r   rF   )r   r   )r   r   )r   r   )r*   r   )r`   r   r   )r   r    r!   r"   rL   r   r   r   r   r   r   r   r   rY   r]   r   r   r`   r%   r&   r'   r   r     s#       EE    
        
              
     04      ^     r&   r   c                  R    e Zd ZdZddZdd	Zedd            ZddZddZ	ddZ
dS )Canvasa6  Canvas for rendering PDFs with pikepdf.

    All drawing is done on a pikepdf canvas using the .do property.
    This interface manages the graphics state of the canvas.

    A Canvas can be exported as a single page Pdf using .to_pdf. This Pdf can
    then be merged into other PDFs or written to a file.
    	page_sizetuple[int | float, int | float]c                  || _         t          j                    | _        | j                            |          | _        t          t                      t                                | j        _        t                      | _	        g | _
        t          | j	        | j
                  | _        | j                                         dS )zInitialize a canvas.)r  )r)   XObjectN)r  r   new_pdfadd_blank_page_pager   	ResourcesrF   r   r   r   	_accessordorY   )r2   r  s     r'   rL   zCanvas.__init__  s    "GII	Y--	-BB
)z||Z\\RRR
'))*,(4<@@r&   resource_namer   rz   r)   c                ^    |                     | j                  | j        j        j        |<   dS )zAdd a font to the page.N)r8   r  r  r  r)   )r2   r  rz   s      r'   add_fontzCanvas.add_font  s(    37==3K3K
!-000r&   r.   r   c                    | j         S )z,Do operations on the current graphics state.)r  rK   s    r'   r  z	Canvas.do  s     ~r&   r   r   c           
     .   | j                             |j                                        |j        j        |j        j        |j        j        dv rt          j        nt          j	        t          j
        t          j        |j        j        dk    rdnd          S )N)r   r   r   r      )WidthHeight
ColorSpacerA   rB   BitsPerComponent)r  make_streamr   tobytesr   r   r   r   
DeviceGray	DeviceRGBr
  r   )r2   r   s     r'   _save_imagezCanvas._save_image  sx    y$$H(.8?#%8=J#>#>DNJ"$(-3"6"6QQA % 

 

 
	
r&   r   c                B   | j                                          | j        j        dk    rt                              d           | j                            | j        	                                          | j
        _        | j        D ].}|                     |          | j
        j        j        |j        <   /t#                      }| j                            |           |                    d           t)          j        |          }| j                                          |S )z'Render the canvas as a single page PDF.r   zNGraphics state stack is not empty when page saved - rendering may be incorrect)r  r]   r  r   logwarningr  r  r   r   r  Contentsr   r   r  r
  r   r
   saveseekr   r   rY   )r2   r   bioresults       r'   to_pdfzCanvas.to_pdf  s    >&!++KK-   #i33DHNN4D4DEE
, 	I 	IB484D4DR4H4HDJ (11ii	s# 	r&   Nc                R    |                                                      ||          S r  )r)  _repr_mimebundle_)r2   includeexcludes      r'   r+  zCanvas._repr_mimebundle_  s     {{}}..w@@@r&   )r  r  )r  r   rz   r)   )r.   r   )r   r   )r.   r   )NN)r   r    r!   r"   rL   r  propertyr  r   r)  r+  r%   r&   r'   r  r  |  s         	 	 	 	L L L L    X
 
 
 
   (A A A A A Ar&   r  c                  P    e Zd ZdZej        fdZddZd ZddZ	ddZ
d Zd ZdS )r   z3Text object for rendering text on a pikepdf canvas.c                l    t                      | _        | j                                         || _        dS )rH   N)rF   r   rg   
_direction)r2   	directions     r'   rL   zText.__init__  s.    '))#r&   rz   r   r{   r-   c                <    | j                             ||           | S )zSet font and size.)r   r~   )r2   rz   r{   s      r'   rz   z	Text.font  s    tT***r&   c                :    | j                             |           | S )r   )r   r   )r2   r   s     r'   render_modezText.render_mode  s    ##D)))r&   r^   r   c                :    | j                             |           | S )r   )r   r   r  s     r'   text_transformzText.text_transform  s      (((r&   r*   str | bytesc                t   t          |t                    rd|                    d          z   }n|}| j        t          j        k    r| j                            |           nW| j                            t          j
                   | j                            |           | j                                         | S )aU  Show text.

        The text must be encoded in character codes expected by the font.
        If a text string is passed, it will be encoded as UTF-16BE.
        Text rendering will not work properly if the font's character
        codes are not consistent with UTF-16BE. This is a rudimentary
        interface. You've been warned.
        s   zutf-16be)r   r+   encoder1  r   r#   r   r   rv   r   ReversedCharsry   )r2   r*   r   s      r'   showz	Text.show  s     dC   	!DKK
$;$;;GGG?m///Hw''''H))$*<===Hw'''H'')))r&   c                :    | j                             |           | S )r   )r   r   )r2   r   s     r'   horiz_scalezText.horiz_scale  s    ,,U333r&   c                <    | j                             ||           | S )r   )r   r   )r2   r   r   s      r'   r   zText.move_cursor  s    Q"""r&   N)rz   r   r{   r-   r   )r*   r8  )r   r    r!   r"   r   r#   rL   rz   r5  r7  r<  r>  r   r%   r&   r'   r   r     s        ==!.!2 $ $ $ $   
  
   
   *  
    r&   r   )4r"   
__future__r   loggingabcr   r   collectionsr   
contextlibr   dataclassesr   enumr	   ior
   pathlibr   PILr   pikepdfr   r   r   r   r   r   r   r   pikepdf.objectsr   	getLoggerr   r"  r   BLACKWHITEBLUECYANGREEN	DARKGREENMAGENTAREDr   r)   r<   rF   r   r   r  r   r%   r&   r'   <module>rU     s   1 0 " " " " " "  # # # # # # # # " " " " " " % % % % % % ! ! ! ! ! !                        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 # " " " " "g!! 	
7===>>aAqaAquQ1auQ1aaAqE!S!Q	
%1a

eAq!Q    D       3   ,
 
 
 
 
 
 
 
$q q q q q q q qh        ` ` ` ` ` ` ` `F@A @A @A @A @A @A @A @AF5 5 5 5 5 5 5 5 5 5r&   