def getglyphinfo(self, glyph, flex=True): logger.warning("We are about to extract font information for the Type 1 font '%s' from its pfb file. This is bad practice (and it's slow). You should use an afm file instead." % self.name) context = T1context(self, flex=flex) p = path() self.updateglyphpath(glyph, p, trafo.trafo(), context) bbox = p.bbox() return context.wx, context.wy, bbox.llx_pt, bbox.lly_pt, bbox.urx_pt, bbox.ury_pt
def getglyphinfo(self, glyph): warnings.warn("We are about to extract font information for the Type 1 font '%s' from its pfb file. This is bad practice (and it's slow). You should use an afm file instead." % self.name) context = T1context(self) p = path() self.updateglyphpath(glyph, p, trafo.trafo(), context) bbox = p.bbox() return context.wx, context.wy, bbox.llx_pt, bbox.lly_pt, bbox.urx_pt, bbox.ury_pt
def _offset(self): """ :return: The drawing offset for the Window canvas :rtype: ``vector`` """ from pyx import trafo return trafo.trafo(vector=(self.width / 2.0, self.height / 2.0))
def embed(self, canvas, x=0, y=0): """ Embeds this Window into the parent canvas at position (x,y) The Window will be scaled according to the current scaling factor :param canvas: the parent canvas :type canvas: ``canvas`` :param x: window x-coordinate in the parent canvas (default 0) :type x: ``int`` or ``float`` :param y: window y-coordinate in the parent canvas (default 0) :type x: ``int`` or ``float`` """ from pyx import trafo offset = trafo.trafo(matrix=((self.scale, 0), (0, self.scale)), vector=(x, y)) canvas.insert(self._canvas, [offset])
def draw_continent_circle(con, name="", draw_upper_landscape=True, draw_lower_landscape=False, draw_upper_green=True, draw_lower_purple=False, draw_train_tracks=False, draw_foliation=True, foliation_style_old=False, foliation_style_split=False, foliation_style_cusp_leaves=True, foliation_style_boundary_leaves=True, shade_triangles=False, draw_fund_domain=False, fund_dom_tets=None, draw_fund_domain_edges=False, draw_tetrahedron_rectangles=[]): global_scale_up = 10.0 edge_thickness = 0.02 track_thickness = 0.02 leaf_thickness = 0.03 edge_colours = { True: color.rgb(0.9, 0.3, 0), False: color.rgb(0, 0.3, 0.9) } green = color.rgb(0.0, 0.5, 0.0) purple = color.rgb(0.5, 0.0, 0.5) scl = trafo.trafo(matrix=((global_scale_up, 0), (0, global_scale_up)), vector=(0, 0)) canv = canvas.canvas() canv.stroke(path.circle(0, 0, global_scale_up), [style.linewidth(0.02)]) n = len(con.coast) for v in con.coast: i = v.coastal_index t = 2 * pi * float(i) / float(n) v.circle_pos = complex(cos(t), sin(t)) vert_pos = v.circle_pos * 1.01 * global_scale_up canv.text(vert_pos.real, vert_pos.imag, "$" + str(con.vertices.index(v)) + "$", textattrs=[ text.size(-4), text.halign.left, text.valign.middle, trafo.rotate( (180 / pi) * atan2(vert_pos.imag, vert_pos.real)) ]) # vert_pos2 = v.circle_pos * 1.2 * global_scale_up # p = path.path(path.moveto(vert_pos.real, vert_pos.imag), path.lineto(vert_pos2.real, vert_pos2.imag)) # canv.stroke(p, [deco.curvedtext("$"+str(con.vertices.index(v))+"$")]) ### highlight vertices of tetrahedra in a fundamental domain if draw_fund_domain: if fund_dom_tets == None: fund_dom_tets = get_fund_domain_tetrahedra(con) for con_tet in fund_dom_tets: if type( con_tet ) == continent_tetrahedron: ### could be an integer if we didnt find this tet if draw_fund_domain_edges: for e in con_tet.edges(): col = edge_colours[e.is_red] u, v = e.vertices p = make_arc(u.circle_pos, v.circle_pos) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(edge_thickness), style.linecap.round, col ]) update_fund_dom_tet_nums(con, fund_dom_tets) for v in [v for v in con.coast if v.fund_dom_tet_nums != []]: vert_pos = v.circle_pos * 1.03 * global_scale_up canv.text(vert_pos.real, vert_pos.imag, "$" + str(v.fund_dom_tet_nums) + "$", textattrs=[ text.size(-4), text.halign.left, text.valign.middle, trafo.rotate( (180 / pi) * atan2(vert_pos.imag, vert_pos.real)) ]) # lower_colours = {True: color.rgb(0.5,0.3,0), False: color.rgb(0,0.3,0.5)} # upper_colours = {True: color.rgb(0.9,0.3,0), False: color.rgb(0,0.3,0.9)} landscape_edges = [con.lower_landscape_edges, con.upper_landscape_edges] # colours = [lower_colours, upper_colours] upper_tris = con.upper_landscape_triangles lower_tris = con.lower_landscape_triangles boundary_tris = [lower_tris, upper_tris] if shade_triangles: u, v, w = con.triangle_path[0].vertices p = make_arc(u.circle_pos, v.circle_pos) q = make_arc(v.circle_pos, w.circle_pos) r = make_arc(w.circle_pos, u.circle_pos) p.append(q[1]) p.append(r[1]) ### remove extraneous moveto commands p = p.transformed(scl) canv.stroke( p, [deco.filled([color.transparency(0.8)]), style.linewidth(0)]) u, v, w = con.triangle_path[-1].vertices p = make_arc(u.circle_pos, v.circle_pos) q = make_arc(v.circle_pos, w.circle_pos) r = make_arc(w.circle_pos, u.circle_pos) p.append(q[1]) p.append(r[1]) ### remove extraneous moveto commands p = p.transformed(scl) canv.stroke( p, [deco.filled([color.transparency(0.8)]), style.linewidth(0)]) # for triangle in con.triangle_path: # u,v,w = triangle.vertices # p = make_arc(u.circle_pos, v.circle_pos) # q = make_arc(v.circle_pos, w.circle_pos) # r = make_arc(w.circle_pos, u.circle_pos) # p.append(q[1]) # p.append(r[1]) ### remove extraneous moveto commands # p = p.transformed(scl) # canv.stroke(p, [deco.filled([color.transparency(0.8)]), style.linewidth(0)]) to_do = [] if draw_lower_landscape: to_do.append(0) if draw_upper_landscape: to_do.append(1) for i in to_do: for e in landscape_edges[i]: col = edge_colours[e.is_red] transp = [] if i == 0: transp = [color.transparency(0.75)] u, v = e.vertices p = make_arc(u.circle_pos, v.circle_pos) p = p.transformed(scl) canv.stroke( p, [style.linewidth(edge_thickness), style.linecap.round, col] + transp) for tri in boundary_tris[i]: center = incenter(tri.vertices[0].circle_pos, tri.vertices[1].circle_pos, tri.vertices[2].circle_pos) # canv.fill(path.circle(global_scale_up*center[0], global_scale_up*center[1], 0.1)) canv.text(global_scale_up * center[0], global_scale_up * center[1], "$" + str(tri.index) + "$", textattrs=[ text.size(-2), text.halign.center, text.valign.middle ] + transp) ### train tracks... purple_train_routes = [ ] ### pairs of coastal edges corresponding to a train route green_train_routes = [] if draw_lower_purple: if draw_train_tracks: for tri in lower_tris: midpts = [] is_reds = [] for e in tri.edges: is_reds.append(e.is_red) u, v = e.vertices p, midpt = make_arc(u.circle_pos, v.circle_pos, return_midpt=True) midpts.append(midpt) for i in range(3): if (is_reds[i] == is_reds[(i + 1) % 3]) or ( not is_reds[i] and is_reds[(i + 1) % 3]): p = make_arc(midpts[i], midpts[(i + 1) % 3]) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(track_thickness), style.linecap.round, purple ]) if draw_foliation: for edge in con.lower_landscape_edges: leaf_end_edges = [] if edge.is_coastal(): if not edge.is_coastal_sink(upper=False): leaf_end_edges.append(edge) for tri in edge.boundary_triangles: if not tri.is_upper: last_tri = con.flow(tri)[0] last_edge = last_tri.edges[ last_tri.downriver_index()] leaf_end_edges.append(last_edge) else: if edge.is_watershed(): for tri in edge.boundary_triangles: last_tri = con.flow(tri)[0] last_edge = last_tri.edges[ last_tri.downriver_index()] leaf_end_edges.append(last_edge) if len(leaf_end_edges) == 2: purple_train_routes.append(leaf_end_edges) if foliation_style_old: leaf_ends = [] for e in leaf_end_edges: endpts = e.vertices _, midpt = make_arc(endpts[0].circle_pos, endpts[1].circle_pos, return_midpt=True) leaf_ends.append(midpt) p = make_arc(leaf_ends[0], leaf_ends[1]) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, purple ]) if draw_upper_green: if draw_train_tracks: for tri in upper_tris: midpts = [] is_reds = [] for e in tri.edges: is_reds.append(e.is_red) u, v = e.vertices p, midpt = make_arc(u.circle_pos, v.circle_pos, return_midpt=True) midpts.append(midpt) for i in range(3): if (is_reds[i] == is_reds[(i + 1) % 3]) or ( is_reds[i] and not is_reds[(i + 1) % 3]): p = make_arc(midpts[i], midpts[(i + 1) % 3]) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(track_thickness), style.linecap.round, green ]) if draw_foliation: for edge in con.upper_landscape_edges: leaf_end_edges = [] if edge.is_coastal(): if not edge.is_coastal_sink(upper=True): leaf_end_edges.append(edge) for tri in edge.boundary_triangles: if tri.is_upper: last_tri = con.flow(tri)[0] last_edge = last_tri.edges[ last_tri.downriver_index()] leaf_end_edges.append(last_edge) else: if edge.is_watershed(): for tri in edge.boundary_triangles: last_tri = con.flow(tri)[0] last_edge = last_tri.edges[ last_tri.downriver_index()] leaf_end_edges.append(last_edge) if len(leaf_end_edges) == 2: green_train_routes.append(leaf_end_edges) if foliation_style_old: leaf_ends = [] for e in leaf_end_edges: endpts = e.vertices _, midpt = make_arc(endpts[0].circle_pos, endpts[1].circle_pos, return_midpt=True) leaf_ends.append(midpt) p = make_arc(leaf_ends[0], leaf_ends[1]) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, green ]) if draw_foliation and (foliation_style_split or foliation_style_cusp_leaves or foliation_style_boundary_leaves): for e in con.coastal_edges: e.purple_ends = [] e.green_ends = [] for e1, e2 in purple_train_routes: e1.purple_ends.append(e2) e2.purple_ends.append(e1) for e1, e2 in green_train_routes: e1.green_ends.append(e2) e2.green_ends.append(e1) for i, e in enumerate(con.coastal_edges): rotated_coastal_edges = con.coastal_edges[ i:] + con.coastal_edges[:i] e.purple_ends.sort( key=lambda e_other: rotated_coastal_edges.index(e_other), reverse=True) e.green_ends.sort( key=lambda e_other: rotated_coastal_edges.index(e_other), reverse=True) if e.is_red: e.ends = e.green_ends + e.purple_ends else: e.ends = e.purple_ends + e.green_ends if foliation_style_split: for e1, e2 in purple_train_routes: p1 = end_pos(e2, e1) p2 = end_pos(e1, e2) p = make_arc(p1, p2) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, purple ]) for e1, e2 in green_train_routes: p1 = end_pos(e2, e1) p2 = end_pos(e1, e2) p = make_arc(p1, p2) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, green ]) if foliation_style_cusp_leaves or foliation_style_boundary_leaves: for i, c in enumerate(con.coast): c.purple_thorn_end_positions = [] ### complex numbers c.purple_thorn_ends = [ ] ### [coastal arc, position along that arc] e = con.coastal_edges[i] e1 = e.purple_ends[0] while True: index = e1.purple_ends.index(e) if index == len(e1.purple_ends) - 1: break else: c.purple_thorn_end_positions.append( end_pos(e, e1, offset=0.5)) c.purple_thorn_ends.append((e1, e1.ends.index(e))) e, e1 = e1, e1.purple_ends[index + 1] if foliation_style_boundary_leaves: e_before = con.coastal_edges[(i - 1) % len(con.coast)] e_after = con.coastal_edges[i] first_pos = end_pos(e_after.purple_ends[0], e_after, offset=-0.25) last_pos = end_pos(e_before.purple_ends[-1], e_before, offset=0.25) c.purple_thorn_end_positions = [ first_pos ] + c.purple_thorn_end_positions + [last_pos] arcs = [] for i in range(len(c.purple_thorn_end_positions) - 1): arcs.append( make_arc(c.purple_thorn_end_positions[i], c.purple_thorn_end_positions[i + 1])) for p in arcs: p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, purple ]) if foliation_style_cusp_leaves: for thorn_end in c.purple_thorn_ends: thorn_end_pos = end_pos2(thorn_end) p = make_arc(c.circle_pos, thorn_end_pos) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, purple ]) for i, c in enumerate(con.coast): c.green_thorn_end_positions = [] ### complex numbers c.green_thorn_ends = [ ] ### [coastal arc, position along that arc] e = con.coastal_edges[i] e1 = e.green_ends[0] while True: index = e1.green_ends.index(e) if index == len(e1.green_ends) - 1: break else: c.green_thorn_end_positions.append( end_pos(e, e1, offset=0.5)) c.green_thorn_ends.append((e1, e1.ends.index(e))) e, e1 = e1, e1.green_ends[index + 1] if foliation_style_boundary_leaves: e_before = con.coastal_edges[(i - 1) % len(con.coast)] e_after = con.coastal_edges[i] first_pos = end_pos(e_after.green_ends[0], e_after, offset=-0.25) last_pos = end_pos(e_before.green_ends[-1], e_before, offset=0.25) c.green_thorn_end_positions = [ first_pos ] + green_thorn_end_positions + [last_pos] arcs = [] for i in range(len(c.green_thorn_end_positions) - 1): arcs.append( make_arc(c.green_thorn_end_positions[i], c.green_thorn_end_positions[i + 1])) for p in arcs: p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, green ]) if foliation_style_cusp_leaves: for thorn_end in c.green_thorn_ends: thorn_end_pos = end_pos2(thorn_end) p = make_arc(c.circle_pos, thorn_end_pos) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(leaf_thickness), style.linecap.round, green ]) for tet in draw_tetrahedron_rectangles: purple_sides = tet_purple_rectangle_sides( tet, actually_do_green=False) green_sides = tet_purple_rectangle_sides( tet, actually_do_green=True) for side in purple_sides: for cusp_leaf in side: if cusp_leaf != None: v, thorn_end = cusp_leaf thorn_end_pos = end_pos2(thorn_end) p = make_arc(v.circle_pos, thorn_end_pos) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(2 * leaf_thickness), style.linecap.round, purple ]) for side in green_sides: for cusp_leaf in side: if cusp_leaf != None: v, thorn_end = cusp_leaf thorn_end_pos = end_pos2(thorn_end) p = make_arc(v.circle_pos, thorn_end_pos) p = p.transformed(scl) canv.stroke(p, [ style.linewidth(2 * leaf_thickness), style.linecap.round, green ]) output_filename = 'Images/CircleContinent/' + name + '.pdf' canv.writePDFfile(output_filename)