forked from skanaar/nomnoml
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnomnoml.renderer.js
More file actions
184 lines (164 loc) · 5.48 KB
/
nomnoml.renderer.js
File metadata and controls
184 lines (164 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var nomnoml = nomnoml || {}
nomnoml.render = function (graphics, config, compartment, setFont){
var padding = config.padding
var g = graphics
var vm = skanaar.vector
function renderCompartment(compartment, style, level){
g.save()
g.translate(padding, padding)
g.fillStyle(config.stroke)
_.each(compartment.lines, function (text, i){
g.textAlign(style.center ? 'center' : 'left')
var x = style.center ? compartment.width/2 - padding : 0
var y = (0.5+(i+0.5)*config.leading)*config.fontSize
if (text){
g.fillText(text, x, y)
}
if (style.underline){
var w = g.measureText(text).width
y += Math.round(config.fontSize * 0.2)+0.5
g.path([{x:x-w/2, y:y}, {x:x+w/2, y:y}]).stroke()
g.lineWidth = config.lineWidth
}
})
g.translate(config.gutter, config.gutter)
_.each(compartment.relations, function (r){ renderRelation(r, compartment) })
_.each(compartment.nodes, function (n){ renderNode(n, level) })
g.restore()
}
function renderNode(node, level){
var x = Math.round(node.x-node.width/2)
var y = Math.round(node.y-node.height/2)
var style = config.styles[node.type] || nomnoml.styles.CLASS
g.fillStyle(style.fill || config.fill[level] || _.last(config.fill))
if (style.dashed){
var dash = Math.max(4, 2*config.lineWidth)
g.setLineDash([dash, dash])
}
var drawNode = nomnoml.visualizers[style.visual] || nomnoml.visualizers.class
drawNode(node, x, y, padding, config, g)
g.setLineDash([])
var yDivider = (style.visual === 'actor' ? y + padding*3/4 : y)
_.each(node.compartments, function (part, i){
var s = i > 0 ? {} : style; // only style node title
if (s.empty) return
g.save()
g.translate(x, yDivider)
setFont(config, s.bold ? 'bold' : 'normal', s.italic)
renderCompartment(part, s, level+1)
g.restore()
if (i+1 === node.compartments.length) return
yDivider += part.height
if (style.visual === 'frame' && i === 0){
var w = g.measureText(node.name).width+part.height/2+padding
g.path([
{x:x, y:yDivider},
{x:x+w-part.height/2, y:yDivider},
{x:x+w, y:yDivider-part.height/2},
{x:x+w, y:yDivider-part.height}
]).stroke()
} else
g.path([{x:x, y:yDivider}, {x:x+node.width, y:yDivider}]).stroke()
})
}
function strokePath(p){
if (config.edges === 'rounded'){
var radius = config.spacing * config.bendSize
g.beginPath()
g.moveTo(p[0].x, p[0].y)
for (var i = 1; i < p.length-1; i++){
g.arcTo(p[i].x, p[i].y, p[i+1].x, p[i+1].y, radius)
}
g.lineTo(_.last(p).x, _.last(p).y)
g.stroke()
}
else
g.path(p).stroke()
}
var empty = false, filled = true, diamond = true
function renderRelation(r, compartment){
var startNode = _.findWhere(compartment.nodes, {name:r.start})
var endNode = _.findWhere(compartment.nodes, {name:r.end})
var start = rectIntersection(r.path[1], _.first(r.path), startNode)
var end = rectIntersection(r.path[r.path.length-2], _.last(r.path), endNode)
var path = _.flatten([start, _.tail(_.initial(r.path)), end])
var fontSize = config.fontSize
g.fillStyle(config.stroke)
setFont(config, 'normal')
var textW = g.measureText(r.endLabel).width
var labelX = config.direction === 'LR' ? -padding-textW : padding
if (r.startLabel) g.fillText(r.startLabel, start.x+padding, start.y+padding+fontSize)
if (r.endLabel) g.fillText(r.endLabel, end.x+labelX, end.y-padding)
if (r.assoc !== '-/-'){
if (g.setLineDash && skanaar.hasSubstring(r.assoc, '--')){
var dash = Math.max(4, 2*config.lineWidth)
g.setLineDash([dash, dash])
strokePath(path)
g.setLineDash([])
}
else
strokePath(path)
}
function drawArrowEnd(id, path, end){
if (id === '>' || id === '<')
drawArrow(path, filled, end)
else if (id === ':>' || id === '<:')
drawArrow(path, empty, end)
else if (id === '+')
drawArrow(path, filled, end, diamond)
else if (id === 'o')
drawArrow(path, empty, end, diamond)
}
var tokens = r.assoc.split('-')
drawArrowEnd(_.last(tokens), path, end)
drawArrowEnd(_.first(tokens), path.reverse(), start)
}
function rectIntersection(p1, p2, rect) {
if (rect.width || rect.height) {
var xBound = rect.width/2 + config.edgeMargin;
var yBound = rect.height/2 + config.edgeMargin;
var delta = vm.diff(p1, p2);
var t;
if (delta.x && delta.y) {
t = Math.min(Math.abs(xBound/delta.x), Math.abs(yBound/delta.y));
} else {
t = Math.abs(delta.x ? xBound/delta.x : yBound/delta.y);
}
return vm.add(p2, vm.mult(delta, t));
}
return p2;
}
function drawArrow(path, isOpen, arrowPoint, diamond){
var size = (config.spacing - 2*config.edgeMargin) * config.arrowSize / 30
var v = vm.diff(path[path.length-2], _.last(path))
var nv = vm.normalize(v)
function getArrowBase(s){ return vm.add(arrowPoint, vm.mult(nv, s*size)) }
var arrowBase = getArrowBase(diamond ? 7 : 10)
var t = vm.rot(nv)
var arrowButt = (diamond) ? getArrowBase(14)
: (isOpen && !config.fillArrows) ? getArrowBase(5) : arrowBase
var arrow = [
vm.add(arrowBase, vm.mult(t, 4*size)),
arrowButt,
vm.add(arrowBase, vm.mult(t, -4*size)),
arrowPoint
]
g.fillStyle(isOpen ? config.stroke : config.fill[0])
g.circuit(arrow).fillAndStroke()
}
function snapToPixels(){
if (config.lineWidth % 2 === 1)
g.translate(0.5, 0.5)
}
g.clear()
setFont(config, 'bold')
g.save()
g.lineWidth(config.lineWidth)
g.lineJoin('round')
g.lineCap('round')
g.strokeStyle(config.stroke)
g.scale(config.zoom, config.zoom)
snapToPixels()
renderCompartment(compartment, {}, 0)
g.restore()
}