
    )iO              	       :   S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKr	S SK
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJr  \" S5      SLr\" S5      SLr\R@                  " \!5      r"S r#\$" \RJ                  RL                  \RJ                  RN                  \RP                  " \RJ                  RN                  SS	9\#S
9r)Sr*\$" SS\+S9r,\$" SSSSS9r-S r. " S S5      r/ " S S\/5      r0 " S S\05      r1 " S S\/5      r2S r3S r4S r5S r6S  r7g)!    N)Path)	find_spec)	add_input	get_inputsetup_loggingiterator_hasvalueBooleanOptionalActionPILcv2c                     U[         R                  :X  a  SnO7U[         R                  :X  a  SnO [        S[        R
                  U    S35      e[        R                  R                  " XU/UQ70 UD6$ )NFTz5Cannot create foreign_simple bitmap with bitmap type .)	pdfium_cFPDFBitmap_BGRxFPDFBitmap_BGRARuntimeErrorpdfium_iBitmapTypeToStrpdfium	PdfBitmapnew_foreign_simple)widthheightformatargskwargs	use_alphas         U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/pypdfium2/_cli/render.py_bitmap_wrapper_foreign_simpler      sp    )))		8++	+	RS[SkSklrSsRttuvww..ui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      r0   r0   r0   r0   r0   r0   )r1   r0   r   r0   c           
        ^ [        U SS9  U R                  SSS SSS9  U R                  SS	S
9  U R                  SS[        R                  SS9  [        [
        [        S.mU R                  SSU4S jS[        TR                  5       5       3S9  U R                  SS[        SS9  U R                  SS[        SSS9  U R                  "  SjSS0[        D6  U R                  S S!S"S#9  U R                  S$S%S&[        S'S(S)9  U R                  S*[        SS+S,9  U R                  S-[        SS.S,9  U R                  S/S0/ S1[        R                  S2S39  U R                  S4S5S6S79  U R                  S8S9S:9nUR                  S;[        R                  5       S<S=[        R                  S>9  UR                  S?S5S@S79  UR                  SASBSC SDS9  UR                  SESF[        SGSH9  UR                  SI[        SJS79  U R                  SKSLS:9nUR                  SMSN[        [        R                   SOSP9  UR                  SQ["        R$                  " 5       [        SRS9  UR                  SSSTSU[        R                  SVSW9  UR                  SXSYSZ[        R                  S[SW9  UR                  S\[        R                  S]S9  U R                  S^S_S:9nUR                  S`S5SaS79  UR                  " Sk0 [        D6  UR                  " Sl0 [        D6  UR                  " Sm0 [        D6  UR                  " Sn0 [        D6  UR                  SbS5ScS79  U R                  SdSeS:9nUR                  SfS5SgS79  UR                  ShS5SiS79  g )oNT)pagesz--outputz-oc                 P    [        U 5      R                  5       R                  5       $ N)r   
expanduserresolve)ps    r   <lambda>attach.<locals>.<lambda><   s    a++-557r   zDOutput 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-fz/The image format to use (default: conditional).)r-   r=   )pilz	numpy+pilz	numpy+cv2z--engine
engine_clsc                 *   > TU R                  5          $ r6   lower)kengines_maps    r   r:   r;   M   s    QWWY/r   zThe saver engine to use )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.)defaultr-   r=   z
--rotationr   )r   Z      i  z'Rotate pages by 90, 180 or 270 degrees.)rG   r-   choicesr=   r=   zColor the bitmap will be filled with before rendering. 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.)rJ   r=   z--cropr)   r*   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r+   r,   r-   rG   r=   z--draw-annotsz1Whether annotations may be shown (default: true).)actionrG   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,   rG   rJ   r-   r=   z--force-halftone
store_truez)Always use halftone for image stretching.)rM   r=   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr!   zThe bitmap maker to use.)rJ   rG   r=   r-   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 .    SSS.U R                  5          $ )NFT)bgrrgbrA   )vs    r   r:   r;      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).)rE   rM   r=   z--maybe-alphazWhether to use BGRA if page content has transparency. Note, this makes format selection page-dependent. As this behavior can be confusing, it is not currently the default, but recommended for performance in these cases.Parallelizationz.Options for rendering with multiple processes.z--linear?zRender non-parallel if page count is less or equal to the specified value (default: 4). 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forkr^   zQThe process start method to use. ('fork' is discouraged due to stability issues.))rJ   rG   r-   r=   z--parallel-libmpftrb   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).zFlat color schemezOptions for using pdfium's color scheme renderer. Note that this may flatten different colors into one, so the usability of this is limited. Alternatively, consider post-processing with lightness inversion (see below).z--sample-themezWUse a dark background sample theme as base. Explicit color params override selectively.z--fill-to-strokea  When rendering with custom color scheme, only draw borders around fill areas using the `path_stroke` color, instead of filling with the `path_fill` color. This is actually recommended, since with a single fill color for paths the boundaries of adjacent fill paths are less visible.zPost processingzgOptions to post-process rendered images. Note, this may have a strongly negative impact on performance.z--invert-lightnessa]  Invert lightness using the HLS color space (e.g. white<->black, dark_blue<->light_blue). The intent is to achieve a dark theme for documents with light background, while providing better visual results than classical color inversion or a flat pdfium color scheme. However, note that --optimize-mode lcd is not recommendable when inverting lightness.z--exclude-imagesz7Whether to exclude PDF images from lightness inversion.)z--fill-color)z--path-fill)z--path-stroke)z--text-fill)z--text-stroke)r   add_argumentstrrB   	PILEngineNumpyPILEngineNumpyCV2Enginetuplekeysfloatint	ColorOptsr	   add_argument_groupBitmapMakersmathinfos	cpu_count)parserbitmapparallelcolor_schemepostprocrD   s        @r   attachry   8   s   fD!
D7U	   U   Dyy@  
 $.~^K
/)%0@0@0B*C)DE	    |	   #8    j 
 "D  
 15@	   &B	   &<	   +yy7   :   && > ' F ##%)yy   A  
 ?L	   &Q	   & m   ((!F ) H  B   ,,.i	   1yyb   yyc   yy]   ,,# s - L h  
 ;;;;;;;; k   ((! ) H  o  
 H  r   c                   &    \ rS rSrS rS rS rSrg)SavingEngine   c                     Xl         X l        g r6   r   postproc_kwargs)self
saver_argsr   s      r   __init__SavingEngine.__init__   s    	.r   c                 ~    U R                   nUR                  UR                   US-   SUR                   S3 SU 3-  $ )NrF   0dr   )r   
output_dirprefixn_digits)r   iextr   s       r   	_get_pathSavingEngine._get_path   sA    yyDKK=1Qt}}oQ4F0Gq!NNNr   c                    U R                   R                  (       aM  U R                   R                  S;   a3  [        R                  " U5      (       a  [
        R                  S5        SnOU R                   R                  nU R                  X5      nU R                  XRX0R                  5        [
        R                  SUS-    SUR                   35        g )N)jpgjpegz8Page has transparency - overriding output format to PNG.pngzWrote page rF   z as )r   maybe_alphar   r   FPDFPage_HasTransparencyloggerinfor   _saving_hookr   name)r   r   ru   pager   out_paths         r   __call__SavingEngine.__call__   s    99  TYY%5%5%HXMnMnosMtMtKKRSC))""C>>!)(D2F2FGk!A#d8==/:;r   r~   N)__name__
__module____qualname____firstlineno__r   r   r   __static_attributes__ r   r   r{   r{      s    /O	<r   r{   c                       \ rS rSrS r\" \R                  R                  5      r	S r
\S 5       rSr\\R                  " SS9S 5       5       r\S	 5       rS
rg)rf   i  c                 x    U R                   S   (       d  g [        R                  S5        SS KqSS KqSS KqSS Kqg )Ninvert_lightnessz&PIL engine imports for post-processingr   )r   r   debug	PIL.Imager
   PIL.ImageOpsPIL.ImageFilterPIL.ImageDrawr   s    r   
do_importsPILEngine.do_imports
  s-    ##$67=>r   c                     UR                  U5      nU R                  U5      nU R                  " XcU40 UD6nUR                  U5        g r6   )get_posconv_to_pil_hookpostprocesssave)r   r   ru   r   r   posconv	pil_images          r   r   PILEngine._saving_hook  sE    $$T*%%f-	$$YgQQ	x r   c                 n    [         R                  " XU5      u  p4nSU-
  n[         R                  " X4U5      $ )NrF   )colorsys
rgb_to_hls
hls_to_rgb)rgbhlss         r   _invert_px_lightnessPILEngine._invert_px_lightness  s4    %%aA.aE""1++r      rF   )maxsizec                 ~    [         R                  R                  R                  U R                  U R
                  5      $ r6   )r
   ImageFilter
Color3DLUTgenerateLINV_LUT_SIZEr   )clss    r   _get_linv_lutPILEngine._get_linv_lut$  s-     ))2233D3DcF^F^__r   c           	      |   UnU(       Ga+  UR                   S:X  a   [        R                  R                  U5      nOUR	                  U R                  5       5      nU(       a  [        UR                  [        R                  /SS95      u  pxU(       a  [        R                  R                  SUR                  5      n	[        R                  R                  U	5      n
U HF  nUR                  5        VVs/ s H  u  pUR!                  X5      PM     nnnU
R#                  USS9  MH     UR%                  XS9  U$ s  snnf )NLrF   	max_depth1)fill)mask)moder
   ImageOpsinvertfilterr   r   get_objectsr   FPDF_PAGEOBJ_IMAGEImagenewsize	ImageDrawDrawget_quad_points	to_bitmappolygonpaste)r   	src_imager   r   r   exclude_images	dst_imagehave_images
obj_walkerr   drawobjxyqpointss                  r   r   PILEngine.postprocess)  s    	~~$LL//	:	%,,S->->-@A	 +<T=M=MxOjOjNkwx=M=y*{'99==inn=D==--d3D)GJGZGZG\"]G\tq7#4#4Q#:G\"]W15  * OOIO9 #^s   3D8r   N)r   r   r   r   r   staticmethodr   r   to_pilr   r   r   r   classmethod	functools	lru_cacher   r   r   r   r   r   rf   rf     sz       0 0 7 78L! , ,
 M#` $ `  r   rf   c                   8   ^  \ rS rSrU 4S jr\S 5       rSrU =r$ )rg   i?  c                 T   > [         R                  S5        SS Kq[        TU ]  5         g )NzNumPy+PIL engine importsr   )r   r   r   r
   superr   )r   	__class__s    r   r   NumpyPILEngine.do_importsA  s    /0r   c                 r    [         R                  R                  U R                  5       U R                  5      $ r6   )r
   r   	fromarrayto_numpyr   )ru   s    r   r   NumpyPILEngine._to_pil_hookG  s$    yy""6??#4fkkBBr   r   )	r   r   r   r   r   r   r   r   __classcell__)r   s   @r   rg   rg   ?  s     C Cr   rg   c                   0    \ rS rSrS rS r\S 5       rSrg)rh   iL  c                 h    [         R                  S5        SS KqU R                  S   (       a  SS Kqg g )NzNumPy+cv2 engine importsr   r   )r   r   r   r   numpynpr   s    r   r   NumpyCV2Engine.do_importsN  s)    /0 01 2r   c                     UR                  5       nU R                  " XRU40 UD6n[        R                  [	        U5      U5        g r6   )r   r   r   imwritere   )r   r   ru   r   r   np_arrays         r   r   NumpyCV2Engine._saving_hookU  s7    ??$##HdNoNCM8,r   c                 6   UnU(       Ga  UR                   [        R                  :X  a  U) nOUR                  (       a   [        R
                  [        R                  4O[        R                  [        R                  4u  px[        R                  Xg5      n[        R                  U5      u  pnU
) n
[        R                  XU/5      n[        R                  Xh5      nU(       Ga/  UR                   [        R                  :w  d   S5       eUR                  U5      n[        UR                  [        R                   /SS95      u  pU(       a  ["        R%                  UR&                  UR(                  S4["        R*                  5      nU Hs  n["        R-                  UR/                  5        VVs/ s H  u  nnUR1                  UU5      PM     snn["        R2                  5      n[        R5                  UU/S5        Mu     [        R7                  XUS9nU$ s  snnf )Nz=Not sure how to paste with mask on {RGB,BGR}X image using cv2rF   r   )r   dst)r   r   FPDFBitmap_GrayrU   r   COLOR_RGB2HLSCOLOR_HLS2RGBCOLOR_BGR2HLSCOLOR_HLS2BGRcvtColorsplitmerger   r   r   r   r   r   zerosr   r   uint8arrayr   r   int32fillPolycopyTo)r   r   ru   r   r   r   r   
convert_toconvert_fromr   r   r   r   r   r   r   r   r   r   r   s                       r   r   NumpyCV2Engine.postprocessZ  s   	}} 8 88&J	U[UiUiC,=,=s?P?P+Qps  qB  qB  DG  DU  DU  pV(
LL?	))I.aBIIqQi0	LLA	}}(@(@@  B  CB  B@ ,,T2*;T=M=MxOjOjNkwx=M=y*{'88V]]FLL!$DbhhOD)"$((PSPcPcPe+fPe1G,=,=a,CPe+fhjhphp"qTG9a8  * !$

9Y
 OI ,gs   -Hr   N)	r   r   r   r   r   r   r   r   r   r   r   r   rh   rh   L  s     -
  r   rh   c                     U " 5         [         R                  S[        R                  " 5        35        U" 5         [        R
                  " X#SS9nU(       a  UR                  5         XuU4qg )NzInitializing data for process T)password	autoclose)r   r   rr   getpidr   PdfDocument
init_formsProcObjs)logging_initengine_initinputr  may_init_formsr   enginepdfs           r   _render_parallel_initr$  t  sQ    N
KK0>?M


U
FC V$Hr   c                 B    X   nUR                   " S0 UD6nU" XU5        g )Nr   )render)r   r#  r   r"  r   ru   s         r   _render_jobr'    s$    6D[["6"F
1dr   c                 $    [        U /[        Q76   g r6   )r'  r  )r   s    r   _render_parallel_jobr)    s    Hr   c                      g r6   r   r   r   r   _do_nothingr+    s    4r   c                   ^ U R                   R                  5       (       d  [        SU R                   < 35      e[        X R                  S9n[        U5      m[        U4S jU R                   5       5      (       d  [        S5      e[        U R                  5      [        [        U R                  5      5      :w  a  [        S5      eU R                  c  U R                  R                   S3U l	        U R                  c  U R                  (       a  SOSU l        U R                  c  U R                  S	   S
:X  a  SOSU l        U R                  c  SU l        U R                   cZ  ["        (       d  [$        (       d   S5       e["        (       a  [$        (       a  U R                  S:X  a  [&        U l        O[(        U l        U R*                  c  [-        U R                   [(        5      U l        U R.                  c5  [-        U R                   [(        5      =(       a    U R                  S:g  U l        [1        5       nU R                  (       a  UR2                  " S"0 [4        D6  UR2                  " S"0 [6         Vs0 s H!  n[9        X5      (       d  M  U[9        X5      _M#     snD6  U(       a  [:        R<                  " S"0 UD6OS n[1        U R>                  U R@                  U RB                  U RD                  U R                  U RF                  U RH                  U R                  U RJ                  U R*                  U R.                  U RL                  [N        U RP                     UU RR                  S9nU RT                   H  nSUSU 3'   M     [V        RX                  " U R                   U R                  [        [[        T5      5      U R                  U RL                  S9n[1        U R\                  U R^                  S9nU R\                  (       a%  U RF                  S:X  a  [`        Rc                  S5        [e        U 5      Rg                  5       n	U	S	 U	S	 U	S   (       a  SU	S'   [`        Ri                  U	 5        U(       a  [`        Ri                  U 5        U R!                  Xx5      n
[        U R                  5      U R                  ::  aE  [`        Rk                  S5        U
Rm                  5         U R                   H  n[o        XXZ5        M     g [`        Rk                  S5        [p        Rr                  " U Rt                  5      n[1        URv                  S4[x        Rz                  " [|        R~                  US9S4S9nXR                     u  pU R                  (       a  U R                  nU Rt                  S :X  a  [        [        nnU
Rm                  5         O[        U
Rl                  nn[1        [        UUUR                  U R                  U R                  XZ4S!9n[        U R                  [        U R                  5      5      nU" U40 UD6 n[9        UU5      nU" [        U R                  5       H  nM     S S S 5        g s  snf ! , (       d  f       g = f)#Nz*Output path is not an existing directory: )r  c              3   N   >#    U  H  nS Us=:*  =(       a    T:  Os  v   M     g7f)r   Nr   ).0r   pdf_lens     r   	<genexpr>main.<locals>.<genexpr>  s!     4AqAs   "%z*Out-of-bounds page indices are prohibited.z&Duplicate page indices are prohibited._)r   r   r   r0   r2      r0   r   r   r*   z?Either pillow or numpy+cv2 must be installed for rendering CLI.)scalerotationcrop	grayscale
fill_coloroptimize_modedraw_annotsmay_draw_formsforce_halftonerU   rZ   r   bitmap_makerrw   fill_to_strokeT	no_smooth)r   r   r   r   r   )r   r   rK   ztLCD optimization clashes with lightness inversion, as post-processing colors defeats the idea of subpixel rendering.
subcommandr4   r  z<obfuscated>zLinear rendering ...zParallel rendering ...imap)
mp_contextmapra   r`   )initializerinitargsr   )Joutputis_dir
ValueErrorr   
draw_formslenallr4   setr   r   stemr8  sample_themer   linearr?   have_pilhave_cv2rh   rf   rU   
issubclassrZ   dictupdateSampleThemeColorSchemeFieldsgetattrr   PdfColorSchemer4  r5  r6  r7  r9  r:  r<  r   ro   r=  r>  no_antialiastypesSimpleNamespacere   r   r   r   warningvarscopyr   r   r   r'  rb   get_contextparallel_strategyPoolr   partialrc   ProcessPoolExecutorparallel_libparallel_mapr+  r   r$  _inputr  min	processesr)  )r   r#  	cs_kwargsfrw   r   r-   r   r   
print_argsr"  r   ctxpool_backends	pool_ctormap_attrr  r  pool_kwargsn_procspoolmap_funcr2  r/  s                          @r   mainrt    s   ;;Edkk_UVV
D__
5C#hG4444EFF
4::#c$**o..ABB{{)+,0,=,=.CW{{#q1S8ee{{ x88f%ff#hh4;;%+?,DO'DO !'C%dooyAZdkkUZFZI';'\5F[5F'RVJZ+74++5F[\9B6((595L

==yyNN__**&&,,**&&&&#D$5$56#,,F" !!%)4&!" " &&[[s7|$&&J 00,,O !3!3u!<  N  	Od"J< *W"5*!/
:
LLJ<!~'__Z9F
4::$++%*+A/ 
 	,-nnT334((F###B$:$:sKUS
 ,,=,=>	((H!!V+(3[+L(5v7H7H+L/$k3::t}}doo_en

 dnnc$**o6w.+.$tX.H2DJJ? @ /._ \^ /.s   2Y5
Y5+Y::
Z)8rr   rp   rZ  loggingr   r   pathlibr   multiprocessingrb   concurrent.futuresfuturesrc   importlib.utilr   pypdfium2._helpers_helpersr   pypdfium2.internalinternalr   pypdfium2.rawrawr   pypdfium2._cli._parsersr   r   r   r   r	   rP  rQ  	getLoggerr   r   r   rS  r   
new_nativenew_foreignrb  ro   rV  rl   rm   rU  ry   r{   rf   rg   rh   r$  r'  r)  r+  rt  r   r   r   <module>r     s8   
         $ # %    U4'U4'			8	$Z ((**&&v'7'7'C'CRVW3	 M AC0	&&&&wt< <,4 4n
Ci 
C%l %P% {r   