-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardgenerator.nim
201 lines (179 loc) · 6.83 KB
/
cardgenerator.nim
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
## CardGenerator documentation
## ===========================
##
## This cli program generates playing-cards from the minimal amount of
## resources:
##
## * card background image (refered here as base-card)
## * at least one base font file
## * your illustrations for the cards
##
##
## Directory names
## ===============
##
## Each directory in the resources dir is a seperate colour. You can have as
## many colours as you wish.
##
## **For example:**
##
## The directories `blue`, `red`, `some_other_name` will all be
## interpreted as different colours.
##
## Inside these directories you put your card images (see 'File names' for that)
##
##
## File names
## ==========
##
## Colour-specific files
## ---------------------
##
## These are files inside the colour directories (such as `./blue/`, `./red/`).
## You can put your own illustrations on here, that will be placed onto the
## middle of your finished card.
##
## Simply name them, what you want the card to say at the end.
##
## **For example:**
##
## A file called `Ace.png` will have `Ace` written in each cornor of the card.
## This makes silly stuff like config files for every card obsolete!
##
## "Global" files
## --------------
##
## These include your standard stuff, like the base card, fonts, etc.
##
## File names have to be as is, otherwise the program gets confused, and that
## is not nice towards the program...
##
## **Reserved file names:**
##
## * `base_card.png`
## * `font.ttf` / `font.otf`
##
## Each colours' base card and/or font can be overridden. Just add a
## `.[colour name]` between the identifier (`base_card`) and the file extention
## (`.png`) -> `base_card.red.png`.
##
## This will override the base card for every red card!
##
## The same works with font files -> `font.red.ttf`
##
##
## Detailed documentation on usage
## ===============================
##
## For a more detailed deocumentation on usage, please refer to the [README on github](https://github.com/nirokay/CardGenerator#readme)
import std/[os, parseopt, strutils, strformat, tables]
import cardgeneratorpkg/[globals, resourceparser, imagegenerator]
export globals, resourceparser, imagegenerator
when isMainModule:
# Program information:
const
programName: string = "CardGenerator"
programVersion: string = "1.0.0"
programAuthors: seq[string] = @["nirokay"]
programSource: string = "https://github.com/nirokay/CardGenerator"
# Commandline commands:
var commands: seq[tuple[nameShort, nameLong, desc: string, call: proc(_: string) {.closure.}]]
proc newCommand(names: tuple[short: char, long: string], desc: string, call: proc(_: string)) =
commands.add((
nameShort: $names.short,
nameLong: names.long,
desc: desc,
call: call
))
newCommand(('h', "help"), "Displays this help message and quits.", proc(_: string) =
var cmdText: seq[string]
for command in commands:
cmdText.add(alignLeft(&"-{command.nameShort}, --{command.nameLong}", 24, ' ') & command.desc)
echo:
&"{programName} - v{programVersion} by " & programAuthors.join(", ") & "\n" &
&"Source: {programSource}\n\n" &
cmdText.join("\n")
quit(0)
)
newCommand(('v', "version"), "Displays the program version and quits.", proc(_: string) =
echo &"{programName} - v{programVersion}"
quit(0)
)
newCommand(('d', "directory"), "Sets the resource directory.", proc(newPath: string) =
if not newPath.dirExists():
echo &"Resource directory '{newPath}' does not exist!"
quit(1)
resourcesDirectoryPath = newPath
)
newCommand(('o', "output"), "Sets the output directory for final cards.", proc(newOutputPath: string) =
if not newOutputPath.dirExists():
try:
newOutputPath.createDir()
except OSError:
echo &"Could not create directory '{newOutputPath}'!"
quit(1)
imageOutputDirectory = newOutputPath
)
newCommand(('f', "fontsize"), "Sets the global font size for card images.", proc(newSize: string) =
try:
globalFontSize = newSize.parseInt()
except ValueError:
echo "Font size has to be an integer!"
quit(1)
)
newCommand(('z', "safezone"), "Sets the pixels-indents from the corners.", proc(newSafezone: string) =
try:
globalSafezone = newSafezone.parseInt()
except ValueError:
echo "Safezone has to be an integer!"
quit(1)
)
newCommand(('s', "shadow"), "Toggles if the text should have shadows (slow).", proc(_: string) =
drawShadowOnTextLayer = true
)
newCommand(('c', "shadowcolour"), "Sets the shadow colour. Four values (rgba) seperated by commas (example: '255,255,255,255').", proc(values: string) =
var
valuesAsStrings: seq[string] = values.split(',')
populatedValues: seq[string]
# Populate sequence, if needed:
case valuesAsStrings.len():
of 0:
echo "Assumimg shadow colour: opaque black"
populatedValues = @["0", "0", "0", "255"]
of 1:
echo "Assumimg shadow colour: *opaque grey-scale*"
populatedValues = @[valuesAsStrings[0], valuesAsStrings[0], valuesAsStrings[0], "255"]
of 2:
echo "Assumimg shadow colour: *grey-scale*"
populatedValues = @[valuesAsStrings[0], valuesAsStrings[0], valuesAsStrings[0], valuesAsStrings[1]]
of 3:
echo "Assumimg shadow colour: *opaque colour*"
populatedValues = @[valuesAsStrings[0], valuesAsStrings[1], valuesAsStrings[2], "255"]
else:
populatedValues = @[valuesAsStrings[0], valuesAsStrings[1], valuesAsStrings[2], valuesAsStrings[3]]
# Parses colour values to byte:
for i, value in populatedValues:
var shadowInt: uint8
try:
shadowInt = uint8 value.parseInt()
except ValueError:
echo &"\tPassed shadow value of '{value}' is not a valid unsigned 8-bit integer! Using '0' instead."
shadowInt = 0
finally:
shadowColours[i] = shadowInt
echo "Using rgba(" & shadowColours.join(", ") & ") as shadow colour!"
)
# Parse and interpret commandline arguments:
var p = initOptParser(commandLineParams())
for kind, key, value in p.getopt():
case kind:
of cmdEnd: break
of cmdArgument: discard
of cmdLongOption, cmdShortOption:
for cmd in commands:
if key == cmd.nameShort or key == cmd.nameLong: cmd.call(value)
# Parse resources:
parseResourceDirectory()
let cardData: Table[string, CardColourData] = parseColourDirectories()
# Generate Images:
generateImagesWith(cardData)