vexyl 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,440 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # Gives a flag as plain data, in the canonical form of section 4 of the SDK: a value is left
6
+ # out if, and only if, it equals the format's default, and what depends on other values is
7
+ # worked out.
8
+ module Canonical
9
+ module_function
10
+
11
+ def model(f)
12
+ m = {}
13
+ m["vexyl"] = f.version
14
+ m["meta"] = meta(f.meta)
15
+ m["palette"] = palette(f)
16
+ m["proportion"] = proportion(f.hoist, f.fly)
17
+ m["outline"] = outline(f.outline) if f.outline
18
+ m["border"] = border(f.border, f.border_outside) if f.border
19
+ m["define"] = f.defines.to_h { |name, d| [name, define(d)] } unless f.defines.empty?
20
+ m["layers"] = layers(f.layers) unless f.layers.empty?
21
+ m["reverse"] = { "layers" => layers(f.reverse.layers) } if f.reverse
22
+ m["adapt"] = adapt(f.adapt) if f.adapt
23
+ m["variant"] = f.variants.to_h { |name, v| [name, variant(v)] } unless f.variants.empty?
24
+ m["fold"] = f.folds.to_h { |name, fold| [name, fold(fold)] } unless f.folds.empty?
25
+ deep_freeze(m)
26
+ end
27
+
28
+ # Makes the model's hashes, arrays and strings unchangeable, all the way down.
29
+ def deep_freeze(value)
30
+ case value
31
+ when Hash then value.each_value { |v| deep_freeze(v) }
32
+ when Array then value.each { |v| deep_freeze(v) }
33
+ end
34
+ value.freeze
35
+ end
36
+
37
+ def proportion(hoist, fly)
38
+ { "hoist" => hoist, "fly" => fly }
39
+ end
40
+
41
+ def text(m, key, value)
42
+ m[key] = value if value && !value.empty?
43
+ end
44
+
45
+ # Leaves out a number that equals its default.
46
+ def number(m, key, value, otherwise)
47
+ m[key] = value if value != otherwise
48
+ end
49
+
50
+ def list(m, key, values)
51
+ m[key] = values.dup unless values.empty?
52
+ end
53
+
54
+ def flag(m, key, value)
55
+ m[key] = true if value
56
+ end
57
+
58
+ def point_in(m, key, p)
59
+ m[key] = point(p) if p
60
+ end
61
+
62
+ def point(p)
63
+ { "fly" => p.fly, "hoist" => p.hoist }
64
+ end
65
+
66
+ def numbers(values)
67
+ values.dup
68
+ end
69
+
70
+ def meta(meta)
71
+ m = { "name" => meta.name }
72
+ text(m, "description", meta.description)
73
+ text(m, "version", meta.version)
74
+ text(m, "date", meta.date)
75
+ text(m, "until", meta.until)
76
+ text(m, "series", meta.series)
77
+ list(m, "note", meta.notes)
78
+ m["law"] = references("law", meta.laws) unless meta.laws.empty?
79
+ m["restriction"] = references("restriction", meta.restrictions) unless meta.restrictions.empty?
80
+ list(m, "designer", meta.designers)
81
+ text(m, "design-rights", meta.design_rights)
82
+ list(m, "encoder", meta.encoders)
83
+ text(m, "license", meta.license)
84
+ text(m, "render-license", meta.render_license)
85
+ unless meta.adapted_from.empty?
86
+ m["adapted-from"] = meta.adapted_from.map do |a|
87
+ work = { "url" => a.url }
88
+ list(work, "author", a.authors)
89
+ text(work, "license", a.license)
90
+ text(work, "part", a.part)
91
+ work
92
+ end
93
+ end
94
+ list(m, "source", meta.sources)
95
+ m
96
+ end
97
+
98
+ def references(node, refs)
99
+ refs.map do |r|
100
+ ref = { node => r.text }
101
+ list(ref, "source", r.sources)
102
+ ref
103
+ end
104
+ end
105
+
106
+ def palette(f)
107
+ palette = {}
108
+ f.palette.each do |name, value|
109
+ color = { "color" => value }
110
+ (f.color_refs[name] || []).each do |ref|
111
+ if ref.other
112
+ (color["reference"] ||= []) << { "system" => ref.system, "value" => ref.value }
113
+ elsif ref.system == "cmyk"
114
+ color["cmyk"] = numbers(ref.numbers)
115
+ elsif ref.system == "dye"
116
+ (color["dye"] ||= []) << ref.value
117
+ else
118
+ color[ref.system] = ref.value
119
+ end
120
+ end
121
+ palette[name] = color
122
+ end
123
+ f.gradients.each do |name, g|
124
+ gradient = { "gradient" => g.colors.dup, "stops" => numbers(g.stops) }
125
+ gradient["along"] = g.along if g.along != "fly"
126
+ palette[name] = gradient
127
+ end
128
+ palette
129
+ end
130
+
131
+ def border(e, outside)
132
+ m = edge(e)
133
+ m["side"] = "outside" if outside
134
+ m
135
+ end
136
+
137
+ def edge(e)
138
+ { "color" => e.color, "width" => e.width }
139
+ end
140
+
141
+ def outline(o)
142
+ m = { "form" => o.kind }
143
+ case o.kind
144
+ when "polygon"
145
+ m["point"] = points(o.vertices)
146
+ when "path"
147
+ m["d"] = o.d
148
+ m["box"] = numbers(o.box)
149
+ else
150
+ number(m, "depth", o.depth, 0)
151
+ number(m, "taper", o.taper, 0)
152
+ number(m, "tongue", o.tongue, 0)
153
+ number(m, "tip", o.tip, 0)
154
+ end
155
+ m
156
+ end
157
+
158
+ def points(points)
159
+ points.map { |p| point(p) }
160
+ end
161
+
162
+ def define(d)
163
+ return shape(d.shape) if d.shape
164
+ m = { "layers" => layers(d.layers) }
165
+ m["proportion"] = proportion(d.hoist, d.fly) if d.fly != 0
166
+ m
167
+ end
168
+
169
+ def adapt(a)
170
+ m = {}
171
+ m["range"] = [a.min, a.max] if a.max != 0
172
+ text(m, "wider", a.wider)
173
+ text(m, "narrower", a.narrower)
174
+ m
175
+ end
176
+
177
+ def variant(v)
178
+ m = {}
179
+ m["from"] = v.from if v.from != "none"
180
+ flag(m, "upright", v.upright)
181
+ number(m, "ratio", v.ratio, 0)
182
+ m["proportion"] = proportion(v.hoist, v.fly) if v.fly != 0
183
+ m["outline"] = outline(v.outline) if v.outline
184
+ m["border"] = border(v.border, v.border_outside) if v.border
185
+ m["layers"] = layers(v.layers) unless v.layers.empty?
186
+ m
187
+ end
188
+
189
+ def fold(f)
190
+ m = {}
191
+ text(m, "description", f.description)
192
+ list(m, "source", f.sources)
193
+ m["steps"] = f.steps.map do |s|
194
+ step = { "step" => s.kind }
195
+ if s.through
196
+ step["through"] = numbers(s.through)
197
+ step["move"] = numbers(s.move_to)
198
+ else
199
+ step["along"] = s.along
200
+ step["at"] = s.at
201
+ step["move"] = s.move
202
+ end
203
+ step
204
+ end
205
+ m
206
+ end
207
+
208
+ def layers(layers)
209
+ layers.map { |l| layer(l) }
210
+ end
211
+
212
+ def effects(m, e)
213
+ text(m, "anchor", e.anchor)
214
+ number(m, "opacity", e.opacity, 1)
215
+ m["blend"] = e.blend if e.blend != "normal"
216
+ m["clip"] = shape(e.clip) if e.clip
217
+ m["cut"] = e.cuts.map { |s| shape(s) } unless e.cuts.empty?
218
+ end
219
+
220
+ def along(m, along)
221
+ m["along"] = along if along != "fly"
222
+ end
223
+
224
+ def fimbriate(m, e)
225
+ m["fimbriate"] = edge(e) if e
226
+ end
227
+
228
+ def line(m, l)
229
+ return if l.nil?
230
+ edge = { "form" => l.kind, "depth" => l.depth }
231
+ number(edge, "period", l.period, 0)
232
+ edge["count"] = l.count if l.count != 0
233
+ m["edge"] = edge
234
+ end
235
+
236
+ def layer(l)
237
+ m = {}
238
+ effects(m, l)
239
+ case l
240
+ when Model::Field
241
+ m["layer"] = "field"
242
+ m["color"] = l.color
243
+ when Model::Bars
244
+ m["layer"] = "bars"
245
+ m["colors"] = l.colors.dup
246
+ m["ratio"] = numbers(l.ratio)
247
+ along(m, l.along)
248
+ number(m, "rotate", l.rotate, 0)
249
+ m["grow"] = numbers(l.grow) if l.grow
250
+ line(m, l.line)
251
+ when Model::Chevrons
252
+ m["layer"] = "chevrons"
253
+ m["colors"] = l.colors.dup
254
+ m["ratio"] = numbers(l.ratio)
255
+ m["depth"] = l.depth
256
+ m["from"] = l.from if l.from != "hoist"
257
+ number(m, "angle", l.angle, 0)
258
+ m["apex"] = l.apex unless l.apex.nil?
259
+ number(m, "rotate", l.rotate, 0)
260
+ fimbriate(m, l.fimbriate)
261
+ when Model::Band
262
+ m["layer"] = "band"
263
+ m["color"] = l.color
264
+ m["width"] = l.width
265
+ along(m, l.along)
266
+ number(m, "offset", l.offset, 0)
267
+ number(m, "rotate", l.rotate, 0)
268
+ fimbriate(m, l.fimbriate)
269
+ when Model::Cross
270
+ m["layer"] = "cross"
271
+ m["color"] = l.color
272
+ m["fly"] = numbers(l.fly_parts)
273
+ m["hoist"] = numbers(l.hoist_parts)
274
+ fimbriate(m, l.fimbriate)
275
+ when Model::Saltire
276
+ m["layer"] = "saltire"
277
+ m["color"] = l.color
278
+ m["width"] = l.width
279
+ fimbriate(m, l.fimbriate)
280
+ flag(m, "counterchange", l.counterchange)
281
+ when Model::Quarters
282
+ m["layer"] = "quarters"
283
+ m["colors"] = l.colors.dup
284
+ flag(m, "diagonal", l.diagonal)
285
+ point_in(m, "split", l.split)
286
+ when Model::Checks
287
+ m["layer"] = "checks"
288
+ m["colors"] = l.colors.dup
289
+ m["rows"] = l.rows
290
+ m["columns"] = l.columns
291
+ m["flow"] = l.flow if l.flow != "bend-sinister"
292
+ when Model::Rays
293
+ m["layer"] = "rays"
294
+ m["colors"] = l.colors.dup
295
+ m["ratio"] = numbers(l.ratio)
296
+ if l.at
297
+ m["from"] = point(l.at)
298
+ elsif l.from != "center"
299
+ m["from"] = l.from
300
+ end
301
+ number(m, "rotate", l.rotate, 0)
302
+ m["measure"] = "edge" if l.by_edge
303
+ when Model::Charge
304
+ charge(m, l)
305
+ when Model::Emblem
306
+ m["layer"] = "emblem"
307
+ point_in(m, "at", l.at)
308
+ m["along"] = l.along unless l.along.empty?
309
+ number(m, "rotate", l.rotate, 0)
310
+ m["layers"] = layers(l.charges)
311
+ when Model::Group
312
+ m["layer"] = "group"
313
+ m["layers"] = layers(l.layers)
314
+ when Model::Panel
315
+ m["layer"] = "panel"
316
+ m["layers"] = layers(l.layers)
317
+ if l.canton
318
+ m["layer"] = "canton"
319
+ m["fly"] = l.fly
320
+ m["hoist"] = l.hoist
321
+ else
322
+ text(m, "side", l.side)
323
+ number(m, "width", l.width, 0)
324
+ text(m, "corner", l.corner)
325
+ point_in(m, "at", l.at)
326
+ number(m, "fly", l.fly, 0)
327
+ number(m, "hoist", l.hoist, 0)
328
+ line(m, l.line)
329
+ end
330
+ when Model::Use
331
+ m["layer"] = "use"
332
+ m["name"] = l.name
333
+ end
334
+ m
335
+ end
336
+
337
+ def charge(m, c)
338
+ m.merge!(shape(c.shape))
339
+ m["layer"] = "charge"
340
+ # A pile or pall is a layer of its own, so it has no shape to name.
341
+ if c.shape.kind == "pile" || c.shape.kind == "pall"
342
+ m["layer"] = c.shape.kind
343
+ m.delete("shape")
344
+ end
345
+ text(m, "color", c.color)
346
+ fimbriate(m, c.fimbriate)
347
+ r = c.repeat
348
+ return if r.nil?
349
+ repeat = { "form" => r.kind }
350
+ case r.kind
351
+ when "ring", "arc"
352
+ repeat["count"] = r.count
353
+ repeat["radius"] = r.radius
354
+ flag(repeat, "turn", r.turn)
355
+ if r.kind == "arc"
356
+ repeat["from"] = r.from
357
+ repeat["to"] = r.to
358
+ end
359
+ when "grid"
360
+ repeat["rows"] = r.rows
361
+ repeat["columns"] = r.columns
362
+ flag(repeat, "stagger", r.stagger)
363
+ when "line"
364
+ repeat["count"] = r.count
365
+ number(repeat, "fly", r.fly, 0)
366
+ number(repeat, "hoist", r.hoist, 0)
367
+ end
368
+ m["repeat"] = repeat
369
+ end
370
+
371
+ def shape(s)
372
+ m = { "shape" => s.kind }
373
+ if s.ats.length > 1 || (s.ats.length == 1 && s.ats[0])
374
+ m["at"] = s.ats.map { |p| p.nil? ? "center" : point(p) }
375
+ elsif s.ats.empty? && s.at
376
+ m["at"] = [point(s.at)]
377
+ end
378
+ number(m, "rotate", s.rotate, 0)
379
+ number(m, "hollow", s.hollow, 0)
380
+ case s.kind
381
+ when "disc", "triangle", "cardioid"
382
+ m["radius"] = s.radius
383
+ when "ellipse", "rect", "lozenge"
384
+ number(m, "fly", s.fly, 0)
385
+ number(m, "hoist", s.hoist, 0)
386
+ when "ring"
387
+ m["radius"] = s.radius
388
+ m["inner"] = s.inner
389
+ if s.has_arc
390
+ m["from"] = s.from
391
+ m["to"] = s.to
392
+ end
393
+ when "plus"
394
+ m["radius"] = s.radius
395
+ m["width"] = s.width
396
+ when "star"
397
+ m["radius"] = s.radius
398
+ m["inner"] = s.inner
399
+ m["points"] = s.points if s.points != 5
400
+ number(m, "lines", s.lines, 0)
401
+ when "crescent"
402
+ m["radius"] = s.radius
403
+ m["inner"] = s.inner
404
+ m["offset"] = s.offset
405
+ when "sun"
406
+ m["radius"] = s.radius
407
+ m["disc"] = s.disc
408
+ number(m, "gap", s.gap, 0)
409
+ m["points"] = s.points if s.points != 12
410
+ number(m, "spread", s.spread, 1)
411
+ when "heart"
412
+ m["radius"] = s.radius
413
+ m["spread"] = s.spread
414
+ when "path"
415
+ m["d"] = s.d
416
+ m["box"] = numbers(s.box)
417
+ number(m, "fly", s.fly, 0)
418
+ number(m, "hoist", s.hoist, 0)
419
+ flag(m, "evenodd", s.even_odd)
420
+ when "polygon"
421
+ m["point"] = points(s.vertices)
422
+ when "pile"
423
+ m["from"] = s.edge if s.edge != "hoist"
424
+ m["base"] = numbers(s.base) if s.base
425
+ m["tip"] = numbers(s.tip) if s.tip
426
+ number(m, "depth", s.depth, 0)
427
+ number(m, "flat", s.flat, 0)
428
+ when "pall"
429
+ m["from"] = s.edge if s.edge != "hoist"
430
+ m["width"] = s.width
431
+ number(m, "junction", s.junction, 0)
432
+ when "svg"
433
+ m["fly"] = s.fly
434
+ m["hoist"] = s.hoist
435
+ m["markup"] = s.markup
436
+ end
437
+ m
438
+ end
439
+ end
440
+ end
data/lib/vexyl/cli.rb ADDED
@@ -0,0 +1,251 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # The vexyl command, which checks Vexyl flag documents and renders them to SVG, with the same
6
+ # subcommands and options as the Go command, apart from `info`.
7
+ module CLI
8
+ USAGE = <<~TEXT
9
+ usage:
10
+ vexyl render [options] FILE render a flag to SVG
11
+ vexyl check [-json] FILE... validate flags, with -json listing one file's problems
12
+ vexyl model FILE print a flag's parsed model as JSON
13
+ vexyl tree [options] FILE print a render as a tree of elements, as JSON
14
+ vexyl locate [-line N] FILE print where each layer and part of a document is, as JSON,
15
+ or just what's innermost at a line
16
+ vexyl share encode FILE print a document as a share link's part after its #
17
+ vexyl share decode FILE print the document in a file holding that part
18
+ vexyl version print this command's version and the format version it reads
19
+
20
+ render options:
21
+ -o FILE write to a file instead of standard output
22
+ -height PIXELS the image's height
23
+ -ratio H:F another proportion, by the flag's adapt rules, written hoist
24
+ then fly, such as 1:2
25
+ -variant NAME one of the flag's variants, such as vertical or banner
26
+ -side SIDE obverse or reverse
27
+ -fit FIT error, the default, or pad with transparency if the flag
28
+ can't reach the ratio
29
+ -align WHERE place a padded flag: left, top or start; center; or right,
30
+ bottom or end
31
+ -fold NAME show one of the flag's folds
32
+ -fold-step N stop the fold after N steps
33
+ -id-prefix TEXT start every ID in the output with this
34
+ -tags mark each drawn element with the layer that drew it
35
+
36
+ FILE can be - for standard input.
37
+ TEXT
38
+
39
+ # A failure to report, with the command's usage or without.
40
+ class Failure < StandardError; end
41
+
42
+ DECIMAL = /\A[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/
43
+ INTEGER = /\A[+-]?[0-9]+\z/
44
+
45
+ module_function
46
+
47
+ # Runs the command with its arguments, giving the exit status.
48
+ def main(args, stdin: $stdin, stdout: $stdout, stderr: $stderr)
49
+ stdout.binmode
50
+ run(args, stdin, stdout, stderr)
51
+ stdout.flush
52
+ 0
53
+ rescue Failure, Error, SystemCallError => e
54
+ stdout.flush
55
+ stderr.puts(e.message)
56
+ 1
57
+ end
58
+
59
+ def run(args, stdin, stdout, stderr)
60
+ raise Failure, USAGE if args.empty?
61
+ rest = args[1..]
62
+ case args[0]
63
+ when "render" then render(rest, false, stdin, stdout)
64
+ when "tree" then render(rest, true, stdin, stdout)
65
+ when "check"
66
+ if rest[0] == "-json"
67
+ check_json(rest[1..], stdin, stdout)
68
+ else
69
+ check(rest, stdin, stderr)
70
+ end
71
+ when "model"
72
+ raise Failure, "model needs one file\n#{USAGE}" if rest.length != 1
73
+ stdout.write(Vexyl.parse(read_all(rest[0], stdin)).model_json, "\n")
74
+ when "locate" then locate(rest, stdin, stdout)
75
+ when "share" then share(rest, stdin, stdout)
76
+ when "version", "-version", "--version"
77
+ stdout.write("vexyl #{VERSION}, format #{FORMAT_VERSION}\n")
78
+ when "help", "-h", "--help"
79
+ stdout.write(USAGE)
80
+ else
81
+ raise Failure, "unknown command \"#{args[0]}\"\n#{USAGE}"
82
+ end
83
+ end
84
+
85
+ def read_all(path, stdin)
86
+ path == "-" ? stdin.binmode.read : File.binread(path)
87
+ end
88
+
89
+ # Lists one file's problems for tools, and fails if it has any.
90
+ def check_json(paths, stdin, stdout)
91
+ raise Failure, "check -json needs one file\n#{USAGE}" if paths.length != 1
92
+ problems = Vexyl.validate(read_all(paths[0], stdin))
93
+ list = problems.map do |p|
94
+ item = { "message" => p.message }
95
+ if p.line?
96
+ item["line"] = p.line
97
+ item["column"] = p.column
98
+ end
99
+ item["node"] = p.node if p.node && !p.node.empty?
100
+ item
101
+ end
102
+ stdout.write(Json.write(list), "\n")
103
+ raise Failure, "#{problems.length} problems" unless problems.empty?
104
+ end
105
+
106
+ def check(paths, stdin, stderr)
107
+ raise Failure, "check needs at least one file\n#{USAGE}" if paths.empty?
108
+ failed = 0
109
+ paths.each do |path|
110
+ Vexyl.validate(read_all(path, stdin)).each do |p|
111
+ stderr.puts("#{path}:#{p}")
112
+ failed += 1
113
+ end
114
+ end
115
+ raise Failure, "#{failed} problems" if failed.positive?
116
+ end
117
+
118
+ def locate(args, stdin, stdout)
119
+ line = 0
120
+ files = []
121
+ i = 0
122
+ while i < args.length
123
+ a = args[i]
124
+ if a == "-line" || a == "--line"
125
+ raise Failure, "-line needs a number" if i + 1 >= args.length
126
+ i += 1
127
+ line = parse_int(args[i], "-line")
128
+ elsif a.start_with?("-line=", "--line=")
129
+ line = parse_int(a[(a.index("=") + 1)..], "-line")
130
+ else
131
+ files << a
132
+ end
133
+ i += 1
134
+ end
135
+ raise Failure, "locate needs one file\n#{USAGE}" if files.length != 1
136
+ src = read_all(files[0], stdin).force_encoding(Encoding::UTF_8).scrub
137
+ spans = Vexyl.locate(src)
138
+ result = if line.positive?
139
+ Vexyl.layer_at(spans, line)&.to_h
140
+ else
141
+ spans.map(&:to_h)
142
+ end
143
+ stdout.write(Json.write(result, indent: false), "\n")
144
+ end
145
+
146
+ def share(args, stdin, stdout)
147
+ if args.length != 2 || !%w[encode decode].include?(args[0])
148
+ raise Failure, "share needs encode or decode, and one file\n#{USAGE}"
149
+ end
150
+ src = read_all(args[1], stdin).force_encoding(Encoding::UTF_8)
151
+ if args[0] == "encode"
152
+ stdout.write(Vexyl.share_encode(src.scrub), "\n")
153
+ return
154
+ end
155
+ # Only the line ending a file is dropped, so that other whitespace is still refused.
156
+ stdout.write(Vexyl.share_decode(src.b.sub(/\n+\z/, "")))
157
+ end
158
+
159
+ def parse_int(text, option)
160
+ raise Failure, "#{option} needs a whole number, got \"#{text}\"" unless INTEGER.match?(text)
161
+ Integer(text, 10)
162
+ end
163
+
164
+ def parse_double(text, option)
165
+ raise Failure, "#{option} needs a number, got \"#{text}\"" unless DECIMAL.match?(text)
166
+ Float(text)
167
+ end
168
+
169
+ def render(args, as_tree, stdin, stdout)
170
+ opts = {}
171
+ output = ""
172
+ files = []
173
+ i = 0
174
+ while i < args.length
175
+ a = args[i]
176
+ i += 1
177
+ if !a.start_with?("-") || a == "-"
178
+ files << a
179
+ next
180
+ end
181
+ name = a.sub(/\A--?/, "")
182
+ value = nil
183
+ if (eq = name.index("="))
184
+ value = name[(eq + 1)..]
185
+ name = name[0, eq]
186
+ end
187
+ if name == "tags"
188
+ opts[:tags] = value.nil? || value.casecmp?("true")
189
+ next
190
+ end
191
+ if value.nil?
192
+ raise Failure, "flag needs an argument: -#{name}" if i >= args.length
193
+ value = args[i]
194
+ i += 1
195
+ end
196
+ case name
197
+ when "o" then output = value
198
+ when "height"
199
+ height = parse_double(value, "-height")
200
+ raise Failure, "-height must be positive" if height <= 0
201
+ opts[:height] = height
202
+ when "ratio" then opts[:ratio] = parse_ratio(value)
203
+ when "variant" then opts[:variant] = value
204
+ when "side" then opts[:side] = value
205
+ when "fit" then opts[:fit] = value
206
+ when "align" then opts[:align] = align(value)
207
+ when "fold" then opts[:fold] = value
208
+ when "fold-step" then opts[:fold_step] = parse_int(value, "-fold-step")
209
+ when "id-prefix" then opts[:id_prefix] = value
210
+ else raise Failure, "flag provided but not defined: -#{name}\n#{USAGE}"
211
+ end
212
+ end
213
+ raise Failure, "render needs one file\n#{USAGE}" if files.length != 1
214
+ flag = Vexyl.parse(read_all(files[0], stdin))
215
+ options = RenderOptions.new(**opts)
216
+ text = if as_tree
217
+ "#{Json.write(flag.svg_tree(options).to_h, indent: false)}\n"
218
+ else
219
+ flag.svg(options)
220
+ end
221
+ if output.empty?
222
+ stdout.write(text)
223
+ else
224
+ File.binwrite(output, text)
225
+ end
226
+ end
227
+
228
+ def align(value)
229
+ case value
230
+ when "left", "top", "start" then "start"
231
+ when "center" then "center"
232
+ when "right", "bottom", "end" then "end"
233
+ else raise Failure, "-align must be left, right, top, bottom or center, got \"#{value}\""
234
+ end
235
+ end
236
+
237
+ # Reads hoist:fly, as flag proportions are written, into fly over hoist.
238
+ def parse_ratio(text)
239
+ bad = "-ratio must look like 1:2, hoist then fly, got \"#{text}\""
240
+ colon = text.index(":")
241
+ raise Failure, bad if colon.nil?
242
+ hoist_text = text[0, colon]
243
+ fly_text = text[(colon + 1)..]
244
+ raise Failure, bad unless DECIMAL.match?(hoist_text) && DECIMAL.match?(fly_text)
245
+ hoist = Float(hoist_text)
246
+ fly = Float(fly_text)
247
+ raise Failure, bad if hoist <= 0 || fly <= 0
248
+ fly / hoist
249
+ end
250
+ end
251
+ end