Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added compact format #23

Merged
merged 5 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Options:
--bits overall bitwidth [default: 32]
--fontsize font size [default: 14]
--bigendian endianness [default: false]
--compact compact format [default: false]
--fontfamily font family [default: "sans-serif"]
--fontweight font weight [default: "normal"]
--help Show help [boolean]
Expand Down
1 change: 1 addition & 0 deletions bin/bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var argv = yargs
.option('bits', {describe: 'overall bitwidth', default: 32})
.option('fontsize', {describe: 'font size', default: 14})
.option('bigendian', {describe: 'endianness', default: false})
.option('compact', {describe: 'compact format', default: false})
.option('fontfamily', {describe: 'font family', default: 'sans-serif'})
.option('fontweight', {describe: 'font weight', default: 'normal'})
.demandOption(['input'])
Expand Down
50 changes: 44 additions & 6 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ function labelArr (desc, opt) {
return;
}
}
bits.push(text(lsb, step * (opt.mod - lsbm - 1)));
if (lsbm !== msbm) {
bits.push(text(msb, step * (opt.mod - msbm - 1)));
if (!opt.compact) {
bits.push(text(lsb, step * (opt.mod - lsbm - 1)));
if (lsbm !== msbm) {
bits.push(text(msb, step * (opt.mod - msbm - 1)));
}
}
if (e.name) {
names.push(getLabel(
Expand Down Expand Up @@ -155,6 +157,21 @@ function labelArr (desc, opt) {
return ['g', blanks, bits, names, attrs];
}

function compactLabels(desc, opt) {
var step = opt.hspace / opt.mod;
var tx = 4.5 + opt.compact*20 + step/2;
var labels = ['g', {
'text-anchor': 'middle',
'font-size': opt.fontsize,
'font-family': opt.fontfamily || 'sans-serif',
'font-weight': opt.fontweight || 'normal'
}];
for (var i = 0; i < opt.mod; i++) {
labels.push(text(opt.mod - 1 - i, tx+ step*i, opt.fontsize));
}
return labels;
}

function cage (desc, opt) {
var hspace = opt.hspace;
var vspace = opt.vspace;
Expand Down Expand Up @@ -185,8 +202,14 @@ function cage (desc, opt) {


function lane (desc, opt) {
return ['g', {
transform: t(4.5, (opt.lanes - opt.index - 1) * opt.vspace + 0.5),
var ty = (opt.lanes - opt.index - 1) * opt.vspace + 0.5;
var tx = 4.5;
if (opt.compact) {
ty = (opt.lanes - opt.index - 1) * opt.vspace / 2 + opt.fontsize/2;
tx += 20;
}
var lane = ['g', {
transform: t(tx, ty),
'text-anchor': 'middle',
'font-size': opt.fontsize,
'font-family': opt.fontfamily || 'sans-serif',
Expand All @@ -195,6 +218,10 @@ function lane (desc, opt) {
cage(desc, opt),
labelArr(desc, opt)
];
if (opt.compact) {
lane.push(['g', text(opt.index, -10, opt.vspace/2 + 4)]);
}
return lane;
}

function render (desc, opt) {
Expand All @@ -206,12 +233,20 @@ function render (desc, opt) {
opt.bits = isIntGTorDefault(opt.bits, 4, 32);
opt.fontsize = isIntGTorDefault(opt.fontsize, 5, 14);

opt.compact = opt.compact || false;
opt.bigendian = opt.bigendian || false;

var attributes = desc.reduce(function (prev, cur) {
return Math.max(prev, (Array.isArray(cur.attr)) ? cur.attr.length : 0);
}, 0) * 16;
var res = getSVG(opt.hspace + 9, (opt.vspace + attributes) * opt.lanes + 5);

var width = opt.hspace + 9;
var height = (opt.vspace + attributes) * opt.lanes + 5;
if (opt.compact) {
width += 20;
height = (opt.vspace + attributes) * (opt.lanes + 1)/2 + opt.fontsize;
}
var res = getSVG(width, height);

var lsb = 0;
var mod = opt.bits / opt.lanes;
Expand All @@ -230,6 +265,9 @@ function render (desc, opt) {
opt.index = i;
res.push(lane(desc, opt));
}
if (opt.compact) {
res.push(compactLabels(desc, opt));
}
return res;
}

Expand Down