a
    ˜ëjî7  ã                   @   sh  d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ erlddlmZ d	Zd
Zeedœdd„Zeedœdd„Zeedœdd„Zeedœdd„Zeeedœdd„Zeeedœdd„Zeeee dœdd„Zeeee dœdd „Z eeee dœd!d"„Z!eeedd#œd$d%„Z"eeedd&œd'd(„Z#eee$ee ee f d)œd*d+„Z%dS ),u£  
Page labels are shown by PDF viewers as "the page number".

A page has a numeric index, starting at 0. Additionally, the page
has a label. In the most simple case:

    label = index + 1

However, the title page and the table of contents might have Roman numerals as
page labels. This makes things more complicated.

Example 1
---------

>>> reader.root_object["/PageLabels"]["/Nums"]
[0, IndirectObject(18, 0, 139929798197504),
 8, IndirectObject(19, 0, 139929798197504)]
>>> reader.get_object(reader.root_object["/PageLabels"]["/Nums"][1])
{'/S': '/r'}
>>> reader.get_object(reader.root_object["/PageLabels"]["/Nums"][3])
{'/S': '/D'}

Example 2
---------
The following is a document with pages labeled
i, ii, iii, iv, 1, 2, 3, A-8, A-9, ...

1 0 obj
    << /Type /Catalog
       /PageLabels << /Nums [
                        0 << /S /r >>
                        4 << /S /D >>
                        7 << /S /D
                             /P ( A- )
                             /St 8
                        >>
                        % A number tree containing
                        % three page label dictionaries
                        ]
                   >>
    ...
    >>
endobj


Â§12.4.2 PDF Specification 1.7 and 2.0
=====================================

Entries in a page label dictionary
----------------------------------
The /S key:
D       Decimal Arabic numerals
R       Uppercase Roman numerals
r       Lowercase Roman numerals
A       Uppercase letters (A to Z for the first 26 pages,
                           AA to ZZ for the next 26, and so on)
a       Lowercase letters (a to z for the first 26 pages,
                           aa to zz for the next 26, and so on)
é    N)ÚTYPE_CHECKINGÚOptionalÚcasté   )ÚPdfCommonDocProtocol)Úlogger_warning)ÚArrayObjectÚDictionaryObjectÚIndirectObjectÚ
NameObjectÚ
NullObjectÚNumberObjectÚis_null_or_none)ÚCallableiŸ  i   )ÚnumÚreturnc                 C   s`   | dkrt dƒ‚| tkr t dƒ‚g d¢}g }|D ]$\}}t| |ƒ\}} | || ¡ q0d |¡S )Nr   úExpecting a positive number.zNumber is out of range.))iè  ÚM)i„  ZCM)iô  ÚD)i�  ZCD)éd   ÚC)éZ   ZXC)é2   ÚL)é(   ZXL)é
   ÚX)é	   ZIX)é   ÚV)é   ZIV)r   ÚIÚ )Ú
ValueErrorÚMAXIMUM_ROMAN_NUMERALÚdivmodÚappendÚjoin)r   ZromanÚresultÚvalueÚsymbolÚcount© r,   úQ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/_page_labels.pyÚnumber2uppercase_roman_numeralZ   s    r.   )Únumberr   c                 C   s   t | ƒ ¡ S ©N)r.   Úlower©r/   r,   r,   r-   Únumber2lowercase_roman_numeralw   s    r3   c                 C   sL   | dkrt dƒ‚t| d dƒ\}}|d }|tkr:t dƒ‚tj| |d  S )Nr   r   r   é   zNumber is too large.)r#   r%   ÚMAXIMUM_PAGE_LABEL_LENGTHÚstringÚascii_uppercase)r/   ZrepetitionsÚpositionÚlengthr,   r,   r-   Únumber2uppercase_letter{   s    r:   c                 C   s   t | ƒ ¡ S r0   )r:   r1   r2   r,   r,   r-   Únumber2lowercase_letter†   s    r;   )Údictionary_objectÚindexr   c              
   C   sš  t t| d ƒ}t|ƒ}d}d }d}||k r²|d |krDtdtd� q²|| }||d   ¡ }|d |krjq²||d  }t|ttfƒsžtdt|d� t	|d ƒS ||kr¨q²|d7 }q"d	d
„ t	t
tttdœ}t|tƒsÞt	|d ƒS | dd¡}	t t	| dd¡ƒ}
| | d¡¡}|d u �r6tdt| d¡d� t	|d ƒS z|
||| |	 ƒ W S  ttf�y” } z*tdt|	|
|d� t	|d ƒW  Y d }~S d }~0 0 d S )Nú/Numsr   r   z(Ignoring last /Nums key without a value.©Úsourceé   z3Ignoring malformed page label key %(key)r in /Nums.)r@   Úkeyc                 S   s   dS )Nr"   r,   )Ú_r,   r,   r-   Ú<lambda>­   ó    z%get_label_from_nums.<locals>.<lambda>)Nz/Dz/Rz/rz/Az/az/Stz/Pr"   z/Sz?Ignoring unknown page label numbering style %(style)r in /Nums.)r@   ÚstylezZIgnoring malformed page label entry in /Nums (/St=%(start)r, /P=%(prefix)r): %(exception)s)r@   ÚstartÚprefixÚ	exception)r   r   Úlenr   Ú__name__Ú
get_objectÚ
isinstanceÚintÚfloatÚstrr.   r3   r:   r;   ÚdictÚgetÚ	TypeErrorr#   )r<   r=   ÚnumsÚnums_lengthÚir)   Ústart_indexZnext_keyÚmrG   rH   Zmapping_functionrI   r,   r,   r-   Úget_label_from_numsŠ   sp    ÿý
ú	

ýûrY   )Úreaderr=   r   c                 C   sš  t t| jƒ}d|vr t|d ƒS |d  ¡ }t|tƒsPtdt|d� t|d ƒS d|v rbt||ƒS d|v �r€t|d t	ƒ�s€d}|dk �r€|d  ¡ }t|t
ƒs²td	t|d
� �q€|D ]Â}| ¡ }t|tƒsÚtdtd� q¶| dt	ƒ ¡ ¡ }t|tƒ�rt|ƒdk �rtdtd� q¶|d |  k�r2|d kr¶n q¶t| dd¡ƒ�sj|d7 }|dk�rbtdƒ‚|} q€t||ƒ  S q¶�q€q€tdt|d� t|d ƒS )z±
    See 7.9.7 "Number Trees".

    Args:
        reader: The PdfReader
        index: The index of the page

    Returns:
        The label of the page, e.g. "iv" or "4".

    ú/PageLabelsr   z1Page labels are not a dictionary: %(number_tree)s)r@   Únumber_treer>   z/Kidsr   r   z*Page label kids are not an array: %(kids)s)r@   Úkidsz6Ignoring kid which is not a dictionary in /PageLabels.r?   z/LimitsrA   z>Ignoring kid with missing or malformed /Limits in /PageLabels.Nz"Too deep nesting is not supported.z6Could not reliably determine page label for %(index)d.)r@   r=   )r   r	   Úroot_objectrP   rL   rM   r   rK   rY   r   r   rR   ÚlistrJ   r   ÚNotImplementedError)rZ   r=   Úrootr\   Úlevelr]   ZkidZlimitsr,   r,   r-   Úindex2labelÐ   sh    
ý


ý
þþ"
ÿrc   )rT   Ú
page_countr   c                 C   sŒ   t | ƒ}d}td|dƒD ]j}| | }|d |ks\t|tƒr\t| |d  tƒs\|durb||krb dS ||kr~|dkr~|d   S |}q|pŠdS )aÔ  
    Get how much of a flat ``/Nums`` array labels the first ``page_count`` pages.

    ``get_label_from_nums`` always reads the first pair, then stops at the first
    key above the page index, so only the array up to the first key at or above
    ``page_count`` after the first pair matters. Returns ``None`` unless that
    part consists of pairs of increasing integer keys and direct values, which
    is what lets ``index2labels`` read it once for all pages.
    Nr   rA   r   )rJ   ÚrangerM   r   r
   )rT   rd   rU   ÚpreviousrV   rB   r,   r,   r-   Ú_get_single_pass_nums_length  s&    

ÿþýüürg   )Úpdfrd   r   c                    s   ‡ fdd„t |ƒD ƒS )Nc                    s   g | ]}t ˆ |ƒ‘qS r,   )rc   )Ú.0r=   ©rh   r,   r-   Ú
<listcomp>9  rE   z._index2labels_page_by_page.<locals>.<listcomp>)re   )rh   rd   r,   rj   r-   Ú_index2labels_page_by_page8  s    rl   c           
      C   sú   |dkrg S t t| jƒ}d|vr*t| |ƒS |d  ¡ }t|tƒrHd|vrRt| |ƒS |d }t|tƒsnt| |ƒS t||ƒ}|du rŠt| |ƒS g }d}t|ƒD ]Z}|d |k rÄ||d  |krÄ|d7 }qžtt	dƒt|||d … ƒiƒ}	| 
t|	|ƒ¡ qš|S )a,  
    Labels of the first ``page_count`` pages, as ``index2label`` would return them.

    A flat ``/Nums`` array with integer keys in increasing order and direct
    values is walked once for all pages instead of from the start for every
    page. Any other number tree is resolved page by page.
    r   r[   r>   NrA   )r   r	   r^   rl   rL   rM   r   rg   re   r   r&   rY   )
rh   rd   ra   r\   rT   rU   Úlabelsr8   r=   Úentryr,   r,   r-   Úindex2labels<  s,    






 ro   )rB   r)   rT   r   c                 C   sˆ   t |ƒd dkrtdƒ‚t |ƒ}|dkrB| ||d  krB|d }q |t |ƒk rh| || krh|||d < n| || ¡ | |d |¡ dS )zÈ
    Insert a key, value pair in a Nums array.

    See 7.9.7 "Number Trees".

    Args:
        key: number key of the entry
        value: value of the entry
        nums: Nums array to modify

    rA   r   ú6A nums like array must have an even number of elementsr   N)rJ   r#   Úinsert)rB   r)   rT   rV   r,   r,   r-   Únums_insert]  s    
rr   )rB   Úpage_index_torT   r   c                 C   sh   t |ƒd dkrtdƒ‚|| k r(tdƒ‚| | ¡d }|t |ƒk rd|| |krd| |¡ | |¡ q6dS )a  
    Remove all entries in a number tree in a range after an entry.

    See 7.9.7 "Number Trees".

    Args:
        key: number key of the entry before the range
        page_index_to: The page index of the upper limit of the range
        nums: Nums array to modify

    rA   r   rp   z/page_index_to must be greater or equal than keyN)rJ   r#   r=   Úpop)rB   rs   rT   rV   r,   r,   r-   Únums_clear_range{  s    
ru   )rB   rT   r   c                 C   sJ   t |ƒd dkrtdƒ‚| | ¡d }|t |ƒk rF|| ||d  fS dS )z±
    Return the (key, value) pair of the entry after the given one.

    See 7.9.7 "Number Trees".

    Args:
        key: number key of the entry
        nums: Nums array

    rA   r   rp   r   )NN)rJ   r#   r=   )rB   rT   rV   r,   r,   r-   Ú	nums_next–  s    rv   )&Ú__doc__r6   Útypingr   r   r   Z
_protocolsr   Ú_utilsr   Zgenericr   r	   r
   r   r   r   r   Úcollections.abcr   r$   r5   rN   rP   r.   r3   r:   r;   rY   rc   rg   r_   rl   ro   rr   ru   Útuplerv   r,   r,   r,   r-   Ú<module>   s@   ;$
FM"üüý