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()
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
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) 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)
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))