Skip to content

Commit

Permalink
Merge pull request #23 from nopeslide/master
Browse files Browse the repository at this point in the history
added compact format
  • Loading branch information
drom authored Dec 8, 2019
2 parents fce165e + 082cb36 commit 39354ca
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
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

0 comments on commit 39354ca

Please sign in to comment.