88import copy
99import enum
1010import functools
11+ import itertools
1112import logging
1213import math
1314import os
@@ -409,14 +410,14 @@ def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float:
409410 metrics = self .get_metrics (
410411 fontname , mpl .rcParams ['mathtext.default' ], 'x' , fontsize , dpi )
411412 return metrics .iceberg
412- xHeight = (pclt ['xHeight' ] / 64.0 ) * (fontsize / 12.0 ) * (dpi / 100.0 )
413- return xHeight
413+ x_height = (pclt ['xHeight' ] / 64 ) * (fontsize / 12 ) * (dpi / 100 )
414+ return x_height
414415
415416 def get_underline_thickness (self , font : str , fontsize : float , dpi : float ) -> float :
416417 # This function used to grab underline thickness from the font
417418 # metrics, but that information is just too un-reliable, so it
418419 # is now hardcoded.
419- return ((0.75 / 12.0 ) * fontsize * dpi ) / 72.0
420+ return ((0.75 / 12 ) * fontsize * dpi ) / 72
420421
421422 def get_kern (self , font1 : str , fontclass1 : str , sym1 : str , fontsize1 : float ,
422423 font2 : str , fontclass2 : str , sym2 : str , fontsize2 : float ,
@@ -1226,21 +1227,13 @@ def kern(self) -> None:
12261227 linked list.
12271228 """
12281229 new_children = []
1229- num_children = len (self .children )
1230- if num_children :
1231- for i in range (num_children ):
1232- elem = self .children [i ]
1233- if i < num_children - 1 :
1234- next = self .children [i + 1 ]
1235- else :
1236- next = None
1237-
1238- new_children .append (elem )
1239- kerning_distance = elem .get_kerning (next )
1240- if kerning_distance != 0. :
1241- kern = Kern (kerning_distance )
1242- new_children .append (kern )
1243- self .children = new_children
1230+ for elem0 , elem1 in itertools .zip_longest (self .children , self .children [1 :]):
1231+ new_children .append (elem0 )
1232+ kerning_distance = elem0 .get_kerning (elem1 )
1233+ if kerning_distance != 0. :
1234+ kern = Kern (kerning_distance )
1235+ new_children .append (kern )
1236+ self .children = new_children
12441237
12451238 def hpack (self , w : float = 0.0 ,
12461239 m : T .Literal ['additional' , 'exactly' ] = 'additional' ) -> None :
@@ -1534,20 +1527,18 @@ class AutoHeightChar(Hlist):
15341527
15351528 def __init__ (self , c : str , height : float , depth : float , state : ParserState ,
15361529 always : bool = False , factor : float | None = None ):
1537- alternatives = state .fontset .get_sized_alternatives_for_symbol (
1538- state .font , c )
1530+ alternatives = state .fontset .get_sized_alternatives_for_symbol (state .font , c )
15391531
1540- xHeight = state .fontset .get_xheight (
1541- state .font , state .fontsize , state .dpi )
1532+ x_height = state .fontset .get_xheight (state .font , state .fontsize , state .dpi )
15421533
15431534 state = state .copy ()
15441535 target_total = height + depth
15451536 for fontname , sym in alternatives :
15461537 state .font = fontname
15471538 char = Char (sym , state )
15481539 # Ensure that size 0 is chosen when the text is regular sized but
1549- # with descender glyphs by subtracting 0.2 * xHeight
1550- if char .height + char .depth >= target_total - 0.2 * xHeight :
1540+ # with descender glyphs by subtracting 0.2 * x_height
1541+ if char .height + char .depth >= target_total - 0.2 * x_height :
15511542 break
15521543
15531544 shift = 0.0
@@ -1574,8 +1565,7 @@ class AutoWidthChar(Hlist):
15741565
15751566 def __init__ (self , c : str , width : float , state : ParserState , always : bool = False ,
15761567 char_class : type [Char ] = Char ):
1577- alternatives = state .fontset .get_sized_alternatives_for_symbol (
1578- state .font , c )
1568+ alternatives = state .fontset .get_sized_alternatives_for_symbol (state .font , c )
15791569
15801570 state = state .copy ()
15811571 for fontname , sym in alternatives :
@@ -2468,7 +2458,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
24682458 state = self .get_state ()
24692459 rule_thickness = state .fontset .get_underline_thickness (
24702460 state .font , state .fontsize , state .dpi )
2471- xHeight = state .fontset .get_xheight (
2461+ x_height = state .fontset .get_xheight (
24722462 state .font , state .fontsize , state .dpi )
24732463
24742464 if napostrophes :
@@ -2537,24 +2527,21 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25372527 nucleus = Hlist ([nucleus ])
25382528
25392529 # Handle regular sub/superscripts
2540- constants = _get_font_constant_set (state )
2530+ consts = _get_font_constant_set (state )
25412531 lc_height = last_char .height
25422532 lc_baseline = 0
25432533 if self .is_dropsub (last_char ):
25442534 lc_baseline = last_char .depth
25452535
25462536 # Compute kerning for sub and super
2547- superkern = constants .delta * xHeight
2548- subkern = constants .delta * xHeight
2537+ superkern = consts .delta * x_height
2538+ subkern = consts .delta * x_height
25492539 if self .is_slanted (last_char ):
2550- superkern += constants .delta * xHeight
2551- superkern += (constants .delta_slanted *
2552- (lc_height - xHeight * 2. / 3. ))
2540+ superkern += consts .delta * x_height
2541+ superkern += consts .delta_slanted * (lc_height - x_height * 2 / 3 )
25532542 if self .is_dropsub (last_char ):
2554- subkern = (3 * constants .delta -
2555- constants .delta_integral ) * lc_height
2556- superkern = (3 * constants .delta +
2557- constants .delta_integral ) * lc_height
2543+ subkern = (3 * consts .delta - consts .delta_integral ) * lc_height
2544+ superkern = (3 * consts .delta + consts .delta_integral ) * lc_height
25582545 else :
25592546 subkern = 0
25602547
@@ -2567,28 +2554,28 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25672554 x = Hlist ([Kern (subkern ), T .cast (Node , sub )])
25682555 x .shrink ()
25692556 if self .is_dropsub (last_char ):
2570- shift_down = lc_baseline + constants .subdrop * xHeight
2557+ shift_down = lc_baseline + consts .subdrop * x_height
25712558 else :
2572- shift_down = constants .sub1 * xHeight
2559+ shift_down = consts .sub1 * x_height
25732560 x .shift_amount = shift_down
25742561 else :
25752562 x = Hlist ([Kern (superkern ), super ])
25762563 x .shrink ()
25772564 if self .is_dropsub (last_char ):
2578- shift_up = lc_height - constants .subdrop * xHeight
2565+ shift_up = lc_height - consts .subdrop * x_height
25792566 else :
2580- shift_up = constants .sup1 * xHeight
2567+ shift_up = consts .sup1 * x_height
25812568 if sub is None :
25822569 x .shift_amount = - shift_up
25832570 else : # Both sub and superscript
25842571 y = Hlist ([Kern (subkern ), sub ])
25852572 y .shrink ()
25862573 if self .is_dropsub (last_char ):
2587- shift_down = lc_baseline + constants .subdrop * xHeight
2574+ shift_down = lc_baseline + consts .subdrop * x_height
25882575 else :
2589- shift_down = constants .sub2 * xHeight
2576+ shift_down = consts .sub2 * x_height
25902577 # If sub and superscript collide, move super up
2591- clr = (2.0 * rule_thickness -
2578+ clr = (2 * rule_thickness -
25922579 ((shift_up - x .depth ) - (y .height - shift_down )))
25932580 if clr > 0. :
25942581 shift_up += clr
@@ -2599,7 +2586,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25992586 x .shift_amount = shift_down
26002587
26012588 if not self .is_dropsub (last_char ):
2602- x .width += constants .script_space * xHeight
2589+ x .width += consts .script_space * x_height
26032590
26042591 # Do we need to add a space after the nucleus?
26052592 # To find out, check the flag set by operatorname
@@ -2624,33 +2611,26 @@ def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathSty
26242611 width = max (num .width , den .width )
26252612 cnum .hpack (width , 'exactly' )
26262613 cden .hpack (width , 'exactly' )
2627- vlist = Vlist ([cnum , # numerator
2628- Vbox (0 , thickness * 2.0 ), # space
2629- Hrule (state , rule ), # rule
2630- Vbox (0 , thickness * 2.0 ), # space
2631- cden # denominator
2632- ])
2614+ vlist = Vlist ([
2615+ cnum , # numerator
2616+ Vbox (0 , 2 * thickness ), # space
2617+ Hrule (state , rule ), # rule
2618+ Vbox (0 , 2 * thickness ), # space
2619+ cden , # denominator
2620+ ])
26332621
26342622 # Shift so the fraction line sits in the middle of the
26352623 # equals sign
26362624 metrics = state .fontset .get_metrics (
26372625 state .font , mpl .rcParams ['mathtext.default' ],
26382626 '=' , state .fontsize , state .dpi )
26392627 shift = (cden .height -
2640- ((metrics .ymax + metrics .ymin ) / 2 -
2641- thickness * 3.0 ))
2628+ ((metrics .ymax + metrics .ymin ) / 2 - 3 * thickness ))
26422629 vlist .shift_amount = shift
26432630
2644- result = [Hlist ([vlist , Hbox (thickness * 2. )])]
2631+ result : list [ Box | Char | str ] = [Hlist ([vlist , Hbox (2 * thickness )])]
26452632 if ldelim or rdelim :
2646- if ldelim == '' :
2647- ldelim = '.'
2648- if rdelim == '' :
2649- rdelim = '.'
2650- return self ._auto_sized_delimiter (ldelim ,
2651- T .cast (list [Box | Char | str ],
2652- result ),
2653- rdelim )
2633+ return self ._auto_sized_delimiter (ldelim or "." , result , rdelim or "." )
26542634 return result
26552635
26562636 def style_literal (self , toks : ParseResults ) -> T .Any :
@@ -2719,7 +2699,7 @@ def sqrt(self, toks: ParseResults) -> T.Any:
27192699
27202700 # Determine the height of the body, and add a little extra to
27212701 # the height so it doesn't seem cramped
2722- height = body .height - body .shift_amount + thickness * 5.0
2702+ height = body .height - body .shift_amount + 5 * thickness
27232703 depth = body .depth + body .shift_amount
27242704 check = AutoHeightChar (r'\__sqrt__' , height , depth , state , always = True )
27252705 height = check .height - check .shift_amount
@@ -2729,13 +2709,13 @@ def sqrt(self, toks: ParseResults) -> T.Any:
27292709 padded_body = Hlist ([Hbox (2 * thickness ), body , Hbox (2 * thickness )])
27302710 rightside = Vlist ([Hrule (state ), Glue ('fill' ), padded_body ])
27312711 # Stretch the glue between the hrule and the body
2732- rightside .vpack (height + (state .fontsize * state .dpi ) / (100.0 * 12.0 ),
2712+ rightside .vpack (height + (state .fontsize * state .dpi ) / (100 * 12 ),
27332713 'exactly' , depth )
27342714
27352715 # Add the root and shift it upward so it is above the tick.
27362716 # The value of 0.6 is a hard-coded hack ;)
27372717 if not root :
2738- root = Box (check . width * 0.5 , 0. , 0. )
2718+ root = Box (0.5 * check . width , 0. , 0. )
27392719 else :
27402720 root = Hlist (root )
27412721 root .shrink ()
@@ -2744,11 +2724,12 @@ def sqrt(self, toks: ParseResults) -> T.Any:
27442724 root_vlist = Vlist ([Hlist ([root ])])
27452725 root_vlist .shift_amount = - height * 0.6
27462726
2747- hlist = Hlist ([root_vlist , # Root
2748- # Negative kerning to put root over tick
2749- Kern (- check .width * 0.5 ),
2750- check , # Check
2751- rightside ]) # Body
2727+ hlist = Hlist ([
2728+ root_vlist , # Root
2729+ Kern (- 0.5 * check .width ), # Negative kerning to put root over tick
2730+ check , # Check
2731+ rightside , # Body
2732+ ])
27522733 return [hlist ]
27532734
27542735 def overline (self , toks : ParseResults ) -> T .Any :
@@ -2757,14 +2738,14 @@ def overline(self, toks: ParseResults) -> T.Any:
27572738 state = self .get_state ()
27582739 thickness = state .get_current_underline_thickness ()
27592740
2760- height = body .height - body .shift_amount + thickness * 3.0
2741+ height = body .height - body .shift_amount + 3 * thickness
27612742 depth = body .depth + body .shift_amount
27622743
27632744 # Place overline above body
27642745 rightside = Vlist ([Hrule (state ), Glue ('fill' ), Hlist ([body ])])
27652746
27662747 # Stretch the glue between the hrule and the body
2767- rightside .vpack (height + (state .fontsize * state .dpi ) / (100.0 * 12.0 ),
2748+ rightside .vpack (height + (state .fontsize * state .dpi ) / (100 * 12 ),
27682749 'exactly' , depth )
27692750
27702751 hlist = Hlist ([rightside ])
@@ -2810,10 +2791,7 @@ def _auto_sized_delimiter(self, front: str,
28102791
28112792 def auto_delim (self , toks : ParseResults ) -> T .Any :
28122793 return self ._auto_sized_delimiter (
2813- toks ["left" ],
2814- # if "mid" in toks ... can be removed when requiring pyparsing 3.
2815- toks ["mid" ].as_list () if "mid" in toks else [],
2816- toks ["right" ])
2794+ toks ["left" ], toks ["mid" ].as_list (), toks ["right" ])
28172795
28182796 def boldsymbol (self , toks : ParseResults ) -> T .Any :
28192797 self .push_state ()
0 commit comments