def bbox(self): if self.font.metric is None: raise ValueError("metric missing") if not self.decode: raise ValueError("decoding required for font metric access (bbox)") return bbox.bbox_pt(self.x_pt, self.y_pt+self.font.metric.depth_pt(self.glyphnames, self.size_pt), self.x_pt+self.font.metric.width_pt(self.glyphnames, self.size_pt), self.y_pt+self.font.metric.height_pt(self.glyphnames, self.size_pt))
def bbox(self): if self.font.metric is None: warnings.warn("We are about to extract the bounding box from the path of the text. This is slow and differs from the font metric information. You should provide an afm file whenever possible.") return self.textpath().bbox() if not self.decode: raise ValueError("decoding required for font metric access (bbox)") return bbox.bbox_pt(self.x_pt, self.y_pt+self.font.metric.depth_pt(self.glyphnames, self.size_pt), self.x_pt+self.font.metric.width_pt(self.glyphnames, self.size_pt), self.y_pt+self.font.metric.height_pt(self.glyphnames, self.size_pt))
def bbox(self): if self.font.metric is None: raise ValueError("metric missing") if not self.decode: raise ValueError("decoding required for font metric access (bbox)") return bbox.bbox_pt( self.x_pt, self.y_pt + self.font.metric.depth_pt(self.glyphnames, self.size_pt), self.x_pt + self.font.metric.width_pt(self.glyphnames, self.size_pt), self.y_pt + self.font.metric.height_pt(self.glyphnames, self.size_pt))
def __init__(self, font, x_pt, y_pt, charcodes, size_pt, fontmap=None): self.font = font self.x_pt = x_pt self.y_pt = y_pt self.charcodes = charcodes self.size_pt = size_pt self.fontmap = fontmap self.width_pt = sum([self.font.getwidth_pt(charcode) for charcode in charcodes]) self.height_pt = max([self.font.getheight_pt(charcode) for charcode in charcodes]) self.depth_pt = max([self.font.getdepth_pt(charcode) for charcode in charcodes]) self._bbox = bbox.bbox_pt(self.x_pt, self.y_pt-self.depth_pt, self.x_pt+self.width_pt, self.y_pt+self.height_pt)
def bbox(self): if self.font.metric is None: logger.warning("We are about to extract the bounding box from the path of the text. This is slow and differs from the font metric information. You should provide an afm file whenever possible.") return self.textpath().bbox() if not self.decode: raise ValueError("decoding required for font metric access (bbox)") if self.kerning: kerning_correction = sum(value or 0 for i, value in enumerate(self.font.metric.resolvekernings(self.glyphnames, self.size_pt)) if i%2) else: kerning_correction = 0 return bbox.bbox_pt(self.x_pt, self.y_pt+self.font.metric.depth_pt(self.glyphnames, self.size_pt), self.x_pt+self.font.metric.width_pt(self.glyphnames, self.size_pt) + (len(self.glyphnames)-1)*self.spaced_pt + kerning_correction, self.y_pt+self.font.metric.height_pt(self.glyphnames, self.size_pt))
def bbox(self): if self.font.metric is None: warnings.warn( "We are about to extract the bounding box from the path of the text. This is slow and differs from the font metric information. You should provide an afm file whenever possible." ) return self.textpath().bbox() if not self.decode: raise ValueError("decoding required for font metric access (bbox)") return bbox.bbox_pt( self.x_pt, self.y_pt + self.font.metric.depth_pt(self.glyphnames, self.size_pt), self.x_pt + self.font.metric.width_pt(self.glyphnames, self.size_pt), self.y_pt + self.font.metric.height_pt(self.glyphnames, self.size_pt))
def special(self, s, fontmap): x = self.pos[_POS_H] * self.pyxconv y = -self.pos[_POS_V] * self.pyxconv if self.debug: self.debugfile.write("%d: xxx '%s'\n" % (self.filepos, s)) if not s.startswith("PyX:"): logger.warning("ignoring special '%s'" % s) return # it is in general not safe to continue using the currently active font because # the specials may involve some gsave/grestore operations self.flushtext(fontmap) command, args = s[4:].split()[0], s[4:].split()[1:] if command == "color_begin": if args[0] == "cmyk": c = color.cmyk(float(args[1]), float(args[2]), float(args[3]), float(args[4])) elif args[0] == "gray": c = color.gray(float(args[1])) elif args[0] == "hsb": c = color.hsb(float(args[1]), float(args[2]), float(args[3])) elif args[0] == "rgb": c = color.rgb(float(args[1]), float(args[2]), float(args[3])) elif args[0] == "RGB": c = color.rgb(int(args[1])/255.0, int(args[2])/255.0, int(args[3])/255.0) elif args[0] == "texnamed": try: c = getattr(color.cmyk, args[1]) except AttributeError: raise RuntimeError("unknown TeX color '%s', aborting" % args[1]) elif args[0] == "pyxcolor": # pyx.color.cmyk.PineGreen or # pyx.color.cmyk(0,0,0,0.0) pat = re.compile(r"(pyx\.)?(color\.)?(?P<model>(cmyk)|(rgb)|(grey)|(gray)|(hsb))[\.]?(?P<arg>.*)") sd = pat.match(" ".join(args[1:])) if sd: sd = sd.groupdict() if sd["arg"][0] == "(": numpat = re.compile(r"[+-]?((\d+\.\d*)|(\d*\.\d+)|(\d+))([eE][+-]\d+)?") arg = tuple([float(x[0]) for x in numpat.findall(sd["arg"])]) try: c = getattr(color, sd["model"])(*arg) except TypeError or AttributeError: raise RuntimeError("cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: try: c = getattr(getattr(color, sd["model"]), sd["arg"]) except AttributeError: raise RuntimeError("cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: raise RuntimeError("cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: raise RuntimeError("color model '%s' cannot be handled by PyX, aborting" % args[0]) self.beginsubpage([c]) elif command == "color_end": self.endsubpage() elif command == "rotate_begin": self.beginsubpage([trafo.rotate_pt(float(args[0]), x, y)]) elif command == "rotate_end": self.endsubpage() elif command == "scale_begin": self.beginsubpage([trafo.scale_pt(float(args[0]), float(args[1]), x, y)]) elif command == "scale_end": self.endsubpage() elif command == "epsinclude": # parse arguments argdict = {} for arg in args: name, value = arg.split("=") argdict[name] = value # construct kwargs for epsfile constructor epskwargs = {} epskwargs["filename"] = argdict["file"] epskwargs["bbox"] = bbox.bbox_pt(float(argdict["llx"]), float(argdict["lly"]), float(argdict["urx"]), float(argdict["ury"])) if "width" in argdict: epskwargs["width"] = float(argdict["width"]) * unit.t_pt if "height" in argdict: epskwargs["height"] = float(argdict["height"]) * unit.t_pt if "clip" in argdict: epskwargs["clip"] = int(argdict["clip"]) self.actpage.insert(epsfile.epsfile(x * unit.t_pt, y * unit.t_pt, **epskwargs)) elif command == "marker": if len(args) != 1: raise RuntimeError("marker contains spaces") for c in args[0]: if c not in string.ascii_letters + string.digits + "@": raise RuntimeError("marker contains invalid characters") if args[0] in self.actpage.markers: raise RuntimeError("marker name occurred several times") self.actpage.markers[args[0]] = x * unit.t_pt, y * unit.t_pt else: raise RuntimeError("unknown PyX special '%s', aborting" % command)
def special(self, s, fontmap): x = self.pos[_POS_H] * self.pyxconv y = -self.pos[_POS_V] * self.pyxconv if self.debug: self.debugfile.write("%d: xxx '%s'\n" % (self.filepos, s)) if not s.startswith("PyX:"): logger.warning("ignoring special '%s'" % s) return # it is in general not safe to continue using the currently active font because # the specials may involve some gsave/grestore operations self.flushtext(fontmap) command, args = s[4:].split()[0], s[4:].split()[1:] if command == "color_begin": if args[0] == "cmyk": c = color.cmyk(float(args[1]), float(args[2]), float(args[3]), float(args[4])) elif args[0] == "gray": c = color.gray(float(args[1])) elif args[0] == "hsb": c = color.hsb(float(args[1]), float(args[2]), float(args[3])) elif args[0] == "rgb": c = color.rgb(float(args[1]), float(args[2]), float(args[3])) elif args[0] == "RGB": c = color.rgb( int(args[1]) / 255.0, int(args[2]) / 255.0, int(args[3]) / 255.0) elif args[0] == "texnamed": try: c = getattr(color.cmyk, args[1]) except AttributeError: raise RuntimeError("unknown TeX color '%s', aborting" % args[1]) elif args[0] == "pyxcolor": # pyx.color.cmyk.PineGreen or # pyx.color.cmyk(0,0,0,0.0) pat = re.compile( r"(pyx\.)?(color\.)?(?P<model>(cmyk)|(rgb)|(grey)|(gray)|(hsb))[\.]?(?P<arg>.*)" ) sd = pat.match(" ".join(args[1:])) if sd: sd = sd.groupdict() if sd["arg"][0] == "(": numpat = re.compile( r"[+-]?((\d+\.\d*)|(\d*\.\d+)|(\d+))([eE][+-]\d+)?" ) arg = tuple( [float(x[0]) for x in numpat.findall(sd["arg"])]) try: c = getattr(color, sd["model"])(*arg) except TypeError or AttributeError: raise RuntimeError( "cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: try: c = getattr(getattr(color, sd["model"]), sd["arg"]) except AttributeError: raise RuntimeError( "cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: raise RuntimeError( "cannot access PyX color '%s' in TeX, aborting" % " ".join(args[1:])) else: raise RuntimeError( "color model '%s' cannot be handled by PyX, aborting" % args[0]) self.beginsubpage([c]) elif command == "color_end": self.endsubpage() elif command == "rotate_begin": self.beginsubpage([trafo.rotate_pt(float(args[0]), x, y)]) elif command == "rotate_end": self.endsubpage() elif command == "scale_begin": self.beginsubpage( [trafo.scale_pt(float(args[0]), float(args[1]), x, y)]) elif command == "scale_end": self.endsubpage() elif command == "epsinclude": # parse arguments argdict = {} for arg in args: name, value = arg.split("=") argdict[name] = value # construct kwargs for epsfile constructor epskwargs = {} epskwargs["filename"] = argdict["file"] epskwargs["bbox"] = bbox.bbox_pt(float(argdict["llx"]), float(argdict["lly"]), float(argdict["urx"]), float(argdict["ury"])) if "width" in argdict: epskwargs["width"] = float(argdict["width"]) * unit.t_pt if "height" in argdict: epskwargs["height"] = float(argdict["height"]) * unit.t_pt if "clip" in argdict: epskwargs["clip"] = int(argdict["clip"]) self.actpage.insert( epsfile.epsfile(x * unit.t_pt, y * unit.t_pt, **epskwargs)) elif command == "marker": if len(args) != 1: raise RuntimeError("marker contains spaces") for c in args[0]: if c not in string.ascii_letters + string.digits + "@": raise RuntimeError("marker contains invalid characters") if args[0] in self.actpage.markers: raise RuntimeError("marker name occurred several times") self.actpage.markers[args[0]] = x * unit.t_pt, y * unit.t_pt else: raise RuntimeError("unknown PyX special '%s', aborting" % command)