
    c?1iO                     R   d dl Z d dlZ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m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mZ  ed      duZ ed      duZ ej@                  e!      Z"d Z# e$ejJ                  jL                  ejJ                  jN                   ejP                  ejJ                  jN                  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/      Z0 G d de0      Z1 G d de/      Z2d Z3d Z4d Z5d Z6d  Z7y)!    N)Path)	find_spec)	add_input	get_inputsetup_loggingiterator_hasvalueBooleanOptionalActionPILcv2c                     |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_cFPDFBitmap_BGRxFPDFBitmap_BGRARuntimeErrorpdfium_iBitmapTypeToStrpdfium	PdfBitmapnew_foreign_simple)widthheightformatargskwargs	use_alphas         _/home/www/therecruiter.miabetepe.com/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      r0   r0   r0   r0   r0   r0   )r1   r0   r   r0   c           
         t        | d       | j                  ddd dd       | j                  dd	
       | j                  ddt        j                  d       t        t
        t        d| j                  ddfddt        j                                       | j                  ddt        d       | j                  ddt        dd        | j                  	 djddit         | j                  d d!d"#       | j                  d$d%d&t        d'd()       | j                  d*t        dd+,       | j                  d-t        dd.,       | j                  d/d0g d1t        j                  d23       | j                  d4d5d67       | j                  d8d9:      }|j                  d;t        j                         d<d=t        j                  >       |j                  d?d5d@7       |j                  dAdBdC dD       |j                  dEdFt        dGH       |j                  dIt        dJ7       | j                  dKdL:      }|j                  dMdNt        t        j                   dOP       |j                  dQt#        j$                         t        dR       |j                  dSdTdUt        j                  dVW       |j                  dXdYdZt        j                  d[W       |j                  d\t        j                  d]       | j                  d^d_:      }|j                  d`d5da7        |j                  dki t          |j                  dli t          |j                  dmi t          |j                  dni t         |j                  dbd5dc7       | j                  ddde:      }|j                  dfd5dg7       |j                  dhd5di7       y )oNT)pagesz--outputz-oc                 P    t        |       j                         j                         S N)r   
expanduserresolve)ps    r   <lambda>zattach.<locals>.<lambda><   s    a++-557 r   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                 *    | j                            S r6   lower)kengines_maps    r   r:   zattach.<locals>.<lambda>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.)rF   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.)rI   r<   z--cropr)   r*   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r+   r,   r-   rF   r<   z--draw-annotsz1Whether annotations may be shown (default: true).)actionrF   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,   rF   rI   r-   r<   z--force-halftone
store_truez)Always use halftone for image stretching.)rL   r<   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr!   zThe bitmap maker to use.)rI   rF   r<   r-   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 .    ddd| j                            S )NFT)bgrrgbr@   )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).)rD   rL   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.))rI   rF   r-   r<   z--parallel-libmpftra   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strrA   	PILEngineNumpyPILEngineNumpyCV2Enginetuplekeysfloatint	ColorOptsr	   add_argument_groupBitmapMakersmathinfos	cpu_count)parserbitmapparallelcolor_schemepostprocrC   s        @r   attachrx   8   s   fD!
D7U	   U   Dyy@  
 $.~^K
/)%0@0@0B*C)DE	    |	   #8   F 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  
 L;;L;;L;;L;; k   ((! ) H  o  
 H  r   c                       e Zd Zd Zd Zd Zy)SavingEnginec                      || _         || _        y r6   )r   postproc_kwargs)self
saver_argsr|   s      r   __init__zSavingEngine.__init__   s    	.r   c                 ~    | j                   }|j                  |j                   |dz   d|j                   dd| z  S )NrE   0dr   )r   
output_dirprefixn_digits)r}   iextr   s       r   	_get_pathzSavingEngine._get_path   sA    yyDKK=1Qt}}oQ4F0Gq!NNNr   c                    | j                   j                  rE| j                   j                  dv r-t        j                  |      rt
        j                  d       d}n| j                   j                  }| j                  ||      }| j                  |||| j                         t
        j                  d|dz    d|j                          y )N)jpgjpegz8Page has transparency - overriding output format to PNG.pngzWrote page rE   z as )r   maybe_alphar   r   FPDFPage_HasTransparencyloggerinfor   _saving_hookr|   name)r}   r   rt   pager   out_paths         r   __call__zSavingEngine.__call__   s    99  TYY%5%5%HXMnMnosMtKKRSC))""C>>!S)(FD$2F2FGk!A#d8==/:;r   N)__name__
__module____qualname__r   r   r    r   r   rz   rz      s    /O	<r   rz   c                       e Zd Zd Z eej                  j                        Zd Z	ed        Z
dZe ej                  d      d               Zed        Zy	)
re   c                 n    | j                   d   sy t        j                  d       dd ladd ladd ladd lay )Ninvert_lightnessz&PIL engine imports for post-processingr   )r|   r   debug	PIL.Imager
   PIL.ImageOpsPIL.ImageFilterPIL.ImageDrawr}   s    r   
do_importszPILEngine.do_imports
  s-    ##$67=>r   c                     |j                  |      }| j                  |      } | j                  |||fi |}|j                  |       y r6   )get_posconv_to_pil_hookpostprocesssave)r}   r   rt   r   r|   posconv	pil_images          r   r   zPILEngine._saving_hook  sI    $$T*%%f-	$D$$YgQQ	x r   c                 p    t        j                  | ||      \  }}}d|z
  }t        j                  |||      S )NrE   )colorsys
rgb_to_hls
hls_to_rgb)rgbhlss         r   _invert_px_lightnesszPILEngine._invert_px_lightness  s:    %%aA.1aE""1a++r      rE   )maxsizec                 ~    t         j                  j                  j                  | j                  | j
                        S r6   )r
   ImageFilter
Color3DLUTgenerateLINV_LUT_SIZEr   )clss    r   _get_linv_lutzPILEngine._get_linv_lut$  s-     ))2233D3DcF^F^__r   c           	      l   |}|r(|j                   dk(  r t        j                  j                  |      }n|j	                  | j                               }|rt        |j                  t        j                  gd            \  }}|rt        j                  j                  d|j                        }	t        j                  j                  |	      }
|D ]G  }|j                         D cg c]  \  }}|j!                  ||       }}}|
j#                  |d       I |j%                  ||	       |S c c}}w )NLrE   	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   zPILEngine.postprocess)  s
   	~~$LL//	:	%,,S->->-@A	 +<T=M=MxOjOjNkwx=M=y*{'Z99==inn=D==--d3D) 6GJGZGZG\"]tq!7#4#4Q#:"]"]W156 OOIDO9 #^s   'D0N)r   r   r   r   staticmethodr   r   to_pilr   r   r   r   classmethod	functools	lru_cacher   r   r   r   r   re   re     s|       0 0 7 78L! , ,
 MY#` $ `  r   re   c                   .     e Zd Z fdZed        Z xZS )rf   c                 T    t         j                  d       dd lat        |           y )NzNumPy+PIL engine importsr   )r   r   r   r
   superr   )r}   	__class__s    r   r   zNumpyPILEngine.do_importsA  s    /0r   c                 r    t         j                  j                  | j                         | j                        S r6   )r
   r   	fromarrayto_numpyr   )rt   s    r   r   zNumpyPILEngine._to_pil_hookG  s$    yy""6??#4fkkBBr   )r   r   r   r   r   r   __classcell__)r   s   @r   rf   rf   ?  s     C Cr   rf   c                   (    e Zd Zd Zd Zed        Zy)rg   c                 ^    t         j                  d       dd la| j                  d   rdd lay y )NzNumPy+cv2 engine importsr   r   )r   r   r   r|   numpynpr   s    r   r   zNumpyCV2Engine.do_importsN  s)    /0 01 2r   c                     |j                         } | j                  |||fi |}t        j                  t	        |      |       y r6   )r   r   r   imwriterd   )r}   r   rt   r   r|   np_arrays         r   r   zNumpyCV2Engine._saving_hookU  s;    ??$#4##HfdNoNCM8,r   c                    |}|r|j                   t        j                  k(  r| }n|j                  r t        j
                  t        j                  fnt        j                  t        j                  f\  }}t        j                  ||      }t        j                  |      \  }	}
}|
 }
t        j                  |	|
|g      }t        j                  ||      }|r,|j                   t        j                  k7  sJ d       |j                  |      }t        |j                  t        j                   gd            \  }}|rt"        j%                  |j&                  |j(                  dft"        j*                        }|D ]o  }t"        j-                  |j/                         D cg c]  \  }}|j1                  ||       c}}t"        j2                        }t        j5                  ||gd       q t        j7                  |||      }|S c c}}w )Nz=Not sure how to paste with mask on {RGB,BGR}X image using cv2rE   r   )r   dst)r   r   FPDFBitmap_GrayrT   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   rt   r   r   r   r   
convert_toconvert_fromr   r   r   r   r   r   r   r   r   r   r   s                       r   r   z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(
LLLJ?	))I.1aBIIq!Qi0	LLLA	}}(@(@@  B  CB  B@ ,,T2*;T=M=MxOjOjNkwx=M=y*{'Z88V]]FLL!$DbhhOD) 9"$((PSPcPcPe+f1G,=,=a,C+fhjhphp"qTG9a89 !$

94Y
 OI ,gs    HN)r   r   r   r   r   r   r   r   r   r   rg   rg   L  s     -
  r   rg   c                      |         t         j                  dt        j                                  |        t	        j
                  ||d      }|r|j                          |||fay )NzInitializing data for process T)password	autoclose)r   r   rq   getpidr   PdfDocument
init_formsProcObjs)logging_initengine_initinputr  may_init_formsr   enginepdfs           r   _render_parallel_initr  t  sU    N
KK0>?M


UX
FC VV$Hr   c                 F    ||    } |j                   di |} || ||       y )Nr   )render)r   r  r   r  r   rt   s         r   _render_jobr    s*    q6DT[["6"F
1fdr   c                 $    t        | gt          y r6   )r  r  )r   s    r   _render_parallel_jobr    s    Hr   c                       y r6   r   r   r   r   _do_nothingr    s    r   c                 z   | j                   j                         st        d| j                         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	   d
k(  rdnd| _        | j                  d| _        | j                   Et"        st$        sJ d       t"        rt$        r| j                  dk(  rt&        | _        nt(        | _        | j*                  t-        | j                   t(              | _        | j.                  0t-        | j                   t(              xr | j                  dk7  | _        t1               }| j                  r |j2                  d"i t4          |j2                  d"i t6        D ci c]  }t9        | |      s|t9        | |       c} |rt;        j<                  d"i |nd }t1        | j>                  | j@                  | jB                  | jD                  | j                  | jF                  | jH                  | j                  | jJ                  | j*                  | j.                  | jL                  tN        | jP                     || jR                        }| jT                  D ]
  }d|d| <    tW        jX                  | j                   | j                  t        t[                    | j                  | jL                        }t1        | j\                  | j^                        }| j\                  r$| jF                  dk(  rt`        jc                  d       te        |       jg                         }	|	d= |	d= |	d   rd|	d<   t`        ji                  |	        |rt`        ji                  |        | j!                  ||      }
t        | j                        | j                  k  rEt`        jk                  d       |
jm                          | j                  D ]  }to        ||||
        y t`        jk                  d       tq        jr                  | jt                        }t1        |jv                  dfty        jz                  t|        j~                  |      df      }|| j                     \  }}| j                  r| j                  }| jt                  d k(  rt        t        }}|
jm                          nt        |
jl                  }}t1        t        |||j                  | j                  | j                  ||
f!      }t        | j                  t        | j                              } ||fi |5 }t9        ||      } |t        | j                        D ]  } 	 d d d        y c c}w # 1 sw Y   y xY w)#Nz*Output path is not an existing directory: )r
  c              3   >   K   | ]  }d |cxk  xr k  nc   yw)r   Nr   ).0r   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   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_halftonerT   rY   r   bitmap_makerrv   fill_to_strokeT	no_smooth)r   r   r   r   r   )r   r   rJ   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mapr`   r_   )initializerinitargsr   )Joutputis_dir
ValueErrorr   
draw_formslenallr4   setr   r  stemr%  sample_themer   linearr>   have_pilhave_cv2rg   re   rT   
issubclassrY   dictupdateSampleThemeColorSchemeFieldsgetattrr   PdfColorSchemer!  r"  r#  r$  r&  r'  r)  r   rn   r*  r+  no_antialiastypesSimpleNamespacerd   r   r   r   warningvarscopyr   r   r   r  ra   get_contextparallel_strategyPoolr   partialrb   ProcessPoolExecutorparallel_libparallel_mapr  r   r  _inputr  min	processesr  )r   r  	cs_kwargsfrv   r   r-   r~   r|   
print_argsr  r   ctxpool_backends	pool_ctormap_attrr  r  pool_kwargsn_procspoolmap_funcr  r  s                          @r   mainra    s'   ;;Edkk_UVV
DT__
5C#hG444EFF
4::#c$**o..ABB{{)+,0,=,=.CW{{#q1S8ee{{ 8f%ff#h4;;%+?,DO'DO !'C%dooyAZdkkUZFZI	';'I\5F['RVXYJZ74++[\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::$++%*+ 	0A3/	0
 	,-nnT334((F###B$:$:sKUS
 ,D,=,=>	8((H!!V+(3[+L(5v7H7H+L/$k3::t}}doo_egmn

 dnnc$**o6w.+. 	$tX.H2DJJ? 	 	_ \^	 	s   =X,X,9)X11X:)8rq   ro   rG  loggingr   r   pathlibr   multiprocessingra   concurrent.futuresfuturesrb   importlib.utilr   pypdfium2._helpers_helpersr   pypdfium2.internalinternalr   pypdfium2.rawrawr   pypdfium2._cli._parsersr   r   r   r   r	   r=  r>  	getLoggerr   r   r   r@  r   
new_nativenew_foreignrO  rn   rC  rk   rl   rB  rx   rz   re   rf   rg   r  r  r  r  ra  r   r   r   <module>rr     s<   
         $ # %    U4'U4'			8	$Z ((**&Y&&v'7'7'C'CRVW3	 M AC0	&&&&wt< <,4 4n
Ci 
C%l %P% {r   