
    Fg%              3       :   S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
Jr  SSKrSSKrSSKrSSKJr  SSKJrJrJrJrJr  SSKJr  SSKJrJr  SS	KJrJr  SS
K J!r!  SSK"J#r#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*                        S-S\S\(S\+S\+S\
\   S\,S\
\\,      S\+S\-S\,S\+S\
\+   S\.S\.S\.S\,S\+S\+S \,S!\S"\+S#\+S$\+S%\S&S42S' jjr/      S.S(\)S\+S)\
\\,      S\,S*\.S\+S\
\   S&\+4S+ jjr0     S/S(\)S\+S)\
\\,      S\,S*\.S\
\   S&\	\   4S, jjr1g)0zGFunctions that can be used for the most common use-cases for pdf2zh.six    N)StringIO)AnyBinaryIO	ContainerIteratorOptionalcast)Document)HOCRConverterHTMLConverterPDFPageAggregatorTextConverterXMLConverter)ImageWriter)LAParamsLTPage)	PDFDeviceTagExtractor)PDFValueError)PDFPageInterpreterPDFResourceManager)PDFPage)AnyIO
FileOrNameopen_filenameinfoutfpoutput_typecodeclaparamsmaxpagespagespasswordscalerotation
layoutmode
output_dirstrip_controldebugdisable_caching
page_countvfontvcharthreaddoc_enlang_inlang_outservicekwargsreturnc                     U(       a2  [         R                  " 5       R                  [         R                  5        SnU(       a  [	        U5      n[        U(       + S9nSn0 nUS:w  a.  U[        R                  :X  a  [        R                  R                  nUS:X  a  [        UUUUUUUUUUUUS9nOoUS:X  a  [        UUUUUUS9nOZUS:X  a  [        UUUUU
UUS9nODUS	:X  a  [        UUUUUS
9nO0US:X  a  [        U[        [        U5      US9nOSU 3n[!        U5      eUc   e0 n[#        UUU5      nU(       a  [%        U5      n OUn [&        R&                  " [(        R*                  " U UUUU(       + S9U SS9 GH  n!UU!R,                     R/                  5       n"[0        R2                  " U"R4                  [0        R6                  5      R9                  U"R:                  U"R<                  S5      SS2SS2SSS24   n#UR?                  U#[A        U"R:                  S-  5      S-  [B        RD                  RG                  5       (       a  SOSS9S   n$[0        RH                  " U"R:                  U"R<                  45      n%U%RJ                  u  n&n'/ SQn([M        U$RN                  5       GH  u  n)n*U$RP                  [A        U*RR                  5         U(;  d  M/  U*RT                  RW                  5       u  n+n,n-n.[0        RX                  " [A        U+S-
  5      SU'S-
  5      [0        RX                  " [A        U&U.-
  S-
  5      SU&S-
  5      [0        RX                  " [A        U-S-   5      SU'S-
  5      [0        RX                  " [A        U&U,-
  S-   5      SU&S-
  5      4u  n+n,n-n.U)S-   U%U,U.2U+U-24'   GM     [M        U$RN                  5       H  u  n)n*U$RP                  [A        U*RR                  5         U(;   d  M.  U*RT                  RW                  5       u  n+n,n-n.[0        RX                  " [A        U+S-
  5      SU'S-
  5      [0        RX                  " [A        U&U.-
  S-
  5      SU&S-
  5      [0        RX                  " [A        U-S-   5      SU'S-
  5      [0        RX                  " [A        U&U,-
  S-   5      SU&S-
  5      4u  n+n,n-n.SU%U,U.2U+U-24'   GM      U%UU!R,                  '   U!RZ                  U	-   S-  U!l-        UR]                  5       U!l/        URa                  U!R^                  S5        URc                  U!R^                  S5        UU!R,                     Re                  U!R^                  5        URg                  U!5        GM     URi                  5         U$ )a  Parses text from inf-file and writes to outfp file-like object.

Takes loads of optional arguments but the defaults are somewhat sane.
Beware laparams: Including an empty LAParams is not the same as passing
None!

:param inf: a file-like object to read PDF structure from, such as a
    file handler (using the builtin `open()` function) or a `BytesIO`.
:param outfp: a file-like object to write the text to.
:param output_type: May be 'text', 'xml', 'html', 'hocr', 'tag'.
    Only 'text' works properly.
:param codec: Text decoding codec
:param laparams: An LAParams object from pdf2zh.layout. Default is None
    but may not layout correctly.
:param maxpages: How many pages to stop parsing after
:param page_numbers: zero-indexed page numbers to operate on.
:param password: For encrypted PDFs, the password to decrypt.
:param scale: Scale factor
:param rotation: Rotation factor
:param layoutmode: Default is 'normal', see
    pdf2zh.converter.HTMLConverter
:param output_dir: If given, creates an ImageWriter for extracted images.
:param strip_control: Does what it says on the tin
:param debug: Output more logging data
:param disable_caching: Does what it says on the tin
:param other:
:return: nothing, acting as it does on two streams. Use StringIO to get
    strings.
Ncachingtext)
r   r    imagewriterr,   r-   r.   layoutr0   r1   r2   xml)r   r    r9   stripcontrolhtml)r   r$   r&   r    r9   hocr)r   r    r<   tag)r   z1Output type can be text, html, xml or tag but is r!   r#   r7   r   )totalposition       zcuda:0cpu)imgszdevice)abandonfiguretableisolate_formulaformula_caption      ih  z<<>>    )5logging	getLoggersetLevelDEBUGr   r   sysstdoutbufferr   r   r   r   r   r	   r   r   r   lentqdmr   	get_pagespageno
get_pixmapnp
fromstringsamplesuint8reshapeheightwidthpredictinttorchcudais_availableonesshape	enumerateboxesnamesclsxyxysqueezecliprotateget_new_xref	page_xrefupdate_objectupdate_streamset_contentsprocess_pageclose)/r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   modelr0   r1   r2   r3   r9   rsrcmgrrH   r:   msg	obj_patchinterpretertotal_pagespagepiximagepage_layoutboxhwvclsidx0y0x1y1s/                                                  b/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/pdf2zh/high_level.pyextract_text_to_fpr      sR   p $$W]]3K!*- _)<=G"&FFf#**!4

!!f#
 
	#&
 
	!#
 
	&
 
	gtHe'<EJ B+OC  I$Wfi@KJ		'++## 1& T[[!,,.ckk2884<<SZZTUVWXZ[]a_a]aWabMMcjjm$R'$zz66888e " 
 	 GGSZZ+,II!M[../CAa$$SZ0D8FFNN,2bGGC1Ia!4RWWS2a[1Q35OPRPWPWX[\^_`\`XabcdefgdgPhikipipqtuvwyuyz{u{q|}~  @A  BC  @C  jD  D2b!"1BrE"R%K 	 0
 [../CAa  QUU,4FFNN,2bGGC1Ia!4RWWS2a[1Q35OPRPWPWX[\^_`\`XabcdefgdgPhikipipqtuvwyuyz{u{q|}~  @A  BC  @C  jD  D2b!"BrE"R%K 	 0
  t{{{{X-4,,.T^^V4T^^C0t{{((8  &I&L LLNrP   pdf_filepage_numbersr7   c           
         Uc
  [        5       n[        U S5       n[        5        n[        [        U5      n[        US9n	[        XXVS9n
[        X5      n[        R                  " UUUUUS9 H  nUR                  U5        M     UR                  5       sSSS5        sSSS5        $ ! , (       d  f       O= fSSS5        g! , (       d  f       g= f)aI  Parse and return the text contained in a PDF file.

:param pdf_file: Either a file path or a file-like object for the PDF file
    to be worked on.
:param password: For encrypted PDFs, the password to decrypt.
:param page_numbers: List of zero-indexed page numbers to extract.
:param maxpages: The maximum number of pages to parse
:param caching: If resources should be cached
:param codec: Text decoding codec
:param laparams: An LAParams object from pdf2zh.layout. If None, uses
    some default settings that often work well.
:return: a string containing all of the text extracted.
Nrbr6   )r   r    r@   )r   r   r   r	   r   r   r   r   r   rZ   rx   getvalue)r   r#   r   r!   r7   r   r    fpoutput_stringr{   rH   r~   r   s                r   extract_textr      s    , :	x	&"hjM(B$W5wUV(9%%
D $$T*
 %%' /9j	&	&jj	&	&	&s#   C A.B&	C &
B4	0C  
Cc           	   #   T  #    Uc
  [        5       n[        U S5       n[        [        U5      n[	        US9n[        XuS9n[        Xx5      n	[        R                  " UUUUUS9 H(  n
U	R                  U
5        UR                  5       nUv   M*     SSS5        g! , (       d  f       g= f7f)a  Extract and yield LTPage objects

:param pdf_file: Either a file path or a file-like object for the PDF file
    to be worked on.
:param password: For encrypted PDFs, the password to decrypt.
:param page_numbers: List of zero-indexed page numbers to extract.
:param maxpages: The maximum number of pages to parse
:param caching: If resources should be cached
:param laparams: An LAParams object from pdf2zh.layout. If None, uses
    some default settings that often work well.
:return: LTPage objects
Nr   r6   )r    r@   )r   r   r	   r   r   r   r   r   rZ   rx   
get_result)r   r#   r   r!   r7   r    r   resource_managerrH   r~   r   r:   s               r   extract_pagesr      s     ( :	x	&"(B-g>"#3G()9B%%
D $$T*&&(FL
 
'	&	&s   B(A2B	B(
B%!B()r8   utf-8Nr   N g      ?r   normalNFFFr   r   r   r   NNr   r   r   )r   Nr   Tr   N)r   Nr   TN)2__doc__rQ   rU   ior   typingr   r   r   r   r   r	   rf   numpyr]   rY   pymupdfr
   pdf2zh.converterr   r   r   r   r   pdf2zh.imager   pdf2zh.layoutr   r   pdf2zh.pdfdevicer   r   pdf2zh.pdfexceptionsr   pdf2zh.pdfinterpr   r   pdf2zh.pdfpager   pdf2zh.utilsr   r   r   strre   floatboolr   r   r    rP   r   <module>r      s   M  
  E E      % * 4 . C " 9 9 #'&* $!1j	jj j 	j
 x j j IcN#j j j j j j j j j  !j" #j$ %j& 'j( )j, -j. /j0 1j2 3j4 
5j^ -1#'(((((( 9S>*(( 	((
 (( (( x (( 	((Z -1#'%%% 9S>*% 	%
 % x % f%rP   