Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c6efec

Browse files
authored
Merge pull request #22468 from anntzer/ship
Turn _mathtext.ship into a plain function.
2 parents 35ced03 + 99d481b commit 3c6efec

File tree

1 file changed

+61
-77
lines changed

1 file changed

+61
-77
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 61 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,8 @@ class StixSansFonts(StixFonts):
717717
#
718718
# The most relevant "chapters" are:
719719
# Data structures for boxes and their friends
720-
# Shipping pages out (Ship class)
721-
# Packaging (hpack and vpack)
720+
# Shipping pages out (ship())
721+
# Packaging (hpack() and vpack())
722722
# Data structures for math mode
723723
# Subroutines for math mode
724724
# Typesetting math formulas
@@ -1421,81 +1421,70 @@ def __init__(self, c, width, state, always=False, char_class=Char):
14211421
self.width = char.width
14221422

14231423

1424-
class Ship:
1424+
def ship(ox, oy, box):
14251425
"""
14261426
Ship boxes to output once they have been set up, this sends them to output.
14271427
1428-
Since boxes can be inside of boxes inside of boxes, the main work of `Ship`
1428+
Since boxes can be inside of boxes inside of boxes, the main work of `ship`
14291429
is done by two mutually recursive routines, `hlist_out` and `vlist_out`,
14301430
which traverse the `Hlist` nodes and `Vlist` nodes inside of horizontal
14311431
and vertical boxes. The global variables used in TeX to store state as it
1432-
processes have become member variables here.
1432+
processes have become local variables here.
14331433
"""
14341434

1435-
def __call__(self, ox, oy, box):
1436-
self.max_push = 0 # Deepest nesting of push commands so far
1437-
self.cur_s = 0
1438-
self.cur_v = 0.
1439-
self.cur_h = 0.
1440-
self.off_h = ox
1441-
self.off_v = oy + box.height
1442-
self.hlist_out(box)
1435+
cur_v = 0.
1436+
cur_h = 0.
1437+
off_h = ox
1438+
off_v = oy + box.height
14431439

1444-
@staticmethod
14451440
def clamp(value):
1446-
if value < -1000000000.:
1447-
return -1000000000.
1448-
if value > 1000000000.:
1449-
return 1000000000.
1450-
return value
1451-
1452-
def hlist_out(self, box):
1453-
cur_g = 0
1454-
cur_glue = 0.
1455-
glue_order = box.glue_order
1456-
glue_sign = box.glue_sign
1457-
base_line = self.cur_v
1458-
left_edge = self.cur_h
1459-
self.cur_s += 1
1460-
self.max_push = max(self.cur_s, self.max_push)
1461-
clamp = self.clamp
1441+
return -1e9 if value < -1e9 else +1e9 if value > +1e9 else value
1442+
1443+
def hlist_out(box):
1444+
nonlocal cur_v, cur_h, off_h, off_v
1445+
1446+
cur_g = 0
1447+
cur_glue = 0.
1448+
glue_order = box.glue_order
1449+
glue_sign = box.glue_sign
1450+
base_line = cur_v
1451+
left_edge = cur_h
14621452

14631453
for p in box.children:
14641454
if isinstance(p, Char):
1465-
p.render(self.cur_h + self.off_h, self.cur_v + self.off_v)
1466-
self.cur_h += p.width
1455+
p.render(cur_h + off_h, cur_v + off_v)
1456+
cur_h += p.width
14671457
elif isinstance(p, Kern):
1468-
self.cur_h += p.width
1458+
cur_h += p.width
14691459
elif isinstance(p, List):
14701460
# node623
14711461
if len(p.children) == 0:
1472-
self.cur_h += p.width
1462+
cur_h += p.width
14731463
else:
1474-
edge = self.cur_h
1475-
self.cur_v = base_line + p.shift_amount
1464+
edge = cur_h
1465+
cur_v = base_line + p.shift_amount
14761466
if isinstance(p, Hlist):
1477-
self.hlist_out(p)
1467+
hlist_out(p)
14781468
else:
14791469
# p.vpack(box.height + box.depth, 'exactly')
1480-
self.vlist_out(p)
1481-
self.cur_h = edge + p.width
1482-
self.cur_v = base_line
1470+
vlist_out(p)
1471+
cur_h = edge + p.width
1472+
cur_v = base_line
14831473
elif isinstance(p, Box):
14841474
# node624
14851475
rule_height = p.height
1486-
rule_depth = p.depth
1487-
rule_width = p.width
1476+
rule_depth = p.depth
1477+
rule_width = p.width
14881478
if np.isinf(rule_height):
14891479
rule_height = box.height
14901480
if np.isinf(rule_depth):
14911481
rule_depth = box.depth
14921482
if rule_height > 0 and rule_width > 0:
1493-
self.cur_v = base_line + rule_depth
1494-
p.render(self.cur_h + self.off_h,
1495-
self.cur_v + self.off_v,
1483+
cur_v = base_line + rule_depth
1484+
p.render(cur_h + off_h, cur_v + off_v,
14961485
rule_width, rule_height)
1497-
self.cur_v = base_line
1498-
self.cur_h += rule_width
1486+
cur_v = base_line
1487+
cur_h += rule_width
14991488
elif isinstance(p, Glue):
15001489
# node625
15011490
glue_spec = p.glue_spec
@@ -1509,38 +1498,36 @@ def hlist_out(self, box):
15091498
cur_glue += glue_spec.shrink
15101499
cur_g = round(clamp(box.glue_set * cur_glue))
15111500
rule_width += cur_g
1512-
self.cur_h += rule_width
1513-
self.cur_s -= 1
1514-
1515-
def vlist_out(self, box):
1516-
cur_g = 0
1517-
cur_glue = 0.
1518-
glue_order = box.glue_order
1519-
glue_sign = box.glue_sign
1520-
self.cur_s += 1
1521-
self.max_push = max(self.max_push, self.cur_s)
1522-
left_edge = self.cur_h
1523-
self.cur_v -= box.height
1524-
top_edge = self.cur_v
1525-
clamp = self.clamp
1501+
cur_h += rule_width
1502+
1503+
def vlist_out(box):
1504+
nonlocal cur_v, cur_h, off_h, off_v
1505+
1506+
cur_g = 0
1507+
cur_glue = 0.
1508+
glue_order = box.glue_order
1509+
glue_sign = box.glue_sign
1510+
left_edge = cur_h
1511+
cur_v -= box.height
1512+
top_edge = cur_v
15261513

15271514
for p in box.children:
15281515
if isinstance(p, Kern):
1529-
self.cur_v += p.width
1516+
cur_v += p.width
15301517
elif isinstance(p, List):
15311518
if len(p.children) == 0:
1532-
self.cur_v += p.height + p.depth
1519+
cur_v += p.height + p.depth
15331520
else:
1534-
self.cur_v += p.height
1535-
self.cur_h = left_edge + p.shift_amount
1536-
save_v = self.cur_v
1521+
cur_v += p.height
1522+
cur_h = left_edge + p.shift_amount
1523+
save_v = cur_v
15371524
p.width = box.width
15381525
if isinstance(p, Hlist):
1539-
self.hlist_out(p)
1526+
hlist_out(p)
15401527
else:
1541-
self.vlist_out(p)
1542-
self.cur_v = save_v + p.depth
1543-
self.cur_h = left_edge
1528+
vlist_out(p)
1529+
cur_v = save_v + p.depth
1530+
cur_h = left_edge
15441531
elif isinstance(p, Box):
15451532
rule_height = p.height
15461533
rule_depth = p.depth
@@ -1549,9 +1536,8 @@ def vlist_out(self, box):
15491536
rule_width = box.width
15501537
rule_height += rule_depth
15511538
if rule_height > 0 and rule_depth > 0:
1552-
self.cur_v += rule_height
1553-
p.render(self.cur_h + self.off_h,
1554-
self.cur_v + self.off_v,
1539+
cur_v += rule_height
1540+
p.render(cur_h + off_h, cur_v + off_v,
15551541
rule_width, rule_height)
15561542
elif isinstance(p, Glue):
15571543
glue_spec = p.glue_spec
@@ -1565,14 +1551,12 @@ def vlist_out(self, box):
15651551
cur_glue += glue_spec.shrink
15661552
cur_g = round(clamp(box.glue_set * cur_glue))
15671553
rule_height += cur_g
1568-
self.cur_v += rule_height
1554+
cur_v += rule_height
15691555
elif isinstance(p, Char):
15701556
raise RuntimeError(
15711557
"Internal mathtext error: Char node found in vlist")
1572-
self.cur_s -= 1
1573-
15741558

1575-
ship = Ship()
1559+
hlist_out(box)
15761560

15771561

15781562
##############################################################################

0 commit comments

Comments
 (0)