/usr/lib/python3/dist-packages/pyx/document.py is in python3-pyx 0.14.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | # -*- encoding: utf-8 -*-
#
#
# Copyright (C) 2005-2011 Jörg Lehmann <joergl@users.sourceforge.net>
# Copyright (C) 2005-2011 André Wobst <wobsta@users.sourceforge.net>
#
# This file is part of PyX (http://pyx.sourceforge.net/).
#
# PyX is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PyX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyX; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import logging, sys
from . import bbox, pswriter, pdfwriter, svgwriter, trafo, style, unit
logger = logging.getLogger("pyx")
class paperformat:
def __init__(self, width, height, name=None):
self.width = width
self.height = height
self.name = name
paperformat.A5 = paperformat(148.5 * unit.t_mm, 210 * unit.t_mm, "A5")
paperformat.A4 = paperformat(210 * unit.t_mm, 297 * unit.t_mm, "A4")
paperformat.A3 = paperformat(297 * unit.t_mm, 420 * unit.t_mm, "A3")
paperformat.A2 = paperformat(420 * unit.t_mm, 594 * unit.t_mm, "A2")
paperformat.A1 = paperformat(594 * unit.t_mm, 840 * unit.t_mm, "A1")
paperformat.A0 = paperformat(840 * unit.t_mm, 1188 * unit.t_mm, "A0")
paperformat.A0b = paperformat(910 * unit.t_mm, 1370 * unit.t_mm, None) # dedicated to our friends in Augsburg
paperformat.Letter = paperformat(8.5 * unit.t_inch, 11 * unit.t_inch, "Letter")
paperformat.Legal = paperformat(8.5 * unit.t_inch, 14 * unit.t_inch, "Legal")
def _paperformatfromstring(name):
return getattr(paperformat, name.capitalize())
class page:
def __init__(self, canvas, pagename=None, paperformat=None, rotated=0, centered=1, fittosize=0,
margin=1*unit.t_cm, bboxenlarge=1*unit.t_pt, bbox=None):
self.canvas = canvas
self.pagename = pagename
# support for deprecated string specification of paper formats
try:
paperformat + ""
except:
self.paperformat = paperformat
else:
self.paperformat = _paperformatfromstring(paperformat)
logger.warning("specification of paperformat by string is deprecated, use document.paperformat.%s instead" % paperformat.capitalize())
self.rotated = rotated
self.centered = centered
self.fittosize = fittosize
self.margin = margin
self.bboxenlarge = bboxenlarge
self.pagebbox = bbox
def _process(self, processMethod, contentfile, writer, context, registry, bbox):
# usually, it is the bbox of the canvas enlarged by self.bboxenlarge, but
# it might be a different bbox as specified in the page constructor
assert not bbox
if self.pagebbox:
bbox.set(self.pagebbox)
else:
bbox.set(self.canvas.bbox()) # this bbox is not accurate
bbox.enlarge(self.bboxenlarge)
# check whether we expect a page trafo and use a temporary canvas to insert the
# page canvas
if self.paperformat and (self.rotated or self.centered or self.fittosize) and bbox:
# calculate the pagetrafo
paperwidth, paperheight = self.paperformat.width, self.paperformat.height
# center (optionally rotated) output on page
if self.rotated:
pagetrafo = trafo.rotate(90).translated(paperwidth, 0)
if self.centered or self.fittosize:
if not self.fittosize and (bbox.height() > paperwidth or bbox.width() > paperheight):
logger.warning("content exceeds the papersize")
pagetrafo = pagetrafo.translated(-0.5*(paperwidth - bbox.height()) + bbox.bottom(),
0.5*(paperheight - bbox.width()) - bbox.left())
else:
if not self.fittosize and (bbox.width() > paperwidth or bbox.height() > paperheight):
logger.warning("content exceeds the papersize")
pagetrafo = trafo.translate(0.5*(paperwidth - bbox.width()) - bbox.left(),
0.5*(paperheight - bbox.height()) - bbox.bottom())
if self.fittosize:
if 2*self.margin > paperwidth or 2*self.margin > paperheight:
raise ValueError("Margins too broad for selected paperformat. Aborting.")
paperwidth -= 2 * self.margin
paperheight -= 2 * self.margin
# scale output to pagesize - margins
if self.rotated:
sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
else:
sfactor = min(unit.topt(paperwidth)/bbox.width_pt(), unit.topt(paperheight)/bbox.height_pt())
pagetrafo = pagetrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)
bbox.transform(pagetrafo)
from . import canvas as canvasmodule
cc = canvasmodule.canvas()
cc.insert(self.canvas, [pagetrafo])
else:
cc = self.canvas
if processMethod != "processSVG":
# for SVG we write the pyx defaults as part of the svg node attributes in the writer
getattr(style.linewidth.normal, processMethod)(contentfile, writer, context, registry)
if self.pagebbox:
bbox = bbox.copy() # don't alter the bbox provided to the constructor -> use a copy
getattr(cc, processMethod)(contentfile, writer, context, registry, bbox)
def processPS(self, *args):
self._process("processPS", *args)
def processPDF(self, *args):
self._process("processPDF", *args)
def processSVG(self, *args):
self._process("processSVG", *args)
class _noclose:
def __init__(self, f):
self.f = f
def __enter__(self):
return self.f
def __exit__(self, type, value, tb):
pass
def _outputstream(file, suffix):
if file is None:
if not sys.argv[0].endswith(".py"):
raise RuntimeError("could not auto-guess filename")
return open("%s.%s" % (sys.argv[0][:-3], suffix), "wb")
if file == "-":
return _noclose(sys.stdout.buffer)
try:
file.write(b"")
except:
if not file.endswith(".%s" % suffix):
return open("%s.%s" % (file, suffix), "wb")
return open(file, "wb")
else:
return _noclose(file)
class document:
"""holds a collection of page instances which are output as pages of a document"""
def __init__(self, pages=None):
if pages is None:
self.pages = []
else:
self.pages = pages
def append(self, page):
self.pages.append(page)
def writeEPSfile(self, file=None, **kwargs):
with _outputstream(file, "eps") as f:
pswriter.EPSwriter(self, f, **kwargs)
def writePSfile(self, file=None, **kwargs):
with _outputstream(file, "ps") as f:
pswriter.PSwriter(self, f, **kwargs)
def writePDFfile(self, file=None, **kwargs):
with _outputstream(file, "pdf") as f:
pdfwriter.PDFwriter(self, f, **kwargs)
def writeSVGfile(self, file=None, **kwargs):
with _outputstream(file, "svg") as f:
svgwriter.SVGwriter(self, f, **kwargs)
def writetofile(self, filename, **kwargs):
for suffix, method in [("eps", pswriter.EPSwriter),
("ps", pswriter.PSwriter),
("pdf", pdfwriter.PDFwriter),
("svg", svgwriter.SVGwriter)]:
if filename.endswith(".{}".format(suffix)):
with open(filename, "wb") as f:
method(self, f, **kwargs)
return
raise ValueError("unknown file extension")
|