Пример #1
0
    def __init__(self, source: ImageSurface) -> None:
        if source is None:
            raise ValueError("Argument 'source' is required.")

        super().__init__()

        self._source = source
        self._size = Dimension(source.get_width(), source.get_height())
Пример #2
0
    def __init__(self, source: BLImage) -> None:
        if source is None:
            raise ValueError("Argument 'source' is required.")

        super().__init__()

        (width, height) = source.size

        self._size = Dimension(width, height)
        self._source = source

        pixels = source.pixels[:]

        channels = 4
        length = width * height

        def load_image() -> Iterable[int]:
            for i in range(0, length):
                offset = i * channels

                r = pixels[offset]
                g = pixels[offset + 1]
                b = pixels[offset + 2]
                a = pixels[offset + 3]

                yield int(b * 255)
                yield int(g * 255)
                yield int(r * 255)
                yield int(a * 255)

        data = bytearray(load_image())

        pattern = ImageSurface.create_for_data(data, FORMAT_ARGB32, width,
                                               height)

        self._surface = ImageSurface(FORMAT_ARGB32, width, height)

        ctx = Graphics(self._surface)

        m = Matrix()
        m.translate(0, height)
        m.scale(1, -1)

        ctx.set_matrix(m)
        ctx.set_source_surface(pattern)
        ctx.paint()

        self.surface.flush()

        pattern.finish()
Пример #3
0
    def create(self, key: str) -> Maybe[TestImage]:
        if key is None:
            raise ValueError("Argument 'key' is required.")

        if not Path(key).exists():
            return Nothing

        source = ImageSurface.create_from_png(key)

        return Some(TestImage(source))
Пример #4
0
 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
Пример #5
0
    def assertImage(self, name: str, context: Context, tolerance: float = 0):
        if name is None:
            raise ValueError("Argument 'name' is required.")

        if context is None:
            raise ValueError("Argument 'context' is required.")

        if tolerance < 0:
            raise ValueError(
                "Argument 'tolerance' should be zero or a positive number.")

        surface = context.surface

        fixture_path = self.fixture_dir / (name + ".png")

        if not fixture_path.exists():
            fixture_path.parent.mkdir(parents=True, exist_ok=True)

            surface.write_to_png(str(fixture_path))

        fixture = ImageSurface.create_from_png(str(fixture_path))

        expected = np.array(fixture.get_data())
        actual = np.array(surface.get_data())

        self.assertEqual(len(expected), len(actual))

        mse = np.sum((expected - actual)**2) / len(expected)

        try:
            self.assertLessEqual(mse, tolerance)

            fixture.finish()
        except AssertionError as e:
            output_path = self.output_dir / (name + ".png")
            output_path.parent.mkdir(parents=True, exist_ok=True)

            surface.write_to_png(str(output_path))

            fixture.finish()

            raise e
Пример #6
0
 def _makeImageSurface(self):
     image = self._image if self._image is not None else self._styleImage
     if image is not None:
         self.imageSurface = ImageSurface.create_from_png(image)
     else:
         self.imageSurface = None
Пример #7
0
            "freq_ratio": [
                ("A0", IN, "input A"),
                ("D5", IN, "input B"),
            ],
        }
    },
}

## CODE

for board_name, board_info in boards.items():
    pad_sides = board_info["pad_sides"]
    pin_defs = board_info["pin_defs"]
    y_offset = board_info["y_offset"]

    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)
Пример #8
0
    def create_surface(self, size: Dimension) -> Surface:
        if size is None:
            raise ValueError("Argument 'size' is required.")

        return ImageSurface(FORMAT_ARGB32, int(size.width), int(size.height))