
    h5jB                       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j0                  dej2                  dej4                  diZ e ej:                  ej=                                     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%y)z7Support for document outlines (e.g. table of contents).    )annotations)Iterable)Enum)chain)cast)PagePdf)Array
DictionaryNameObjectStringc                  0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)PageLocationz.Page view location definitions, from PDF spec.                        N)__name__
__module____qualname____doc__XYZFitFitHFitVFitRFitBFitBHFitBV     M/var/www/html/qr/venv/lib/python3.12/site-packages/pikepdf/models/outlines.pyr   r      s-    8
C
CDDDDEEr&   r   )lefttopzoom)r)   )r(   )r(   bottomrightr)   Nr(   r)   r,   r+   r*   c          
     (    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*.
    )page_locationr(   r)   r,   r+   r*   )_make_page_destination)pdfpage_numr/   r(   r)   r,   r+   r*   s           r'   make_page_destinationr3   *   s)    4 "#	 	r&   c                *   j                         D ci c]  \  }}|	|| c}}| j                  |   j                  g}|rt        |t              r|}|j
                  }n|}	 t        |   }|j                  t        d|              t        j                  |      }	|	rD|j                  fd|	D               t        |      S |j                  t        j                         t        |      S c c}}w # t        $ r t        d|       d w xY w)Nz*Invalid or unsupported page location type /c              3  B   K   | ]  }j                  |d         yw)r   N)get).0kkwargss     r'   	<genexpr>z)_make_page_destination.<locals>.<genexpr>h   s     @Avzz!Q'@s   )itemspagesobj
isinstancer   nameKeyError
ValueErrorappendr   PAGE_LOCATION_ARGSr7   extendr   r
   )
r1   r2   r/   r:   r9   vresloc_keyloc_strdest_arg_namess
      `      r'   r0   r0   P   s     &||~?tq!ad?F$'IIh$7$;$;#<Cm\2#GllG#G&w/
 	

4!G9&'+//8JJ@@@ : 	

488:+ @   @	Js   
C3C3'	C9 9Dc                      e Zd ZdZy)OutlineStructureErrorz1Indicates an error in the outline data structure.N)r   r   r   r   r%   r&   r'   rL   rL   n   s    ;r&   rL   c                      e Zd ZdZ	 	 	 	 ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zedddd	       Zddd
Z	y)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-   c               >   || _         || _        || _        i | _        || _        | j                  | j                  t        d      || _        t        ||||	|
      }|j                         D ci c]  \  }}|	|| c}}| _        d| _	        g | _
        yc c}}w )zInitialize OutlineItem.Nz-Only one of destination and action may be setr-   F)titledestinationr/   page_location_kwargsactionrB   r>   dictr<   	is_closedchildren)selfrP   rQ   r/   rS   r>   r(   r)   r,   r+   r*   r:   r9   rF   s                 r'   __init__zOutlineItem.__init__   s     
&*$&!'DKK,CLMM4Sf4P6<lln$VdaQT$V!+- %Ws   1
B<Bc                
   | j                   r| j                  rd}nd}nd}| j                  t        | j                  t              r'| j                  d   }t        |      }|j                  }nt        | j                  t              rd| j                   d}nXt        | 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 -> )rV   rU   rQ   r?   r
   r   labelr   r   intrP   )rW   oc_indicatorraw_pagepagedests        r'   __str__zOutlineItem.__str__   s   ==~~$$ L'$**E2  ++A.H~zzD,,f5 N''(+  D,,d3 N''(+  D,,c2 0 013DqD77r&   c                P    d| j                   j                   d| j                   dS )N	<pikepdf.z: "z">)	__class__r   rP   rW   s    r'   __repr__zOutlineItem.__repr__   s&    4>>2233tzzl"EEr&   Fstrictc                  	 t        |j                        }|j	                  t
        j                        }|,t        |t        t        z  t
        z        st        d|      |j	                  t
        j                        }|t        |t              st        d|       | ||||      S # t        $ r}|rt        d      |d}Y d}~d}~ww xY w)a  Create a ``OutlineItem`` from a ``Dictionary``.

        Does not process nested items.

        Arguments:
            obj: ``Dictionary`` object representing a single outline node.
            strict: If ``True``, raise :class:`OutlineStructureError` on any
                structural problem (such as a missing required ``/Title``).
                If ``False`` (default), quietly correct such problems where the
                repair is known; a missing ``/Title`` becomes an empty string.
        z'Outline node is missing required /Title Nz+Unexpected object type in Outline's /Dest: z(Unexpected object type in Outline's /A: )rQ   rS   r>   )strTitleAttributeErrorrL   r7   r   Destr?   r
   r   Ar   )clsr>   ri   rP   erQ   rS   s          r'   from_dictionary_objectz"OutlineItem.from_dictionary_object   s    			NE ggdii(":$.,
 (=k_M  j&D':6*E  5k&cJJ)  	+= E	s   B/ /	C8CCc                   |s| j                   !|j                  t                     x| _         }n| j                   }| j                  |_        | j
                  rt        | j
                  t              r1t        || j
                  | j                  fi | j                  | _        | j
                  |_        t        j                  |v r|`|S | 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.
        )r>   make_indirectr   rP   rm   rQ   r?   r]   r3   r/   rR   ro   r   rp   rS   )rW   r1   
create_newr>   s       r'   to_dictionary_objectz OutlineItem.to_dictionary_object   s     ) ..z|<<DHs((CJJ	'$**C0#8$$&&$ //	$  ''CHvv}E
 
	 [[$KKCEyyCH
r&   )NNNN)rP   rl   rQ   z"Array | String | Name | int | Noner/   PageLocation | str | NonerS   Dictionary | Noner>   ry   r(   float | Noner)   rz   r,   rz   r+   rz   r*   rz   )r>   r   ri   bool)F)r1   r	   rv   r{   returnr   )
r   r   r   r   rX   rb   rg   classmethodrs   rw   r%   r&   r'   rN   rN   r   s    F ;?37$(!%. " "#!.. 8. 1	.
 ". . . . . . .8 8DF GL "K "KHr&   rN   c                      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ej"                  dd       Zy)Outlinea  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: When ``False`` (default), pikepdf quietly corrects minor
            structural problems in the outline where the correct repair is
            known. For example, a missing required ``/Title`` is treated as an
            empty string, and object references that re-occur while reading or
            writing are recovered without raising. When ``True``, any such
            structural problem raises a :class:`pikepdf.OutlineStructureError`.

    See Also:
        :meth:`pikepdf.Pdf.open_outline`
    c                J    d| _         || _        || _        || _        d| _        y)zInitialize Outline.NF)_root_pdf
_max_depth_strict	_updating)rW   r1   	max_depthri   s       r'   rX   zOutline.__init__.  s%    /3
	#r&   c                ,    t        | j                        S N)rl   rootrf   s    r'   rb   zOutline.__str__6  s    499~r&   c                b    d| j                   j                   dt        | j                         dS )Nrd   z: z items>)re   r   lenr   rf   s    r'   rg   zOutline.__repr__9  s+    4>>2232c$))n5EWMMr&   c                   |r|j                  d       y |j                  ddd      5  t        | j                        D ]/  \  }}|j	                          |j                  t        |             1 	 d d d        y # 1 sw Y   y xY w)Nz...r   z!pikepdf.models.outlines.Outline<
z
>)textgroup	enumerater   	breakableprettyrl   )rW   pcycle_items        r'   _repr_pretty_zOutline._repr_pretty_<  sk    FF5M@%H ((3 (GAtKKMHHSY'(( ( (s   AA::Bc                    d| _         | S )NT)r   rf   s    r'   	__enter__zOutline.__enter__E  s    r&   c                b    	 |		 d| _         y | j                          d| _         y # d| _         w xY w)NF)r   _save)rW   exc_typeexc_valexc_tbs       r'   __exit__zOutline.__exit__I  s3    	## #DN JJL"DNUDNs   % % 	.c                8   d}d }d }|D ]-  }|j                  | j                        }	|	j                  }
|
|v r9| j                  rt	        d|
 d      |j                  | j                  d      }	n|j                  |
       ||	_        |dz  }||	|_        ||	_        n|	}t        j                  |	v r|	`|	}|| j                  k  r|j                  }nd}| j                  |	||dz   |       |j                  r"t        t        |	j                          |	_        |t        t        |	j                         z  }0 |r0||J t        j                  |v r|`||_        ||_        ||_        y t        j"                  |v r|`t        j$                  |v r|`||_        y )Nr   Outline object  reoccurred in structureT)rv   r   r%   )rw   r   objgenr   rL   addParentNextPrevr   r   rV   _save_level_outlinerU   r   r]   CountFirstLast)rW   parentoutline_itemslevelvisited_objscountprevfirstr   out_objr   	sub_itemss               r'   r   zOutline._save_level_outlineQ  s    "&#'! 	2D//		:G^^F%<</)&1IJ  33DII$3O  (#GNQJE#	#99'Dt&37==		$$WiLQ~~!%c7==!9 9c7==11=	2> #(999yyD I FLFK 	 zzV#LyyF"Kr&   c                   |}|rQ|j                   }||v r| j                  rt        d| d      y |j                  |       t        j                  || j                        }|j                  t        j                        }t        |t              rk|| j                  k  r\| j                  ||j                  |dz   |       |j                  t        j                        }	t        |	t              r|	dk  rd|_        |j#                  |       |j                  t        j$                        }
|
t        |
t              r|
}nt        d| d      |rPy y )Nr   r   rh   r   r   Tz points to non-dictionary)r   r   rL   r   rN   rs   r7   r   r   r?   r   r   _load_level_outlinerV   r   r]   rU   rC   r   )rW   	first_objr   r   r   current_objr   r   first_childr   next_objs              r'   r   zOutline._load_level_outline  s0    *3 ''F%<</)&1IJ  V$55k$,,5WD%//$**5K+z2ut7N((	< $

3eS)eai%)DN  &"tyy1H:h
#C&+%fX-FG 1 r&   c                   | j                   y t        j                  | j                  j                  v r!| j                  j                  j                  }nN| j                  j                  t        t        j                              x| j                  j                  _        }| j                  || j                   dt                      y )N)Typer   )	r   r   Outlinesr   Rootru   r   r   set)rW   outliness     r'   r   zOutline._save  s    ::==DIINN*yy~~..H151H1H.2 DIINN#h 	  4::q#%@r&   c                (   g x| _         }t        j                  | j                  j                  vry | j                  j                  j                  xs i }|j                  t        j                        }|r| j                  ||dt                      y y )Nr   )	r   r   r   r   r   r7   r   r   r   )rW   r   r   r   s       r'   _loadzOutline._load  sm    
T==		.99>>**0bLL,	$$Ya? r&   c                    | j                   | j                          t        ||      }| j                   	|g| _         n| j                   j                  |       | j                  s| j                          |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   rN   rC   r   r   )rW   rP   rQ   r   s       r'   r   zOutline.add  s]     ::JJL5+.::DJJJd#~~JJLr&   c                |    | j                   | j                          t        t        t           | j                         S )z$Return the root node of the outline.)r   r   r   listrN   rf   s    r'   r   zOutline.root  s-     ::JJLD%tzz22r&   c                    t        |t              st        d      |D ]  }t        |t              rt        d       || _        y)z!Set the root node of the outline.z+Root must be a list of OutlineItem objects.z)Each item in root must be an OutlineItem.N)r?   r   rB   rN   r   )rW   new_rootr   s      r'   r   zOutline.root  sJ     (D)JKK 	NDdK0 !LMM	N 
r&   N)   F)r1   r	   r   r]   ri   r{   )r   r   r   zIterable[OutlineItem]r   r]   r   set[tuple[int, int]])r   r   r   zlist[Object]r   r]   r   r   )rP   rl   rQ   zArray | int | Noner|   rN   )r|   list[OutlineItem])r   r   )r   r   r   r   rX   rb   rg   r   r   r   r   r   r   r   r   propertyr   setterr%   r&   r'   r   r     s    $N(#44 -4 	4
 +4l"" $" 	"
 +"H	A@* 3 3 
[[ r&   r   r   )r1   r	   r2   r]   r/   rx   r(   rz   r)   rz   r,   rz   r+   rz   r*   rz   r|   r
   )r1   r	   r2   r]   r/   rx   r|   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$   rD   r   from_iterablevaluesALL_PAGE_LOCATION_KWARGSr3   r0   	ExceptionrL   rN   r   r%   r&   r'   <module>r      sk  
 > " $    # C C
4 
 -xy9	  2u223E3L3L3NOP  04#
 #	## -#
 # 
# # # # #R 04	 -
 <<I <f fRI Ir&   