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 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 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_fill(self, ctx: Context, geom: Union[Polygon, MultiPolygon]): if isinstance(geom, MultiPolygon): for sub_geom in geom.geoms: self._draw_fill(ctx, sub_geom) return CairoHelper.draw_polygon(ctx, geom) ctx.fill()
def draw(self, context: cairo.Context, x: float, y: float, fxy: FixXY): if debug: context.set_source_rgb(1 if self.no_page_break else 0, 0, 0) context.rectangle(*fxy(x - 5, y), *fxy(2, 16)) context.fill() context.set_source_rgb(0.5, 0.5, 0.5) context.rectangle(*fxy(self.indent + x, y), *fxy(self.width, self.height)) context.stroke()
def draw_background(self, g: Graphics, component: T, color: RGBA) -> 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) self.draw_rect(g, area, 8) g.fill()
def draw(self, ctx: Context): text_baseline = self.get_text_baseline(ctx) default_font_max_height = ctx.get_scaled_font().extents()[0] text_max_length = float('inf') line_string_bottom_length = text_baseline.length trimmed = False while text_max_length > line_string_bottom_length: text_max_length = ctx.text_extents(self.text)[4] / default_font_max_height * self.text_height + \ (len(self.text)-1) * self.text_spacing if text_max_length > line_string_bottom_length: self.text = self.text[:-1].strip() trimmed = True text_start_interp_pos_min = 0 text_start_interp_pos_max = line_string_bottom_length - text_max_length if self.text_alignment == 'left': text_start_interp_pos = self.text_alignment_offset elif self.text_alignment == 'center': text_start_interp_pos = line_string_bottom_length / 2 - text_max_length / 2 - self.text_alignment_offset else: text_start_interp_pos = line_string_bottom_length - text_max_length - self.text_alignment_offset text_start_interp_pos = min( text_start_interp_pos_max, max(text_start_interp_pos_min, text_start_interp_pos)) text_interp_pos = text_start_interp_pos for char in self.text: char_pos_origin = text_baseline.interpolate(text_interp_pos) char_advance_x = ctx.text_extents( char)[4] / default_font_max_height * self.text_height char_pos_end = text_baseline.interpolate(text_interp_pos + char_advance_x) ctx.save() ctx.translate(char_pos_origin.x, char_pos_origin.y) text_angle = math.atan2(char_pos_end.y - char_pos_origin.y, char_pos_end.x - char_pos_origin.x) ctx.rotate(text_angle) ctx.scale(1 / default_font_max_height * self.text_height) if not trimmed: ctx.set_source_rgba(0, 0, 0, 1) else: ctx.set_source_rgba(0, 0, 0.2, 1) ctx.show_text(char) ctx.fill() ctx.restore() text_interp_pos += char_advance_x + self.text_spacing
def draw(self, ctx: Context): if not self.canvas_set: raise Exception("Canvas size not set!") top_left = (0, 0) top_right = (self.canvas_width, 0) bottom_left = (0, self.canvas_height) bottom_right = (self.canvas_width, self.canvas_height) canvas = Polygon( [top_left, top_right, bottom_right, bottom_left, top_left]) ctx.set_source_rgba(*self.color) CairoHelper.draw_polygon(ctx, canvas) ctx.fill()
def _draw_grass_area(self, ctx: Context, outline: Polygon): tags = outline.get_osm_tags() if 'leisure' in tags and tags['leisure'] == 'park': ctx.set_source_rgba(0, 0, 0, 0.03) elif 'leisure' in tags and tags['leisure'] == 'garden': ctx.set_source_rgba(0, 0, 0, 0.1) elif 'leisure' in tags and tags['leisure'] == 'common': ctx.set_source_rgba(0, 0, 0, 0.03) elif 'leisure' in tags and tags['leisure'] == 'recreation_ground': ctx.set_source_rgba(0, 0, 0, 0.03) elif 'landuse' in tags and tags['landuse'] == 'grass': ctx.set_source_rgba(0, 0, 0, 0.03) elif 'landuse' in tags and tags['landuse'] == 'forest': ctx.set_source_rgba(0, 0, 0, 0.03) elif 'natural' in tags and tags['natural'] == 'wood': ctx.set_source_rgba(0, 0, 0, 0.03) CairoHelper.draw_polygon(ctx, outline) ctx.fill()
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 make_train_spaces(ctx: cairo.Context, train: trains.Train, broken_spaces: Sequence = None, frac: float = 1 / 4): spaces, brokens = break_spaces(train.spaces, broken_spaces) ctx.save() radius = train.interfaces[ 0].radius # if len(self.interfaces) > 0 else self.spaces[0] y_next = 0 xys_last = np.asarray([[-radius, 0], [radius, 0]]) n = train.interfaces[0].n1 for space, next_interface, broken in zip(spaces, train.interfaces + (None, ), brokens): ctx.set_source_rgba(*index_colors.get(n.name, (0, 0, 1, 1))) y_next += space # Generate next interface points. if next_interface is None: xys_next = np.asarray([[-radius, 0], [radius, 0]]) else: xys_next = next_interface.get_points() xys_next += [0, y_next] if broken: num = 8 n = np.arange(num + 1) jagged = np.c_[2 * (n / num - 0.5) * radius, (n % 2) * space * frac + y_next - space / 2] xys = np.r_[xys_last, jagged[::-1, :] - (0, space * frac)] draw_polyline(ctx, xys) ctx.fill() xys = np.r_[jagged, xys_next[::-1, :]] draw_polyline(ctx, xys) ctx.fill() else: xys = np.r_[xys_last, xys_next[::-1, :]] draw_polyline(ctx, xys) ctx.fill() xys_last = xys_next if next_interface is not None: n = next_interface.n2 ctx.restore()
def draw_point(ctx: Context, point: Point, size): ctx.arc(point.x, point.y, size / 2, 0, 2 * 3.1416) ctx.fill()
def _draw_water_area(self, ctx: Context, outline: Union[Polygon, MultiPolygon]): CairoHelper.draw_polygon(ctx, outline) ctx.fill()
def _draw_fill(self, ctx: Context, geom: Point): CairoHelper.draw_circle(ctx, geom, self.size) ctx.fill()
def draw(self, context: cairo.Context, x: float, y: float, fxy: FixXY): super().draw(context, x, y, fxy) if debug: context.set_source_rgb(1 if self.no_page_break else 0, 0, 0) context.rectangle(*fxy(x, y), *fxy(10, 10)) context.fill()
def paint_background(self, ctx: Context): """ Renders the background of this widget. Is called by Widget.paint. """ ctx.rectangle(0, 0, *self.size) ctx.fill()
base_labels = board_info["variants"]["_base"] for variant_name, variant_info in board_info["variants"].items(): if variant_name.startswith("_"): # skip "_base" continue # create canvas w_p = int(w * (1 + 2 * pad_sides)) canvas = ImageSurface(bg.get_format(), w_p, h) ctx = Context(canvas) ctx.set_source_rgb(1, 1, 1) ctx.rectangle(0, 0, w_p, h) ctx.fill() ctx.set_source_surface(bg, (w_p - w) // 2, -h_bg * y_offset) ctx.rectangle((w_p - w) // 2, 0, w, h_bg) ctx.fill() # make B&W ctx.set_source_rgb(0.5, 0.5, 0.5) ctx.set_operator(OPERATOR_HSL_SATURATION) ctx.rectangle(0, 0, w_p, h) ctx.fill() if True: canvas.write_to_png(f"{board_name}_bg.png") canvas = None
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(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()