
    j5j'                       d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ e
rddlZn	 ddlZddlmZ ddlmZ dd	lmZ dd
lmZ  ej.                  e      Z ej4                         Zed        ZddZddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ ddZ!e	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z"y# e$ r dZY w xY w)zDBuilt-in plugin to implement PDF page rasterization using pypdfium2.    )annotationsN)closing)Path)TYPE_CHECKINGLiteral)Image)hookimpl)MissingDependencyError)
Resolutionc                F    | j                   dk(  rt        t        d      yy)z:Check that pypdfium2 is available if explicitly requested.pypdfiumNzgThe --rasterizer pypdfium option requires the pypdfium2 package. Install it with: pip install pypdfium2)
rasterizerpdfiumr
   )optionss    W/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/builtin_plugins/pypdfium.pycheck_optionsr   #   s0     Z'FN$5
 	
 -;'    c                F    t         J d       t        j                  |       S )z$Open a PDF document using pypdfium2.z1pypdfium2 must be available to call this function)r   PdfDocument)
input_files    r   _open_pdf_documentr   -   s%    RRRj))r   c                B    | j                         } | j                  |  y)u  Set the page's CropBox to its MediaBox so PDFium renders the full page.

    PDFium renders to the CropBox by default. Negative ``crop`` values to
    ``render()`` are not supported and only pad the output canvas without
    expanding the rendered area — content outside the CropBox is clipped.
    The supported approach is to widen the CropBox in memory before rendering.
    The document is never saved back to disk, so this mutation is local.
    See https://github.com/ocrmypdf/OCRmyPDF/issues/1685.
    N)get_mediaboxset_cropbox)pagemediaboxs     r   _expand_cropbox_to_mediaboxr   3   s"       "HDhr   c                   |j                  d      }| j                         \  }}t        t        ||j                  z  dz              }t        t        ||j                  z  dz              }|j                         dz  }	|r!| j                  | dz         |dz  dk(  r||}}|j                         dv }
|st        |        | j                  |	ddd|
	      }|||fS )
zRender a PDF page to a bitmap.   g      R@ih     Z   )pngmonopngmonodpnggrayjpeggrayr   T)scalerotationmay_draw_formsdraw_annots	grayscale)
roundget_sizeintxy	to_scalarset_rotationlowerr   render)r   raster_device
raster_dpir'   use_cropboxpage_width_ptspage_height_ptsexpected_widthexpected_heightr&   r*   bitmaps               r   _render_page_to_bitmapr<   A   s     !!!$J '+mmo#NO ~
<tCDEN%*,, > EFGO   "T)E  	8)c/*c>R.=~ON
 ##% * I #D)[[  F >?22r   c           
     .   |r|r| j                   | j                  }}t        ||z
        }	t        ||z
        }
|	dk  s|
dk  rW|	dkD  s|
dkD  rMt        j	                  d| d| d| d|        | j                  ||ft        j                  j                        } |r:t        |j                        t        |j                        f}|| j                  d<   n9t        |j                        t        |j                        f}|| j                  d<   |j                         }|dv rA| j                  dk7  r| j                  d	vr| j                  d
      } | j                  d      } n|dv r | j                  d	vr| j                  d
      } n|dk(  rA| j                  dk7  r| j                  dvr| j                  d      } | j!                  d      } nz|dv rv| j                  dk(  rHt        j"                  d| j$                  d      }|j'                  | | j)                         d          |} n| j                  dvr| j                  d      } d}||v rd}| |fS |dv rd}| |fS |dv rd}| |fS d}|rt+        d|       t        j-                  d| d        | |fS )!z9Process PIL image for output format and set DPI metadata.   r   z#Adjusting rendered dimensions from r.   z to expected dpi)r"   r#   1)Lr@   rA   )r$   r%   png256P)RGBRGBArD      )colors)png16mjpegrE   )   rJ   rJ   )mask)rD   )pngr"   r#   r$   rB   rH   pngalphaPNG)rI   r%   jpgJPEG)tifftifTIFFzUnsupported raster device: zUnsupported raster device z, using PNG)widthheightabslogdebugresizer   
ResamplingLANCZOSfloatr.   r/   infor2   modeconvertquantizenewsizepastesplit
ValueErrorwarning)	pil_imager4   r5   page_dpistop_on_soft_errorr9   r:   actual_widthactual_height
width_diffheight_diff	dpi_tupleraster_device_lower
backgroundpng_devicesformat_names                   r   _process_image_for_outputrt   z   s    /&/ooy7G7Gm67
-/9: !O{a/NkAoII5.-!"!O#46
 "((153C3C3K3KI
 8::&hjj(9:	 )	u :<<(%
*=>	 )	u (--/55>>S ~~Z/%--c2	!))#.I	 7	7>>+!))#.I		(>>S ~~_4%--e4	!**#*6I	 2	2>>V#5)../JJYY__->r-BC"I^^8+!))%0IK k) k!! 
 ;	; k!! 
	/ k!! :=/JKKKK4]O;OPk!!r   c                    i }|dv rd| j                   v s|dk(  r d| j                   v r| j                   d   |d<    | j                  |fd|i| y)z5Save PIL image to file with appropriate DPI metadata.)rO   rT   r?   rQ   formatN)r^   save)rh   output_filers   save_kwargss       r   _save_imagerz      sZ    K&Y^^#& Y^^#&^^E2EINN;B{BkBr   c           
         |	|	j                   dk(  ryt        yt        j                  d|       t        5  t        t        |             5 }t        ||dz
           5 }t        |||||
      \  }}}t        |      5  |j                         }ddd       ddd       ddd       ddd       t        ||||      \  }}t        |||       |S # 1 sw Y   DxY w# 1 sw Y   HxY w# 1 sw Y   LxY w# 1 sw Y   PxY w)zRasterize a single page of a PDF file using pypdfium2.

    Returns None if pypdfium2 is not available or if the user has selected
    a different rasterizer, allowing Ghostscript to be used.
    Nghostscriptz1Rasterizing page %d with the pypdfium2 rasterizer   )r   r   rX   rY   _pdfium_lockr   r   r<   to_pilrt   rz   )r   rx   r4   r5   pagenori   r'   filter_vectorrj   r   r6   pdfr   r;   r9   r:   rh   rs   s                     r   rasterize_pdf_pager      s   ( w11]B~IIA6J 	(":./(36FQJ ( %) 3I-X{3
/ V_ 	(I	(( ( ( 7I{ 	;4#	( 	(( ( ( ( ( (sS   C4C(C<C	CC(C4CCC%!C((C1	-C44C=)r   r   )returnNone)r   zpdfium.PdfPager4   strr5   r   r'   
int | Noner6   boolr   z!tuple[pdfium.PdfBitmap, int, int])NN)rh   Image.Imager4   r   r5   r   ri   Resolution | Nonerj   r   r9   r   r:   r   r   z2tuple[Image.Image, Literal['PNG', 'TIFF', 'JPEG']])rh   r   rx   r   rs   r   r   r   )r   r   rx   r   r4   r   r5   r   r   r-   ri   r   r'   r   r   r   rj   r   r6   r   r   zPath | None)#__doc__
__future__r   logging	threading
contextlibr   pathlibr   typingr   r   	pypdfium2r   ImportErrorPILr   ocrmypdfr	   ocrmypdf.exceptionsr
   ocrmypdf.helpersr   	getLogger__name__rX   Lockr~   r   r   r   r<   rt   rz   r    r   r   <module>r      s   K "     )"   6 'g! y~~ 

 

* 63
6363 63 	63
 63 '63~ "&"&]"]"]" ]"  	]"
 ]" ]"  ]" 8]"@C 
666 6 	6
 6  6 6 6 6 6 6 
6o  s   C CC