def export_svg(fn, paths, size, line_with=0.1, scale_factor=None): from cairocffi import SVGSurface, Context from .ddd import spatial_sort_2d as sort if not scale_factor: scale_factor = size s = SVGSurface(fn, size, size) c = Context(s) c.set_line_width(0.1) paths = sort(paths) for path in paths: path *= scale_factor c.new_path() c.move_to(*path[0,:]) for p in path[1:]: c.line_to(*p) c.stroke() c.save()
def coloured_bezier(ctx: cairo.Context, p0, p1, p2, p3, colors, width, detail=100, fade=None): p0 = np.array(p0) p1 = np.array(p1) p2 = np.array(p2) p3 = np.array(p3) bez, bezd = cubic_bezier(p0, p1, p2, p3, numpoints=detail) bezd = normalize(bezd, norm='l2', axis=1) frac_sum = -.5 for color, frac in colors: (r, g, b, a) = color if fade is None: ctx.set_source_rgba(r, g, b, a) else: ctx.set_source(fade_pattern(p0[0], p0[1], p3[0], p3[1], r, g, b, a, fade)) ctx.set_line_width(frac*width) frac_sum += frac/2 ctx.move_to(bez[0][0] - width * frac_sum * bezd[0][1], bez[0][1] + width * frac_sum * bezd[0][0]) for i in range(0, bez.shape[0] - 3, 3): ctx.curve_to(bez[i + 1][0] - width * frac_sum * bezd[i + 1][1], bez[i + 1][1] + width * frac_sum * bezd[i + 1][0], bez[i + 2][0] - width * frac_sum * bezd[i + 2][1], bez[i + 2][1] + width * frac_sum * bezd[i + 2][0], bez[i + 3][0] - width * frac_sum * bezd[i + 3][1], bez[i + 3][1] + width * frac_sum * bezd[i + 3][0]) # for i in range(0, bez.shape[0] - 3, 2): # ctx.move_to(bez[i][0] - width * frac_sum * bezd[i][1], bez[i][1] + width * frac_sum * bezd[i][0]) # ctx.curve_to(bez[i + 1][0] - width * frac_sum * bezd[i + 1][1], bez[i + 1][1] + width * frac_sum * bezd[i + 1][0], # bez[i + 2][0] - width * frac_sum * bezd[i + 2][1], bez[i + 2][1] + width * frac_sum * bezd[i + 2][0], # bez[i + 3][0] - width * frac_sum * bezd[i + 3][1], bez[i + 3][1] + width * frac_sum * bezd[i + 3][0]) ctx.stroke() frac_sum += frac/2
def draw(self, ctx: Context, square: bool) -> None: point_diameter_current = self.point_diameter_initial ctx.set_source_rgb(self.color[0], self.color[1], self.color[2]) points_per_row = math.floor(self.target_width / self.point_margin) i = 0 while ((self.point_diameter_check_is_greater and point_diameter_current > self.point_diameter_final) or (not self.point_diameter_check_is_greater and point_diameter_current < self.point_diameter_final)): col = i % points_per_row row = math.floor(i / points_per_row) x = self.position[ 0] + col * self.point_margin + self.point_margin / 2 y = self.position[ 1] + row * self.point_margin + self.point_margin / 2 radius = point_diameter_current / 2 if square: ctx.move_to(x - radius, y - radius) ctx.line_to(x + radius, y - radius) ctx.line_to(x + radius, y + radius) ctx.line_to(x - radius, y + radius) ctx.fill() else: ctx.arc(x, y, radius, 0, 2 * math.pi) ctx.fill() i += 1 point_diameter_current += self.point_diameter_delta
def _(d: Line, ctx: cairo.Context, scale: float): ctx.save() ctx.move_to(d.x0 * scale, d.y0 * scale) ctx.line_to(d.x1 * scale, d.y1 * scale) ctx.stroke() # ctx.fill() ctx.restore()
def draw_polygon(ctx: Context, polygon: Polygon): if hasattr(polygon.exterior, 'coords'): start = True ctx.new_path() for x, y in polygon.exterior.coords: if start: ctx.move_to(x, y) else: ctx.line_to(x, y) start = False for interior in polygon.interiors: ctx.new_sub_path() start = True for x, y in interior.coords: if start: ctx.move_to(x, y) else: ctx.line_to(x, y) start = False ctx.close_path() ctx.close_path()
def draw(self, ctx: Context, polygon: bool) -> None: line_width_current = self.line_width_initial ctx.set_source_rgb(self.color[0], self.color[1], self.color[2]) x_start = self.position[0] x_end = x_start + self.target_width y = self.position[1] while ((self.line_width_check_is_greater and line_width_current > self.line_width_final) or (not self.line_width_check_is_greater and line_width_current < self.line_width_final)): ctx.set_line_width(line_width_current) y += line_width_current / 2 if polygon: ctx.move_to(x_start, y - line_width_current / 2) ctx.line_to(x_end, y - line_width_current / 2) ctx.line_to(x_end, y + line_width_current / 2) ctx.line_to(x_start, y + line_width_current / 2) ctx.fill() else: ctx.move_to(x_start, y) ctx.line_to(x_end, y) ctx.stroke() # Add the white space y += line_width_current # prepare next line width y += line_width_current / 2 line_width_current += self.line_width_delta
def draw_text(self, g: Graphics, component: Label, font: FontFace, color: RGBA) -> None: text = component.text size = component.text_size extents = component.context.toolkit.fonts.text_extent(text, font, size) padding = component.resolve_insets(StyleKeys.Padding).value_or( Insets(0, 0, 0, 0)) (x, y, w, h) = component.bounds.tuple rh = self._ratio_for_align[component.text_align] rv = self._ratio_for_align[component.text_vertical_align] tx = (w - extents.width - padding.left - padding.right) * rh + x + padding.left ty = (h - extents.height - padding.top - padding.bottom) * rv + extents.height + y + padding.top g.set_font_face(font) g.set_font_size(size) # FIXME: Temporary workaround until we get a better way of handling text shadows. if component.shadow: g.move_to(tx + 1, ty + 1) g.set_source_rgba(0, 0, 0, 0.8) g.show_text(text) g.move_to(tx, ty) g.set_source_rgba(color.r, color.g, color.b, color.a) g.show_text(text)
def draw_line_string(ctx: Context, line_string: LineString): start = True for x, y in line_string.coords: if start: ctx.move_to(x, y) else: ctx.line_to(x, y) start = False
def paint_foreground(self, ctx: Context): ctx.set_line_width(self.line_width) if self.orientation == Line.Orientation.HORIZONTAL: ctx.move_to(*self.size.position(Anchor.CENTER_LEFT)) ctx.line_to(*self.size.position(Anchor.CENTER_RIGHT)) else: ctx.move_to(*self.size.position(Anchor.TOP_CENTER)) ctx.line_to(*self.size.position(Anchor.BOTTOM_CENTER)) ctx.stroke()
def paint_scale_background(self, ctx: Context): cpu_count = Global.system_data.cpu_count for i in range(cpu_count): if i % 2: ctx.set_source_rgba(*Color.GRAY33) else: ctx.set_source_rgba(*Color.GRAY50) ctx.move_to(0, self.height - self.height / cpu_count * i) ctx.line_to(self.width, self.height - self.height / cpu_count * i) ctx.stroke()
def _rectangle_path( context: cairocffi.Context, rectangle: pangocffi.Rectangle ): x, y, width, height = _convert_rectangle_to_pixels(rectangle) context.move_to(x, y) context.line_to(x + width, y) context.line_to(x + width, y + height) context.line_to(x, y + height) context.line_to(x, y)
def draw_ray_trace(r0: Sequence[float], elements: Sequence[Element], ctx: cairo.Context, scale: float): ctx.save() r = r0 for element in elements: ctx.move_to(0, r[0] * scale) ctx.translate(element.thickness * scale, 0) rp = element.matrix.dot(r) ctx.line_to(0, rp[0] * scale) ctx.stroke() r = rp ctx.restore()
def _coordinate_path( context: cairocffi.Context, point: Tuple[int, int], radius: float = 1 ): for i in range(0, 8): p_x = pangocffi.units_to_double(point[0]) + \ math.sin(i/4 * math.pi) * radius p_y = pangocffi.units_to_double(point[1]) + \ math.cos(i/4 * math.pi) * radius if i == 0: context.move_to(p_x, p_y) else: context.line_to(p_x, p_y)
def paint_foreground(self, ctx: Context): if self._image: sx = self.width / self._image.get_width() sy = self.height / self._image.get_height() s = min(sx, sy) sw = self._image.get_width() * s sh = self._image.get_height() * s ctx.scale(s, s) x = 0 y = 0 if self.alignment == Anchor.TOP_LEFT: x = 0 y = 0 elif self.alignment == Anchor.TOP_CENTER: x = (self.width - sw) / 2 y = 0 elif self.alignment == Anchor.TOP_RIGHT: x = self.width - sw y = 0 elif self.alignment == Anchor.CENTER_LEFT: x = 0 y = (self.height - sh) / 2 elif self.alignment == Anchor.CENTER_CENTER: x = (self.width - sw) / 2 y = (self.height - sh) / 2 elif self.alignment == Anchor.CENTER_RIGHT: x = (self.width - sw) y = (self.height - sh) / 2 elif self.alignment == Anchor.BOTTOM_LEFT: x = 0 y = self.height - sh elif self.alignment == Anchor.BOTTOM_CENTER: x = (self.width - sw) / 2 y = self.height - sh elif self.alignment == Anchor.BOTTOM_RIGHT: x = self.width - sw y = self.height - sh x /= s y /= s ctx.set_source_surface(self._image, x, y) ctx.paint() else: ctx.move_to(0, 0) ctx.line_to(self.size.width, self.size.height) ctx.stroke() ctx.move_to(0, self.size.height) ctx.line_to(self.size.width, 0) ctx.stroke()
def paint_foreground(self, ctx: Context): layout = self.font.get_layout(self.text, ctx, self.foreground, self.escape) if self.h_alignment == TextWidget.HAlignment.LEFT: layout.set_alignment(Alignment.LEFT) else: layout.set_width(round(self.width * 1000)) if self.h_alignment == TextWidget.HAlignment.CENTER: layout.set_alignment(Alignment.CENTER) elif self.h_alignment == TextWidget.HAlignment.RIGHT: layout.set_alignment(Alignment.RIGHT) y = -layout.get_extents()[0].y / 1000 if self.v_alignment == TextWidget.VAlignment.BOTTOM: y += self.height - layout.get_extents()[0].height / 1000 elif self.v_alignment == TextWidget.VAlignment.CENTER: y += (self.height - layout.get_extents()[0].height / 1000) / 2 ctx.move_to(0, y) pangocairo.show_layout(ctx, layout)
def __draw_sector (self: 'HueSatWheelWidget', cr: cairocffi.Context, center_x: float, center_y: float) -> None: cr.save () cr.set_line_width (1) offset = self.rotation for idx, sector in enumerate (self.sector[0]): half_angle = 2 * numpy.pi * sector / 2 offset += self.sector[1][idx] * 2 * numpy.pi cr.set_source_rgba (0, 0, 0, 1) cr.move_to (center_x, center_y) cr.arc (center_x, center_y, (self.size - 2) / 2., offset - half_angle, half_angle + offset) cr.line_to (center_x, center_y) cr.stroke_preserve () cr.set_source_rgba (0, 0, 0, 0.25) cr.fill () cr.restore ()
def draw_labels(self, ctx: Context) -> None: point_diameter_current = self.point_diameter_initial text_height_room = 0 ctx.set_source_rgb(self.color[0], self.color[1], self.color[2]) points_per_row = math.floor(self.target_width / self.point_margin) i = 0 while ((self.point_diameter_check_is_greater and point_diameter_current > self.point_diameter_final) or (not self.point_diameter_check_is_greater and point_diameter_current < self.point_diameter_final)): col = i % points_per_row row = math.floor(i / points_per_row) x = self.position[ 0] + col * self.point_margin + self.point_margin / 2 y = self.position[ 1] + row * self.point_margin + self.point_margin / 2 first_point_diameter_in_row = point_diameter_current last_point_diameter_in_row = point_diameter_current + self.point_diameter_delta * ( points_per_row - 1) print(first_point_diameter_in_row) print(last_point_diameter_in_row) if text_height_room > 2: ctx.move_to(x + 0, y) ctx.line_to(x + 2, y) ctx.stroke() self.draw_text( ctx, "{0:.3f} - {1:.3f}".format(first_point_diameter_in_row, last_point_diameter_in_row) + " mm", x + 3, y, 2) text_height_room = 0 i += points_per_row point_diameter_current += self.point_diameter_delta * points_per_row text_height_room += self.point_margin
def rounded_rectangle(ctx: cairo.Context, x, y, w, h, rx, ry): # https://www.cairographics.org/cookbook/roundedrectangles/ arc_to_bezier = 0.55228475 if rx > w - rx: rx = w / 2 if ry > h - ry: ry = h / 2 c1 = arc_to_bezier * rx c2 = arc_to_bezier * ry ctx.new_path() ctx.move_to(x + rx, y) ctx.rel_line_to(w - 2 * rx, 0.0) ctx.rel_curve_to(c1, 0.0, rx, c2, rx, ry) ctx.rel_line_to(0, h - 2 * ry) ctx.rel_curve_to(0.0, c2, c1 - rx, ry, -rx, ry) ctx.rel_line_to(-w + 2 * rx, 0) ctx.rel_curve_to(-c1, 0, -rx, -c2, -rx, -ry) ctx.rel_line_to(0, -h + 2 * ry) ctx.rel_curve_to(0.0, -c2, rx - c1, -ry, rx, -ry) ctx.close_path()
def draw_labels(self, ctx: Context): ctx.set_line_width(0.1) line_width_current = self.line_width_initial text_height_room = 0 ctx.set_source_rgb(self.color[0], self.color[1], self.color[2]) x = self.position[0] y = self.position[1] while ((self.line_width_check_is_greater and line_width_current > self.line_width_final) or (not self.line_width_check_is_greater and line_width_current < self.line_width_final)): y += line_width_current / 2 text_height_room += line_width_current / 2 # Draw the line annotating the row if text_height_room > 2: ctx.move_to(x + 0, y) ctx.line_to(x + 2, y) ctx.stroke() self.draw_text(ctx, "{0:.2f}".format(line_width_current) + " mm", x + 3, y, 2) text_height_room = 0 # Add the white space y += line_width_current text_height_room += line_width_current # prepare next line width y += line_width_current / 2 text_height_room += line_width_current / 2 line_width_current += self.line_width_delta
def _show_layout_y_ranges( context: cairocffi.Context, layout: pangocffi.Layout ): layout_iter = layout.get_iter() context.set_line_width(0.5) context.set_dash([1, 1]) alternate = True while True: alternate = not alternate extents = layout_iter.get_line_extents() y_ranges = layout_iter.get_line_yrange() context.set_source_rgba(0, 0, 1 if alternate else 0.5, 0.9) context.move_to( pangocffi.units_to_double(extents[0].x), pangocffi.units_to_double(y_ranges[0]) ) context.line_to( pangocffi.units_to_double(extents[0].x + extents[0].width), pangocffi.units_to_double(y_ranges[0]) ) context.stroke() context.move_to( pangocffi.units_to_double(extents[0].x), pangocffi.units_to_double(y_ranges[1]) ) context.line_to( pangocffi.units_to_double(extents[0].x + extents[0].width), pangocffi.units_to_double(y_ranges[1]) ) context.stroke() if not layout_iter.next_run(): break
def _generate_layer(self, transcription, page, layer): surface = PDFSurface(layer, *page.page_size) context = Context(surface) # context.select_font_face('Georgia') context.set_source_rgba(1, 1, 1, 1 / 256) # almost invisible context.set_font_size(2) for line_ink, line_transcription in zip(page.lines, transcription): ink, transformation = writing.normalized(line_ink) context.save() context.transform( Matrix(*(Transformation.translation( 0, page.page_size[1]).parameter))) context.transform(Matrix(*(Transformation.mirror(0).parameter))) context.transform(Matrix(*((~transformation).parameter))) context.transform(Matrix(*(Transformation.mirror(0).parameter))) HANDWRITING_WIDTH = ink.boundary_box[1] TYPEWRITING_WIDTH = context.text_extents(line_transcription)[2] context.scale(HANDWRITING_WIDTH / TYPEWRITING_WIDTH, 1) context.move_to(0, 0) context.show_text(line_transcription) context.restore() context.stroke() context.show_page() surface.flush()
def _show_layout_baseline( context: cairocffi.Context, layout: pangocffi.Layout ): layout_iter = layout.get_iter() context.set_line_width(0.5) context.set_dash([1, 1]) while True: extents = layout_iter.get_line_extents() baseline = layout_iter.get_baseline() y_ranges = layout_iter.get_line_yrange() context.set_source_rgba(1, 0, 0, 0.9) context.move_to( pangocffi.units_to_double(extents[0].x), pangocffi.units_to_double(y_ranges[0]) ) context.line_to( pangocffi.units_to_double(extents[0].x + extents[0].width), pangocffi.units_to_double(y_ranges[0]) ) context.stroke() context.set_source_rgba(0, 1, 0, 0.9) context.stroke() context.move_to( pangocffi.units_to_double(extents[0].x), pangocffi.units_to_double(baseline) ) context.line_to( pangocffi.units_to_double(extents[0].x + extents[0].width), pangocffi.units_to_double(baseline) ) context.stroke() context.set_source_rgba(0, 0, 1, 0.9) context.move_to( pangocffi.units_to_double(extents[0].x), pangocffi.units_to_double(y_ranges[1]) ) context.line_to( pangocffi.units_to_double(extents[0].x + extents[0].width), pangocffi.units_to_double(y_ranges[1]) ) context.stroke() if not layout_iter.next_run(): break
y = mean_pin_y - label_height // 2 print(f"{text=}\t{y=}") if pass_ == 0: ctx.set_source_rgba(*color, 1) draw_rounded_rectangle(ctx, x, y, label_width, label_height, label_radius) ctx.fill() for pin_name in pin_names: pin_x, pin_y, align = pin_defs[pin_name] pin_x = pin_x * w pin_y = pin_y * h_bg - h_bg * y_offset ctx.move_to(line_origin_x, y + label_height // 2) ctx.line_to(w * pad_sides + pin_x, pin_y) ctx.set_line_width(4) ctx.stroke() else: ctx.move_to(x + label_pad[0] - x_bearing, y - y_bearing + label_pad[1]) ctx.set_source_rgb(1, 1, 1) ctx.show_text(text) # save to file canvas.write_to_png(f"{board_name}_{variant_name}.png") # stride = ImageSurface.format_stride_for_width(format, width) # data = bytearray(stride * height) # surface = ImageSurface(format, width, height, data, stride)
def draw_polyline(ctx: cairo.Context, xys): ctx.move_to(*xys[0]) for x, y in xys[1:]: ctx.line_to(x, y)