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,120 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # The limits of section 15 of the format, so that a small document can't demand unbounded work.
6
+ module Limits
7
+ MAX_DEPTH = 32
8
+ # How much a whole flag can paint.
9
+ MAX_SIZE = 200_000
10
+ MAX_COPIES = 10_000
11
+ # How many pieces a fold can make, since each step can double them.
12
+ MAX_FACETS = 10_000
13
+ # How many bytes of path data or inline SVG count as one part.
14
+ BYTES_PER_PART = 64
15
+ # The largest size a number can have, and its inverse the smallest.
16
+ NUMBER_LIMIT = 1e12
17
+
18
+ module_function
19
+
20
+ # Whether a number is finite and of a reasonable size.
21
+ def in_limits(v)
22
+ a = v.abs
23
+ !v.nan? && a <= NUMBER_LIMIT && (a == 0 || a >= 1 / NUMBER_LIMIT)
24
+ end
25
+
26
+ # Adds sizes without passing the point where they stop mattering.
27
+ def add(a, b)
28
+ [a + b, MAX_SIZE + 1].min
29
+ end
30
+
31
+ def mul(a, b)
32
+ return 0 if a == 0 || b == 0
33
+ [a * b, MAX_SIZE + 1].min
34
+ end
35
+
36
+ def bytes(s)
37
+ s.bytesize
38
+ end
39
+
40
+ # How many parts one copy of a shape counts as.
41
+ def shape_parts(s)
42
+ case s.kind
43
+ when "star", "sun" then [s.points, 1].max
44
+ when "polygon" then [s.vertices.size, 1].max
45
+ when "path" then 1 + bytes(s.d) / BYTES_PER_PART
46
+ when "svg" then 1 + bytes(s.markup) / BYTES_PER_PART
47
+ else 1
48
+ end
49
+ end
50
+
51
+ def charge_copies(c)
52
+ return c.repeat.rows * c.repeat.columns if c.repeat && c.repeat.kind == "grid"
53
+ return c.repeat.count if c.repeat
54
+ return c.shape.ats.size if c.shape.ats.size > 1
55
+ 1
56
+ end
57
+
58
+ # What a layer paints. A definition's size was worked out when it was read.
59
+ def layer_size(l, longer)
60
+ size = l.clip ? shape_parts(l.clip) : 0
61
+ cuts = 0
62
+ l.cuts.each { |s| cuts = add(cuts, shape_parts(s)) }
63
+ # A charge's cuts go with each of its copies.
64
+ cuts = mul(charge_copies(l), cuts) if l.is_a?(Model::Charge)
65
+ add(add(size, cuts), content_size(l, longer))
66
+ end
67
+
68
+ def content_size(l, longer)
69
+ case l
70
+ when Model::Bars
71
+ return mul(l.colors.size, 1 + l.line.waves(longer)) if l.line
72
+ l.colors.size
73
+ when Model::Chevrons, Model::Rays
74
+ l.colors.size
75
+ when Model::Quarters
76
+ 4
77
+ when Model::Checks
78
+ mul(l.rows, l.columns)
79
+ when Model::Charge
80
+ mul(charge_copies(l), shape_parts(l.shape))
81
+ when Model::Emblem
82
+ size = 0
83
+ l.charges.each { |c| size = add(size, layer_size(c, longer)) }
84
+ size
85
+ when Model::Group
86
+ layers_size(l.layers, longer)
87
+ when Model::Panel
88
+ size = layers_size(l.layers, longer)
89
+ size = add(size, l.line.waves(longer)) if l.line
90
+ size
91
+ when Model::Use
92
+ l.design.size
93
+ else
94
+ 1
95
+ end
96
+ end
97
+ private_class_method :content_size
98
+
99
+ def layers_size(layers, longer)
100
+ size = 0
101
+ layers.each { |l| size = add(size, layer_size(l, longer)) }
102
+ size
103
+ end
104
+
105
+ # How many levels a layer nests, counting what it uses.
106
+ def layer_height(l)
107
+ case l
108
+ when Model::Group, Model::Panel then 1 + layers_height(l.layers)
109
+ when Model::Use then 1 + l.design.height
110
+ else 1
111
+ end
112
+ end
113
+
114
+ def layers_height(layers)
115
+ height = 0
116
+ layers.each { |l| height = [height, layer_height(l)].max }
117
+ height
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,133 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
3
+
4
+ module Vexyl
5
+ # Where a layer or other part of a document is, as section 7.3 of the SDK describes it. +path+
6
+ # is a layer's path, such as "3.1" or "variant/NAME/0", or what else it is, such as
7
+ # "palette/NAME", and +node+ the KDL node's name. Lines and columns are counted as a Problem's
8
+ # are, and a part ends just past its last character, not counting a ; or line ending.
9
+ Span = Struct.new(:path, :node, :line, :column, :end_line, :end_column) do
10
+ def to_h
11
+ { "path" => path, "node" => node, "line" => line, "column" => column,
12
+ "endLine" => end_line, "endColumn" => end_column }
13
+ end
14
+ end
15
+
16
+ # Finds each part of a document from its syntax alone.
17
+ module Locate
18
+ LAYER_NAMES = %w[field bars chevrons band cross saltire quarters checks rays pile pall charge
19
+ emblem group panel canton use].freeze
20
+ HOLDS_LAYERS = %w[emblem group panel canton].freeze
21
+
22
+ module_function
23
+
24
+ # Writes a name in a path, where a slash separates the parts.
25
+ def encode_name(name)
26
+ name.gsub("%", "%25").gsub("/", "%2F")
27
+ end
28
+
29
+ def decode_name(part)
30
+ part.gsub("%2F", "/").gsub("%25", "%")
31
+ end
32
+
33
+ # The path of what holds a layer, or "" if it's at the top of its scope.
34
+ def parent_path(path)
35
+ dot = path.rindex(".")
36
+ slash = path.rindex("/") || -1
37
+ dot && dot > slash ? path[0, dot] : ""
38
+ end
39
+
40
+ # Turns code point offsets into lines and columns, as Problems count them.
41
+ class Places
42
+ def initialize(src)
43
+ @starts = [0]
44
+ src.each_char.with_index { |c, i| @starts << (i + 1) if c == "\n" }
45
+ end
46
+
47
+ def [](offset)
48
+ line = @starts.bsearch_index { |s| s > offset } || @starts.length
49
+ [line, offset - @starts[line - 1] + 1]
50
+ end
51
+ end
52
+
53
+ def locate(text)
54
+ src = Parser.normalize_newlines(text)
55
+ begin
56
+ doc = Kdl.parse(src)
57
+ rescue Kdl::SyntaxError
58
+ return []
59
+ end
60
+ places = Places.new(src)
61
+ spans = []
62
+ index = 0
63
+ doc.each do |n|
64
+ name = n.name
65
+ if LAYER_NAMES.include?(name)
66
+ path = index.to_s
67
+ index += 1
68
+ add(spans, places, n, path)
69
+ layers(spans, places, n.children, "#{path}.") if HOLDS_LAYERS.include?(name)
70
+ elsif name == "palette"
71
+ add(spans, places, n, name)
72
+ n.children.each { |c| add(spans, places, c, "palette/#{encode_name(c.name)}") }
73
+ elsif name == "define" || name == "fold"
74
+ path = named(n, name)
75
+ if path
76
+ add(spans, places, n, path)
77
+ layers(spans, places, n.children, "#{path}/") if name == "define"
78
+ end
79
+ elsif name == "variant"
80
+ path = named(n, name)
81
+ if path
82
+ add(spans, places, n, path)
83
+ layers(spans, places, n.children, "#{path}/")
84
+ n.children.each do |c|
85
+ add(spans, places, c, "#{path}/#{c.name}") if c.name == "outline" || c.name == "border"
86
+ end
87
+ end
88
+ elsif name == "reverse"
89
+ add(spans, places, n, name)
90
+ layers(spans, places, n.children, "reverse/")
91
+ else
92
+ add(spans, places, n, name)
93
+ end
94
+ end
95
+ spans
96
+ end
97
+
98
+ def named(n, kind)
99
+ return nil if n.args.empty? || !n.args[0].string?
100
+ "#{kind}/#{encode_name(n.args[0].string)}"
101
+ end
102
+
103
+ def layers(spans, places, list, base)
104
+ i = 0
105
+ list.each do |n|
106
+ next unless LAYER_NAMES.include?(n.name)
107
+ path = "#{base}#{i}"
108
+ add(spans, places, n, path)
109
+ layers(spans, places, n.children, "#{path}.") if HOLDS_LAYERS.include?(n.name)
110
+ i += 1
111
+ end
112
+ end
113
+
114
+ def add(spans, places, n, path)
115
+ line, column = places[n.start]
116
+ # The end is just past the last character, as the reference counts it.
117
+ end_line, end_column = places[n.stop]
118
+ spans << Span.new(path, n.name, line, column, end_line, end_column).freeze
119
+ end
120
+
121
+ # The innermost span holding a line: the one that starts latest, and in a tie, the last.
122
+ def layer_at(spans, line)
123
+ best = nil
124
+ spans.each do |s|
125
+ next unless line >= s.line && line <= s.end_line
126
+ if best.nil? || s.line > best.line || (s.line == best.line && s.column >= best.column)
127
+ best = s
128
+ end
129
+ end
130
+ best
131
+ end
132
+ end
133
+ end