1
0
Fork 0
mirror of https://git.lynn.is/Gwen/python-layout.git synced 2024-01-13 01:31:55 +01:00
python-layout/pillow_layout/document.py

80 lines
2.7 KiB
Python
Raw Normal View History

2023-01-31 21:46:27 +01:00
from PIL import Image, ImageFont
2023-02-07 23:56:17 +01:00
from . import Layout, ColorMode
from .internal.helpers import max_with_none, min_with_none
2023-01-31 21:46:27 +01:00
class Document(Layout):
def __init__(
self,
content=None,
**kwargs
):
super().__init__(**kwargs)
2023-02-04 15:36:54 +01:00
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
2023-01-31 21:46:27 +01:00
self.complete_init(None)
2023-02-04 15:36:54 +01:00
def children(self):
if self.content is not None:
return [self.content]
2023-01-31 21:46:27 +01:00
else:
return []
def get_ideal_inner_dimensions(self, min_width=None, min_height=None, available_width=None, available_height=None):
2023-02-04 15:36:54 +01:00
if self.content is None:
2023-01-31 21:46:27 +01:00
return 0,0
else:
2023-02-04 15:36:54 +01:00
return self.content.get_ideal_outer_dimensions(min_width, min_height, available_width, available_height)
2023-01-31 21:46:27 +01:00
def get_min_inner_height(self, max_width=None):
2023-02-04 15:36:54 +01:00
if self.content is None:
2023-01-31 21:46:27 +01:00
return 0
else:
2023-02-04 15:36:54 +01:00
return self.content.get_min_outer_height(max_width)
2023-01-31 21:46:27 +01:00
def get_min_inner_width(self, max_height=None):
2023-02-04 15:36:54 +01:00
if self.content is None:
2023-01-31 21:46:27 +01:00
return 0
else:
2023-02-04 15:36:54 +01:00
return self.content.get_min_outer_width(max_height)
2023-01-31 21:46:27 +01:00
def render_content(self, rect):
image = self.make_canvas()
2023-02-04 15:36:54 +01:00
if self.content is not None:
content_image = self.content.render(rect)
2023-01-31 21:46:27 +01:00
image.alpha_composite(content_image)
return image
def get_image(self):
2023-02-04 15:36:54 +01:00
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)
2023-01-31 21:46:27 +01:00
width, height = self.get_ideal_outer_dimensions(min_width=min_width,
min_height=min_height,
available_width=max_width,
available_height=max_height)
2023-02-04 15:36:54 +01:00
if self.width is not None:
width = self.width
2023-01-31 21:46:27 +01:00
else:
2023-02-04 15:36:54 +01:00
width = min_with_none(max_with_none(width, self.min_width), self.max_width)
if self.height is not None:
height = self.height
2023-01-31 21:46:27 +01:00
else:
2023-02-04 15:36:54 +01:00
height = min_with_none(max_with_none(height, self.min_height), self.max_height)
2023-01-31 21:46:27 +01:00
2023-02-04 15:36:54 +01:00
self.actual_size = (width, height)
2023-01-31 21:46:27 +01:00
2023-02-04 15:36:54 +01:00
background = Image.new('RGBA', (width, height), self.bg_color)
2023-01-31 21:46:27 +01:00
content = self.render((0, 0, width - 1, height - 1))
return Image.alpha_composite(background, content)