def __init__(self, matrix=((1, 0), (0, 1)), vector=(0, 0), epsilon=_marker): trafo_pt.__init__(self, matrix, (unit.topt(vector[0]), unit.topt(vector[1])), epsilon=epsilon)
def _bracepath(self, x0_pt, y0_pt, x1_pt, y1_pt): # <<< height_pt = unit.topt(self.totalheight) totallength_pt = math.hypot(x1_pt - x0_pt, y1_pt - y0_pt) leftlength_pt = self.middlerelpos * totallength_pt rightlength_pt = totallength_pt - leftlength_pt ithick_pt = unit.topt(self.innerstrokesthickness) othick_pt = unit.topt(self.outerstrokesthickness) bthick_pt = unit.topt(self.barthickness) # create the left halfbrace with positive slanting # because we will mirror this part cos_iangle = math.cos( math.radians(0.5 * self.innerstrokesangle - self.slantstrokesangle)) sin_iangle = math.sin( math.radians(0.5 * self.innerstrokesangle - self.slantstrokesangle)) cos_oangle = math.cos( math.radians(self.outerstrokesangle - self.slantstrokesangle)) sin_oangle = math.sin( math.radians(self.outerstrokesangle - self.slantstrokesangle)) cos_slangle = math.cos(math.radians(-self.slantstrokesangle)) sin_slangle = math.sin(math.radians(-self.slantstrokesangle)) ilength_pt = self.innerstrokesrelheight * height_pt / cos_iangle olength_pt = self.outerstrokesrelheight * height_pt / cos_oangle bracepath = self._halfbracepath_pt( leftlength_pt, height_pt, ilength_pt, olength_pt, ithick_pt, othick_pt, bthick_pt, cos_iangle, sin_iangle, cos_oangle, sin_oangle, cos_slangle, sin_slangle).reversed().transformed(trafo.mirror(90)) # create the right halfbrace with negative slanting cos_iangle = math.cos( math.radians(0.5 * self.innerstrokesangle + self.slantstrokesangle)) sin_iangle = math.sin( math.radians(0.5 * self.innerstrokesangle + self.slantstrokesangle)) cos_oangle = math.cos( math.radians(self.outerstrokesangle + self.slantstrokesangle)) sin_oangle = math.sin( math.radians(self.outerstrokesangle + self.slantstrokesangle)) cos_slangle = math.cos(math.radians(-self.slantstrokesangle)) sin_slangle = math.sin(math.radians(-self.slantstrokesangle)) ilength_pt = self.innerstrokesrelheight * height_pt / cos_iangle olength_pt = self.outerstrokesrelheight * height_pt / cos_oangle bracepath = bracepath << self._halfbracepath_pt( rightlength_pt, height_pt, ilength_pt, olength_pt, ithick_pt, othick_pt, bthick_pt, cos_iangle, sin_iangle, cos_oangle, sin_oangle, cos_slangle, sin_slangle) return bracepath.transformed( # two trafos for matching the given endpoints trafo.translate_pt(x0_pt, y0_pt) * trafo.rotate_pt( math.degrees(math.atan2(y1_pt - y0_pt, x1_pt - x0_pt))) * # one trafo to move the brace's left outer stroke to zero trafo.translate_pt(leftlength_pt, 0))
def __init__(self, angle, x=None, y=None, epsilon=_marker): vector = 0, 0 if x is not None or y is not None: if x is None or y is None: raise TrafoException("either specify both x and y or none of them") vector=_rvector(angle, unit.topt(x), unit.topt(y)) trafo_pt.__init__(self, matrix=_rmatrix(angle), vector=vector, epsilon=epsilon)
def __init__(self, dist, angle, strokestyles=[]): pattern.__init__(self, painttype=1, tilingtype=1, xstep=dist, ystep=dist, bbox=None, trafo=trafo.rotate(angle)) self.strokestyles = attr.mergeattrs([style.linewidth.THIN] + strokestyles) attr.checkattrs(self.strokestyles, [style.strokestyle]) self.dist = dist self.angle = angle self.stroke(path.line_pt(0, 0, 0, unit.topt(dist)), self.strokestyles) self.stroke(path.line_pt(0, 0, unit.topt(dist), 0), self.strokestyles)
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): warnings.warn("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): warnings.warn("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) cc = canvasmodule.canvas() cc.insert(self.canvas, [pagetrafo]) else: cc = self.canvas getattr(style.linewidth.normal, processMethod)(contentfile, writer, context, registry, bbox) 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 __init__(self, xpos, ypos, image, width=None, height=None, **kwargs): xpos_pt = unit.topt(xpos) ypos_pt = unit.topt(ypos) if width is not None: width_pt = unit.topt(width) else: width_pt = None if height is not None: height_pt = unit.topt(height) else: height_pt = None bitmap_pt.__init__(self, xpos_pt, ypos_pt, image, width_pt=width_pt, height_pt=height_pt, **kwargs)
def write(self, file, writer, registry): file.write("<<\n" "/Type /Page\n" "/Parent %i 0 R\n" % registry.getrefno(self.PDFpages)) paperformat = self.page.paperformat if paperformat: file.write("/MediaBox [0 0 %f %f]\n" % (unit.topt(paperformat.width), unit.topt(paperformat.height))) else: file.write("/MediaBox [%f %f %f %f]\n" % self.PDFcontent.bbox.highrestuple_pt()) if self.PDFcontent.bbox and writer.writebbox: file.write("/CropBox [%f %f %f %f]\n" % self.PDFcontent.bbox.highrestuple_pt()) if self.page.rotated: file.write("/Rotate 90\n") file.write("/Contents %i 0 R\n" % registry.getrefno(self.PDFcontent)) self.pageregistry.writeresources(file) file.write(">>\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 _bracepath(self, x0_pt, y0_pt, x1_pt, y1_pt): # <<< height_pt = unit.topt(self.totalheight) totallength_pt = math.hypot(x1_pt - x0_pt, y1_pt - y0_pt) leftlength_pt = self.middlerelpos * totallength_pt rightlength_pt = totallength_pt - leftlength_pt ithick_pt = unit.topt(self.innerstrokesthickness) othick_pt = unit.topt(self.outerstrokesthickness) bthick_pt = unit.topt(self.barthickness) # create the left halfbrace with positive slanting # because we will mirror this part cos_iangle = math.cos(math.radians(0.5*self.innerstrokesangle - self.slantstrokesangle)) sin_iangle = math.sin(math.radians(0.5*self.innerstrokesangle - self.slantstrokesangle)) cos_oangle = math.cos(math.radians(self.outerstrokesangle - self.slantstrokesangle)) sin_oangle = math.sin(math.radians(self.outerstrokesangle - self.slantstrokesangle)) cos_slangle = math.cos(math.radians(-self.slantstrokesangle)) sin_slangle = math.sin(math.radians(-self.slantstrokesangle)) ilength_pt = self.innerstrokesrelheight * height_pt / cos_iangle olength_pt = self.outerstrokesrelheight * height_pt / cos_oangle bracepath = self._halfbracepath_pt(leftlength_pt, height_pt, ilength_pt, olength_pt, ithick_pt, othick_pt, bthick_pt, cos_iangle, sin_iangle, cos_oangle, sin_oangle, cos_slangle, sin_slangle).reversed().transformed(trafo.mirror(90)) # create the right halfbrace with negative slanting cos_iangle = math.cos(math.radians(0.5*self.innerstrokesangle + self.slantstrokesangle)) sin_iangle = math.sin(math.radians(0.5*self.innerstrokesangle + self.slantstrokesangle)) cos_oangle = math.cos(math.radians(self.outerstrokesangle + self.slantstrokesangle)) sin_oangle = math.sin(math.radians(self.outerstrokesangle + self.slantstrokesangle)) cos_slangle = math.cos(math.radians(-self.slantstrokesangle)) sin_slangle = math.sin(math.radians(-self.slantstrokesangle)) ilength_pt = self.innerstrokesrelheight * height_pt / cos_iangle olength_pt = self.outerstrokesrelheight * height_pt / cos_oangle bracepath = bracepath << self._halfbracepath_pt(rightlength_pt, height_pt, ilength_pt, olength_pt, ithick_pt, othick_pt, bthick_pt, cos_iangle, sin_iangle, cos_oangle, sin_oangle, cos_slangle, sin_slangle) return bracepath.transformed( # two trafos for matching the given endpoints trafo.translate_pt(x0_pt, y0_pt) * trafo.rotate_pt(math.degrees(math.atan2(y1_pt-y0_pt, x1_pt-x0_pt))) * # one trafo to move the brace's left outer stroke to zero trafo.translate_pt(leftlength_pt, 0))
def __init__(self, box1, box2, relangle=45, absbulge=None, relbulge=None, boxdists=[0,0]): if absbulge is not None: absbulge = unit.topt(absbulge) arc_pt.__init__(self, box1, box2, relangle=relangle, absbulge=absbulge, relbulge=relbulge, boxdists=map(unit.topt, boxdists))
def enlarged(self, all=0, bottom=None, left=None, top=None, right=None): """return bbox enlarged all is used, if bottom, left, top and/or right are not given. """ if self.llx_pt is None: return empty() bottom_pt = left_pt = top_pt = right_pt = unit.topt(all) if bottom is not None: bottom_pt = unit.topt(bottom) if left is not None: left_pt = unit.topt(left) if top is not None: top_pt = unit.topt(top) if right is not None: right_pt = unit.topt(right) return bbox_pt(self.llx_pt-left_pt, self.lly_pt-bottom_pt, self.urx_pt+right_pt, self.ury_pt+top_pt)
def __init__(self, box1, box2, relangle1=45, relangle2=45, absangle1=None, absangle2=None, absbulge=0, relbulge=0.39, boxdists=[0,0]): curve_pt.__init__(self, box1, box2, relangle1=relangle1, relangle2=relangle2, absangle1=absangle1, absangle2=absangle2, absbulge=unit.topt(absbulge), relbulge=relbulge, boxdists=map(unit.topt, boxdists))
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 write(self, file, writer, registry): file.write("<<\n" "/Type /Page\n" "/Parent %i 0 R\n" % registry.getrefno(self.PDFpages)) paperformat = self.page.paperformat if paperformat: file.write( "/MediaBox [0 0 %f %f]\n" % (unit.topt(paperformat.width), unit.topt(paperformat.height))) else: file.write("/MediaBox [%f %f %f %f]\n" % self.PDFcontent.bbox.highrestuple_pt()) if self.PDFcontent.bbox and writer.writebbox: file.write("/CropBox [%f %f %f %f]\n" % self.PDFcontent.bbox.highrestuple_pt()) if self.page.rotated: file.write("/Rotate 90\n") file.write("/Contents %i 0 R\n" % registry.getrefno(self.PDFcontent)) self.pageregistry.writeresources(file) file.write(">>\n")
def enlarge(self, all=0, bottom=None, left=None, top=None, right=None): """enlarge bbox in place all is used, if bottom, left, top and/or right are not given. """ if self.llx_pt is None: return bottom_pt = left_pt = top_pt = right_pt = unit.topt(all) if bottom is not None: bottom_pt = unit.topt(bottom) if left is not None: left_pt = unit.topt(left) if top is not None: top_pt = unit.topt(top) if right is not None: right_pt = unit.topt(right) self.llx_pt -= left_pt self.lly_pt -= bottom_pt self.urx_pt += right_pt self.ury_pt += top_pt
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 path(self, centerradius=None, bezierradius=None, beziersoftness=None): pathitems = [] if centerradius is not None and self.center is not None: r = unit.topt(centerradius) pathitems.append(path.arc_pt(self.center[0], self.center[1], r, 0, 360)) pathitems.append(path.closepath()) if bezierradius is not None or beziersoftness is not None: raise ValueError("smooth functionality removed; apply smooth deformer on path") pathitems.append(path.moveto_pt(self.corners[0][0], self.corners[0][1])) for x, y in self.corners[1:]: pathitems.append(path.lineto_pt(x, y)) pathitems.append(path.closepath()) return path.path(*pathitems)
def _decocanvas(self, angle, dp, texrunner): dp.ensurenormpath() dist_pt = unit.topt(self.dist) c = canvas.canvas([canvas.clip(dp.path)]) llx_pt, lly_pt, urx_pt, ury_pt = dp.path.bbox().highrestuple_pt() center_pt = 0.5*(llx_pt+urx_pt), 0.5*(lly_pt+ury_pt) radius_pt = 0.5*math.hypot(urx_pt-llx_pt, ury_pt-lly_pt) + dist_pt n = int(2*radius_pt / dist_pt) + 1 for i in range(n): x_pt = center_pt[0] - radius_pt + i*dist_pt c.stroke(path.line_pt(x_pt, center_pt[1]-radius_pt, x_pt, center_pt[1]+radius_pt), [trafo.rotate_pt(angle, center_pt[0], center_pt[1])] + self.strokestyles) return c
def __init__(self, box1, box2, absangle1=None, absangle2=None, relangle1=None, relangle2=None, relangleM=None, length1=None, length2=None, bezierradius=None, beziersoftness=1, arcradius=None, boxdists=[0,0]): if length1 is not None: length1 = unit.topt(length1) if length2 is not None: length2 = unit.topt(length2) if bezierradius is not None: bezierradius = unit.topt(bezierradius) if arcradius is not None: arcradius = unit.topt(arcradius) twolines_pt.__init__(self, box1, box2, absangle1=absangle1, absangle2=absangle2, relangle1=relangle1, relangle2=relangle2, relangleM=relangleM, length1=length1, length2=length2, bezierradius=bezierradius, beziersoftness=1, arcradius=arcradius, boxdists=map(unit.topt, boxdists))
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 path(self, centerradius=None, bezierradius=None, beziersoftness=None): pathitems = [] if centerradius is not None and self.center is not None: r = unit.topt(centerradius) pathitems.append( path.arc_pt(self.center[0], self.center[1], r, 0, 360)) pathitems.append(path.closepath()) if bezierradius is not None or beziersoftness is not None: raise ValueError( "smooth functionality removed; apply smooth deformer on path") pathitems.append(path.moveto_pt(self.corners[0][0], self.corners[0][1])) for x, y in self.corners[1:]: pathitems.append(path.lineto_pt(x, y)) pathitems.append(path.closepath()) return path.path(*pathitems)
def decorate(self, dp, texrunner): dp.ensurenormpath() x0_pt, y0_pt = dp.path.atbegin_pt() x1_pt, y1_pt = dp.path.atend_pt() if self.reverse: x0_pt, y0_pt, x1_pt, y1_pt = x1_pt, y1_pt, x0_pt, y0_pt if self.stretch is not None: xm, ym = 0.5*(x0_pt+x1_pt), 0.5*(y0_pt+y1_pt) x0_pt, y0_pt = xm + self.stretch*(x0_pt-xm), ym + self.stretch*(y0_pt-ym) x1_pt, y1_pt = xm + self.stretch*(x1_pt-xm), ym + self.stretch*(y1_pt-ym) if self.dist is not None: d = unit.topt(self.dist) dx, dy = dp.path.rotation_pt(dp.path.begin()).apply_pt(0, 1) x0_pt += d*dx; y0_pt += d*dy dx, dy = dp.path.rotation_pt(dp.path.end()).apply_pt(0, 1) x1_pt += d*dx; y1_pt += d*dy dp.ornaments.fill(self._bracepath(x0_pt, y0_pt, x1_pt, y1_pt), self.fillattrs)
def _decocanvas(self, angle, dp, texrunner): dp.ensurenormpath() dist_pt = unit.topt(self.dist) c = canvas.canvas([canvas.clip(dp.path)]) llx_pt, lly_pt, urx_pt, ury_pt = dp.path.bbox().highrestuple_pt() center_pt = 0.5 * (llx_pt + urx_pt), 0.5 * (lly_pt + ury_pt) radius_pt = 0.5 * math.hypot(urx_pt - llx_pt, ury_pt - lly_pt) + dist_pt n = int(2 * radius_pt / dist_pt) + 1 for i in range(n): x_pt = center_pt[0] - radius_pt + i * dist_pt c.stroke( path.line_pt(x_pt, center_pt[1] - radius_pt, x_pt, center_pt[1] + radius_pt), [trafo.rotate_pt(angle, center_pt[0], center_pt[1])] + self.strokestyles) return c
def decorate(self, dp, texrunner): dp.ensurenormpath() x0_pt, y0_pt = dp.path.atbegin_pt() x1_pt, y1_pt = dp.path.atend_pt() if self.reverse: x0_pt, y0_pt, x1_pt, y1_pt = x1_pt, y1_pt, x0_pt, y0_pt if self.stretch is not None: xm, ym = 0.5 * (x0_pt + x1_pt), 0.5 * (y0_pt + y1_pt) x0_pt, y0_pt = xm + self.stretch * ( x0_pt - xm), ym + self.stretch * (y0_pt - ym) x1_pt, y1_pt = xm + self.stretch * ( x1_pt - xm), ym + self.stretch * (y1_pt - ym) if self.dist is not None: d = unit.topt(self.dist) dx, dy = dp.path.rotation_pt(dp.path.begin()).apply_pt(0, 1) x0_pt += d * dx y0_pt += d * dy dx, dy = dp.path.rotation_pt(dp.path.end()).apply_pt(0, 1) x1_pt += d * dx y1_pt += d * dy dp.ornaments.fill(self._bracepath(x0_pt, y0_pt, x1_pt, y1_pt), self.fillattrs)
def __init__(self, x, y, width, height, relcenter=(0, 0), abscenter=(0, 0), **args): rect_pt.__init__(self, unit.topt(x), unit.topt(y), unit.topt(width), unit.topt(height), relcenter=relcenter, abscenter=(unit.topt(abscenter[0]), unit.topt(abscenter[1])), **args)
def __init__(self, x, y): lineto_pt.__init__(self, unit.topt(x), unit.topt(y))
def __init__(self, x, y, radius, **kwargs): circle_pt.__init__(self, unit.topt(x), unit.topt(y), unit.topt(radius), **kwargs)
def __init__(self, x, y, a, b, angle, **kwargs): ellipse_pt.__init__(self, unit.topt(x), unit.topt(y), unit.topt(a), unit.topt(b), angle, **kwargs)
def __init__(self, x1, y1, x2, y2): line_pt.__init__(self, unit.topt(x1), unit.topt(y1), unit.topt(x2), unit.topt(y2))
def __init__(self, x, y, width, height): rect_pt.__init__(self, unit.topt(x), unit.topt(y), unit.topt(width), unit.topt(height))
def apply(self, x, y): # for the transformation we have to convert to points tx, ty = self.apply_pt(unit.topt(x), unit.topt(y)) return tx * unit.t_pt, ty * unit.t_pt
def __init__(self, x1, y1, x2, y2, r): arct_pt.__init__(self, unit.topt(x1), unit.topt(y1), unit.topt(x2), unit.topt(y2), unit.topt(r))
def __init__(self, dx1, dy1, dx2, dy2, dx3, dy3): rcurveto_pt.__init__(self, unit.topt(dx1), unit.topt(dy1), unit.topt(dx2), unit.topt(dy2), unit.topt(dx3), unit.topt(dy3))
def __init__(self, x1, y1, x2, y2, x3, y3): curveto_pt.__init__(self, unit.topt(x1), unit.topt(y1), unit.topt(x2), unit.topt(y2), unit.topt(x3), unit.topt(y3))
def __init__(self, matrix=((1,0), (0,1)), vector=(0, 0), epsilon=_marker): trafo_pt.__init__(self, matrix, (unit.topt(vector[0]), unit.topt(vector[1])), epsilon=epsilon)
def __init__(self, dx, dy): rlineto_pt.__init__(self, unit.topt(dx), unit.topt(dy))
def __init__(self, x, y, filename, width=None, height=None, scale=None, align="bl", clip=1, translatebbox=1, bbox=None, kpsearch=0): """inserts epsfile Object for an EPS file named filename at position (x,y). Width, height, scale and aligment can be adjusted by the corresponding parameters. If clip is set, the result gets clipped to the bbox of the EPS file. If translatebbox is not set, the EPS graphics is not translated to the corresponding origin. If bbox is not None, it overrides the bounding box in the epsfile itself. If kpsearch is set then filename is searched using the kpathsea library. """ self.x_pt = unit.topt(x) self.y_pt = unit.topt(y) self.filename = filename self.kpsearch = kpsearch if bbox: self.mybbox = bbox else: epsfile = self.open() self.mybbox = _readbbox(epsfile) epsfile.close() # determine scaling in x and y direction self.scalex = self.scaley = scale if width is not None or height is not None: if scale is not None: raise ValueError("cannot set both width and/or height and scale simultaneously") if height is not None: self.scaley = unit.topt(height)/(self.mybbox.ury_pt-self.mybbox.lly_pt) if width is not None: self.scalex = unit.topt(width)/(self.mybbox.urx_pt-self.mybbox.llx_pt) if self.scalex is None: self.scalex = self.scaley if self.scaley is None: self.scaley = self.scalex # set the actual width and height of the eps file (after a # possible scaling) self.width_pt = self.mybbox.urx_pt-self.mybbox.llx_pt if self.scalex: self.width_pt *= self.scalex self.height_pt = self.mybbox.ury_pt-self.mybbox.lly_pt if self.scaley: self.height_pt *= self.scaley # take alignment into account self.align = align if self.align[0]=="b": pass elif self.align[0]=="c": self.y_pt -= self.height_pt/2.0 elif self.align[0]=="t": self.y_pt -= self.height_pt else: raise ValueError("vertical alignment can only be b (bottom), c (center), or t (top)") if self.align[1]=="l": pass elif self.align[1]=="c": self.x_pt -= self.width_pt/2.0 elif self.align[1]=="r": self.x_pt -= self.width_pt else: raise ValueError("horizontal alignment can only be l (left), c (center), or r (right)") self.clip = clip self.translatebbox = translatebbox self.trafo = trafo.translate_pt(self.x_pt, self.y_pt) if self.scalex is not None: self.trafo = self.trafo * trafo.scale_pt(self.scalex, self.scaley) if translatebbox: self.trafo = self.trafo * trafo.translate_pt(-self.mybbox.llx_pt, -self.mybbox.lly_pt)
def __init__(self, x, y, r, angle1, angle2): arc_pt.__init__(self, unit.topt(x), unit.topt(y), unit.topt(r), angle1, angle2)
def __init__(self, coords, value): node_pt.__init__(self, [unit.topt(coord) for coord in coords], value)
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 processPDF(self, file, writer, context, registry, bbox): file.write("%f w\n" % unit.topt(self.width)) context.linewidth_pt = unit.topt(self.width)