
    w;3h42                        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
 	 d dlZ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j6                  e      Zd Z eej@                  jB                  ej@                  jD                   ejF                  ej@                  jD                  d      e      Z$dZ% ed	d
e&      Z' edddd      Z(d Z) G d d      Z* G d de*      Z+ G d de*      Z,d Z-d Z.d Z/d Z0y# e$ r dZY w xY w)    N)Path)	add_input	get_inputsetup_loggingBooleanOptionalActionc                     |t         j                  k(  rd}n6|t         j                  k(  rd}n t        dt        j
                  |    d      t        j                  j                  | ||g|i |S )NFTz5Cannot create foreign_simple bitmap with bitmap type .)	pdfium_rFPDFBitmap_BGRxFPDFBitmap_BGRARuntimeErrorpdfium_iBitmapTypeToStrpdfium	PdfBitmapnew_foreign_simple)widthheightformatargskwargs	use_alphas         U/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/pypdfium2/_cli/render.py_bitmap_wrapper_foreign_simpler      sr    )))		8++	+	RS[SkSklrSsRttuvww..ufiY$YRXYY    T)force_packed)nativeforeignforeign_packedforeign_simple)	path_fillpath_stroke	text_filltext_strokeC   )metavarnargstype)   d   r      )r      r,   r,   r,   r,   r,   r,   )r-   r,   r   r,   c                    t        | d       | j                  ddt        dd       | j                  dd	       | j                  d
ddt        j                  d       | j                  ddd d       | j                  ddt
        d       | j                  ddt        dd        | j                  	 d`ddit         | j                  ddd !       | j                  d"d#t
        d$d%&       | j                  d't        dd()       | j                  d*t        dd+)       | j                  d,d-g d.t        j                  d/0       | j                  d1d2d34       | j                  d5d67      }|j                  d8t        j                         d9d:t        j                  ;       |j                  d<d2d=4       |j                  d>d?d@ dA       |j                  dBdCt        dDE       | j                  dFdG7      }|j                  dHdIt        t        j                  dJK       |j                  dLt        j                         t        dM       |j                  dNdOdPt        j                  dQR       |j                  dSdTdUt        j                  dVR       |j                  dWt        j                  dXY       | j                  dZd[7      }|j                  d\d2d]4        |j                  	 dai t          |j                  	 dbi t          |j                  	 dci t          |j                  	 ddi t         |j                  d^d2d_4       y )eNT)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.)r2   z--formatz-fjpgzThe image format to use.)defaultr)   r2   z--engine
engine_clsc                 >    t         t        d| j                            S )N)pilz	numpy+cv2)	PILEngineNumpyCV2Enginelower)ks    r   <lambda>zattach.<locals>.<lambda>M   s    HS r   z(The saver engine to use (pil, numpy+cv2))destr)   r2   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.)r4   r)   choicesr2   r2   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)lcdprintz3The rendering optimisation mode. None if not given.)rA   r2   z--cropr&   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r(   r)   r4   r2   z--draw-annotsz1Whether annotations may be shown (default: true).)actionr4   r2   z--draw-formsz+Whether forms may be shown (default: true).z--no-antialias+)textimagepathz&Item types that shall not be smoothed.)r(   r4   rA   r)   r2   z--force-halftone
store_truez)Always use halftone for image stretching.)rD   r2   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr   zThe bitmap maker to use.)rA   r4   r2   r)   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 .    ddd| j                            S )NFT)bgrrgb)r:   )vs    r   r<   zattach.<locals>.<lambda>   s    t4QWWY? 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=   rD   r2   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)   constr2   z--processeszXThe maximum number of parallel rendering processes. Defaults to the number of CPU cores.z--parallel-strategy)spawn
forkserverforkrU   zQThe process start method to use. ('fork' is discouraged due to stability issues.))rA   r4   r)   r2   z--parallel-libmpftrY   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)   r2   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.z--fill-to-strokezqOnly draw borders around fill areas using the `path_stroke` color, instead of filling with the `path_fill` color.)z--fill-color)z--path-fill)z--path-stroke)z--text-fill)z--text-stroke)r   add_argumentr   strr:   floatint	ColorOptsr   add_argument_groupBitmapMakerskeysmathinfos	cpu_count)parserbitmapparallelcolor_schemes       r   attachrk   8   s.   fD!
DU	   U   Dyy)	   S9	    |	   #8   F m 
 "D  
 @   &B	   &<	   +yy7   :   && > ' F ##%)yy   A  
 ?L	   &Q	   ((!F ) H  N   ,,.i	   1yyb   yyc   yy]   ,,%s - L h  
 L
 L
 L
 L
  C  r   c                       e Zd Zd Zd Zd Zy)SavingEnginec                     || _         y N_path_parts)self
path_partss     r   __init__zSavingEngine.__init__   s
    %r   c                 L    | j                   \  }}}}|| |dz   d| dd| z  S )Nr>   0dr	   rp   )rr   i
output_dirprefixn_digitsr   s         r   	_get_pathzSavingEngine._get_path   s?    /3/?/?,
FHfvhqs1XJa-&8&BBBr   c                     | j                  |      }| j                  ||       t        j                  d|dz    d|j                          y )NzWrote page r>   z as )r|   _saving_hookloggerinfoname)rr   rh   rx   out_paths       r   __call__zSavingEngine.__call__   sA    >>!$(F+k!A#d8==/:;r   N)__name__
__module____qualname__rt   r|   r    r   r   rm   rm      s    &C<r   rm   c                       e Zd Zd Zy)r8   c                 B    |j                         j                  |       y ro   )to_pilsaverr   r   rh   s      r   r~   zPILEngine._saving_hook   s    X&r   Nr   r   r   r~   r   r   r   r8   r8      s    'r   r8   c                       e Zd Zd Zy)r9   c                 ^    t        j                  t        |      |j                                y ro   )cv2imwriter\   to_numpyr   s      r   r~   zNumpyCV2Engine._saving_hook   s    CM6??#45r   Nr   r   r   r   r9   r9      s    6r   r9   c                     | r |         t         j                  dt        j                                 t	        j
                  ||d      }|r|j                          |||fay )NzInitializing data for process T)password	autoclose)r   r   re   getpidr   PdfDocument
init_formsProcObjs)
extra_initinputr   may_init_formsr   enginepdfs          r   _render_parallel_initr     sS    
KK0>?


UX
FC VV$Hr   c                 D    ||    } |j                   di |} |||        y )Nr   )render)rx   r   r   r   pagerh   s         r   _render_jobr     s(    q6DT[["6"F
61r   c                 $    t        | gt          y ro   )r   r   )rx   s    r   _render_parallel_jobr     s     .X.r   c                 H
   t        | | j                        }t        |      t        fd| j                  D              st        d      t        | j                        t        t        | j                              k7  rt        d      | j                  | j                  j                   d| _        | j                  | j                  rdnd| _
        | j                  | j                  dk(  rd	nd
| _        | j                  /t        d k7  r| j                  dk(  rt         | _        nt"        | _        | j$                  | j                  t"        u | _        | j&                  (| j                  t"        u xr | j                  dk7  | _        t)               }| j                  r |j*                  di t,          |j*                  di t.        D ci c]  }t1        | |      s|t1        | |       c} t        |      dkD  rt3        j4                  di |nd }t)        | j6                  | j8                  | j:                  | j<                  | j                  || j>                  | j@                  | jB                  | j                  | jD                  | j$                  | j&                  tF        | jH                           }| jJ                  D ]
  }d|d| <    tL        jO                  | j                  jP                   d| j                   d| j$                   d| j&                          t        tS                    }| jT                  | j                  || j                  f}| j                  |      }	t        | j                        | j                  k  r5tL        jO                  d       | j                  D ]  }
tW        |
|||	        y tL        jO                  d       tY        jZ                  | j\                        }t)        |j^                  dfta        jb                  td        jf                  |      df      }|| jh                     \  }}| jj                  r| jj                  }| j\                  dv rtl        nd }t)        tn        ||jp                  | jr                  | j                  ||	f      }tu        | jv                  t        | j                              } ||fi |5 }t1        ||      } |tx        | j                        D ]  } 	 d d d        y c c}w # 1 sw Y   y xY w)N)r   c              3   >   K   | ]  }d |cxk  xr k  nc   yw)r   Nr   ).0rx   pdf_lens     r   	<genexpr>zmain.<locals>.<genexpr>#  s     4AqA4s   z*Out-of-bounds page indices are prohibited.z&Duplicate page indices are prohibited._)r   r   r   r,   r.   r3         pngr   )scalerotationcrop	grayscale
fill_colorrj   fill_to_strokeoptimize_modedraw_annotsmay_draw_formsforce_halftonerL   rQ   bitmap_makerT	no_smoothz
, Format: z, rev_byteorder: z, prefer_bgrx zLinear rendering ...zParallel rendering ...imap)
mp_contextmaprX   )rU   rV   )initializerinitargsr   )=r   
draw_formslenallr0   
ValueErrorsetrz   r   stemr   sample_themelinearr   r5   r   r9   r8   rL   rQ   dictupdateSampleThemeCsFieldsgetattrr   PdfColorSchemer   r   r   r   r   r   r   r   ra   r   no_antialiasr   r   r   r\   outputr   rY   get_contextparallel_strategyPool	functoolspartialrZ   ProcessPoolExecutorparallel_libparallel_mapr   r   _inputr   min	processesr   )r   r   	cs_kwargsfcsr   r)   r{   rs   r   rx   ctxpool_backends	pool_ctormap_attrr   pool_kwargsn_procspoolmap_funcr   r   s                        @r   mainr     sV    DT__
5C #hG444EFF
4::#c$**o..ABB{{)+,0,=,=.CW{{;;%/aQ $;4;;%/,DO'DO !!__	9??i7PDKK5<PI	';'ISXRqAQ74++RS/29~/A			+	+tB

==yyNN__,,**&&,,**&&#D$5$56F  !! *%)4&!"* KK4??++,Jt{{mCTUYUgUgThhvw{  xH  xH  wI  J  K3w< H++t{{HdkkBJ__Z(F
4::$++%*+ 	0A3/	0
 	,-nnT334((F###B$:$:sKUS
 ,D,=,=>	8((H'+'='=AX'Xm^b
/"CJJtPVX^_

 dnnc$**o6w.+. 	$tX.H2DJJ? 	 	q Sp	 	s   T!T )TT!)1re   rc   loggingr   pathlibr   multiprocessingrY   concurrent.futuresfuturesrZ   r   ImportErrorpypdfium2._helpers_helpersr   pypdfium2.internalinternalr   pypdfium2.rawrawr
   pypdfium2._cli._parsersr   r   r   r   	getLoggerr   r   r   r   r   
new_nativenew_foreignr   ra   r   r^   r_   r   rk   rm   r8   r9   r   r   r   r   r   r   r   <module>r      s    
       $ %    
		8	$Z ((**&Y&&v'7'7'C'CRVW3	 DAC0	&&&&od< <' '6l 6
%/`[  
Cs   C< <DD