This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
diagram.ts
213 lines (197 loc) · 8.8 KB
/
diagram.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2022
// Carlos Alberto Ruiz Naranjo [[email protected]]
// Ismael Perez Rojo [[email protected] ]
//
// This file is part of colibri2
//
// Colibri is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Colibri is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with colibri2. If not, see <https://www.gnu.org/licenses/>.
import * as common_hdl from "../parser/common";
import * as doxygen from "./doxygen_parser";
const INPUT_LABEL_LIST = ['in', 'input'];
const OUTPUT_LABEL_LIST = ['out', 'output', 'inout', 'buffer'];
export type Diagram_options = {
blackandwhite: boolean;
};
export function diagram_generator(structure: common_hdl.Hdl_element, opt: Diagram_options): string {
const window = require('svgdom');
const SVG = require('svg.js')(window);
const document = window.document;
const canvas = SVG(document.documentElement);
canvas.clear();
let border = 'black';
let genBox = '#bdecb6'; //'blue'
let portBox = '#fdfd96'; //'red'
let locx = 200;
let locy = 0;
let width = 100;
let high = 100;
let total_high = 100;
const size = 20;
const font = 'Helvetica';
const offset = 10;
const text_offset = 7;
const text_space = 15;
const text_space_pin = text_space / 3;
const separator = 10;
const name = 0;
const kind = 1;
let generics: any = [[], []];
let in_ports: any = [[], []];
let out_ports: any = [[], []];
if (opt.blackandwhite) {
border = 'black';
genBox = 'white';
portBox = 'white';
}
generics = get_generics(structure, name, kind);
in_ports = get_ports_in(structure, name, kind);
out_ports = get_ports_out(structure, name, kind);
locx = (size / 2) * max_string(generics, in_ports, [0, 0], kind) + 2 * offset;
width = (size / 2) * (max_string(generics, in_ports, [0, 0], name) + max_string([0, 0], [0, 0], out_ports, name));
if ((generics[0].length === 0 && in_ports[0].length === 0) || out_ports[0].length === 0) {
width = width * 1.5;
}
let min_x = 0;
let max_x = 0;
let max_leght_text_x = 0;
let max_leght_text_out_kind = 0;
//generic square
high = size * generics[0].length;
total_high = high + offset / 2;
if (generics[0].length > 0) {
canvas.rect(width, high + offset).fill(border).move(locx, locy);
canvas.rect(width - 4, high + offset / 2).fill(genBox).move(locx + 2, locy + 2);
//write generics
for (let i = 0; i < generics[0].length; i++) {
locy = size * i + offset / 2;
let textleft = canvas.text(generics[kind][i]).move(locx - text_space - text_space_pin, locy - text_offset)
.font({ family: font, size: size, anchor: 'end' });
textleft = canvas.text(generics[name][i]).move(locx + text_space, locy - text_offset)
.font({ family: font, size: size, anchor: 'start' });
const max_local = generics[name][i].length + generics[kind][i].length;
max_leght_text_x = Math.max(max_leght_text_x, max_local);
max_x = max_leght_text_x;
min_x = Math.min(min_x, textleft['node'].getAttribute('x'));
canvas.line(locx - text_space, 0, locx, 0).move(locx - text_space, locy + size * 2 / 4)
.stroke({ color: 'black', width: size / 4, linecap: 'rec' });
}
}
//ports square
locy = high + offset / 2 + separator;
high = size * Math.max(in_ports[0].length, out_ports[0].length);
total_high = total_high + high + offset / 2;
if (in_ports[0].length > 0 || out_ports[0].length > 0) {
canvas.rect(width, high + offset).fill(border).move(locx, locy);
canvas.rect(width - 4, high + offset / 2).fill(portBox).move(locx + 2, locy + 2);
//write ports
for (let i = 0; i < in_ports[0].length; i++) {
locy = size * generics[0].length + offset + size * i + separator;
let textleft = canvas.text(in_ports[kind][i]).move(locx - text_space - text_space_pin, locy - text_offset)
.font({ family: font, size: size, anchor: 'end' });
textleft = canvas.text(in_ports[name][i]).move(locx + text_space, locy - text_offset)
.font({ family: font, size: size, anchor: 'start' });
const max_local = in_ports[kind][i].length;
max_leght_text_x = Math.max(max_leght_text_x, max_local);
min_x = Math.min(min_x, textleft['node'].getAttribute('x'));
canvas.line(locx - text_space, 0, locx, 0).move(locx - text_space, locy + size * 2 / 4)
.stroke({ color: 'black', width: size / 4, linecap: 'rec' });
}
}
max_x = width;
max_leght_text_out_kind = Math.max(max_leght_text_x + offset, offset);
if (out_ports[0].length > 0) {
max_x = 0;
max_leght_text_out_kind = 0;
for (let i = 0; i < out_ports[0].length; i++) {
locy = size * generics[0].length + offset + size * i + separator;
let textright = canvas.text(out_ports[kind][i])
.move(locx + width + text_space + text_space_pin, locy - text_offset)
.font({ family: font, size: size, anchor: 'start' });
textright = canvas.text(out_ports[name][i]).move(locx + width - text_space, locy - text_offset)
.font({ family: font, size: size, anchor: 'end' });
const max_local = out_ports[kind][i].length;
max_leght_text_out_kind = Math.max(max_leght_text_out_kind, max_local);
max_x = Math.max(max_x, textright['node'].getAttribute('x'));
canvas.line(locx - text_space, 0, locx, 0).move(locx + width, locy + size * 2 / 4)
.stroke({ color: 'black', width: size / 4, linecap: 'rec' });
}
}
const total_width = max_x + (size / 2) * max_leght_text_out_kind + 2 * offset;
canvas.viewbox(0, 0, total_width, 2 * offset + total_high);
if (in_ports[0].length > 0 || out_ports[0].length > 0 || generics[0].length > 0) {
return canvas.svg();
} else {
return '';
}
}
function nomalize_str(input: string): string {
return input.replace(/`/g, '\\`');
}
function get_generics(structure: common_hdl.Hdl_element, name: number, kind: number) {
const str: any = [[], []];
const generic_list = structure.get_generic_array();
for (let x = 0; x <= generic_list.length - 1; ++x) {
str[name][x] = ' ' + nomalize_str(generic_list[x].info.name) + ' ';
str[kind][x] = ' ' + nomalize_str(generic_list[x].type) + ' ';
}
return str;
}
function get_ports(hdl_element: common_hdl.Hdl_element) {
const port_list = hdl_element.get_port_array();
const port_list_vbus = doxygen.get_virtual_bus(port_list);
let complete_list: any[] = [];
complete_list = complete_list.concat(port_list_vbus.port_list);
complete_list = complete_list.concat(port_list_vbus.v_port_list);
return complete_list;
}
function get_ports_in(structure: common_hdl.Hdl_element, name: number, kind: number) {
const str_in: any = [[], []];
const port_list = get_ports(structure);
let cont_in = 0;
for (let x = 0; x <= port_list.length - 1; ++x) {
if (INPUT_LABEL_LIST.includes(port_list[x].direction)) {
str_in[name][cont_in] = ' ' + nomalize_str(port_list[x].info.name) + ' ';
str_in[kind][cont_in] = ' ' + nomalize_str(port_list[x].type) + ' ';
cont_in++;
}
}
return str_in;
}
function get_ports_out(structure: common_hdl.Hdl_element, name: number, kind: number) {
const str_in: any = [[], []];
let cont_in = 0;
const port_list = get_ports(structure);
for (let x = 0; x <= port_list.length - 1; ++x) {
if (OUTPUT_LABEL_LIST.includes(port_list[x].direction)) {
str_in[name][cont_in] = ' ' + nomalize_str(port_list[x].info.name) + ' ';
str_in[kind][cont_in] = ' ' + nomalize_str(port_list[x].type) + ' ';
cont_in++;
}
}
return str_in;
}
function max_string(generics: any, in_ports: any, output_ports: any, data: number) {
let max = 2;
for (let i = 0; i < generics[data].length; i++) {
max = Math.max(max, generics[data][i].length);
}
max = max / 1.5;
for (let i = 0; i < in_ports[data].length; i++) {
max = Math.max(max, in_ports[data][i].length);
}
for (let i = 0; i < output_ports[data].length; i++) {
max = Math.max(max, output_ports[data][i].length);
}
return max;
}