
    h^                     D   S r SSKJr  SSKrSSKJrJrJr  SSKrSSK	r	SSK
J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Jr  \R*                  " \5      r\R0                  " SS5      r\" S	S
5      r\" SS5      r " S S\" SS5      5      r\" S S S S S S S S S9rSJS jr " S S5      r  " S S5      r! " S S\ 5      r"S  r# " S! S"5      r$\" S#S$5      r% " S% S&5      r&S' r' " S( S)5      r(\S* 5       r)\S+ 5       r*\" \*\$S,5      r+\" \*\"S-5      r,\S.:X  Ga  SS/K-J.r.  SSK/r/\." 5       r0\0Rc                  S05        \0Rc                  S1S2\2SS39  \0Rg                  5       r4\ " \4Rj                  \4Rl                  5       r7\&" \)" S45      5      r8\7 GHu  r9\:" S5\9Rv                   S6\9Rx                   S7\9Rz                   S835        \/R|                  " \9R~                  S9 5       H  u  r@rA\:" S:\@R                  R                  S;5      < S<\@R                  S=-   35        \:" S>S?S@SASBSCSDSE9  \A Hm  r?\:" \?R                  \?R                  \?R                  \H" \?R                  5      R                  5       (       a  \H" \?R                  5      OSF\?Rv                  SDSE9  Mo     M     \9R                  (       d  GM$  \:" S>S?SGSBSHSISDSE9  \9R                   H5  rK\:" \KR                  \KR                  \KRx                  \KRv                  SDSE9  M7     GMx     SSS5        gg! , (       d  f       g= f)KaC  
A module for reading dvi files output by TeX. Several limitations make
this not (currently) useful as a general-purpose dvi preprocessor, but
it is currently used by the pdf backend for processing usetex text.

Interface::

  with Dvi(filename, 72) as dvi:
      # iterate over pages:
      for page in dvi:
          w, h, d = page.width, page.height, page.descent
          for x, y, font, glyph, width in page.text:
              fontname = font.texname
              pointsize = font.size
              ...
          for x, y, height, width in page.boxes:
              ...
    )
namedtupleN)	lru_cachepartialwraps)Path)_apicbookDviStatez!pre outer inpage post_post finalePageztext boxes height width descentBoxzx y height widthc                   ^    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)
TextA   a  
A glyph in the dvi file.

The *x* and *y* attributes directly position the glyph.  The *font*,
*glyph*, and *width* attributes are kept public for back-compatibility,
but users wanting to draw the glyph themselves are encouraged to instead
load the font specified by `font_path` at `font_size`, warp it with the
effects specified by `font_effects`, and load the glyph specified by
`glyph_name_or_index`.
c                 X    [        [        S5      5      U R                  R                     $ )N
pdftex.map)
PsfontsMapfind_tex_filefonttexnameselfs    D/var/www/html/env/lib/python3.13/site-packages/matplotlib/dviread.py_get_pdftexmap_entryText._get_pdftexmap_entryM   s!    -56tyy7H7HII    c                     U R                  5       nUR                  cM  [        SR                  UR                  R                  S5      UR                  R                  S5      5      5      e[        UR                  5      $ )z/The `~pathlib.Path` to the font for this glyph.zINo usable font file found for {} ({}); the font may lack a Type-1 versionascii)r   filename
ValueErrorformatpsnamedecoder   r   )r   psfonts     r   	font_pathText.font_pathP   sp     **,??" B$fV]]%9%9'%B%+^^%:%:7%CEF F FOO$$r   c                 .    U R                   R                  $ )zThe font size.)r   sizer   s    r   	font_sizeText.font_size[   s     yy~~r   c                 6    U R                  5       R                  $ )z
The "font effects" dict for this glyph.

This dict contains the values for this glyph of SlantFont and
ExtendFont (if any), read off :file:`pdftex.map`.
)r   effectsr   s    r   font_effectsText.font_effects`   s     ((*222r   c                     U R                  5       nUR                  b"  [        UR                  5      U R                     $ U R                  $ )a<  
Either the glyph name or the native charmap glyph index.

If :file:`pdftex.map` specifies an encoding for this glyph's font, that
is a mapping of glyph indices to Adobe glyph names; use it to convert
dvi indices to glyph names.  Callers can then convert glyph names to
glyph indices (with FT_Get_Name_Index/get_name_index), and load the
glyph using FT_Load_Glyph/load_glyph.

If :file:`pdftex.map` specifies no encoding, the indices directly map
to the font's "native" charmap; glyphs should directly load using
FT_Load_Char/load_char after selecting the native charmap.
)r   encoding
_parse_encglyph)r   entrys     r   glyph_name_or_indexText.glyph_name_or_indexj   sD     ))+>>- 5>>*4::6 	?37::	?r    N)__name__
__module____qualname____firstlineno____doc__r   propertyr$   r(   r,   r3   __static_attributes__r5   r   r   r   r   A   s]    	J % %   3 3 ? ?r   r   zx y font glyph widthc                     U$ Nr5   dvideltas     r   <lambda>rB      s    5r   c                 "    U R                  SSS9$ N   Fsigned	_read_argr?   s     r   rB   rB          #--%-8r   c                 "    U R                  SSS9$ )N   FrF   rH   r?   s     r   rB   rB      rJ   r   c                 "    U R                  SSS9$ )NrL   TrF   rH   r?   s     r   rB   rB      s    #--$-7r   c                 4    U(       a  U R                  USS9$ S $ )NTrF   rH   r?   s     r   rB   rB      s    CMM%M=PDPr   c                 (    U R                  US-   SS9$ )NrE   TrF   rH   r?   s     r   rB   rB      s    S]]519T]Br   c                 (    U R                  US-   SS9$ rD   rH   r?   s     r   rB   rB      s    S]]519U]Cr   c                 .    U R                  US-   US:H  S9$ )NrE      rF   rH   r?   s     r   rB   rB      s    S]]519eqj]Jr   )rawu1u4s4slenslen1ulen1olen1c                 $   ^ ^^^^ UUUUU 4S jnU$ )a)  
Decorator for dispatch by opcode. Sets the values in *table*
from *min* to *max* to this method, adds a check that the Dvi state
matches *state* if not None, reads arguments from the file according
to *args*.

Parameters
----------
table : dict[int, callable]
    The dispatch table to be filled in.

min, max : int
    Range of opcodes that calls the registered function; *max* defaults to
    *min*.

state : _dvistate, optional
    State of the Dvi object in which these opcodes are allowed.

args : list[str], default: ['raw']
    Sequence of argument specifications:

    - 'raw': opcode minus minimum
    - 'u1': read one unsigned byte
    - 'u4': read four bytes, treat as an unsigned number
    - 's4': read four bytes, treat as a signed number
    - 'slen': read (opcode - minimum) bytes, treat as signed
    - 'slen1': read (opcode - minimum + 1) bytes, treat as signed
    - 'ulen1': read (opcode - minimum + 1) bytes, treat as unsigned
    - 'olen1': read (opcode - minimum + 1) bytes, treat as unsigned
      if under four bytes, signed if four bytes
c                    >^ ^ T Vs/ s H  n[         U   PM     snm[        T 5      UU UU4S j5       nTc  UT	T'   U$ [        TTS-   5       H  nT	U   b   eUT	U'   M     U$ s  snf )Nc                    > Tb  U R                   T:w  a  [        S5      eT" U /T Vs/ s H  o"" XT-
  5      PM     snQ76 $ s  snf )Nzstate precondition failed)stater   )r   bytefget_argsmethodminr^   s      r   wrapper,_dispatch.<locals>.decorate.<locals>.wrapper   sK     TZZ5%8 !<==$GX!FX!Ds("3X!FGG!Fs   A
rE   )_arg_mappingr   range)
rb   xrd   ira   argsmaxrc   r^   tables
   `   @r   decorate_dispatch.<locals>.decorate   s    -12TLOT2	v	H 
	H ; E#J
  3A&Qx'''"a '  3s   A$r5   )rl   rc   rk   r^   rj   rm   s   ````` r   	_dispatchro      s    @  Or   c                      \ rS rSrSrS/S-  r\" \\5      rS rS r	S r
S rS	 rS
 rS rS]S jr\" SS\R"                  S9S 5       r\" SS\R"                  SS9S 5       r\" S\R"                  SS9S 5       r\" SS\R"                  SS9S 5       rS r\" S\R"                  SS9S 5       rS  r\" S!5      S" 5       r\" S#\R4                  S$S9S% 5       r\" S&\R"                  S'9S( 5       r\" S)\R"                  S'9S* 5       r\" S+\R"                  S'9S, 5       r\" S-S.\R"                  S/S9S0 5       r\" S1S2\R"                  S3S9S4 5       r \" S5S6\R"                  S3S9S7 5       r!\" S8S9\R"                  S/S9S: 5       r"\" S;S<\R"                  S3S9S= 5       r#\" S>S?\R"                  S3S9S@ 5       r$\" SASB\R"                  S9SC 5       r%\" SDSE\R"                  SS9SF 5       r&\" SGSHSISJ9SK 5       r'\" SLSMSNSJ9SO 5       r(SP r)\" SQ\RT                  SRS9SS 5       r+\" ST\R4                  S'9SU 5       r,\" SV5      SW 5       r-\" SXSYSZ9S[ 5       r.S\r/g)^Dvi   a!  
A reader for a dvi ("device-independent") file, as produced by TeX.

The current implementation can only iterate through pages in order,
and does not even attempt to verify the postamble.

This class can be used as a context manager to close the underlying
file upon exit. Pages can be read via iteration. Here is an overly
simple way to extract text without trying to detect whitespace::

    >>> with matplotlib.dviread.Dvi('input.dvi', 72) as dvi:
    ...     for page in dvi:
    ...         print(''.join(chr(t.glyph) for t in page.text))
N   c                     [         R                  SU5        [        US5      U l        X l        0 U l        [        R                  U l        SU l	        g)z
Read the data from the file named *filename* and convert
TeX's internal units to units of *dpi* per inch.
*dpi* only sets the units and does not limit the resolution.
Use None to return TeX's internal units.
zDvi: %srbN)
_logdebugopenfiledpifonts	_dvistateprer^   _missing_font)r   r   rz   s      r   __init__Dvi.__init__   s?     	

9h'4(	
]]
!r   c                     U $ )z+Context manager enter method, does nothing.r5   r   s    r   	__enter__Dvi.__enter__   s    r   c                 $    U R                  5         g)zH
Context manager exit method, closes the underlying file if it is open.
N)close)r   etypeevalueetraces       r   __exit__Dvi.__exit__   s     	

r   c              #      #    U R                  5       (       a*  U R                  5       v   U R                  5       (       a  M)  gg7f)a  
Iterate through the pages of the file.

Yields
------
Page
    Details of all the text and box objects on the page.
    The Page tuple contains lists of Text and Box tuples and
    the page dimensions, and the Text and Box tuples contain
    coordinates transformed into a standard Cartesian
    coordinate system at the dpi value given when initializing.
    The coordinates are floating point numbers, but otherwise
    precision is not lost and coordinate values are not clipped to
    integers.
N)_read_outputr   s    r   __iter__Dvi.__iter__   s+       jjll,,.  jjlls   =AAc                 p    U R                   R                  (       d  U R                   R                  5         gg)z(Close the underlying file if it is open.N)ry   closedr   r   s    r   r   	Dvi.close  s"    yyIIOO  r   c                 $   [         R                  =p[         R                  * =p4[         R                  * nU R                  U R                  -    Hy  n[	        U[
        5      (       a  Uu  pxpSnOUu  pxpn
UR                  U5      u  p[        X5      n[        X(U	-
  5      n[        X7U
-   5      n[        XHU-   5      n[        XX5      nM{     U R                  b  U R                  nSU l	        U R                  (       d  U R                  (       d  [        / / SSSS9$ U R                  c'  [        U R                  U R                  X1-
  XR-
  XE-
  S9$ U R                  S-  nXE-
  U-  nU R                   VVVVV
s/ s H%  u  pxnp[        Xq-
  U-  XH-
  U-  U-
  UXU-  5      PM'     nnnnnn
U R                   VVV	V
s/ s H%  u  pxp[        Xq-
  U-  XH-
  U-  U-
  X-  X-  5      PM'     nn	nnn
[        UUX1-
  U-  XR-
  U-  US9$ s  sn
nnnnf s  sn
n	nnf )zS
Output the text and boxes belonging to the most recent page.
page = dvi._output()
r   NtextboxeswidthheightdescentgzGRA)npinfr   r   
isinstancer   _height_depth_ofrc   rk   _baseline_vr   rz   r   )r   minxminymaxxmaxy	maxy_pureeltrh   yhwer   gdr   r`   r   r   s                      r   r   Dvi._output  s   
 ffvvgVVG	99tzz)C#s## 
a#& dq,,Q/t<DtU#DtU#DtU#DI)I * '((I#DyyRr1aHH88TYYdjj"i	 $0 0
 HH&#q( (,yy2'0OQ1a afaZ$&!g!5q!qSA'0 	 2 &*ZZ1%/\aA afaZ$&!g!5qsAC@%/ 	 1 U49a-%NA-w@ 	@21s   6,H9,H

c                    S/nSU l          U R                  R                  S5      S   nU R                  U   " X5        U R                  (       a  U R                  R                  5       eU R                  U   R                  nUS:X  a  UR                  US   5        O*US:X  a  UR                  5         OUS:X  a  US==   S-  ss'   U R                   c4  [        [        U S	/ 5      5      S
:X  a  US   S:  a  U R                  U l         US:X  a  gU R                  [        R                  L a  U R                  5         gGM5  )\
Read one page from the file. Return True if successful,
False if there were no more pages.
r   NTrE   _push_pop_downstackrR   rL      F)r   ry   read_dtabler~   to_exceptionr6   appendpoplengetattrvr^   r|   	post_postr   )r   
down_stackr_   names       r   r   	Dvi._read:  s   . S
99>>!$Q'DLLt*!!((5577<<%..Dw!!*R.1 2!#  (GD'2671<"2!+#'66 s{zzY000

) r   c                 ^    [         R                  U R                  R                  U5      SUS9$ )zg
Read and return a big-endian integer *nbytes* long.
Signedness is determined by the *signed* keyword.
bigrF   )int
from_bytesry   r   )r   nbytesrG   s      r   rI   Dvi._read_argi  s&    
 ~~diinnV4eF~KKr   r      )rc   rk   r^   c                 
   U R                  U5        [        U R                  U R                     [        R
                  5      (       a  g U =R                  U R                  U R                     R                  U5      -  sl        g r>   _put_char_realr   r{   r`   r	   _ExceptionInfor   	_width_ofr   chars     r   _set_char_immediateDvi._set_char_immediatep  Y    D!djj(%*>*>??$**TVV$..t44r         )rZ   )rc   rk   r^   rj   c                 
   U R                  U5        [        U R                  U R                     [        R
                  5      (       a  g U =R                  U R                  U R                     R                  U5      -  sl        g r>   r   r   s     r   	_set_charDvi._set_charw  r   r      )rV   rV   )r^   rj   c                 P    U R                  X5        U =R                  U-  sl        g r>   )_put_rule_realr   r   abs      r   	_set_ruleDvi._set_rule~  s    A!!r         c                 &    U R                  U5        g r>   )r   r   s     r   	_put_charDvi._put_char  s    D!r   c                    U R                   U R                     n[        U[        R                  5      (       a  X l        g UR                  cK  U R                  R                  [        U R                  U R                  X!UR                  U5      5      5        g UR                  nUR                  U   R                   H  u  pEpgn[        [        X6R                  5      UR                   UR"                  UR                  S9n	U R                  R                  [        U R                  [        XC5      -   U R                  [        XS5      -   XU	R                  U5      5      5        M     U R$                  R'                  UR                  U   R$                   VVV
Vs/ s HR  u  pEp[)        U R                  [        XC5      -   U R                  [        XS5      -   [        X5      [        X5      5      PMT     snn
nn5        g s  snn
nnf )Nscaletfmr   vf)r{   r`   r   r	   r   r~   _vfr   r   r   r   r   r   _scaleDviFont_mul2012_tfmr   r   extendr   )r   r   r   r   rh   r   r`   r   r   newfr   r   s               r   r   Dvi._put_char_real  s   zz$&&!dE0011!%XXIIT$&&$&&$"&.."68 9 KKE!%$!4!4aAXeXX%>#$66199H		  dffx/A&A&*ffx/A&A&*t~~a/@"B C "5 JJ 26$1E1EG 2F:1  #466HQ,>#>#'66HQ,>#>#+A#5x7I K 2FG H Gs   AG>   c                 &    U R                  X5        g r>   )r   r   s      r   	_put_ruleDvi._put_rule  s    A!r   c                     US:  aB  US:  a;  U R                   R                  [        U R                  U R                  X5      5        g g g Nr   )r   r   r   r   r   r   s      r   r   Dvi._put_rule_real  s9    q5QUJJc$&&$&&!78 5r      c                     g r>   r5   r   _s     r   _nopDvi._nop  s    r      )rV   rV   rV   rV   rV   rV   rV   rV   rV   rV   rV   c                     [         R                  U l        S=U l        =U l        =U l        =U l        =U l        U l        / U l	        / U l
        / U l        g r   )r|   inpager^   r   r   r   rh   r   zr   r   r   )r   c0c1c2c3c4c5c6c7c8c9ps               r   _bopDvi._bop  sN    %%
>????$&?46?DFTV
	
r   r   )r^   c                 J    [         R                  U l        U ?U ?U ?U ?U ?U ?U ?	g r>   )
r|   outerr^   r   r   r   rh   r   r   r   r   s     r   _eopDvi._eop  s*    __
FDFDFDFDFDFDJr      c                     U R                   R                  U R                  U R                  U R                  U R
                  U R                  U R                  45        g r>   )r   r   r   r   r   rh   r   r   r   s     r   r   	Dvi._push  s9    

466466466466466466JKr      c                     U R                   R                  5       u  U l        U l        U l        U l        U l        U l        g r>   )r   r   r   r   r   rh   r   r   r   s     r   r   Dvi._pop  s+    9=9I6r         )rX   c                 .    U =R                   U-  sl         g r>   )r   )r   r   s     r   _right
Dvi._right      !r         )rW   c                 T    Ub  Xl         U =R                  U R                   -  sl        g r>   )r   r   )r   new_ws     r   _right_wDvi._right_w      F$&&r         c                 T    Ub  Xl         U =R                  U R                   -  sl        g r>   )rh   r   )r   new_xs     r   _right_xDvi._right_x  r$  r         c                 .    U =R                   U-  sl         g r>   )r   )r   r   s     r   r   	Dvi._down  r  r         c                 T    Ub  Xl         U =R                  U R                   -  sl        g r>   )r   r   )r   new_ys     r   _down_yDvi._down_y  r$  r         c                 T    Ub  Xl         U =R                  U R                   -  sl        g r>   )r   r   )r   new_zs     r   _down_zDvi._down_z  r$  r         c                     Xl         g r>   r`   )r   ks     r   _fnt_num_immediateDvi._fnt_num_immediate  s    r         c                     Xl         g r>   r>  )r   new_fs     r   _fnt_numDvi._fnt_num  s    r         )rY   )rc   rk   rj   c                     U R                   R                  U5      n[        R                  SSR	                  U Vs/ s H#  nSUs=::  a  S:  a  O  O[        U5      OSU-  PM%     sn5      5        g s  snf )Nz!Dvi._xxx: encountered special: %s     r   z<%02x>)ry   r   rv   rw   joinchr)r   datalenspecialchs       r   _xxxDvi._xxx  sf    ))..)

/GG&(& !#b3SWHrMA&( )	*(s   *A1      )rZ   rU   rU   rU   rT   rT   c                 *    U R                  XX4XV5        g r>   )_fnt_def_real)r   r?  csr   r   ls          r   _fnt_defDvi._fnt_def  s    1q,r   c                    U R                   R                  XV-   5      nXv* S  R                  S5      n [        U5      n	US:w  a-  U	R                  S:w  a  X)R                  :w  a  [        SU 35      e [        U5      n[        X9X{S9U R                  U'   g ! [         a6  n
[
        R                  R                  U
5      U R                  U'    S n
A
g S n
A
ff = f! [         a    S n Nif = f)Nr   r   ztfm checksum mismatch: r   )ry   r   r"   _tfmfileFileNotFoundErrorr	   r   from_exceptionr{   checksumr   _vffiler   )r   r?  rX  rY  r   r   rZ  nfontnamer   excr   s               r   rW  Dvi._fnt_def_real  s    IINN15!RS6==)		8$C 6clla'A,=6qc:;;	"B  a!C

1 ! 	 "00??DDJJqM	 ! 	B	s)   B 2C 
C,CCC'&C'   )rT   rU   rU   rU   rT   c                     U R                   R                  U5        US:w  a  [        SU 35      eUS:w  d  US:w  a  [        S5      eUS:w  a  [        S5      e[        R                  U l        g )N   zUnknown dvi format ii  ;zNonstandard units in dvi file  z%Nonstandard magnification in dvi file)ry   r   r   r|   r  r^   )r   ri   numdenmagr?  s         r   _preDvi._pre  sh    		q621#677(?c\1<== $;DEE __
r      c                 .    [         R                  U l        g r>   )r|   r   r^   r   s     r   _post	Dvi._post  s    ((
r      c                     [         er>   )NotImplementedErrorr   s     r   
_post_postDvi._post_post  s    !!r         )rc   rk   c                 $    [        SSU-    35      e)Nzunknown command: byte ry  )r   )r   offsets     r   
_malformedDvi._malformed!  s    1#,@AAr   )r   r~   r   rz   r`   ry   r{   r   r   r^   r   r   r   rh   r   r   )F)0r6   r7   r8   r9   r:   r   r   ro   r   r   r   r   r   r   r   rI   r|   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r  r"  r)  r   r3  r9  r@  rF  rR  r[  rW  r}   rn  rr  rw  r}  r<   r5   r   r   rq   rq      s    fslG	7+I"!&
+@Z-^L 1#Y%5%565 75 3Cy'7'7jI5 J5 s)**> ? 3Cy'7'7jI" J"H( s)**>" ?"9 s^  s)//
; < s)**+G ,G s)**+L ,L s)**+J ,J 3Cy'7'7jI J 3Cy'7'7iH I
 3Cy'7'7iH I
 3Cy'7'7jI J 3Cy'7'7iH I
 3Cy'7'7iH I
 3Cy'7'78 9 3Cy'7'7jI J 3Cj1* 2* 3C&MN- O-D* s)--.LM% N%" s)//*) +)
 s^" " 3C B !Br   rq   c                   @    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
rg)r   i&  a  
Encapsulation of a font that a DVI file can refer to.

This class holds a font's texname and size, supports comparison,
and knows the widths of glyphs in the same units as the AFM file.
There are also internal attributes (for use by dviread.py) that
are *not* used for comparison.

The size is in Adobe points (converted from TeX points).

Parameters
----------
scale : float
    Factor by which the font is scaled from its natural size.
tfm : Tfm
    TeX font metrics for this font
texname : bytes
   Name of the font as used internally by TeX and friends, as an ASCII
   bytestring.  This is usually very different from any external font
   names; `PsfontsMap` can be used to find the external name of the font.
vf : Vf
   A TeX "virtual font" file, or None if this font is not virtual.

Attributes
----------
texname : bytes
size : float
   Size of the font in Adobe points, converted from the slightly
   smaller TeX points.
widths : list
   Widths of glyphs in glyph-space units, typically 1/1000ths of
   the point size.

)r   r'   widthsr   r   r   c                 \   [         R                  " [        US9  Xl        X l        X0l        X@l        US-  U l         [        UR                  5      S-   n[        U5       Vs/ s H%  nSUR                  R                  US5      -  S-	  PM'     snU l        g ! [         a    Sn NPf = fs  snf )N)r   g]e>rE   r   rj     )r   check_isinstancebytesr   r   r   r   r'   rk   r   r   rg   getr  )r   r   r   r   r   ncharsr   s          r   r   DviFont.__init__K  s    eW5	34		^a'F $)=2#04 SYY]]433:#02  	F	2s   B ",B)B&%B&c                     [        U 5      [        U5      L =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r>   )typer   r'   r   others     r   __eq__DviFont.__eq__Y  sD    T
d5k) NLLEMM1N6:ii5::6M	Or   c                 .    U R                  U5      (       + $ r>   )r  r  s     r   __ne__DviFont.__ne__]  s    ;;u%%%r   c                 N    S[        U 5      R                   SU R                   S3$ )N<z: >)r  r6   r   r   s    r   __repr__DviFont.__repr__`  s&    4:&&'r$,,q99r   c                     U R                   R                  R                  US5      nUb  [        X R                  5      $ [
        R                  SXR                  5        g)zWidth of char in dvi units.Nz No width for char %d in font %s.r   )r   r   r  r   r   rv   rw   r   )r   r   r   s      r   r   DviFont._width_ofc  sG    		##D$/E;;//

5t\\Jr   c                    / nU R                   R                  S4U R                   R                  S44 Hr  u  p4UR                  US5      nUc4  [        R                  SXAU R                  5        UR                  S5        MN  UR                  [        XPR                  5      5        Mt     [        R                  " SU R                  5      (       a  US:X  a  SUS'   U$ )z&Height and depth of char in dvi units.r   depthNzNo %s for char %d in font %sr   s	   ^cmsy\d+$r   )r   r   r  r  rv   rw   r   r   r   r   rematch)r   r   resultmetricr   values         r   r   DviFont._height_depth_ofk  s    "ii..9"iioow79LFJJtT*E}

9t||5a hukk:;9 88M4<<00TQYF2Jr   )r   r   r   r'   r   r  N)r6   r7   r8   r9   r:   	__slots__r   r  r  r  r   r   r<   r5   r   r   r   r   &  s/    !D GI2O&:r   r   c                   J   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	rU =r$ )
Vfi  a  
A virtual font (\*.vf file) containing subroutines for dvi files.

Parameters
----------
filename : str or path-like

Notes
-----
The virtual font format is a derivative of dvi:
http://mirrors.ctan.org/info/knuth/virtual-fonts
This class reuses some of the machinery of `Dvi`
but replaces the `_read` loop and dispatch mechanism.

Examples
--------
::

    vf = Vf(filename)
    glyph = vf[code]
    glyph.text, glyph.boxes, glyph.width
c                    > [         TU ]  US5         S U l        0 U l        U R	                  5         U R                  5         g ! U R                  5         f = fr   )superr   _first_font_charsr   r   )r   r   	__class__s     r   r   Vf.__init__  sB    1%	#DDKJJLJJLDJJLs   A Ac                      U R                   U   $ r>   )r  )r   codes     r   __getitem__Vf.__getitem__  s    {{4  r   c                 *   S=pS=p4 U R                   R                  S5      S   nU R                  [        R                  L a  U R                   R                  5       S-
  nXb:X  a  U R                  X5        S=n=pOFXb:  a  [        S5      eUS;   d  US:  a  [        SU S35      e[        R                  U   " X5        M  US	:  aL  UnU R                  S5      nU R                  S
5      nU R                  U5      n[        R                  U l        GO~US	:X  aF  U R                  S5      nU R                  S5      nU R                  S5      nU R                  U5        GO2SUs=::  a  S::  a  O  OU R                  US	-
  US:H  5      nU R                  S5      nU R                  S5      n	U R                  S5      n
U R                  S5      nU R                  S5      nU R                  XxXX5        U R                  c  Xpl        OUS:X  as  U R                  S5      nU R                  S5      nU R                   R                  U5      nU R                  S5      nU R                  S5      nU R                  XUU5        OUS:X  a  g[        SU 35      eGM  )r   NrE   r   z!Packet length mismatch in vf file)r   r   rT  zInappropriate opcode z in vf filerI  rR   rL   rU  rg  rp  zUnknown vf opcode )ry   r   r^   r|   r   tell_finalize_packetr   rq   r   rI   _init_packetrW  r  rn  )r   packet_charpacket_ends
packet_lenpacket_widthr_   byte_atr?  rX  rY  r   r   rZ  ri   rh   csdss                    r   r   Vf._read  s[   
 %)($((
99>>!$Q'DzzY---))..*1,)))+D>BBJB|*$%HIIz)TS[(+@k)RSSKK%d1 cz!
"nnQ/#~~a0"//5&--
!^^A.
"nnQ/#~~a0!!*-##NN4#:ts{;NN1%NN1%NN1%NN1%NN1%""1q4##+'($NN1%NN1%IINN1%^^A&^^A&		!B' #5dV!<==c r   c                 <   U R                   [        R                  :w  a  [        S5      eS=U l        =U l        =U l        =U l        =U l        U l	        / U l
        / U l        / U l        U R                  U l        S U l        U R                   R#                  5       U-   $ )NzMisplaced packet in vf filer   )r^   r|   r  r   r   r   r   rh   r   r   r   r   r   r  r`   r~   ry   r  )r   pls     r   r  Vf._init_packet  s    ::(:;;>????$&?46?DFTV
	
!!!yy~~"$$r   c                     U R                   (       d.  [        U R                  U R                  US S S9U R                  U'   [
        R                  U l        g )Nr   )r~   r   r   r   r  r|   r  r^   )r   r  r  s      r   r  Vf._finalize_packet  s?    !!'+YYdjjT(+DKK$ __
r   c                     U R                   [        R                  La  [        S5      eUS:w  a  [        SU 35      e[	        U5      (       a  [
        R                  SU5        [        R                  U l         g )Nz pre command in middle of vf filerr   zUnknown vf format zvf file comment: %s)r^   r|   r}   r   r   rv   rw   r  )r   ri   rh   r  r  s        r   rn  Vf._pre  s[    ::Y]]*?@@81!566q66JJ,a0__
r   )r  r  r~   r   r`   r   r   r^   r   r   r   rh   r   r   )r6   r7   r8   r9   r:   r   r  r   r  r  rn  r<   __classcell__)r  s   @r   r  r    s,    .!8>t	%%% %r   r  c                     X-  S-	  $ )z1Multiply two numbers in 20.12 fixed point format.r  r5   )num1num2s     r   r   r     s     I"r   c                   "    \ rS rSrSrSrS rSrg)Tfmi  a  
A TeX Font Metric file.

This implementation covers only the bare minimum needed by the Dvi class.

Parameters
----------
filename : str or path-like

Attributes
----------
checksum : int
   Used for verifying against the dvi file.
design_size : int
   Design size of the font (unknown units)
width, height, depth : dict
   Dimensions of each character, need to be scaled by the factor
   specified in the dvi file. These are dicts because indexing may
   not start from 0.
)ra  design_sizer   r   r  c           
         [         R                  SU5        [        US5       nUR                  S5      n[        R
                  " SUSS 5      u  pEpgp[         R                  SXEXgX5        UR                  SU-  5      n
[        R
                  " S	U
S S
 5      u  U l        U l        UR                  SXe-
  S-   -  5      n[        R
                  " SU S3UR                  SU-  5      5      n[        R
                  " SU S3UR                  SU-  5      5      n[        R
                  " SU	 S3UR                  SU	-  5      5      nS S S 5        0 U l        0 U l	        0 U l
        [        [        WWS-   5      5       HU  u  nnWSU-     nUSU-  S-      nWU   U R                  U'   WUS-	     U R                  U'   WUS-     U R                  U'   MW     g ! , (       d  f       N= f)Nzopening tfm file %sru      z!6Hri     z(lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%drL   z!2I   rE   !ri      )rv   rw   rx   r   structunpackra  r  r   r   r  	enumeraterg   )r   r   ry   header1lhbcecnwnhndheader2	char_infor  heightsdepthsidxr   byte0byte1s                      r   r   Tfm.__init__  s   

((3(D!TiimG%+]]5'!B-%H"BBBJJArr/ii"oG.4mmE72A;.O+DM4+		!RU1W+.I]]Qrd!9dii"o>Fmmat1Ityy2?G]]Qrd!9dii"o>F " 

"5RT?3ICae$EaeAg&E%e}DJJt '
 3DKK%eck2DJJt 4 "!s   D#G
G#)ra  r  r  r   r   N)r6   r7   r8   r9   r:   r  r   r<   r5   r   r   r  r    s    ( HI3r   r  PsFontz(texname psname effects encoding filenamec                   8    \ rS rSrSrSr\S 5       rS rS r	Sr
g)	r   i5  a  
A psfonts.map formatted file, mapping TeX fonts to PS fonts.

Parameters
----------
filename : str or path-like

Notes
-----
For historical reasons, TeX knows many Type-1 fonts by different
names than the outside world. (For one thing, the names have to
fit in eight characters.) Also, TeX's native fonts are not Type-1
but Metafont, which is nontrivial to convert to PostScript except
as a bitmap. While high-quality conversions to Type-1 format exist
and are shipped with modern TeX distributions, we need to know
which Type-1 fonts are the counterparts of which native fonts. For
these reasons a mapping is needed from internal font names to font
file names.

A texmf tree typically includes mapping files called e.g.
:file:`psfonts.map`, :file:`pdftex.map`, or :file:`dvipdfm.map`.
The file :file:`psfonts.map` is used by :program:`dvips`,
:file:`pdftex.map` by :program:`pdfTeX`, and :file:`dvipdfm.map`
by :program:`dvipdfm`. :file:`psfonts.map` might avoid embedding
the 35 PostScript fonts (i.e., have no filename for them, as in
the Times-Bold example above), while the pdf-related files perhaps
only avoid the "Base 14" pdf fonts. But the user may have
configured these files differently.

Examples
--------
>>> map = PsfontsMap(find_tex_file('pdftex.map'))
>>> entry = map[b'ptmbo8r']
>>> entry.texname
b'ptmbo8r'
>>> entry.psname
b'Times-Bold'
>>> entry.encoding
'/usr/local/texlive/2008/texmf-dist/fonts/enc/dvips/base/8r.enc'
>>> entry.effects
{'slant': 0.16700000000000001}
>>> entry.filename
)	_filename	_unparsed_parsedc                 `   [         R                  U 5      n[        R                  " U5      Ul        [        US5       n0 Ul        U HC  nUR                  SS5      S   nUR                  R                  U/ 5      R                  U5        ME     S S S 5        0 Ul
        U$ ! , (       d  f       N= f)Nru       rE   r   )object__new__osfsdecoder  rx   r  split
setdefaultr   r  )clsr   r   ry   linetfmnames         r   r  PsfontsMap.__new__f  s    ~~c"X.
 (D!TDN**T1-a0))'26==dC  "
  "!s   AB
B-c                 Z   [        U[        5      (       d   eXR                  ;   a:  U R                  R                  U5       H  nU R	                  U5      (       d  M    O    U R
                  U   $ ! [         a/    [        SUR                  S5      < SU R                  < S35      S ef = f)NzWAn associated PostScript font (required by Matplotlib) could not be found for TeX font r   z in zo; this problem can often be solved by installing a suitable PostScript font package in your TeX package manager)
r   r  r  r   _parse_and_cache_liner  KeyErrorLookupErrorr"   r  )r   r   r  s      r   r  PsfontsMap.__getitem__v  s    '5))))nn$**73--d33 4	.<<(( 	.33:>>'3J2M Nnn' ("#$
 *..	.s   "A1 19B*c           	         U(       a  UR                  S5      (       a  gS=n=n=n=pVS=n=p[        R                  " SU5      n
U
 H  nUR                  5       u  pU(       a  UR                  S5      (       a  USS nM9  UR                  S5      (       a  USS nMV  UR                  S5      (       aX  US	S =(       d,    [	        [        S[	        U
5      R                  5       5      5      nUR                  S
5      (       a  UnM  UnSnM  Uc  UnM  Uc  UnM  M  U(       d  M  UnM     0 nU(       a`  [        UR                  5       5      nU HA  nUS:X  a  [        [	        U5      5      US'   M"  US:X  d  M*  [        [	        U5      5      US'   MC     Ub2  UR                  S5      (       a  Sn	OUR                  S5      (       d  SnOUb  SnU	(       a  U(       a  Uc  gU(       d  SU;   d  SU;   a  g[        UR                  SS5      5      S	:  a  g[        UR                  SS5      5      S:  a  gUc  UnUb  [        U5      nUb  [        U5      n[        X#UXVS9U R                  U'   g)a  
Parse a line in the font mapping file.

The format is (partially) documented at
http://mirrors.ctan.org/systems/doc/pdftex/manual/pdftex-a.pdf
https://tug.org/texinfohtml/dvips.html#psfonts_002emap
Each line can have the following fields:

- tfmname (first, only required field),
- psname (defaults to tfmname, must come immediately after tfmname if
  present),
- fontflags (integer, must come immediately after psname if present,
  ignored by us),
- special (SlantFont and ExtendFont, only field that is double-quoted),
- fontfile, encodingfile (optional, prefixed by <, <<, or <[; << always
  precedes a font, <[ always precedes an encoding, < can precede either
  but then an encoding file must have extension .enc; < and << also
  request different font subsetting behaviors but we ignore that; < can
  be separated from the filename by whitespace).

special, fontfile, and encodingfile can appear in any order.
)r     %   *   ;   #NFs   "([^"]*)(?:"|$)|(\S+)s   <<ri  s   <[   <rE   s   .encTs	   SlantFontslants
   ExtendFontr   )s   .ttfs   .ttcs   .otfr   )r   r!   r+   r/   r   )
startswithr  finditergroupsnextfilterendswithreversedr  floatabsr  r   r  r  )r   r  r  basenamerP  encodingfilefontfileis_subsettedis_t1is_truetypematchesr  quotedunquotedwordr+   wordss                    r   r   PsfontsMap._parse_and_cache_line  se   8 t'EFFAEEE(EWE|-222u++7>E$||~F&&u--'|H((//#+AB<L((.. ! FtT']-A-A-C DE	 
 }}W--'+#''+_&G%'H & / 0 W]]_-E<'',T%['9GG$]*(-d5k(:GH%	    !344"&&w//!E<L,@'W,G0Cw{{7A&'!+w{{8Q'(1,H#(6L$X.H &g!!6W r   )r  r  r  N)r6   r7   r8   r9   r:   r  r   r  r  r  r<   r5   r   r   r   r   5  s.    *V 6I
  . ]r   r   c                    [         R                  " SS[        U 5      R                  SS95      n[         R                  " SU5      R                  S5      nUR                  5        Vs/ s H  o3(       d  M  UPM     nn[        S U 5       5      (       a  U Vs/ s H  o3SS PM	     sn$ [        S	U  S
35      es  snf s  snf )a  
Parse a \*.enc file referenced from a psfonts.map style file.

The format supported by this function is a tiny subset of PostScript.

Parameters
----------
path : `os.PathLike`

Returns
-------
list
    The nth entry of the list is the PostScript glyph name of the nth
    glyph.
z%.*rK  r   )r/   z(?s)\[(.*)\]rE   c              3   B   #    U  H  oR                  S 5      v   M     g7f)/N)r  ).0r  s     r   	<genexpr>_parse_enc.<locals>.<genexpr>  s     
2ED??3Es   NzFailed to parse z as Postscript encoding)	r  subr   	read_textsearchgroupr  allr   )pathno_commentsarrayr  liness        r   r0   r0     s      &&DJ$8$8'$8$JKKIIo{399!<E#kkm4mdtTmE4

2E
222%*+UTQRU+++D61HIJJ	 5+s   '
B85B8B=c                   0    \ rS rSr\S 5       rS rS rSrg)_LuatexKpsewhichi  c                 Z    [         R                  U 5      nUR                  5       Ul        U$ r>   )r  r  	_new_proc_proc)r  r   s     r   r  _LuatexKpsewhich.__new__   s#    ~~c"^^%
r   c           	          [         R                  " SS[        [        R                  " S5      5      /[         R
                  [         R
                  S9$ )Nluatexz	--luaonlyzkpsewhich.lua)stdinstdout)
subprocessPopenstrr	   _get_data_pathPIPEr   s    r   r#  _LuatexKpsewhich._new_proc  s?    {%%o679//*//; 	;r   c                    U R                   R                  5       b  U R                  5       U l         U R                   R                  R	                  [
        R                  " U5      S-   5        U R                   R                  R                  5         U R                   R                  R                  5       R                  5       nUS:X  a  S $ [
        R                  " U5      $ )N   
s   nil)r$  pollr#  r(  writer  fsencodeflushr)  readlinerstripr  )r   r   outs      r   r  _LuatexKpsewhich.search  s    ::??()DJ

r{{84u<=

 jj((*113f}t:"++c*::r   )r$  N)	r6   r7   r8   r9   r   r  r#  r  r<   r5   r   r   r!  r!    s     
;;r   r!  c                    [        U [        5      (       a  U R                  SSS9n  [        5       nU(       a  UR                  U 5      nOt[        R                  S:X  a  0 [        R                  ESS0ESS.nO[        R                  " 5       SS	.n [        R                  " S
U /[        40 UD6R                  S5      nU(       a  U$ [	        SU < S35      e! [         a    Sn Nf = f! [        [        4 a    Sn N?f = f)a  
Find a file in the texmf tree using kpathsea_.

The kpathsea library, provided by most existing TeX distributions, both
on Unix-like systems and on Windows (MikTeX), is invoked via a long-lived
luatex process if luatex is installed, or via kpsewhich otherwise.

.. _kpathsea: https://www.tug.org/kpathsea/

Parameters
----------
filename : str or path-like

Raises
------
FileNotFoundError
    If the file is not found.
zutf-8replace)errorsNwin32command_line_encoding)envr/   surrogateescape)r/   r<  	kpsewhich
z:Matplotlib's TeX implementation searched for a file named z* in your texmf tree, but could not find it)r   r  r"   r!  r_  r  sysplatformr  environgetfilesystemencodingr	   _check_and_log_subprocessrv   r7  RuntimeError)r   lkr  kwargss       r   r   r     s   . (E""??79?= 
yy"<<7" N

M,CWM")+F #&";";"= 13F	33[(4K48D<BDVD\  HlDFG 	G5  ( "<0 	D	s#   
C -C) C&%C&)C>=C>c                 (    U " [        X!-   5      5      $ r>   )r   )r  suffixr   s      r   	_fontfilerM  P  s    }W-.//r   z.tfmz.vf__main__)ArgumentParserr   rz   ?)nargsr  defaultr   z=== new page === (w: z, h: z, d: )c                     U R                   $ r>   )r   )r   s    r   rB   rB   g  s    DIIr   zfont: zlatin-1z	scale: i   rh   r   r1   rN  r   z(glyphs)	)sep.r   rK  z(boxes))NN)rS   )Lr:   collectionsr   enum	functoolsr   r   r   loggingr  pathlibr   r  r  r*  rC  numpyr   
matplotlibr   r	   	getLoggerr6   rv   Enumr|   r   r   r   dictrf   ro   rq   r   r  r   r  r  r   r0   r!  r   rM  r^  rb  argparserO  	itertoolsparseradd_argumentr  
parse_argsrj   r   rz   r@   fontmappageprintr   r   r   groupbyr   r   r  r   r"   r   rh   r   r1   rN  isprintabler   boxr5   r   r   <module>rm     s2  & #  / /  	  	   
  "", IIj"EF	 &;<*+:?:f45 :?D  887	P
B
C K#*/dYB YBx
X Xvw% w%v-3 -3` 
HH	In nbK2; ;, 7G 7Gt 0 0 9c6*
)R
' z'F

#
SudCD	T]]DHH	%]<89D E$++eDLL>L M(00II5 7et||229=@ A  $g 568 9c3ZTJ!D$&&$&&$**-0_-H-H-J-J#djj/"**$0 " 7 zzzc3S"iTB::C#%%

CII4H &  
&	%  
&	%s   <D0L1AL
L