
    ib!              	       ,   S r SSKJr  SSK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)Optional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        L/home/james-whalen/.local/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   r.   r,   s     @r-   number2uppercase_roman_numeralr5   K   s;    E s x}  774	#'((    numberc                 4    [        U 5      R                  5       $ N)r5   lowerr7   s    r-   number2lowercase_roman_numeralr<   g   s    )&17799r6   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   r0      )
ValueErrorrangeordchr)r7   ialphabetrep	remainders        r-   number2uppercase_letterrI   k   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       $ r9   )rI   r:   r;   s    r-   number2lowercase_letterrK   {   s    "6*0022r6   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)Nr0    )r+   s    r-   <lambda>%get_label_from_nums.<locals>.<lambda>   s    r6   c                     [        U 5      $ r9   )r2   )ns    r-   rS   rT      s    Ar6   )Nz/Dz/Rz/rz/Az/az/Stz/Pr0   z/S)r   r	   len
get_objectr5   r<   rI   rK   
isinstancedictr2   get)	rL   rM   numsrE   valuestart_indexmstartprefixs	            r-   get_label_from_numsrb      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Cr6   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   rO   z/Kidsr   r   z/LimitsNz"Too deep nesting is not supported.z,Could not reliably determine page label for .)r   r
   root_objectr2   rX   rb   rY   r   r4   r1   r   r[   NotImplementedErrorr   __name__)rc   rM   rootnumber_treelevelkidskidlimitss           r-   index2labelro      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>r6   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

rP   r   6A nums like array must have an even number of elementsr   N)rW   rA   insert)rp   r]   r\   rE   s       r-   nums_insertrt      s      4y1}QRRD	A
q&SQK'E q&SQK' 	3t9}QQUAAE5!r6   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

rP   r   rr   z/page_index_to must be greater or equal than keyN)rW   rA   rM   pop)rp   ru   r\   rE   s       r-   nums_clear_rangerx      s      4y1}QRRJKK

3!A
c$i-DG}4 c$i-DG}4-4-r6   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

rP   r   rr   r   )NN)rW   rA   rM   )rp   r\   rE   s      r-   	nums_nextrz     sS     4y1}QRR

3!A3t9}!e%%r6   )__doc__collections.abcr   typingr   r   
_protocolsr   _utilsr   genericr	   r
   r   r   r   r1   r2   r5   r<   rI   rK   rb   ro   rt   rx   tuplerz   rR   r6   r-   <module>r      s1  :x % ! , " ) ) )8:3 :3 :C C  3C 3C 3!D+; !DC !DC !DH., .S .S .b"	"" " 
	"<	  
	6	
 8L!8,<#==>r6   