
    Ng=                       d 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 ddlmZmZmZmZmZ  G d	 d
e          Zej        dej        dej        dej        dej        dej        diZ e ej        e                                                    Z	 d'ddddddd(dZ 	 d'd)d Z! G d! d"e"          Z# G d# d$          Z$ G d% d&          Z%dS )*z7Support for document outlines (e.g. table of contents).    )annotations)Iterable)Enum)chain)cast)PagePdf)Array
DictionaryNameObjectStringc                  2    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )PageLocationz.Page view location definitions, from PDF spec.                        N)__name__
__module____qualname____doc__XYZFitFitHFitVFitRFitBFitBHFitBV     S/var/www/html/ai-engine/env/lib/python3.11/site-packages/pikepdf/models/outlines.pyr   r      s<        88
C
CDDDDEEEEr&   r   )lefttopzoom)r)   )r(   )r(   bottomrightr)   Nr(   r)   r,   r+   r*   pdfr	   page_numintpage_locationPageLocation | str | Noner(   float | Noner)   r,   r+   r*   returnr
   c          
     0    t          | |||||||          S )au  Create a destination ``Array`` with reference to a Pdf document's page number.

    Arguments:
        pdf: PDF document object.
        page_num: Page number (zero-based).
        page_location: Optional page location, as a string or :enum:`PageLocation`.
        left: Specify page viewport rectangle.
        top: Specify page viewport rectangle.
        right: Specify page viewport rectangle.
        bottom: Specify page viewport rectangle.
        zoom: Specify page viewport rectangle's zoom level.

    left, top, right, bottom, zoom are used in conjunction with the page fit style
    specified by *page_location*.
    )r1   r(   r)   r,   r+   r*   )_make_page_destination)r.   r/   r1   r(   r)   r,   r+   r*   s           r'   make_page_destinationr7   *   s3    4 "#	 	 	 	r&   c                .   d                                  D             | j        |         j        g}|rt          |t                    r
|}|j        }n2|}	 t          |         }n!# t          $ r t          d|           d w xY w|                    t          d|                      t                              |          }|r!|                    fd|D                        n|                    t          j                   t          |          S )Nc                    i | ]
\  }}|||S Nr%   .0kvs      r'   
<dictcomp>z*_make_page_destination.<locals>.<dictcomp>V   s    ???tq!ar&   z*Invalid or unsupported page location type /c              3  D   K   | ]}                     |d           V  dS )r   N)get)r<   r=   kwargss     r'   	<genexpr>z)_make_page_destination.<locals>.<genexpr>h   s1      @@Avzz!Q''@@@@@@r&   )itemspagesobj
isinstancer   nameKeyError
ValueErrorappendr   PAGE_LOCATION_ARGSrB   extendr   r
   )r.   r/   r1   rC   resloc_keyloc_strdest_arg_namess      `    r'   r6   r6   P   s2    @?v||~~???F$'Ih$7$;#<C m\22 
	#GlGG#G&w/    JJJ  	

4G&&'''+//88 	AJJ@@@@@@@@@@

48::s   A% %Bc                      e Zd ZdZdS )OutlineStructureErrorz1Indicates an error in the outline data structure.N)r   r   r   r   r%   r&   r'   rT   rT   n   s        ;;;;r&   rT   c                  `    e Zd ZdZ	 	 	 	 dddddddd dZd Zd Zed!d            Zd"d#dZ	dS )$OutlineItema  Manage a single item in a PDF document outlines structure.

    Includes nested items.

    Arguments:
        title: Title of the outlines item.
        destination: Page number, destination name, or any other PDF object
            to be used as a reference when clicking on the outlines entry. Note
            this should be ``None`` if an action is used instead. If set to a
            page number, it will be resolved to a reference at the time of
            writing the outlines back to the document.
        page_location: Supplemental page location for a page number
            in ``destination``, e.g. ``PageLocation.Fit``. May also be
            a simple string such as ``'FitH'``.
        action: Action to perform when clicking on this item. Will be ignored
           during writing if ``destination`` is also set.
        obj: ``Dictionary`` object representing this outlines item in a ``Pdf``.
            May be ``None`` for creating a new object. If present, an existing
            object is modified in-place during writing and original attributes
            are retained.
        left, top, bottom, right, zoom: Describes the viewport position associated
            with a destination.

    This object does not contain any information about higher-level or
    neighboring elements.

    Valid destination arrays:
        [page /XYZ left top zoom]
        generally
        [page, PageLocationEntry, 0 to 4 ints]
    Nr-   titlestrdestination"Array | String | Name | int | Noner1   r2   actionDictionary | NonerG   r(   r3   r)   r,   r+   r*   c                  || _         || _        || _        i | _        || _        | j        | j        t          d          || _        t          ||||	|
          }d |                                D             | _        d| _	        g | _
        dS )zInitialize OutlineItem.Nz-Only one of destination and action may be setr-   c                    i | ]
\  }}|||S r:   r%   r;   s      r'   r?   z(OutlineItem.__init__.<locals>.<dictcomp>   s    $V$V$VdaQr&   F)rW   rY   r1   page_location_kwargsr[   rK   rG   dictrE   	is_closedchildren)selfrW   rY   r1   r[   rG   r(   r)   r,   r+   r*   rC   s               r'   __init__zOutlineItem.__init__   s     
&*$&!'DK,CLMMM4Sf4PPP$V$Vfllnn$V$V$V!+-r&   c                   | j         r| j        rd}nd}nd}| j        t          | j        t                    r$| j        d         }t          |          }|j        }ntt          | j        t                    rd| j         d}nNt          | j        t                    rd| j         d}n(t          | j        t                    rd| j         d}nd	}| d
| j
         d| S )Nz[+]z[-]z[ ]r   z7<Named Destination in document .Root.Names dictionary: >z7<Named Destination in document .Root.Dests dictionary: z<Page z<Action> z -> )rb   ra   rY   rH   r
   r   labelr   r   r0   rW   )rc   oc_indicatorraw_pagepagedests        r'   __str__zOutlineItem.__str__   s&   = 	!~ %$$ L'$*E22 4  +A.H~~zD,f55 4+'+ + +  D,d33 4+'+ + +  D,c22 43 0333D7777777r&   c                2    d| j         j         d| j         dS )N	<pikepdf.z: "z">)	__class__r   rW   rc   s    r'   __repr__zOutlineItem.__repr__   s"    E4>2EEtzEEEEr&   r   c                   t          |j                  }|                    t          j                  }|4t          |t          t          t          f          st          d|          |                    t          j	                  }|'t          |t                    st          d|           | ||||          S )zCreate a ``OutlineItem`` from a ``Dictionary``.

        Does not process nested items.

        Arguments:
            obj: ``Dictionary`` object representing a single outline node.
        Nz+Unexpected object type in Outline's /Dest: z(Unexpected object type in Outline's /A: )rY   r[   rG   )rX   TitlerB   r   DestrH   r
   r   rT   Ar   )clsrG   rW   rY   r[   s        r'   from_dictionary_objectz"OutlineItem.from_dictionary_object   s     CIggdi((":%.,
 ,
" (MkMM   j&D&D'E6EE   s5k&cJJJJr&   Fr.   r	   
create_newboolr4   c                   |s| j         )|                    t                                x| _         }n| j         }| j        |_        | j        Zt          | j        t                    r#t          || j        | j	        fi | j
        | _        | j        |_        t          j        |v r|`n#| j        | j        |_        t          j        |v r|`|S )a\  Create/update a ``Dictionary`` object from this outline node.

        Page numbers are resolved to a page reference on the input
        ``Pdf`` object.

        Arguments:
            pdf: PDF document object.
            create_new: If set to ``True``, creates a new object instead of
                modifying an existing one in-place.
        )rG   make_indirectr   rW   rt   rY   rH   r0   r7   r1   r_   ru   r   rv   r[   )rc   r.   ry   rG   s       r'   to_dictionary_objectz OutlineItem.to_dictionary_object   s      	) ..z||<<<DHss(CJ	'$*C00 #8$&$ $ /	$ $  'CHv}}E[$KCEyCH
r&   )NNNN)rW   rX   rY   rZ   r1   r2   r[   r\   rG   r\   r(   r3   r)   r3   r,   r3   r+   r3   r*   r3   )rG   r   )F)r.   r	   ry   rz   r4   r   )
r   r   r   r   rd   rm   rr   classmethodrx   r}   r%   r&   r'   rV   rV   r   s         F ;?37$(!%. " "#!. . . . . .8 8  8  8DF F F K K K [K0      r&   rV   c                  v    e Zd ZdZd'd(d
Zd Zd Zd Zd Zd Z	d)dZ
d*dZd Zd Zd+d#Zed,d%            Zd&S )-OutlineaE  Maintains a intuitive interface for creating and editing PDF document outlines.

    See |pdfrm| section 12.3.

    Arguments:
        pdf: PDF document object.
        max_depth: Maximum recursion depth to consider when reading the outline.
        strict: If set to ``False`` (default) silently ignores structural errors.
            Setting it to ``True`` raises a
            :class:`pikepdf.OutlineStructureError`
            if any object references re-occur while the outline is being read or
            written.

    See Also:
        :meth:`pikepdf.Pdf.open_outline`
       Fr.   r	   	max_depthr0   strictrz   c                L    d| _         || _        || _        || _        d| _        dS )zInitialize Outline.NF)_root_pdf
_max_depth_strict	_updating)rc   r.   r   r   s       r'   rd   zOutline.__init__!  s)    /3
	#r&   c                *    t          | j                  S r:   )rX   rootrq   s    r'   rm   zOutline.__str__)  s    49~~r&   c                L    d| j         j         dt          | j                   dS )Nro   z: z items>)rp   r   lenr   rq   s    r'   rr   zOutline.__repr__,  s(    M4>2MMc$)nnMMMMr&   c                :   |r|                     d           d S |                    ddd          5  t          | j                  D ];\  }}|                                 |                    t          |                     <	 d d d            d S # 1 swxY w Y   d S )Nz...r   z!pikepdf.models.outlines.Outline<
z
>)textgroup	enumerater   	breakableprettyrX   )rc   pcycle_items        r'   _repr_pretty_zOutline._repr_pretty_/  s     	(FF5MMMMM@%HH ( ((33 ( (GAtKKMMMHHSYY''''(( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   ABBBc                    d| _         | S )NT)r   rq   s    r'   	__enter__zOutline.__enter__8  s    r&   c                n    	 |
	 d| _         d S |                                  d| _         d S # d| _         w xY w)NF)r   _save)rc   exc_typeexc_valexc_tbs       r'   __exit__zOutline.__exit__<  sJ    	## #DNNN JJLLL"DNNNUDN""""s   + + 	4parentr   outline_itemsIterable[OutlineItem]levelvisited_objsset[tuple[int, int]]c                   d}d }d }|D ]}|                     | j                  }	|	j        }
|
|v r7| j        rt	          d|
 d          |                     | j        d          }	n|                    |
           ||	_        |dz  }||	|_        ||	_        n|	}t          j        |	v r|	`|	}|| j
        k     r|j        }nd}|                     |	||dz   |           |j        r!t          t          |	j                   |	_        |t          t          |	j                  z  }|r%||J t          j        |v r|`||_        ||_        n t          j        |v r|`t          j        |v r|`||_        d S )Nr   Outline object  reoccurred in structureT)ry   r   r%   )r}   r   objgenr   rT   addParentNextPrevr   r   rb   _save_level_outlinera   r   r0   CountFirstLast)rc   r   r   r   r   countprevfirstr   out_objr   	sub_itemss               r'   r   zOutline._save_level_outlineD  s    "&#'! 	2 	2D//	::G^F%%< /J&JJJ   33DI$3OO  (((#GNQJE#	#9''Dt&&37=			$$WiLQQQ~ 2!%c7=!9!9 9c7=111 
	 #(9(9(9yD  I FLFKKzV##LyF""Kr&   	first_objlist[Object]c                   |}|r\|j         }||v r| j        rt          d| d          d S |                    |           t                              |          }|                    t          j                  }t          |t                    rl|| j        k     ra|                     ||j        |dz   |           |                    t          j                  }	t          |	t                    r|	dk     rd|_        |                    |           |                    t          j                  }
|
t          |
t                    r|
}nt          d| d          |Zd S d S )Nr   r   r   r   Tz points to non-dictionary)r   r   rT   r   rV   rx   rB   r   r   rH   r   r   _load_level_outlinerb   r   r0   ra   rL   r   )rc   r   r   r   r   current_objr   r   first_childr   next_objs              r'   r   zOutline._load_level_outlinez  s    *3 	 'F%%< /J&JJJ   V$$$55kBBD%//$*55K+z22 *ut7N7N((	<   $
33eS)) *eaii%)DN  &&&"ty11H:h
#C#C&+GfGGG  1  	 	 	 	 	r&   c                D   | j         d S t          j        | j        j        v r| j        j        j        }nC| j                            t          t          j                            x| j        j        _        }|                     || j         dt                                 d S )N)Typer   )	r   r   Outlinesr   Rootr|   r   r   set)rc   outliness     r'   r   zOutline._save  s    :F=DIN**y~.HH151H1H...2 2 DIN#h 	  4:q#%%@@@@@r&   c                    g x| _         }t          j        | j        j        vrd S | j        j        j        pi }|                    t          j                  }|r&|                     ||dt                                 d S d S )Nr   )	r   r   r   r   r   rB   r   r   r   )rc   r   r   r   s       r'   _loadzOutline._load  s    
T=	..F9>*0bLL,,	 	@$$Ya?????	@ 	@r&   rW   rX   rY   Array | int | Noner4   rV   c                    | j         |                                  t          ||          }| j         	|g| _         n| j                             |           | j        s|                                  |S )zAdd an item to the outline.

        Arguments:
            title: Title of the outline item.
            destination: Destination to jump to when the item is selected.

        Returns:
            The newly created :class:`OutlineItem`.
        )r   r   rV   rL   r   r   )rc   rW   rY   r   s       r'   r   zOutline.add  sm     :JJLLL5+..:DJJJd###~ 	JJLLLr&   list[OutlineItem]c                    | j         |                                  t          t          t                   | j                   S )z$Return the root node of the outline.)r   r   r   listrV   rq   s    r'   r   zOutline.root  s0     :JJLLLD%tz222r&   N)r   F)r.   r	   r   r0   r   rz   )r   r   r   r   r   r0   r   r   )r   r   r   r   r   r0   r   r   )rW   rX   rY   r   r4   rV   )r4   r   )r   r   r   r   rd   rm   rr   r   r   r   r   r   r   r   r   propertyr   r%   r&   r'   r   r     s        "      N N N( ( (  # # #4 4 4 4l" " " "H	A 	A 	A@ @ @   * 3 3 3 X3 3 3r&   r   r:   )r.   r	   r/   r0   r1   r2   r(   r3   r)   r3   r,   r3   r+   r3   r*   r3   r4   r
   )r.   r	   r/   r0   r1   r2   r4   r
   )&r   
__future__r   collections.abcr   enumr   	itertoolsr   typingr   pikepdf._corer   r	   pikepdf.objectsr
   r   r   r   r   r   r   r   r    r!   r#   r$   rM   r   from_iterablevaluesALL_PAGE_LOCATION_KWARGSr7   r6   	ExceptionrT   rV   r   r%   r&   r'   <module>r      s+  
 > = " " " " " " $ $ $ $ $ $                   # # # # # # # # C C C C C C C C C C C C C C
 
 
 
 
4 
 
 
 -xy9	  32u23E3L3L3N3NOOPP  04#
 # # # # # #R 04    << < < < <I < < <Z Z Z Z Z Z Z Zz}3 }3 }3 }3 }3 }3 }3 }3 }3 }3r&   