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 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 paint_foreground(self, ctx: Context): if self.border_corner: draw_rounded_rectangle(ctx, Rectangle(ZERO_TOP_LEFT, self.size), self.border_corner) ctx.clip() self.paint_scale_background(ctx) if self.values: pos = 0.0 for value in self.values: l_pos = pos pos += value[0] / self._total if value[1]: ctx.set_source_rgba(*value[1]) if self.orientation == BarWidget.Orientation.HORIZONTAL_LEFT_TO_RIGHT: ctx.rectangle(l_pos * self.width, 0, (pos - l_pos) * self.width, self.height) elif self.orientation == BarWidget.Orientation.HORIZONTAL_RIGHT_TO_LEFT: ctx.rectangle(self.width - l_pos * self.width, 0, self.width - (l_pos - pos) * self.width, self.height) elif self.orientation == BarWidget.Orientation.VERTICAL_DOWN: ctx.rectangle(0, l_pos * self.height, self.width, (pos - l_pos) * self.height) elif self.orientation == BarWidget.Orientation.VERTICAL_UP: ctx.rectangle(0, self.height - l_pos * self.height, self.width, (l_pos - pos) * self.height) ctx.fill() self.paint_scale_foreground(ctx) if self.border: ctx.set_source_rgba(*self.border) ctx.set_line_width(self.border_width) draw_rounded_rectangle(ctx, Rectangle(ZERO_TOP_LEFT, self.size), self.border_corner) ctx.stroke()
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, polygons: List[Union[Polygon, MultiPolygon]]): if self.high_quality: ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(self.line_width) self._draw_iterator(ctx, polygons, self._draw_water) else: ctx.set_source_rgba(0, 0, 0, 0.3) self._draw_iterator(ctx, polygons, self._draw_water_area)
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 draw(self, ctx: Context, polygons: List[Union[Polygon, MultiPolygon]]): if self.stroke_set: ctx.set_source_rgba(*self.stroke_color) ctx.set_line_width(self.stroke_width) self._draw_iterator(ctx, polygons, self._draw_stroke) if self.fill_set: ctx.set_source_rgba(*self.fill_color) self._draw_iterator(ctx, polygons, self._draw_fill)
def draw(self, ctx: Context, points: List[Point]): if self.circle_stroke_set: ctx.set_source_rgba(*self.circle_stroke_color) ctx.set_line_width(self.circle_stroke_width) self._draw_iterator(ctx, points, self._draw_stroke) if self.circle_fill_set: ctx.set_source_rgba(*self.circle_fill_color) self._draw_iterator(ctx, points, self._draw_fill)
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
def draw_border( self, g: Graphics, component: T, color: RGBA, thickness: float = GlassLookAndFeel.BorderThickness) -> None: area = component.bounds if area.width == 0 or area.height == 0: return g.set_source_rgba(color.r, color.g, color.b, color.a) g.set_line_width(thickness) self.draw_rect(g, area, 8) g.stroke()
def _show_layout_line_logical_extents( 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() context.set_source_rgba(0, 0, 1 if alternate else 0.5, 0.9) _rectangle_path(context, extents[1]) context.stroke() _coordinate_path(context, (extents[1].x, extents[1].y)) context.fill() if not layout_iter.next_run(): break
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 _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 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 draw_railways(self, ctx: Context, railways: List[LineString]): # font = cairo.ToyFontFace('CMU Concrete', weight=1) # ctx.set_font_face(font) ctx.set_line_width(0.1) ctx.set_source_rgba(0, 0, 0, 1) self._draw_iterator(ctx, railways, self._draw_railway)
def __draw_ring (self: 'HueSatWheelWidget', cr: cairocffi.Context, width: int, height: int, center_x: float, center_y: float, outer: float, inner: float) -> None: self.__redraw = False stride = cairocffi.ImageSurface.format_stride_for_width (cairocffi.FORMAT_ARGB32, width) buf = numpy.empty (int (height * stride), dtype = numpy.uint8) for y in range (height): idx = y * width * 4 dy = -(y - center_y) for x in range (width): dx = x - center_x dist = dx * dx + dy * dy angle = math.atan2 (dy, dx) if angle < 0: angle += 2 * numpy.pi hue = angle / (2 * numpy.pi) hue_idx = int ((angle + 2 * numpy.pi / 3) / (2 * numpy.pi) * 255) hue_idx = hue_idx % 256 if dist < ((inner - 1) ** 2) * (1 - self.__hist[255 - hue_idx]) or \ dist > ((outer + 1) ** 2): buf[idx + 0] = 0 buf[idx + 1] = 0 buf[idx + 2] = 0 buf[idx + 3] = 0 idx += 4 continue r, g, b = colorsys.hsv_to_rgb (hue, 1.0, 1.0) a = 255 buf[idx + 0] = int (math.floor (r * 255 + 0.5)) buf[idx + 1] = int (math.floor (g * 255 + 0.5)) buf[idx + 2] = int (math.floor (b * 255 + 0.5)) buf[idx + 3] = a idx += 4 source = cairocffi.ImageSurface.create_for_data ( memoryview (buf), cairocffi.FORMAT_ARGB32, width, height, stride ) fg_color = self.get_style_context ().get_color (Gtk.StateFlags.NORMAL) cr.save () cr.set_source_rgba (0, 0, 0, 0) cr.paint () cr.set_source_surface (source, 0, 0) cr.paint () cr.set_line_width (1) cr.new_path () cr.set_source_rgba (*list (fg_color)) cr.arc (center_x, center_y, (self.size - 4) / 2. - self.ring_width, 0, 2 * numpy.pi) cr.stroke () cr.arc (center_x, center_y, (self.size - 2) / 2, 0, 2 * numpy.pi) cr.stroke () cr.arc (center_x, center_y, 5, 0, 2 * numpy.pi) cr.fill () cr.restore ()
def draw_walls(self, ctx: Context, walls: List[LineString]): ctx.set_line_width(0.05) ctx.set_source_rgba(0, 0, 0, 1) self._draw_iterator(ctx, walls, self._draw_wall)
def draw(self, ctx: Context): if not self.canvas_set or not self.margin_set or not self.legend_set: raise Exception("Canvas, margin or legend not set!") margin_exterior_top_left = (0, 0) margin_exterior_top_right = (self.canvas_width, 0) margin_exterior_bottom_left = (0, self.canvas_height) margin_exterior_bottom_right = (self.canvas_width, self.canvas_height) legend_exterior_top_left = (self.margin_left, self.margin_top) legend_exterior_top_right = (self.canvas_width - self.margin_right, self.margin_top) legend_exterior_bottom_left = (self.margin_left, self.canvas_height - self.margin_bottom) legend_exterior_bottom_right = (self.canvas_width - self.margin_right, self.canvas_height - self.margin_bottom) legend_interior_top_left = (self.margin_left + self.legend_left, self.margin_top + self.legend_top) legend_interior_top_right = (self.canvas_width - self.margin_right - self.legend_right, self.margin_top + self.legend_top) legend_interior_bottom_left = (self.margin_left + self.legend_left, self.canvas_height - self.margin_bottom - self.legend_bottom) legend_interior_bottom_right = (self.canvas_width - self.margin_right - self.legend_right, self.canvas_height - self.margin_bottom - self.legend_bottom) margin_and_legend = Polygon([ margin_exterior_top_left, margin_exterior_top_right, margin_exterior_bottom_right, margin_exterior_bottom_left, margin_exterior_top_left ], [ list( reversed([ legend_interior_top_left, legend_interior_top_right, legend_interior_bottom_right, legend_interior_bottom_left, legend_interior_top_left ])) ]) legend_exterior = Polygon([ legend_exterior_top_left, legend_exterior_top_right, legend_exterior_bottom_right, legend_exterior_bottom_left, legend_exterior_top_left ]) legend_interior = Polygon([ legend_exterior_top_left, legend_exterior_top_right, legend_exterior_bottom_right, legend_exterior_bottom_left, legend_exterior_top_left ], [ list( reversed([ legend_interior_top_left, legend_interior_top_right, legend_interior_bottom_right, legend_interior_bottom_left, legend_interior_top_left ])) ]) ctx.set_line_width(0.05) ctx.set_source_rgb(1, 1, 1) CairoHelper.draw_polygon(ctx, margin_and_legend) ctx.fill() # ctx.set_line_width(self.exterior_stroke_width) ctx.set_source_rgb(0, 0, 0) CairoHelper.draw_polygon(ctx, legend_exterior) ctx.stroke() # ctx.set_line_width(self.interior_stroke_width) CairoHelper.draw_polygon(ctx, legend_interior) ctx.stroke()
def draw(self, ctx: Context, line_strings: List[Union[LineString, MultiLineString]]): ctx.set_source_rgba(*self.stroke_color) ctx.set_line_width(self.stroke_width) self._draw_iterator(ctx, line_strings, self._draw_stroke)
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)