8
8
import copy
9
9
import enum
10
10
import functools
11
+ import itertools
11
12
import logging
12
13
import math
13
14
import os
@@ -409,14 +410,14 @@ def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float:
409
410
metrics = self .get_metrics (
410
411
fontname , mpl .rcParams ['mathtext.default' ], 'x' , fontsize , dpi )
411
412
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
414
415
415
416
def get_underline_thickness (self , font : str , fontsize : float , dpi : float ) -> float :
416
417
# This function used to grab underline thickness from the font
417
418
# metrics, but that information is just too un-reliable, so it
418
419
# is now hardcoded.
419
- return ((0.75 / 12.0 ) * fontsize * dpi ) / 72.0
420
+ return ((0.75 / 12 ) * fontsize * dpi ) / 72
420
421
421
422
def get_kern (self , font1 : str , fontclass1 : str , sym1 : str , fontsize1 : float ,
422
423
font2 : str , fontclass2 : str , sym2 : str , fontsize2 : float ,
@@ -1226,21 +1227,13 @@ def kern(self) -> None:
1226
1227
linked list.
1227
1228
"""
1228
1229
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
1244
1237
1245
1238
def hpack (self , w : float = 0.0 ,
1246
1239
m : T .Literal ['additional' , 'exactly' ] = 'additional' ) -> None :
@@ -1534,20 +1527,18 @@ class AutoHeightChar(Hlist):
1534
1527
1535
1528
def __init__ (self , c : str , height : float , depth : float , state : ParserState ,
1536
1529
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 )
1539
1531
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 )
1542
1533
1543
1534
state = state .copy ()
1544
1535
target_total = height + depth
1545
1536
for fontname , sym in alternatives :
1546
1537
state .font = fontname
1547
1538
char = Char (sym , state )
1548
1539
# 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 :
1551
1542
break
1552
1543
1553
1544
shift = 0.0
@@ -1574,8 +1565,7 @@ class AutoWidthChar(Hlist):
1574
1565
1575
1566
def __init__ (self , c : str , width : float , state : ParserState , always : bool = False ,
1576
1567
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 )
1579
1569
1580
1570
state = state .copy ()
1581
1571
for fontname , sym in alternatives :
@@ -2468,7 +2458,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
2468
2458
state = self .get_state ()
2469
2459
rule_thickness = state .fontset .get_underline_thickness (
2470
2460
state .font , state .fontsize , state .dpi )
2471
- xHeight = state .fontset .get_xheight (
2461
+ x_height = state .fontset .get_xheight (
2472
2462
state .font , state .fontsize , state .dpi )
2473
2463
2474
2464
if napostrophes :
@@ -2537,24 +2527,21 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
2537
2527
nucleus = Hlist ([nucleus ])
2538
2528
2539
2529
# Handle regular sub/superscripts
2540
- constants = _get_font_constant_set (state )
2530
+ consts = _get_font_constant_set (state )
2541
2531
lc_height = last_char .height
2542
2532
lc_baseline = 0
2543
2533
if self .is_dropsub (last_char ):
2544
2534
lc_baseline = last_char .depth
2545
2535
2546
2536
# 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
2549
2539
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 )
2553
2542
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
2558
2545
else :
2559
2546
subkern = 0
2560
2547
@@ -2567,28 +2554,28 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
2567
2554
x = Hlist ([Kern (subkern ), T .cast (Node , sub )])
2568
2555
x .shrink ()
2569
2556
if self .is_dropsub (last_char ):
2570
- shift_down = lc_baseline + constants .subdrop * xHeight
2557
+ shift_down = lc_baseline + consts .subdrop * x_height
2571
2558
else :
2572
- shift_down = constants .sub1 * xHeight
2559
+ shift_down = consts .sub1 * x_height
2573
2560
x .shift_amount = shift_down
2574
2561
else :
2575
2562
x = Hlist ([Kern (superkern ), super ])
2576
2563
x .shrink ()
2577
2564
if self .is_dropsub (last_char ):
2578
- shift_up = lc_height - constants .subdrop * xHeight
2565
+ shift_up = lc_height - consts .subdrop * x_height
2579
2566
else :
2580
- shift_up = constants .sup1 * xHeight
2567
+ shift_up = consts .sup1 * x_height
2581
2568
if sub is None :
2582
2569
x .shift_amount = - shift_up
2583
2570
else : # Both sub and superscript
2584
2571
y = Hlist ([Kern (subkern ), sub ])
2585
2572
y .shrink ()
2586
2573
if self .is_dropsub (last_char ):
2587
- shift_down = lc_baseline + constants .subdrop * xHeight
2574
+ shift_down = lc_baseline + consts .subdrop * x_height
2588
2575
else :
2589
- shift_down = constants .sub2 * xHeight
2576
+ shift_down = consts .sub2 * x_height
2590
2577
# If sub and superscript collide, move super up
2591
- clr = (2.0 * rule_thickness -
2578
+ clr = (2 * rule_thickness -
2592
2579
((shift_up - x .depth ) - (y .height - shift_down )))
2593
2580
if clr > 0. :
2594
2581
shift_up += clr
@@ -2599,7 +2586,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
2599
2586
x .shift_amount = shift_down
2600
2587
2601
2588
if not self .is_dropsub (last_char ):
2602
- x .width += constants .script_space * xHeight
2589
+ x .width += consts .script_space * x_height
2603
2590
2604
2591
# Do we need to add a space after the nucleus?
2605
2592
# 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
2624
2611
width = max (num .width , den .width )
2625
2612
cnum .hpack (width , 'exactly' )
2626
2613
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
+ ])
2633
2621
2634
2622
# Shift so the fraction line sits in the middle of the
2635
2623
# equals sign
2636
2624
metrics = state .fontset .get_metrics (
2637
2625
state .font , mpl .rcParams ['mathtext.default' ],
2638
2626
'=' , state .fontsize , state .dpi )
2639
2627
shift = (cden .height -
2640
- ((metrics .ymax + metrics .ymin ) / 2 -
2641
- thickness * 3.0 ))
2628
+ ((metrics .ymax + metrics .ymin ) / 2 - 3 * thickness ))
2642
2629
vlist .shift_amount = shift
2643
2630
2644
- result = [Hlist ([vlist , Hbox (thickness * 2. )])]
2631
+ result : list [ Box | Char | str ] = [Hlist ([vlist , Hbox (2 * thickness )])]
2645
2632
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 "." )
2654
2634
return result
2655
2635
2656
2636
def style_literal (self , toks : ParseResults ) -> T .Any :
@@ -2719,7 +2699,7 @@ def sqrt(self, toks: ParseResults) -> T.Any:
2719
2699
2720
2700
# Determine the height of the body, and add a little extra to
2721
2701
# 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
2723
2703
depth = body .depth + body .shift_amount
2724
2704
check = AutoHeightChar (r'\__sqrt__' , height , depth , state , always = True )
2725
2705
height = check .height - check .shift_amount
@@ -2729,13 +2709,13 @@ def sqrt(self, toks: ParseResults) -> T.Any:
2729
2709
padded_body = Hlist ([Hbox (2 * thickness ), body , Hbox (2 * thickness )])
2730
2710
rightside = Vlist ([Hrule (state ), Glue ('fill' ), padded_body ])
2731
2711
# 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 ),
2733
2713
'exactly' , depth )
2734
2714
2735
2715
# Add the root and shift it upward so it is above the tick.
2736
2716
# The value of 0.6 is a hard-coded hack ;)
2737
2717
if not root :
2738
- root = Box (check . width * 0.5 , 0. , 0. )
2718
+ root = Box (0.5 * check . width , 0. , 0. )
2739
2719
else :
2740
2720
root = Hlist (root )
2741
2721
root .shrink ()
@@ -2744,11 +2724,12 @@ def sqrt(self, toks: ParseResults) -> T.Any:
2744
2724
root_vlist = Vlist ([Hlist ([root ])])
2745
2725
root_vlist .shift_amount = - height * 0.6
2746
2726
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
+ ])
2752
2733
return [hlist ]
2753
2734
2754
2735
def overline (self , toks : ParseResults ) -> T .Any :
@@ -2757,14 +2738,14 @@ def overline(self, toks: ParseResults) -> T.Any:
2757
2738
state = self .get_state ()
2758
2739
thickness = state .get_current_underline_thickness ()
2759
2740
2760
- height = body .height - body .shift_amount + thickness * 3.0
2741
+ height = body .height - body .shift_amount + 3 * thickness
2761
2742
depth = body .depth + body .shift_amount
2762
2743
2763
2744
# Place overline above body
2764
2745
rightside = Vlist ([Hrule (state ), Glue ('fill' ), Hlist ([body ])])
2765
2746
2766
2747
# 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 ),
2768
2749
'exactly' , depth )
2769
2750
2770
2751
hlist = Hlist ([rightside ])
@@ -2810,10 +2791,7 @@ def _auto_sized_delimiter(self, front: str,
2810
2791
2811
2792
def auto_delim (self , toks : ParseResults ) -> T .Any :
2812
2793
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" ])
2817
2795
2818
2796
def boldsymbol (self , toks : ParseResults ) -> T .Any :
2819
2797
self .push_state ()
0 commit comments