def __init__(self, document, file, **kwargs): _PSwriter.__init__(self, **kwargs) if len(document.pages) != 1: raise ValueError("EPS file can be constructed out of a single page document only") page = document.pages[0] canvas = page.canvas pagefile = cStringIO.StringIO() registry = PSregistry() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) file.write("%!PS-Adobe-3.0 EPSF-3.0\n") if pagebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % pagebbox.highrestuple_pt()) self.writeinfo(file) file.write("%%EndComments\n") file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") file.write(pagefile.getvalue()) pagefile.close() file.write("showpage\n") file.write("%%Trailer\n") file.write("%%EOF\n")
def bbox(self): bb = bbox.empty() bb.includepoint_pt(*self.pdftrafo.apply_pt(0.0, 0.0)) bb.includepoint_pt(*self.pdftrafo.apply_pt(0.0, 1.0)) bb.includepoint_pt(*self.pdftrafo.apply_pt(1.0, 0.0)) bb.includepoint_pt(*self.pdftrafo.apply_pt(1.0, 1.0)) return bb
def processPDF(self, file, writer, context, registry, bbox): context = context() if self.items: file.write("q\n") # gsave nbbox = bboxmodule.empty() for item in self.items: if isinstance(item, type1font.text_pt): if not context.textregion: file.write("BT\n") context.textregion = 1 else: if context.textregion: file.write("ET\n") context.textregion = 0 context.font = None item.processPDF(file, writer, context, registry, nbbox) if context.textregion: file.write("ET\n") context.textregion = 0 context.font = None # update bounding bbox nbbox.transform(self.trafo) if self.clipbbox is not None: nbbox *= self.clipbbox bbox += nbbox file.write("Q\n") # grestore
def processPDF(self, file, writer, context, registry, bbox): context = context() context.trafo = context.trafo * self.trafo if self.items: file.write("q\n") # gsave nbbox = bboxmodule.empty() for item in self.items: if isinstance(item, style.fillstyle): context.fillstyles.append(item) if not writer.text_as_path: if isinstance(item, font.text_pt): if not context.textregion: file.write("BT\n") context.textregion = 1 else: if context.textregion: file.write("ET\n") context.textregion = 0 context.selectedfont = None item.processPDF(file, writer, context, registry, nbbox) if context.textregion: file.write("ET\n") context.textregion = 0 context.selectedfont = None # update bounding bbox nbbox.transform(self.trafo) if self.clipbbox is not None: nbbox *= self.clipbbox bbox += nbbox file.write("Q\n") # grestore
def __init__(self, page, writer, registry): PDFobject.__init__(self, registry, "content") contentfile = cStringIO.StringIO() self.bbox = bbox.empty() acontext = context() page.processPDF(contentfile, writer, acontext, registry, self.bbox) self.content = contentfile.getvalue() contentfile.close()
def bbox(self): """return bbox of path""" if self.pathitems: bbox = self.pathitems[0].createbbox() context = self.pathitems[0].createcontext() for pathitem in self.pathitems[1:]: pathitem.updatebbox(bbox, context) return bbox else: return bboxmodule.empty()
def processPS(self, file, writer, context, registry, bbox): context = context() if self.items: file.write("gsave\n") nbbox = bboxmodule.empty() for item in self.items: item.processPS(file, writer, context, registry, nbbox) # update bounding bbox nbbox.transform(self.trafo) if self.clipbbox is not None: nbbox *= self.clipbbox bbox += nbbox file.write("grestore\n")
def __init__(self, document, file): if len(document.pages) != 1: raise ValueError( "EPS file can be constructed out of a single page document only" ) page = document.pages[0] canvas = page.canvas try: file.write("") except: filename = file if not filename.endswith(".eps"): filename += ".eps" try: file = open(filename, "w") except IOError: raise IOError("cannot open output file") else: filename = "stream" pagefile = cStringIO.StringIO() registry = PSregistry() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) file.write("%!PS-Adobe-3.0 EPSF-3.0\n") if pagebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % pagebbox.highrestuple_pt()) file.write("%%%%Creator: PyX %s\n" % version.version) file.write("%%%%Title: %s\n" % filename) file.write("%%%%CreationDate: %s\n" % time.asctime(time.localtime(time.time()))) file.write("%%EndComments\n") file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") file.write(pagefile.getvalue()) pagefile.close() file.write("showpage\n") file.write("%%Trailer\n") file.write("%%EOF\n")
def processPDF(self, file, writer, context, registry, bbox): # we need to keep track of the resources used by the pattern, hence # we create our own registry, which we merge immediately in the main registry patternregistry = pdfwriter.PDFregistry() patternfile = cStringIO.StringIO() realpatternbbox = bboxmodule.empty() canvas.canvas.processPDF(self, patternfile, writer, pdfwriter.context(), patternregistry, realpatternbbox) patternproc = patternfile.getvalue() patternfile.close() registry.mergeregistry(patternregistry) if self.xstep is None: xstep = unit.topt(realpatternbbox.width()) else: xstep = unit.topt(self.xstep) if self.ystep is None: ystep = unit.topt(realpatternbbox.height()) else: ystep = unit.topt(self.ystep) if not xstep: raise ValueError("xstep in pattern cannot be zero") if not ystep: raise ValueError("ystep in pattern cannot be zero") patternbbox = self.patternbbox or realpatternbbox.enlarged(5 * unit.pt) patterntrafo = self.patterntrafo or trafo.trafo() registry.add( PDFpattern(self.id, self.patterntype, self.painttype, self.tilingtype, patternbbox, xstep, ystep, patterntrafo, patternproc, writer, registry, patternregistry)) # activate pattern if context.colorspace != "Pattern": # we only set the fill color space (see next comment) file.write("/Pattern cs\n") context.colorspace = "Pattern" if context.strokeattr: # using patterns as stroke colors doesn't seem to work, so # we just don't do this... warnings.warn("ignoring stroke color for patterns in PDF") if context.fillattr: file.write("/%s scn\n" % self.id)
def bbox(self): """returns bounding box of canvas Note that this bounding box doesn't take into account the linewidths, so is less accurate than the one used when writing the output to a file. """ obbox = bboxmodule.empty() for cmd in self.items: obbox += cmd.bbox() # transform according to our global transformation and # intersect with clipping bounding box (which has already been # transformed in canvas.__init__()) obbox.transform(self.trafo) if self.clipbbox is not None: obbox *= self.clipbbox return obbox
def __init__(self, document, file): if len(document.pages) != 1: raise ValueError("EPS file can be constructed out of a single page document only") page = document.pages[0] canvas = page.canvas try: file.write("") except: filename = file if not filename.endswith(".eps"): filename += ".eps" try: file = open(filename, "w") except IOError: raise IOError("cannot open output file") else: filename = "stream" pagefile = cStringIO.StringIO() registry = PSregistry() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) file.write("%!PS-Adobe-3.0 EPSF-3.0\n") if pagebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % pagebbox.highrestuple_pt()) file.write("%%%%Creator: PyX %s\n" % version.version) file.write("%%%%Title: %s\n" % filename) file.write("%%%%CreationDate: %s\n" % time.asctime(time.localtime(time.time()))) file.write("%%EndComments\n") file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") file.write(pagefile.getvalue()) pagefile.close() file.write("showpage\n") file.write("%%Trailer\n") file.write("%%EOF\n")
def processPDF(self, file, writer, context, registry, bbox): # we need to keep track of the resources used by the pattern, hence # we create our own registry, which we merge immediately in the main registry patternregistry = pdfwriter.PDFregistry() patternfile = cStringIO.StringIO() realpatternbbox = bboxmodule.empty() canvas._canvas.processPDF(self, patternfile, writer, pdfwriter.context(), patternregistry, realpatternbbox) patternproc = patternfile.getvalue() patternfile.close() registry.mergeregistry(patternregistry) if self.xstep is None: xstep = unit.topt(realpatternbbox.width()) else: xstep = unit.topt(self.xstep) if self.ystep is None: ystep = unit.topt(realpatternbbox.height()) else: ystep = unit.topt(self.ystep) if not xstep: raise ValueError("xstep in pattern cannot be zero") if not ystep: raise ValueError("ystep in pattern cannot be zero") patternbbox = self.patternbbox or realpatternbbox.enlarged(5*unit.pt) patterntrafo = self.patterntrafo or trafo.trafo() registry.add(PDFpattern(self.id, self.patterntype, self.painttype, self.tilingtype, patternbbox, xstep, ystep, patterntrafo, patternproc, writer, registry, patternregistry)) # activate pattern if context.colorspace != "Pattern": # we only set the fill color space (see next comment) file.write("/Pattern cs\n") context.colorspace = "Pattern" if context.strokeattr: # using patterns as stroke colors doesn't seem to work, so # we just don't do this... warnings.warn("ignoring stroke color for patterns in PDF") if context.fillattr: file.write("/%s scn\n"% self.id)
def processPS(self, file, writer, context, registry, bbox): # process pattern, letting it register its resources and calculate the bbox of the pattern patternfile = cStringIO.StringIO() realpatternbbox = bboxmodule.empty() canvas.canvas.processPS(self, patternfile, writer, pswriter.context(), registry, realpatternbbox) patternproc = patternfile.getvalue() patternfile.close() if self.xstep is None: xstep = unit.topt(realpatternbbox.width()) else: xstep = unit.topt(self.xstep) if self.ystep is None: ystep = unit.topt(realpatternbbox.height()) else: ystep = unit.topt(self.ystep) if not xstep: raise ValueError("xstep in pattern cannot be zero") if not ystep: raise ValueError("ystep in pattern cannot be zero") patternbbox = self.patternbbox or realpatternbbox.enlarged( self.bboxenlarge) patternprefix = "\n".join( ("<<", "/PatternType %d" % self.patterntype, "/PaintType %d" % self.painttype, "/TilingType %d" % self.tilingtype, "/BBox [%g %g %g %g]" % patternbbox.highrestuple_pt(), "/XStep %g" % xstep, "/YStep %g" % ystep, "/PaintProc {\nbegin\n")) patterntrafostring = self.patterntrafo is None and "matrix" or str( self.patterntrafo) patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring registry.add( pswriter.PSdefinition( self.id, "".join((patternprefix, patternproc, patternsuffix)))) # activate pattern file.write("%s setpattern\n" % self.id)
def processPS(self, file, writer, context, registry, bbox): # process pattern, letting it register its resources and calculate the bbox of the pattern patternfile = cStringIO.StringIO() realpatternbbox = bboxmodule.empty() canvas._canvas.processPS(self, patternfile, writer, pswriter.context(), registry, realpatternbbox) patternproc = patternfile.getvalue() patternfile.close() if self.xstep is None: xstep = unit.topt(realpatternbbox.width()) else: xstep = unit.topt(self.xstep) if self.ystep is None: ystep = unit.topt(realpatternbbox.height()) else: ystep = unit.topt(self.ystep) if not xstep: raise ValueError("xstep in pattern cannot be zero") if not ystep: raise ValueError("ystep in pattern cannot be zero") patternbbox = self.patternbbox or realpatternbbox.enlarged(5*unit.pt) patternprefix = "\n".join(("<<", "/PatternType %d" % self.patterntype, "/PaintType %d" % self.painttype, "/TilingType %d" % self.tilingtype, "/BBox[%d %d %d %d]" % patternbbox.lowrestuple_pt(), "/XStep %g" % xstep, "/YStep %g" % ystep, "/PaintProc {\nbegin\n")) patterntrafostring = self.patterntrafo is None and "matrix" or str(self.patterntrafo) patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring registry.add(pswriter.PSdefinition(self.id, "".join((patternprefix, patternproc, patternsuffix)))) # activate pattern file.write("%s setpattern\n" % self.id)
def __init__(self, document, file, **kwargs): _PSwriter.__init__(self, **kwargs) if len(document.pages) != 1: raise ValueError( "EPS file can be constructed out of a single page document only" ) page = document.pages[0] canvas = page.canvas pagefile = cStringIO.StringIO() registry = PSregistry() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) file.write("%!PS-Adobe-3.0 EPSF-3.0\n") if pagebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % pagebbox.highrestuple_pt()) self.writeinfo(file) file.write("%%EndComments\n") file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") file.write(pagefile.getvalue()) pagefile.close() file.write("showpage\n") file.write("%%Trailer\n") file.write("%%EOF\n")
def bbox(self): return bboxmodule.empty()
def bbox(self): # TODO: see TODO in bbox method of canvasitem return bboxmodule.empty()
def bbox(self): # as a canvasitem a clipping path has NO influence on the bbox... return bboxmodule.empty()
def __init__(self, document, file, writebbox=False, **kwargs): _PSwriter.__init__(self, **kwargs) # We first have to process the content of the pages, writing them into the stream pagesfile # Doing so, we fill the registry and also calculate the page bounding boxes, which are # stored in page._bbox for every page pagesfile = cStringIO.StringIO() registry = PSregistry() # calculated bounding boxes of the whole document documentbbox = bbox.empty() for nr, page in enumerate(document.pages): # process contents of page pagefile = cStringIO.StringIO() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) documentbbox += pagebbox pagesfile.write("%%%%Page: %s %d\n" % (page.pagename is None and str(nr + 1) or page.pagename, nr + 1)) if page.paperformat: pagesfile.write("%%%%PageMedia: %s\n" % page.paperformat.name) pagesfile.write("%%%%PageOrientation: %s\n" % (page.rotated and "Landscape" or "Portrait")) if pagebbox and writebbox: pagesfile.write("%%%%PageBoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) # page setup section pagesfile.write("%%BeginPageSetup\n") pagesfile.write("/pgsave save def\n") pagesfile.write("%%EndPageSetup\n") pagesfile.write(pagefile.getvalue()) pagefile.close() pagesfile.write("pgsave restore\n") pagesfile.write("showpage\n") pagesfile.write("%%PageTrailer\n") file.write("%!PS-Adobe-3.0\n") if documentbbox and writebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % documentbbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % documentbbox.highrestuple_pt()) self.writeinfo(file) # required paper formats paperformats = {} for page in document.pages: if page.paperformat: paperformats[page.paperformat] = page.paperformat first = 1 for paperformat in paperformats.values(): if first: file.write("%%DocumentMedia: ") first = 0 else: file.write("%%+ ") file.write("%s %d %d 75 white ()\n" % (paperformat.name, unit.topt( paperformat.width), unit.topt(paperformat.height))) # file.write(%%DocumentNeededResources: ") # register not downloaded fonts here file.write("%%%%Pages: %d\n" % len(document.pages)) file.write("%%PageOrder: Ascend\n") file.write("%%EndComments\n") # document defaults section #file.write("%%BeginDefaults\n") #file.write("%%EndDefaults\n") # document prolog section file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") # document setup section #file.write("%%BeginSetup\n") #file.write("%%EndSetup\n") file.write(pagesfile.getvalue()) pagesfile.close() file.write("%%Trailer\n") file.write("%%EOF\n")
def __init__(self, document, file, writebbox=False, **kwargs): _PSwriter.__init__(self, **kwargs) # We first have to process the content of the pages, writing them into the stream pagesfile # Doing so, we fill the registry and also calculate the page bounding boxes, which are # stored in page._bbox for every page pagesfile = cStringIO.StringIO() registry = PSregistry() # calculated bounding boxes of the whole document documentbbox = bbox.empty() for nr, page in enumerate(document.pages): # process contents of page pagefile = cStringIO.StringIO() acontext = context() pagebbox = bbox.empty() page.processPS(pagefile, self, acontext, registry, pagebbox) documentbbox += pagebbox pagesfile.write("%%%%Page: %s %d\n" % (page.pagename is None and str(nr+1) or page.pagename, nr+1)) if page.paperformat: pagesfile.write("%%%%PageMedia: %s\n" % page.paperformat.name) pagesfile.write("%%%%PageOrientation: %s\n" % (page.rotated and "Landscape" or "Portrait")) if pagebbox and writebbox: pagesfile.write("%%%%PageBoundingBox: %d %d %d %d\n" % pagebbox.lowrestuple_pt()) # page setup section pagesfile.write("%%BeginPageSetup\n") pagesfile.write("/pgsave save def\n") pagesfile.write("%%EndPageSetup\n") pagesfile.write(pagefile.getvalue()) pagefile.close() pagesfile.write("pgsave restore\n") pagesfile.write("showpage\n") pagesfile.write("%%PageTrailer\n") file.write("%!PS-Adobe-3.0\n") if documentbbox and writebbox: file.write("%%%%BoundingBox: %d %d %d %d\n" % documentbbox.lowrestuple_pt()) file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % documentbbox.highrestuple_pt()) self.writeinfo(file) # required paper formats paperformats = {} for page in document.pages: if page.paperformat: paperformats[page.paperformat] = page.paperformat first = 1 for paperformat in paperformats.values(): if first: file.write("%%DocumentMedia: ") first = 0 else: file.write("%%+ ") file.write("%s %d %d 75 white ()\n" % (paperformat.name, unit.topt(paperformat.width), unit.topt(paperformat.height))) # file.write(%%DocumentNeededResources: ") # register not downloaded fonts here file.write("%%%%Pages: %d\n" % len(document.pages)) file.write("%%PageOrder: Ascend\n") file.write("%%EndComments\n") # document defaults section #file.write("%%BeginDefaults\n") #file.write("%%EndDefaults\n") # document prolog section file.write("%%BeginProlog\n") registry.output(file, self) file.write("%%EndProlog\n") # document setup section #file.write("%%BeginSetup\n") #file.write("%%EndSetup\n") file.write(pagesfile.getvalue()) pagesfile.close() file.write("%%Trailer\n") file.write("%%EOF\n")