
    HAiT!              	       ,   S r SSKJrJrJrJrJr  SSKJr  SSK	J
r
  SSKJrJrJrJrJr  S\S\4S	 jrS
\S\4S jrS
\S\4S jrS
\S\4S jrS\S\S\4S jrS\S\S\4S jrS\S\S\SS4S jrS\S\S\SS4S jrS\S\S\\\   \\   4   4S jrg)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)
    )IteratorListOptionalTuplecast   )PdfCommonDocProtocol)logger_warning)ArrayObjectDictionaryObject
NullObjectNumberObjectis_null_or_nonenumreturnc                    ^ / SQmS[         S[        [           4U4S jjnSR                  [	        U" U 5      5      5      $ )N))i  M)i  CM)i  D)i  CD)d   C)Z   XC)2   L)(   XL)
   X)	   IX)   V)   IV)r   Ir   r   c              3   h   >#    T H'  u  p[        X5      u  p4X#-  v   XU-  -  n U S::  d  M'    g    g 7f)Nr   )divmod)r   decimal
roman_reprx_romans        N/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pypdf/_page_labels.py	roman_num1number2uppercase_roman_numeral.<locals>.roman_num[   s=     #(G#'DA. Q;Cax $)s   &22 )intr   strjoinlist)r   r0   r.   s     @r/   number2uppercase_roman_numeralr7   J   s;    E s x}  774	#'((    numberc                 4    [        U 5      R                  5       $ N)r7   lowerr9   s    r/   number2lowercase_roman_numeralr>   f   s    )&17799r8   c                    U S::  a  [        S5      e[        [        S5      [        S5      S-   5       Vs/ s H  n[        U5      PM     nnSnU S:  a(  U S-  nUS:X  a  SnX$S-
     U-   nX-  n U S-  n U S:  a  M(  U$ s  snf )Nr   zExpecting a positive numberAZr   r2      )
ValueErrorrangeordchr)r9   ialphabetrep	remainders        r/   number2uppercase_letterrK   j   s    {677 %c#hC1 => =1A =H>
C
1*RK	>I1}%+2 1* J ?s   A>c                 4    [        U 5      R                  5       $ r;   )rK   r<   r=   s    r/   number2lowercase_letterrM   z   s    "6*0022r8   dictionary_objectindexc                    [        [        U S   5      nSnS nSnU[        U5      :  aN  X#   nX#S-      R                  5       nUS-   [        U5      :X  a  O"X#S-      U:  a  OUS-  nU[        U5      :  a  MN  S S [        [
        [        [        S.n[        U[        5      (       d  [        US-   5      $ UR                  SS5      nUR                  S	S
5      nXUR                  S5         " X-
  U-   5      -   $ )N/Numsr   r      c                     g)Nr2    )r-   s    r/   <lambda>%get_label_from_nums.<locals>.<lambda>   s    r8   c                     [        U 5      $ r;   )r4   )ns    r/   rU   rV      s    Ar8   )Nz/Dz/Rz/rz/Az/az/Stz/Pr2   z/S)r   r   len
get_objectr7   r>   rK   rM   
isinstancedictr4   get)	rN   rO   numsrG   valuestart_indexmstartprefixs	            r/   get_label_from_numsrd   ~   s     .w78D	AEK
c$i-gU&&(q5CIA;	Q c$i- ,,%%	A eT""519~IIeQEYYtR Feiio&u':U'BCCCr8   readerc                    [        [        U R                  5      nSU;  a  [        US-   5      $ [        [        US   R	                  5       5      nSU;   a  [        X15      $ SU;   a  [        US   [        5      (       d  SnUS:  a  [        [        [           US   5      nU H{  n[        [        [           US   5      nUS   Us=::  a  US   ::  d  M2  O  M6  [        UR                  SS5      5      (       d  US-  nUS:X  a  [        S	5      eUn  O[        Xa5      s  $    OUS:  a  M  [        S
U S3[        5        [        US-   5      $ )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".

z/PageLabelsr   rQ   z/Kidsr   r   z/LimitsNz"Too deep nesting is not supported.z,Could not reliably determine page label for .)r   r   root_objectr4   rZ   rd   r[   r   r   r3   r   r]   NotImplementedErrorr
   __name__)re   rO   rootnumber_treelevelkidskidlimitss           r/   index2labelrq      sN     &"4"45DD 519~'m)<)G)G)IJK+";66+jW1Ez&R&R ck-.G0DEDd3iY8!9222*3777D+ABB
 C<"5 D#  '* .s:: & + ck. A%JHUuqy>r8   keyr_   r^   Nc                 0   [        U5      S-  S:w  a  [        S5      e[        U5      nUS:w  a#  XUS-
     ::  a  US-
  nUS:w  a  XUS-
     ::  a  M  U[        U5      :  a  XU   :X  a  XUS-   '   gUR                  X05        UR                  US-   U5        g)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

rR   r   6A nums like array must have an even number of elementsr   N)rY   rC   insert)rr   r_   r^   rG   s       r/   nums_insertrv      s      4y1}QRRD	A
q&SQK'E q&SQK' 	3t9}QQUAAE5!r8   page_index_toc                 0   [        U5      S-  S:w  a  [        S5      eX:  a  [        S5      eUR                  U 5      S-   nU[        U5      :  aF  X#   U::  a=  UR                  U5        UR                  U5        U[        U5      :  a  X#   U::  a  M;  gggg)z
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

rR   r   rt   z/page_index_to must be greater or equal than keyN)rY   rC   rO   pop)rr   rw   r^   rG   s       r/   nums_clear_rangerz      s      4y1}QRRJKK

3!A
c$i-DG}4 c$i-DG}4-4-r8   c                     [        U5      S-  S:w  a  [        S5      eUR                  U 5      S-   nU[        U5      :  a  X   XS-      4$ g)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

rR   r   rt   r   )NN)rY   rC   rO   )rr   r^   rG   s      r/   	nums_nextr|     sS     4y1}QRR

3!A3t9}!e%%r8   )__doc__typingr   r   r   r   r   
_protocolsr	   _utilsr
   genericr   r   r   r   r   r3   r4   r7   r>   rK   rM   rd   rq   rv   rz   r|   rT   r8   r/   <module>r      s1  :x 9 8 , " ) ) )8:3 :3 :C C  3C 3C 3!D+; !DC !DC !DH., .S .S .b"	"" " 
	"<	  
	6	
 8L!8,<#==>r8   