def draw(self, context: Context): text_path_glyph_items = self._compute_text_path_glyph_items() for text_path_glyph_item in text_path_glyph_items: glyph_position = text_path_glyph_item.position glyph_rotation = text_path_glyph_item.rotation context.save() context.translate(glyph_position.x, glyph_position.y) context.rotate(glyph_rotation) show_glyph_item(context, self._layout_text, text_path_glyph_item.glyph_item) context.restore()
def draw(self, ctx: Context, surface: Surface): ctx.save() ctx.translate(self.translate_x, self.translate_y) ctx.rotate(self.rotation) ctx.scale(self.width / self.svg_width, self.height / self.svg_height) ctx.translate(-self.origin_x, -self.origin_y) CairoSVGSurface(self.tree, None, 96, output_cairo=surface, output_cairo_context=ctx) ctx.restore()
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 transform( ctx: cairo.Context, point: Tuple[float, float], rotation: float = None, scale_x=1.0, scale_y=1.0, ): ctx.translate(point[0], point[1]) if rotation is not None: ctx.rotate(math.radians(rotation)) if scale_x != 1.0 or scale_y != 1.0: ctx.scale(sx=scale_x, sy=scale_y) ctx.translate(-point[0], -point[1])
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 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