def test_layout_clusters_get_max_logical_extent(): surface = SVGSurface(None, 100, 100) cairo_context = Context(surface) layout_1 = pangocairocffi.create_layout(cairo_context) layout_1.set_markup('Text with one line') layout_1_clusters = LayoutClusters(layout_1) extent_1 = layout_1_clusters.get_max_logical_extent() layout_2 = pangocairocffi.create_layout(cairo_context) layout_2.set_markup('text with\ntwo lines') layout_2_clusters = LayoutClusters(layout_2) extent_2 = layout_2_clusters.get_max_logical_extent() assert extent_1.x == 0 assert extent_1.y == 0 assert extent_1.width > 0 assert extent_1.height > 0 assert extent_2.x == extent_1.x assert extent_2.y == extent_1.y assert extent_1.width != extent_2.width assert extent_1.height * 2 == extent_2.height surface.finish()
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 _create_real_surface(self, filename: str, width: int = 100, height: int = 100) -> Tuple[Surface, Context]: surface = SVGSurface('tests/output/text_path_%s' % filename, width, height) cairo_context = Context(surface) return surface, cairo_context
def __init__(self, width, height): from cairocffi import SVGSurface, Context self.plot_id = next(_debug_generator) self._surface = SVGSurface("debug_plot-%05d.svg" % self.plot_id, width, height) self._ctx = Context(self._surface) self._ctx.set_source_rgb(0., 0., 0.) self._ctx.paint()
def test_layout_clusters_properties_have_same_length(): surface = SVGSurface(None, 100, 100) cairo_context = Context(surface) layout = pangocairocffi.create_layout(cairo_context) layout.set_markup('Hi from Παν語\nThis is a test') expected_length = len(layout.get_text()) - 1 # Subtract newline char layout_clusters = LayoutClusters(layout) assert layout_clusters.get_layout() is layout assert len(layout_clusters.get_clusters()) == expected_length assert len(layout_clusters.get_logical_extents()) == expected_length surface.finish()
def get_layout(self, text: str, ctx: Optional[Context] = None, color: Color = Color.WHITE, escape_text=True)\ -> Layout: """ :param text: Text to layout. :param ctx: If None, a dummy context will be created. :param color: Color of the text. :param escape_text: If True, control characters will be macerated. :return: A pango layout object that can be rendered on the screen. """ if ctx is None: ctx = Context(ImageSurface(cairo.FORMAT_RGB16_565, 1, 1)) layout = pangocairo.create_layout(ctx) style = '"italic"' if self.italic else '"normal"' weight = '"bold"' if self.bold else '"normal"' layout.set_markup(f'<span font_family={quoteattr(self.name)} size={quoteattr(str(round(self.size * 1000)))} ' f'foreground={quoteattr(color.to_hex())} style={style} ' f'weight={weight}>{escape(text) if escape_text else text}</span>') return layout
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 _create_void_surface(self) -> Tuple[Surface, Context]: surface = SVGSurface(None, 100, 100) cairo_context = Context(surface) return surface, cairo_context
bg = ImageSurface.create_from_png(board_info["input"]) w, h_bg = bg.get_width(), bg.get_height() h = int(h_bg * (1 - y_offset)) 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()