o
    gi                     @   sp   d Z ddlZddl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 G d	d
 d
eZdS )z
    The cross-reference table contains information that permits random access to indirect objects within the file so
    that the entire file need not be read to locate any particular object.
    N)HighLevelTokenizer)	TokenType)
Dictionary)Name)	Reference)XREFc                	       s   e Zd ZdZ fddZdejejej	ej
f dedeje fddZdejejej	ej
f dedefd	d
Z	ddejejej	ej
f dedeje ddfddZ  ZS )PlainTextXREFa3  
    The cross-reference table contains information that permits random access to indirect objects within the file so
    that the entire file need not be read to locate any particular object. The table shall contain a one-line entry for
    each indirect object, specifying the byte offset of that object within the body of the file. (Beginning with PDF 1.5,
    some or all of the cross-reference information may alternatively be contained in cross-reference streams; see
    7.5.8, "Cross-Reference Streams.")

    NOTE 1
    The cross-reference table is the only part of a PDF file with a fixed format, which permits entries in the table to
    be accessed randomly.
    The table comprises one or more cross-reference sections. Initially, the entire table consists of a single section
    (or two sections if the file is linearized; see Annex F). One additional section shall be added each time the file is
    incrementally updated (see 7.5.6, "Incremental Updates").

    Each cross-reference section shall begin with a line containing the keyword xref. Following this line shall be
    one or more cross-reference subsections, which may appear in any order. For a file that has never been
    incrementally updated, the cross-reference section shall contain only one subsection, whose object numbering
    begins at 0.
    c                    s   t    d S N)super__init__)self	__class__ P/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/xref/plaintext_xref.pyr   ,   s   zPlainTextXREF.__init__srctokreturnc              	      s   fddt ddD }|d d usJ |d d usJ |d  dv r/||d   g S |d  tjks:J |d  tjksEJ t|d  }t|d  }g }t d|D ]z} fddt ddD }|d d usrJ |d  dvs|J |d  tjksJ |d d usJ |d  tjksJ |d d usJ |d  tjksJ |d  d	v sJ |	t
|| t|d  t|d  |d  d
kd q\|S )Nc                       g | ]}   qS r   next_non_comment_token.0_r   r   r   
<listcomp>8       z/PlainTextXREF._read_section.<locals>.<listcomp>r         )trailer	startxrefc                    r   r   r   r   r   r   r   r   G   r      )fnr#   )object_numberbyte_offsetgeneration_number	is_in_use)rangeget_textseekget_byte_offsetget_token_typer   NUMBERintOTHERappendr   )r   r   r   tokensstart_object_numbernumber_of_objectsindirect_referencesir   r   r   _read_section3   s<   
zPlainTextXREF._read_sectionc                 C   s   |  }|d us
J | dkrt S |  }|d usJ | tjks&J |dtj |	 }|  }|d us;J | tj
ksDJ | dksLJ |S )Nr   r    )r   r)   r   r,   r   
START_DICTr*   ioSEEK_CURread_dictionaryr/   )r   r   r   tokentrailer_dictr   r   r   _read_trailer_   s   zPlainTextXREF._read_trailerNinitial_offsetr   c                 C   s   |dur
| | n| || | }|dusJ | dks"J 	 | ||}t|dkr0n|D ]}| | q2q#| ||| td< | td 	|  | S )z
        This method attempts to read a plaintext XREF from the given io_source.
        It will either throw an exception, or return this XREF
        NxrefTr   Trailer)
r*   _seek_to_xref_tokenr   r)   r6   lenaddr>   r   
set_parent)r   r   r   r?   r<   xref_sectionrr   r   r   read   s    	zPlainTextXREF.readr	   )__name__
__module____qualname____doc__r   typingUnionr9   BufferedIOBase	RawIOBaseBytesIOr   Listr   r6   r   r>   Optionalr.   rH   __classcell__r   r   r   r   r      s6    
,
'r   )rL   r9   rM   *borb.io.read.tokenize.high_level_tokenizerr   )borb.io.read.tokenize.low_level_tokenizerr   borb.io.read.typesr   r   r   borb.pdf.xref.xrefr   r   r   r   r   r   <module>   s   