def child_pos(self, style: Style, bounding_box: Rectangle) -> Rectangle: if not self.sizes: return Rectangle() text_align = style.get("text-align", TextAlign.CENTER) vertical_align = style.get("vertical-align", VerticalAlign.BOTTOM) vertical_spacing = style.get("vertical-spacing", 0) # should be margin? ws, hs = list(zip(*self.sizes)) max_w = max(ws) total_h = sum(hs) if text_align == TextAlign.CENTER: x = bounding_box.x + (bounding_box.width - max_w) / 2 elif text_align == TextAlign.LEFT: x = bounding_box.x - max_w - vertical_spacing elif text_align == TextAlign.RIGHT: x = bounding_box.x + bounding_box.width + vertical_spacing if vertical_align == VerticalAlign.BOTTOM: y = bounding_box.y + bounding_box.height + vertical_spacing elif vertical_align == VerticalAlign.MIDDLE: y = bounding_box.y + (bounding_box.height - total_h) / 2 elif vertical_align == VerticalAlign.TOP: y = bounding_box.y - total_h - vertical_spacing return Rectangle( x, y, max_w, total_h, )
def set_font(self, font: Style): font_family = font.get("font-family") font_size = font.get("font-size") font_weight = font.get("font-weight") font_style = font.get("font-style") assert font_family, "Font family should be set" assert font_size, "Font size should be set" font_id = (font_family, font_size, font_weight, font_style) if font_id == self.font_id: return self.font_id = font_id fd = Pango.FontDescription.new() fd.set_family(font_family) fd.set_absolute_size(font_size * Pango.SCALE) if font_weight: assert isinstance(font_weight, FontWeight) fd.set_weight(getattr(Pango.Weight, font_weight.name)) if font_style: assert isinstance(font_style, FontStyle) fd.set_style(getattr(Pango.Style, font_style.name)) self.layout.set_font_description(fd) underline = ( font.get("text-decoration", TextDecoration.NONE) == TextDecoration.UNDERLINE ) if self.underline != underline: self.underline = underline self.update_text()