<commit_before>"""
A convenience wrapper around Cairo.
"""

import contextlib

import cairo

from path_tiler import replay_path


class Drawing:
    def __init__(self, width, height, bg=(1, 1, 1)):
        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        self.ctx = cairo.Context(self.surface)

        # Start with a solid-color canvas.
        self.set_source_rgb(*bg)
        self.set_operator(cairo.OPERATOR_SOURCE)
        self.paint()

    def __getattr__(self, name):
        """Use the drawing like a context, or a surface."""
        try:
            return getattr(self.ctx, name)
        except AttributeError:
            return getattr(self.surface, name)

    @contextlib.contextmanager
    def saved(self):
        self.ctx.save()
        try:
            yield
        finally:
            self.ctx.restore()

    def get_size(self):
        return (self.get_width(), self.get_height())

    def multi_stroke(self, paths, styles):
        for width, color in styles:
            self.set_line_width(width)
            for path in paths:
                replay_path(path, self)
                if callable(color):
                    self.set_source_rgb(*color())
                else:
                    self.set_source_rgb(*color)
                self.stroke()<commit_msg>A line-style convenience<commit_after>"""
A convenience wrapper around Cairo.
"""

import contextlib

import cairo

from path_tiler import replay_path


class Drawing:
    def __init__(self, width, height, bg=(1, 1, 1)):
        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        self.ctx = cairo.Context(self.surface)

        # Start with a solid-color canvas.
        self.set_source_rgb(*bg)
        self.set_operator(cairo.OPERATOR_SOURCE)
        self.paint()

    def __getattr__(self, name):
        """Use the drawing like a context, or a surface."""
        try:
            return getattr(self.ctx, name)
        except AttributeError:
            return getattr(self.surface, name)

    @contextlib.contextmanager
    def saved(self):
        self.ctx.save()
        try:
            yield
        finally:
            self.ctx.restore()

    def get_size(self):
        return (self.get_width(), self.get_height())

    def multi_stroke(self, paths, styles):
        for width, color in styles:
            self.set_line_width(width)
            for path in paths:
                replay_path(path, self)
                if callable(color):
                    self.set_source_rgb(*color())
                else:
                    self.set_source_rgb(*color)
                self.stroke()

    @contextlib.contextmanager
    def line_style(self, rgb=None, width=None, dash=None):
        o_source = self.get_source()
        o_width = self.get_line_width()
        o_dash = self.get_dash()
        try:
            if rgb is not None:
                self.set_source_rgb(*rgb)
            if width is not None:
                self.set_line_width(width)
            if dash is not None:
                self.set_dash(dash)
            yield
        finally:
            self.set_source(o_source)
            self.set_line_width(o_width)
            self.set_dash(*o_dash)
{'fraction_non_alphanumeric': 0.09560643564356436, 'fraction_numerical': 0.003094059405940594, 'mean_word_length': 1.8763345195729537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3244520', 'n_tokens_mistral': 961, 'n_tokens_neox': 923, 'n_words': 223}