2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 (La)TeX interface of PyX
28 This module provides the classes tex and latex, which can be inserted into a
29 PyX canvas. The method (la)tex.text prints text, while (la)tex.textwd,
30 (la)tex.textht, and (la)tex.textdp appraise the width, height, and depth of a
31 text, respectively. The method (la)tex.define can be used to define macros in
35 import os
, string
, tempfile
, sys
, md5
, traceback
, time
, StringIO
, re
, atexit
36 import base
, unit
, epsfile
, color
, attrlist
39 ################################################################################
41 ################################################################################
45 """base class for all TeX attributes"""
50 class fontsize(_texattr
):
52 """fontsize TeX attribute"""
54 def __init__(self
, value
):
61 fontsize
.tiny
= fontsize("tiny")
62 fontsize
.scriptsize
= fontsize("scriptsize")
63 fontsize
.footnotesize
= fontsize("footnotesize")
64 fontsize
.small
= fontsize("small")
65 fontsize
.normalsize
= fontsize("normalsize")
66 fontsize
.large
= fontsize("large")
67 fontsize
.Large
= fontsize("Large")
68 fontsize
.LARGE
= fontsize("LARGE")
69 fontsize
.huge
= fontsize("huge")
70 fontsize
.Huge
= fontsize("Huge")
73 class halign(_texattr
):
75 """tex horizontal align attribute"""
77 def __init__(self
, value
):
80 def __cmp__(self
, other
):
81 if other
is None: return 1
82 return cmp(self
.value
, other
.value
)
87 halign
.left
= halign("left")
88 halign
.center
= halign("center")
89 halign
.right
= halign("right")
92 class valign(_texattr
):
94 """abstract tex vertical align attribute"""
96 def __init__(self
, hsize
):
100 class _valignvtop(valign
):
102 """tex top vertical align attribute"""
107 valign
.top
= _valignvtop
110 class _valignvbox(valign
):
112 """tex bottom vertical align attribute"""
117 valign
.bottom
= _valignvbox
120 class direction(_texattr
):
122 """tex output direction attribute"""
124 def __init__(self
, value
):
128 return "%.5f" % self
.value
132 direction
.horizontal
= direction(0)
133 direction
.vertical
= direction(90)
134 direction
.upsidedown
= direction(180)
135 direction
.rvertical
= direction(270)
138 class style(_texattr
):
140 """tex style modification attribute"""
142 def __init__(self
, praefix
, suffix
):
143 self
.praefix
= praefix
146 def ModifyCmd(self
, cmd
):
147 return self
.praefix
+ cmd
+ self
.suffix
150 style
.text
= style("", "")
151 style
.math
= style("$\displaystyle{}", "$")
154 ################################################################################
155 # TeX message handlers
156 ################################################################################
158 class msghandler(_texattr
):
160 """abstract base class for tex message handlers
162 A message handler has to provide a parsemsg method. It gets a string and
163 returns a string. Within the parsemsg method the handler may remove any
164 part of the message it is familiar with."""
166 def removeemptylines(self
, msg
):
167 """any message parser may use this method to remove empty lines"""
169 msg
= re
.sub("^(\n)*", "", msg
)
170 msg
= re
.sub("(\n){3,}", "\n\n", msg
)
171 msg
= re
.sub("(\n)+$", "\n", msg
)
175 class _msghandlershowall(msghandler
):
177 """a message handler, which shows all messages"""
179 def parsemsg(self
, msg
):
183 msghandler
.showall
= _msghandlershowall()
185 class _msghandlerhideload(msghandler
):
187 """a message handler, which hides all messages inside proper '(filename' and ')'
188 the string filename has to be a readable file"""
190 def parsemsg(self
, msg
):
195 if newlevel
and (c
in (list(string
.whitespace
) + ["(", ")"])):
196 if filestr
not in ("c", "C"):
199 if not os
.access(filestr
,os
.R_OK
):
215 # replace msg only if loop was completed and no ")" is missing
217 msg
= self
.removeemptylines(newstr
)
221 msghandler
.hideload
= _msghandlerhideload()
224 class _msghandlerhidegraphicsload(msghandler
):
226 """a message handler, which hides all messages like '<filename>'
227 the string filename has to be a readable file"""
229 def parsemsg(self
, msg
):
242 if not os
.access(filestr
,os
.R_OK
):
243 newstr
+= "<" + filestr
+ ">"
249 # replace msg only if loop was completed and no ">" missing
251 msg
= self
.removeemptylines(newstr
)
255 msghandler
.hidegraphicsload
= _msghandlerhidegraphicsload()
258 class _msghandlerhidefontwarning(msghandler
):
260 """a message handler, which hides LaTeX font warnings, e.g.
261 Messages starting with 'LaTeX Font Warning: ' which might be
262 continued on following lines by '(Font) '"""
264 def parsemsg(self
, msg
):
265 msglines
= string
.split(msg
, "\n")
268 for line
in msglines
:
269 if fontwarning
and line
[:20] != "(Font) ":
271 if not fontwarning
and line
[:20] == "LaTeX Font Warning: ":
274 newmsglines
.append(line
)
275 newmsg
= reduce(lambda x
, y
: x
+ y
+ "\n", newmsglines
, "")
276 return self
.removeemptylines(newmsg
)
279 msghandler
.hidefontwarning
= _msghandlerhidefontwarning()
282 class _msghandlerhidebuterror(msghandler
):
284 """a message handler, hides all messages whenever they do
285 not contain a line starting with '! '"""
287 def parsemsg(self
, msg
):
288 # the "\n" + msg instead of msg itself is needed, if the message starts with "! "
289 if string
.find("\n" + msg
, "\n! ") != -1:
295 msghandler
.hidebuterror
= _msghandlerhidebuterror()
298 class _msghandlerhideall(msghandler
):
300 """a message handler, which hides all messages"""
302 def parsemsg(self
, msg
):
306 msghandler
.hideall
= _msghandlerhideall()
309 ################################################################################
311 ################################################################################
313 class missextents(_texattr
):
315 """abstract base class for handling missing extents
317 A miss extent class has to provide a misshandler method."""
320 _missextentsreturnzero_report
= 0
321 def _missextentsreturnzero_printreport():
323 pyx.tex: Some requested extents were missing and have been replaced by zero.
324 Please run the file again to get correct extents.\n""")
326 class _missextentsreturnzero(missextents
):
328 def misshandler(self
, texinstance
):
329 global _missextentsreturnzero_report
330 if not _missextentsreturnzero_report
:
331 atexit
.register(_missextentsreturnzero_printreport
)
332 _missextentsreturnzero_report
= 1
333 return map(lambda x
: unit
.t_pt(0), texinstance
.BoxCmds
[0].CmdExtents
)
336 missextents
.returnzero
= _missextentsreturnzero()
339 class _missextentsreturnzeroquiet(missextents
):
341 def misshandler(self
, texinstance
):
342 return map(lambda x
: unit
.t_pt(0), texinstance
.BoxCmds
[0].CmdExtents
)
345 missextents
.returnzeroquiet
= _missextentsreturnzeroquiet()
348 class _missextentsraiseerror(missextents
):
350 def misshandler(self
, texinstance
):
351 raise TexMissExtentError
354 missextents
.raiseerror
= _missextentsraiseerror()
357 class _missextentscreateextents(missextents
):
359 def misshandler(self
, texinstance
):
360 if isinstance(texinstance
, latex
):
361 storeauxfilename
= texinstance
.auxfilename
362 texinstance
.auxfilename
= None
363 texinstance
.DoneRunTex
= 0
365 texinstance
.DoneRunTex
= 0
366 if isinstance(texinstance
, latex
):
367 texinstance
.auxfilename
= storeauxfilename
368 return texinstance
.BoxCmds
[0].Extents(texinstance
.BoxCmds
[0].CmdExtents
,
369 missextents
.returnzero
, texinstance
)
372 missextents
.createextents
= _missextentscreateextents()
375 class _missextentscreateallextents(missextents
):
377 def misshandler(self
, texinstance
):
378 if isinstance(texinstance
, latex
):
379 storeauxfilename
= texinstance
.auxfilename
380 texinstance
.auxfilename
= None
381 texinstance
.DoneRunTex
= 0
382 storeextents
= texinstance
.BoxCmds
[0].CmdExtents
[0]
383 texinstance
.BoxCmds
[0].CmdExtents
= [_extent
.wd
, _extent
.ht
, _extent
.dp
]
385 texinstance
.BoxCmds
[0].CmdExtents
[0] = storeextents
386 texinstance
.DoneRunTex
= 0
387 if isinstance(texinstance
, latex
):
388 texinstance
.auxfilename
= storeauxfilename
389 return texinstance
.BoxCmds
[0].Extents(texinstance
.BoxCmds
[0].CmdExtents
,
390 missextents
.returnzero
, texinstance
)
393 missextents
.createallextents
= _missextentscreateallextents()
396 ################################################################################
398 ################################################################################
400 class TexExcept(base
.PyXExcept
):
405 class TexLeftParenthesisError(TexExcept
):
408 return "no matching parenthesis for '{' found"
411 class TexRightParenthesisError(TexExcept
):
414 return "no matching parenthesis for '}' found"
417 class TexHalignError(TexExcept
):
420 return "unkown halign"
423 class TexValignError(TexExcept
):
426 return "unkown valign"
429 class TexDefAfterBoxError(TexExcept
):
432 return "definition commands not allowed after output commands"
435 class TexMissExtentError(TexExcept
):
438 return "requested tex extent not available"
441 ################################################################################
442 # modules internal stuff
443 ################################################################################
447 def __init__(self
, value
):
454 _extent
.wd
= _extent("wd")
455 _extent
.ht
= _extent("ht")
456 _extent
.dp
= _extent("dp")
461 """class for all user supplied commands"""
463 PyxMarker
= "PyxMarker"
464 BeginPyxMarker
= "Begin" + PyxMarker
465 EndPyxMarker
= "End" + PyxMarker
467 def __init__(self
, Marker
, Stack
, msghandlers
):
470 self
.msghandlers
= msghandlers
472 def TexParenthesisCheck(self
, Cmd
):
473 """check for proper usage of "{" and "}" in Cmd"""
478 if c
== "{" and not esc
:
480 if c
== "}" and not esc
:
483 raise TexRightParenthesisError
489 raise TexLeftParenthesisError
491 def BeginMarkerStr(self
):
492 return "%s[%s]" % (self
.BeginPyxMarker
, self
.Marker
, )
494 def WriteBeginMarker(self
, file):
495 file.write("\\immediate\\write16{%s}%%\n" % self
.BeginMarkerStr())
497 def EndMarkerStr(self
):
498 return "%s[%s]" % (self
.EndPyxMarker
, self
.Marker
, )
500 def WriteEndMarker(self
, file):
501 file.write("\\immediate\\write16{%s}%%\n" % self
.EndMarkerStr())
503 def WriteError(self
, msg
):
504 sys
.stderr
.write("Traceback (innermost last):\n")
505 traceback
.print_list(self
.Stack
)
506 sys
.stderr
.write("(La)TeX Message:\n" + msg
+ "\n")
508 def CheckMarkerError(self
, file):
509 """read markers and identify the message"""
511 line
= file.readline()
512 while (line
!= "") and (line
[:-1] != self
.BeginMarkerStr()):
513 line
= file.readline()
515 line
= file.readline()
516 while (line
!= "") and (line
[:-1] != self
.EndMarkerStr()):
518 line
= file.readline()
523 # check if message can be ignored
526 for msghandler
in self
.msghandlers
:
527 parsedmsg
= msghandler
.parsemsg(parsedmsg
)
529 if c
not in string
.whitespace
:
530 self
.WriteError(parsedmsg
)
534 class _DefCmd(_TexCmd
):
536 """definition commands"""
538 def __init__(self
, DefCmd
, Marker
, Stack
, msghandlers
):
539 _TexCmd
.__init
__(self
, Marker
, Stack
, msghandlers
)
540 self
.TexParenthesisCheck(DefCmd
)
541 self
.DefCmd
= "%s%%\n" % DefCmd
543 def write(self
, file):
544 self
.WriteBeginMarker(file)
545 file.write(self
.DefCmd
)
546 self
.WriteEndMarker(file)
551 """print parameters for a BoxCmd (data structure)"""
553 def __init__(self
, x
, y
, halign
, direction
, color
):
557 self
.direction
= direction
561 class _BoxCmd(_TexCmd
):
563 """BoxCmd (for printing text and getting extents)"""
565 def __init__(self
, DefCmdsStr
, BoxCmd
, style
, fontsize
, valign
, Marker
, Stack
, msghandlers
):
566 _TexCmd
.__init
__(self
, Marker
, Stack
, msghandlers
)
567 self
.TexParenthesisCheck(BoxCmd
)
568 self
.DefCmdsStr
= DefCmdsStr
569 self
.BoxCmd
= "{%s}%%\n" % BoxCmd
# add another "{" to ensure, that everything goes into the Box
570 self
.CmdPuts
= [] # list, where to put the command
571 self
.CmdExtents
= [] # list, which extents are requested
573 self
.BoxCmd
= style
.ModifyCmd(self
.BoxCmd
)
574 if valign
is not None:
575 if isinstance(valign
, _valignvtop
):
576 self
.BoxCmd
= "\\linewidth%.5ftruept\\vtop{\\hsize\\linewidth{%s}}" % \
577 (unit
.topt(valign
.hsize
) * 72.27/72, self
.BoxCmd
, )
578 elif isinstance(valign
, _valignvbox
):
579 self
.BoxCmd
= "\\linewidth%.5ftruept\\vbox{\\hsize\\linewidth{%s}}" % \
580 (unit
.topt(valign
.hsize
) * 72.27/72, self
.BoxCmd
, )
583 self
.BoxCmd
= "\\setbox\\localbox=\\hbox{\\%s%s}%%\n" % (fontsize
, self
.BoxCmd
, )
585 def __cmp__(self
, other
):
586 if other
is None: return 1
587 return cmp(self
.BoxCmd
, other
.BoxCmd
)
591 def write(self
, file):
592 self
.WriteBeginMarker(file)
593 file.write(self
.BoxCmd
)
594 self
.WriteEndMarker(file)
595 for CmdExtent
in self
.CmdExtents
:
596 file.write("\\immediate\\write\\sizefile{%s:%s:%s:\\the\\%s\\localbox}%%\n" %
597 (self
.MD5(), CmdExtent
, time
.time(), CmdExtent
, ))
598 for CmdPut
in self
.CmdPuts
:
600 file.write("{\\vbox to0pt{\\kern%.5ftruept\\hbox{\\kern%.5ftruept\\ht\\localbox0pt" %
601 (-CmdPut
.y
, CmdPut
.x
))
603 if CmdPut
.direction
!= direction
.horizontal
:
604 file.write("\\special{ps: gsave currentpoint currentpoint translate " +
605 str(CmdPut
.direction
) + " neg rotate neg exch neg exch translate }")
606 if CmdPut
.color
!= color
.gray
.black
:
607 file.write("\\special{ps: ")
608 CmdPut
.color
.write(file)
610 if CmdPut
.halign
== halign
.left
:
612 elif CmdPut
.halign
== halign
.center
:
613 file.write("\kern-.5\wd\localbox")
614 elif CmdPut
.halign
== halign
.right
:
615 file.write("\kern-\wd\localbox")
618 file.write("\\copy\\localbox")
620 if CmdPut
.color
!= color
.gray
.black
:
621 file.write("\\special{ps: ")
622 color
.gray
.black
.write(file)
624 if CmdPut
.direction
!= direction
.horizontal
:
625 file.write("\\special{ps: currentpoint grestore moveto }")
626 file.write("}\\vss}\\nointerlineskip}%\n")
629 """creates an MD5 hex string for texinit + Cmd"""
633 s
= md5
.md5(self
.DefCmdsStr
+ self
.BoxCmd
).digest()
636 r
= r
+ h
[(i
>> 4) & 0xF] + h
[i
& 0xF]
639 def Put(self
, x
, y
, halign
, direction
, color
):
640 self
.CmdPuts
.append(_CmdPut(x
, y
, halign
, direction
, color
))
642 def Extents(self
, extents
, missextents
, texinstance
):
643 """get sizes from previous LaTeX run"""
645 for extent
in extents
:
646 if extent
not in self
.CmdExtents
:
647 self
.CmdExtents
.append(extent
)
650 for extent
in extents
:
651 s
= self
.MD5() + ":" + str(extent
)
652 for size
in texinstance
.Sizes
:
653 if size
[:len(s
)] == s
:
654 texpt
= float(string
.rstrip(size
.split(":")[3][:-3]))
655 result
.append(unit
.t_pt(texpt
* 72.0 / 72.27))
662 # extent was not found --- temporarily remove all other commands in
663 # order to allow the misshandler to access everything it ever wants
664 storeboxcmds
= texinstance
.BoxCmds
665 storecmdputs
= self
.CmdPuts
666 storecmdextents
= self
.CmdExtents
667 texinstance
.BoxCmds
= [self
, ]
669 self
.CmdExtents
= extents
671 result
= missextents
.misshandler(texinstance
)
673 texinstance
.BoxCmds
= storeboxcmds
674 self
.CmdPuts
= storecmdputs
675 self
.CmdExtents
= storecmdextents
679 ################################################################################
681 ################################################################################
683 class _tex(base
.PSCmd
, attrlist
.attrlist
):
685 """major parts are of tex and latex class are shared and implemented here"""
687 def __init__(self
, defaultmsghandlers
=msghandler
.hideload
,
688 defaultmissextents
=missextents
.returnzero
,
690 if isinstance(defaultmsghandlers
, msghandler
):
691 self
.defaultmsghandlers
= (defaultmsghandlers
,)
693 self
.defaultmsghandlers
= defaultmsghandlers
694 self
.defaultmissextents
= defaultmissextents
695 self
.texfilename
= texfilename
697 self
.DefCmdsStr
= None
701 if len(os
.path
.basename(sys
.argv
[0])):
702 basename
= os
.path
.basename(sys
.argv
[0])
703 if basename
[-3:] == ".py":
704 basename
= basename
[:-3]
705 self
.SizeFileName
= os
.path
.join(os
.getcwd(), basename
+ ".size")
707 self
.SizeFileName
= os
.path
.join(os
.getcwd(), "pyxput.size")
709 file = open(self
.SizeFileName
, "r")
710 self
.Sizes
= file.readlines()
716 return traceback
.extract_stack(sys
._getframe
())
718 def _execute(self
, command
):
719 if os
.system(command
):
720 sys
.stderr
.write("The exit code of the following command was non-zero:\n" + command
+
721 """\nUsually, additional information causing this trouble appears closeby.
722 However, you may check the origin by keeping all temporary files.
723 In order to achieve this, you have to specify a texfilename in the
724 constructor of the class pyx.(la)tex. You can then try to run the
725 command by yourself.\n""")
727 def _createaddfiles(self
, tempname
):
730 def _removeaddfiles(self
, tempname
):
733 def _executetex(self
, tempname
):
736 def _executedvips(self
, tempname
):
737 self
._execute
("dvips -O0in,11in -E -o %(t)s.eps %(t)s.dvi > %(t)s.dvipsout 2> %(t)s.dvipserr" % {"t": tempname
})
740 """run (La)TeX & dvips, report errors, fill self.abbox & self.epsdata"""
746 mktemp
= str(self
.texfilename
)
748 storetempdir
= tempfile
.tempdir
749 tempfile
.tempdir
= os
.curdir
750 mktemp
= tempfile
.mktemp()
751 tempfile
.tempdir
= storetempdir
752 tempname
= os
.path
.basename(mktemp
)
754 self
._createaddfiles
(tempname
)
756 texfile
= open(tempname
+ ".tex", "w")
758 texfile
.write("\\nonstopmode%\n")
759 texfile
.write("\\def\PyX{P\\kern-.3em\\lower.5ex\\hbox{Y}\\kern-.18em X}%\n")
760 texfile
.write("\\newwrite\\sizefile%\n\\newbox\\localbox%\n\\newbox\\pagebox%\n")
761 texfile
.write("{\\catcode`\\~=12\\immediate\\openout\\sizefile=%s.size\\relax}%%\n" % tempname
)
763 for Cmd
in self
.DefCmds
:
766 texfile
.write("\\setbox\\pagebox=\\vbox{%\n")
768 for Cmd
in self
.BoxCmds
:
771 texfile
.write("}\n\\immediate\\closeout\\sizefile\n\\shipout\\copy\\pagebox\n")
772 texfile
.write(self
._endcmd
())
775 self
._executetex
(tempname
)
778 outfile
= open(tempname
+ ".texout", "r")
779 for Cmd
in self
.DefCmds
+ self
.BoxCmds
:
780 Cmd
.CheckMarkerError(outfile
)
783 sys
.stderr
.write("""An unexpected error occured while reading the (La)TeX output.
784 May be, you just have no disk space available. Or something badly
785 in your commands caused (La)TeX to give up completely. Or your
786 (La)TeX installation might be broken at all.
787 You may try to check the origin by keeping all temporary files.
788 In order to achieve this, you have to specify a texfilename in the
789 constructor of the class pyx.tex. You can then try to run (La)TeX
792 if not os
.access(tempname
+ ".dvi", 0):
793 sys
.stderr
.write("""Can't find the dvi file which should be produced by (La)TeX.
794 May be, you just have no disk space available. Or something badly
795 in your commands caused (La)TeX to give up completely. Or your
796 (La)TeX installation might be broken at all.
797 You may try to check the origin by keeping all temporary files.
798 In order to achieve this, you have to specify a texfilename in the
799 constructor of the class pyx.tex. You can then try to run (La)TeX
803 self
._executedvips
(tempname
)
804 if not os
.access(tempname
+ ".eps", 0):
805 sys
.stderr
.write("""Error reading the eps file which should be produced by dvips.
806 May be, you just have no disk space available. Or something badly
807 in your commands caused dvips to give up completely. Or your
808 (La)TeX installation might be broken at all.
809 You may try to check the origin by keeping all temporary files.
810 In order to achieve this, you have to specify a texfilename in the
811 constructor of the class pyx.tex. You can then try to run dvips
814 aepsfile
= epsfile
.epsfile(0, 0, tempname
+ ".eps", translatebbox
=0, clip
=0)
815 self
.abbox
= aepsfile
.bbox()
816 self
.aprolog
= aepsfile
.prolog()
817 epsdatafile
= StringIO
.StringIO()
818 aepsfile
.write(epsdatafile
)
819 self
.epsdata
= epsdatafile
.getvalue()
823 OldSizes
= self
.Sizes
826 NewSizeFile
= open(tempname
+ ".size", "r")
827 NewSizes
= NewSizeFile
.readlines()
832 if (len(NewSizes
) != 0) or (len(OldSizes
) != 0):
833 SizeFile
= open(self
.SizeFileName
, "w")
834 SizeFile
.writelines(NewSizes
)
835 self
.Sizes
= NewSizes
836 for OldSize
in OldSizes
:
837 OldSizeSplit
= OldSize
.split(":")
838 for NewSize
in NewSizes
:
839 if NewSize
.split(":")[0:2] == OldSizeSplit
[0:2]:
842 if time
.time() < float(OldSizeSplit
[2]) + 60*60*24: # we keep size results for one day
843 SizeFile
.write(OldSize
)
844 self
.Sizes
.append(OldSize
)
846 if not self
.texfilename
:
847 for suffix
in ("tex", "log", "size", "dvi", "eps", "texout", "texerr", "dvipsout", "dvipserr", ):
849 os
.unlink(tempname
+ "." + suffix
)
853 self
._removeaddfiles
(tempname
)
864 def write(self
, file):
866 file.writelines(self
.epsdata
)
868 def define(self
, Cmd
, *attrs
):
869 if len(self
.BoxCmds
):
870 raise TexDefAfterBoxError
872 self
.attrcheck(attrs
, (), (msghandler
,))
873 self
.DefCmds
.append(_DefCmd(Cmd
,
874 len(self
.DefCmds
)+ len(self
.BoxCmds
),
876 self
.attrgetall(attrs
, msghandler
, self
.defaultmsghandlers
)))
878 def _insertcmd(self
, Cmd
, *attrs
):
879 if not len(self
.BoxCmds
):
881 self
.DefCmdsStr
= reduce(lambda x
,y
: x
+ y
.DefCmd
, self
.DefCmds
, "")
882 mystyle
= self
.attrget(attrs
, style
, style
.text
)
883 myfontsize
= self
.attrget(attrs
, fontsize
, fontsize
.normalsize
)
884 myvalign
= self
.attrget(attrs
, valign
, None)
885 mymsghandlers
= self
.attrgetall(attrs
, msghandler
, self
.defaultmsghandlers
)
886 MyCmd
= _BoxCmd(self
.DefCmdsStr
, Cmd
, mystyle
, myfontsize
, myvalign
,
887 len(self
.DefCmds
) + len(self
.BoxCmds
), self
._getstack
(), mymsghandlers
)
888 if MyCmd
not in self
.BoxCmds
:
889 self
.BoxCmds
.append(MyCmd
)
890 for Cmd
in self
.BoxCmds
:
892 UseCmd
= Cmd
# we could use MyCmd directly if we have just inserted it before
893 # (that's due to the side effect, that append doesn't make a copy of the element,
894 # but we ignore this here -- we don't want to depend on this side effect)
897 def _text(self
, x
, y
, Cmd
, *attrs
):
898 """print Cmd at (x, y) --- position parameters in postscipt points"""
901 self
.attrcheck(attrs
, (style
, fontsize
, halign
, valign
, direction
, color
.color
), (msghandler
,))
902 myhalign
= self
.attrget(attrs
, halign
, halign
.left
)
903 mydirection
= self
.attrget(attrs
, direction
, direction
.horizontal
)
904 mycolor
= self
.attrget(attrs
, color
.color
, color
.gray
.black
)
905 self
._insertcmd
(Cmd
, *attrs
).Put(x
* 72.27 / 72.0, y
* 72.27 / 72.0, myhalign
, mydirection
, mycolor
)
907 def text(self
, x
, y
, Cmd
, *attrs
):
908 """print Cmd at (x, y)"""
910 self
._text
(unit
.topt(x
), unit
.topt(y
), Cmd
, *attrs
)
912 def textwd(self
, Cmd
, *attrs
):
913 """get width of Cmd"""
916 self
.attrcheck(attrs
, (style
, fontsize
, missextents
), (msghandler
,))
917 mymissextents
= self
.attrget(attrs
, missextents
, self
.defaultmissextents
)
918 return self
._insertcmd
(Cmd
, *attrs
).Extents((_extent
.wd
, ), mymissextents
, self
)[0]
920 def textht(self
, Cmd
, *attrs
):
921 """get height of Cmd"""
924 self
.attrcheck(attrs
, (style
, fontsize
, valign
, missextents
), (msghandler
,))
925 mymissextents
= self
.attrget(attrs
, missextents
, self
.defaultmissextents
)
926 return self
._insertcmd
(Cmd
, *attrs
).Extents((_extent
.ht
, ), mymissextents
, self
)[0]
929 def textdp(self
, Cmd
, *attrs
):
930 """get depth of Cmd"""
933 self
.attrcheck(attrs
, (style
, fontsize
, valign
, missextents
), (msghandler
,))
934 mymissextents
= self
.attrget(attrs
, missextents
, self
.defaultmissextents
)
935 return self
._insertcmd
(Cmd
, *attrs
).Extents((_extent
.dp
, ), mymissextents
, self
)[0]
940 """tex class adds the specializations to _tex needed for tex"""
942 def __init__(self
, lfs
="10pt", **addargs
):
943 _tex
.__init
__(self
, **addargs
)
944 # XXX other ways for creating font sizes?
946 LocalLfsName
= str(lfs
) + ".lfs"
947 lfsdef
= open(LocalLfsName
, "r").read()
951 SysLfsName
= os
.path
.join(sys
.prefix
, "share", "pyx", str(lfs
) + ".lfs")
952 lfsdef
= open(SysLfsName
, "r").read()
954 SysLfsName
= os
.path
.join(os
.path
.dirname(__file__
), "lfs", str(lfs
) + ".lfs")
955 lfsdef
= open(SysLfsName
, "r").read()
957 files
= map(lambda x
: x
[:-4],
958 filter(lambda x
: x
[-4:] == ".lfs",
960 os
.listdir(os
.path
.join(sys
.prefix
, "share", "pyx")),
961 os
.listdir(os
.path
.join(os
.path
.dirname(__file__
), "lfs"))))
962 raise IOError("file '%s.lfs' not found. Available latex font sizes:\n%s" % (lfs
, files
))
964 self
.define("\\newdimen\\linewidth%\n\\hsize0truein%\n\\vsize0truein%\n\\hoffset-1truein%\n\\voffset-1truein")
966 def _beginboxcmds(self
):
972 def _executetex(self
, tempname
):
973 self
._execute
("tex %(t)s.tex > %(t)s.texout 2> %(t)s.texerr" % {"t": tempname
})
978 """latex class adds the specializations to _tex needed for latex"""
980 def __init__(self
, docclass
="article", docopt
=None, auxfilename
=None, **addargs
):
981 _tex
.__init
__(self
, **addargs
)
982 self
.auxfilename
= auxfilename
984 self
.define("\\documentclass[" + str(docopt
) + "]{" + str(docclass
) + "}")
986 self
.define("\\documentclass{" + str(docclass
) + "}")
987 self
.define("\\hsize0truein%\n\\vsize0truein%\n\\hoffset-1truein%\n\\voffset-1truein")
989 def _beginboxcmds(self
):
990 self
.define("\\begin{document}")
993 return "\\end{document}\n"
995 def _createaddfiles(self
, tempname
):
996 if self
.auxfilename
is not None:
999 os
.rename(self
.auxfilename
+ ".aux", tempname
+ ".aux")
1005 auxfile
= open(tempname
+ ".aux", "w")
1006 auxfile
.write("\\relax\n")
1009 def _executetex(self
, tempname
):
1010 self
._execute
("latex %(t)s.tex > %(t)s.texout 2> %(t)s.texerr" % {"t": tempname
})
1012 def _removeaddfiles(self
, tempname
):
1013 if self
.auxfilename
is not None:
1014 os
.rename(tempname
+ ".aux", self
.auxfilename
+ ".aux")
1016 os
.unlink(tempname
+ ".aux")