From d8b4279495cbd32b12a2217b346153504a1e346f Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Mon, 17 May 2021 22:19:12 +0200 Subject: [PATCH] Accept both absolute and relative values for config padding (#308) --- source/app/metrics/utils.mjs | 27 +++++++++++++++++---------- source/plugins/core/README.md | 6 ++++-- source/plugins/core/metadata.yml | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/source/app/metrics/utils.mjs b/source/app/metrics/utils.mjs index a673fa96..d7f9f957 100644 --- a/source/app/metrics/utils.mjs +++ b/source/app/metrics/utils.mjs @@ -283,13 +283,20 @@ export const svg = { console.debug(`metrics/svg/resize > started ${await svg.resize.browser.version()}`) } //Format padding - const [pw = 1, ph] = (Array.isArray(paddings) ? paddings : `${paddings}`.split(",").map(x => x.trim())).map(padding => `${padding}`.substring(0, padding.length - 1)).map(value => 1 + Number(value) / 100) - const padding = {width:pw, height:(ph ?? pw)} - if (!Number.isFinite(padding.width)) - padding.width = 1 - if (!Number.isFinite(padding.height)) - padding.height = 1 - console.debug(`metrics/svg/resize > padding width*${padding.width}, height*${padding.height}`) + const padding = {width:1, height:1, absolute:{width:0, height:0}} + paddings = Array.isArray(paddings) ? paddings : `${paddings}`.split(",").map(x => x.trim()) + for (const [i, dimension] of [[0, "width"], [1, "height"]]) { + let operands = (paddings?.[i] ?? paddings[0]) + let {relative} = operands.match(/(?[+-]?[\d.]+)%$/)?.groups ?? {} + operands = operands.replace(relative, "").trim() + let {absolute} = operands.match(/^(?[+-]?[\d.]+)/)?.groups ?? {} + operands = operands.replace(absolute, "").trim() + if (Number.isFinite(Number(absolute))) + padding.absolute[dimension] = Number(absolute) + if (Number.isFinite(Number(relative))) + padding[dimension] = 1 + Number(relative/100) + } + console.debug(`metrics/svg/resize > padding width*${padding.width}+${padding.absolute.width}, height*${padding.height}+${padding.absolute.height}`) //Render through browser and resize height console.debug("metrics/svg/resize > loading svg") const page = await svg.resize.browser.newPage() @@ -311,9 +318,9 @@ export const svg = { //Get bounds and resize let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect() console.debug(`bounds width=${width}, height=${height}`) - height = Math.ceil(height * padding.height) - width = Math.ceil(width * padding.width) - console.debug(`bounds after applying padding width=${width} (*${padding.width}), height=${height} (*${padding.height})`) + height = Math.ceil(height * padding.height + padding.absolute.height) + width = Math.ceil(width * padding.width + padding.absolute.width) + console.debug(`bounds after applying padding width=${width} (*${padding.width}+${padding.absolute.width}), height=${height} (*${padding.height}+${padding.absolute.height})`) //Resize svg document.querySelector("svg").setAttribute("height", height) //Enable animations diff --git a/source/plugins/core/README.md b/source/plugins/core/README.md index 6fdc98ef..ba9e98f1 100644 --- a/source/plugins/core/README.md +++ b/source/plugins/core/README.md @@ -161,7 +161,9 @@ As it can depend on fonts and operating system, it is possible that final result You can adjust padding by using `config_padding` option. -Specify a single value to apply it to both height and with, and two values to use the first one for width and the second for height. Both positive and negative values are accepted, but you must specify a percentage. +Specify a single value to apply it to both height and with, and two values to use the first one for width and the second for height. Both positive and negative values are accepted. + +The allowed format is `(absolute padding) + (relative padding)%` (each operand is optional). #### ℹ️ Examples workflows @@ -169,7 +171,7 @@ Specify a single value to apply it to both height and with, and two values to us - uses: lowlighter/metrics@latest with: # ... other options - config_padding: 6%, 15% # 6% width padding, 15% height padding + config_padding: 16, 32 + 8% # 16px width padding, 32px + 8% height padding ``` ### 🧶 Using commits, pull requests, manual reviews or gists to handle metrics output diff --git a/source/plugins/core/metadata.yml b/source/plugins/core/metadata.yml index e96da424..3082aae1 100644 --- a/source/plugins/core/metadata.yml +++ b/source/plugins/core/metadata.yml @@ -180,7 +180,7 @@ inputs: description: Image padding type: array format: comma-separated - default: 6%, 15% + default: 16, 32 + 8% # Metrics output format config_output: