2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 import glob
, os
, threading
, Queue
, traceback
, re
, tempfile
, sys
, atexit
, time
26 import config
, siteconfig
, unit
, box
, canvas
, trafo
, version
, attr
, style
, dvifile
28 ###############################################################################
30 # - please don't get confused:
31 # - there is a texmessage (and a texmessageparsed) attribute within the
32 # texrunner; it contains TeX/LaTeX response from the last command execution
33 # - instances of classes derived from the class texmessage are used to
34 # parse the TeX/LaTeX response as it is stored in the texmessageparsed
35 # attribute of a texrunner instance
36 # - the multiple usage of the name texmessage might be removed in the future
37 # - texmessage instances should implement _Itexmessage
38 ###############################################################################
40 class TexResultError(RuntimeError):
41 """specialized texrunner exception class
42 - it is raised by texmessage instances, when a texmessage indicates an error
43 - it is raised by the texrunner itself, whenever there is a texmessage left
44 after all parsing of this message (by texmessage instances)"""
46 def __init__(self
, description
, texrunner
):
47 self
.description
= description
48 self
.texrunner
= texrunner
51 """prints a detailed report about the problem
52 - the verbose level is controlled by texrunner.errordebug"""
53 if self
.texrunner
.errordebug
>= 2:
54 return ("%s\n" % self
.description
+
55 "The expression passed to TeX was:\n"
56 " %s\n" % self
.texrunner
.expr
.replace("\n", "\n ").rstrip() +
57 "The return message from TeX was:\n"
58 " %s\n" % self
.texrunner
.texmessage
.replace("\n", "\n ").rstrip() +
59 "After parsing this message, the following was left:\n"
60 " %s" % self
.texrunner
.texmessageparsed
.replace("\n", "\n ").rstrip())
61 elif self
.texrunner
.errordebug
== 1:
62 firstlines
= self
.texrunner
.texmessageparsed
.split("\n")
63 if len(firstlines
) > 5:
64 firstlines
= firstlines
[:5] + ["(cut after 5 lines, increase errordebug for more output)"]
65 return ("%s\n" % self
.description
+
66 "The expression passed to TeX was:\n"
67 " %s\n" % self
.texrunner
.expr
.replace("\n", "\n ").rstrip() +
68 "After parsing the return message from TeX, the following was left:\n" +
69 reduce(lambda x
, y
: "%s %s\n" % (x
,y
), firstlines
, "").rstrip())
71 return self
.description
74 class TexResultWarning(TexResultError
):
75 """as above, but with different handling of the exception
76 - when this exception is raised by a texmessage instance,
77 the information just get reported and the execution continues"""
82 """validates/invalidates TeX/LaTeX response"""
84 def check(self
, texrunner
):
85 """check a Tex/LaTeX response and respond appropriate
86 - read the texrunners texmessageparsed attribute
87 - if there is an problem found, raise an appropriate
88 exception (TexResultError or TexResultWarning)
89 - remove any valid and identified TeX/LaTeX response
90 from the texrunners texmessageparsed attribute
91 -> finally, there should be nothing left in there,
92 otherwise it is interpreted as an error"""
95 class texmessage(attr
.attr
): pass
98 class _texmessagestart(texmessage
):
99 """validates TeX/LaTeX startup"""
101 __implements__
= _Itexmessage
103 startpattern
= re
.compile(r
"This is [-0-9a-zA-Z\s_]*TeX")
105 def check(self
, texrunner
):
106 # check for "This is e-TeX"
107 m
= self
.startpattern
.search(texrunner
.texmessageparsed
)
109 raise TexResultError("TeX startup failed", texrunner
)
110 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
[m
.end():]
112 # check for filename to be processed
114 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
.split("%s.tex" % texrunner
.texfilename
, 1)[1]
115 except (IndexError, ValueError):
116 raise TexResultError("TeX running startup file failed", texrunner
)
118 # check for \raiseerror -- just to be sure that communication works
120 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
.split("*! Undefined control sequence.\n<*> \\raiseerror\n %\n", 1)[1]
121 except (IndexError, ValueError):
122 raise TexResultError("TeX scrollmode check failed", texrunner
)
125 class _texmessagenoaux(texmessage
):
126 """allows for LaTeXs no-aux-file warning"""
128 __implements__
= _Itexmessage
130 def check(self
, texrunner
):
132 s1
, s2
= texrunner
.texmessageparsed
.split("No file %s.aux." % texrunner
.texfilename
, 1)
133 texrunner
.texmessageparsed
= s1
+ s2
134 except (IndexError, ValueError):
136 s1
, s2
= texrunner
.texmessageparsed
.split("No file %s%s%s.aux." % (os
.curdir
,
138 texrunner
.texfilename
), 1)
139 texrunner
.texmessageparsed
= s1
+ s2
140 except (IndexError, ValueError):
144 class _texmessageinputmarker(texmessage
):
145 """validates the PyXInputMarker"""
147 __implements__
= _Itexmessage
149 def check(self
, texrunner
):
151 s1
, s2
= texrunner
.texmessageparsed
.split("PyXInputMarker:executeid=%s:" % texrunner
.executeid
, 1)
152 texrunner
.texmessageparsed
= s1
+ s2
153 except (IndexError, ValueError):
154 raise TexResultError("PyXInputMarker expected", texrunner
)
157 class _texmessagepyxbox(texmessage
):
158 """validates the PyXBox output"""
160 __implements__
= _Itexmessage
162 pattern
= re
.compile(r
"PyXBox:page=(?P<page>\d+),lt=-?\d*((\d\.?)|(\.?\d))\d*pt,rt=-?\d*((\d\.?)|(\.?\d))\d*pt,ht=-?\d*((\d\.?)|(\.?\d))\d*pt,dp=-?\d*((\d\.?)|(\.?\d))\d*pt:")
164 def check(self
, texrunner
):
165 m
= self
.pattern
.search(texrunner
.texmessageparsed
)
166 if m
and m
.group("page") == str(texrunner
.page
):
167 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
[:m
.start()] + texrunner
.texmessageparsed
[m
.end():]
169 raise TexResultError("PyXBox expected", texrunner
)
172 class _texmessagepyxpageout(texmessage
):
173 """validates the dvi shipout message (writing a page to the dvi file)"""
175 __implements__
= _Itexmessage
177 def check(self
, texrunner
):
179 s1
, s2
= texrunner
.texmessageparsed
.split("[80.121.88.%s]" % texrunner
.page
, 1)
180 texrunner
.texmessageparsed
= s1
+ s2
181 except (IndexError, ValueError):
182 raise TexResultError("PyXPageOutMarker expected", texrunner
)
185 class _texmessagefontsubstitution(texmessage
):
186 """validates the font substituion Warning"""
188 __implements__
= _Itexmessage
190 pattern
= re
.compile("LaTeX Font Warning: Font shape (?P<font>.*) in size <(?P<orig>.*)> not available\s*\(Font\)(.*) size <(?P<subst>.*)> substituted on input line (?P<line>.*)\.")
192 def check(self
, texrunner
):
193 m
= self
.pattern
.search(texrunner
.texmessageparsed
)
195 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
[:m
.start()] + texrunner
.texmessageparsed
[m
.end():]
196 raise TexResultWarning("LaTeX Font Warning on input line %s" % (m
.group('line')), texrunner
)
199 class _texmessagetexend(texmessage
):
200 """validates TeX/LaTeX finish"""
202 __implements__
= _Itexmessage
204 def check(self
, texrunner
):
206 s1
, s2
= texrunner
.texmessageparsed
.split("(%s.aux)" % texrunner
.texfilename
, 1)
207 texrunner
.texmessageparsed
= s1
+ s2
208 except (IndexError, ValueError):
210 s1
, s2
= texrunner
.texmessageparsed
.split("(%s%s%s.aux)" % (os
.curdir
,
212 texrunner
.texfilename
), 1)
213 texrunner
.texmessageparsed
= s1
+ s2
214 except (IndexError, ValueError):
217 # pass font size summary over to PyX user
218 fontpattern
= re
.compile(r
"LaTeX Font Warning: Size substitutions with differences\s*\(Font\).* have occurred.\s*")
219 m
= fontpattern
.search(texrunner
.texmessageparsed
)
221 sys
.stderr
.write("LaTeX has detected Font Size substituion differences.\n")
222 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
[:m
.start()] + texrunner
.texmessageparsed
[m
.end():]
224 # check for "(see the transcript file for additional information)"
226 s1
, s2
= texrunner
.texmessageparsed
.split("(see the transcript file for additional information)", 1)
227 texrunner
.texmessageparsed
= s1
+ s2
228 except (IndexError, ValueError):
231 # check for "Output written on ...dvi (1 page, 220 bytes)."
232 dvipattern
= re
.compile(r
"Output written on %s\.dvi \((?P<page>\d+) pages?, \d+ bytes\)\." % texrunner
.texfilename
)
233 m
= dvipattern
.search(texrunner
.texmessageparsed
)
236 raise TexResultError("TeX dvifile messages expected", texrunner
)
237 if m
.group("page") != str(texrunner
.page
):
238 raise TexResultError("wrong number of pages reported", texrunner
)
239 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
[:m
.start()] + texrunner
.texmessageparsed
[m
.end():]
242 s1
, s2
= texrunner
.texmessageparsed
.split("No pages of output.", 1)
243 texrunner
.texmessageparsed
= s1
+ s2
244 except (IndexError, ValueError):
245 raise TexResultError("no dvifile expected", texrunner
)
247 # check for "Transcript written on ...log."
249 s1
, s2
= texrunner
.texmessageparsed
.split("Transcript written on %s.log." % texrunner
.texfilename
, 1)
250 texrunner
.texmessageparsed
= s1
+ s2
251 except (IndexError, ValueError):
252 raise TexResultError("TeX logfile message expected", texrunner
)
255 class _texmessageemptylines(texmessage
):
256 """validates "*-only" (TeX/LaTeX input marker in interactive mode) and empty lines
257 also clear TeX interactive mode warning (Please type a command or say `\\end')
260 __implements__
= _Itexmessage
262 def check(self
, texrunner
):
263 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
.replace(r
"(Please type a command or say `\end')", "")
264 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
.replace("*\n", "")
265 texrunner
.texmessageparsed
= texrunner
.texmessageparsed
.replace("\n", "")
268 class _texmessageload(texmessage
):
269 """validates inclusion of arbitrary files
270 - the matched pattern is "(<filename> <arbitrary other stuff>)", where
271 <fielname> is a readable file and other stuff can be anything
272 - "(" and ")" must be used consistent (otherwise this validator just does nothing)
273 - this is not always wanted, but we just assume that file inclusion is fine"""
275 __implements__
= _Itexmessage
277 pattern
= re
.compile(r
" *\((?P<filename>[^()\s\n]+)[^()]*\) *")
279 def baselevels(self
, s
, maxlevel
=1, brackets
="()"):
280 """strip parts of a string above a given bracket level
281 - return a modified (some parts might be removed) version of the string s
282 where all parts inside brackets with level higher than maxlevel are
284 - if brackets do not match (number of left and right brackets is wrong
285 or at some points there were more right brackets than left brackets)
286 just return the unmodified string"""
293 if level
> highestlevel
:
295 if level
<= maxlevel
:
299 if level
== 0 and highestlevel
> 0:
302 def check(self
, texrunner
):
303 lowestbracketlevel
= self
.baselevels(texrunner
.texmessageparsed
)
304 if lowestbracketlevel
is not None:
305 m
= self
.pattern
.search(lowestbracketlevel
)
307 if os
.access(m
.group("filename"), os
.R_OK
):
308 lowestbracketlevel
= lowestbracketlevel
[:m
.start()] + lowestbracketlevel
[m
.end():]
311 m
= self
.pattern
.search(lowestbracketlevel
)
313 texrunner
.texmessageparsed
= lowestbracketlevel
316 class _texmessageloadfd(_texmessageload
):
317 """validates the inclusion of font description files (fd-files)
318 - works like _texmessageload
319 - filename must end with .fd and no further text is allowed"""
321 pattern
= re
.compile(r
" *\((?P<filename>[^)]+.fd)\) *")
324 class _texmessagegraphicsload(_texmessageload
):
325 """validates the inclusion of files as the graphics packages writes it
326 - works like _texmessageload, but using "<" and ">" as delimiters
327 - filename must end with .eps and no further text is allowed"""
329 pattern
= re
.compile(r
" *<(?P<filename>[^>]+.eps)> *")
331 def baselevels(self
, s
, brackets
="<>", **args
):
332 return _texmessageload
.baselevels(self
, s
, brackets
=brackets
, **args
)
335 class _texmessageignore(_texmessageload
):
336 """validates any TeX/LaTeX response
337 - this might be used, when the expression is ok, but no suitable texmessage
339 - PLEASE: - consider writing suitable tex message parsers
340 - share your ideas/problems/solutions with others (use the PyX mailing lists)"""
342 __implements__
= _Itexmessage
344 def check(self
, texrunner
):
345 texrunner
.texmessageparsed
= ""
348 class _texmessagewarning(_texmessageload
):
349 """validates any TeX/LaTeX response
350 - this might be used, when the expression is ok, but no suitable texmessage
352 - PLEASE: - consider writing suitable tex message parsers
353 - share your ideas/problems/solutions with others (use the PyX mailing lists)"""
355 __implements__
= _Itexmessage
357 def check(self
, texrunner
):
358 if len(texrunner
.texmessageparsed
):
359 texrunner
.texmessageparsed
= ""
360 raise TexResultWarning("TeX result is ignored", texrunner
)
363 texmessage
.start
= _texmessagestart()
364 texmessage
.noaux
= _texmessagenoaux()
365 texmessage
.inputmarker
= _texmessageinputmarker()
366 texmessage
.pyxbox
= _texmessagepyxbox()
367 texmessage
.pyxpageout
= _texmessagepyxpageout()
368 texmessage
.texend
= _texmessagetexend()
369 texmessage
.emptylines
= _texmessageemptylines()
370 texmessage
.load
= _texmessageload()
371 texmessage
.loadfd
= _texmessageloadfd()
372 texmessage
.graphicsload
= _texmessagegraphicsload()
373 texmessage
.ignore
= _texmessageignore()
374 texmessage
.warning
= _texmessagewarning()
375 texmessage
.fontsubstitution
= _texmessagefontsubstitution()
378 ###############################################################################
380 ###############################################################################
382 _textattrspreamble
= ""
385 "a textattr defines a apply method, which modifies a (La)TeX expression"
387 class halign(attr
.exclusiveattr
, textattr
):
389 def __init__(self
, hratio
):
391 attr
.exclusiveattr
.__init
__(self
, halign
)
393 def apply(self
, expr
):
394 return r
"\gdef\PyXHAlign{%.5f}%s" % (self
.hratio
, expr
)
396 halign
.center
= halign(0.5)
397 halign
.right
= halign(1)
398 halign
.clear
= attr
.clearclass(halign
)
399 halign
.left
= halign
.clear
402 class _localattr
: pass
404 class _mathmode(attr
.attr
, textattr
, _localattr
):
407 def apply(self
, expr
):
408 return r
"$\displaystyle{%s}$" % expr
410 mathmode
= _mathmode()
411 nomathmode
= attr
.clearclass(_mathmode
)
414 class _phantom(attr
.attr
, textattr
, _localattr
):
417 def apply(self
, expr
):
418 return r
"\phantom{%s}" % expr
421 nophantom
= attr
.clearclass(_phantom
)
424 defaultsizelist
= ["normalsize", "large", "Large", "LARGE", "huge", "Huge", None, "tiny", "scriptsize", "footnotesize", "small"]
426 class size(attr
.sortbeforeattr
, textattr
, _localattr
):
429 def __init__(self
, sizeindex
=None, sizename
=None, sizelist
=defaultsizelist
):
430 if (sizeindex
is None and sizename
is None) or (sizeindex
is not None and sizename
is not None):
431 raise RuntimeError("either specify sizeindex or sizename")
432 attr
.sortbeforeattr
.__init
__(self
, [_mathmode
])
433 if sizeindex
is not None:
434 if sizeindex
>= 0 and sizeindex
< sizelist
.index(None):
435 self
.size
= sizelist
[sizeindex
]
436 elif sizeindex
< 0 and sizeindex
+ len(sizelist
) > sizelist
.index(None):
437 self
.size
= sizelist
[sizeindex
]
439 raise IndexError("index out of sizelist range")
443 def apply(self
, expr
):
444 return r
"\%s{%s}" % (self
.size
, expr
)
447 size
.scriptsize
= size
.script
= size(-3)
448 size
.footnotesize
= size
.footnote
= size(-2)
449 size
.small
= size(-1)
450 size
.normalsize
= size
.normal
= size(0)
456 size
.clear
= attr
.clearclass(size
)
459 _textattrspreamble
+= "\\newbox\\PyXBoxVBox%\n\\newdimen\PyXDimenVBox%\n"
461 class parbox_pt(attr
.sortbeforeexclusiveattr
, textattr
):
467 def __init__(self
, width
, baseline
=top
):
469 self
.baseline
= baseline
470 attr
.sortbeforeexclusiveattr
.__init
__(self
, parbox_pt
, [_localattr
])
472 def apply(self
, expr
):
473 if self
.baseline
== self
.top
:
474 return r
"\linewidth%.5ftruept\vtop{\hsize\linewidth{}%s}" % (self
.width
* 72.27 / 72, expr
)
475 elif self
.baseline
== self
.middle
:
476 return r
"\linewidth%.5ftruept\setbox\PyXBoxVBox=\hbox{{\vtop{\hsize\linewidth{}%s}}}\PyXDimenVBox=0.5\dp\PyXBoxVBox\setbox\PyXBoxVBox=\hbox{{\vbox{\hsize\linewidth{}%s}}}\advance\PyXDimenVBox by -0.5\dp\PyXBoxVBox\lower\PyXDimenVBox\box\PyXBoxVBox" % (self
.width
, expr
, expr
)
477 elif self
.baseline
== self
.bottom
:
478 return r
"\linewidth%.5ftruept\vbox{\hsize\linewidth{}%s}" % (self
.width
* 72.27 / 72, expr
)
480 RuntimeError("invalid baseline argument")
482 parbox_pt
.clear
= attr
.clearclass(parbox_pt
)
484 class parbox(parbox_pt
):
486 def __init__(self
, width
, **kwargs
):
487 parbox_pt
.__init
__(self
, unit
.topt(width
), **kwargs
)
489 parbox
.clear
= parbox_pt
.clear
492 _textattrspreamble
+= "\\newbox\\PyXBoxVAlign%\n\\newdimen\PyXDimenVAlign%\n"
494 class valign(attr
.sortbeforeexclusiveattr
, textattr
):
497 attr
.sortbeforeexclusiveattr
.__init
__(self
, valign
, [parbox_pt
, _localattr
])
499 class _valigntop(valign
):
501 def apply(self
, expr
):
502 return r
"\setbox\PyXBoxVAlign=\hbox{{%s}}\lower\ht\PyXBoxVAlign\box\PyXBoxVAlign" % expr
504 class _valignmiddle(valign
):
506 def apply(self
, expr
):
507 return r
"\setbox\PyXBoxVAlign=\hbox{{%s}}\PyXDimenVAlign=0.5\ht\PyXBoxVAlign\advance\PyXDimenVAlign by -0.5\dp\PyXBoxVAlign\lower\PyXDimenVAlign\box\PyXBoxVAlign" % expr
509 class _valignbottom(valign
):
511 def apply(self
, expr
):
512 return r
"\setbox\PyXBoxVAlign=\hbox{{%s}}\raise\dp\PyXBoxVAlign\box\PyXBoxVAlign" % expr
514 valign
.top
= _valigntop()
515 valign
.middle
= _valignmiddle()
516 valign
.bottom
= _valignbottom()
517 valign
.clear
= attr
.clearclass(valign
)
518 valign
.baseline
= valign
.clear
521 class _vshift(attr
.sortbeforeattr
, textattr
):
524 attr
.sortbeforeattr
.__init
__(self
, [valign
, parbox_pt
, _localattr
])
526 class vshift(_vshift
):
527 "vertical down shift by a fraction of a character height"
529 def __init__(self
, lowerratio
, heightstr
="0"):
530 _vshift
.__init
__(self
)
531 self
.lowerratio
= lowerratio
532 self
.heightstr
= heightstr
534 def apply(self
, expr
):
535 return r
"\setbox0\hbox{{%s}}\lower%.5f\ht0\hbox{{%s}}" % (self
.heightstr
, self
.lowerratio
, expr
)
537 class _vshiftmathaxis(_vshift
):
538 "vertical down shift by the height of the math axis"
540 def apply(self
, expr
):
541 return r
"\setbox0\hbox{$\vcenter{\vrule width0pt}$}\lower\ht0\hbox{{%s}}" % expr
544 vshift
.bottomzero
= vshift(0)
545 vshift
.middlezero
= vshift(0.5)
546 vshift
.topzero
= vshift(1)
547 vshift
.mathaxis
= _vshiftmathaxis()
548 vshift
.clear
= attr
.clearclass(_vshift
)
551 ###############################################################################
553 ###############################################################################
556 class _readpipe(threading
.Thread
):
557 """threaded reader of TeX/LaTeX output
558 - sets an event, when a specific string in the programs output is found
559 - sets an event, when the terminal ends"""
561 def __init__(self
, pipe
, expectqueue
, gotevent
, gotqueue
, quitevent
):
562 """initialize the reader
563 - pipe: file to be read from
564 - expectqueue: keeps the next InputMarker to be wait for
565 - gotevent: the "got InputMarker" event
566 - gotqueue: a queue containing the lines recieved from TeX/LaTeX
567 - quitevent: the "end of terminal" event"""
568 threading
.Thread
.__init
__(self
)
569 self
.setDaemon(1) # don't care if the output might not be finished (nevertheless, it shouldn't happen)
571 self
.expectqueue
= expectqueue
572 self
.gotevent
= gotevent
573 self
.gotqueue
= gotqueue
574 self
.quitevent
= quitevent
580 read
= self
.pipe
.readline() # read, what comes in
582 self
.expect
= self
.expectqueue
.get_nowait() # read, what should be expected
586 # universal EOL handling (convert everything into unix like EOLs)
587 # XXX is this necessary on pipes?
588 read
= read
.replace("\r", "").replace("\n", "") + "\n"
589 self
.gotqueue
.put(read
) # report, whats read
590 if self
.expect
is not None and read
.find(self
.expect
) != -1:
591 self
.gotevent
.set() # raise the got event, when the output was expected (XXX: within a single line)
592 read
= self
.pipe
.readline() # read again
594 self
.expect
= self
.expectqueue
.get_nowait()
599 if self
.expect
is not None and self
.expect
.find("PyXInputMarker") != -1:
600 raise RuntimeError("TeX/LaTeX finished unexpectedly")
604 class textbox(box
.rect
, canvas
._canvas
):
605 """basically a box.rect, but it contains a text created by the texrunner
606 - texrunner._text and texrunner.text return such an object
607 - _textbox instances can be inserted into a canvas
608 - the output is contained in a page of the dvifile available thru the texrunner"""
609 # TODO: shouldn't all boxes become canvases? how about inserts then?
611 def __init__(self
, x
, y
, left
, right
, height
, depth
, finishdvi
, attrs
):
613 - finishdvi is a method to be called to get the dvicanvas
614 (e.g. the finishdvi calls the setdvicanvas method)
615 - attrs are fillstyles"""
618 self
.width
= left
+ right
621 self
.texttrafo
= trafo
.scale(unit
.scale
["x"]).translated(x
, y
)
622 box
.rect
.__init
__(self
, x
- left
, y
- depth
, left
+ right
, depth
+ height
, abscenter
= (left
, depth
))
623 canvas
._canvas
.__init
__(self
)
624 self
.finishdvi
= finishdvi
625 self
.dvicanvas
= None
627 self
.insertdvicanvas
= 0
629 def transform(self
, *trafos
):
630 if self
.insertdvicanvas
:
631 raise RuntimeError("can't apply transformation after dvicanvas was inserted")
632 box
.rect
.transform(self
, *trafos
)
634 self
.texttrafo
= trafo
* self
.texttrafo
636 def setdvicanvas(self
, dvicanvas
):
637 if self
.dvicanvas
is not None:
638 raise RuntimeError("multiple call to setdvicanvas")
639 self
.dvicanvas
= dvicanvas
641 def ensuredvicanvas(self
):
642 if self
.dvicanvas
is None:
644 assert self
.dvicanvas
is not None, "finishdvi is broken"
645 if not self
.insertdvicanvas
:
646 self
.insert(self
.dvicanvas
, [self
.texttrafo
])
647 self
.insertdvicanvas
= 1
649 def marker(self
, marker
):
650 self
.ensuredvicanvas()
651 return self
.texttrafo
.apply(*self
.dvicanvas
.markers
[marker
])
654 self
.ensuredvicanvas()
655 return canvas
._canvas
.prolog(self
)
657 def outputPS(self
, file):
658 self
.ensuredvicanvas()
659 canvas
._canvas
.outputPS(self
, file)
661 def outputPDF(self
, file):
662 self
.ensuredvicanvas()
663 canvas
._canvas
.outputPDF(self
, file)
666 def _cleantmp(texrunner
):
667 """get rid of temporary files
668 - function to be registered by atexit
669 - files contained in usefiles are kept"""
670 if texrunner
.texruns
: # cleanup while TeX is still running?
671 texrunner
.expectqueue
.put_nowait(None) # do not expect any output anymore
672 if texrunner
.mode
== "latex": # try to immediately quit from TeX or LaTeX
673 texrunner
.texinput
.write("\n\\catcode`\\@11\\relax\\@@end\n")
675 texrunner
.texinput
.write("\n\\end\n")
676 texrunner
.texinput
.close() # close the input queue and
677 if not texrunner
.waitforevent(texrunner
.quitevent
): # wait for finish of the output
678 return # didn't got a quit from TeX -> we can't do much more
679 texrunner
.texruns
= 0
680 texrunner
.texdone
= 1
681 for usefile
in texrunner
.usefiles
:
682 extpos
= usefile
.rfind(".")
684 os
.rename(texrunner
.texfilename
+ usefile
[extpos
:], usefile
)
687 for file in glob
.glob("%s.*" % texrunner
.texfilename
):
692 if texrunner
.texdebug
is not None:
694 texrunner
.texdebug
.close()
695 texrunner
.texdebug
= None
701 """TeX/LaTeX interface
702 - runs TeX/LaTeX expressions instantly
703 - checks TeX/LaTeX response
704 - the instance variable texmessage stores the last TeX
706 - the instance variable texmessageparsed stores a parsed
707 version of texmessage; it should be empty after
708 texmessage.check was called, otherwise a TexResultError
710 - the instance variable errordebug controls the verbose
711 level of TexResultError"""
713 defaulttexmessagesstart
= [texmessage
.start
]
714 defaulttexmessagesdocclass
= [texmessage
.load
]
715 defaulttexmessagesbegindoc
= [texmessage
.load
, texmessage
.noaux
]
716 defaulttexmessagesend
= [texmessage
.texend
]
717 defaulttexmessagesdefaultpreamble
= [texmessage
.load
]
718 defaulttexmessagesdefaultrun
= [texmessage
.loadfd
, texmessage
.graphicsload
]
720 def __init__(self
, mode
="tex",
725 fontmaps
=config
.get("text", "fontmaps", "psfonts.map"),
726 waitfortex
=config
.getint("text", "waitfortex", 60),
727 showwaitfortex
=config
.getint("text", "showwaitfortex", 5),
728 texipc
=config
.getboolean("text", "texipc", 0),
735 texmessagesdocclass
=[],
736 texmessagesbegindoc
=[],
738 texmessagesdefaultpreamble
=[],
739 texmessagesdefaultrun
=[]):
741 if mode
!= "tex" and mode
!= "latex":
742 raise ValueError("mode \"TeX\" or \"LaTeX\" expected")
745 self
.docclass
= docclass
747 self
.usefiles
= usefiles
748 self
.fontmaps
= fontmaps
749 self
.waitfortex
= waitfortex
750 self
.showwaitfortex
= showwaitfortex
752 if texdebug
is not None:
753 if texdebug
[-4:] == ".tex":
754 self
.texdebug
= open(texdebug
, "w")
756 self
.texdebug
= open("%s.tex" % texdebug
, "w")
759 self
.dvidebug
= dvidebug
760 self
.errordebug
= errordebug
761 self
.dvicopy
= dvicopy
762 self
.pyxgraphics
= pyxgraphics
763 self
.texmessagesstart
= texmessagesstart
764 self
.texmessagesdocclass
= texmessagesdocclass
765 self
.texmessagesbegindoc
= texmessagesbegindoc
766 self
.texmessagesend
= texmessagesend
767 self
.texmessagesdefaultpreamble
= texmessagesdefaultpreamble
768 self
.texmessagesdefaultrun
= texmessagesdefaultrun
772 self
.preamblemode
= 1
776 self
.needdvitextboxes
= [] # when texipc-mode off
778 self
.textboxesincluded
= 0
779 savetempdir
= tempfile
.tempdir
780 tempfile
.tempdir
= os
.curdir
781 self
.texfilename
= os
.path
.basename(tempfile
.mktemp())
782 tempfile
.tempdir
= savetempdir
784 def waitforevent(self
, event
):
785 """waits verbosely with an timeout for an event
786 - observes an event while periodly while printing messages
787 - returns the status of the event (isSet)
788 - does not clear the event"""
789 if self
.showwaitfortex
:
792 while waited
< self
.waitfortex
and not hasevent
:
793 if self
.waitfortex
- waited
> self
.showwaitfortex
:
794 event
.wait(self
.showwaitfortex
)
795 waited
+= self
.showwaitfortex
797 event
.wait(self
.waitfortex
- waited
)
798 waited
+= self
.waitfortex
- waited
799 hasevent
= event
.isSet()
801 if waited
< self
.waitfortex
:
802 sys
.stderr
.write("*** PyX Info: still waiting for %s after %i (of %i) seconds...\n" % (self
.mode
, waited
, self
.waitfortex
))
804 sys
.stderr
.write("*** PyX Error: the timeout of %i seconds expired and %s did not respond.\n" % (waited
, self
.mode
))
807 event
.wait(self
.waitfortex
)
810 def execute(self
, expr
, texmessages
):
811 """executes expr within TeX/LaTeX
812 - if self.texruns is not yet set, TeX/LaTeX is initialized,
813 self.texruns is set and self.preamblemode is set
814 - the method must not be called, when self.texdone is already set
815 - expr should be a string or None
816 - when expr is None, TeX/LaTeX is stopped, self.texruns is unset and
817 self.texdone becomes set
818 - when self.preamblemode is set, the expr is passed directly to TeX/LaTeX
819 - when self.preamblemode is unset, the expr is passed to \ProcessPyXBox
820 - texmessages is a list of texmessage instances"""
822 if self
.texdebug
is not None:
823 self
.texdebug
.write("%% PyX %s texdebug file\n" % version
.version
)
824 self
.texdebug
.write("%% mode: %s\n" % self
.mode
)
825 self
.texdebug
.write("%% date: %s\n" % time
.asctime(time
.localtime(time
.time())))
826 for usefile
in self
.usefiles
:
827 extpos
= usefile
.rfind(".")
829 os
.rename(usefile
, self
.texfilename
+ usefile
[extpos
:])
832 texfile
= open("%s.tex" % self
.texfilename
, "w") # start with filename -> creates dvi file with that name
833 texfile
.write("\\relax%\n")
840 self
.texinput
, self
.texoutput
= os
.popen4("%s%s %s" % (self
.mode
, ipcflag
, self
.texfilename
), "t", 0)
842 # XXX: workaround for MS Windows (bufsize = 0 makes trouble!?)
843 self
.texinput
, self
.texoutput
= os
.popen4("%s%s %s" % (self
.mode
, ipcflag
, self
.texfilename
), "t")
844 atexit
.register(_cleantmp
, self
)
845 self
.expectqueue
= Queue
.Queue(1) # allow for a single entry only -> keeps the next InputMarker to be wait for
846 self
.gotevent
= threading
.Event() # keeps the got inputmarker event
847 self
.gotqueue
= Queue
.Queue(0) # allow arbitrary number of entries
848 self
.quitevent
= threading
.Event() # keeps for end of terminal event
849 self
.readoutput
= _readpipe(self
.texoutput
, self
.expectqueue
, self
.gotevent
, self
.gotqueue
, self
.quitevent
)
851 self
.fontmap
= dvifile
.readfontmap(self
.fontmaps
.split())
852 oldpreamblemode
= self
.preamblemode
853 self
.preamblemode
= 1
854 self
.execute("\\scrollmode\n\\raiseerror%\n" # switch to and check scrollmode
855 "\\def\\PyX{P\\kern-.3em\\lower.5ex\hbox{Y}\kern-.18em X}%\n" # just the PyX Logo
856 "\\gdef\\PyXHAlign{0}%\n" # global PyXHAlign (0.0-1.0) for the horizontal alignment, default to 0
857 "\\newbox\\PyXBox%\n" # PyXBox will contain the output
858 "\\newbox\\PyXBoxHAligned%\n" # PyXBox will contain the horizontal aligned output
859 "\\newdimen\\PyXDimenHAlignLT%\n" # PyXDimenHAlignLT/RT will contain the left/right extent
860 "\\newdimen\\PyXDimenHAlignRT%\n" +
861 _textattrspreamble
+ # insert preambles for textattrs macros
862 "\\long\\def\\ProcessPyXBox#1#2{%\n" # the ProcessPyXBox definition (#1 is expr, #2 is page number)
863 "\\setbox\\PyXBox=\\hbox{{#1}}%\n" # push expression into PyXBox
864 "\\PyXDimenHAlignLT=\\PyXHAlign\\wd\\PyXBox%\n" # calculate the left/right extent
865 "\\PyXDimenHAlignRT=\\wd\\PyXBox%\n"
866 "\\advance\\PyXDimenHAlignRT by -\\PyXDimenHAlignLT%\n"
867 "\\gdef\\PyXHAlign{0}%\n" # reset the PyXHAlign to the default 0
868 "\\immediate\\write16{PyXBox:page=#2," # write page and extents of this box to stdout
869 "lt=\\the\\PyXDimenHAlignLT,"
870 "rt=\\the\\PyXDimenHAlignRT,"
871 "ht=\\the\\ht\\PyXBox,"
872 "dp=\\the\\dp\\PyXBox:}%\n"
873 "\\setbox\\PyXBoxHAligned=\\hbox{\\kern-\\PyXDimenHAlignLT\\box\\PyXBox}%\n" # align horizontally
874 "\\ht\\PyXBoxHAligned0pt%\n" # baseline alignment (hight to zero)
875 "{\\count0=80\\count1=121\\count2=88\\count3=#2\\shipout\\box\\PyXBoxHAligned}}%\n" # shipout PyXBox to Page 80.121.88.<page number>
876 "\\def\\PyXInput#1{\\immediate\\write16{PyXInputMarker:executeid=#1:}}%\n" # write PyXInputMarker to stdout
877 "\\def\\PyXMarker#1{\\hskip0pt\\special{PyX:marker #1}}%\n", # write PyXMarker special into the dvi-file
878 self
.defaulttexmessagesstart
+ self
.texmessagesstart
)
879 os
.remove("%s.tex" % self
.texfilename
)
880 if self
.mode
== "tex":
881 if len(self
.lfs
) > 4 and self
.lfs
[-4:] == ".lfs":
884 lfsname
= "%s.lfs" % self
.lfs
885 for fulllfsname
in [lfsname
,
886 os
.path
.join(siteconfig
.lfsdir
, lfsname
)]:
888 lfsfile
= open(fulllfsname
, "r")
889 lfsdef
= lfsfile
.read()
895 allfiles
= (glob
.glob("*.lfs") +
896 glob
.glob(os
.path
.join(siteconfig
.lfsdir
, "*.lfs")))
901 lfsnames
.append(os
.path
.basename(f
)[:-4])
906 raise IOError("file '%s' is not available or not readable. Available LaTeX font size files (*.lfs): %s" % (lfsname
, lfsnames
))
908 raise IOError("file '%s' is not available or not readable. No LaTeX font size files (*.lfs) available. Check your installation." % lfsname
)
909 self
.execute(lfsdef
, [])
910 self
.execute("\\normalsize%\n", [])
911 self
.execute("\\newdimen\\linewidth%\n", [])
912 elif self
.mode
== "latex":
914 pyxdef
= os
.path
.join(siteconfig
.sharedir
, "pyx.def")
916 open(pyxdef
, "r").close()
918 IOError("file 'pyx.def' is not available or not readable. Check your installation or turn off the pyxgraphics option.")
919 pyxdef
= os
.path
.abspath(pyxdef
).replace(os
.sep
, "/")
920 self
.execute("\\makeatletter%\n"
921 "\\let\\saveProcessOptions=\\ProcessOptions%\n"
922 "\\def\\ProcessOptions{%\n"
923 "\\def\\Gin@driver{" + pyxdef
+ "}%\n"
924 "\\def\\c@lor@namefile{dvipsnam.def}%\n"
925 "\\saveProcessOptions}%\n"
928 if self
.docopt
is not None:
929 self
.execute("\\documentclass[%s]{%s}" % (self
.docopt
, self
.docclass
),
930 self
.defaulttexmessagesdocclass
+ self
.texmessagesdocclass
)
932 self
.execute("\\documentclass{%s}" % self
.docclass
,
933 self
.defaulttexmessagesdocclass
+ self
.texmessagesdocclass
)
934 self
.preamblemode
= oldpreamblemode
936 if expr
is not None: # TeX/LaTeX should process expr
937 self
.expectqueue
.put_nowait("PyXInputMarker:executeid=%i:" % self
.executeid
)
938 if self
.preamblemode
:
939 self
.expr
= ("%s%%\n" % expr
+
940 "\\PyXInput{%i}%%\n" % self
.executeid
)
943 self
.expr
= ("\\ProcessPyXBox{%s%%\n}{%i}%%\n" % (expr
, self
.page
) +
944 "\\PyXInput{%i}%%\n" % self
.executeid
)
945 else: # TeX/LaTeX should be finished
946 self
.expectqueue
.put_nowait("Transcript written on %s.log" % self
.texfilename
)
947 if self
.mode
== "latex":
948 self
.expr
= "\\end{document}%\n"
950 self
.expr
= "\\end%\n"
951 if self
.texdebug
is not None:
952 self
.texdebug
.write(self
.expr
)
953 self
.texinput
.write(self
.expr
)
954 gotevent
= self
.waitforevent(self
.gotevent
)
955 self
.gotevent
.clear()
956 if expr
is None and gotevent
: # TeX/LaTeX should have finished
959 self
.texinput
.close() # close the input queue and
960 gotevent
= self
.waitforevent(self
.quitevent
) # wait for finish of the output
964 self
.texmessage
+= self
.gotqueue
.get_nowait()
967 self
.texmessageparsed
= self
.texmessage
970 texmessage
.inputmarker
.check(self
)
971 if not self
.preamblemode
:
972 texmessage
.pyxbox
.check(self
)
973 texmessage
.pyxpageout
.check(self
)
974 texmessages
= attr
.mergeattrs(texmessages
)
975 for t
in texmessages
:
978 except TexResultWarning
:
979 traceback
.print_exc()
980 texmessage
.emptylines
.check(self
)
981 if len(self
.texmessageparsed
):
982 raise TexResultError("unhandled TeX response (might be an error)", self
)
984 raise TexResultError("TeX didn't respond as expected within the timeout period (%i seconds)." % self
.waitfortex
, self
)
987 """finish TeX/LaTeX and read the dvifile
988 - this method ensures that all textboxes can access their
990 self
.execute(None, self
.defaulttexmessagesend
+ self
.texmessagesend
)
992 os
.system("dvicopy %(t)s.dvi %(t)s.dvicopy > %(t)s.dvicopyout 2> %(t)s.dvicopyerr" % {"t": self
.texfilename
})
993 dvifilename
= "%s.dvicopy" % self
.texfilename
995 dvifilename
= "%s.dvi" % self
.texfilename
997 self
.dvifile
= dvifile
.dvifile(dvifilename
, self
.fontmap
, debug
=self
.dvidebug
)
999 for box
in self
.needdvitextboxes
:
1000 box
.setdvicanvas(self
.dvifile
.readpage([ord("P"), ord("y"), ord("X"), page
, 0, 0, 0, 0, 0, 0]))
1002 if self
.dvifile
.readpage(None) is not None:
1003 raise RuntimeError("end of dvifile expected")
1005 self
.needdvitextboxes
= []
1007 def reset(self
, reinit
=0):
1008 "resets the tex runner to its initial state (upto its record to old dvi file(s))"
1011 if self
.texdebug
is not None:
1012 self
.texdebug
.write("%s\n%% preparing restart of %s\n" % ("%"*80, self
.mode
))
1017 self
.preamblemode
= 1
1018 for expr
, texmessages
in self
.preambles
:
1019 self
.execute(expr
, texmessages
)
1020 if self
.mode
== "latex":
1021 self
.execute("\\begin{document}", self
.defaulttexmessagesbegindoc
+ self
.texmessagesbegindoc
)
1022 self
.preamblemode
= 0
1025 self
.preamblemode
= 1
1027 def set(self
, mode
=None,
1034 showwaitfortex
=None,
1041 texmessagesstart
=None,
1042 texmessagesdocclass
=None,
1043 texmessagesbegindoc
=None,
1044 texmessagesend
=None,
1045 texmessagesdefaultpreamble
=None,
1046 texmessagesdefaultrun
=None):
1047 """provide a set command for TeX/LaTeX settings
1048 - TeX/LaTeX must not yet been started
1049 - especially needed for the defaultrunner, where no access to
1050 the constructor is available"""
1052 raise RuntimeError("set not allowed -- TeX/LaTeX already started")
1053 if mode
is not None:
1055 if mode
!= "tex" and mode
!= "latex":
1056 raise ValueError("mode \"TeX\" or \"LaTeX\" expected")
1060 if docclass
is not None:
1061 self
.docclass
= docclass
1062 if docopt
is not None:
1063 self
.docopt
= docopt
1064 if usefiles
is not None:
1065 self
.usefiles
= usefiles
1066 if fontmaps
is not None:
1067 self
.fontmaps
= fontmaps
1068 if waitfortex
is not None:
1069 self
.waitfortex
= waitfortex
1070 if showwaitfortex
is not None:
1071 self
.showwaitfortex
= showwaitfortex
1072 if texipc
is not None:
1073 self
.texipc
= texipc
1074 if texdebug
is not None:
1075 if self
.texdebug
is not None:
1076 self
.texdebug
.close()
1077 if texdebug
[-4:] == ".tex":
1078 self
.texdebug
= open(texdebug
, "w")
1080 self
.texdebug
= open("%s.tex" % texdebug
, "w")
1081 if dvidebug
is not None:
1082 self
.dvidebug
= dvidebug
1083 if errordebug
is not None:
1084 self
.errordebug
= errordebug
1085 if dvicopy
is not None:
1086 self
.dvicopy
= dvicopy
1087 if pyxgraphics
is not None:
1088 self
.pyxgraphics
= pyxgraphics
1089 if errordebug
is not None:
1090 self
.errordebug
= errordebug
1091 if texmessagesstart
is not None:
1092 self
.texmessagesstart
= texmessagesstart
1093 if texmessagesdocclass
is not None:
1094 self
.texmessagesdocclass
= texmessagesdocclass
1095 if texmessagesbegindoc
is not None:
1096 self
.texmessagesbegindoc
= texmessagesbegindoc
1097 if texmessagesend
is not None:
1098 self
.texmessagesend
= texmessagesend
1099 if texmessagesdefaultpreamble
is not None:
1100 self
.texmessagesdefaultpreamble
= texmessagesdefaultpreamble
1101 if texmessagesdefaultrun
is not None:
1102 self
.texmessagesdefaultrun
= texmessagesdefaultrun
1104 def preamble(self
, expr
, texmessages
=[]):
1105 r
"""put something into the TeX/LaTeX preamble
1106 - in LaTeX, this is done before the \begin{document}
1107 (you might use \AtBeginDocument, when you're in need for)
1108 - it is not allowed to call preamble after calling the
1109 text method for the first time (for LaTeX this is needed
1110 due to \begin{document}; in TeX it is forced for compatibility
1111 (you should be able to switch from TeX to LaTeX, if you want,
1112 without breaking something)
1113 - preamble expressions must not create any dvi output
1114 - args might contain texmessage instances"""
1115 if self
.texdone
or not self
.preamblemode
:
1116 raise RuntimeError("preamble calls disabled due to previous text calls")
1117 texmessages
= self
.defaulttexmessagesdefaultpreamble
+ self
.texmessagesdefaultpreamble
+ texmessages
1118 self
.execute(expr
, texmessages
)
1119 self
.preambles
.append((expr
, texmessages
))
1121 PyXBoxPattern
= re
.compile(r
"PyXBox:page=(?P<page>\d+),lt=(?P<lt>-?\d*((\d\.?)|(\.?\d))\d*)pt,rt=(?P<rt>-?\d*((\d\.?)|(\.?\d))\d*)pt,ht=(?P<ht>-?\d*((\d\.?)|(\.?\d))\d*)pt,dp=(?P<dp>-?\d*((\d\.?)|(\.?\d))\d*)pt:")
1123 def text(self
, x
, y
, expr
, textattrs
=[], texmessages
=[]):
1124 """create text by passing expr to TeX/LaTeX
1125 - returns a textbox containing the result from running expr thru TeX/LaTeX
1126 - the box center is set to x, y
1127 - *args may contain attr parameters, namely:
1128 - textattr instances
1129 - texmessage instances
1130 - trafo._trafo instances
1131 - style.fillstyle instances"""
1133 raise ValueError("None expression is invalid")
1135 self
.reset(reinit
=1)
1137 if self
.preamblemode
:
1138 if self
.mode
== "latex":
1139 self
.execute("\\begin{document}", self
.defaulttexmessagesbegindoc
+ self
.texmessagesbegindoc
)
1140 self
.preamblemode
= 0
1142 if self
.texipc
and self
.dvicopy
:
1143 raise RuntimeError("texipc and dvicopy can't be mixed up")
1144 textattrs
= attr
.mergeattrs(textattrs
) # perform cleans
1145 attr
.checkattrs(textattrs
, [textattr
, trafo
.trafo_pt
, style
.fillstyle
])
1146 trafos
= attr
.getattrs(textattrs
, [trafo
.trafo_pt
])
1147 fillstyles
= attr
.getattrs(textattrs
, [style
.fillstyle
])
1148 textattrs
= attr
.getattrs(textattrs
, [textattr
])
1149 # reverse loop over the merged textattrs (last is applied first)
1150 lentextattrs
= len(textattrs
)
1151 for i
in range(lentextattrs
):
1152 expr
= textattrs
[lentextattrs
-1-i
].apply(expr
)
1153 self
.execute(expr
, self
.defaulttexmessagesdefaultrun
+ self
.texmessagesdefaultrun
+ texmessages
)
1156 self
.dvifile
= dvifile
.dvifile("%s.dvi" % self
.texfilename
, self
.fontmap
, debug
=self
.dvidebug
)
1157 match
= self
.PyXBoxPattern
.search(self
.texmessage
)
1158 if not match
or int(match
.group("page")) != self
.page
:
1159 raise TexResultError("box extents not found", self
)
1160 left
, right
, height
, depth
= [float(xxx
)*72/72.27*unit
.x_pt
for xxx
in match
.group("lt", "rt", "ht", "dp")]
1161 box
= textbox(x
, y
, left
, right
, height
, depth
, self
.finishdvi
, fillstyles
)
1165 box
.setdvicanvas(self
.dvifile
.readpage([ord("P"), ord("y"), ord("X"), self
.page
, 0, 0, 0, 0, 0, 0]))
1167 self
.needdvitextboxes
.append(box
)
1170 def text_pt(self
, x
, y
, expr
, *args
, **kwargs
):
1171 return self
.text(x
* unit
.t_pt
, y
* unit
.t_pt
, expr
, *args
, **kwargs
)
1173 PyXVariableBoxPattern
= re
.compile(r
"PyXVariableBox:page=(?P<page>\d+),par=(?P<par>\d+),prevgraf=(?P<prevgraf>\d+):")
1175 def textboxes(self
, text
, pageshapes
):
1176 # this is some experimental code to put text into several boxes
1177 # while the bounding shape changes from box to box (rectangles only)
1178 # first we load sev.tex
1179 if not self
.textboxesincluded
:
1180 self
.execute(r
"\input textboxes.tex", [texmessage
.load
])
1181 self
.textboxesincluded
= 1
1182 # define page shapes
1183 pageshapes_str
= "\\hsize=%.5ftruept%%\n\\vsize=%.5ftruept%%\n" % (72.27/72*unit
.topt(pageshapes
[0][0]), 72.27/72*unit
.topt(pageshapes
[0][1]))
1184 pageshapes_str
+= "\\lohsizes={%\n"
1185 for hsize
, vsize
in pageshapes
[1:]:
1186 pageshapes_str
+= "{\\global\\hsize=%.5ftruept}%%\n" % (72.27/72*unit
.topt(hsize
))
1187 pageshapes_str
+= "{\\relax}%\n}%\n"
1188 pageshapes_str
+= "\\lovsizes={%\n"
1189 for hsize
, vsize
in pageshapes
[1:]:
1190 pageshapes_str
+= "{\\global\\vsize=%.5ftruept}%%\n" % (72.27/72*unit
.topt(vsize
))
1191 pageshapes_str
+= "{\\relax}%\n}%\n"
1197 self
.execute(pageshapes_str
, [])
1198 parnos_str
= "}{".join(parnos
)
1200 parnos_str
= "{%s}" % parnos_str
1201 parnos_str
= "\\parnos={%s{\\relax}}%%\n" % parnos_str
1202 self
.execute(parnos_str
, [])
1203 parshapes_str
= "\\parshapes={%%\n%s%%\n{\\relax}%%\n}%%\n" % "%\n".join(parshapes
)
1204 self
.execute(parshapes_str
, [])
1205 self
.execute("\\global\\count0=1%%\n"
1206 "\\global\\parno=0%%\n"
1207 "\\global\\myprevgraf=0%%\n"
1208 "\\global\\showprevgraf=0%%\n"
1209 "\\global\\outputtype=0%%\n"
1210 "\\global\\leastcost=10000000%%\n"
1212 "\\vfill\\supereject%%\n" % text
, [texmessage
.ignore
])
1214 if self
.dvifile
is None:
1215 self
.dvifile
= dvifile
.dvifile("%s.dvi" % self
.texfilename
, self
.fontmap
, debug
=self
.dvidebug
)
1217 raise RuntimeError("textboxes currently needs texipc")
1220 lastparshapes
= parshapes
1223 lastpar
= prevgraf
= -1
1224 m
= self
.PyXVariableBoxPattern
.search(self
.texmessage
)
1227 page
= int(m
.group("page"))
1228 assert page
== pages
1229 par
= int(m
.group("par"))
1230 prevgraf
= int(m
.group("prevgraf"))
1231 if page
<= len(pageshapes
):
1232 width
= 72.27/72*unit
.topt(pageshapes
[page
-1][0])
1234 width
= 72.27/72*unit
.topt(pageshapes
[-1][0])
1235 if page
< len(pageshapes
):
1236 nextwidth
= 72.27/72*unit
.topt(pageshapes
[page
][0])
1238 nextwidth
= 72.27/72*unit
.topt(pageshapes
[-1][0])
1241 # a new paragraph is to be broken
1242 parnos
.append(str(par
))
1243 parshape
= " 0pt ".join(["%.5ftruept" % width
for i
in range(prevgraf
)])
1245 parshape
= " 0pt " + parshape
1246 parshapes
.append("{\\parshape %i%s 0pt %.5ftruept}" % (prevgraf
+ 1, parshape
, nextwidth
))
1247 elif prevgraf
== lastprevgraf
:
1250 # we have to append the breaking of the previous paragraph
1251 oldparshape
= " ".join(parshapes
[-1].split(' ')[2:2+2*lastprevgraf
])
1252 oldparshape
= oldparshape
.split('}')[0]
1254 oldparshape
= " " + oldparshape
1255 parshape
= " 0pt ".join(["%.5ftruept" % width
for i
in range(prevgraf
- lastprevgraf
)])
1257 parshape
= " 0pt " + parshape
1260 parshapes
[-1] = "{\\parshape %i%s%s 0pt %.5ftruept}" % (prevgraf
+ 1, oldparshape
, parshape
, nextwidth
)
1262 lastprevgraf
= prevgraf
1264 m
= self
.PyXVariableBoxPattern
.search(self
.texmessage
, nextpos
)
1266 for i
in range(pages
):
1267 result
.append(self
.dvifile
.readpage([i
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
1268 if parnos
== lastparnos
and parshapes
== lastparshapes
:
1272 raise TexResultError("Too many loops in textboxes ", texrunner
)
1275 # the module provides an default texrunner and methods for direct access
1276 defaulttexrunner
= texrunner()
1277 reset
= defaulttexrunner
.reset
1278 set = defaulttexrunner
.set
1279 preamble
= defaulttexrunner
.preamble
1280 text
= defaulttexrunner
.text
1281 text_pt
= defaulttexrunner
.text_pt
1283 def escapestring(s
):
1284 """escape special TeX/LaTeX characters
1286 Returns a string, where some special characters of standard
1287 TeX/LaTeX are replaced by appropriate escaped versions. Note
1288 that we cannot handle the three ASCII characters '{', '}',
1289 and '\' that way, since they do not occure in the TeX default
1290 encoding and thus are more likely to need some special handling.
1291 All other ASCII characters should usually (but not always)
1294 # ASCII strings only
1299 s
= s
[:i
] + "\\" + s
[i
:]
1302 s
= s
[:i
] + r
"\string" + s
[i
:]