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.
- checksums.yaml +7 -0
- data/LICENSE +202 -0
- data/NOTICE +8 -0
- data/README.md +72 -0
- data/exe/vexyl +8 -0
- data/lib/vexyl/adapter.rb +451 -0
- data/lib/vexyl/canonical.rb +440 -0
- data/lib/vexyl/cli.rb +251 -0
- data/lib/vexyl/element.rb +81 -0
- data/lib/vexyl/errors.rb +47 -0
- data/lib/vexyl/flag.rb +66 -0
- data/lib/vexyl/geometry.rb +253 -0
- data/lib/vexyl/json.rb +75 -0
- data/lib/vexyl/kdl.rb +709 -0
- data/lib/vexyl/limits.rb +120 -0
- data/lib/vexyl/locate.rb +133 -0
- data/lib/vexyl/model.rb +568 -0
- data/lib/vexyl/numbers.rb +151 -0
- data/lib/vexyl/parser.rb +1902 -0
- data/lib/vexyl/render.rb +1036 -0
- data/lib/vexyl/render_options.rb +63 -0
- data/lib/vexyl/sanitizer.rb +401 -0
- data/lib/vexyl/shapes.rb +953 -0
- data/lib/vexyl/share.rb +119 -0
- data/lib/vexyl/share_dictionary.rb +124 -0
- data/lib/vexyl/version.rb +7 -0
- data/lib/vexyl/xml.rb +570 -0
- data/lib/vexyl.rb +101 -0
- metadata +71 -0
data/lib/vexyl/render.rb
ADDED
|
@@ -0,0 +1,1036 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
|
|
3
|
+
|
|
4
|
+
require "digest"
|
|
5
|
+
require "strscan"
|
|
6
|
+
|
|
7
|
+
module Vexyl
|
|
8
|
+
# Renders a flag as SVG, as section 6 of the format paints it.
|
|
9
|
+
class Render
|
|
10
|
+
LOCAL_ID = /\A[A-Za-z_][A-Za-z0-9_.:-]*\z/
|
|
11
|
+
# Parser::NO_COLOR, written out so this file doesn't depend on load order.
|
|
12
|
+
NONE = "none"
|
|
13
|
+
|
|
14
|
+
# How a flag's back turns in a variant, so that it's the mirror image of the turned front.
|
|
15
|
+
REVERSE_TURN = {
|
|
16
|
+
"none" => "none", "mirror" => "mirror", "half-turn" => "half-turn", "invert" => "invert",
|
|
17
|
+
"transpose" => "anti-transpose", "anti-transpose" => "transpose",
|
|
18
|
+
"rotate" => "counter-rotate", "counter-rotate" => "rotate"
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
SIDEWAYS = %w[rotate counter-rotate transpose anti-transpose].freeze
|
|
22
|
+
|
|
23
|
+
# A flag with a side, a variant and a proportion applied, and how it's then shown.
|
|
24
|
+
# canvas: the proportion to pad the result to, or 0.
|
|
25
|
+
class Resolved
|
|
26
|
+
attr_accessor :flag, :turn, :upright, :mirror, :canvas
|
|
27
|
+
|
|
28
|
+
def initialize
|
|
29
|
+
@flag = nil
|
|
30
|
+
@turn = "none"
|
|
31
|
+
@upright = false
|
|
32
|
+
@mirror = false
|
|
33
|
+
@canvas = 0.0
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
attr_accessor :tagging, :scope_layers, :scope_border, :upright, :id_prefix
|
|
38
|
+
attr_reader :out
|
|
39
|
+
|
|
40
|
+
def initialize(flag, id_prefix)
|
|
41
|
+
@flag = flag
|
|
42
|
+
@out = +""
|
|
43
|
+
@depth = 0
|
|
44
|
+
@next_id = 0
|
|
45
|
+
@gradients = {}
|
|
46
|
+
# The transform that undoes a turned variant's rotation.
|
|
47
|
+
@upright = ""
|
|
48
|
+
@id_prefix = id_prefix
|
|
49
|
+
# Marks each drawn element with the path of the layer that drew it.
|
|
50
|
+
@tagging = false
|
|
51
|
+
@scope_layers = ""
|
|
52
|
+
@scope_border = ""
|
|
53
|
+
# The path of the layer being drawn, and how many use layers it's inside.
|
|
54
|
+
@tag = ""
|
|
55
|
+
@using = 0
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
class << self
|
|
59
|
+
def svg(original, opts)
|
|
60
|
+
write(original, opts)
|
|
61
|
+
rescue Shapes::Refusal => e
|
|
62
|
+
raise Error, e.message
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Reads a render back as a tree of elements. Only a title or desc holds any text.
|
|
66
|
+
def tree(svg)
|
|
67
|
+
TreeReader.new(svg).element
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# How a variant turns a flag, as a transform and what undoes it for charges.
|
|
71
|
+
def view(kind, x0, y0, width, tall)
|
|
72
|
+
num = Shapes.method(:num)
|
|
73
|
+
case kind
|
|
74
|
+
when "transpose"
|
|
75
|
+
["matrix(0 1 1 0 0 0)", "matrix(0 1 1 0 0 0)"]
|
|
76
|
+
when "rotate"
|
|
77
|
+
["matrix(0 1 -1 0 #{num.(2 * y0 + tall)} 0)", "matrix(0 -1 1 0 0 0)"]
|
|
78
|
+
when "anti-transpose"
|
|
79
|
+
["matrix(0 -1 -1 0 #{num.(2 * y0 + tall)} #{num.(2 * x0 + width)})", "matrix(0 -1 -1 0 0 0)"]
|
|
80
|
+
when "counter-rotate"
|
|
81
|
+
["matrix(0 -1 1 0 0 #{num.(2 * x0 + width)})", "matrix(0 1 -1 0 0 0)"]
|
|
82
|
+
when "mirror"
|
|
83
|
+
["matrix(-1 0 0 1 #{num.(2 * x0 + width)} 0)", "matrix(-1 0 0 1 0 0)"]
|
|
84
|
+
when "half-turn"
|
|
85
|
+
["matrix(-1 0 0 -1 #{num.(2 * x0 + width)} #{num.(2 * y0 + tall)})", "matrix(-1 0 0 -1 0 0)"]
|
|
86
|
+
when "invert"
|
|
87
|
+
["matrix(1 0 0 -1 0 #{num.(2 * y0 + tall)})", "matrix(1 0 0 -1 0 0)"]
|
|
88
|
+
else
|
|
89
|
+
["", ""]
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Where each copy of a charge goes, and how far it turns with its repeat, in painting order.
|
|
94
|
+
def charge_places(c, s, reg)
|
|
95
|
+
places = []
|
|
96
|
+
at = Shapes.position(s, reg)
|
|
97
|
+
r = c.repeat
|
|
98
|
+
if s.ats.size > 1
|
|
99
|
+
s.ats.each do |p|
|
|
100
|
+
places << [p.nil? ? reg.center : Geometry::Vec.new(p.fly, p.hoist), 0.0]
|
|
101
|
+
end
|
|
102
|
+
elsif r.nil?
|
|
103
|
+
places << [at, 0.0]
|
|
104
|
+
elsif r.kind == "ring"
|
|
105
|
+
r.count.times do |i|
|
|
106
|
+
a = i * 360.0 / r.count
|
|
107
|
+
places << [at.add(Geometry::Vec.new(0.0, -r.radius).rotate(a)), r.turn ? a : 0.0]
|
|
108
|
+
end
|
|
109
|
+
elsif r.kind == "arc"
|
|
110
|
+
# Clockwise from one to the other, as a ring's own arc goes.
|
|
111
|
+
span = (r.to - r.from).remainder(360)
|
|
112
|
+
span += 360 if span < 0
|
|
113
|
+
r.count.times do |i|
|
|
114
|
+
a = r.from + span * i / (r.count - 1)
|
|
115
|
+
places << [at.add(Geometry::Vec.new(0.0, -r.radius).rotate(a)), r.turn ? a : 0.0]
|
|
116
|
+
end
|
|
117
|
+
elsif r.kind == "line"
|
|
118
|
+
r.count.times do |i|
|
|
119
|
+
places << [at.add(Geometry::Vec.new(r.fly, r.hoist).mul(i.to_f)), 0.0]
|
|
120
|
+
end
|
|
121
|
+
else
|
|
122
|
+
(1..r.rows).each do |row|
|
|
123
|
+
(1..r.columns).each do |col|
|
|
124
|
+
next if r.stagger && (row + col).odd?
|
|
125
|
+
places << [Geometry::Vec.new(reg.w * col / (r.columns + 1), reg.h * row / (r.rows + 1)), 0.0]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
places
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
private
|
|
133
|
+
|
|
134
|
+
# Refuses what would otherwise be silently ignored, or can't be drawn.
|
|
135
|
+
def check_options(f, opts)
|
|
136
|
+
if !opts.height.nil? && (opts.height <= 0 || !Limits.in_limits(opts.height))
|
|
137
|
+
raise Error, "height must be a positive number of a reasonable size"
|
|
138
|
+
end
|
|
139
|
+
if !opts.ratio.nil? && (opts.ratio <= 0 || !Limits.in_limits(opts.ratio))
|
|
140
|
+
raise Error, "ratio must be a positive number of a reasonable size"
|
|
141
|
+
end
|
|
142
|
+
if !opts.id_prefix.empty? && !LOCAL_ID.match?(opts.id_prefix)
|
|
143
|
+
raise Error, "idPrefix #{Parser.quote(opts.id_prefix)} isn't what an ID can start with"
|
|
144
|
+
end
|
|
145
|
+
has_ratio = !opts.ratio.nil?
|
|
146
|
+
unless opts.variant.empty?
|
|
147
|
+
v = f.variants[opts.variant]
|
|
148
|
+
has_ratio = true if v && v.ratio != 0
|
|
149
|
+
end
|
|
150
|
+
if !opts.fit.empty? && !has_ratio
|
|
151
|
+
raise Error, "fit says what to do about a ratio, and there isn't one"
|
|
152
|
+
end
|
|
153
|
+
if !opts.align.empty? && opts.fit != "pad"
|
|
154
|
+
raise Error, "align places a padded flag, so it needs a fit of pad"
|
|
155
|
+
end
|
|
156
|
+
if opts.fold.empty?
|
|
157
|
+
raise Error, "foldStep needs a fold" unless opts.fold_step.nil?
|
|
158
|
+
return
|
|
159
|
+
end
|
|
160
|
+
if opts.side == "reverse"
|
|
161
|
+
raise Error, "a fold shows both sides, so it can't be asked for the reverse"
|
|
162
|
+
end
|
|
163
|
+
raise Error, "a fold is cropped to its pieces, so it can't be padded" if opts.fit == "pad"
|
|
164
|
+
fold = f.folds[opts.fold]
|
|
165
|
+
if fold && !opts.fold_step.nil? && (opts.fold_step < 1 || opts.fold_step > fold.steps.size)
|
|
166
|
+
raise Error, "foldStep must be from 1 to #{fold.steps.size}"
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def write(original, opts)
|
|
171
|
+
check_options(original, opts)
|
|
172
|
+
height = opts.height.nil? ? RenderOptions::DEFAULT_HEIGHT : opts.height
|
|
173
|
+
res = resolve(original, opts)
|
|
174
|
+
f = res.flag
|
|
175
|
+
r = new(f, original.id_prefix)
|
|
176
|
+
if opts.tags
|
|
177
|
+
r.tagging = true
|
|
178
|
+
r.scope_layers, r.scope_border = tag_scope(original, opts)
|
|
179
|
+
end
|
|
180
|
+
if !opts.id_prefix.empty?
|
|
181
|
+
r.id_prefix = opts.id_prefix
|
|
182
|
+
elsif !opts.ratio.nil? || !opts.variant.empty? || !opts.side.empty? || !opts.fit.empty? ||
|
|
183
|
+
!opts.align.empty? || !opts.fold.empty? || !opts.fold_step.nil?
|
|
184
|
+
# A page can show several forms of one flag, which mustn't share definitions.
|
|
185
|
+
key = "#{Json.number(opts.ratio.nil? ? 0.0 : opts.ratio)}|#{opts.variant}|#{opts.side}|" \
|
|
186
|
+
"#{opts.fit}|#{opts.align}|#{opts.fold}|#{opts.fold_step.nil? ? 0 : opts.fold_step}"
|
|
187
|
+
r.id_prefix = "#{r.id_prefix}#{Digest::SHA256.digest(key).byteslice(0, 2).unpack1('H*')}-"
|
|
188
|
+
end
|
|
189
|
+
unless opts.fold.empty?
|
|
190
|
+
fold = f.folds[opts.fold]
|
|
191
|
+
raise Error, "#{f.meta.name} has no fold #{Parser.quote(opts.fold)}" if fold.nil?
|
|
192
|
+
raise Error, "a fold can't be applied to a turned variant" if res.turn != "none"
|
|
193
|
+
r.write_folded(f, fold, opts, height)
|
|
194
|
+
return r.out
|
|
195
|
+
end
|
|
196
|
+
x0 = 0.0
|
|
197
|
+
y0 = 0.0
|
|
198
|
+
width = f.fly
|
|
199
|
+
tall = f.hoist
|
|
200
|
+
if f.border && f.border_outside
|
|
201
|
+
b = Shapes.bounds(Geometry.offset_polygon(Geometry.boundary(f), f.border.width))
|
|
202
|
+
x0, y0, width, tall = b
|
|
203
|
+
end
|
|
204
|
+
shown = res.turn
|
|
205
|
+
# Seen from behind, the turned flag is the mirror image of its front.
|
|
206
|
+
shown = REVERSE_TURN[res.turn] if opts.side == "reverse"
|
|
207
|
+
transform, undo = view(shown, x0, y0, width, tall)
|
|
208
|
+
if res.mirror
|
|
209
|
+
# The back is the mirror image of the front, then turned.
|
|
210
|
+
transform = "#{transform} matrix(-1 0 0 1 #{Shapes.num(2 * x0 + width)} 0)".strip
|
|
211
|
+
# A charge is put back the way it is on the back, which is the front's turn undone.
|
|
212
|
+
undo = view(res.turn, x0, y0, width, tall)[1]
|
|
213
|
+
end
|
|
214
|
+
vx = x0
|
|
215
|
+
vy = y0
|
|
216
|
+
vw = width
|
|
217
|
+
vh = tall
|
|
218
|
+
sideways = SIDEWAYS.include?(res.turn)
|
|
219
|
+
if sideways
|
|
220
|
+
vx = y0
|
|
221
|
+
vy = x0
|
|
222
|
+
vw = tall
|
|
223
|
+
vh = width
|
|
224
|
+
end
|
|
225
|
+
r.upright = undo if res.upright
|
|
226
|
+
if res.canvas != 0
|
|
227
|
+
# The canvas ratio is the design's, so a turned flag's is the other way up.
|
|
228
|
+
want = sideways ? 1 / res.canvas : res.canvas
|
|
229
|
+
share = opts.align == "start" ? 0.0 : opts.align == "end" ? 1.0 : 0.5
|
|
230
|
+
if vw / vh < want
|
|
231
|
+
extra = vh * want - vw
|
|
232
|
+
vx -= extra * share
|
|
233
|
+
vw += extra
|
|
234
|
+
else
|
|
235
|
+
extra = vw / want - vh
|
|
236
|
+
vy -= extra * share
|
|
237
|
+
vh += extra
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
r.write_flag(f, vx, vy, vw, vh, height, transform)
|
|
241
|
+
r.out
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# What starts the paths of a render's layers, and of its border.
|
|
245
|
+
def tag_scope(f, opts)
|
|
246
|
+
v = f.variants[opts.variant]
|
|
247
|
+
if opts.variant.empty? || v.nil?
|
|
248
|
+
return ["reverse/", ""] if opts.side == "reverse" && f.reverse
|
|
249
|
+
return ["", ""]
|
|
250
|
+
end
|
|
251
|
+
# Locate.encodeName
|
|
252
|
+
scope = "variant/#{opts.variant.gsub('%', '%25').gsub('/', '%2F')}/"
|
|
253
|
+
layers = ""
|
|
254
|
+
border = ""
|
|
255
|
+
if !v.layers.empty?
|
|
256
|
+
layers = scope
|
|
257
|
+
elsif opts.side == "reverse" && f.reverse
|
|
258
|
+
layers = "reverse/"
|
|
259
|
+
end
|
|
260
|
+
border = scope if v.border
|
|
261
|
+
[layers, border]
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Applies a side, a variant and a proportion to the flag.
|
|
265
|
+
def resolve(f, opts)
|
|
266
|
+
res = Resolved.new
|
|
267
|
+
out = f.copy
|
|
268
|
+
ratio = opts.ratio.nil? ? 0.0 : opts.ratio
|
|
269
|
+
variant = nil
|
|
270
|
+
unless opts.variant.empty?
|
|
271
|
+
variant = f.variants[opts.variant]
|
|
272
|
+
raise Error, "#{f.meta.name} has no variant #{Parser.quote(opts.variant)}" if variant.nil?
|
|
273
|
+
end
|
|
274
|
+
case opts.fit
|
|
275
|
+
when "", "error", "pad"
|
|
276
|
+
else raise Error, "unknown fit #{Parser.quote(opts.fit)}"
|
|
277
|
+
end
|
|
278
|
+
case opts.align
|
|
279
|
+
when "", "start", "center", "end"
|
|
280
|
+
else raise Error, "unknown align #{Parser.quote(opts.align)}"
|
|
281
|
+
end
|
|
282
|
+
case opts.side
|
|
283
|
+
when "", "obverse"
|
|
284
|
+
when "reverse"
|
|
285
|
+
if f.reverse
|
|
286
|
+
out.layers = f.reverse.layers
|
|
287
|
+
out.seen_from_behind = true
|
|
288
|
+
else
|
|
289
|
+
res.mirror = true
|
|
290
|
+
end
|
|
291
|
+
else
|
|
292
|
+
raise Error, "unknown side #{Parser.quote(opts.side)}"
|
|
293
|
+
end
|
|
294
|
+
if variant
|
|
295
|
+
res.turn = variant.from
|
|
296
|
+
res.upright = variant.upright
|
|
297
|
+
# The flag's outline and border were drawn for its own shape.
|
|
298
|
+
if variant.fly > 0
|
|
299
|
+
out.fly = variant.fly
|
|
300
|
+
out.hoist = variant.hoist
|
|
301
|
+
out.outline = nil
|
|
302
|
+
out.border = nil
|
|
303
|
+
out.border_outside = false
|
|
304
|
+
end
|
|
305
|
+
unless variant.layers.empty?
|
|
306
|
+
# Its own design has nothing to do with the flag's reverse, so it's mirrored.
|
|
307
|
+
out.layers = variant.layers
|
|
308
|
+
out.reverse = nil
|
|
309
|
+
out.seen_from_behind = false
|
|
310
|
+
res.mirror = opts.side == "reverse"
|
|
311
|
+
end
|
|
312
|
+
out.outline = variant.outline if variant.outline
|
|
313
|
+
if variant.border
|
|
314
|
+
out.border = variant.border
|
|
315
|
+
out.border_outside = variant.border_outside
|
|
316
|
+
end
|
|
317
|
+
ratio = variant.ratio if ratio == 0
|
|
318
|
+
end
|
|
319
|
+
res.flag = out
|
|
320
|
+
return res if ratio == 0
|
|
321
|
+
if opts.fit == "pad"
|
|
322
|
+
adapted =
|
|
323
|
+
begin
|
|
324
|
+
Adapter.with_ratio(out, Adapter.nearest_ratio(out, ratio))
|
|
325
|
+
rescue Error
|
|
326
|
+
# The rules allow that proportion, but this design can't reach it.
|
|
327
|
+
out
|
|
328
|
+
end
|
|
329
|
+
res.flag = adapted
|
|
330
|
+
res.canvas = ratio
|
|
331
|
+
return res
|
|
332
|
+
end
|
|
333
|
+
adapted = Adapter.with_ratio(out, ratio)
|
|
334
|
+
if !opts.fold.empty? && out.reverse
|
|
335
|
+
# A fold shows the back as well, which adapts as it's seen from behind.
|
|
336
|
+
back = out.copy
|
|
337
|
+
back.layers = out.reverse.layers
|
|
338
|
+
back.seen_from_behind = true
|
|
339
|
+
adapted_back = Adapter.with_ratio(back, ratio)
|
|
340
|
+
adapted = out.copy if adapted.equal?(out)
|
|
341
|
+
reverse = Model::Reverse.new
|
|
342
|
+
reverse.layers = adapted_back.layers
|
|
343
|
+
adapted.reverse = reverse
|
|
344
|
+
end
|
|
345
|
+
res.flag = adapted
|
|
346
|
+
res
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
# Writes the svg element around the flag's art, for an unfolded render.
|
|
351
|
+
def write_flag(f, vx, vy, vw, vh, height, transform)
|
|
352
|
+
line("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"#{num(vx)} #{num(vy)} #{num(vw)} #{num(vh)}" \
|
|
353
|
+
"\" width=\"#{num(height * vw / vh)}\" height=\"#{num(height)}\">")
|
|
354
|
+
@depth += 1
|
|
355
|
+
line("<title>#{Xml.escape(f.meta.name)}</title>")
|
|
356
|
+
line("<desc>#{Xml.escape(f.meta.description)}</desc>") unless f.meta.description.empty?
|
|
357
|
+
unless transform.empty?
|
|
358
|
+
line("<g transform=\"#{transform}\">")
|
|
359
|
+
@depth += 1
|
|
360
|
+
end
|
|
361
|
+
flag_art(f)
|
|
362
|
+
unless transform.empty?
|
|
363
|
+
@depth -= 1
|
|
364
|
+
line("</g>")
|
|
365
|
+
end
|
|
366
|
+
@depth -= 1
|
|
367
|
+
line("</svg>")
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# Renders a flag in a folded position.
|
|
371
|
+
def write_folded(f, fold, opts, height)
|
|
372
|
+
shape = Geometry.fold_shape(f)
|
|
373
|
+
steps = opts.fold_step.nil? ? 0 : opts.fold_step
|
|
374
|
+
stack = Geometry.fold_facets(shape, fold, steps)
|
|
375
|
+
all = []
|
|
376
|
+
stack.each { |facet| all.concat(facet.poly) }
|
|
377
|
+
x, y, w, h = Shapes.bounds(all)
|
|
378
|
+
line("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"#{num(x)} #{num(y)} #{num(w)} #{num(h)}" \
|
|
379
|
+
"\" width=\"#{num(height * w / h)}\" height=\"#{num(height)}\">")
|
|
380
|
+
@depth += 1
|
|
381
|
+
line("<title>#{Xml.escape(f.meta.name)}</title>")
|
|
382
|
+
line("<defs>")
|
|
383
|
+
@depth += 1
|
|
384
|
+
front = id("front")
|
|
385
|
+
line("<g id=\"#{front}\">")
|
|
386
|
+
@depth += 1
|
|
387
|
+
flag_art(f)
|
|
388
|
+
@depth -= 1
|
|
389
|
+
line("</g>")
|
|
390
|
+
back = ""
|
|
391
|
+
if f.reverse
|
|
392
|
+
back = id("back")
|
|
393
|
+
reverse = f.copy
|
|
394
|
+
reverse.layers = f.reverse.layers
|
|
395
|
+
line("<g id=\"#{back}\">")
|
|
396
|
+
@depth += 1
|
|
397
|
+
scope = @scope_layers
|
|
398
|
+
@scope_layers = "reverse/"
|
|
399
|
+
was = @flag
|
|
400
|
+
@flag = reverse
|
|
401
|
+
flag_art(reverse)
|
|
402
|
+
@flag = was
|
|
403
|
+
@scope_layers = scope
|
|
404
|
+
@depth -= 1
|
|
405
|
+
line("</g>")
|
|
406
|
+
end
|
|
407
|
+
@depth -= 1
|
|
408
|
+
line("</defs>")
|
|
409
|
+
# A crease line reads at any scale, without hiding the design. SPEC section 13 fixes it.
|
|
410
|
+
crease = "fill=\"none\" stroke=\"#000\" stroke-opacity=\"0.35\" stroke-linejoin=\"round\" stroke-width=\"" \
|
|
411
|
+
"#{num([w, h].max * 0.004)}\""
|
|
412
|
+
stack.each do |fc|
|
|
413
|
+
clip = id("facet")
|
|
414
|
+
line("<clipPath id=\"#{clip}\">")
|
|
415
|
+
@depth += 1
|
|
416
|
+
unmarked(Shapes::Geom.poly(fc.poly), "")
|
|
417
|
+
@depth -= 1
|
|
418
|
+
line("</clipPath>")
|
|
419
|
+
art = front
|
|
420
|
+
place = fc.place
|
|
421
|
+
if fc.flipped && !back.empty?
|
|
422
|
+
# A separate back is written as seen from that side, so it mirrors back.
|
|
423
|
+
art = back
|
|
424
|
+
place = Geometry::Matrix.new(-1.0, 0.0, 0.0, 1.0, f.fly, 0.0).followed_by(fc.place)
|
|
425
|
+
end
|
|
426
|
+
line("<g clip-path=\"url(##{clip})\"><use href=\"##{art}\" transform=\"matrix(#{num(place.a)} #{num(place.b)} " \
|
|
427
|
+
"#{num(place.c)} #{num(place.d)} #{num(place.e)} #{num(place.f)})\"/></g>")
|
|
428
|
+
unmarked(Shapes::Geom.poly(fc.poly), crease)
|
|
429
|
+
end
|
|
430
|
+
@depth -= 1
|
|
431
|
+
line("</svg>")
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
private
|
|
435
|
+
|
|
436
|
+
def num(v)
|
|
437
|
+
Shapes.num(v)
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
def line(text)
|
|
441
|
+
@out << (" " * @depth) << text << "\n"
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def color(name)
|
|
445
|
+
v = @flag.palette[name]
|
|
446
|
+
v.nil? ? "" : v.downcase
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
# The paint value for a palette name. A gradient spans the box given, or the bounding box of
|
|
450
|
+
# each shape it's used on if that's nil.
|
|
451
|
+
def fill(name, box)
|
|
452
|
+
g = @flag.gradients[name]
|
|
453
|
+
return color(name) if g.nil?
|
|
454
|
+
if box.nil? && @gradients.key?(name)
|
|
455
|
+
return "url(##{@gradients[name]})"
|
|
456
|
+
end
|
|
457
|
+
gid = id("gradient")
|
|
458
|
+
x1 = 0
|
|
459
|
+
y1 = 0
|
|
460
|
+
x2 = 1
|
|
461
|
+
y2 = 0
|
|
462
|
+
case g.along
|
|
463
|
+
when "hoist"
|
|
464
|
+
x2 = 0
|
|
465
|
+
y2 = 1
|
|
466
|
+
when "bend"
|
|
467
|
+
y2 = 1
|
|
468
|
+
when "bend-sinister"
|
|
469
|
+
y1 = 1
|
|
470
|
+
end
|
|
471
|
+
units = ""
|
|
472
|
+
if box
|
|
473
|
+
units = " gradientUnits=\"userSpaceOnUse\" gradientTransform=\"translate(#{num(box[0])} #{num(box[1])}" \
|
|
474
|
+
") scale(#{num(box[2])} #{num(box[3])})\""
|
|
475
|
+
end
|
|
476
|
+
line("<linearGradient id=\"#{gid}\" x1=\"#{x1}\" y1=\"#{y1}\" x2=\"#{x2}\" y2=\"#{y2}\"#{units}>")
|
|
477
|
+
@depth += 1
|
|
478
|
+
g.colors.each_with_index do |c, i|
|
|
479
|
+
line("<stop offset=\"#{num(g.stops[i])}\" stop-color=\"#{color(c)}\"/>")
|
|
480
|
+
end
|
|
481
|
+
@depth -= 1
|
|
482
|
+
line("</linearGradient>")
|
|
483
|
+
@gradients[name] = gid if box.nil?
|
|
484
|
+
"url(##{gid})"
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def id(kind)
|
|
488
|
+
@next_id += 1
|
|
489
|
+
"#{@id_prefix}#{kind}#{@next_id}"
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
# Paints a flag's outside border, then everything within its outline.
|
|
493
|
+
def flag_art(f)
|
|
494
|
+
if f.border && f.border_outside
|
|
495
|
+
outer = Geometry.offset_polygon(Geometry.boundary(f), f.border.width)
|
|
496
|
+
@tag = "#{@scope_border}border"
|
|
497
|
+
# A gradient spans the shape that the border edges.
|
|
498
|
+
box = nil
|
|
499
|
+
shape = Geometry.boundary(f)
|
|
500
|
+
box = Shapes.bounds(shape) if shape
|
|
501
|
+
geom(Shapes::Geom.poly(outer), "fill=\"#{fill(f.border.color, box)}\"")
|
|
502
|
+
end
|
|
503
|
+
root = Shapes::Region.new(f.fly, f.hoist)
|
|
504
|
+
shape = Shapes::Geom.poly(Geometry.rect_poly(0.0, 0.0, f.fly, f.hoist))
|
|
505
|
+
shape = Shapes.outline_geom(f.outline, f.fly, f.hoist) if f.outline
|
|
506
|
+
flag_clip = id("flag")
|
|
507
|
+
line("<clipPath id=\"#{flag_clip}\">")
|
|
508
|
+
@depth += 1
|
|
509
|
+
unmarked(shape, "")
|
|
510
|
+
@depth -= 1
|
|
511
|
+
line("</clipPath>")
|
|
512
|
+
line("<g clip-path=\"url(##{flag_clip})\">")
|
|
513
|
+
@depth += 1
|
|
514
|
+
layers(f.layers, root, @scope_layers)
|
|
515
|
+
if f.border && !f.border_outside
|
|
516
|
+
# Half of the stroke falls outside the outline, and the clip removes it.
|
|
517
|
+
@tag = "#{@scope_border}border"
|
|
518
|
+
geom(shape, "fill=\"none\" stroke=\"#{fill(f.border.color, nil)}\" stroke-width=\"#{num(2 * f.border.width)}" \
|
|
519
|
+
"\" stroke-linejoin=\"miter\" stroke-miterlimit=\"10\"")
|
|
520
|
+
end
|
|
521
|
+
@depth -= 1
|
|
522
|
+
line("</g>")
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
# Paints layers, whose paths start with base: a scope, or a parent's path and a dot.
|
|
526
|
+
def layers(list, reg, base)
|
|
527
|
+
list.each_with_index do |l, i|
|
|
528
|
+
layer(l, reg, "#{base}#{i}")
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
def layer(l, reg, path)
|
|
533
|
+
@tag = path if @using.zero?
|
|
534
|
+
attrs = []
|
|
535
|
+
style = []
|
|
536
|
+
effects(l, reg, cut_moves(l, reg), attrs, style)
|
|
537
|
+
# A group, and any layer with an opacity, a blend, a clip or a cut, composites as a unit,
|
|
538
|
+
# so a blend inside it mixes only with what else is inside. Saying so outright keeps every
|
|
539
|
+
# renderer to that. A panel's own group already says it.
|
|
540
|
+
if l.is_a?(Model::Group) || l.opacity != 1 || l.blend != "normal" || l.clip || !l.cuts.empty?
|
|
541
|
+
style << "isolation:isolate"
|
|
542
|
+
end
|
|
543
|
+
attrs << "style=\"#{style.join(';')}\"" unless style.empty?
|
|
544
|
+
unless attrs.empty?
|
|
545
|
+
line("<g #{attrs.join(' ')}>")
|
|
546
|
+
@depth += 1
|
|
547
|
+
end
|
|
548
|
+
content(l, reg, path)
|
|
549
|
+
return if attrs.empty?
|
|
550
|
+
@depth -= 1
|
|
551
|
+
line("</g>")
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
# Writes any clip and mask a layer needs. A charge's cuts are drawn once for each of its moves.
|
|
555
|
+
def effects(e, reg, moves, attrs, style)
|
|
556
|
+
attrs << "opacity=\"#{num(e.opacity)}\"" if e.opacity != 1
|
|
557
|
+
style << "mix-blend-mode:#{e.blend}" if e.blend != "normal"
|
|
558
|
+
if e.clip
|
|
559
|
+
cid = id("clip")
|
|
560
|
+
line("<clipPath id=\"#{cid}\">")
|
|
561
|
+
@depth += 1
|
|
562
|
+
clip = Shapes.for_region(e.clip, reg)
|
|
563
|
+
unmarked(Shapes.shape_geom(clip, Shapes.position(clip, reg), 0.0), "")
|
|
564
|
+
@depth -= 1
|
|
565
|
+
line("</clipPath>")
|
|
566
|
+
attrs << "clip-path=\"url(##{cid})\""
|
|
567
|
+
end
|
|
568
|
+
return if e.cuts.empty?
|
|
569
|
+
mid = id("mask")
|
|
570
|
+
b = reg.bounds
|
|
571
|
+
line("<mask id=\"#{mid}\" maskUnits=\"userSpaceOnUse\" x=\"#{num(b[0])}\" y=\"#{num(b[1])}" \
|
|
572
|
+
"\" width=\"#{num(b[2])}\" height=\"#{num(b[3])}\">")
|
|
573
|
+
@depth += 1
|
|
574
|
+
unmarked(Shapes::Geom.poly(Geometry.rect_poly(b[0], b[1], b[0] + b[2], b[1] + b[3])), "fill=\"#fff\"")
|
|
575
|
+
moves = [""] if moves.nil?
|
|
576
|
+
moves.each do |move|
|
|
577
|
+
e.cuts.each do |s|
|
|
578
|
+
cut = Shapes.for_region(s, reg)
|
|
579
|
+
g = Shapes.shape_geom(cut, Shapes.position(cut, reg), 0.0)
|
|
580
|
+
g.transform = "#{move} #{g.transform}".strip unless move.empty?
|
|
581
|
+
unmarked(g, "fill=\"#000\"")
|
|
582
|
+
end
|
|
583
|
+
end
|
|
584
|
+
@depth -= 1
|
|
585
|
+
line("</mask>")
|
|
586
|
+
attrs << "mask=\"url(##{mid})\""
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
def box_of(inside)
|
|
590
|
+
inside.empty? ? nil : Shapes.bounds(inside)
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
def gradient?(name)
|
|
594
|
+
@flag.gradients.key?(name)
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
def poly_geoms(*polys)
|
|
598
|
+
polys.map { |p| Shapes::Geom.poly(p) }
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
def content(l, reg, path)
|
|
602
|
+
case l
|
|
603
|
+
when Model::Field
|
|
604
|
+
paint(l.color, nil, nil, nil, poly_geoms(Geometry.rect_poly(0.0, 0.0, reg.w, reg.h)))
|
|
605
|
+
when Model::Bars
|
|
606
|
+
has_gap = l.colors.include?(NONE)
|
|
607
|
+
colors = l.colors
|
|
608
|
+
polys = Shapes.bar_polys(l, reg, ->(i) { gradient?(colors[i]) || has_gap })
|
|
609
|
+
polys.each_with_index do |p, i|
|
|
610
|
+
paint(colors[i], nil, nil, nil, poly_geoms(p)) unless p.empty?
|
|
611
|
+
end
|
|
612
|
+
when Model::Chevrons
|
|
613
|
+
nested, bands = Shapes.raw_chevrons(l, reg)
|
|
614
|
+
polys = Shapes.chevron_polys(l, reg)
|
|
615
|
+
# A gap has to show what's beneath, so each band stands alone.
|
|
616
|
+
polys = bands if l.colors.include?(NONE)
|
|
617
|
+
if l.fimbriate
|
|
618
|
+
# A gradient spans the outermost chevron's part inside the region.
|
|
619
|
+
box = box_of(Shapes.clip_convex(nested[0], reg))
|
|
620
|
+
paint(l.colors[polys.size - 1], box, l.fimbriate, poly_geoms(nested[0]), nil)
|
|
621
|
+
end
|
|
622
|
+
polys.each_with_index do |p, i|
|
|
623
|
+
next if p.empty?
|
|
624
|
+
# A gradient spans the whole chevron's part inside the region, even where a gap
|
|
625
|
+
# leaves only its band to paint.
|
|
626
|
+
box = box_of(Shapes.clip_convex(nested[i], reg))
|
|
627
|
+
paint(l.colors[polys.size - 1 - i], box, nil, nil, poly_geoms(p))
|
|
628
|
+
end
|
|
629
|
+
when Model::Band
|
|
630
|
+
# The fimbriation's stroke mustn't follow the region's edge, so it uses the band's
|
|
631
|
+
# unclipped shape. A clipped polygon can be empty.
|
|
632
|
+
stroke = poly_geoms(Shapes.placed_band_poly(reg, l.fixed, l.along, l.rotate, l.width, l.offset, true))
|
|
633
|
+
poly = Shapes.placed_band_poly(reg, l.fixed, l.along, l.rotate, l.width, l.offset, false)
|
|
634
|
+
if poly.empty?
|
|
635
|
+
paint(l.color, nil, l.fimbriate, stroke, nil)
|
|
636
|
+
else
|
|
637
|
+
# A gradient spans the part inside the region, for the fimbriation too.
|
|
638
|
+
paint(l.color, Shapes.bounds(poly), l.fimbriate, stroke, poly_geoms(poly))
|
|
639
|
+
end
|
|
640
|
+
when Model::Cross
|
|
641
|
+
# A gradient spans a cross as one shape, so it's given the whole region.
|
|
642
|
+
box = [0.0, 0.0, reg.w, reg.h]
|
|
643
|
+
stroke = nil
|
|
644
|
+
if l.fimbriate
|
|
645
|
+
# Keep the arms' ends, and their stroke, beyond the region's edge.
|
|
646
|
+
sp = Shapes.cross_polys(l, reg, l.fimbriate.width * 2)
|
|
647
|
+
stroke = poly_geoms(sp[0], sp[1])
|
|
648
|
+
end
|
|
649
|
+
polys = Shapes.cross_polys(l, reg, 0.0)
|
|
650
|
+
paint(l.color, box, l.fimbriate, stroke, poly_geoms(polys[0], polys[1]))
|
|
651
|
+
when Model::Quarters
|
|
652
|
+
quarters(l, reg)
|
|
653
|
+
when Model::Checks
|
|
654
|
+
checks(l, reg)
|
|
655
|
+
when Model::Rays
|
|
656
|
+
polys = Shapes.ray_polys(l, reg)
|
|
657
|
+
unless l.colors.include?(NONE)
|
|
658
|
+
# Beneath the wedges, so that antialiasing can't leave seams between them.
|
|
659
|
+
paint(l.colors[0], nil, nil, nil, poly_geoms(Geometry.rect_poly(0.0, 0.0, reg.w, reg.h)))
|
|
660
|
+
end
|
|
661
|
+
polys.each_with_index do |p, i|
|
|
662
|
+
paint(l.colors[i], nil, nil, nil, poly_geoms(p)) unless p.empty?
|
|
663
|
+
end
|
|
664
|
+
when Model::Saltire
|
|
665
|
+
if l.counterchange
|
|
666
|
+
counterchange(l, reg)
|
|
667
|
+
return
|
|
668
|
+
end
|
|
669
|
+
box = [0.0, 0.0, reg.w, reg.h]
|
|
670
|
+
stroke = nil
|
|
671
|
+
if l.fimbriate
|
|
672
|
+
stroke = poly_geoms(Shapes.band_poly(reg, "bend", 0.0, l.width, 0.0, true),
|
|
673
|
+
Shapes.band_poly(reg, "bend-sinister", 0.0, l.width, 0.0, true))
|
|
674
|
+
end
|
|
675
|
+
paint(l.color, box, l.fimbriate, stroke,
|
|
676
|
+
poly_geoms(Shapes.band_poly(reg, "bend", 0.0, l.width, 0.0, false),
|
|
677
|
+
Shapes.band_poly(reg, "bend-sinister", 0.0, l.width, 0.0, false)))
|
|
678
|
+
when Model::Charge
|
|
679
|
+
charge(l, reg)
|
|
680
|
+
when Model::Emblem
|
|
681
|
+
emblem(l, reg, path)
|
|
682
|
+
when Model::Group
|
|
683
|
+
layers(l.layers, reg, "#{path}.")
|
|
684
|
+
when Model::Panel
|
|
685
|
+
panel(l, reg, path)
|
|
686
|
+
when Model::Use
|
|
687
|
+
# Everything a use draws is marked with its path, since it's the use in the document.
|
|
688
|
+
@using += 1
|
|
689
|
+
begin
|
|
690
|
+
if l.design.fly == 0
|
|
691
|
+
layers(l.design.layers, reg, "")
|
|
692
|
+
else
|
|
693
|
+
# A design with its own grid is scaled to fill the region it's used in.
|
|
694
|
+
line("<g transform=\"scale(#{Shapes.factor(reg.w / l.design.fly)} #{Shapes.factor(reg.h / l.design.hoist)})\">")
|
|
695
|
+
@depth += 1
|
|
696
|
+
layers(l.design.layers, Shapes::Region.new(l.design.fly, l.design.hoist), "")
|
|
697
|
+
@depth -= 1
|
|
698
|
+
line("</g>")
|
|
699
|
+
end
|
|
700
|
+
ensure
|
|
701
|
+
@using -= 1
|
|
702
|
+
end
|
|
703
|
+
end
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
def panel(p, reg, path)
|
|
707
|
+
x, y, w, h = Shapes.panel_rect(p, reg)
|
|
708
|
+
shape = Shapes::Geom.poly(Geometry.rect_poly(0.0, 0.0, w, h))
|
|
709
|
+
shape = Shapes::Geom.poly(Shapes.panel_edge(p, w, h)) if p.line
|
|
710
|
+
pid = id("panel")
|
|
711
|
+
line("<clipPath id=\"#{pid}\">")
|
|
712
|
+
@depth += 1
|
|
713
|
+
unmarked(shape, "")
|
|
714
|
+
@depth -= 1
|
|
715
|
+
line("</clipPath>")
|
|
716
|
+
line("<g transform=\"translate(#{num(x)} #{num(y)})\" clip-path=\"url(##{pid})\" style=\"isolation:isolate\">")
|
|
717
|
+
@depth += 1
|
|
718
|
+
layers(p.layers, Shapes::Region.new(w, h), "#{path}.")
|
|
719
|
+
@depth -= 1
|
|
720
|
+
line("</g>")
|
|
721
|
+
end
|
|
722
|
+
|
|
723
|
+
def quarters(q, reg)
|
|
724
|
+
c = reg.center
|
|
725
|
+
if q.diagonal
|
|
726
|
+
corners = reg.corners
|
|
727
|
+
4.times do |i|
|
|
728
|
+
paint(q.colors[i], nil, nil, nil, poly_geoms([c, corners[i], corners[(i + 1) % 4]]))
|
|
729
|
+
end
|
|
730
|
+
return
|
|
731
|
+
end
|
|
732
|
+
c = Geometry::Vec.new(q.split.fly, q.split.hoist) if q.split
|
|
733
|
+
# Each runs on beneath the next, so antialiasing can't leave a seam, unless gaps have to
|
|
734
|
+
# show or a gradient has to span its own quarter.
|
|
735
|
+
gaps = q.colors.include?(NONE)
|
|
736
|
+
exact = q.colors.map { |name| gaps || gradient?(name) }
|
|
737
|
+
right = reg.w
|
|
738
|
+
bottom = reg.h
|
|
739
|
+
if exact[0]
|
|
740
|
+
right = c.x
|
|
741
|
+
bottom = c.y
|
|
742
|
+
end
|
|
743
|
+
paint(q.colors[0], nil, nil, nil, poly_geoms(Geometry.rect_poly(0.0, 0.0, right, bottom)))
|
|
744
|
+
bottom = exact[1] ? c.y : reg.h
|
|
745
|
+
paint(q.colors[1], nil, nil, nil, poly_geoms(Geometry.rect_poly(c.x, 0.0, reg.w, bottom)))
|
|
746
|
+
right = exact[2] ? c.x : reg.w
|
|
747
|
+
paint(q.colors[2], nil, nil, nil, poly_geoms(Geometry.rect_poly(0.0, c.y, right, reg.h)))
|
|
748
|
+
paint(q.colors[3], nil, nil, nil, poly_geoms(Geometry.rect_poly(c.x, c.y, reg.w, reg.h)))
|
|
749
|
+
end
|
|
750
|
+
|
|
751
|
+
def checks(ch, reg)
|
|
752
|
+
n = ch.colors.size
|
|
753
|
+
cell_w = reg.w / ch.columns
|
|
754
|
+
cell_h = reg.h / ch.rows
|
|
755
|
+
gaps = ch.colors.include?(NONE)
|
|
756
|
+
bend = ch.flow == "bend"
|
|
757
|
+
ch.rows.times do |row|
|
|
758
|
+
ch.columns.times do |col|
|
|
759
|
+
i = row + col
|
|
760
|
+
i = col - row + ch.rows * n if bend
|
|
761
|
+
x = col * cell_w
|
|
762
|
+
y = row * cell_h
|
|
763
|
+
# Each cell runs on beneath the next ones, unless gaps have to show or a gradient has
|
|
764
|
+
# to span its own cell.
|
|
765
|
+
right = reg.w
|
|
766
|
+
bottom = reg.h
|
|
767
|
+
color = ch.colors[i % n]
|
|
768
|
+
if gaps || gradient?(color)
|
|
769
|
+
right = x + cell_w
|
|
770
|
+
bottom = y + cell_h
|
|
771
|
+
end
|
|
772
|
+
paint(color, nil, nil, nil, poly_geoms(Geometry.rect_poly(x, y, right, bottom)))
|
|
773
|
+
end
|
|
774
|
+
end
|
|
775
|
+
end
|
|
776
|
+
|
|
777
|
+
# Paints a saltire twice its width, clipped to four triangles, so one side of each diagonal shows.
|
|
778
|
+
def counterchange(l, reg)
|
|
779
|
+
c = reg.center
|
|
780
|
+
vec = Geometry::Vec
|
|
781
|
+
cid = id("clip")
|
|
782
|
+
line("<clipPath id=\"#{cid}\">")
|
|
783
|
+
@depth += 1
|
|
784
|
+
unmarked(Shapes::Geom.poly([
|
|
785
|
+
c, vec.new(reg.w, c.y), vec.new(reg.w, reg.h),
|
|
786
|
+
c, vec.new(c.x, reg.h), vec.new(0.0, reg.h),
|
|
787
|
+
c, vec.new(0.0, c.y), vec.new(0.0, 0.0),
|
|
788
|
+
c, vec.new(c.x, 0.0), vec.new(reg.w, 0.0)
|
|
789
|
+
]), "")
|
|
790
|
+
@depth -= 1
|
|
791
|
+
line("</clipPath>")
|
|
792
|
+
line("<g clip-path=\"url(##{cid})\">")
|
|
793
|
+
@depth += 1
|
|
794
|
+
box = [0.0, 0.0, reg.w, reg.h]
|
|
795
|
+
paint(l.color, box, nil, nil,
|
|
796
|
+
poly_geoms(Shapes.band_poly(reg, "bend", 0.0, 2 * l.width, 0.0, false),
|
|
797
|
+
Shapes.band_poly(reg, "bend-sinister", 0.0, 2 * l.width, 0.0, false)))
|
|
798
|
+
@depth -= 1
|
|
799
|
+
line("</g>")
|
|
800
|
+
end
|
|
801
|
+
|
|
802
|
+
def emblem(e, reg, path)
|
|
803
|
+
at = e.at ? Geometry::Vec.new(e.at.fly, e.at.hoist) : reg.center
|
|
804
|
+
angle = e.rotate
|
|
805
|
+
unless e.along.empty?
|
|
806
|
+
dir = Shapes.axis(reg, e.along, 0.0)[0]
|
|
807
|
+
angle += Math.atan2(dir.y, dir.x) * 180 / Math::PI
|
|
808
|
+
end
|
|
809
|
+
# Painting is still clipped to the region, so the frame only has to reach the region's
|
|
810
|
+
# farthest corner.
|
|
811
|
+
reach = 0.0
|
|
812
|
+
reg.corners.each do |corner|
|
|
813
|
+
reach = [reach, Math.hypot(corner.x - at.x, corner.y - at.y)].max
|
|
814
|
+
end
|
|
815
|
+
keep_upright(at) do
|
|
816
|
+
line("<g transform=\"translate(#{num(at.x)} #{num(at.y)}) rotate(#{num(angle)})\">")
|
|
817
|
+
@depth += 1
|
|
818
|
+
# Charges in an emblem turn with it, so they aren't each kept upright as well.
|
|
819
|
+
was = @upright
|
|
820
|
+
@upright = ""
|
|
821
|
+
e.charges.each_with_index do |c, i|
|
|
822
|
+
layer(c, Shapes::Region.new(0.0, 0.0, reach), "#{path}.#{i}")
|
|
823
|
+
end
|
|
824
|
+
@upright = was
|
|
825
|
+
@depth -= 1
|
|
826
|
+
line("</g>")
|
|
827
|
+
end
|
|
828
|
+
end
|
|
829
|
+
|
|
830
|
+
def charge(c, reg)
|
|
831
|
+
s = Shapes.for_region(c.shape, reg)
|
|
832
|
+
at = Shapes.position(s, reg)
|
|
833
|
+
if s.kind == "svg"
|
|
834
|
+
keep_upright(at) { embedded(c, at) }
|
|
835
|
+
return
|
|
836
|
+
end
|
|
837
|
+
copies = []
|
|
838
|
+
centers = []
|
|
839
|
+
self.class.charge_places(c, s, reg).each do |place, turn|
|
|
840
|
+
copies << Shapes.shape_geom(s, place, turn)
|
|
841
|
+
centers << place
|
|
842
|
+
end
|
|
843
|
+
box = nil
|
|
844
|
+
if c.shape.kind == "pile" || c.shape.kind == "pall"
|
|
845
|
+
# Its shape runs on past the region, and a gradient spans only what's inside.
|
|
846
|
+
points = s.vertices.map { |p| Geometry::Vec.new(p.fly, p.hoist) }
|
|
847
|
+
box = box_of(Shapes.clip_convex(points, reg))
|
|
848
|
+
end
|
|
849
|
+
draw = lambda do |shapes, fimbriate, color|
|
|
850
|
+
if s.hollow > 0
|
|
851
|
+
outline_only(color, box, s.hollow, shapes)
|
|
852
|
+
elsif s.lines > 0
|
|
853
|
+
outline_only(color, box, s.lines, shapes)
|
|
854
|
+
else
|
|
855
|
+
paint(color, box, fimbriate, shapes, shapes)
|
|
856
|
+
end
|
|
857
|
+
end
|
|
858
|
+
if @upright.empty? || s.kind == "polygon"
|
|
859
|
+
draw.call(copies, c.fimbriate, c.color)
|
|
860
|
+
return
|
|
861
|
+
end
|
|
862
|
+
if c.fimbriate
|
|
863
|
+
# Every copy's fimbriation lies beneath all of their fills, as it does unturned.
|
|
864
|
+
copies.each_with_index do |g, i|
|
|
865
|
+
keep_upright(centers[i]) { paint(NONE, nil, c.fimbriate, [g], nil) }
|
|
866
|
+
end
|
|
867
|
+
end
|
|
868
|
+
copies.each_with_index do |g, i|
|
|
869
|
+
keep_upright(centers[i]) { draw.call([g], nil, c.color) }
|
|
870
|
+
end
|
|
871
|
+
end
|
|
872
|
+
|
|
873
|
+
# A transform for each copy of a charge, which takes its cuts from where they're written, for
|
|
874
|
+
# the first copy, to that copy. A layer that isn't a charge with several copies gets nil.
|
|
875
|
+
def cut_moves(l, reg)
|
|
876
|
+
return nil unless l.is_a?(Model::Charge)
|
|
877
|
+
return nil if l.cuts.empty? || l.shape.kind == "svg"
|
|
878
|
+
places = self.class.charge_places(l, Shapes.for_region(l.shape, reg), reg)
|
|
879
|
+
return nil if places.size < 2
|
|
880
|
+
first_at, first_turn = places[0]
|
|
881
|
+
moves = [""]
|
|
882
|
+
places.drop(1).each do |at, t|
|
|
883
|
+
turn = t - first_turn
|
|
884
|
+
moves << if turn != 0
|
|
885
|
+
"translate(#{num(at.x)} #{num(at.y)}) rotate(#{num(turn)}) translate(" \
|
|
886
|
+
"#{num(-first_at.x)} #{num(-first_at.y)})"
|
|
887
|
+
else
|
|
888
|
+
"translate(#{num(at.x - first_at.x)} #{num(at.y - first_at.y)})"
|
|
889
|
+
end
|
|
890
|
+
end
|
|
891
|
+
moves
|
|
892
|
+
end
|
|
893
|
+
|
|
894
|
+
# Undoes a turned variant's rotation around a point, if there is one.
|
|
895
|
+
def keep_upright(at)
|
|
896
|
+
if @upright.empty?
|
|
897
|
+
yield
|
|
898
|
+
return
|
|
899
|
+
end
|
|
900
|
+
line("<g transform=\"translate(#{num(at.x)} #{num(at.y)}) #{@upright} translate(#{num(-at.x)} #{num(-at.y)})\">")
|
|
901
|
+
@depth += 1
|
|
902
|
+
yield
|
|
903
|
+
@depth -= 1
|
|
904
|
+
line("</g>")
|
|
905
|
+
end
|
|
906
|
+
|
|
907
|
+
def outline_only(color, box, width, shapes)
|
|
908
|
+
return if color == NONE
|
|
909
|
+
stroke = fill(color, box)
|
|
910
|
+
shapes.each do |g|
|
|
911
|
+
geom(g, "fill=\"none\" stroke=\"#{stroke}\" stroke-width=\"#{num(width / g.stroke_scale)}" \
|
|
912
|
+
"\" stroke-linejoin=\"miter\" stroke-miterlimit=\"10\"")
|
|
913
|
+
end
|
|
914
|
+
end
|
|
915
|
+
|
|
916
|
+
def embedded(c, at)
|
|
917
|
+
s = c.shape
|
|
918
|
+
attrs = []
|
|
919
|
+
attrs << "color=\"#{color(c.color)}\"" unless c.color.empty?
|
|
920
|
+
attrs << "transform=\"rotate(#{num(s.rotate)} #{num(at.x)} #{num(at.y)})\"" if s.rotate != 0
|
|
921
|
+
unless attrs.empty?
|
|
922
|
+
line("<g #{attrs.join(' ')}>")
|
|
923
|
+
@depth += 1
|
|
924
|
+
end
|
|
925
|
+
# Each time it's drawn gets its own prefix, since a definition can draw it many times.
|
|
926
|
+
prefix = "#{id('svg')}-"
|
|
927
|
+
art = s.embedded
|
|
928
|
+
line("<svg#{mark} x=\"#{num(at.x - s.fly / 2)}\" y=\"#{num(at.y - s.hoist / 2)}\" width=\"#{num(s.fly)}" \
|
|
929
|
+
"\" height=\"#{num(s.hoist)}\" viewBox=\"#{Xml.escape(art.view_box)}" \
|
|
930
|
+
"\" preserveAspectRatio=\"xMidYMid meet\"#{art.attrs(prefix)}>#{art.body(prefix)}</svg>")
|
|
931
|
+
return if attrs.empty?
|
|
932
|
+
@depth -= 1
|
|
933
|
+
line("</g>")
|
|
934
|
+
end
|
|
935
|
+
|
|
936
|
+
# Fills shapes, after painting any fimbriation beneath all of them. Each shape's stroke lies
|
|
937
|
+
# beneath the others' fills, so arms read as a single shape. A gradient spans box, or each
|
|
938
|
+
# shape's own bounds if box is nil.
|
|
939
|
+
def paint(color, box, fimbriate, stroke, shapes)
|
|
940
|
+
if fimbriate && stroke
|
|
941
|
+
edge = fill(fimbriate.color, box)
|
|
942
|
+
stroke.each do |g|
|
|
943
|
+
geom(g, "fill=\"#{edge}\" stroke=\"#{edge}\" stroke-width=\"#{num(2 * fimbriate.width / g.stroke_scale)}" \
|
|
944
|
+
"\" stroke-linejoin=\"miter\" stroke-miterlimit=\"10\"")
|
|
945
|
+
end
|
|
946
|
+
end
|
|
947
|
+
return if shapes.nil? || shapes.empty? || color == NONE
|
|
948
|
+
paint_fill = fill(color, box)
|
|
949
|
+
shapes.each { |g| geom(g, "fill=\"#{paint_fill}\"") }
|
|
950
|
+
end
|
|
951
|
+
|
|
952
|
+
# The attribute that marks a drawn element, if this render has them.
|
|
953
|
+
def mark
|
|
954
|
+
return "" unless @tagging
|
|
955
|
+
" #{Vexyl::TAG_ATTRIBUTE}=\"#{Xml.escape(@tag)}\""
|
|
956
|
+
end
|
|
957
|
+
|
|
958
|
+
# Writes what's only part of a definition, which no layer drew.
|
|
959
|
+
def unmarked(g, attrs)
|
|
960
|
+
was = @tagging
|
|
961
|
+
@tagging = false
|
|
962
|
+
geom(g, attrs)
|
|
963
|
+
@tagging = was
|
|
964
|
+
end
|
|
965
|
+
|
|
966
|
+
def geom(g, attrs)
|
|
967
|
+
a = +mark
|
|
968
|
+
a << " " << attrs unless attrs.empty?
|
|
969
|
+
a << " transform=\"" << g.transform << "\"" unless g.transform.empty?
|
|
970
|
+
a << " fill-rule=\"evenodd\"" if g.evenodd
|
|
971
|
+
if !g.path.empty?
|
|
972
|
+
line("<path d=\"#{g.path}\"#{a}/>")
|
|
973
|
+
elsif g.circle
|
|
974
|
+
line("<circle cx=\"#{num(g.c.x)}\" cy=\"#{num(g.c.y)}\" r=\"#{num(g.r)}\"#{a}/>")
|
|
975
|
+
elsif g.ring
|
|
976
|
+
line("<path fill-rule=\"evenodd\" d=\"#{Shapes.circle_path(g.c, g.r)} #{Shapes.circle_path(g.c, g.inner)}\"#{a}/>")
|
|
977
|
+
else
|
|
978
|
+
points = +""
|
|
979
|
+
(g.poly || []).each_with_index do |v, i|
|
|
980
|
+
points << " " if i > 0
|
|
981
|
+
points << num(v.x) << "," << num(v.y)
|
|
982
|
+
end
|
|
983
|
+
line("<polygon points=\"#{points}\"#{a}/>")
|
|
984
|
+
end
|
|
985
|
+
end
|
|
986
|
+
|
|
987
|
+
# Reads the renderer's own output, which is plain XML with double-quoted attributes.
|
|
988
|
+
class TreeReader
|
|
989
|
+
REFS = { "amp" => "&", "lt" => "<", "gt" => ">", "quot" => "\"", "apos" => "'" }.freeze
|
|
990
|
+
|
|
991
|
+
def initialize(s)
|
|
992
|
+
@s = StringScanner.new(s)
|
|
993
|
+
end
|
|
994
|
+
|
|
995
|
+
def element
|
|
996
|
+
s = @s
|
|
997
|
+
s.skip(/[^<]*/)
|
|
998
|
+
s.getch
|
|
999
|
+
name = s.scan(/[^\s>\/]*/)
|
|
1000
|
+
attrs = []
|
|
1001
|
+
loop do
|
|
1002
|
+
s.skip(/\s*/)
|
|
1003
|
+
return Element.new(name, attrs, "", []) if s.skip(%r{/>})
|
|
1004
|
+
break if s.skip(/>/)
|
|
1005
|
+
key = s.scan(/[^=]*/).strip
|
|
1006
|
+
s.skip(/[^"]*"/)
|
|
1007
|
+
value = s.scan(/[^"]*/)
|
|
1008
|
+
s.getch
|
|
1009
|
+
attrs << Element::Attribute.new(key, unescape(value))
|
|
1010
|
+
end
|
|
1011
|
+
children = []
|
|
1012
|
+
text = +""
|
|
1013
|
+
keep_text = name == "title" || name == "desc"
|
|
1014
|
+
loop do
|
|
1015
|
+
chunk = s.scan(/[^<]*/)
|
|
1016
|
+
text << unescape(chunk) if keep_text
|
|
1017
|
+
if s.check(%r{</})
|
|
1018
|
+
s.skip(/[^>]*>/)
|
|
1019
|
+
return Element.new(name, attrs, text, children)
|
|
1020
|
+
end
|
|
1021
|
+
children << element
|
|
1022
|
+
end
|
|
1023
|
+
end
|
|
1024
|
+
|
|
1025
|
+
private
|
|
1026
|
+
|
|
1027
|
+
def unescape(t)
|
|
1028
|
+
return t unless t.include?("&")
|
|
1029
|
+
t.gsub(/&([^;]*);/) do
|
|
1030
|
+
ref = Regexp.last_match(1)
|
|
1031
|
+
REFS[ref] || [ref.start_with?("#x") ? ref[2..].to_i(16) : ref[1..].to_i].pack("U")
|
|
1032
|
+
end
|
|
1033
|
+
end
|
|
1034
|
+
end
|
|
1035
|
+
end
|
|
1036
|
+
end
|