-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
152 lines (122 loc) · 4.13 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import math
import os
import textwrap
import aiohttp
import shutil
import urllib
from PIL import Image, ImageDraw, ImageFont
import time
from os import listdir, getcwd
from os.path import isfile, join
# Card options
CARD_SCALE = 200
CONTENT_TEXT_SCALE = 100
TITLE_TEXT_SCALE = 40
# Margins (top big text, top small text, sides, bottom)
MARGINS = (150, 50, 50, 50)
# Chars to wrap at
TEXT_WRAP = 16
# Folders for the in-progress cards and built templates
CARDS_DIR = ""
BUILD_DIR = "build"
# Program constants
# Font
CONTENT_TEXT_HEIGHT = math.floor(CONTENT_TEXT_SCALE * 1.2)
TITLE_TEXT_HEIGHT = math.floor(TITLE_TEXT_SCALE * 1.2)
# Other
CARD_SIZE = (CARD_SCALE * 5, CARD_SCALE * 7)
COLOURS = ("white", "black")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
IMG_FORMAT = "jpg"
BASE_URL = "cahbot.edjoduf.co.uk:8081/"
PROTOCOL = "http"
class CardFontConfig:
def __init__(self, cardFont, contentFontSize=CONTENT_TEXT_SCALE, titleFontSize=TITLE_TEXT_SCALE):
self.cardFont = cardFont
self.contentFont = ImageFont.truetype(cardFont, size=contentFontSize)
self.titleFont = ImageFont.truetype(cardFont, size=titleFontSize)
self.contentFontHeight = math.floor(contentFontSize * 1.2)
self.titleFontHeight = math.floor(titleFontSize * 1.2)
def deck_path(decks_dir, guild_id, deck_name):
if type(deck_name) == int:
return join(decks_dir, str(guild_id), str(deck_name))
return join(decks_dir, str(guild_id), str(hash(deck_name)))
def clear_deck_path(decks_dir, guild_id, deck_name):
try:
shutil.rmtree(deck_path(decks_dir, guild_id, deck_name))
except FileNotFoundError:
pass
def card_path(existing_folders, decks_dir, guild_id, deck_name, card_type, num, expansion=None, build=False, root_dir=False):
"Returns deck_path/{CARDS_DIR|BUILD_DIR}/[hash(expansion)]/card_type/card<num>." + IMG_FORMAT
folder = join(deck_path(decks_dir, guild_id, deck_name), (BUILD_DIR if build else CARDS_DIR))
if root_dir:
return join(folder, f"card{num}." + IMG_FORMAT)
if expansion is not None:
folder = join(folder, str(hash(expansion)))
folder = join(folder, card_type)
if not (existing_folders.get(folder) or os.path.isdir(folder)):
os.makedirs(folder)
existing_folders[folder] = True
return join(folder, f"card{num}." + IMG_FORMAT)
def local_file_url(card_path):
return PROTOCOL + "://" + BASE_URL + urllib.parse.quote(card_path.lstrip("/" + os.sep).replace(os.sep, "/"))
def url_to_local_path(url):
return os.path.normpath(url.split(BASE_URL)[1].lstrip("/"))
def make_card(
card_text,
file_name,
fonts,
expansion="",
card_type=COLOURS[0],
show_small=True,
game_name="",
):
if card_type == COLOURS[0]:
text_col = BLACK
back_col = WHITE
else:
text_col = WHITE
back_col = BLACK
# Skip blank lines created by fucked formatting
if card_text in ([""], "", []):
return
if isinstance(card_text, list):
card_text = "".join(card_text)
rawText = card_text
# Wrapping
splitting_text = card_text.split("\\n")
card_text = []
for line in splitting_text:
for newline in textwrap.wrap(line, width=TEXT_WRAP):
card_text.append(newline)
# Initialise image
current_img = Image.new("RGB", CARD_SIZE, color=back_col)
drawn = ImageDraw.Draw(current_img)
# Add the main text
for num, line in enumerate(card_text):
pos = (MARGINS[2], MARGINS[0] + (fonts.contentFontHeight * num))
drawn.text(
pos,
line,
font=fonts.contentFont,
fill=text_col,
)
if show_small is True:
# Add the header text
drawn.text(
(MARGINS[2], MARGINS[1]),
game_name,
font=fonts.titleFont,
fill=text_col,
)
# Add the footer text
drawn.text(
(MARGINS[2], CARD_SIZE[1] - MARGINS[3] - fonts.titleFontHeight),
expansion,
font=fonts.titleFont,
fill=text_col,
)
# Save it
current_img.save(file_name)
current_img.close()