o
    el,                     @  s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZ eeZdZdd	 Zd
d Zdd ZG dd dZdS )z9For grafting text-only PDF pages onto freeform PDF pages.    )annotationsN)suppress)Path)

DictionaryNameObjectOperatorPdfPdfError	PdfMatrixStreamparse_content_streamunparse_content_streamd   c                 C  s   || vr
t i | |< | | S N)r   )objname r   ?D:\Projects\ConvertPro\env\Lib\site-packages\ocrmypdf/_graft.py_ensure_dictionary   s   r   c                 C  sD   t | tj}t |tj}|dur||vr|||< |r ||d< dS dS )zlUpdate this obj's fonts with a reference to the Glyphless font.

    obj can be a page or Form XObject.
    Nz/ProcSet)r   r   	ResourcesZFont)r   fontfont_keyprocset	resourcesZfontsr   r   r   _update_resources#   s   r   c           	      C  s   g }d}d}g }t |dD ]F\}}|s-|tdkr%d}d}|||f q|||f q|tdkr7|d }|||f |tdkrSd}|dkrO|| |  qt|}t| ||_d S )	NFr    ZBTTZTrET   )r   r   appendextendclearr   r   Contents)	ZpdfpagestreamZin_text_objZrender_modeZtext_objectsZoperandsoperatorZcontent_streamr   r   r   strip_invisible_text4   s,   
r&   c                   @  sD   e Zd ZdZdd Zdd
dZdd Zdd Zdd ZdddZ	dS ) 
OcrGrafterz2Manages grafting text-only PDFs onto regular PDFs.c                 C  s`   || _ |j| _t| j| _d\| _| _|j| _|	d| _
| jtd| _d| _d| _d S )NNNzgraft_layers.pdfs&   [ /PDF /Text /ImageB /ImageC /ImageI ]   r   )contextorigin	path_baser	   openpdf_baser   r   pdfinfoget_pathoutput_fileZmake_indirectr   parser   emplacementsinterim_count)selfr*   r   r   r   __init__S   s   
zOcrGrafter.__init__pagenointimagePath | Nonetextpdfautorotate_correctionc             
   C  s  |r| j s| |\| _ | _d}| j| j}|rt| nd }|d url|| jkrlt	d t
|/}|  jd7  _|jd }	| jj|	 | jjd }
| jj| |
 | jjd= W d    n1 sew   Y  d}|rp|}|}|| d }t	d| d	| d	| d
|  |r| j r| jjj}| j|d || j | j|| j|d || d }|| jj| _t	d| d	| d
|  | jt dkr|   d S d S )NFzEmplacement updater)   r   Th  zCText rotation: (text, autorotate, content) -> text misalignment = (z, z) -> )page_numr;   r   r   text_rotationr   strip_old_textz*Page rotation: (content, auto) -> page = ()r   
_find_fontr   r/   Zrotationr   resolver,   logdebugr	   r-   r3   pagesr.   r   Zemplacer*   optionsZredo_ocr_graft_text_layerr   ZRotateMAX_REPLACE_PAGESsave_and_reload)r5   r7   r9   r;   r<   Zemplaced_pageZcontent_rotationZ
path_imageZ	pdf_imageZforeign_image_pageZlocal_image_pager@   Ztext_misalignedZ	strip_oldZpage_rotationr   r   r   
graft_paged   sp   




zOcrGrafter.graft_pagec                 C  s   | j jd }t|| j| j| jd | jd| jd  d}| j	j
js;tt |  W d   n1 s6w   Y  | jd| jd  d}| j | | j   t|| _ | j jd jj| _d\| _| _|  jd7  _dS )zSave and reload the Pdf.

        This will keep a lid on our memory usage for very large files. Attach
        the font to page 1 even if page 1 doesn't use it, so we have a way to get it
        back.
        r   r   r   r   r   z.workingr)   z.pdfNr(   )r.   rF   r   r   r   r   r1   with_suffixr4   r*   rG   Zkeep_temporary_filesr   FileNotFoundErrorunlinksavecloser	   r-   r   ZProcSet)r5   Zpage0Zold_fileZ	next_filer   r   r   rJ      s$   	



zOcrGrafter.save_and_reloadc                 C  s   | j | j | j   | jS r   )r.   rP   r1   rQ   )r5   r   r   r   finalize   s   
zOcrGrafter.finalizec           	      C  s   d\}}d}z[t |K}z|jd jdi }W n tttfy-   Y W d   W dS w d}|D ]}||d}|durB|} nq2|rK| j	|}||fW  d   W S 1 sZw   Y  W dS  t
tfym   Y dS w )z0Copy a font from the filename text into pdf_baser(   )z/f-0-0z/F1r   z/FontN)r	   r-   rF   r   getAttributeError
IndexErrorKeyErrorr.   Zcopy_foreignrN   r
   )	r5   textr   r   Zpossible_font_namespdf_textZpdf_text_fontsZpdf_text_fontfr   r   r   rB      s0   (zOcrGrafter._find_fontr?   r   r   r   r   r   r@   rA   boolc                  s:  t d t| jdkrdS t|jd j	 }| j
j| fddtdD }	|	d |	d  |	d |	d	  }
} fd
dtdD }	|	d |	d  |	d |	d	  }}t |
 d | d }t |d |d }t |	d |	d	 }| d }t |}|dv r||
}
}||
 }|| }t ||}|| | | | }t tj}t|tj}tjdd}| j
|}|||< tj|_tj|_d	|_|	|_t|||tjgd d|  d|  d }t| j
|}|rt | j
   j!|dd t |||d W d   dS 1 sw   Y  dS )zAInsert the text layer from text page 0 on to pdf_base at page_numZGraftingr   Nc                   s    g | ]}t  jd  j| qS )r   )floatrF   MediaBox.0v)rX   r   r   
<listcomp>   s     z0OcrGrafter._graft_text_layer.<locals>.<listcomp>      r   r)   c                   s   g | ]	}t  j| qS r   )r[   r\   r]   )	base_pager   r   r`     s    r>   )Z   i  zOCR-)prefixrL   s   q %s cm
s   %s Do
s   
Q
T)prepend)"rD   rE   r   statst_sizer	   r-   rF   r"   
read_bytesr.   pranger   Z
translatedZrotatedZscaledr   r   r   ZXObjectrandomZmake_streamTypeZFormZSubtypeZFormTypeZBBoxr   ZPDFencoder   r&   Zcontents_add)r5   r?   r;   r   r   r   r@   rA   Zpdf_text_contentsZmediaboxwthtZwphp	translateZuntranslateZcornerrotateZscale_xZscale_yscaleZctmZbase_resourcesZ
base_xobjsZtext_xobj_nameZxobjZpdf_draw_xobjZnew_text_layerr   )rc   rX   r   rH      sV   
""

$zOcrGrafter._graft_text_layerN)r7   r8   r9   r:   r;   r:   r<   r8   )r?   r8   r;   r   r   r   r   r   r   r   r@   r8   rA   rZ   )
__name__
__module____qualname____doc__r6   rK   rJ   rR   rB   rH   r   r   r   r   r'   P   s    
>"r'   )rx   
__future__r   logging
contextlibr   pathlibr   Zpikepdfr   r   r   r   r	   r
   r   r   r   r   	getLoggerru   rD   rI   r   r   r&   r'   r   r   r   r   <module>   s   0
