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 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 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, 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 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 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 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 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 paint(self, ctx: Context): """ Renders this widget. :param ctx: Cairo context. """ if self.background: ctx.set_source_rgba(*self.background) self.paint_background(ctx) ctx.set_source_rgba(*self.foreground) self.paint_foreground(ctx) self.dirty = False
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 render_cluster_glyph_items( ctx: cairocffi.Context, layout: pangocffi.Layout ): """ Renders each cluster within a layout with a unique rotation and color. Warning: Does not support bidirectional text. :param ctx: a Cairo context :param layout: a Pango layout """ layout_run_iter = layout.get_iter() layout_cluster_iter = layout.get_iter() layout_text = layout.get_text() alternate = False while True: layout_run = layout_run_iter.get_run() layout_line_baseline = layout_run_iter.get_baseline() if layout_run is None: if not layout_run_iter.next_run(): break continue clusters = get_clusters_from_glyph_item(layout_run, layout_text) for cluster in clusters: cluster_extents = layout_cluster_iter.get_cluster_extents()[1] layout_cluster_iter.next_cluster() alternate = not alternate ctx.set_source_rgba(0.5, 0, 1 if alternate else 0.5, 0.9) ctx.save() ctx.translate( pangocffi.units_to_double(cluster_extents.x), pangocffi.units_to_double(layout_line_baseline) ) ctx.rotate(-0.05 if alternate else 0.05) pangocairocffi.show_glyph_item( ctx, layout_text, cluster ) ctx.restore() if not layout_run_iter.next_run(): break
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(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_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 set_source_rgb(self, colour: Union[ColorType, List[ColorType]], ctx: cairocffi.Context = None): # If an alternate context is not provided then we draw to the # drawer's default context if ctx is None: ctx = self.ctx if isinstance(colour, list): if len(colour) == 0: # defaults to black ctx.set_source_rgba(*utils.rgb("#000000")) elif len(colour) == 1: ctx.set_source_rgba(*utils.rgb(colour[0])) else: linear = cairocffi.LinearGradient(0.0, 0.0, 0.0, self.height) step_size = 1.0 / (len(colour) - 1) step = 0.0 for c in colour: rgb_col = utils.rgb(c) if len(rgb_col) < 4: rgb_col[3] = 1 linear.add_color_stop_rgba(step, *rgb_col) step += step_size ctx.set_source(linear) else: ctx.set_source_rgba(*utils.rgb(colour))
def render_run_glyph_items( ctx: cairocffi.Context, layout: pangocffi.Layout ) -> None: """ Renders each layout run within a layout with a unique rotation and color. :param ctx: a Cairo context :param layout: a Pango layout """ layout_iter = layout.get_iter() layout_text = layout.get_text() alternate = False while True: layout_run = layout_iter.get_run() layout_run_extents = layout_iter.get_run_extents()[1] layout_line_baseline = layout_iter.get_baseline() if layout_run is None: if not layout_iter.next_run(): break continue alternate = not alternate ctx.set_source_rgba(0, 0.5, 1 if alternate else 0.5, 0.9) ctx.save() ctx.translate( pangocffi.units_to_double(layout_run_extents.x), pangocffi.units_to_double(layout_line_baseline) ) ctx.rotate(0.05 if alternate else -0.05) pangocairocffi.show_glyph_item(ctx, layout_text, layout_run) ctx.restore() if not layout_iter.next_run(): break
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
# SYSTEM: "383431", # } r, g, b = color_table[type][0:2], color_table[type][ 2:4], color_table[type][4:6] color = (int(r, 16) / 255, int(g, 16) / 255, int(b, 16) / 255) mean_pin_y = statistics.mean( [pin_defs[pin_name][1] for pin_name in pin_names]) mean_pin_y = mean_pin_y * h_bg - h_bg * y_offset 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,
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_labels(self, ctx: Context, highways: List[LineString]): # font = cairo.ToyFontFace('CMU Concrete', weight=1) # ctx.set_font_face(font) ctx.set_source_rgba(0, 0, 0, 1) self._draw_iterator(ctx, highways, self._draw_label)
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)
def _show_layout( context: cairocffi.Context, layout: pangocffi.Layout ): context.set_source_rgba(0, 0, 0, 0.9) pangocairocffi.show_layout(context, layout)
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_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 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)