From 66d0790c6ea72f41972ea992486c35b77b8b4f11 Mon Sep 17 00:00:00 2001 From: drechsler Date: Thu, 31 Oct 2019 10:59:48 +0100 Subject: [PATCH 1/5] added compact format --- README.md | 1 + bin/bitfield.js | 1 + lib/render.js | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) 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..c1f2b5c 100644 --- a/lib/render.js +++ b/lib/render.js @@ -125,9 +125,18 @@ 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) { + if (opt.index == opt.lanes-1) { + var fields = opt.bits/opt.lanes; + for (var i = 0; i < fields; i++) { + bits.push(text(fields - 1 - i, step*i)); + } + } + } else { + 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( @@ -185,8 +194,9 @@ function cage (desc, opt) { function lane (desc, opt) { + var vspace = opt.vspace / (opt.compact + 1); return ['g', { - transform: t(4.5, (opt.lanes - opt.index - 1) * opt.vspace + 0.5), + transform: t(4.5, (opt.lanes - opt.index - 1) * vspace + 0.5), 'text-anchor': 'middle', 'font-size': opt.fontsize, 'font-family': opt.fontfamily || 'sans-serif', @@ -206,6 +216,7 @@ 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) { From 3bfc2a2e560e139c1c126c3a5389f6e13a686ea0 Mon Sep 17 00:00:00 2001 From: drechsler Date: Thu, 31 Oct 2019 13:50:58 +0100 Subject: [PATCH 2/5] added front labels in compact view --- lib/render.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/render.js b/lib/render.js index c1f2b5c..0fb025b 100644 --- a/lib/render.js +++ b/lib/render.js @@ -194,16 +194,25 @@ function cage (desc, opt) { function lane (desc, opt) { - var vspace = opt.vspace / (opt.compact + 1); + var ty = (opt.lanes - opt.index - 1); + var frontLabel=""; + if (opt.compact) { + ty = ty * opt.vspace / 2; + frontLabel = text(opt.index, -10, opt.vspace/2 + 4); + } else { + ty = ty * opt.vspace + 0.5; + } + var tx = 4.5 + opt.compact*20; return ['g', { - transform: t(4.5, (opt.lanes - opt.index - 1) * vspace + 0.5), + transform: t(tx, ty), 'text-anchor': 'middle', 'font-size': opt.fontsize, 'font-family': opt.fontfamily || 'sans-serif', 'font-weight': opt.fontweight || 'normal' }, cage(desc, opt), - labelArr(desc, opt) + labelArr(desc, opt), + ['g', frontLabel] ]; } @@ -222,7 +231,7 @@ function render (desc, opt) { 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 res = getSVG(opt.hspace + 9 + 20*opt.compact, (opt.vspace + attributes) * opt.lanes + 5); var lsb = 0; var mod = opt.bits / opt.lanes; From f9ef66c171b9705481a3b93a3e06fb3228391b1d Mon Sep 17 00:00:00 2001 From: drechsler Date: Thu, 31 Oct 2019 19:07:37 +0100 Subject: [PATCH 3/5] fixed svg height --- lib/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/render.js b/lib/render.js index 0fb025b..acd537f 100644 --- a/lib/render.js +++ b/lib/render.js @@ -231,7 +231,7 @@ function render (desc, opt) { 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 + 20*opt.compact, (opt.vspace + attributes) * opt.lanes + 5); + var res = getSVG(opt.hspace + 9 + 20*opt.compact, (opt.vspace + attributes) * (opt.lanes + opt.compact)/(opt.compact + 1) + 5); var lsb = 0; var mod = opt.bits / opt.lanes; From 67fc26160aca7abff25d2db1688e6f69be435cc5 Mon Sep 17 00:00:00 2001 From: drechsler Date: Fri, 1 Nov 2019 11:19:43 +0100 Subject: [PATCH 4/5] refactor & cleanup --- lib/render.js | 54 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/render.js b/lib/render.js index acd537f..f4f0fe5 100644 --- a/lib/render.js +++ b/lib/render.js @@ -125,14 +125,7 @@ function labelArr (desc, opt) { return; } } - if (opt.compact) { - if (opt.index == opt.lanes-1) { - var fields = opt.bits/opt.lanes; - for (var i = 0; i < fields; i++) { - bits.push(text(fields - 1 - i, step*i)); - } - } - } else { + if (!opt.compact) { bits.push(text(lsb, step * (opt.mod - lsbm - 1))); if (lsbm !== msbm) { bits.push(text(msb, step * (opt.mod - msbm - 1))); @@ -164,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; @@ -194,16 +202,13 @@ function cage (desc, opt) { function lane (desc, opt) { - var ty = (opt.lanes - opt.index - 1); - var frontLabel=""; + var ty = (opt.lanes - opt.index - 1) * opt.vspace + 0.5; + var tx = 4.5; if (opt.compact) { - ty = ty * opt.vspace / 2; - frontLabel = text(opt.index, -10, opt.vspace/2 + 4); - } else { - ty = ty * opt.vspace + 0.5; + ty = (opt.lanes - opt.index - 1) * opt.vspace / 2 + opt.fontsize/2; + tx += 20 } - var tx = 4.5 + opt.compact*20; - return ['g', { + var lane = ['g', { transform: t(tx, ty), 'text-anchor': 'middle', 'font-size': opt.fontsize, @@ -212,8 +217,11 @@ function lane (desc, opt) { }, cage(desc, opt), labelArr(desc, opt), - ['g', frontLabel] ]; + if (opt.compact) { + lane.push(['g', text(opt.index, -10, opt.vspace/2 + 4)]); + } + return lane; } function render (desc, opt) { @@ -231,7 +239,14 @@ function render (desc, opt) { 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 + 20*opt.compact, (opt.vspace + attributes) * (opt.lanes + opt.compact)/(opt.compact + 1) + 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; @@ -250,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; } From 082cb36ea988050b31e055bb8c63374169021d95 Mon Sep 17 00:00:00 2001 From: drechsler Date: Fri, 1 Nov 2019 11:43:56 +0100 Subject: [PATCH 5/5] fixed semi,ws,comma warnings --- lib/render.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/render.js b/lib/render.js index f4f0fe5..c5ef44d 100644 --- a/lib/render.js +++ b/lib/render.js @@ -206,7 +206,7 @@ function lane (desc, opt) { var tx = 4.5; if (opt.compact) { ty = (opt.lanes - opt.index - 1) * opt.vspace / 2 + opt.fontsize/2; - tx += 20 + tx += 20; } var lane = ['g', { transform: t(tx, ty), @@ -216,7 +216,7 @@ function lane (desc, opt) { 'font-weight': opt.fontweight || 'normal' }, cage(desc, opt), - labelArr(desc, opt), + labelArr(desc, opt) ]; if (opt.compact) { lane.push(['g', text(opt.index, -10, opt.vspace/2 + 4)]); @@ -266,7 +266,7 @@ function render (desc, opt) { res.push(lane(desc, opt)); } if (opt.compact) { - res.push(compactLabels(desc,opt)); + res.push(compactLabels(desc, opt)); } return res; }