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,451 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # Moves layers along one axis as their region changes size, as section 11 of the format says.
6
+ class Adapter
7
+ ANCHORS = { "extend" => "start", "center" => "center", "scale" => "scale" }.freeze
8
+
9
+ def initialize(axis, anchor, from_behind, other)
10
+ @axis = axis
11
+ @anchor = anchor
12
+ # An anchor that a container passed on to its layers, if one did.
13
+ @passed = ""
14
+ # Says that the layers are a reverse's own, with the hoist on the right.
15
+ @from_behind = from_behind
16
+ # The region's size across the axis, which stays the same.
17
+ @other = other
18
+ @err = nil
19
+ end
20
+
21
+ # The flag at another proportion, as fly divided by hoist, following its adapt rules.
22
+ def self.with_ratio(f, ratio)
23
+ current = f.fly / f.hoist
24
+ return f if (ratio - current).abs < 1e-9 * current
25
+ raise Error, "#{f.meta.name} doesn't say how it adapts to other proportions" if f.adapt.nil?
26
+ if (f.adapt.min > 0 && ratio < f.adapt.min) || (f.adapt.max > 0 && ratio > f.adapt.max)
27
+ raise Error, "#{f.meta.name} only adapts between ratios of #{Json.number(f.adapt.min)} " \
28
+ "and #{Json.number(f.adapt.max)}"
29
+ end
30
+ mode = ratio > current ? f.adapt.wider : f.adapt.narrower
31
+ raise Error, "#{f.meta.name} has no rule for this direction of change" if mode.empty?
32
+ out = f.copy
33
+ on_hoist = mode.start_with?("hoist-")
34
+ base = on_hoist ? mode.delete_prefix("hoist-") : mode
35
+ anchor = ANCHORS[base]
36
+ if on_hoist
37
+ a = new("hoist", anchor, f.seen_from_behind, f.fly)
38
+ out.hoist = f.fly / ratio
39
+ out.layers = a.adapt_layers(f.layers, f.hoist, out.hoist)
40
+ else
41
+ a = new("fly", anchor, f.seen_from_behind, f.hoist)
42
+ out.fly = f.hoist * ratio
43
+ out.layers = a.adapt_layers(f.layers, f.fly, out.fly)
44
+ end
45
+ if f.outline && f.outline.kind == "polygon"
46
+ # A silhouette has no sensible anchor, so a drawn one stretches with the flag.
47
+ o = f.outline.copy
48
+ o.vertices = f.outline.vertices.map do |p|
49
+ Model::Point.new(p.fly * out.fly / f.fly, p.hoist * out.hoist / f.hoist)
50
+ end
51
+ out.outline = o
52
+ end
53
+ raise Error, a.err if a.err
54
+ out
55
+ end
56
+
57
+ # The proportion closest to a target that the flag's rules allow.
58
+ def self.nearest_ratio(f, target)
59
+ current = f.fly / f.hoist
60
+ return current if f.adapt.nil?
61
+ if (target > current && f.adapt.wider.empty?) || (target < current && f.adapt.narrower.empty?)
62
+ return current
63
+ end
64
+ target = [target, f.adapt.min].max if f.adapt.min > 0
65
+ target = [target, f.adapt.max].min if f.adapt.max > 0
66
+ target
67
+ end
68
+
69
+ # Maps a position along the axis, by a layer's anchor.
70
+ def self.move(x, old, now, anchor)
71
+ case anchor
72
+ when "center" then x + (now - old) / 2
73
+ when "end" then x + now - old
74
+ when "scale" then x * now / old
75
+ else x
76
+ end
77
+ end
78
+
79
+ # How far a region reaches either side of a point, along a normal.
80
+ def self.spread(r, c, normal)
81
+ lo = Float::INFINITY
82
+ hi = -Float::INFINITY
83
+ r.corners.each do |corner|
84
+ offset = corner.sub(c).dot(normal)
85
+ lo = [lo, offset].min
86
+ hi = [hi, offset].max
87
+ end
88
+ [lo, hi]
89
+ end
90
+
91
+ # The first refusal found while adapting, if there was one.
92
+ attr_reader :err
93
+
94
+ # Adapts the flag's own layers.
95
+ def adapt_layers(list, old, now)
96
+ layers(list, old, now)
97
+ end
98
+
99
+ private
100
+
101
+ # A layer's anchor: its own, or one passed on to it, or the rule's, and whether it was given one.
102
+ def anchor_of(e)
103
+ anchor = e.anchor
104
+ anchor = @passed if anchor.empty?
105
+ anchor = @anchor if anchor.empty?
106
+ if @from_behind && @axis == "fly"
107
+ # Seen from behind, the hoist is on the right, and start is still the hoist.
108
+ if anchor == "start"
109
+ anchor = "end"
110
+ elsif anchor == "end"
111
+ anchor = "start"
112
+ end
113
+ end
114
+ anchor
115
+ end
116
+
117
+ def given(e)
118
+ !e.anchor.empty? || !@passed.empty?
119
+ end
120
+
121
+ # Adapts a container's layers with its anchor, if it gives one.
122
+ def pass_on(e)
123
+ was = @passed
124
+ @passed = e.anchor unless e.anchor.empty?
125
+ yield
126
+ ensure
127
+ @passed = was
128
+ end
129
+
130
+ # The region's center, for pinning what's there to an anchor.
131
+ def center(old)
132
+ return Model::Point.new(@other / 2, old / 2) if @axis == "hoist"
133
+ Model::Point.new(old / 2, @other / 2)
134
+ end
135
+
136
+ def fail_with(message)
137
+ @err = message if @err.nil?
138
+ end
139
+
140
+ def layers(list, old, now)
141
+ list.map { |l| layer(l, old, now) }
142
+ end
143
+
144
+ def move(x, old, now, anchor)
145
+ Adapter.move(x, old, now, anchor)
146
+ end
147
+
148
+ def point(p, old, now, anchor)
149
+ return nil if p.nil?
150
+ return Model::Point.new(move(p.fly, old, now, anchor), p.hoist) if @axis == "fly"
151
+ Model::Point.new(p.fly, move(p.hoist, old, now, anchor))
152
+ end
153
+
154
+ # Moves a shape, which keeps its own form unless it's drawn from points. The center stays the
155
+ # center, so a charge there follows it unless anchored otherwise.
156
+ def shape(input, old, now, anchor, explicit)
157
+ s = input.copy
158
+ k = now / old
159
+ s.at = center(old) if s.at.nil? && explicit && anchor != "scale" && s.kind != "polygon"
160
+ s.at = point(s.at, old, now, anchor)
161
+ s.ats = input.ats.map { |p| point(p, old, now, anchor) }
162
+ s.vertices = input.vertices.map { |p| point(p, old, now, anchor) }
163
+ if anchor == "scale"
164
+ case s.kind
165
+ when "rect", "lozenge", "ellipse"
166
+ if @axis == "fly"
167
+ s.fly *= k
168
+ else
169
+ s.hoist *= k
170
+ end
171
+ end
172
+ end
173
+ # Piles and palls are set from their edge, so depths only change when scaling, and
174
+ # positions along the edge move like any other.
175
+ if s.kind == "pile" || s.kind == "pall"
176
+ runs_along_axis = (s.edge == "hoist" || s.edge == "fly") == (@axis == "fly")
177
+ if runs_along_axis
178
+ if anchor == "scale"
179
+ s.depth *= k
180
+ s.junction *= k
181
+ s.tip = [s.tip[0], s.tip[1] * k] if s.tip
182
+ end
183
+ else
184
+ s.base = [move(s.base[0], old, now, anchor), move(s.base[1], old, now, anchor)] if s.base
185
+ s.tip = [move(s.tip[0], old, now, anchor), s.tip[1]] if s.tip
186
+ end
187
+ end
188
+ s
189
+ end
190
+
191
+ # Copies a layer with its clip and cuts moved, and gives its anchor.
192
+ def effects(c, e, old, now)
193
+ anchor = anchor_of(e)
194
+ c.clip = shape(e.clip, old, now, anchor, false) if e.clip
195
+ c.cuts = e.cuts.map { |s| shape(s, old, now, anchor, false) }
196
+ anchor
197
+ end
198
+
199
+ # Shares a change in size across parts along the axis, by a layer's anchor.
200
+ def parts(what, parts, old, now, anchor)
201
+ total = 0.0
202
+ parts.each { |p| total += p }
203
+ out = parts.map { |p| p * old / total }
204
+ d = now - old
205
+ last = out.size - 1
206
+ case anchor
207
+ when "start"
208
+ out[last] += d
209
+ changed = [last]
210
+ when "end"
211
+ out[0] += d
212
+ changed = [0]
213
+ when "center"
214
+ out[0] += d / 2
215
+ out[last] += d / 2
216
+ changed = [0, last]
217
+ else
218
+ return parts
219
+ end
220
+ changed.each { |i| keep(what, out[i], old) }
221
+ out
222
+ end
223
+
224
+ # Refuses a part that a change of size has brought to nothing, allowing for rounding.
225
+ def keep(what, part, old)
226
+ fail_with("#{what} can't be cut to this proportion") if part <= 1e-9 * old
227
+ end
228
+
229
+ def layer(l, old, now)
230
+ k = now / old
231
+ case l
232
+ when Model::Bars
233
+ c = l.copy
234
+ anchor = effects(c, l, old, now)
235
+ return rotated_bars(c, old, now, anchor) if l.rotate != 0
236
+ if (@axis == "fly") != (l.along == "hoist") || l.along == "bend" || l.along == "bend-sinister"
237
+ return c
238
+ end
239
+ if l.grow
240
+ total = 0.0
241
+ weights = 0.0
242
+ l.ratio.each_index do |i|
243
+ total += l.ratio[i]
244
+ weights += l.grow[i]
245
+ end
246
+ if weights > 0
247
+ c.ratio = Array.new(l.ratio.size) do |i|
248
+ v = l.ratio[i] * old / total + (now - old) * l.grow[i] / weights
249
+ keep("bars", v, old) if l.grow[i] > 0
250
+ v
251
+ end
252
+ return c
253
+ end
254
+ end
255
+ c.ratio = parts("bars", l.ratio, old, now, anchor)
256
+ c
257
+ when Model::Chevrons
258
+ c = l.copy
259
+ anchor = effects(c, l, old, now)
260
+ along_axis = (l.from == "hoist" || l.from == "fly") == (@axis == "fly")
261
+ if along_axis && anchor == "scale"
262
+ c.depth *= k
263
+ elsif !along_axis && !l.apex.nil?
264
+ c.apex = move(l.apex, old, now, anchor)
265
+ end
266
+ c
267
+ when Model::Band
268
+ c = l.copy
269
+ anchor = effects(c, l, old, now)
270
+ if l.rotate != 0
271
+ # It keeps its angle and width, and its center line moves like any position.
272
+ before = regions(old, now)[0]
273
+ frame = Shapes.frame(l.fixed, before)
274
+ center = Shapes.frame_center(l.fixed, before)
275
+ normal = Shapes.axis(frame, l.along, l.rotate)[1]
276
+ moved = move_vec(center.add(normal.mul(l.offset)), old, now, anchor)
277
+ c.fixed = Model::FixedFrame.new(frame.w, frame.h, moved.x, moved.y)
278
+ c.offset = 0.0
279
+ return c
280
+ end
281
+ across_axis = (@axis == "fly" && l.along == "hoist") || (@axis == "hoist" && l.along == "fly")
282
+ if across_axis
283
+ case anchor
284
+ when "start"
285
+ c.offset -= (now - old) / 2
286
+ when "end"
287
+ c.offset += (now - old) / 2
288
+ when "scale"
289
+ c.offset *= k
290
+ c.width *= k
291
+ end
292
+ end
293
+ c
294
+ when Model::Cross
295
+ c = l.copy
296
+ anchor = effects(c, l, old, now)
297
+ if @axis == "fly"
298
+ c.fly_parts = parts("a cross", l.fly_parts, old, now, anchor).dup
299
+ else
300
+ c.hoist_parts = parts("a cross", l.hoist_parts, old, now, anchor).dup
301
+ end
302
+ c
303
+ when Model::Quarters
304
+ c = l.copy
305
+ anchor = effects(c, l, old, now)
306
+ c.split = point(l.split, old, now, anchor)
307
+ c
308
+ when Model::Rays
309
+ c = l.copy
310
+ anchor = effects(c, l, old, now)
311
+ c.at = point(l.at, old, now, anchor)
312
+ c
313
+ when Model::Charge
314
+ c = l.copy
315
+ anchor = effects(c, l, old, now)
316
+ c.shape = shape(l.shape, old, now, anchor, given(l))
317
+ if l.repeat && l.repeat.kind == "line" && anchor == "scale"
318
+ r = l.repeat.copy
319
+ if @axis == "fly"
320
+ r.fly *= k
321
+ else
322
+ r.hoist *= k
323
+ end
324
+ c.repeat = r
325
+ end
326
+ c
327
+ when Model::Emblem
328
+ c = l.copy
329
+ anchor = effects(c, l, old, now)
330
+ # One at the center that's given an anchor is pinned to it, as a charge is.
331
+ c.at = center(old) if c.at.nil? && given(l) && anchor != "scale"
332
+ c.at = point(c.at, old, now, anchor)
333
+ c
334
+ when Model::Group
335
+ c = l.copy
336
+ effects(c, l, old, now)
337
+ pass_on(l) { c.layers = layers(l.layers, old, now) }
338
+ c
339
+ when Model::Panel
340
+ panel(l, old, now)
341
+ when Model::Use
342
+ c = l.copy
343
+ effects(c, l, old, now)
344
+ if l.design.fly == 0
345
+ d = Model::Define.new
346
+ d.shape = l.design.shape
347
+ d.fly = l.design.fly
348
+ d.hoist = l.design.hoist
349
+ d.size = l.design.size
350
+ d.height = l.design.height
351
+ pass_on(l) { d.layers = layers(l.design.layers, old, now) }
352
+ c.design = d
353
+ end
354
+ c
355
+ else
356
+ # A field, saltire or checks follows its region.
357
+ c = l.copy
358
+ effects(c, l, old, now)
359
+ c
360
+ end
361
+ end
362
+
363
+ # The regions before and after the change.
364
+ def regions(old, now)
365
+ if @axis == "hoist"
366
+ return [Shapes::Region.new(@other, old), Shapes::Region.new(@other, now)]
367
+ end
368
+ [Shapes::Region.new(old, @other), Shapes::Region.new(now, @other)]
369
+ end
370
+
371
+ # Moves a point along the axis, by a layer's anchor.
372
+ def move_vec(v, old, now, anchor)
373
+ return Geometry::Vec.new(v.x, move(v.y, old, now, anchor)) if @axis == "hoist"
374
+ Geometry::Vec.new(move(v.x, old, now, anchor), v.y)
375
+ end
376
+
377
+ # Keeps a rotated bars' angle, and the lines between its bars where they were about its center,
378
+ # which moves like any position. The first and last bars reach the new region's corners.
379
+ def rotated_bars(c, old, now, anchor)
380
+ before, after = regions(old, now)
381
+ frame = Shapes.frame(c.fixed, before)
382
+ center = Shapes.frame_center(c.fixed, before)
383
+ normal = Shapes.axis(frame, c.along, c.rotate)[1]
384
+ moved = move_vec(center, old, now, anchor)
385
+ lo, hi = Adapter.spread(before, center, normal)
386
+ will = Adapter.spread(after, moved, normal)
387
+ total = 0.0
388
+ c.ratio.each { |part| total += part }
389
+ n = c.ratio.size
390
+ ratio = Array.new(n)
391
+ line = lo
392
+ start = will[0]
393
+ n.times do |i|
394
+ line += (hi - lo) * c.ratio[i] / total
395
+ stop = i == n - 1 ? will[1] : line
396
+ ratio[i] = stop - start
397
+ start = stop
398
+ end
399
+ keep("bars", ratio[0], old)
400
+ keep("bars", ratio[n - 1], old)
401
+ c.ratio = ratio
402
+ c.fixed = Model::FixedFrame.new(frame.w, frame.h, moved.x, moved.y)
403
+ c
404
+ end
405
+
406
+ # Keeps a panel at its edge or corner. One running along the axis grows with its parent, and
407
+ # only scaling changes the size of any other.
408
+ def panel(t, old, now)
409
+ k = now / old
410
+ c = t.copy
411
+ anchor = effects(c, t, old, now)
412
+ if t.side.empty? && t.corner.empty? && c.at.nil? && given(t) && anchor != "scale"
413
+ # One at the center that's given an anchor is pinned to it, as a charge is.
414
+ c.at = center(old)
415
+ end
416
+ c.at = point(c.at, old, now, anchor)
417
+ runs_along = (@axis == "fly" && (t.side == "top" || t.side == "bottom")) ||
418
+ (@axis == "hoist" && (t.side == "hoist" || t.side == "fly"))
419
+ outer = @other
420
+ was = @passed
421
+ begin
422
+ @passed = t.anchor unless t.anchor.empty?
423
+ if runs_along
424
+ @other = t.width
425
+ c.layers = layers(t.layers, old, now)
426
+ return c
427
+ end
428
+ return c if anchor != "scale"
429
+ # Everything inside is measured against the panel's own size along the axis.
430
+ size = t.fly
431
+ if !t.side.empty?
432
+ size = t.width
433
+ c.width *= k
434
+ @other = outer
435
+ elsif @axis == "fly"
436
+ @other = t.hoist
437
+ c.fly *= k
438
+ else
439
+ size = t.hoist
440
+ @other = t.fly
441
+ c.hoist *= k
442
+ end
443
+ c.layers = layers(t.layers, size, size * k)
444
+ c
445
+ ensure
446
+ @other = outer
447
+ @passed = was
448
+ end
449
+ end
450
+ end
451
+ end