o
    e:                     @  s  d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZ G d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e Z	d'ddddddd(ddZ	d'd)dd Z G d!d" d"e!Z"G d#d$ d$Z#G d%d& d&Z$dS )*z7Support for document outlines (e.g. table of contents).    )annotations)Enum)chain)IterableListcast)Array
DictionaryNameObjectPagePdfStringc                   @  s0   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FitRZFitBFitBHFitBV r#   r#   GD:\Projects\ConvertPro\env\Lib\site-packages\pikepdf/models/outlines.pyr      s    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             
   C  s   t | |||||||dS )az  
    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*.
    )r.   r%   r&   r)   r(   r'   )_make_page_destination)r+   r,   r.   r%   r&   r)   r(   r'   r#   r#   r$   make_page_destination(   s   r3   c                   s   dd    D  | j| jg}|rVt|tr|}|j}n|}zt| }W n ty4   td| d w |t	d|  t
|}|rR| fdd|D  t|S |t	j t|S )Nc                 S     i | ]\}}|d ur||qS Nr#   .0kvr#   r#   r$   
<dictcomp>U       z*_make_page_destination.<locals>.<dictcomp>z*Invalid or unsupported page location type /c                 3  s    | ]	}  |d V  qdS )r   N)get)r7   r8   kwargsr#   r$   	<genexpr>g   s    z)_make_page_destination.<locals>.<genexpr>)itemsZpagesobj
isinstancer   nameKeyError
ValueErrorappendr
   PAGE_LOCATION_ARGSr=   extendr   r   )r+   r,   r.   r?   resZloc_keyZloc_strZdest_arg_namesr#   r>   r$   r2   O   s.   

r2   c                   @  s   e Zd ZdZdS )OutlineStructureErrorz1Indicates an error in the outline data structure.N)r   r   r   r   r#   r#   r#   r$   rK   m   s    rK   c                   @  s\   e Zd ZdZ				d$ddddddd%ddZdd Zdd Zed&ddZd'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 | Noner.   r/   actionDictionary | NonerB   r%   r0   r&   r)   r(   r'   c                C  sv   || _ || _|| _i | _|| _| jd ur| jd urtd|| _t||||	|
d}dd | D | _d| _	g | _
d S )Nz-Only one of destination and action may be setr*   c                 S  r4   r5   r#   r6   r#   r#   r$   r:      r;   z(OutlineItem.__init__.<locals>.<dictcomp>F)rM   rO   r.   page_location_kwargsrQ   rF   rB   dictrA   	is_closedchildren)selfrM   rO   r.   rQ   rB   r%   r&   r)   r(   r'   r?   r#   r#   r$   __init__   s   
zOutlineItem.__init__c                 C  s   | j r| jr	d}nd}nd}| jd urPt| jtr&| jd }t|}|j}n,t| jtr4d| j d}nt| jtrBd| j d}nt| jt	rOd| 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   rO   rC   r   r   labelr   r
   r-   rM   )rW   Zoc_indicatorZraw_pagepagedestr#   r#   r$   __str__   s&   

zOutlineItem.__str__c                 C  s   d| j j d| j dS )N	<pikepdf.z: "z">)	__class__r   rM   rW   r#   r#   r$   __repr__   s   zOutlineItem.__repr__r	   c                 C  sx   t |j}|tj}|durt|tttfstd||tj	}|dur4t|t
s4td|| ||||dS )zCreates 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: )rO   rQ   rB   )rN   Titler=   r
   DestrC   r   r   rK   Ar	   )clsrB   rM   rO   rQ   r#   r#   r$   from_dictionary_object   s   
	

z"OutlineItem.from_dictionary_objectFr+   r   
create_newboolr1   c                 C  s   |s| j du r|t  | _ }n| j }| j|_| jdur>t| jtr1t|| j| j	fi | j
| _| j|_tj|v r<|`|S | jdurN| j|_tj|v rN|`|S )a^  Creates/updates 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.
        N)rB   make_indirectr	   rM   rc   rO   rC   r-   r3   r.   rS   rd   r
   re   rQ   )rW   r+   rh   rB   r#   r#   r$   to_dictionary_object   s,   



z OutlineItem.to_dictionary_object)NNNN)rM   rN   rO   rP   r.   r/   rQ   rR   rB   rR   r%   r0   r&   r0   r)   r0   r(   r0   r'   r0   )rB   r	   )F)r+   r   rh   ri   r1   r	   )
r   r   r   r   rX   r^   rb   classmethodrg   rk   r#   r#   r#   r$   rL   q   s$    #rL   c                   @  sn   e Zd ZdZd*d+d
dZdd Zdd Zdd Zdd Zd,ddZ	d-dd Z
d!d" Zd#d$ Zed.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_depthr-   strictri   c                 C  s"   d | _ || _|| _|| _d| _d S NF)_root_pdf
_max_depth_strict	_updating)rW   r+   ro   rp   r#   r#   r$   rX     s
   
zOutline.__init__c                 C  s
   t | jS r5   )rN   rootra   r#   r#   r$   r^      s   
zOutline.__str__c                 C  s   d| j j dt| j dS )Nr_   z: z items>)r`   r   lenrw   ra   r#   r#   r$   rb   #  s   zOutline.__repr__c                 C  s
   d| _ | S )NT)rv   ra   r#   r#   r$   	__enter__&  s   zOutline.__enter__c                 C  s2   z|d urW d| _ d S |   W d| _ d S d| _ w rq   )rv   _save)rW   exc_typeexc_valexc_tbr#   r#   r$   __exit__*  s   

zOutline.__exit__parentr	   outline_itemsIterable[OutlineItem]levelvisited_objsset[tuple[int, int]]c                 C  sH  d}d }d }|D ]n}| | j}	|	j}
|
|v r+| jr"td|
 d|j | jdd}	n||
 ||	_|d7 }|d urB|	|_||	_n	|	}t	j|	v rK|	`|	}|| j
k rV|j}nd}| |	||d | |jrntt|	j |	_q|tt|	j7 }q|r|d ur|d usJ t	j|v r|`||_||_nt	j|v r|`t	j|v r|`||_d S )Nr   Outline object  reoccurred in structureT)rh   r   r#   )rk   rs   objgenru   rK   addZParentNextZPrevr
   rt   rV   _save_level_outlinerU   r   r-   CountFirstZLast)rW   r   r   r   r   countprevfirstitemZout_objr   Z	sub_itemsr#   r#   r$   r   2  sP   







zOutline._save_level_outline	first_objlist[Object]c                 C  s   |}|rr|j }||v r| jrtd| dd S || t|}|tj}t	|t
rO|| jk rO| ||j|d | |tj}	t	|	trO|	dk rOd|_|| |tj}
|
d u sct	|
t
rf|
}ntd| d|sd S d S )Nr   r   r   r   Tz points to non-dictionary)r   ru   rK   r   rL   rg   r=   r
   r   rC   r	   rt   _load_level_outlinerV   r   r-   rU   rG   r   )rW   r   r   r   r   Zcurrent_objr   r   Zfirst_childr   Znext_objr#   r#   r$   r   h  s6   




zOutline._load_level_outlinec                 C  s^   | j d u rd S tj| jjv r| jjj}n| jttjd | jj_}| || j dt  d S )N)Typer   )	rr   r
   Outlinesrs   Rootrj   r	   r   set)rW   outlinesr#   r#   r$   rz     s   

zOutline._savec                 C  sT   g  | _ }tj| jjvrd S | jjjpi }|tj}|r(| ||dt  d S d S )Nr   )	rr   r
   r   rs   r   r=   r   r   r   )rW   rw   r   r   r#   r#   r$   _load  s   
zOutline._loadr1   list[OutlineItem]c                 C  s"   | j du r	|   ttt | j S )z$Return the root node of the outline.N)rr   r   r   r   rL   ra   r#   r#   r$   rw     s   
zOutline.rootN)rn   F)r+   r   ro   r-   rp   ri   )r   r	   r   r   r   r-   r   r   )r   r	   r   r   r   r-   r   r   )r1   r   )r   r   r   r   rX   r^   rb   ry   r~   r   r   rz   r   propertyrw   r#   r#   r#   r$   rm     s    

6$	rm   r5   )r+   r   r,   r-   r.   r/   r%   r0   r&   r0   r)   r0   r(   r0   r'   r0   r1   r   )r+   r   r,   r-   r.   r/   r1   r   )%r   
__future__r   enumr   	itertoolsr   typingr   r   r   Zpikepdfr   r	   r
   r   r   r   r   r   r   r   r   r    r!   r"   rH   r   from_iterablevaluesZALL_PAGE_LOCATION_KWARGSr3   r2   	ExceptionrK   rL   rm   r#   r#   r#   r$   <module>   s:   $* 