ea 0.6.146 → 0.6.148
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 +4 -4
- data/lib/ea/svg/parity/differ.rb +409 -158
- data/lib/ea/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d2395d5ef03b627d02de55c09f65eff28984a512919ed75b39d8b1cbe110dc20
|
|
4
|
+
data.tar.gz: ff25b771f4134f453882054a6921eba3e0ffbbb2fe07347292ffdb1ade43e9b2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3c0e61969712ad88fc8d9ccc9da3a034ce5877f880738271935a192eff7219d15927f609134b1f6d956f2abb15d9191492bddf14085372068b1b3bb9544d37fe
|
|
7
|
+
data.tar.gz: fa6306f9f66d3e3192c921127710d365f3a53a64fac9a3498011a9f7c0c632ccb04c8777a5da45bbc78386fc0ba8f8cad18f7c3e5d6fa9597bd4b147cb2391e9
|
data/lib/ea/svg/parity/differ.rb
CHANGED
|
@@ -24,207 +24,458 @@ module Ea
|
|
|
24
24
|
@shift = dominant_shift
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
#
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
# Extracts the primitives consumed by both comparison reports.
|
|
28
|
+
class PrimitiveParser
|
|
29
|
+
def parse(svg)
|
|
30
|
+
Nokogiri::XML(svg).css("g").flat_map do |group|
|
|
31
|
+
group.element_children.filter_map { |node| primitive(node, group["style"] || "") }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def primitive(node, style)
|
|
36
|
+
case node.name
|
|
37
|
+
when "text" then text_primitive(node, style)
|
|
38
|
+
when "rect" then rect_primitive(node, style)
|
|
39
|
+
when "polygon" then points_primitive(:polygon, node["points"], style)
|
|
40
|
+
when "line" then points_primitive(:line, line_points(node), style)
|
|
41
|
+
when "path" then path_primitive(node, style)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
private :primitive
|
|
45
|
+
|
|
46
|
+
def line_points(node)
|
|
47
|
+
%w[x1 y1 x2 y2].map { |key| node[key] }.join(" ")
|
|
48
|
+
end
|
|
49
|
+
private :line_points
|
|
50
|
+
|
|
51
|
+
def text_primitive(node, g_style)
|
|
52
|
+
style = node["style"] || ""
|
|
53
|
+
{
|
|
54
|
+
kind: :text,
|
|
55
|
+
x: float(node["x"]), y: float(node["y"]),
|
|
56
|
+
content: node.text.strip,
|
|
57
|
+
**text_style(style),
|
|
58
|
+
fill: style[/[^-]fill:([^;]+)/, 1] || g_style[/fill:([^;]+)/, 1]
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
private :text_primitive
|
|
62
|
+
|
|
63
|
+
def text_style(style)
|
|
64
|
+
{
|
|
65
|
+
family: style[/font-family:([^;]+)/, 1],
|
|
66
|
+
size: style[/font-size:([^;]+)/, 1],
|
|
67
|
+
weight: normalize_weight(style[/font-weight:([^;]+)/, 1]),
|
|
68
|
+
style: style[/font-style:([^;]+)/, 1]
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
private :text_style
|
|
72
|
+
|
|
73
|
+
def rect_primitive(node, g_style)
|
|
74
|
+
{
|
|
75
|
+
kind: :rect,
|
|
76
|
+
x: float(node["x"]), y: float(node["y"]),
|
|
77
|
+
width: float(node["width"]), height: float(node["height"]),
|
|
78
|
+
centroid: rect_centroid(node),
|
|
79
|
+
fill: g_style[/fill:([^;]+)/, 1],
|
|
80
|
+
stroke: g_style[/stroke:([^;]+)/, 1]
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
private :rect_primitive
|
|
84
|
+
|
|
85
|
+
def rect_centroid(node)
|
|
86
|
+
x = float(node["x"])
|
|
87
|
+
y = float(node["y"])
|
|
88
|
+
return [0.0, 0.0] if x.nil? || y.nil?
|
|
89
|
+
|
|
90
|
+
[x + float(node["width"]).to_f / 2, y + float(node["height"]).to_f / 2]
|
|
91
|
+
end
|
|
92
|
+
private :rect_centroid
|
|
93
|
+
|
|
94
|
+
def path_primitive(node, g_style)
|
|
95
|
+
points = (node["d"] || "").scan(/-?\d+(?:\.\d+)?/).each_slice(2)
|
|
96
|
+
.map { |(x, y)| [x.to_f, y.to_f] }
|
|
97
|
+
{
|
|
98
|
+
kind: :path, points: points,
|
|
99
|
+
centroid: centroid(points), closed: node["d"].to_s.match?(/[zZ]/),
|
|
100
|
+
stroke: g_style[/stroke:([^;]+)/, 1]
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
private :path_primitive
|
|
104
|
+
|
|
105
|
+
def points_primitive(kind, points_attr, g_style)
|
|
106
|
+
points = (points_attr || "").scan(/-?\d+(?:\.\d+)?/).each_slice(2)
|
|
107
|
+
.map { |(x, y)| [x.to_f, y.to_f] }
|
|
108
|
+
{
|
|
109
|
+
kind: kind, points: points, centroid: centroid(points),
|
|
110
|
+
stroke: g_style[/stroke:([^;]+)/, 1]
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
private :points_primitive
|
|
114
|
+
|
|
115
|
+
def centroid(points)
|
|
116
|
+
return [0.0, 0.0] if points.nil? || points.empty?
|
|
117
|
+
|
|
118
|
+
xs = points.map(&:first)
|
|
119
|
+
ys = points.map(&:last)
|
|
120
|
+
[xs.sum / points.size, ys.sum / points.size]
|
|
121
|
+
end
|
|
122
|
+
private :centroid
|
|
123
|
+
|
|
124
|
+
def float(value)
|
|
125
|
+
value&.to_f
|
|
126
|
+
end
|
|
127
|
+
private :float
|
|
128
|
+
|
|
129
|
+
def normalize_weight(raw)
|
|
130
|
+
return "400" if raw.nil? || raw == "0"
|
|
131
|
+
|
|
132
|
+
raw
|
|
133
|
+
end
|
|
134
|
+
private :normalize_weight
|
|
44
135
|
end
|
|
45
|
-
|
|
136
|
+
private_constant :PrimitiveParser
|
|
137
|
+
|
|
138
|
+
# Matches content and measures text/font drift after canvas alignment.
|
|
139
|
+
module TextComparison
|
|
140
|
+
# EA crops each published image to its own content bounds, so
|
|
141
|
+
# the two canvases differ by a per-diagram translation. Estimate
|
|
142
|
+
# it as the modal delta over content-matched texts and subtract
|
|
143
|
+
# it before comparing positions.
|
|
144
|
+
def dominant_shift
|
|
145
|
+
references = texts_by_content
|
|
146
|
+
deltas = our_texts.filter_map { |ours| text_shift(ours, references[ours[:content]]) }
|
|
147
|
+
return [0.0, 0.0] if deltas.empty?
|
|
148
|
+
|
|
149
|
+
deltas.tally.max_by { |_, count| count }.first.map(&:to_f)
|
|
150
|
+
end
|
|
151
|
+
private :dominant_shift
|
|
152
|
+
|
|
153
|
+
# Texts pair on identical content; positions, family, size and
|
|
154
|
+
# weight are then compared directly.
|
|
155
|
+
def text_deltas
|
|
156
|
+
references = texts_by_content
|
|
157
|
+
deltas = Hash.new { |hash, key| hash[key] = [] }
|
|
158
|
+
our_texts.each { |ours| compare_text(ours, references[ours[:content]], deltas) }
|
|
159
|
+
deltas[:missing_text].concat(references.values.flatten)
|
|
160
|
+
deltas
|
|
161
|
+
end
|
|
46
162
|
|
|
47
|
-
|
|
163
|
+
def our_texts
|
|
164
|
+
@ours.select { |primitive| primitive[:kind] == :text }
|
|
165
|
+
end
|
|
166
|
+
private :our_texts
|
|
48
167
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
168
|
+
def texts_by_content
|
|
169
|
+
@reference.select { |primitive| primitive[:kind] == :text }.group_by { |primitive| primitive[:content] }
|
|
170
|
+
end
|
|
171
|
+
private :texts_by_content
|
|
172
|
+
|
|
173
|
+
def text_shift(ours, candidates)
|
|
174
|
+
reference = candidates&.min_by { |candidate| distance(ours, candidate) }
|
|
175
|
+
return unless reference
|
|
176
|
+
|
|
177
|
+
[(reference[:x] - ours[:x]).round, (reference[:y] - ours[:y]).round]
|
|
178
|
+
end
|
|
179
|
+
private :text_shift
|
|
180
|
+
|
|
181
|
+
def compare_text(ours, candidates, deltas)
|
|
182
|
+
reference = candidates&.min_by { |candidate| distance(ours, candidate) }
|
|
183
|
+
return deltas[:extra_text] << ours unless reference
|
|
184
|
+
|
|
185
|
+
candidates.delete(reference)
|
|
186
|
+
deltas[:moved_text] << ours if moved?(ours, reference)
|
|
187
|
+
compare_font(ours, reference, deltas)
|
|
188
|
+
end
|
|
189
|
+
private :compare_text
|
|
190
|
+
|
|
191
|
+
def compare_font(ours, reference, deltas)
|
|
192
|
+
{ family: :wrong_font, size: :wrong_size, weight: :wrong_weight }.each do |key, mismatch|
|
|
193
|
+
next if font_value(ours, key) == font_value(reference, key)
|
|
194
|
+
|
|
195
|
+
deltas[mismatch] << [ours, reference]
|
|
65
196
|
end
|
|
66
197
|
end
|
|
67
|
-
|
|
68
|
-
end
|
|
69
|
-
private :parse
|
|
198
|
+
private :compare_font
|
|
70
199
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
x: float(node["x"]), y: float(node["y"]),
|
|
76
|
-
content: node.text.strip,
|
|
77
|
-
family: style[/font-family:([^;]+)/, 1],
|
|
78
|
-
size: style[/font-size:([^;]+)/, 1],
|
|
79
|
-
weight: normalize_weight(style[/font-weight:([^;]+)/, 1]),
|
|
80
|
-
style: style[/font-style:([^;]+)/, 1],
|
|
81
|
-
fill: style[/[^-]fill:([^;]+)/, 1] || g_style[/fill:([^;]+)/, 1]
|
|
82
|
-
}
|
|
83
|
-
end
|
|
84
|
-
private :text_primitive
|
|
200
|
+
def font_value(primitive, key)
|
|
201
|
+
key == :weight ? normalize_weight(primitive[key]) : primitive[key]
|
|
202
|
+
end
|
|
203
|
+
private :font_value
|
|
85
204
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
centroid: rect_centroid(node),
|
|
92
|
-
fill: g_style[/fill:([^;]+)/, 1],
|
|
93
|
-
stroke: g_style[/stroke:([^;]+)/, 1]
|
|
94
|
-
}
|
|
95
|
-
end
|
|
96
|
-
private :rect_primitive
|
|
205
|
+
def moved?(ours, ref)
|
|
206
|
+
((ours[:x] + @shift[0]) - ref[:x]).abs > EPSILON ||
|
|
207
|
+
((ours[:y] + @shift[1]) - ref[:y]).abs > EPSILON
|
|
208
|
+
end
|
|
209
|
+
private :moved?
|
|
97
210
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
211
|
+
def distance(ours, reference)
|
|
212
|
+
Math.hypot(ours[:x] - reference[:x], ours[:y] - reference[:y])
|
|
213
|
+
end
|
|
214
|
+
private :distance
|
|
101
215
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
private :rect_centroid
|
|
216
|
+
def normalize_weight(raw)
|
|
217
|
+
return "400" if raw.nil? || raw == "0"
|
|
105
218
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
kind: :path, points: points,
|
|
111
|
-
centroid: centroid(points), closed: node["d"].to_s.include?("Z"),
|
|
112
|
-
stroke: g_style[/stroke:([^;]+)/, 1]
|
|
113
|
-
}
|
|
219
|
+
raw
|
|
220
|
+
end
|
|
221
|
+
private :normalize_weight
|
|
114
222
|
end
|
|
115
|
-
|
|
223
|
+
private_constant :TextComparison
|
|
224
|
+
include TextComparison
|
|
225
|
+
|
|
226
|
+
# Endpoint distances are Euclidean distances after canvas alignment.
|
|
227
|
+
# Arrowhead tips correspond by geometry. An unpaired arrowhead
|
|
228
|
+
# has infinite drift; correctly placed endpoints and tips have zero.
|
|
229
|
+
module ConnectorComparison
|
|
230
|
+
def connector_deltas
|
|
231
|
+
heads = shape_pairs(arrowheads(@reference), arrowheads(@ours))
|
|
232
|
+
connector_pairs.map do |ours, ref|
|
|
233
|
+
connector_delta(ours, ref).merge(arrowhead_deltas: arrowhead_deltas(ours, ref, heads))
|
|
234
|
+
end
|
|
235
|
+
end
|
|
116
236
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
private :points_primitive
|
|
237
|
+
def connector_delta(ours, ref)
|
|
238
|
+
{
|
|
239
|
+
kind: ours[:kind], ours: ours, reference: ref,
|
|
240
|
+
source_delta: endpoint_distance(ours[:points].first, ref[:points].first),
|
|
241
|
+
target_delta: endpoint_distance(ours[:points].last, ref[:points].last)
|
|
242
|
+
}
|
|
243
|
+
end
|
|
244
|
+
private :connector_delta
|
|
126
245
|
|
|
127
|
-
|
|
128
|
-
|
|
246
|
+
def connectors(primitives)
|
|
247
|
+
primitives.select do |primitive|
|
|
248
|
+
%i[path line].include?(primitive[:kind]) && !primitive[:closed] &&
|
|
249
|
+
primitive[:points].size >= 2 && !open_arrowhead?(primitive)
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
private :connectors
|
|
253
|
+
|
|
254
|
+
# Preserve local matches even when both docks move: the allowance
|
|
255
|
+
# for total endpoint drift scales with connector length. Choose the
|
|
256
|
+
# smallest total endpoint drift first, so a shared target cannot
|
|
257
|
+
# steal another connector's exact match.
|
|
258
|
+
def connector_pairs
|
|
259
|
+
candidates = connectors(@ours).product(connectors(@reference))
|
|
260
|
+
candidates.sort_by! { |ours, ref| connector_distance(ours, ref) }
|
|
261
|
+
used = {}.compare_by_identity
|
|
262
|
+
candidates.select do |ours, ref|
|
|
263
|
+
next false if used[ours] || used[ref] || !connector_distance(ours, ref).finite?
|
|
264
|
+
|
|
265
|
+
used[ours] = used[ref] = true
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
private :connector_pairs
|
|
129
269
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
end
|
|
133
|
-
private :centroid
|
|
270
|
+
def connector_distance(ours, ref)
|
|
271
|
+
return Float::INFINITY unless ours[:kind] == ref[:kind]
|
|
134
272
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
273
|
+
drifts = connector_delta(ours, ref).values_at(:source_delta, :target_delta)
|
|
274
|
+
return drifts.sum if drifts.min <= MATCH_DISTANCE
|
|
275
|
+
return drifts.sum if shape_distance(shift_primitive(ours), ref) <= MATCH_DISTANCE
|
|
276
|
+
return drifts.sum if drifts.sum <= [connector_length(ours), connector_length(ref)].max
|
|
139
277
|
|
|
140
|
-
|
|
141
|
-
|
|
278
|
+
Float::INFINITY
|
|
279
|
+
end
|
|
280
|
+
private :connector_distance
|
|
281
|
+
|
|
282
|
+
def connector_length(primitive)
|
|
283
|
+
primitive[:points].each_cons(2).sum { |first, last| point_distance(first, last) }
|
|
284
|
+
end
|
|
285
|
+
private :connector_length
|
|
286
|
+
|
|
287
|
+
def endpoint_distance(ours, ref)
|
|
288
|
+
return Float::INFINITY unless ours && ref
|
|
142
289
|
|
|
143
|
-
|
|
290
|
+
Math.hypot(ours[0] + @shift[0] - ref[0], ours[1] + @shift[1] - ref[1])
|
|
291
|
+
end
|
|
292
|
+
private :endpoint_distance
|
|
144
293
|
end
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
#
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
294
|
+
private_constant :ConnectorComparison
|
|
295
|
+
include ConnectorComparison
|
|
296
|
+
|
|
297
|
+
# EA emits triangle/diamond polygons and symmetric open V paths. The
|
|
298
|
+
# latter have their tip at the middle vertex, rather than at an end.
|
|
299
|
+
module ArrowheadComparison
|
|
300
|
+
def arrowheads(primitives)
|
|
301
|
+
primitives.select { |primitive| primitive[:kind] == :polygon || open_arrowhead?(primitive) }
|
|
302
|
+
end
|
|
303
|
+
private :arrowheads
|
|
304
|
+
|
|
305
|
+
def open_arrowhead?(primitive)
|
|
306
|
+
return false unless primitive[:kind] == :path && !primitive[:closed] && primitive[:points].size == 3
|
|
307
|
+
|
|
308
|
+
symmetric_wings?(*primitive[:points])
|
|
309
|
+
end
|
|
310
|
+
private :open_arrowhead?
|
|
311
|
+
|
|
312
|
+
def symmetric_wings?(first, tip, last)
|
|
313
|
+
left = point_distance(first, tip)
|
|
314
|
+
right = point_distance(last, tip)
|
|
315
|
+
base = point_distance(first, last)
|
|
316
|
+
# EA rounds each vertex to whole pixels, allowing up to sqrt(2)
|
|
317
|
+
# error per wing. Ordinary right-angle bends are not arrowheads.
|
|
318
|
+
(left - right).abs <= 2 * Math.sqrt(2) && base.positive? && base < [left, right].min
|
|
319
|
+
end
|
|
320
|
+
private :symmetric_wings?
|
|
321
|
+
|
|
322
|
+
def point_distance(first, last)
|
|
323
|
+
Math.hypot(first[0] - last[0], first[1] - last[1])
|
|
324
|
+
end
|
|
325
|
+
private :point_distance
|
|
326
|
+
|
|
327
|
+
def arrowhead_deltas(ours, ref, heads)
|
|
328
|
+
{ source: 0, target: -1 }.filter_map do |name, index|
|
|
329
|
+
pair = arrowhead_pair(ours[:points][index], ref[:points][index], heads)
|
|
330
|
+
next unless pair
|
|
331
|
+
|
|
332
|
+
our_head, ref_head, anchor, tip = pair
|
|
333
|
+
{ end: name, ours: our_head, reference: ref_head,
|
|
334
|
+
tip_delta: endpoint_distance(corresponding_tip(our_head, anchor, tip),
|
|
335
|
+
corresponding_tip(ref_head, anchor, tip)) }
|
|
162
336
|
end
|
|
163
|
-
|
|
337
|
+
end
|
|
338
|
+
private :arrowhead_deltas
|
|
339
|
+
|
|
340
|
+
def arrowhead_pair(our_point, ref_point, heads)
|
|
341
|
+
if (attachment = touching_arrowhead(@reference, ref_point))
|
|
342
|
+
ref, tip = attachment
|
|
343
|
+
ours = heads.find { |_, candidate| candidate.equal?(ref) }&.first
|
|
344
|
+
elsif (attachment = touching_arrowhead(@ours, our_point))
|
|
345
|
+
ours, tip = attachment
|
|
346
|
+
ref = heads.find { |candidate, _| candidate.equal?(ours) }&.last
|
|
347
|
+
else
|
|
348
|
+
return
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
[ours, ref, attachment.first, tip]
|
|
352
|
+
end
|
|
353
|
+
private :arrowhead_pair
|
|
354
|
+
|
|
355
|
+
# Removing each head's translation lets the reference tip identify
|
|
356
|
+
# its counterpart even when polygon vertices start at another corner
|
|
357
|
+
# or run in the opposite direction.
|
|
358
|
+
def corresponding_tip(head, anchor, index)
|
|
359
|
+
return unless head
|
|
360
|
+
return head[:points][1] if head[:kind] == :path
|
|
361
|
+
|
|
362
|
+
offset = centered_point(anchor[:points][index], anchor)
|
|
363
|
+
head[:points].min_by { |point| point_distance(centered_point(point, head), offset) }
|
|
364
|
+
end
|
|
365
|
+
private :corresponding_tip
|
|
164
366
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
deltas[:wrong_size] << [ours, ref] if ours[:size] != ref[:size]
|
|
168
|
-
deltas[:wrong_weight] << [ours, ref] if normalize_weight(ours[:weight]) != normalize_weight(ref[:weight])
|
|
367
|
+
def centered_point(point, primitive)
|
|
368
|
+
[point[0] - primitive[:centroid][0], point[1] - primitive[:centroid][1]]
|
|
169
369
|
end
|
|
370
|
+
private :centered_point
|
|
170
371
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
372
|
+
def touching_arrowhead(primitives, endpoint)
|
|
373
|
+
vertices = arrowheads(primitives).flat_map do |head|
|
|
374
|
+
indices = head[:kind] == :path ? [1] : head[:points].each_index
|
|
375
|
+
indices.map { |index| [head, index] }
|
|
376
|
+
end
|
|
377
|
+
nearest = vertices.min_by { |polygon, index| vertex_distance(polygon, index, endpoint) }
|
|
378
|
+
nearest if nearest && vertex_distance(*nearest, endpoint) <= EPSILON
|
|
379
|
+
end
|
|
380
|
+
private :touching_arrowhead
|
|
381
|
+
|
|
382
|
+
def vertex_distance(polygon, index, endpoint)
|
|
383
|
+
point = polygon[:points][index]
|
|
384
|
+
Math.hypot(point[0] - endpoint[0], point[1] - endpoint[1])
|
|
385
|
+
end
|
|
386
|
+
private :vertex_distance
|
|
387
|
+
end
|
|
388
|
+
private_constant :ArrowheadComparison
|
|
389
|
+
include ArrowheadComparison
|
|
390
|
+
|
|
391
|
+
def parse(svg)
|
|
392
|
+
PrimitiveParser.new.parse(svg)
|
|
174
393
|
end
|
|
394
|
+
private :parse
|
|
175
395
|
|
|
176
|
-
# Shapes pair greedily by centroid distance
|
|
177
|
-
#
|
|
178
|
-
# agree within epsilon (routing bend-level diffs need the
|
|
179
|
-
# point-level comparison and are reported as count mismatches).
|
|
396
|
+
# Shapes pair greedily by shift-corrected centroid distance
|
|
397
|
+
# within the same kind. Geometry deltas do not affect these counts.
|
|
180
398
|
def shape_summary
|
|
181
|
-
|
|
182
|
-
.map { |p| shift_primitive(p) }
|
|
399
|
+
our_count = @ours.count { |p| p[:kind] != :text }
|
|
183
400
|
ref_shapes = @reference.reject { |p| p[:kind] == :text }
|
|
401
|
+
matched = shape_pairs(ref_shapes).size
|
|
184
402
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
403
|
+
{
|
|
404
|
+
our_shapes: our_count, ref_shapes: ref_shapes.size + matched,
|
|
405
|
+
matched: matched, missing: ref_shapes.size, extra: our_count - matched
|
|
406
|
+
}
|
|
407
|
+
end
|
|
189
408
|
|
|
190
|
-
|
|
191
|
-
|
|
409
|
+
# Signed rectangle deltas are shifted ours minus reference.
|
|
410
|
+
# Primitives retain their original coordinates; polygon vertices
|
|
411
|
+
# correspond by index, with unequal counts reported as infinity.
|
|
412
|
+
def shape_deltas
|
|
413
|
+
shape_pairs.filter_map do |ours, ref|
|
|
414
|
+
delta = case ours[:kind]
|
|
415
|
+
when :rect
|
|
416
|
+
rect_deltas(ours, ref)
|
|
417
|
+
when :polygon
|
|
418
|
+
{ max_point_delta: max_point_delta(ours[:points], ref[:points]) }
|
|
419
|
+
end
|
|
420
|
+
{ kind: ours[:kind], ours: ours, reference: ref }.merge(delta) if delta
|
|
192
421
|
end
|
|
422
|
+
end
|
|
193
423
|
|
|
424
|
+
def rect_deltas(ours, ref)
|
|
194
425
|
{
|
|
195
|
-
|
|
196
|
-
|
|
426
|
+
dx: dimension_delta(ours, ref, :x) + @shift[0],
|
|
427
|
+
dy: dimension_delta(ours, ref, :y) + @shift[1],
|
|
428
|
+
dwidth: dimension_delta(ours, ref, :width),
|
|
429
|
+
dheight: dimension_delta(ours, ref, :height)
|
|
197
430
|
}
|
|
198
431
|
end
|
|
432
|
+
private :rect_deltas
|
|
199
433
|
|
|
200
|
-
def
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
434
|
+
def dimension_delta(ours, ref, key)
|
|
435
|
+
ours[key].to_f - ref[key].to_f
|
|
436
|
+
end
|
|
437
|
+
private :dimension_delta
|
|
438
|
+
|
|
439
|
+
def shape_pairs(ref_shapes = @reference.reject { |p| p[:kind] == :text },
|
|
440
|
+
our_shapes = @ours.reject { |p| p[:kind] == :text })
|
|
441
|
+
our_shapes.filter_map do |ours|
|
|
442
|
+
shifted = shift_primitive(ours)
|
|
443
|
+
ref = ref_shapes.min_by { |c| shape_distance(shifted, c) }
|
|
444
|
+
next unless ref && shape_distance(shifted, ref) <= MATCH_DISTANCE
|
|
445
|
+
|
|
446
|
+
ref_shapes.delete(ref)
|
|
447
|
+
[ours, ref]
|
|
205
448
|
end
|
|
206
449
|
end
|
|
207
|
-
private :
|
|
450
|
+
private :shape_pairs
|
|
208
451
|
|
|
209
|
-
def
|
|
210
|
-
return Float::INFINITY unless
|
|
211
|
-
return Float::INFINITY if a[:centroid].nil? || b[:centroid].nil?
|
|
452
|
+
def max_point_delta(ours, ref)
|
|
453
|
+
return Float::INFINITY unless ours.size == ref.size
|
|
212
454
|
|
|
213
|
-
|
|
214
|
-
|
|
455
|
+
ours.zip(ref).map do |(x, y), (ref_x, ref_y)|
|
|
456
|
+
Math.hypot(x + @shift[0] - ref_x, y + @shift[1] - ref_y)
|
|
457
|
+
end.max || 0.0
|
|
215
458
|
end
|
|
216
|
-
private :
|
|
459
|
+
private :max_point_delta
|
|
217
460
|
|
|
218
|
-
def
|
|
219
|
-
|
|
220
|
-
(
|
|
461
|
+
def shift_primitive(primitive)
|
|
462
|
+
if primitive[:centroid]
|
|
463
|
+
primitive.merge(centroid: [primitive[:centroid][0] + @shift[0], primitive[:centroid][1] + @shift[1]])
|
|
464
|
+
else
|
|
465
|
+
primitive
|
|
466
|
+
end
|
|
221
467
|
end
|
|
222
|
-
private :
|
|
468
|
+
private :shift_primitive
|
|
223
469
|
|
|
224
|
-
def
|
|
225
|
-
|
|
470
|
+
def shape_distance(ours, reference)
|
|
471
|
+
return Float::INFINITY unless ours[:kind] == reference[:kind]
|
|
472
|
+
return Float::INFINITY if ours[:centroid].nil? || reference[:centroid].nil?
|
|
473
|
+
|
|
474
|
+
ax, ay = ours[:centroid]
|
|
475
|
+
bx, by = reference[:centroid]
|
|
476
|
+
Math.sqrt(((ax - bx).abs**2) + ((ay - by).abs**2))
|
|
226
477
|
end
|
|
227
|
-
private :
|
|
478
|
+
private :shape_distance
|
|
228
479
|
end
|
|
229
480
|
end
|
|
230
481
|
end
|
data/lib/ea/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ea
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.148
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: lutaml-model
|