mirror of
https://git.lynn.is/Gwen/python-layout.git
synced 2024-01-13 01:31:55 +01:00
81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
from PIL import Image, ImageFont
|
|
|
|
from layout.enums import ColorMode
|
|
from layout.internal.helpers import max_with_none, min_with_none
|
|
from layout.layout import Layout
|
|
|
|
|
|
class Document(Layout):
|
|
def __init__(
|
|
self,
|
|
|
|
content=None,
|
|
|
|
**kwargs
|
|
):
|
|
super().__init__(**kwargs)
|
|
if self.overflow is None:
|
|
self.overflow = True
|
|
if self.font is None:
|
|
self.font = ImageFont.load_default()
|
|
self.content = content
|
|
self.actual_size = None
|
|
self.complete_init(None)
|
|
|
|
def children(self):
|
|
if self.content is not None:
|
|
return [self.content]
|
|
else:
|
|
return []
|
|
|
|
def get_ideal_inner_dimensions(self, min_width=None, min_height=None, available_width=None, available_height=None):
|
|
if self.content is None:
|
|
return 0,0
|
|
else:
|
|
return self.content.get_ideal_outer_dimensions(min_width, min_height, available_width, available_height)
|
|
|
|
def get_min_inner_height(self, max_width=None):
|
|
if self.content is None:
|
|
return 0
|
|
else:
|
|
return self.content.get_min_outer_height(max_width)
|
|
|
|
def get_min_inner_width(self, max_height=None):
|
|
if self.content is None:
|
|
return 0
|
|
else:
|
|
return self.content.get_min_outer_width(max_height)
|
|
|
|
def render_content(self, rect):
|
|
image = self.make_canvas()
|
|
if self.content is not None:
|
|
content_image = self.content.render(rect)
|
|
image.alpha_composite(content_image)
|
|
return image
|
|
|
|
def get_image(self):
|
|
min_width = max_with_none(self.width, self.min_width)
|
|
min_height = max_with_none(self.height, self.min_height)
|
|
max_width = min_with_none(self.width, self.max_width)
|
|
max_height = min_with_none(self.height, self.max_height)
|
|
|
|
width, height = self.get_ideal_outer_dimensions(min_width=min_width,
|
|
min_height=min_height,
|
|
available_width=max_width,
|
|
available_height=max_height)
|
|
|
|
if self.width is not None:
|
|
width = self.width
|
|
else:
|
|
width = min_with_none(max_with_none(width, self.min_width), self.max_width)
|
|
if self.height is not None:
|
|
height = self.height
|
|
else:
|
|
height = min_with_none(max_with_none(height, self.min_height), self.max_height)
|
|
|
|
self.actual_size = (width, height)
|
|
|
|
background = Image.new('RGBA', (width, height), self.bg_color)
|
|
content = self.render((0, 0, width - 1, height - 1))
|
|
return Image.alpha_composite(background, content)
|