
    ?pj5                         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JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r   \RB                  " \"5      r#Sr$SSSS	S
S.r%S r&S\4S jr' " S S5      r( " S S\(5      r) " S S\(5      r*g)z
Office Math Markup Language (OMML) to LaTeX converter.

Adapted from https://github.com/xiilei/dwml/blob/master/dwml/omml.py
via MinerU 3.0.0 (MIT License)
    N   )ALNARR	BACKSLASHBLANKBRKCHARSCHRCHR_BOCHR_DEFAULT	D_DEFAULT	F_DEFAULTFUNC
FUNC_PLACELIM_FUNCLIM_TOLIM_UPPPOSPOS_DEFAULTRADRAD_DEFAULTSUBSUPDFMTz<{http://schemas.openxmlformats.org/officeDocument/2006/math}z\mathscr{{{0}}}z\mathfrak{{{0}}}z\mathbb{{{0}}}z\mathsf{{{0}}}z\mathtt{{{0}}})scriptfrakturzdouble-struckz
sans-serif	monospacec                     S n/ nU R                  SS5      n U  HC  nU[        ;   a#  U[        :w  a  UR                  [        U-   5        OUR                  U5        UnME     [        R
                  " U5      $ )Nz\\\)replacer	   r   appendr   join)strslastnew_chrcs       W/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddleocr/_doc2md/math/omml.pyescape_latexr+   E   sf    DG<<t$DJTY.NN9q=)NN1  ::g    c                 @    U b  U(       d  U $ UR                  X 5      $ U$ N)get)keydefaultstores      r*   get_valr3   R   s"    
s8UYYs%88r,   c                   B    \ rS rSrS	S jrS	S jrS	S jrS	S jrS rSr	g)

Tag2MethodY   Nc                     U R                   R                  nUc   UR                  R                  [        S5      nU" U5      nU(       a  U" X5      $ g )N )tag2methr/   tagr#   OMML_NS)selfelmstag	getmethodmethods        r*   call_methodTag2Method.call_methodZ   sD    MM%%	<77??7B/D4$$$r,   c              #     #    [        U5       Hu  n[        UR                  ;  a  M  UR                  R                  [        S5      nU(       a  XB;  a  MG  U R	                  X4S9nUc  U R                  X45      nUc  Mo  XEU4v   Mw     g7f)z-
process children of the elm,return iterable
r8   )r>   N)listr;   r:   r#   rA   process_unknow)r<   r=   include_er>   ts         r*   process_children_list Tag2Method.process_children_listd   s}      s)Bbff$66>>'2.DD/   /Ay''19B- s   BBc                 X    [        5       nU R                  X5       H
  u  pEnXSU'   M     U$ )z)
process children of the elm,return dict
)dictrI   )r<   r=   rF   latex_charsr>   rH   es          r*   process_children_dict Tag2Method.process_children_dictu   s3     f44SBJDQ ! Cr,   c                 Z    [         R                  " S U R                  X5       5       5      $ )z+
process children of the elm,return string
c              3   l   #    U  H*  u  pn[        U[        5      (       d  UO
[        U5      v   M,     g 7fr.   )
isinstancer5   str).0r>   rH   rN   s       r*   	<genexpr>.Tag2Method.process_children.<locals>.<genexpr>   s0      "JJDQ $Az22A>"Js   24)r   r%   rI   )r<   r=   rF   s      r*   process_childrenTag2Method.process_children~   s-     zz"&"<"<S"J
 	
r,   c                     g r.    r<   r=   r>   s      r*   rE   Tag2Method.process_unknow   s    r,   r[   r.   )
__name__
__module____qualname____firstlineno__rA   rI   rO   rX   rE   __static_attributes__r[   r,   r*   r5   r5   Y   s     "	
r,   r5   c                   R    \ rS rSrSrSrSr S rS rS r	S r
S	 r\
\\\\\S
.rSrg)Pr   r8   )chrposbegChrendChrtypeNc                 >    0 U l         U R                  U5      U l        g r.   )_Pr__innerdictrX   textr<   r=   s     r*   __init__Pr.__init__   s    ))#.	r,   c                     U R                   $ r.   rm   r<   s    r*   __str__
Pr.__str__   s    yyr,   c                 :    U R                   R                  US 5      $ r.   )rl   r/   )r<   names     r*   __getattr__Pr.__getattr__   s    ##D$//r,   c                 4    [         U R                  S'   [         $ )Nbrk)r   rl   rn   s     r*   do_brk	Pr.do_brk   s    "%
r,   c                     UR                   R                  [        S5      nX R                  ;   a&  UR	                  [         S35      nX0R
                  U'   g )Nr8   val)r:   r#   r;   _Pr__val_tagsr/   rl   )r<   r=   r>   rH   s       r*   	do_commonPr.do_common   sE    www+??"7)3(A%&T"r,   )r{   rf   rg   rh   ri   rj   )__innerdictrm   )r^   r_   r`   ra   rm   r   rl   ro   rt   rx   r|   r   r9   rb   r[   r,   r*   rd   rd      sH    D;JK'/0 Hr,   rd   c                   *   \ rS rSrSr\rSrSrS r	S r
S r\S 5       rS	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r 0 S\_S\ _S\_S\_S \_S!\_S"\_S#\_S$\_S%\_S&\_S'\_S(\_S)\_S*\_S+\_S,\_S-\0Er!S.r"g)/oMath2Latex   z(
Convert oMath element of omml to latex
)boxsSubsSupsSubSupnumdendegrN   Nc                     [         R                  c   SSKJn  U" SSSS9[         l        [         R                  U l        U R                  U5      U l        g ! [         a    [        S5      ef = f)Nr   )UnicodeToLatexEncoderz[pylatexenc is required for math formula conversion. Install it with: pip install pylatexencz
braces-allkeepF)replacement_latex_protectionunknown_char_policyunknown_char_warning)	r   _encoderpylatexenc.latexencoder   ImportErrorRuntimeErrorurX   _latex)r<   elementr   s      r*   ro   oMath2Latex.__init__   sr    'H $9-9$*%*$K 
 %%++G4  "> s   A A,c                 :    U R                   R                  SS5      $ )Nz   )latexr#   rs   s    r*   rt   oMath2Latex.__str__   s    zz!!$,,r,   c                 l    X R                   ;   a  U R                  U5      $ USS  S:X  a  [        U5      $ g )Nrd   )_oMath2Latex__direct_tagsrX   rd   r\   s      r*   rE   oMath2Latex.process_unknow   s9    %%%((--"#Y$c7Nr,   c                     U R                   $ r.   )r   rs   s    r*   r   oMath2Latex.latex   s    {{r,   c                     U R                  U5      n[        US   R                  [        R                  " S5      [
        S9nUR                  US   5      $ )z
the accent function
accPrACC_VALr1   r2   rN   )rO   r3   rf   r   r/   r
   format)r<   r=   c_dictlatex_ss       r*   do_accoMath2Latex.do_acc   sM     ++C07O)C3
 ~~fSk**r,   c                     U R                  U5      nUS   n[        UR                  [        R                  " S5      [
        S9nUR                  UR                  US   5      -   $ )z
the bar function
barPrBAR_VALr   rN   )rO   r3   rg   r   r/   r   rm   r   r<   r=   r   prr   s        r*   do_baroMath2Latex.do_bar   sT     ++C0G_"&&+//)*DCPwws444r,   c                    U R                  U5      nUS   n[        R                  " S5      n[        UR                  [        R                  " S5      [
        S9n[        UR                  [        R                  " S5      [
        S9nUR                  [        R                  " U(       d  UO
[        U5      US   U(       d  UO
[        U5      S9-   nU$ )z
the delimiter object
dPrnullleftr   rightrN   )r   rm   r   )rO   r   r/   r3   rh   r   ri   rm   r   r   r+   )r<   r=   r   r   r   s_vale_valdelims           r*   do_doMath2Latex.do_d   s     ++C0E]}}V$		9==+@J		9==+AK!(("U(;#$e)<
 

 r,   c                 P    U R                  U5      n[        R                  " U5      $ r.   )rX   r   r   r<   r=   rm   s      r*   do_suboMath2Latex.do_sub  !    $$S)zz$r,   c                 P    U R                  U5      n[        R                  " U5      $ r.   )rX   r   r   r   s      r*   do_supoMath2Latex.do_sup	  r   r,   c                    U R                  U5      nUR                  S5      nUcI  [        R                  S5        [        nUR                  UR                  S5      UR                  S5      S9$ [        UR                  [        [        S9nUR                  UR                  UR                  S5      UR                  S5      S9-   $ )z
the fraction object
fPrz9Missing fPr element in fraction, using default formattingr   r   )r   r   r   )
rO   r/   loggerdebugr   r   r3   rj   r   rm   r   s        r*   do_foMath2Latex.do_f  s     ++C0ZZ:LLTUG>>JJu%JJu% "   "''9A>wwFJJu,=6::eCTUUUr,   c                     U R                  U5      nUR                  S5      nUR                  [        UR                  S5      5      $ )z.
the Function-Apply object (Examples:sin cos)
fNamerN   )rO   r/   r#   r   )r<   r=   r   	func_names       r*   do_funcoMath2Latex.do_func  s=     ++C0JJw'	  VZZ_==r,   c                    / nU R                  U5       H  u  p4nUS:X  au  [        R                  " U5      (       a  UR                  [        U   5        MA  [        R                  SU5        [        U[        5      (       a  UR                  U5        M  M  [        U[        5      (       d  M  UR                  U5        M     [        R                  " U5      n[        U;   a  U$ U[        -   $ )z
the func name
rz0Function not supported, will default to text: %s)rI   r   r/   r$   r   warningrS   rT   r   r%   r   )r<   r=   rM   r>   rH   rN   s         r*   do_fnameoMath2Latex.do_fname%  s     44S9JDQs{88A;;&&tAw/NNJA "!S))#**1- *As##""1% : JJ{#!Oq7Z7r,   c                     U R                  U5      nUS   n[        UR                  5      nUR                  UR	                  US   5      -   $ )z
the Group-Character object

groupChrPrrN   )rO   r3   rf   rm   r   r   s        r*   do_groupchroMath2Latex.do_groupchr9  sG     ++C0L!"&&/wws444r,   c                     U R                  U5      nUR                  S5      nUR                  S5      nU(       a  [        R                  " XCS9$ [        R                  " US9$ )z
the radical object
rN   r   )r   rm   rr   )rO   r/   r   r   r   )r<   r=   r   rm   deg_texts        r*   do_radoMath2Latex.do_radB  sR     ++C0zz#::e$::(66%%400r,   c                    U R                  USS9 VVVs/ s H  u  p#oCPM	     nnnn[        U5      S:X  a_  US   n[        R                  " SU5      nU(       a:  USUR	                  5        R                  5       nUR                  S5      n	U SU	 S3$ U$ [        R                  " [        R                  " U5      S	9$ s  snnnf )
z
the Array object.
rN   rF   r   r   z\\#\s*\(([^)]*)\)\s*$Nz\tag{}rr   )rI   lenresearchstartrstripgroupr   r   r   r%   )
r<   r=   r>   rH   rN   rowsrow	tag_matchformulatag_contents
             r*   do_eqarroMath2Latex.do_eqarrN  s     %)$>$>sF$>$ST$Sjdq$STt9>q'C		":C@I1	 1299;'ooa0!'+b99Jzzsxx~.. Us   B=c                     U R                  USS9n[        R                  " US   5      nU(       d  [        SR	                  US   5      5      eUR	                  UR                  S5      S9$ )z
the Lower-Limit object
rN   limr   rN   zNot support lim {}r   )r   )rO   r   r/   r   r   )r<   r=   t_dictr   s       r*   	do_limlowoMath2Latex.do_limlow_  sc     ++C+F,,vc{+3::6#;GHH>>fjj&7>88r,   c                     U R                  USS9n[        R                  " UR                  S5      UR                  S5      S9$ )z
the Upper-Limit object
r   r   r   rN   )r   rm   )rO   r   r   r/   )r<   r=   r   s      r*   	do_limuppoMath2Latex.do_limuppj  s;     ++C+F~~&**U"3&**S/JJr,   c                 `    U R                  U5      R                  [        S   [        S   5      $ )zQ
the lower limit of the limLow object and the upper limit of the limUpp function
r   r   )rX   r#   r   rn   s     r*   do_limoMath2Latex.do_limq  s*     $$S)11&)VAYGGr,   c                     / nU R                  U5       H'  u  p4nUS:X  a  M  US:X  d  M  UR                  U5        M)     [        R                  " [        R
                  " U5      S9$ )z
the Matrix object
mPrmrrr   )rI   r$   r   r   r   r%   )r<   r=   r   r>   rH   rN   s         r*   do_moMath2Latex.do_mw  sW     44S9JDQu}A	 :
 xxSXXd^,,r,   c           
          [         R                  " U R                  USS9 VVVs/ s H  u  p#oCPM	     snnn5      $ s  snnnf )z
a single row of the matrix m
r   r   )r   r%   rI   )r<   r=   r>   rH   rN   s        r*   do_mroMath2Latex.do_mr  s@     xx!%!;!;C!;!PQ!P:4AQ!PQ
 	
Qs   <c                     / nSnU R                  U5       H8  u  pEnUS:X  a  [        UR                  S[        S9nM'  UR	                  U5        M:     U[
        R                  " U5      -   $ )z
the n-ary object
r8   naryPrz\intr   )rI   r3   rf   r   r$   r   r%   )r<   r=   resbor>   rH   rN   s          r*   do_naryoMath2Latex.do_nary  sa     44S9JDQxQUUG6B

1	 :
 EJJsO##r,   c                    U R                   R                  U5      nUb  U$ U R                  R                  U5      nUR	                  S5      SL aI  UR	                  S5      (       a3  UR                  S5      SL a  UR                  S5      (       a	  SUSS  S3nSU;   a$  UR                  SS5      nUR                  SS5      nU$ )	N{Fr   r   r   
ensuremathz\ensuremath{)_t_dictr/   r   unicode_to_latex
startswithendswithr#   )r<   st_resultout_latex_strs       r*   process_unicodeoMath2Latex.process_unicode  s    <<##A&O//2 LL&((--

35(&&s++a 34A6M=()11/3GM)11#s;Mr,   c                    / n/ nUR                  S[         S35      nU(       a<  U H6  nU R                  U5      nUR                  U5        UR                  U5        M8     [	        [
        R                  " U5      5      n[
        R                  " U5      nSU;  a  SU;   a  UR                  SS5      nSU;  a  SU;   a  UR                  SS5      nUR                  [         S35      n	U	b  U	R                  [         S35      n
U
bh  U
R                  [         S	35      n[        R                  U5      nU(       a4  UR                  5       (       a  UR                  UR                  5       5      nU$ )
zA
Get text from 'r' element,And try convert them to latex symbols
z./rH   r  z\{r   z\}rPrscrr   )findtextr;   r  r$   r+   r   r%   r#   findr/   SCR_TO_LATEXstripr   )r<   r=   _str	_base_str
found_textr  r  proc_strbase_proc_strr  scr_elemscr_vallatex_templates                r*   do_roMath2Latex.do_r  sD    	\\Bwiq/2
 $ 4 4Q 7M*  #  
  

4 01

9-m#(9''s3Hm#(9''s3H hh'#'?xx7)30H#",,'#7!-!1!1'!:!hnn&6&6-44X^^5EFHr,   accr   barsubsupffuncr   groupChrdradeqArrlimLowlimUppr   mr   nary)r   r   )#r^   r_   r`   ra   __doc__r   r  r   r   ro   rt   rE   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r'  r9   rb   r[   r,   r*   r   r      sy    GPMH5$-  +5"  V >8(5
1/"	9KH
-
$* DvT 	v 	v	
 	v 	T 	 	 	K 	T 	v 	 	) 	) 	v  	T!" 	e#$ 	%Hr,   r   )+r7  loggingr   
lxml.etreeetreeET
latex_dictr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	getLoggerr^   r   r;   r  r+   r3   r5   rd   r   r[   r,   r*   <module>r?     s     	       : 
		8	$
H !"&#"
 S 1 1h% %Pm* mr,   