Import missing functionality from updated megascripts repo.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import textwrap
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from yaml import load, dump, FullLoader
|
from yaml import load, dump, FullLoader
|
||||||
@@ -89,7 +90,8 @@ class Field ():
|
|||||||
def __init__ (self, style_rules, value, card):
|
def __init__ (self, style_rules, value, card):
|
||||||
self.style_rules = style_rules
|
self.style_rules = style_rules
|
||||||
self.card = card
|
self.card = card
|
||||||
self.value = value
|
self.raw_value = value
|
||||||
|
self.value = self.raw_value
|
||||||
self.value = self.__resolve(style_rules.get("formatter", str))
|
self.value = self.__resolve(style_rules.get("formatter", str))
|
||||||
|
|
||||||
def get (self, rule_name, default_value=None):
|
def get (self, rule_name, default_value=None):
|
||||||
@@ -121,7 +123,11 @@ def load_font (name, size):
|
|||||||
|
|
||||||
raise OSError("failed to open font: " + name)
|
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")):
|
if not (field.has("y") and field.has("x")):
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -130,14 +136,29 @@ def draw_text (image, draw, field):
|
|||||||
font_name = field.get("typeface", DEFAULT_FONT)
|
font_name = field.get("typeface", DEFAULT_FONT)
|
||||||
text_anchor = field.get("anchor", None)
|
text_anchor = field.get("anchor", None)
|
||||||
alignment = field.get("align", "left")
|
alignment = field.get("align", "left")
|
||||||
direction = field.get("direction", "ltr")
|
|
||||||
|
|
||||||
font = load_font(font_name, font_size)
|
font = load_font(font_name, font_size)
|
||||||
font_variant = field.get("font_variant")
|
font_variant = field.get("font_variant")
|
||||||
if font_variant:
|
if font_variant:
|
||||||
font.set_variation_by_name(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):
|
def draw_image (image, draw, field):
|
||||||
source_image = images[field.value]
|
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")))
|
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 rescale_text_for_sizes (*args):
|
||||||
def scaler (value):
|
def scaler (value):
|
||||||
for arg in args:
|
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:
|
with open(os.path.join(target_directory, "index.html"), "w") as index_html:
|
||||||
index_html.write("\n".join(card_index))
|
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():
|
def main():
|
||||||
yagicard_root = util.find_nearest(".", lambda name: name == YAGICARDFILE)
|
yagicard_root = util.find_nearest(".", lambda name: name == YAGICARDFILE)
|
||||||
if yagicard_root is None:
|
if yagicard_root is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user