def print_text(ctx: cairo.Context, text, right=False, fd=default_font): layout = pc.create_layout(ctx) ctx.set_source_rgb(0, 0, 0) layout.set_font_description(fd) layout.set_text(text) w, h = layout.get_pixel_size() if right: ctx.rel_move_to(-w, -h / 2) else: ctx.rel_move_to(0, -h / 2) pc.show_layout(ctx, layout) return (w, h)
def flap_cut_marks(ctx: cairo.Context, width: float, half_h: float, mark_len: float) -> None: cross(ctx, mark_len, ['down', 'right']) ctx.rel_move_to(width, 0) cross(ctx, mark_len, ['down', 'left']) ctx.rel_move_to(0, 2 * half_h) cross(ctx, mark_len, ['up', 'left']) ctx.rel_move_to(-width, 0) cross(ctx, mark_len, ['up', 'right']) ctx.rel_move_to(0, -2 * half_h)
def cross(ctx: cairo.Context, span: float, tblr=['up', 'down', 'left', 'right']) -> None: if 'up' in tblr: ctx.rel_move_to(0, -span / 2) ctx.rel_line_to(0, span / 2) if 'down' in tblr: ctx.rel_move_to(0, span / 2) ctx.rel_line_to(0, -span / 2) if 'left' in tblr: ctx.rel_move_to(-span / 2, 0) ctx.rel_line_to(span / 2, 0) if 'right' in tblr: ctx.rel_move_to(span / 2, 0) ctx.rel_line_to(-span / 2, 0)
def draw(self, cr: cairo.Context) -> None: vertex_pos = self._vertex_pos if vertex_pos is None: return vertex_pos = self._parent._widget_coord_from_canvas(vertex_pos) start_angle = -self._start_angle delta_angle = -self._delta_angle end_angle = start_angle + delta_angle if not (math.isfinite(start_angle) and math.isfinite(end_angle)): return stroke_width = self._stroke_width stroke_color = self._stroke_color cr.set_line_width(stroke_width) cr.set_source_rgb(*stroke_color) # Start marker path start_marker_radius = self._start_marker_radius self._append_hand_path(cr, vertex_pos, start_angle, start_marker_radius) # End marker path end_marker_radius = self._end_marker_radius self._append_hand_path(cr, vertex_pos, end_angle, end_marker_radius) cr.stroke() # Angle arc path angle_radius = self._angle_radius if self._clockwise: which_arc = cr.arc else: which_arc = cr.arc_negative which_arc(*vertex_pos, angle_radius, start_angle, end_angle) cr.stroke() # Text mid_angle = (start_angle + end_angle) / 2 text_radius = self._text_radius text_font_size = self._text_font_size cr.move_to(*vertex_pos) cr.rel_move_to(text_radius * math.cos(mid_angle), text_radius * math.sin(mid_angle)) cr.set_font_size(text_font_size) cr.set_source_rgb(*stroke_color) angle_text = '{:.1f}°'.format(math.degrees(abs(delta_angle))) text_extents = cr.text_extents(angle_text) cr.rel_move_to(-text_extents.x_bearing, -text_extents.y_bearing) cr.rel_move_to(-text_extents.width / 2, -text_extents.height / 2) cr.show_text(angle_text)
def draw(self, cr: cairo.Context) -> None: start_angle = self._start_angle delta_angle = self._delta_angle clockwise = self._clockwise arc_radius = self._arc_radius start_line_radius = self._start_line_radius end_line_radius = self._end_line_radius x = self._x y = self._y stroke_color = self._stroke_color stroke_width = self._stroke_width text_radius = self._text_radius font_size = self._font_size text_color = self._text_color scale_radii = self._scale_radii scale_text = self._scale_text scale_strokes = self._scale_strokes if not (math.isfinite(start_angle) and math.isfinite(delta_angle)): return end_angle = start_angle + delta_angle matrix = cr.get_matrix() if scale_radii: radius_scale = 1/(0.5 * (matrix.xx + matrix.yy)) else: radius_scale = 1.0 self._show_hand(cr, x, y, -start_angle, radius_scale*start_line_radius) self._show_hand(cr, x, y, -end_angle, radius_scale*end_line_radius) if clockwise: cr.arc(x, y, radius_scale*arc_radius, -start_angle, -end_angle) else: cr.arc_negative(x, y, radius_scale*arc_radius, -start_angle, -end_angle) cr.save() if scale_strokes: cr.identity_matrix() cr.set_source_rgba(*stroke_color) cr.set_line_width(stroke_width) cr.stroke() cr.restore() half_angle = (start_angle + end_angle) / 2 cr.move_to(x, y) cr.rel_move_to( radius_scale*text_radius * math.cos(half_angle), radius_scale*text_radius * -math.sin(half_angle), ) cr.save() cr.set_source_rgba(*text_color) cr.set_font_size(font_size) if scale_text: cr.identity_matrix() text = '{:.1f}°'.format(math.degrees(math.fabs(delta_angle))) text_extents = cr.text_extents(text) cr.rel_move_to(-text_extents.x_bearing, -text_extents.y_bearing) cr.rel_move_to(-text_extents.width/2, -text_extents.height/2) cr.show_text(text) cr.fill() cr.restore()
def _show_aligned_text(self, ctx: cairo.Context, text, x, y): extents = ctx.text_extents(text) ctx.rel_move_to(x * extents.width, y * extents.height) ctx.show_text(text)