lutaml-diagram 0.1.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/.github/workflows/release.yml +39 -0
- data/.gitignore +5 -0
- data/Gemfile +13 -0
- data/README.adoc +205 -0
- data/exe/lutaml-lml +6 -0
- data/lib/lutaml/diagram/version.rb +7 -0
- data/lib/lutaml/diagram.rb +39 -0
- data/lib/lutaml/lml/cli.rb +489 -0
- data/lib/lutaml/lml/formatter/elk.rb +45 -0
- data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +89 -0
- data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +72 -0
- data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +89 -0
- data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +130 -0
- data/lib/lutaml/lml/formatter/graphviz.rb +93 -0
- data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +134 -0
- data/lib/lutaml/lml/formatter/svg_pdf.rb +285 -0
- data/lib/lutaml/lml/layout/elk_engine.rb +15 -0
- data/lib/lutaml/lml/layout/elk_graph_builder.rb +129 -0
- data/lib/lutaml/lml/layout/elk_svg_markers.rb +45 -0
- data/lib/lutaml/lml/layout/elk_svg_renderer.rb +119 -0
- data/lutaml-diagram.gemspec +40 -0
- metadata +108 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Formatter
|
|
5
|
+
# SVG-to-PDF renderer for ELK-layout diagrams (issue #53). Parses the
|
|
6
|
+
# ELK SVG output and re-emits it through pdfrb's Canvas: base-14
|
|
7
|
+
# standard fonts for text (metrics ship inside pdfrb, no external
|
|
8
|
+
# font resources), stroke/fill operators for shapes, and arrow
|
|
9
|
+
# markers drawn manually at polyline ends (SVG marker geometry is
|
|
10
|
+
# resolved from the document's <defs> and inlined with rotation).
|
|
11
|
+
class SvgPdf
|
|
12
|
+
autoload :PathParser, 'lutaml/lml/formatter/svg_pdf/path_parser'
|
|
13
|
+
|
|
14
|
+
FONT_VARIANTS = {
|
|
15
|
+
'Helvetica' => { bold: :Helvetica_Bold, normal: :Helvetica },
|
|
16
|
+
'Courier' => { bold: :Courier_Bold, normal: :Courier },
|
|
17
|
+
'Times' => { bold: :Times_Bold, normal: :Times_Roman }
|
|
18
|
+
}.freeze
|
|
19
|
+
ROUNDING = 0.5523
|
|
20
|
+
|
|
21
|
+
def initialize(svg)
|
|
22
|
+
@svg = svg
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def render
|
|
26
|
+
require 'pdfrb'
|
|
27
|
+
doc = Pdfrb::Document.new
|
|
28
|
+
page = doc.pages.add
|
|
29
|
+
@canvas = page.canvas
|
|
30
|
+
root = Moxml.parse(@svg).root
|
|
31
|
+
@width, @height = page_dimensions(root)
|
|
32
|
+
@canvas.line_width = 1
|
|
33
|
+
@markers = {}
|
|
34
|
+
@marker_parents = {}
|
|
35
|
+
collect_markers(root, nil)
|
|
36
|
+
walk(root, 0.0, 0.0)
|
|
37
|
+
io = StringIO.new
|
|
38
|
+
doc.write(io: io)
|
|
39
|
+
io.string.b
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def page_dimensions(root)
|
|
45
|
+
[float_attr(root, 'width', 612.0), float_attr(root, 'height', 792.0)]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def float_attr(node, name, fallback)
|
|
49
|
+
node.attribute(name)&.value&.to_f || fallback
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def collect_markers(node, _parent)
|
|
53
|
+
node.children.each do |child|
|
|
54
|
+
if child.name == 'marker'
|
|
55
|
+
shape = child.children.find { |c| %w[path polygon polyline].include?(c.name) }
|
|
56
|
+
if shape
|
|
57
|
+
@markers[child.attribute('id')&.value] = shape
|
|
58
|
+
@marker_parents[shape] = child
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
collect_markers(child, node)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Depth-first walk applying cumulative translate(x,y).
|
|
66
|
+
def walk(node, dx, dy)
|
|
67
|
+
node.children.each do |child|
|
|
68
|
+
# Whitespace between elements arrives as text nodes under
|
|
69
|
+
# some moxml adapters; only elements carry geometry.
|
|
70
|
+
next unless child.element?
|
|
71
|
+
|
|
72
|
+
cdx, cdy = translate_of(child)
|
|
73
|
+
case child.name
|
|
74
|
+
when 'g'
|
|
75
|
+
walk(child, dx + cdx, dy + cdy)
|
|
76
|
+
when 'rect'
|
|
77
|
+
draw_rect(child, dx + cdx, dy + cdy)
|
|
78
|
+
when 'polyline'
|
|
79
|
+
draw_polyline(child, dx, dy)
|
|
80
|
+
when 'polygon'
|
|
81
|
+
draw_polygon(child, dx, dy)
|
|
82
|
+
when 'line'
|
|
83
|
+
draw_line(child, dx, dy)
|
|
84
|
+
when 'path'
|
|
85
|
+
draw_path(child, dx + cdx, dy + cdy)
|
|
86
|
+
when 'text'
|
|
87
|
+
draw_text(child, dx + cdx, dy + cdy)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def translate_of(node)
|
|
93
|
+
t = node.attribute('transform')&.value.to_s
|
|
94
|
+
m = t.match(/translate\(\s*([-\d.]+)[,\s]+([-\d.]+)\s*\)/)
|
|
95
|
+
m ? [m[1].to_f, m[2].to_f] : [0.0, 0.0]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# SVG user space is y-down; PDF is y-up.
|
|
99
|
+
def pt(x, y)
|
|
100
|
+
[x, @height - y]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def draw_rect(node, dx, dy)
|
|
104
|
+
x = float_attr(node, 'x', 0.0) + dx
|
|
105
|
+
y = float_attr(node, 'y', 0.0) + dy
|
|
106
|
+
w = float_attr(node, 'width', 0.0)
|
|
107
|
+
h = float_attr(node, 'height', 0.0)
|
|
108
|
+
rx = float_attr(node, 'rx', 0.0)
|
|
109
|
+
set_fill(node)
|
|
110
|
+
set_stroke(node)
|
|
111
|
+
if rx.positive?
|
|
112
|
+
rounded_path(x, y, w, h, rx)
|
|
113
|
+
else
|
|
114
|
+
@canvas.rectangle(*pt(x, y), w, h)
|
|
115
|
+
end
|
|
116
|
+
paint(node)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def rounded_path(x, y, w, h, r)
|
|
120
|
+
r = [r, w / 2.0, h / 2.0].min
|
|
121
|
+
k = ROUNDING * r
|
|
122
|
+
_, y2 = pt(x + w, y + h)
|
|
123
|
+
top_left, = pt(x, y)
|
|
124
|
+
@canvas.move_to(*pt(x + r, y))
|
|
125
|
+
@canvas.line_to(*pt(x + w - r, y))
|
|
126
|
+
@canvas.curve_to(x + w - r + k, y, x + w, y + r - k, x + w, y + r)
|
|
127
|
+
@canvas.line_to(x + w, y + h - r)
|
|
128
|
+
@canvas.curve_to(x + w, y + h - r + k, x + w - r + k, y + h, x + w - r, y2)
|
|
129
|
+
@canvas.line_to(x + r, y2)
|
|
130
|
+
@canvas.curve_to(x + r - k, y + h, x, y + h - r + k, x, y + h - r)
|
|
131
|
+
@canvas.line_to(x, y + r)
|
|
132
|
+
@canvas.curve_to(x, y + r - k, x + r - k, y, x + r, top_left)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def draw_polyline(node, dx, dy)
|
|
136
|
+
points = point_list(node, dx, dy)
|
|
137
|
+
return if points.empty?
|
|
138
|
+
|
|
139
|
+
set_stroke(node)
|
|
140
|
+
@canvas.line_width = float_attr(node, 'stroke-width', 1.0)
|
|
141
|
+
@canvas.polyline(points.map { |x, y| pt(x, y) })
|
|
142
|
+
@canvas.stroke
|
|
143
|
+
draw_marker(node, points)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def draw_polygon(node, dx, dy)
|
|
147
|
+
points = point_list(node, dx, dy)
|
|
148
|
+
return if points.empty?
|
|
149
|
+
|
|
150
|
+
set_fill(node)
|
|
151
|
+
set_stroke(node)
|
|
152
|
+
@canvas.polygon(points.map { |x, y| pt(x, y) })
|
|
153
|
+
paint(node)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def draw_line(node, dx, dy)
|
|
157
|
+
x1 = float_attr(node, 'x1', 0.0) + dx
|
|
158
|
+
y1 = float_attr(node, 'y1', 0.0) + dy
|
|
159
|
+
x2 = float_attr(node, 'x2', 0.0) + dx
|
|
160
|
+
y2 = float_attr(node, 'y2', 0.0) + dy
|
|
161
|
+
set_stroke(node)
|
|
162
|
+
@canvas.line(*pt(x1, y1), *pt(x2, y2))
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def point_list(node, dx, dy)
|
|
166
|
+
node.attribute('points')&.value.to_s.scan(/(-?[\d.]+)[,\s]+(-?[\d.]+)/)
|
|
167
|
+
.map { |x, y| [x.to_f + dx, y.to_f + dy] }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def draw_path(node, dx, dy)
|
|
171
|
+
subpaths = PathParser.new(node.attribute('d')&.value.to_s, dx, dy).subpaths
|
|
172
|
+
set_fill(node)
|
|
173
|
+
set_stroke(node)
|
|
174
|
+
subpaths.each do |segments|
|
|
175
|
+
first = segments.first
|
|
176
|
+
next unless first
|
|
177
|
+
|
|
178
|
+
@canvas.move_to(*pt(*first.last_point))
|
|
179
|
+
segments.each do |segment|
|
|
180
|
+
case segment
|
|
181
|
+
when PathParser::Line
|
|
182
|
+
@canvas.line_to(*pt(*segment.point))
|
|
183
|
+
when PathParser::Curve
|
|
184
|
+
c1 = pt(*segment.c1)
|
|
185
|
+
c2 = pt(*segment.c2)
|
|
186
|
+
to = pt(*segment.point)
|
|
187
|
+
@canvas.curve_to(*c1, *c2, *to)
|
|
188
|
+
when PathParser::Close
|
|
189
|
+
@canvas.close_path
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
paint(node)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def draw_marker(node, points)
|
|
197
|
+
ref = node.attribute('marker-end')&.value.to_s[/url\(#([^)]+)\)/, 1]
|
|
198
|
+
shape = @markers[ref]
|
|
199
|
+
return unless shape && points.size >= 2
|
|
200
|
+
|
|
201
|
+
(x1, y1), (x2, y2) = points[-2, 2]
|
|
202
|
+
angle = Math.atan2(y2 - y1, x2 - x1)
|
|
203
|
+
cos = Math.cos(angle)
|
|
204
|
+
sin = Math.sin(angle)
|
|
205
|
+
marker = @marker_parents[shape]
|
|
206
|
+
ref_x = float_attr(marker, 'refX', 0.0)
|
|
207
|
+
ref_y = float_attr(marker, 'refY', 0.0)
|
|
208
|
+
set_fill(shape)
|
|
209
|
+
set_stroke(shape)
|
|
210
|
+
trace_marker(shape, x2, y2, cos, sin, ref_x, ref_y)
|
|
211
|
+
paint(shape)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Trace the marker's shape geometry translated to (x2,y2) with the
|
|
215
|
+
# marker origin at (refX,refY), rotated by the edge direction.
|
|
216
|
+
def trace_marker(shape, ox, oy, cos, sin, ref_x, ref_y)
|
|
217
|
+
transform = lambda { |x, y|
|
|
218
|
+
lx = x - ref_x
|
|
219
|
+
ly = y - ref_y
|
|
220
|
+
[ox + lx * cos - ly * sin, oy + lx * sin + ly * cos]
|
|
221
|
+
}
|
|
222
|
+
d = shape.name == 'path' ? shape.attribute('d')&.value.to_s : nil
|
|
223
|
+
segments = d ? PathParser.new(d, 0.0, 0.0).subpaths.first : nil
|
|
224
|
+
return unless segments&.any?
|
|
225
|
+
|
|
226
|
+
first = segments.first.last_point
|
|
227
|
+
@canvas.move_to(*pt(*transform.call(*first)))
|
|
228
|
+
segments.each do |segment|
|
|
229
|
+
case segment
|
|
230
|
+
when PathParser::Line
|
|
231
|
+
@canvas.line_to(*pt(*transform.call(*segment.point)))
|
|
232
|
+
when PathParser::Curve
|
|
233
|
+
@canvas.curve_to(*pt(*transform.call(*segment.c1)),
|
|
234
|
+
*pt(*transform.call(*segment.c2)),
|
|
235
|
+
*pt(*transform.call(*segment.point)))
|
|
236
|
+
when PathParser::Close
|
|
237
|
+
@canvas.close_path
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def draw_text(node, dx, dy)
|
|
243
|
+
x = float_attr(node, 'x', 0.0) + dx
|
|
244
|
+
y = float_attr(node, 'y', 0.0) + dy
|
|
245
|
+
size = float_attr(node, 'font-size', 12.0)
|
|
246
|
+
@canvas.text(node.text.to_s, at: pt(x, y),
|
|
247
|
+
font: font_for(node), size: size)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def font_for(node)
|
|
251
|
+
family = node.attribute('font-family')&.value.to_s
|
|
252
|
+
base = FONT_VARIANTS.keys.find { |k| family.include?(k) } || 'Helvetica'
|
|
253
|
+
bold = node.attribute('font-weight')&.value.to_s =~ /bold/i
|
|
254
|
+
FONT_VARIANTS[base][bold ? :bold : :normal]
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def set_fill(node)
|
|
258
|
+
@fill_color = color_attr(node, 'fill')
|
|
259
|
+
@canvas.fill_color(@fill_color) if @fill_color
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def set_stroke(node)
|
|
263
|
+
@stroke_color = color_attr(node, 'stroke')
|
|
264
|
+
@canvas.stroke_color(@stroke_color) if @stroke_color
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def paint(node)
|
|
268
|
+
fill = color_attr(node, 'fill')
|
|
269
|
+
stroke = color_attr(node, 'stroke')
|
|
270
|
+
if fill && fill != 'none' && stroke && stroke != 'none'
|
|
271
|
+
@canvas.fill_stroke
|
|
272
|
+
elsif fill && fill != 'none'
|
|
273
|
+
@canvas.fill
|
|
274
|
+
else
|
|
275
|
+
@canvas.stroke
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def color_attr(node, name)
|
|
280
|
+
value = node.attribute(name)&.value.to_s
|
|
281
|
+
value =~ /\A#/ ? value : nil
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Layout
|
|
5
|
+
# In-process layout engine backed by elkrb (pure-Ruby ELK). The input
|
|
6
|
+
# is an already-built Elkrb::Graph; rendering produces standalone SVG.
|
|
7
|
+
class ElkEngine < Engine
|
|
8
|
+
def render(_type)
|
|
9
|
+
require 'elkrb'
|
|
10
|
+
Elkrb::Layout::LayoutEngine.layout(@input, algorithm: 'layered')
|
|
11
|
+
ElkSvgRenderer.new.render(@input)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Layout
|
|
5
|
+
# Builds an Elkrb::Graph: a node per classifier (name, definition,
|
|
6
|
+
# members); an edge per association and parent specializer.
|
|
7
|
+
class ElkGraphBuilder
|
|
8
|
+
CHAR_WIDTH = 7.2
|
|
9
|
+
LINE_HEIGHT = 16.0
|
|
10
|
+
PADDING = 14.0
|
|
11
|
+
MIN_WIDTH = 120.0
|
|
12
|
+
COLLECTIONS = %i[classes enums primitives data_types].freeze
|
|
13
|
+
def build(document)
|
|
14
|
+
graph = elkrb_graph
|
|
15
|
+
add_entities(graph, document)
|
|
16
|
+
add_association_edges(graph, document)
|
|
17
|
+
add_parent_edges(graph, document)
|
|
18
|
+
graph
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def elkrb_graph
|
|
24
|
+
require 'elkrb'
|
|
25
|
+
Elkrb::Graph::Graph.new(id: 'diagram')
|
|
26
|
+
rescue LoadError
|
|
27
|
+
raise Error, 'The elkrb gem is required for ELK layout: add `gem "elkrb"` to your Gemfile'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def entities(document)
|
|
31
|
+
([document] + Array(document.packages)).flat_map { |scope| scope_entities(scope) }.flatten.compact
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def scope_entities(scope)
|
|
35
|
+
COLLECTIONS.filter_map do |collection|
|
|
36
|
+
next unless scope.respond_to?(collection)
|
|
37
|
+
|
|
38
|
+
scope.public_send(collection)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def node_for(entity)
|
|
43
|
+
lines = label_lines(entity)
|
|
44
|
+
Elkrb::Graph::Node.new(
|
|
45
|
+
id: entity_id_for(entity.name),
|
|
46
|
+
width: node_width(lines),
|
|
47
|
+
height: PADDING + lines.size * LINE_HEIGHT,
|
|
48
|
+
labels: lines.each_with_index.map do |line, i|
|
|
49
|
+
Elkrb::Graph::Label.new(text: line, x: 0, y: i * LINE_HEIGHT)
|
|
50
|
+
end
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def label_lines(entity)
|
|
55
|
+
lines = [entity.name.to_s]
|
|
56
|
+
if entity.respond_to?(:definition) && !entity.definition.to_s.empty?
|
|
57
|
+
lines << entity.definition.to_s.gsub("\n", ' ')
|
|
58
|
+
end
|
|
59
|
+
Array(entity.attributes).each do |attr|
|
|
60
|
+
lines << attr_line(attr)
|
|
61
|
+
end
|
|
62
|
+
lines
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def attr_line(attr)
|
|
66
|
+
line = "+ #{attr.name}"
|
|
67
|
+
line += ": #{attr.type}" if attr.type && !attr.type.to_s.empty?
|
|
68
|
+
line
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def node_width(lines)
|
|
72
|
+
widest = lines.map(&:length).max || 0
|
|
73
|
+
[widest * CHAR_WIDTH + PADDING, MIN_WIDTH].max
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def entity_id(entity)
|
|
77
|
+
entity.name.to_s.gsub(/[^0-9a-zA-Z_]/, '_')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def associations(document)
|
|
81
|
+
Array(document.associations)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def parent_edges(document)
|
|
85
|
+
Array(document.classes).filter_map do |klass|
|
|
86
|
+
next unless klass.respond_to?(:parent_class) && klass.parent_class
|
|
87
|
+
|
|
88
|
+
{ owner: klass.parent_class, member: klass.name.to_s,
|
|
89
|
+
member_end_type: 'generalization', name: nil }
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def add_entities(graph, document)
|
|
94
|
+
entities(document).each { |entity| graph.children << node_for(entity) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def add_association_edges(graph, document)
|
|
98
|
+
associations(document).each { |assoc| graph.edges << edge_for(association_spec(assoc)) }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def add_parent_edges(graph, document)
|
|
102
|
+
parent_edges(document).each { |edge| graph.edges << edge_for(edge) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def edge_for(spec)
|
|
106
|
+
edge = elkrb_edge(spec)
|
|
107
|
+
edge.properties = { 'member_end_type' => spec[:member_end_type].to_s }
|
|
108
|
+
edge.labels = [Elkrb::Graph::Label.new(text: spec[:name].to_s)] if spec[:name]
|
|
109
|
+
edge
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def elkrb_edge(spec)
|
|
113
|
+
require 'elkrb'
|
|
114
|
+
Elkrb::Graph::Edge.new(
|
|
115
|
+
id: "edge_#{spec[:owner]}_#{spec[:member]}",
|
|
116
|
+
sources: [entity_id_for(spec[:owner])],
|
|
117
|
+
targets: [entity_id_for(spec[:member])]
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def association_spec(assoc)
|
|
122
|
+
{ owner: assoc.owner_end.to_s, member: assoc.member_end.to_s,
|
|
123
|
+
member_end_type: assoc.member_end_type.to_s, name: assoc.name }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def entity_id_for(name) = name.to_s.gsub(/[^0-9a-zA-Z_]/, '_')
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Layout
|
|
5
|
+
# SVG arrow-marker vocabulary and the <defs> block for ElkSvgRenderer.
|
|
6
|
+
# Marker ids are keyed by association member_end_type.
|
|
7
|
+
module ElkSvgMarkers
|
|
8
|
+
EDGE_STROKE = '#666666'
|
|
9
|
+
|
|
10
|
+
ARROW_MARKERS = {
|
|
11
|
+
'association' => 'arrow',
|
|
12
|
+
'aggregation' => 'diamond',
|
|
13
|
+
'composition' => 'diamond_filled',
|
|
14
|
+
'generalization' => 'triangle',
|
|
15
|
+
'uses' => 'arrow',
|
|
16
|
+
'direct' => 'arrow'
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
MARKER_SHAPES = {
|
|
20
|
+
'arrow' => %(<path d="M 0 0 L 10 5 L 0 10 z" fill="#{EDGE_STROKE}"/>),
|
|
21
|
+
'triangle' => %(<path d="M 0 0 L 12 5 L 0 10 z" fill="white" stroke="#{EDGE_STROKE}"/>),
|
|
22
|
+
'diamond' => %(<path d="M 0 4 L 7 0 L 14 4 L 7 8 z" fill="white" stroke="#{EDGE_STROKE}"/>),
|
|
23
|
+
'diamond_filled' => %(<path d="M 0 4 L 7 0 L 14 4 L 7 8 z" fill="#{EDGE_STROKE}"/>)
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
def arrow_marker(edge)
|
|
27
|
+
edge_type = edge.properties&.fetch('member_end_type', nil)
|
|
28
|
+
ARROW_MARKERS.fetch(edge_type.to_s, 'arrow')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def defs
|
|
32
|
+
MARKER_SHAPES.filter_map { |id, path| marker_def(id, path) }.join("\n")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def marker_def(id, path)
|
|
38
|
+
w, h = id.start_with?('diamond') ? %w[14 8] : %w[10 10]
|
|
39
|
+
%(<marker id="#{id}" viewBox="0 0 #{w} #{h}" refX="9" refY="4" ) +
|
|
40
|
+
%(markerWidth="#{w}" markerHeight="#{h}" ) +
|
|
41
|
+
%(orient="auto-start-reverse">#{path}</marker>)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Layout
|
|
5
|
+
# Renders a laid-out Elkrb::Graph (nodes positioned by the layout) as
|
|
6
|
+
# standalone SVG: rounded-rect nodes with text-line labels and
|
|
7
|
+
# polyline edges with per-type arrow markers.
|
|
8
|
+
class ElkSvgRenderer
|
|
9
|
+
include ElkSvgMarkers
|
|
10
|
+
|
|
11
|
+
EDGE_STROKE = ElkSvgMarkers::EDGE_STROKE
|
|
12
|
+
|
|
13
|
+
HEADER = <<~XML
|
|
14
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
15
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%<width>s" height="%<height>s" viewBox="0 0 %<width>s %<height>s">
|
|
16
|
+
XML
|
|
17
|
+
NODE_FILL = '#f8f8f8'
|
|
18
|
+
NODE_STROKE = '#333333'
|
|
19
|
+
|
|
20
|
+
def render(graph)
|
|
21
|
+
@graph = graph
|
|
22
|
+
@out = +''
|
|
23
|
+
@out << format(HEADER, width: bounds_width, height: bounds_height)
|
|
24
|
+
@out << "<defs>\n#{defs}\n</defs>\n"
|
|
25
|
+
nodes
|
|
26
|
+
edges
|
|
27
|
+
@out << "</svg>\n"
|
|
28
|
+
@out
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def nodes
|
|
34
|
+
@graph.children.each { |node| render_node(node) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def render_node(node)
|
|
38
|
+
@out << %(<g transform="translate(#{node.x.to_i},#{node.y.to_i})">)
|
|
39
|
+
@out << node_rect(node)
|
|
40
|
+
node.labels.each_with_index { |label, i| @out << node_label(label, i) }
|
|
41
|
+
@out << '</g>'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def node_rect(node)
|
|
45
|
+
%(<rect width="#{node.width.to_i}" height="#{node.height.to_i}" rx="6" ) +
|
|
46
|
+
%(fill="#{NODE_FILL}" stroke="#{NODE_STROKE}"/>)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def node_label(label, index)
|
|
50
|
+
y = ((index + 0.7) * ElkGraphBuilder::LINE_HEIGHT).round(1)
|
|
51
|
+
weight = index.zero? ? ' font-weight="bold"' : ''
|
|
52
|
+
%(<text x="7" y="#{y}" font-family="Helvetica" font-size="12"#{weight}>#{escape(label.text)}</text>)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def edges
|
|
56
|
+
@graph.edges.each { |edge| render_edge(edge) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def render_edge(edge)
|
|
60
|
+
points = edge_points(edge)
|
|
61
|
+
return if points.empty?
|
|
62
|
+
|
|
63
|
+
@out << edge_polyline(edge, points)
|
|
64
|
+
edge_label(edge, points)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def edge_polyline(edge, points)
|
|
68
|
+
pts = points.map { |p| "#{p.x.round(1)},#{p.y.round(1)}" }.join(' ')
|
|
69
|
+
%(<polyline fill="none" stroke="#{EDGE_STROKE}" marker-end="url(##{arrow_marker(edge)})" points="#{pts}"/>)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def edge_label(edge, points)
|
|
73
|
+
label = Array(edge.labels).first
|
|
74
|
+
return unless label&.text
|
|
75
|
+
|
|
76
|
+
@out << edge_label_text(label, edge_anchor(points))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def edge_anchor(points)
|
|
80
|
+
mid = points[points.size / 2]
|
|
81
|
+
{ x: (mid.x + 4).round(1), y: (mid.y - 4).round(1) }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def edge_label_text(label, anchor)
|
|
85
|
+
%(<text x="#{anchor[:x]}" y="#{anchor[:y]}" font-family="Helvetica" ) +
|
|
86
|
+
%(font-size="10" fill="#{EDGE_STROKE}">#{escape(label.text)}</text>)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def edge_points(edge)
|
|
90
|
+
sections = Array(edge.sections)
|
|
91
|
+
return [] if sections.empty?
|
|
92
|
+
|
|
93
|
+
points = [sections.first.start_point]
|
|
94
|
+
sections.each do |section|
|
|
95
|
+
points.concat(Array(section.bend_points))
|
|
96
|
+
points << section.end_point
|
|
97
|
+
end
|
|
98
|
+
points.compact
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def bounds_width
|
|
102
|
+
extent(:x, :width) + 20
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def bounds_height
|
|
106
|
+
extent(:y, :height) + 20
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def extent(pos, size)
|
|
110
|
+
values = @graph.children.map { |n| n.public_send(pos).to_f + n.public_send(size).to_f }
|
|
111
|
+
[values.max || 0, 0].max.round
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def escape(text)
|
|
115
|
+
text.to_s.gsub('&', '&').gsub('<', '<').gsub('>', '>')
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require "lutaml/diagram/version"
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = "lutaml-diagram"
|
|
9
|
+
spec.version = Lutaml::Diagram::VERSION
|
|
10
|
+
spec.authors = ["Ribose Inc."]
|
|
11
|
+
spec.email = ["open.source@ribose.com"]
|
|
12
|
+
|
|
13
|
+
spec.summary = "Diagram rendering for the LutaML Model Language"
|
|
14
|
+
spec.description = "Graphviz, ELK and SVG/PDF render backends, layout " \
|
|
15
|
+
"engines and the lutaml-lml CLI for the LutaML Model " \
|
|
16
|
+
"Language. The language core lives in lutaml-model."
|
|
17
|
+
spec.homepage = "https://github.com/lutaml/lutaml-diagram"
|
|
18
|
+
spec.license = "BSD-2-Clause"
|
|
19
|
+
|
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
21
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
22
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
23
|
+
|
|
24
|
+
# The LML language core (parser, model compiler, executor, LML seam
|
|
25
|
+
# namespaces) ships in lutaml-model from 0.8.188 (the lutaml-lml
|
|
26
|
+
# subsume); this gem reopens Lutaml::Formatter and Lutaml::Layout
|
|
27
|
+
# with the render backends and ships the CLI.
|
|
28
|
+
spec.add_dependency "lutaml-model", "~> 0.8.188"
|
|
29
|
+
spec.add_dependency "elkrb", "~> 2.0"
|
|
30
|
+
spec.add_dependency "thor"
|
|
31
|
+
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
34
|
+
f.match(%r{^(test|spec|features)/})
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
spec.bindir = "exe"
|
|
38
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
39
|
+
spec.require_paths = ["lib"]
|
|
40
|
+
end
|