U
    :vh                     @   sv   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 G dd deZ
G dd	 d	eZG d
d deZG dd dZdS )u  
Quoting the PDF spec:
> PDF’s logical _structure facilities_ provide a mechanism for incorporating
> structural information about a document’s content into a PDF file.

> The logical structure of a document is described by a hierarchy of objects called
> the _structure hierarchy_ or _structure tree_.
> At the root of the hierarchy is a dictionary object called the _structure tree root_,
> located by means of the **StructTreeRoot** entry in the document catalog.

The contents of this module are internal to fpdf2, and not part of the public API.
They may change at any time without prior warning or any deprecation period,
in non-backward-compatible ways.
    )defaultdict)ListUnion   )	PDFObject	PDFStringPDFArrayc                       s2   e Zd ZdZdZ fddZd fdd	Z  ZS )	
NumberTreeu  A number tree is similar to a name tree, except that its keys are integers
    instead of strings and are sorted in ascending numerical order.

    A name tree serves a similar purpose to a dictionary—associating keys and
    values—but by different means.

    The values associated with the keys may be objects of any type. Stream objects
    are required to be specified by indirect object references. It is recommended,
    though not required, that dictionary, array, and string objects be specified by
    indirect object references, and other PDF objects (nulls, numbers, booleans,
    and names) be specified as direct objects
    )_idnumsc                    s   t    tt| _d S N)super__init__r   listr   self	__class__ 7/tmp/pip-unpacked-wheel-dvf6lv8i/fpdf/structure_tree.pyr   &   s    
zNumberTree.__init__Nc                    s:   d d  fdd| j D }t dd| diS )N
c                 3   s2   | ]*\}}| d   dd |D  dV  qdS )z [c                 s   s   | ]}|j V  qd S r   )ref.0struct_elemr   r   r   	<genexpr>-   s     z1NumberTree.serialize.<locals>.<genexpr>.<genexpr>]N)join)r   Zstruct_parent_idZstruct_elemsnewliner   r   r   ,   s   z'NumberTree.serialize.<locals>.<genexpr>z/Nums[r   )r   r   itemsr   	serialize)r   Zobj_dictZ_security_handlerZserialized_numsr   r   r   r"   *   s
    zNumberTree.serialize)NN)__name__
__module____qualname____doc__	__slots__r   r"   __classcell__r   r   r   r   r	      s   r	   c                       s    e Zd ZdZ fddZ  ZS )StructTreeRoot)r
   typeparent_treekc                    s$   t    d| _t | _t | _d S )Nz/StructTreeRoot)r   r   r*   r	   r+   r   r,   r   r   r   r   r   6   s    
zStructTreeRoot.__init__)r#   r$   r%   r'   r   r(   r   r   r   r   r)   3   s   r)   c                       sJ   e Zd ZdZdeeeee ed  f eeed fddZ	dd Z
  ZS )	
StructElem)	r
   r*   spr,   taltpg_page_numberNstruct_typeparentkidspage_numbertitler1   c                    sb   t    d| _|| _|| _t|| _|d kr2d nt|| _|d krHd nt|| _	d | _
|| _d S )Nz/StructElem)r   r   r*   r.   r/   r   r,   r   r0   r1   r2   r3   )r   r5   r6   r7   r8   r9   r1   r   r   r   r   L   s    	

zStructElem.__init__c                 C   s   | j S r   )r3   r   r   r   r   r8   b   s    zStructElem.page_number)NNN)r#   r$   r%   r'   strr   r   r   intr   r8   r(   r   r   r   r   r-   ?   s      r-   c                   @   sD   e Zd Zdd ZdeeeeedddZdd Zd	d
 Zdd Z	dS )StructureTreeBuilderc                 C   s4   t  | _td| jg d| _| jj| j i | _d S )Nz	/Document)r5   r6   r7   )r)   struct_tree_rootr-   doc_struct_elemr,   appendspid_per_page_numberr   r   r   r   r   g   s      zStructureTreeBuilder.__init__N)r8   r5   mcidr9   alt_textc                 C   sv   | j |}|d kr(t| j }|| j |< t|| j|d kr<g n|g|||d}| jj| | jjj	| | ||fS )Nr4   )
r@   getlenr-   r>   r,   r?   r=   r+   r   )r   r8   r5   rA   r9   rB   Zstruct_parents_idr   r   r   r   add_marked_contento   s    

z'StructureTreeBuilder.add_marked_contentc                    s   t  fdd| jjD S )Nc                 3   s$   | ]}|   kr|jrd V  qdS )r   N)r8   r,   r   r8   r   r   r      s   z:StructureTreeBuilder.next_mcid_for_page.<locals>.<genexpr>)sumr>   r,   )r   r8   r   rF   r   next_mcid_for_page   s    z'StructureTreeBuilder.next_mcid_for_pagec                 C   s
   | j j S r   )r>   r,   r   r   r   r   empty   s    zStructureTreeBuilder.emptyc                 c   s,   | j V  | jV  | j jV  | jjE dH  dS )z@Iterate all PDF objects in the tree, starting with the tree rootN)r=   r>   r+   r,   r   r   r   r   __iter__   s    
zStructureTreeBuilder.__iter__)NNN)
r#   r$   r%   r   r;   r:   rE   rH   rI   rJ   r   r   r   r   r<   f   s      r<   N)r&   collectionsr   typingr   r   Zsyntaxr   r   r   r	   r)   r-   r<   r   r   r   r   <module>   s   '