
    Ng*                       d 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 ddlmZ ddlmZmZmZmZ e	rddlmZ ee_        ee_         G d	 d
 ee                    Z G d de          Z G d dee          Z G d dee          Z G d dee          Z G d dee          Z G d dee          Z G d dee          ZdS )at  Provide classes to stand in for PDF objects.

The purpose of these is to provide nice-looking classes to allow explicit
construction of PDF objects and more pythonic idioms and facilitate discovery
by documentation generators and linters.

It's also a place to narrow the scope of input types to those more easily
converted to C++.

There is some deliberate "smoke and mirrors" here: all of the objects are truly
instances of ``pikepdf.Object``, which is a variant container object. The
``__new__`` constructs a ``pikepdf.Object`` in each case, and the rest of the
class definition is present as an aide for code introspection.
    )annotations)IterableMapping)token_urlsafe)TYPE_CHECKINGAnycast)_core)MatrixObject
ObjectType	Rectangle)Pdfc                      e Zd ZdZddZdS )	_ObjectMetazSupport instance checking.instancer   returnboolc                R    t          |          t          urdS | j        |j        k    S )NF)typer   object_type
_type_code)selfr   s     K/var/www/html/ai-engine/env/lib/python3.11/site-packages/pikepdf/objects.py__instancecheck__z_ObjectMeta.__instancecheck__0   s)    >>''58#666    N)r   r   r   r   )__name__
__module____qualname____doc__r    r   r   r   r   -   s.        $$7 7 7 7 7 7r   r   c                  *    e Zd ZdZddZdd
ZddZdS )_NameObjectMetaz9Support usage pikepdf.Name.Whatever -> Name('/Whatever').attrstrr   Namec                    |                     d          s|dk    rt          t          |          S t          d|z             S )N_r   /)
startswithgetattrr   r&   )r   r$   s     r   __getattr__z_NameObjectMeta.__getattr__:   sB    ??3 	.4=#8#8;---C$Jr   valuer   Nonec                     t          d          )NzgAttributes may not be set on pikepdf.Name. Perhaps you meant to modify a Dictionary rather than a Name?)AttributeError)r   r$   r-   s      r   __setattr__z_NameObjectMeta.__setattr__?   s     6
 
 	
r   itemc                l    |                     d          r
|dd          }t          d| d| d          )Nr)      zHpikepdf.Name is not subscriptable. You probably meant:
    pikepdf.Name.z
or
    pikepdf.Name('/z')
)r*   	TypeError)r   r2   s     r   __getitem__z_NameObjectMeta.__getitem__H   s[    ??3 	8D- $- - #'- - -
 
 	
r   N)r$   r%   r   r&   )r$   r%   r-   r   r   r.   )r2   r%   r   r.   )r   r   r   r    r,   r1   r6   r!   r   r   r#   r#   7   sV        CC       

 
 
 

 
 
 
 
 
r   r#   c                  B    e Zd ZdZej        ZddZeddd            Z	dS )r&   aN  Construct a PDF Name object.

    Names can be constructed with two notations:

        1. ``Name.Resources``

        2. ``Name('/Resources')``

    The two are semantically equivalent. The former is preferred for names
    that are normally expected to be in a PDF. The latter is preferred for
    dynamic names and attributes.
    name
str | Namer   c                    t          |t                    rt          d          t          |t                    r|S t	          j        |          S )zConstruct a PDF Name.zName should be str)
isinstancebytesr5   r&   r
   	_new_nameclsr8   s     r   __new__zName.__new__c   sL     dE"" 	20111dD!! 	Kt$$$r       len_intprefixr%   c                R    t          |          }t          j        d| |           S )a`  Generate a cryptographically strong, random, valid PDF Name.

        If you are inserting a new name into a PDF (for example,
        name for a new image), you can use this function to generate a
        cryptographically strong random name that is almost certainly already
        not already in the PDF, and not colliding with other existing names.

        This function uses Python's secrets.token_urlsafe, which returns a
        URL-safe encoded random number of the desired length. An optional
        *prefix* may be prepended. (The encoding is ultimately done with
        :func:`base64.urlsafe_b64encode`.) Serendipitously, URL-safe is also
        PDF-safe.

        When the length parameter is 16 (16 random bytes or 128 bits), the result
        is probably globally unique and can be treated as never colliding with
        other names.

        The length of the returned string may vary because it is encoded,
        but will always have ``8 * len_`` random bits.

        Args:
            len_: The length of the random string.
            prefix: A prefix to prepend to the random string.
        r)   )r   r
   r=   )r?   rC   rE   random_strings       r   randomzName.randomm   s/    4 &d++:6:=::;;;r   N)r8   r9   r   r&   )rA   rB   )rC   rD   rE   r%   r   r&   )
r   r   r   r    r   name_r   r@   classmethodrH   r!   r   r   r&   r&   S   sa          "K% % % % < < < < [< < <r   r&   )	metaclassc                  (    e Zd ZdZej        ZddZdS )Operatora  Construct an operator for use in a content stream.

    An Operator is one of a limited set of commands that can appear in PDF content
    streams (roughly the mini-language that draws objects, lines and text on a
    virtual PDF canvas). The commands :func:`parse_content_stream` and
    :func:`unparse_content_stream` create and expect Operators respectively, along
    with their operands.

    pikepdf uses the special Operator "INLINE IMAGE" to denote an inline image
    in a content stream.
    r8   r%   r   c                F    t          dt          j        |                    S )zConstruct an operator.rM   )r	   r
   _new_operatorr>   s     r   r@   zOperator.__new__   s    J 3D 9 9:::r   N)r8   r%   r   rM   )r   r   r   r    r   operatorr   r@   r!   r   r   rM   rM      s<        
 
 %K; ; ; ; ; ;r   rM   c                  (    e Zd ZdZej        ZddZdS )StringzConstruct a PDF String object.sstr | bytesr   c                |    t          |t                    rt          j        |          S t          j        |          S )zConstruct a PDF String.

        Args:
            s: The string to use. String will be encoded for
                PDF, bytes will be constructed without encoding.
        )r;   r<   r
   _new_string_new_string_utf8)r?   rS   s     r   r@   zString.__new__   s8     a 	($Q'''%a(((r   N)rS   rT   r   rR   )r   r   r   r    r   stringr   r@   r!   r   r   rR   rR      s6        ((#K	) 	) 	) 	) 	) 	)r   rR   c                  *    e Zd ZdZej        ZdddZdS )	ArrayzConstruct a PDF Array object.Na$Iterable | Rectangle | Matrix | Noner   c                b   t          |t          t          f          rt          d          |g }nlt          |t          t
          f          r|                                S t          |t                    r't          t          |	                                          S t          j        |          S )zConstruct a PDF Array.

        Args:
            a: An iterable of objects. All objects must be either
                `pikepdf.Object` or convertible to `pikepdf.Object`.
        z.Strings cannot be converted to arrays of chars)r;   r%   r<   r5   r   r   as_arrayrZ   r	   __copy__r
   
_new_array)r?   r[   s     r   r@   zArray.__new__   s     a#u&& 	NLMMM9AAIv.// 	-::<<5!! 	-qzz||,,,"""r   N)r[   r\   r   rZ   )r   r   r   r    r   arrayr   r@   r!   r   r   rZ   rZ      s;        ''"K# # # # # # #r   rZ   c                  *    e Zd ZdZej        ZdddZdS )	
Dictionaryz"Construct a PDF Dictionary object.NdMapping | Noner   c                   |r|t          d          |r0t          j        d |                                D                       S t	          |t
                    r|                                S |si }|r:t          d |                                D                       rt          d          t          j        |          S )a  Construct a PDF Dictionary.

        Works from either a Python ``dict`` or keyword arguments.

        These two examples are equivalent:

        .. code-block:: python

            pikepdf.Dictionary({'/NameOne': 1, '/NameTwo': 'Two'})

            pikepdf.Dictionary(NameOne=1, NameTwo='Two')

        In either case, the keys must be strings, and the strings
        correspond to the desired Names in the PDF Dictionary. The values
        must all be convertible to `pikepdf.Object`.
        Nz1Cannot use both a mapping object and keyword argsc                     i | ]\  }}d |z   |S )r)   r!   ).0kvs      r   
<dictcomp>z&Dictionary.__new__.<locals>.<dictcomp>   s"    )R)R)R41a37Q)R)R)Rr   c              3  N   K   | ] }|d k    p|                     d            V  !dS )r)   N)r*   )ri   keys     r   	<genexpr>z%Dictionary.__new__.<locals>.<genexpr>   s:      OOsSCZ:s~~c':':#:OOOOOOr   z3Dictionary created from strings must begin with '/')

ValueErrorr
   _new_dictionaryitemsr;   rd   r_   anykeysKeyError)r?   re   kwargss      r   r@   zDictionary.__new__   s    "  	RamPQQQ 	T ()R)R6<<>>)R)R)RSSSa$$ 	 ::<< 	A 	ROOaffhhOOOOO 	RPQQQ$Q'''r   ra   )re   rf   r   rd   )r   r   r   r    r   
dictionaryr   r@   r!   r   r   rd   rd      s;        ,,'K( ( ( ( ( ( (r   rd   c                  *    e Zd ZdZej        Zd	d
dZdS )StreamzConstruct a PDF Stream object.Nownerr   databytes | Noner   c                    |t          d          d}|s|rt          |fi |}t          j        ||          }|r||_        |S )a  Create a new stream object.

        Streams stores arbitrary binary data and may or may not be compressed.
        It also may or may not be a page or Form XObject's content stream.

        A stream dictionary is like a pikepdf.Dictionary or Python dict, except
        it has a binary payload of data attached. The dictionary describes
        how the data is compressed or encoded.

        The dictionary may be initialized just like pikepdf.Dictionary is initialized,
        using a mapping object or keyword arguments.

        Args:
            owner: The Pdf to which this stream shall be attached.
            data: The data bytes for the stream.
            d: An optional mapping object that will be used to construct the stream's
                dictionary.
            kwargs: Keyword arguments that will define the stream dictionary. Do not set
                /Length here as pikepdf will manage this value. Set /Filter
                if the data is already encoded in some format.

        Examples:
            Using kwargs:
                >>> pdf = pikepdf.Pdf.new()
                >>> s1 = pikepdf.Stream(
                ...     pdf,
                ...     b"uncompressed image data",
                ...     BitsPerComponent=8,
                ...     ColorSpace=pikepdf.Name.DeviceRGB,
                ... )
            Using dict:
                >>> pdf = pikepdf.Pdf.new()
                >>> d = pikepdf.Dictionary(Key1=1, Key2=2)
                >>> s2 = pikepdf.Stream(
                ...     pdf,
                ...     b"data",
                ...     d
                ... )

        .. versionchanged:: 2.2
            Support creation of ``pikepdf.Stream`` from existing dictionary.

        .. versionchanged:: 3.0
            ``obj`` argument was removed; use ``data``.
        Nz!Must make Stream from binary data)r5   rd   r
   _new_streamstream_dict)r?   rz   r{   re   rv   r   streams          r   r@   zStream.__new__   si    \ <?@@@ 	2 	2$Q11&11K"5$// 	-!,Fr   )NN)rz   r   r{   r|   r   ry   )r   r   r   r    r   r   r   r@   r!   r   r   ry   ry      s;        ((#K8 8 8 8 8 8 8r   ry   N)r    
__future__r   collections.abcr   r   secretsr   typingr   r   r	   pikepdfr
   pikepdf._corer   r   r   r   r   r   r   r   r   r#   r&   rM   rR   rZ   rd   ry   r!   r   r   <module>r      s]    # " " " " " - - - - - - - - " ! ! ! ! ! + + + + + + + + + +       ? ? ? ? ? ? ? ? ? ? ? ? 
   
 7 7 7 7 7$$v,, 7 7 7
 
 
 
 
k 
 
 
85< 5< 5< 5< 5<6_ 5< 5< 5< 5<p; ; ; ; ;v ; ; ; ;() ) ) ) )V{ ) ) ) )"# # # # #Fk # # # #0#( #( #( #( #(; #( #( #( #(L= = = = =V{ = = = = = =r   