o
    Zh42                     @   sT  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlm	Z
 zd dlZW n ey1   dZY nw d dlmZ d dlmZ d dlmZ d dlmZmZmZmZ eeZdd Zeej j!ej j"ej#ej j"ddedZ$d	Z%ed
de&dZ'eddddd	Z(dd Z)G dd dZ*G dd de*Z+G dd de*Z,dd Z-dd Z.dd Z/dd  Z0dS )!    N)Path)	add_input	get_inputsetup_loggingBooleanOptionalActionc                 O   sT   |t jkrd}n|t jkrd}ntdtj|  dtjj| ||g|R i |S )NFTz5Cannot create foreign_simple bitmap with bitmap type .)	pdfium_rZFPDFBitmap_BGRxZFPDFBitmap_BGRARuntimeErrorpdfium_iZBitmapTypeToStrpdfium	PdfBitmapZnew_foreign_simple)widthheightformatargskwargsZ	use_alpha r   L/var/www/html/lang_env/lib/python3.10/site-packages/pypdfium2/_cli/render.py_bitmap_wrapper_foreign_simple   s   

r   T)Zforce_packed)nativeZforeignZforeign_packedZforeign_simple)Z	path_fillZpath_strokeZ	text_fillZtext_strokeC   )metavarnargstype)   d   r      )r      r   r   r   r   r   r   )r   r   r   r   c                 C   sX  t | dd | jddtddd | jddd	 | jd
ddtjdd | jdddd dd | jddtdd | jddtddd | j	dfddit | jd d!d"d# | jd$d%td&d'd( | jd)tdd*d+ | jd,tdd-d+ | jd.d/g d0tjd1d2 | jd3d4d5d6 | j	d7d8d9}|jd:t
 d;d<tjd= |jd>d4d?d6 |jd@dAdBd dCd |jdDdEtdFdG | j	dHdId9}|jdJdKttjdLdM |jdNt tdOd |jdPdQdRtjdSdT |jdUdVdWtjdXdT |jdYtjdZd[ | j	d\d]d9}|jd^d4d_d6 |j	`dgi t |j	adhi t |j	bdii t |j	cdji t |jddd4ded6 d S )kNT)pagesz--outputz-ozDOutput directory where the serially numbered images shall be placed.)r   requiredhelpz--prefixzDCustom prefix for the images. Defaults to the input filename's stem.)r"   z--formatz-fjpgzThe image format to use.)defaultr   r"   z--engine
engine_clsc                 S   s   t td|   S )N)Zpilz	numpy+cv2)	PILEngineNumpyCV2Enginelower)kr   r   r   <lambda>M       zattach.<locals>.<lambda>z(The saver engine to use (pil, numpy+cv2))destr   r"   z--scale   zDefine the resolution of the output images. By default, one PDF point (1/72in) is rendered to 1x1 pixel. This factor scales the number of pixels that represent one point.z
--rotationr   )r   Z      i  z'Rotate pages by 90, 180 or 270 degrees.)r$   r   choicesr"   --fill-colorr"   zColor the bitmap will be filled with before rendering. It shall be given in RGBA format as a sequence of integers ranging from 0 to 255. Defaults to white.z--optimize-mode)Zlcdprintz3The rendering optimisation mode. None if not given.)r0   r"   z--cropr   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r   r   r$   r"   z--draw-annotsz1Whether annotations may be shown (default: true).)actionr$   r"   z--draw-formsz+Whether forms may be shown (default: true).z--no-antialias+)textimagepathz&Item types that shall not be smoothed.)r   r$   r0   r   r"   z--force-halftone
store_truez)Always use halftone for image stretching.)r3   r"   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr   zThe bitmap maker to use.)r0   r$   r"   r   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 S   s   ddd|    S )NFT)Zbgrrgb)r(   )vr   r   r   r*      r+   z;Whether to use BGR or RGB byteorder (default: conditional).z--x-channelprefer_bgrxz@Whether to prefer BGRx/RGBx over BGR/RGB (default: conditional).)r,   r3   r"   ZParallelizationz.Options for rendering with multiple processes.z--linear?zRender non-parallel if page count is less or equal to the specified value (default is conditional). If this flag is given without a value, then render linear regardless of document length.)r   r   constr"   z--processeszXThe maximum number of parallel rendering processes. Defaults to the number of CPU cores.z--parallel-strategy)spawn
forkserverforkrA   zQThe process start method to use. ('fork' is discouraged due to stability issues.))r0   r$   r   r"   z--parallel-libmpftrE   zRThe parallelization module to use (mp = multiprocessing, ft = concurrent.futures).z--parallel-mapzLThe map function to use (backend specific, the default is an iterative map).)r   r"   zForced color schemez[Options for using pdfium's forced color scheme renderer. Deprecated, considered not useful.z--sample-themezWUse a dark background sample theme as base. Explicit color params override selectively.--path-fill--path-stroke--text-fill--text-strokez--fill-to-strokezqOnly draw borders around fill areas using the `path_stroke` color, instead of filling with the `path_fill` color.)r1   )rG   )rH   )rI   )rJ   )r   add_argumentr   strr(   floatint	ColorOptsr   add_argument_groupBitmapMakerskeysmathinfos	cpu_count)parserbitmapZparallelcolor_schemer   r   r   attach8   sd  
rZ   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SavingEnginec                 C   s
   || _ d S NZ_path_parts)self
path_partsr   r   r   __init__   s   
zSavingEngine.__init__c                 C   s2   | j \}}}}|| |d d| dd|  S )Nr-   0dr   r]   )r^   i
output_dirprefixn_digitsr   r   r   r   	_get_path   s   $zSavingEngine._get_pathc                 C   s6   |  |}| || td|d  d|j  d S )NzWrote page r-   z as )rg   _saving_hookloggerinfoname)r^   rX   rc   out_pathr   r   r   __call__   s   
 zSavingEngine.__call__N)__name__
__module____qualname__r`   rg   rm   r   r   r   r   r[      s    r[   c                   @      e Zd Zdd ZdS )r&   c                 C   s   |  | d S r\   )Zto_pilsaver^   rl   rX   r   r   r   rh      s   zPILEngine._saving_hookNrn   ro   rp   rh   r   r   r   r   r&          r&   c                   @   rq   )r'   c                 C   s   t t||  d S r\   )cv2ZimwriterL   Zto_numpyrs   r   r   r   rh      s   zNumpyCV2Engine._saving_hookNrt   r   r   r   r   r'      ru   r'   c                 C   sH   | r|   t dt   tj||dd}|r|  |||fad S )NzInitializing data for process T)passwordZ	autoclose)ri   rj   rU   getpidr   ZPdfDocument
init_formsProcObjs)
extra_initinputrw   Zmay_init_formsr   enginepdfr   r   r   _render_parallel_init  s   r   c                 C   s&   ||  }|j di |}|||  d S )Nr   )render)rc   r~   r   r}   pagerX   r   r   r   _render_job  s   r   c                 C   s   t | gtR   d S r\   )r   rz   )rc   r   r   r   _render_parallel_job  s   r   c                    s:  t   jd}t|tfdd jD stdt jtt jkr+td jd u r8 jj	 d _ j
d u rE jrBdnd _
 jd u rT jd	krQd
nd _ jd u ritd krf jdkrft _nt _ jd u rt jtu  _ jd u r jtu o jdk _t } jr|jdi t |jdi  fddtD  t|dkrtjdi |nd }t j j j j j
| j j j  j j! j jt" j# d} j$D ]	}d|d| < qt%& jj' d j d j d j  tt(} j) j| jf} |}t j jkr&t%&d  jD ]
}	t*|	||| qd S t%&d t+, j-}
t|
j.dft/j0t1j2|
ddfd}| j3 \}} j4rP j4} j-dv rXt5nd }tt6||j7 j8 j||fd}t9 j:t j}||fi |}t;||}|t< jD ]}qW d    d S 1 sw   Y  d S )N)ry   c                 3   s(    | ]}d |  ko k n  V  qdS )r   Nr   ).0rc   )pdf_lenr   r   	<genexpr>#  s   & zmain.<locals>.<genexpr>z*Out-of-bounds page indices are prohibited.z&Duplicate page indices are prohibited._)r   r   r   r   r   r#         Zpngc                    s"   i | ]}t  |r|t  |qS r   )getattr)r   f)r   r   r   
<dictcomp>@  s   " zmain.<locals>.<dictcomp>r   )scalerotationcrop	grayscale
fill_colorrY   fill_to_strokeoptimize_modedraw_annotsZmay_draw_formsforce_halftoner;   r>   bitmap_makerTZ	no_smoothz
, Format: z, rev_byteorder: z, prefer_bgrx zLinear rendering ...zParallel rendering ...imap)Z
mp_contextmaprD   )rA   rB   )ZinitializerZinitargsr   )=r   Z
draw_formslenallr    
ValueErrorsetre   r|   stemr   Zsample_themeZlinearr   r%   rv   r'   r&   r;   r>   dictupdateSampleThemeCsFieldsr   ZPdfColorSchemer   r   r   r   r   r   r   r   rQ   r   Zno_antialiasri   rj   rn   rL   outputr   rE   Zget_contextZparallel_strategyZPool	functoolspartialrF   ProcessPoolExecutorZparallel_libZparallel_mapr   r   _inputrw   minZ	processesr   r   )r   r~   Z	cs_kwargscsr   r   rf   r_   r}   rc   ctxZpool_backendsZ	pool_ctorZmap_attrr{   pool_kwargsZn_procspoolZmap_funcr   r   )r   r   r   main  s   





 
*




$r   )1rU   rS   loggingr   pathlibr   multiprocessingrE   concurrent.futuresZfuturesrF   rv   ImportErrorZpypdfium2._helpersZ_helpersr   Zpypdfium2.internalZinternalr
   Zpypdfium2.rawrawr   Zpypdfium2._cli._parsersr   r   r   r   	getLoggerrn   ri   r   r   r   Z
new_nativeZnew_foreignr   rQ   r   rN   rO   r   rZ   r[   r&   r'   r   r   r   r   r   r   r   r   <module>   sR   
	 3