
    OAi؆                       S r SSK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  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJrJrJr  SSKJr  SSKJr  SSKJ r J!r!J"r"J#r#J$r$  \RJ                  " \&5      r'\" S/ SQ5      r(\(" SSSS5      r)\(" SSSS5      r*\(" SSSS5      r+\(" SSSS5      r,\(" SSSS5      r-\(" SSSS5      r.\(" SSSS5      r/\(" SSSS5      r0 " S S\5      r1 " S S\5      r2 " S S\25      r3 " S S\25      r4 " S S\35      r5S+S jr6S,S  jr7 " S! S"5      r8\ " S# S$5      5       r9 " S% S&5      r: " S' S(5      r; " S) S*5      r<g)-z*Module for generating PDF content streams.    )annotationsN)ABCabstractmethod)
namedtuple)contextmanager)	dataclassDecimal)Enum)BytesIO)Path)Image)ContentStreamInstructionMatrixPdf)CHARNAMES_TO_UNICODE)unparse_content_stream)Array
DictionaryNameOperatorStringColor)redgreenbluealpha   g      ?c                  "    \ rS rSrSrSr SrSrg)TextDirection(   zEnumeration for text direction.r       N)__name__
__module____qualname____firstlineno____doc__LTRRTL__static_attributes__r#       J/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pikepdf/canvas.pyr    r    (   s    )
C%
C6r,   r    c                  L    \ rS rSrSr\      SS j5       r\SS j5       rSrg)	Font1   zBase class for fonts.c                    g)FEstimate the width of a text string when rendered with the given font.Nr#   selftextfontsizes      r-   
text_widthFont.text_width4       r,   c                    g)a  Register the font.

Create several data structures in the Pdf to describe the font.

After registering the font, the returned object should be added to the
/Resources dictionary of any page or Form XObject that uses the font. For
example one might write:

```python
page.Resources.Font[Name.Arial] = font.register(pdf)
```

The same object can be used for multiple pages or Form XObjects, since it is
an indirect object.

Returns a Dictionary suitable for insertion into a /Resources /Font dictionary.
Nr#   r4   pdfs     r-   registerFont.register:   r9   r,   r#   Nr5   str | bytesr6   float | int | DecimalreturnrA   r<   r   rB   r   )	r$   r%   r&   r'   r(   r   r7   r=   r+   r#   r,   r-   r/   r/   1   sH    UU+@U	U U
  r,   r/   c                      \ rS rSrSr\\S	S j5       5       r\\S
S j5       5       r\\S
S j5       5       r	\SS j5       r
Srg)DimensionedFontO   zBase class for fonts that have dimensional information.

Specifically, these fonts can provide leading and ascent/descent values, and
encode strings to the encoding used by the font.

.. versionadded:: 9.8.1
c                    g)zHDefault leading (line spacing) value for this font; 0 if not applicable.Nr#   r4   s    r-   leadingDimensionedFont.leadingX   r9   r,   c                    g)z.The max height of the font above the baseline.Nr#   rH   s    r-   ascentDimensionedFont.ascent]   r9   r,   c                    g)z.The max height of the font below the baseline.Nr#   rH   s    r-   descentDimensionedFont.descentb   r9   r,   c                    g)z2Encode a string in the encoding used by this font.Nr#   r4   r5   s     r-   encodeDimensionedFont.encodeg   r9   r,   r#   N)rB   zDecimal | int)rB   zDecimal | int | Noner5   strrB   bytes)r$   r%   r&   r'   r(   propertyr   rI   rL   rO   rS   r+   r#   r,   r-   rE   rE   O   ss     W  W =  = =  = A Ar,   rE   c                  8    \ rS rSrSr      SS jrSS jrSrg)		Helvetical   a  Helvetica font.

Helvetica is one of the 14 PDF standard fonts that can typically be counted on being
present even if not embedded in the PDF document. However, starting with PDF 2.0,
PDF processors are no longer guaranteed to have these fonts. See 9.6.2.2.
c                    [        5       e)r2   )NotImplementedErrorr3   s      r-   r7   Helvetica.text_widtht   s     "##r,   c                    UR                  [        [        R                  [        R                  [        R
                  S95      $ )Register the font.)BaseFontTypeSubtype)make_indirectr   r   rZ   r/   Type1r;   s     r-   r=   Helvetica.registerz   s2      YY


 	
r,   r#   Nr?   rC   )r$   r%   r&   r'   r(   r7   r=   r+   r#   r,   r-   rZ   rZ   l   s)    $$+@$	$
r,   rZ   c                  $   \ rS rSr% SrS\S'   SrSS jr\SS j5       r	SS jr
\SS	 j5       r\SS
 j5       r\SS j5       rSS jr S     SS jjr S     SS jjrSS jrSS jr S      S!S jjr SSSS.         S"S jjjrSrg)#
SimpleFont   a  Font implementation designed to work with Type 1 Fonts and TrueType fonts.

As described in section 9.6 of the PDF spec.

See also section 9.8: Font Descriptors.

The PDF spec also considers Type3 fonts to be "Simple Fonts", but Type3 fonts are
not implemented here.
r   dataNc                    [         R                  U;  d=  UR                  [         R                  [         R                  [         R                  4;  a  [        SU5      eXl        g)z=Create a SimpleFont instance from a font resource dictionary.zDFont resource dictionary does not describe a Type1 or TrueType font:N)r   rc   re   MMType1TrueType
ValueErrorrj   )r4   rj   s     r-   __init__SimpleFont.__init__   sO    <<t#t||JJLLMM<
 (

 V  	r,   c           	        XR                   ;  a9  [        SU SSR                  UR                   R                  5       5       S35      eUR                   U   n[	        U[
        5      (       d  [        SU S[        U5       35      eU " U5      $ )z3Load a font from the specified resource dictionary.z!Cannot find font information for z (Available fonts: z, )zFont data for z is not a dictionary, but a )r/   LookupErrorjoinkeys
isinstancer   	TypeErrortype)clsnameresource_dict	font_datas       r-   loadSimpleFont.load   s     )))3D6 :%%)YY}/A/A/F/F/H%I$J!M  "&&t,	)Z00 &B4	?BST  9~r,   c                8    UR                  U R                  5      $ )r`   )rd   rj   r;   s     r-   r=   SimpleFont.register   s      ++r,   c                    [         R                  U R                  R                  ;   a   U R                  R                  R                  $ g)z!Returns leading for a SimpleFont.r   )r   Leadingrj   FontDescriptorrH   s    r-   rI   SimpleFont.leading   s3     <<49933399++333r,   c                B    U R                   R                  R                  $ )z Returns ascent for a SimpleFont.)rj   r   AscentrH   s    r-   rL   SimpleFont.ascent   s     yy''...r,   c                B    U R                   R                  R                  $ )z!Returns descent for a SimpleFont.)rj   r   DescentrH   s    r-   rO   SimpleFont.descent   s     yy''///r,   c                \   [        U[        5      (       a  U R                  [        5      n[        U[        5      (       a  US   nU[	        U R
                  R                  [        R                  S5      5      -
  n[        R                  U R
                  ;   a>  [        U R
                  R                  5      U:  a  U R
                  R                  U   nU$ [        R                  U R
                  R                  ;   a"  U R
                  R                  R                  nU$ [        S5      nU$ )zGet the (unscaled) width of the character, in glyph-space units.

Args:
    char: The character to check. May be a char code, or a string containing a
        single character.
r   )rv   rV   rS   rW   intrj   getr   	FirstCharWidthslenMissingWidthr   r
   )r4   char	char_codewidths       r-   unscaled_char_widthSimpleFont.unscaled_char_width   s     dC  ;;s#DdE""7D3tyy}}T^^Q?@@	;;$))#DII,<,<(=	(III$$Y/E
 	 $))":"::II,,99E  AJEr,   c                &    [        S5      nX-  U-  $ )ar  Convert width from glyph space to text space, scaling by font size.

Scaling based on the nominal height (see 9.2.2):

"This standard is arranged so that the nominal height of tightly spaced lines of
text is 1 unit. ... The standard-size font shall then be scaled to be usable."

This means, essentially, that a font size of 1 means a character is 1 text-space
unit high, and a font size of 12 is 12 text-space units high. Assuming no text
scaling is in place (such as via the text matrix), and the PDF has not set a
user-defined unit in the page dictionary, then text space units will be points
(defined as 1/72 of an inch).
  r	   r4   r   r6   glyph_space_ratios       r-   convert_widthSimpleFont.convert_width   s    $ $DM)X55r,   c                &    [        S5      nX-  U-  $ )zHConvert width from text space back to glyph space, scaling by font size.r   r	   r   s       r-   convert_width_reverse SimpleFont.convert_width_reverse   s     $DM)X55r,   c                p   [         R                  U R                  ;  a  [        S5      e[	        U R                  R                  [         5      (       a%  U R                  XR                  R                  5      $ [	        U R                  R                  [        5      (       a  [         R                  U R                  R                  ;   ab  U R                  UU R                  R                  R                  U R                  R                  R                  [         R                  5      5      $ [         R                  U R                  R                  ;  a  [        S5      eU R                  XR                  R                  R                  5      $ [        S[        U R                  R                  5       35      e)zEncode a string in the encoding used by this font.

This currently only works with fonts that use the WinAnsiEncoding or the
MacRomanEncoding. Differences maps are supported, though with a limited
set of recognized character names.
z1Cannot encode without explicitly defined encodingzUnsupported encoding type: )r   Encodingrj   r]   rv   _encode_namedr   Differences_encode_diffmapr   BaseEncodingrw   rx   rR   s     r-   rS   SimpleFont.encode   s@    ==		) &C  dii(($//%%dII,>,>??dii((*55499#5#55++II&&22II&&**4+<+<= 
   		(:(::)G  %%dII,>,>,K,KLL5d499;M;M6N5OPQQr,   c                t   U[         R                  :X  a  [        S5      eU[         R                  :X  a  UR	                  S5      $ U[         R
                  :X  a  UR	                  S5      $ U[         R                  :X  a  [        S5      eU[         R                  :X  a  UR	                  S5      $ [        SU5      e)Nz!Cannot encode to StandardEncodingcp1252	mac_romanz"Cannot encode to MacExpertEncodingpdfdoc_pikepdfzUnknown encoding:)	r   StandardEncodingr]   WinAnsiEncodingrS   MacRomanEncodingMacExpertEncodingPDFDocEncodingrn   )r4   r5   encodings      r-   r   SimpleFont._encode_named  s    t,,, &&IJJt+++;;x((t,,,;;{++t--- &&JKKt*** ;;/00,h77r,   c                   U R                   c  [        U5      U l         [        5       nU H  nXPR                   ;   a   UR                  U R                   U   5        M2  Ub"  UR	                  U R                  XS5      5        MW  UR                  5       (       a  UR                  [        U5      5        M  [        R                  S[        U5       S35        M     g )NzNo mapping for z in current encoding; skipped)_diffmap_cache_differences_map_lookup	bytearrayappendextendr   isasciiordlogwarningrepr)r4   r5   diffmapbase_encodingresultr   s         r-   r   SimpleFont._encode_diffmap+  s     &"9'"BDD***d11$78*d00EFc$i( od4j\9VWX r,   r   )char_spacingword_spacingc                   Sn[        S5      n[        U[        5      (       a  U R                  U5      nU H$  nXPR	                  U5      U-   -  nXv:X  d  M   XT-  nM&     U R                  XR5      $ )a  Get the width of the string.

This is the width of the string when rendered with the current font, scaled by
the given font size.

Args:
    text: The string to check
    fontsize: The target font size in text-space units. (Assuming text space
        isn't being scaled, this means the font size in points.)
    char_spacing: Additional space that will be added between each character.
        May be negative.
    word_spacing: Additional space that will be added after each ASCII space
        character (' '). May be negative.
r    )r   rv   rV   rS   r   r   )r4   r5   r6   r   r   r   ascii_spacebytes           r-   r7   SimpleFont.text_width<  sr    , #hdC  ;;t$DD
 --d3lBBE"%  !!%22r,   )r   rj   )rj   r   )rz   r   r{   r   rB   rh   rC   )rB   int | Decimal)rB   r
   )r   zint | bytes | strrB   r
   )r   )r   r   r6   r   rB   r   rU   )r5   rV   r   r   N)r5   rV   r   r   r   zName | None)
r5   r@   r6   r   r   r   r   r   rB   r   )r$   r%   r&   r'   r(   __annotations__r   ro   classmethodr}   r=   rX   rI   rL   rO   r   r   r   rS   r   r   r7   r+   r#   r,   r-   rh   rh      s8    N  ,   / /
 0 0
, ?@6"6.;6	6, ?@6"6.;6	6R:8( GKYY"'Y8CY( #$"3
 '(&'"3"3  "3
 $"3 $"3 
"3 "3r,   rh   c              #  h   #    SnU  H&  n[        U[        5      (       a  X4v   US-  nM$  UnM(     g7f)a0  Parses a Differences map to ``(char_code, char_name)`` pairs.

This procedure is as described in 9.6.5.1.

Here, ``char_code`` refers to the byte value of the character as it would appear in
a text content stream using this font; it is the PDF encoding, not the true unicode
character code. The corresponding ``char_name`` refers to the name of the glyph. The
name is used by Type1 and Type3 fonts to look up the actual glyph used from the
font.

A partial mapping of glyph names to true unicode characters is available at
pikepdf._data.CHARNAMES_TO_UNICODE`.
r   r   N)rv   r   )r   countervalues      r-   _parse_differences_mapr   a  s;      GeT"". qLG G s   02c                    0 n[        U 5       H  u  p# X![        [        U5         '   M     U$ ! [         a%    [        R                  S[        U5       35         MN  f = f)a_  Convert a Differences map (See 9.6.5.1) to a Python dict.

The Python dict maps unicode characters to the character index value.

The character index values are the byte values used in actual text content streams.

If the difference map encodes characters whose names aren't recognized, they will be
omitted from the final map, and a warning emitted.
z*Unknown character name in difference map: )r   r   rV   KeyErrorr   r   )r   diffindexrz   s       r-   r   r   }  sd     D-g6	R49%c$i01 7
 K  	RKKDSYKPQ	Rs   .+AAc                  L   \ rS rSrSrS rS'S jrS(S jrS rS r	S)S jr
S	 rS
 rS*S jrS+S j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S0S jrS1S jrS0S jr      S2S jrS rS rS rS rS3S jrS4S jr S4S jr!S  r"S5S! jr#S6S# jr$S7S$ jr%S8S% jr&S&r'g")9ContentStreamBuilderi  zContent stream builder.c                "    [        5       U l        gzInitialize.N)r   _streamrH   s    r-   ro   ContentStreamBuilder.__init__  s     {r,   c                H    U =R                   [        U/5      S-   -  sl         g )N   
)r   r   r4   insts     r-   _appendContentStreamBuilder._append  s    .v6>>r,   c                    [        U[        5      (       a   U =R                  UR                  -  sl        gU =R                  US-   -  sl        g)zAppend another content stream.r   N)rv   r   r   )r4   others     r-   r   ContentStreamBuilder.extend  s4    e122LLEMM)LLLEEM)Lr,   c                R    [        / [        S5      5      nU R                  U5        U $ )Save the graphics state.qr   r   r   r   s     r-   pushContentStreamBuilder.push  #    'HSM:Tr,   c                R    [        / [        S5      5      nU R                  U5        U $ )zRestore the graphics state.Qr   r   s     r-   popContentStreamBuilder.pop  r   r,   c                f    [        UR                  [        S5      5      nU R                  U5        U $ )zConcatenate matrix.cmr   	shorthandr   r   r4   matrixr   s      r-   r   ContentStreamBuilder.cm  s)    '(8(8(4.ITr,   c                R    [        / [        S5      5      nU R                  U5        U $ )zBegin text object.

All text operations must be contained within a text object, and are invalid
otherwise. The text matrix and font are reset for each text object. Text objects
may not be nested.
BTr   r   s     r-   
begin_textContentStreamBuilder.begin_text  s%     (HTN;Tr,   c                R    [        / [        S5      5      nU R                  U5        U $ )zEnd text object.ETr   r   s     r-   end_textContentStreamBuilder.end_text  s#    'HTN;Tr,   c                d    [        U[        US9/[        S5      5      nU R                  U5        U $ )Begin marked content sequence.)MCIDBDC)r   r   r   r   )r4   mctypemcidr   s       r-   begin_marked_content_proplist2ContentStreamBuilder.begin_marked_content_proplist  s2    'ZT*+Xe_
 	Tr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )r   BMCr   )r4   r  r   s      r-   begin_marked_content)ContentStreamBuilder.begin_marked_content  s%    '(5/BTr,   c                R    [        / [        S5      5      nU R                  U5        U $ )zEnd marked content sequence.EMCr   r   s     r-   end_marked_content'ContentStreamBuilder.end_marked_content  s#    'HUO<Tr,   c                T    [        X/[        S5      5      nU R                  U5        U $ )a  Set text font and size.

This operator is mandatory in order to show text. Any text object which attempts
to show text without first calling this operator is invalid.

The font name must match an entry in the current resources dictionary. The font
size is expressed in text-space units. Assuming no text scaling is in place, and
the PDF has not set a user-defined unit in the page dictionary, then text space
units will be points (defined as 1/72 of an inch).
Tfr   )r4   fontsizer   s       r-   set_text_font"ContentStreamBuilder.set_text_font  s'     (htnETr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )a2  Set the character spacing (Tc) for future text operations.

This is a value, measured in unscaled text-space units, which will be used to
adjust the spacing between characters. A value of 0 (the default) means that,
for each rendered glyph, the cursor will advance only the actual width of the
glyph. Positive values will result in additional space between characters, and
negative values will cause glyphs to overlap.

In vertical writing, the sign works opposite of what one might expect: a
positive value shrinks the space, and a negative value increases it.
Tcr   r4   r  r   s      r-   set_text_char_spacing*ContentStreamBuilder.set_text_char_spacing  s'     (?Tr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )a>  Set the word spacing (Tw) for future text operations.

This is a value, measured in unscaled text-space units, which will be added to
the width of any ASCII space characters.

In vertical writing, the sign works opposite of what one might expect: a
positive value shrinks the space, and a negative value increases it.
Twr   r  s      r-   set_text_word_spacing*ContentStreamBuilder.set_text_word_spacing  '     (?Tr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )zSet the leading value (TL) for future text operations.

This is the vertical spacing between lines. Specifically, it is defined as the
distance between the baseline of the previous line to the baseline of the next
line.
TLr   r  s      r-   set_text_leading%ContentStreamBuilder.set_text_leading  s'     (?Tr,   c                f    [        UR                  [        S5      5      nU R                  U5        U $ )zSet text matrix.

The text matrix defines the conversion between text-space and page-space, in
terms of both scaling and translation. If this matrix scales the text, then
it redefines text-space units as being some scale factor of page-space units.
Tmr   r   s      r-   set_text_matrix$ContentStreamBuilder.set_text_matrix  s+     ((8(8(4.ITr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )Set text rendering mode.Trr   )r4   moder   s      r-   set_text_rendering'ContentStreamBuilder.set_text_rendering  s%    '?Tr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )Set text horizontal scaling.Tzr   )r4   scaler   s      r-   set_text_horizontal_scaling0ContentStreamBuilder.set_text_horizontal_scaling   s%    '$@Tr,   c                z    [        [        [        U5      /5      /[        S5      5      nU R	                  U5        U $ )zNShow text.

The text must be encoded in character codes expected by the font.
TJ)r   r   r   r   r   r4   encodedr   s      r-   	show_textContentStreamBuilder.show_text&  s4     (w/@)A(BHTNSTr,   c                t    [        [        S U 5       5      /[        S5      5      nU R                  U5        U $ )a  Show text, with manual spacing (kerning) options.

Arguments are either bytes, which represent the actual text to show, or numbers,
which move the cursor. The units for the numbers are expressed in thousandths
of a text-space unit (thus typically equivalent to a glyph-space unit).

For horizontal writing, positive values move the cursor left, and negative
right. For vertical writing, positive values move down and negative up.

The text must be encoded in character codes expected by the font.
c              3  f   #    U  H'  n[        U[        5      (       a  [        U5      OUv   M)     g 7fr   )rv   rW   r   ).0parts     r-   	<genexpr>>ContentStreamBuilder.show_text_with_kerning.<locals>.<genexpr>C  s)      RW$JtU$;$;F4LERWs   /1r2  )r   r   r   r   )r4   partsr   s      r-   show_text_with_kerning+ContentStreamBuilder.show_text_with_kerning5  sD     ( RW 
 TN
 	Tr,   c                f    [        [        U5      /[        S5      5      nU R                  U5        U $ )zAdvance to the next line and show text.

The text must be encoded in character codes expected by the font.

This is functionally equivalent to ``move_cursor_new_line()`` followed by
``show_text_string(encoded)``, but in a single operation.
'r   r   r   r   r3  s      r-   show_text_line#ContentStreamBuilder.show_text_lineL  s,     ((98C=ITr,   c                h    [        X#[        U5      /[        S5      5      nU R                  U5        U $ )a;  Advance to the next line and show text.

The text must be encoded in character codes expected by the font.

This is functionally equivalent to ``set_text_char_spacing(char_spacing)`` and
``set_text_word_spacing()``, followed by ``move_cursor_new_line()`` and then
``show_text(encoded)``, all in a single operation.
"rB  )r4   r4  r   r   r   s        r-   show_text_line_with_spacing0ContentStreamBuilder.show_text_line_with_spacingX  s3     (98C=
 	Tr,   c                T    [        X/[        S5      5      nU R                  U5        U $ )a  Move cursor by the given offset, relative to the start of the current line.

This operator modifies the both current text matrix and the text line matrix.
This means that, in addition to moving the current cursor, the new cursor will
also be defined as the start of a new line.

The new position will be redefined as the new start of the line even if the y
offset is 0; what to a user may look like a single line of text could be encoded
in the PDF content stream as multiple "lines". It's not uncommon for PDFs to be
written with every word as a separate "line", allowing the PDF writer to
explicitly define the spacing between each word.
Tdr   )r4   dxdyr   s       r-   move_cursor ContentStreamBuilder.move_cursori  s'     ((4.ATr,   c                R    [        / [        S5      5      nU R                  U5        U $ )a  Move cursor to the start of the next line.

This moves down by the current leading value, and resets the x position back to
the value it had at the beginning of the current line.

This operator modifies the both current text matrix and the text line matrix.
This means that, in addition to moving the current cursor, the new cursor will
also be defined as the start of a new line.

The value this operation moves the cursor is set using ``set_text_leading``.
zT*r   r   s     r-   move_cursor_new_line)ContentStreamBuilder.move_cursor_new_linez  s%     (HTN;Tr,   c                R    [        / [        S5      5      nU R                  U5        U $ )Stroke and close path.sr   r   s     r-   stroke_and_close%ContentStreamBuilder.stroke_and_close  r   r,   c                R    [        / [        S5      5      nU R                  U5        U $ )rS  fr   r   s     r-   fillContentStreamBuilder.fill  r   r,   c                V    [        XX4/[        S5      5      nU R                  U5        U $ )zAppend rectangle to path.rer   )r4   xywhr   s         r-   append_rectangle%ContentStreamBuilder.append_rectangle  s'    'qhtnETr,   c                V    [        XU/[        S5      5      nU R                  U5        U $ )zSet RGB stroke color.RGr   r4   rgbr   s        r-   set_stroke_color%ContentStreamBuilder.set_stroke_color  '    'q	8D>BTr,   c                V    [        XU/[        S5      5      nU R                  U5        U $ )zSet RGB fill color.rgr   re  s        r-   set_fill_color#ContentStreamBuilder.set_fill_color  rk  r,   c                T    [        U/[        S5      5      nU R                  U5        U $ )Set line width.r_  r   )r4   r   r   s      r-   set_line_width#ContentStreamBuilder.set_line_width  s%    '#?Tr,   c                    [        X/[        S5      5      [        X4/[        S5      5      /nU R                  US   5        U R                  US   5        U $ )z
Draw line.mlr   r   r   )r4   x1y1x2y2instss         r-   lineContentStreamBuilder.line  sR     %bXx}=$bXx}=
 	U1XU1Xr,   Nc                    Uc  / n[        U[        [        -  5      (       a  X4nSn[        X/[	        S5      5      nU R                  U5        U $ )Set dashes.r   d)rv   r   floatr   r   r   )r4   arrayphaser   s       r-   
set_dashesContentStreamBuilder.set_dashes  sL    =EeS5[))NEE'FTr,   c                T    [        U/[        S5      5      nU R                  U5        U $ )zDraw XObject.

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

Args:
    name: Name of XObject
Dor   )r4   rz   r   s      r-   draw_xobject!ContentStreamBuilder.draw_xobject  r  r,   c                ,    [        U R                  5      $ )zBuild content stream.)rW   r   rH   s    r-   buildContentStreamBuilder.build  s    T\\""r,   )r   )r   r   )r   zContentStreamBuilder | bytesr   r   )r  r   r  r   )r  r   )r  r   r  int | float | Decimal)r  r  )r(  r   )r.  r  )r4  rW   )r=  zbytes | int | float | Decimal)r4  rW   r   r   r   r   )r]  r  r^  r  r_  r  r`  r  )rf  r  rg  r  rh  r  )rw  r  rx  r  ry  r  rz  r  Nr   )rz   r   )rB   rW   )(r$   r%   r&   r'   r(   ro   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r#  r)  r/  r5  r>  rC  rG  rM  rP  rU  rY  ra  ri  rn  rr  r|  r  r  r  r+   r#   r,   r-   r   r     s    !#?*	 		.
,/?B"" 	#r,   r   c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	LoadedImagei  zWLoaded image.

This class is used to track images that have been loaded into a
canvas.
r   rz   zImage.Imageimager#   N)r$   r%   r&   r'   r(   r   r+   r#   r,   r-   r  r    s     Jr,   r  c                      \ rS rSrSrSSS jjrSS jrSS jrS rS r	SS	 jr
SS
 jrSS jrS rS rS r\SS.SS jj5       rSS jrSrg)_CanvasAccessori  z;Contains all drawing methods class for drawing on a Canvas.Nc                6    Xl         Ub  UO/ U l        SU l        g r  _cs_images_stack_depth)r4   csimagess      r-   ro   _CanvasAccessor.__init__  s    !'!3vr,   c                    UR                   UR                  UR                  pCnU R                  R	                  X#U5        U $ )zSet stroke color.)r   r   r   r  ri  r4   colorrf  rg  rh  s        r-   stroke_color_CanvasAccessor.stroke_color  s2    ))U[[%**a!!!*r,   c                    UR                   UR                  UR                  pCnU R                  R	                  X#U5        U $ )zSet fill color.)r   r   r   r  rn  r  s        r-   
fill_color_CanvasAccessor.fill_color  s2    ))U[[%**aa(r,   c                <    U R                   R                  U5        U $ )rq  )r  rr  )r4   r   s     r-   
line_width_CanvasAccessor.line_width  s    &r,   c                r    U R                   R                  XX45        U R                   R                  5         U $ )z"Draw line from (x1,y1) to (x2,y2).)r  r|  rU  )r4   rw  rx  ry  rz  s        r-   r|  _CanvasAccessor.line  s)    bb%!!#r,   c                    U R                   R                  XX45        U(       a  U R                   R                  5         U $ U R                   R                  5         U $ )zDDraw optionally filled rectangle at (x,y) with width w and height h.)r  ra  rY  rU  )r4   r]  r^  r_  r`  rY  s         r-   rect_CanvasAccessor.rect   sD    !!!-HHMMO  HH%%'r,   c           
     4   U R                  [        USSXRU5      S9   [        U[        [        -  5      (       a  [
        R                  " U5      nUR                  5         UR                  S:X  a  UR                  S5      nUR                  S;  a  [        SUR                   35      e[        R                  " SS9n[        Xa5      nU R                  R                  U5        U R                   R#                  U5        S	S	S	5        U $ ! , (       d  f       U $ = f)
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   rv   r   rV   r   openr}   r(  convertrn   r   randomr  r  r   r  r  )r4   r  r]  r^  r   heightrz   lis           r-   
draw_image_CanvasAccessor.draw_image	  s    __uaFq A_B%,,

5)JJLzzS e,zz!22 #;EJJ<!HII;;d+DT)BLL#HH!!$' C  CB s   C!D
Dc                    U R                   R                  UR                   5        U R                   R                  5         U $ )zDraw text object.)r  r   r   rR   s     r-   	draw_text_CanvasAccessor.draw_text  s+    !r,   c                8    U R                   R                  " U6   U $ )r  )r  r  )r4   argss     r-   dashes_CanvasAccessor.dashes  s    T"r,   c                d    U R                   R                  5         U =R                  S-  sl        U $ )r   r   )r  r   r  rH   s    r-   r   _CanvasAccessor.push$  s%    Qr,   c                d    U R                   R                  5         U =R                  S-  sl        U $ )z$Restore the previous graphics state.r   )r  r   r  rH   s    r-   r   _CanvasAccessor.pop*  s%    Qr,   r  c             #  |   #    U R                  5         Ub  U R                  U5        U v   U R                  5         g7f)zSave the graphics state and restore it on exit.

Optionally, concatenate a transformation matrix. Implements
the commonly used pattern of:

    q cm ... Q
N)r   r   r   )r4   r   s     r-   r  _CanvasAccessor.save_state0  s.      			>GGBK

s   :<c                <    U R                   R                  U5        U $ )z>Concatenate a new transformation matrix to the current matrix.)r  r   r4   r   s     r-   r   _CanvasAccessor.cm?  s    Fr,   r  r   )r  r   )r  r   )rY  bool)r  zPath | str | Image.Image)r5   Text)r   zMatrix | Noner  )r$   r%   r&   r'   r(   ro   r  r  r  r|  r  r  r  r  r   r   r   r  r   r+   r#   r,   r-   r  r    sX    E

 
 04  r,   r  c                  ^    \ rS rSrS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
rg)CanvasiE  a*  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.
c               |   Xl         [        R                  " 5       U l        U R                  R	                  US9U l        [        [        5       [        5       S9U R
                  l        [        5       U l	        / U l
        [        U R                  U R                  5      U l        U R                  R                  5         g)zInitialize a canvas.)	page_size)r/   XObjectN)r  r   new_pdfadd_blank_page_pager   	Resourcesr   r  r  r  	_accessordor   )r4   r  s     r-   ro   Canvas.__init__O  sx    "GGI	YY--	-B
)z|Z\R

')*,(4<<@r,   c                |    UR                  U R                  5      U R                  R                  R                  U'   g)zAdd a font to the page.N)r=   r  r  r  r/   )r4   resource_namer  s      r-   add_fontCanvas.add_fontZ  s)    37==3K

!!-0r,   c                    U R                   $ )z,Do operations on the current graphics state.)r  rH   s    r-   r  	Canvas.do^  s     ~~r,   c           
        U R                   R                  UR                  R                  5       UR                  R                  UR                  R
                  UR                  R                  S;   a  [        R                  O[        R                  [        R                  [        R                  UR                  R                  S:X  a  SS9$ SS9$ )N)r  r  r  r      )WidthHeight
ColorSpacerb   rc   BitsPerComponent)r  make_streamr  tobytesr   r  r(  r   
DeviceGray	DeviceRGBr  r   )r4   r  s     r-   _save_imageCanvas._save_imagec  s    yy$$HH((..88??#%88==J#>DNNJJ"$((--3"6Q % 

 
	
 => % 

 
	
r,   c                   U R                   R                  5         U R                  R                  S:w  a  [        R                  S5        U R                  R                  U R                  R                  5       5      U R                  l        U R                   H?  nU R                  U5      U R                  R                  R                  UR                   '   MA     [#        5       nU R                  R%                  U5        UR'                  S5        [(        R*                  " U5      nU R                   R-                  5         U$ )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  r   r   r  r  r  r  r  Contentsr  r  r  r  rz   r   saveseekr   r  r   )r4   r  bior   s       r-   to_pdfCanvas.to_pdfp  s    >>&&!+KK- #ii33DHHNN4DE

,,B484D4DR4HDJJ  ((1 i		s# 	r,   Nc                @    U R                  5       R                  X5      $ r   )r  _repr_mimebundle_)r4   includeexcludes      r-   r  Canvas._repr_mimebundle_  s    {{}..w@@r,   )r  r  r  r  r  r  )r  ztuple[int | float, int | float])r  r   r  r/   )rB   r  )r  r  )rB   r   )NN)r$   r%   r&   r'   r(   ro   r  rX   r  r  r  r  r+   r#   r,   r-   r  r  E  s6    	L  
(Ar,   r  c                  h    \ rS rSrSr\R                  4S jrSS jrS r	SS jr
SS jrS rS	 rS
rg)r  i  z3Text object for rendering text on a pikepdf canvas.c                b    [        5       U l        U R                  R                  5         Xl        gr   )r   r  r   
_direction)r4   	directions     r-   ro   Text.__init__  s!    ')#r,   c                <    U R                   R                  X5        U $ )zSet font and size.)r  r  )r4   r  r  s      r-   r  	Text.font  s    t*r,   c                <    U R                   R                  U5        U $ )r&  )r  r)  )r4   r(  s     r-   render_modeText.render_mode  s    ##D)r,   c                <    U R                   R                  U5        U $ )zSet text matrix.)r  r#  r  s     r-   text_transformText.text_transform  s      (r,   c                   [        U[        5      (       a  SUR                  S5      -   nOUnU R                  [        R
                  :X  a  U R                  R                  U5        U $ U R                  R                  [        R                  5        U R                  R                  U5        U R                  R                  5         U $ )a%  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)rv   rV   rS   r  r    r)   r  r5  r  r   ReversedCharsr  )r4   r5   r4  s      r-   show	Text.show  s     dC  !DKK
$;;GG??m///HHw'
  HH))$*<*<=HHw'HH'')r,   c                <    U R                   R                  U5        U $ )r,  )r  r/  )r4   r.  s     r-   horiz_scaleText.horiz_scale  s    ,,U3r,   c                <    U R                   R                  X5        U $ )zMove cursor.)r  rM  )r4   r]  r^  s      r-   rM  Text.move_cursor  s    Q"r,   )r  r  N)r  r   r  r  r  )r5   r@   )r$   r%   r&   r'   r(   r    r)   ro   r  r  r  r	  r  rM  r+   r#   r,   r-   r  r    s1    =!.!2!2 $


*
r,   r  )r   r   )r   r   rB   dict)=r(   
__future__r   loggingabcr   r   collectionsr   
contextlibr   dataclassesr   decimalr
   enumr   ior   pathlibr   PILr   pikepdf._corer   r   r   pikepdf._datar   pikepdf.modelsr   pikepdf.objectsr   r   r   r   r   	getLoggerr$   r   r   BLACKWHITEBLUECYANGREEN	DARKGREENMAGENTAREDr    r/   rE   rZ   rh   r   r   r   r  r  r  r  r#   r,   r-   <module>r)     ss   1 "  # " % !      ? ? . 1 E E! 	7=>aAqaAqQ1aQ1aaAq!S!Q	
1a
Aq!Q7D 73 <Ad A:
 
2Y3 Y3x8&B# B#J
   a aH@A @AF5 5r,   