def write(self, filename='schematic'): schpath = self.place(0, 0) for cdev, devpath in enumerate(schpath): for clay, laypath in enumerate(devpath): laypath, laybbox, laytext = laypath laybbox_path = laybbox.to_path() laycanv = pyxcanvas.canvas([pyxcanvas.clip(laybbox_path)]) for cfea, feapath in enumerate(laypath): feapath, color, stroke_color = feapath laycanv.fill(feapath, [color]) if stroke_color is None: laycanv.stroke(feapath, [stroke_color]) if laytext != '': xc = (laybbox.x1 + laybbox.x2) / 2 yc = (laybbox.y1 + laybbox.y2) / 2 t = text.Text(laytext, scale=2) laycanv.text(xc, yc, t) self.canvas.insert(laycanv) self.canvas.writeEPSfile(filename)
def labels(self, ticks): labeledticks = [] for tick in ticks: if tick.label is None and tick.labellevel is not None: labeledticks.append(tick) tick.temp_rationalnum = tick.num tick.temp_rationaldenom = tick.denom tick.temp_rationalminus = 1 if tick.temp_rationalnum < 0: tick.temp_rationalminus = -tick.temp_rationalminus tick.temp_rationalnum = -tick.temp_rationalnum if tick.temp_rationaldenom < 0: tick.temp_rationalminus = -tick.temp_rationalminus tick.temp_rationaldenom = -tick.temp_rationaldenom gcd = self.gcd(tick.temp_rationalnum, tick.temp_rationaldenom) (tick.temp_rationalnum, dummy1), (tick.temp_rationaldenom, dummy2) = divmod(tick.temp_rationalnum, gcd), divmod(tick.temp_rationaldenom, gcd) if self.equaldenom: equaldenom = self.lcm(*[tick.temp_rationaldenom for tick in ticks if tick.label is None]) if equaldenom is not None: for tick in labeledticks: factor, dummy = divmod(equaldenom, tick.temp_rationaldenom) tick.temp_rationalnum, tick.temp_rationaldenom = factor * tick.temp_rationalnum, factor * tick.temp_rationaldenom for tick in labeledticks: rationalminus = rationalnumminus = rationaldenomminus = "" if tick.temp_rationalminus == -1: plusminus = self.minus else: plusminus = self.plus if self.minuspos == 0: rationalminus = plusminus elif self.minuspos == 1: rationalnumminus = plusminus elif self.minuspos == -1: rationaldenomminus = plusminus else: raise RuntimeError("invalid minuspos") if self.skipnum0 and tick.temp_rationalnum == 0: tick.label = "0" elif (self.skip1 and self.skipdenom1 and tick.temp_rationalnum == 1 and tick.temp_rationaldenom == 1 and (len(self.prefix) or len(self.infix) or len(self.suffix)) and not len(rationalnumminus) and not len(self.numprefix) and not len(self.numinfix) and not len(self.numsuffix) and not len(rationaldenomminus) and not len(self.denomprefix) and not len(self.denominfix) and not len(self.denomsuffix)): tick.label = "%s%s%s%s" % (self.prefix, rationalminus, self.infix, self.suffix) else: if self.skipnum1 and tick.temp_rationalnum == 1 and (len(self.numprefix) or len(self.numinfix) or len(self.numsuffix)): tick.temp_rationalnum = "%s%s%s%s" % (self.numprefix, rationalnumminus, self.numinfix, self.numsuffix) else: tick.temp_rationalnum = "%s%s%s%i%s" % (self.numprefix, rationalnumminus, self.numinfix, tick.temp_rationalnum, self.numsuffix) if self.skipdenom1 and tick.temp_rationaldenom == 1 and not len(rationaldenomminus) and not len(self.denomprefix) and not len(self.denominfix) and not len(self.denomsuffix): tick.label = "%s%s%s%s%s" % (self.prefix, rationalminus, self.infix, tick.temp_rationalnum, self.suffix) else: tick.temp_rationaldenom = "%s%s%s%i%s" % (self.denomprefix, rationaldenomminus, self.denominfix, tick.temp_rationaldenom, self.denomsuffix) tick.label = text.MultiEngineText("%s%s%s%s%s" % (self.prefix, rationalminus, self.infix, self.over % (tick.temp_rationalnum, tick.temp_rationaldenom), self.suffix), ["%s%s%s" % (self.prefix, rationalminus, self.infix)] + [text.StackedText([text.Text(tick.temp_rationalnum, shift=0.3), text.Text(tick.temp_rationaldenom, shift=-0.9)], frac=True, align=0.5)] + [self.suffix]) tick.labelattrs = tick.labelattrs + self.labelattrs
def labels(self, ticks): labeledticks = [] for tick in ticks: if tick.label is None and tick.labellevel is not None: labeledticks.append(tick) tick.labelattrs = tick.labelattrs + self.labelattrs if tick.num: # express tick = tick.temp_sign * tick.temp_mantissa * self.base ** tick.temp_exponent with 1 <= temp_mantissa < self.base # and decide whether a tick is to be written in exponential notation tick.temp_sign = 1 if tick >= 0 else -1 tick.temp_mantissa = abs(Fraction(tick.num, tick.denom)) tick.temp_exponent = 0 while tick.temp_mantissa >= self.base: tick.temp_exponent += 1 tick.temp_mantissa /= self.base while tick.temp_mantissa < 1: tick.temp_exponent -= 1 tick.temp_mantissa *= self.base tick.temp_wantexponent = not (-self.minnegexponent < tick.temp_exponent < self.minexponent) else: tick.temp_mantissa = tick.temp_exponent = 0 tick.temp_sign = 1 tick.temp_wantexponent = not (-self.minnegexponent < 0 < self.minexponent) # make decision on exponential notation uniform if requested if self.uniformexponent and any(tick.temp_wantexponent for tick in labeledticks): for tick in labeledticks: if tick.num: tick.temp_wantexponent = True # mark mantissa == 1 to be not labeled if self.skipmantissaunity == skipmantissaunity.each: for tick in labeledticks: if tick.temp_wantexponent and tick.temp_mantissa == 1: tick.temp_mantissa = None elif self.skipmantissaunity == skipmantissaunity.all and all(tick.temp_mantissa == 1 for tick in labeledticks if tick.temp_wantexponent): for tick in labeledticks: if tick.temp_wantexponent: tick.temp_mantissa = None # construct labels basetick = Tick(self.base, labellevel=0) self.basetexter.labels([basetick]) for tick in labeledticks: if tick.temp_wantexponent: if tick.temp_mantissa is not None: tick.temp_mantissatick = Tick(tick.temp_sign * tick.temp_mantissa, labellevel=0) tick.temp_exponenttick = Tick(tick.temp_exponent, labellevel=0) else: tick.temp_mantissatick = tick self.mantissatexter.labels([tick.temp_mantissatick for tick in labeledticks if tick.temp_mantissa is not None]) self.exponenttexter.labels([tick.temp_exponenttick for tick in labeledticks if tick.temp_wantexponent]) for tick in labeledticks: if tick.temp_wantexponent: if tick.temp_mantissa is not None: mantissalabel_tex = tick.temp_mantissatick.label + self.multiplication_tex mantissalabel_unicode = tick.temp_mantissatick.label + self.multiplication_unicode else: mantissalabel_tex = self.minusunity if tick.temp_sign == -1 else "" mantissalabel_unicode = self.minusunity if tick.temp_sign == -1 else "" tick.label = text.MultiEngineText("%s%s^{%s}" % (mantissalabel_tex, basetick.label, tick.temp_exponenttick.label), [mantissalabel_unicode + basetick.label, text.Text(tick.temp_exponenttick.label, scale=0.8, shift=0.5)])