diff --git a/yagicard/__init__.py b/yagicard/__init__.py index 681eaf5..5cb3ea8 100755 --- a/yagicard/__init__.py +++ b/yagicard/__init__.py @@ -1,5 +1,6 @@ import sys import os +import textwrap from collections import OrderedDict from yaml import load, dump, FullLoader @@ -89,7 +90,8 @@ class Field (): def __init__ (self, style_rules, value, card): self.style_rules = style_rules self.card = card - self.value = value + self.raw_value = value + self.value = self.raw_value self.value = self.__resolve(style_rules.get("formatter", str)) def get (self, rule_name, default_value=None): @@ -121,23 +123,42 @@ def load_font (name, size): raise OSError("failed to open font: " + name) -def draw_text (image, draw, field): +def draw_text_array (image, draw, field): + for item in field.raw_value: + draw_text(image, draw, field, item) + +def draw_text (image, draw, field, text=None): if not (field.has("y") and field.has("x")): return - + color = field.get("color", DEFAULT_TEXT_COLOR) font_size = field.get("size", DEFAULT_FONT_SIZE) font_name = field.get("typeface", DEFAULT_FONT) text_anchor = field.get("anchor", None) alignment = field.get("align", "left") - direction = field.get("direction", "ltr") font = load_font(font_name, font_size) font_variant = field.get("font_variant") if font_variant: font.set_variation_by_name(font_variant) - - draw.multiline_text((field.get("x"), field.get("y")), field.value, font=font, fill=color, anchor=text_anchor, align=alignment) #, direction=direction) + + (x, y) = (field.get("x"), field.get("y")) + + if text is None: + text = field.value + + if field.has("width"): + width = field.get("width") + letter_size = font.getsize("x") + text = "\n".join(textwrap.wrap(text, width / letter_size[0])) + + if alignment == "center" and field.has("width"): + width = field.get("width") + text_width = font.getsize_multiline(text)[0] + if text_width < width: + x = x + ((width - text_width) / 2) + + draw.multiline_text((x, y), text, font=font, fill=color, anchor=text_anchor, align=alignment) def draw_image (image, draw, field): source_image = images[field.value] @@ -169,6 +190,11 @@ def draw_symbols (image, draw, field): symbol_drawer.draw_onto(image, (field.get("x"), field.get("y"))) +def draw_image_under (image, draw, field): + original_image = image.copy() + draw_image(image, draw, field) + image.alpha_composite(original_image, (0, 0)) + def rescale_text_for_sizes (*args): def scaler (value): for arg in args: @@ -285,6 +311,32 @@ def make_set (card_set, target_directory, default_frame, frames={}): with open(os.path.join(target_directory, "index.html"), "w") as index_html: index_html.write("\n".join(card_index)) +def make_sheets (set_filename, scale_factor=(1, 1), card_size=None): + if os.path.isfile(set_filename): + set_filename = os.path.splitext(set_filename)[0] + + sheet_size = (2550, 3300) + margins = (50, 50) + sheet = Image.new("RGBA", sheet_size) + (x, y) = margins + + for image_file in os.listdir(set_filename): + if not os.path.splitext(image_file)[1] in [".jpg", ".png"]: + continue + + image = Image.open(os.path.join(set_filename, image_file)) + size = (card_size[0], card_size[1]) if card_size else (image.width, image.height) + image = image.resize((int(size[0] * scale_factor[0]), int(size[1] * scale_factor[1]))) + + sheet.paste(image, (x, y)) + x = x + margins[0] + image.width + + if x + image.width > sheet_size[0]: + x = margins[0] + y = y + margins[1] + image.height + + sheet.save(f"{set_filename}.png") + def main(): yagicard_root = util.find_nearest(".", lambda name: name == YAGICARDFILE) if yagicard_root is None: