@@ -302,6 +302,7 @@ def _read(self):
302302 # Pages appear to start with the sequence
303303 # bop (begin of page)
304304 # xxx comment
305+ # <push, ..., pop> # if using chemformula
305306 # down
306307 # push
307308 # down
@@ -313,17 +314,24 @@ def _read(self):
313314 # etc.
314315 # (dviasm is useful to explore this structure.)
315316 # Thus, we use the vertical position at the first time the stack depth
316- # reaches 3, while at least three "downs" have been executed, as the
317+ # reaches 3, while at least three "downs" have been executed (excluding
318+ # those popped out (corresponding to the chemformula preamble)), as the
317319 # baseline (the "down" count is necessary to handle xcolor).
318- downs = 0
320+ down_stack = [ 0 ]
319321 self ._baseline_v = None
320322 while True :
321323 byte = self .file .read (1 )[0 ]
322324 self ._dtable [byte ](self , byte )
323- downs += self ._dtable [byte ].__name__ == "_down"
325+ name = self ._dtable [byte ].__name__
326+ if name == "_push" :
327+ down_stack .append (down_stack [- 1 ])
328+ elif name == "_pop" :
329+ down_stack .pop ()
330+ elif name == "_down" :
331+ down_stack [- 1 ] += 1
324332 if (self ._baseline_v is None
325333 and len (getattr (self , "stack" , [])) == 3
326- and downs >= 4 ):
334+ and down_stack [ - 1 ] >= 4 ):
327335 self ._baseline_v = self .v
328336 if byte == 140 : # end of page
329337 return True
0 commit comments