diff --git a/README.md b/README.md index 0fc2fd3..f243dd2 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/bin/bitfield.js b/bin/bitfield.js index 994dc4b..5f4d285 100755 --- a/bin/bitfield.js +++ b/bin/bitfield.js @@ -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']) diff --git a/lib/render.js b/lib/render.js index 386cce5..c5ef44d 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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( @@ -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; @@ -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', @@ -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) { @@ -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; @@ -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; }