U
    :vh*                     @   s   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ ertdd	lmZ eG d
d dZG dd deZG dd deZdd ZG dd dZdS )u  
Quoting section 8.2.2 "Document Outline" of the 2006 PDF spec 1.7:
> The document outline consists of a tree-structured hierarchy of outline items (sometimes called bookmarks),
> which serve as a visual table of contents to display the document’s structure to the user.

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.
    )	dataclass)ListOptionalTYPE_CHECKING   )AlignXPosYPos)	TextStyle)Destination	PDFObject	PDFString)
StructElem)FPDFc                   @   sH   e Zd ZU dZeed< eed< eed< eed< ee	 ed< d
dd	Z
dS )OutlineSectionnamelevelpage_numberdeststruct_elemr   r   r   r   r   Nc                 C   s"   || _ || _|| _|| _|| _d S Nr   )selfr   r   r   r   r    r   0/tmp/pip-unpacked-wheel-dvf6lv8i/fpdf/outline.py__init__"   s
    zOutlineSection.__init__)N)__name__
__module____qualname__	__slots__str__annotations__intr   r   r   r   r   r   r   r   r      s   
r   c                       s4   e Zd ZdZdeeed fddZdd Z  Z	S )	OutlineItemDictionary)
_idtitleparentprevnextfirstlastcountr   r   Nr%   r   r   c                    sL   t    t|dd| _d | _d | _d | _d | _d | _d| _	|| _
|| _d S )NT)encryptr   )superr   r   r%   r&   r'   r(   r)   r*   r+   r   r   )r   r%   r   r   	__class__r   r   r   8   s    
zOutlineItemDictionary.__init__c                 C   s   d| j  d| j dS )NzOutlineItemDictionary(title=z, dest=))r%   r   r   r   r   r   __str__I   s    zOutlineItemDictionary.__str__)NN)
r   r   r   r   r    r   r   r   r3   __classcell__r   r   r/   r   r#   *   s     r#   c                       s(   e Zd ZdZ fddZdd Z  ZS )OutlineDictionary)r$   typer)   r*   r+   c                    s*   t  jf | d| _d | _d | _d| _d S )Nz	/Outlinesr   )r.   r   r6   r)   r*   r+   )r   kwargsr/   r   r   r   P   s
    zOutlineDictionary.__init__c                 C   s   d| j  dS )NzOutlineDictionary(count=r1   )r+   r2   r   r   r   r3   W   s    zOutlineDictionary.__str__)r   r   r   r   r   r3   r4   r   r   r/   r   r5   M   s   r5   c                 #   s   t  }|V  g }i }| D ] t j j jd}|V   j|krV| j }||_||_ jd |krt| jd  }n|}||_|j	dkr||_	||_
| jd7  _|| || j<  fdd| D }q|g| S )z
    Build PDF objects constitutive of the documents outline,
    and yield them one by one, starting with the outline dictionary
    r,   r   Nc                    s    i | ]\}}| j kr||qS r   )r   ).0r   Zoitemsectionr   r   
<dictcomp>z   s   
 z&build_outline_objs.<locals>.<dictcomp>)r5   r#   r   r   r   r   r(   r'   r&   r)   r*   r+   appenditems)sectionsoutlineZoutline_itemsZlast_outline_item_per_levelZoutline_itemZlast_outline_item_at_levelZparent_outline_itemr   r9   r   build_outline_objs[   s:    





r@   c                   @   sX   e Zd ZdZdee ddd	Zd
edddZd
edddZ	d
e
e dddZdS )TableOfContentsa  
    A reference implementation of a Table of Contents (ToC) for use with `fpdf2`.

    This class provides a customizable Table of Contents that can be used directly or subclassed
    for additional functionality. To use this class, create an instance of `TableOfContents`,
    configure it as needed, and pass its `render_toc` method as the `render_toc_function` argument
    to `FPDF.insert_toc_placeholder()`.
    NF      @      ?T)
text_stylec                 C   s(   |pt  | _|| _|| _|| _|| _d S r   )r
   rD   use_section_title_styleslevel_indentline_spacingignore_pages_before_toc)r   rD   rE   rF   rG   rH   r   r   r   r      s
    zTableOfContents.__init__r   )pdfitemc                 C   sH   | j r|j|j r|j|j S t| jjttfrBtd| jj | jS )Nz2Unsupported l_margin value provided as TextStyle: )	rE   Zsection_title_stylesr   
isinstancerD   l_marginr    r   
ValueError)r   rI   rJ   r   r   r   get_text_style   s    zTableOfContents.get_text_stylec           
   
   C   s  |j |jd}|j|j  }|| ||P |j| j }||j	|  |j
|j| |jtjtj|tj|j| j d | }||}|j| | |j }|dk r|  | }|j| | |j }d}	|dkrB||	d |k r|	d7 }	qt|	dkrB|j
|j| |j |	d d tjtj|tj|j| j d || |j
|j| |j |tjtj|tj|j| j d W 5 Q R X d S )	N)page)wtextZnew_xZnew_ylinkZalignhr    z  .r   )Zadd_linkr   ZpagesZ	get_labelZuse_text_stylerN   r   rF   Zset_xrL   Z
multi_cellZepwr   r   ZENDr	   ZLASTr   JZ	font_sizerG   Zget_xZget_string_widthrP   Zr_marginlnlenLZLMARGINZNEXTR)
r   rI   rJ   rR   Z
page_labelindentZ	current_xZpage_label_lengthZin_between_spaceZ
in_betweenr   r   r   render_toc_item   sZ    







zTableOfContents.render_toc_item)rI   r?   c                 C   s0   |D ]&}| j r|j|jjkrq| || qdS )zUThis method can be overridden by subclasses to customize the Table of Contents style.N)rH   r   Ztoc_placeholderZ
start_pager]   )r   rI   r?   r:   r   r   r   
render_toc   s    zTableOfContents.render_toc)NFrB   rC   T)r   r   r   __doc__r   r
   r   r   rN   r]   r   r^   r   r   r   r   rA      s        	7rA   N)r_   dataclassesr   typingr   r   r   enumsr   r   r	   Zfontsr
   Zsyntaxr   r   r   Zstructure_treer   Zfpdfr   r   r#   r5   r@   rA   r   r   r   r   <module>   s   
#'