From dd6ebdcabff8134f238d7383c32733084196a00d Mon Sep 17 00:00:00 2001 From: Tau <92740480+Tao-Boy@users.noreply.github.com> Date: Wed, 16 Sep 2026 08:46:22 +0000 Subject: [PATCH] feat(cli): add configurable padding for PNG and SVG --- README.md | 1 + README.zh-CN.md | 1 + crates/ratex-render/src/main.rs | 10 +++++++++- crates/ratex-svg/src/bin/render_svg.rs | 10 +++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e5575ca..e08af79c 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,7 @@ CLI notes - `render-svg --stdout`: write SVG documents to stdout instead of files; status messages go to stderr. - `--help`: show supported options and whether the current build uses embedded fonts. - `--color` / `--background-color` accept named colors (for example `black`, `red`, `teal`), 3- or 6-digit hex (`#f00`, `#ff0000`), and KaTeX / MathJax-style color model values (`[RGB]255,0,0`, `[rgb]1,0,0`, `[HTML]B22222`, `[gray]0.5`, `[cmyk]0,1,1,0`). +- `--padding ` sets the base padding on each side for PNG and SVG output. The effective padding is scaled by `--dpr`. - `render-svg --office-compatible-colors`: emit SVG paint colors as `rgb(...)` plus opacity attributes instead of the default `rgba(...)`, for Office versions that do not understand `rgba(...)` in SVG. - `ratex-render --background-color transparent` produces a transparent PNG. diff --git a/README.zh-CN.md b/README.zh-CN.md index 1c9183d9..508b3e77 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -238,6 +238,7 @@ CLI 说明 - `render-svg --stdout`:将 SVG 文档写到 stdout,而不是文件;状态信息写到 stderr。 - `--help`:查看当前二进制支持的选项,以及当前构建是否启用了嵌入字体。 - `--color` / `--background-color` 支持命名颜色(如 `black`、`red`、`teal`)、三位或六位十六进制(如 `#f00`、`#ff0000`)、以及 KaTeX / MathJax 风格的颜色模型写法(如 `[RGB]255,0,0`、`[rgb]1,0,0`、`[HTML]B22222`、`[gray]0.5`、`[cmyk]0,1,1,0`)。 +- `--padding `:设置 PNG 和 SVG 输出四周的基础空白边距。实际边距会随 `--dpr` 缩放。 - `render-svg --office-compatible-colors`:将 SVG 颜色写成 `rgb(...)` 加 opacity 属性,而不是默认的 `rgba(...)`,用于不识别 SVG `rgba(...)` 的 Office 版本。 - `ratex-render` 的 `--background-color transparent` 可输出透明背景 PNG。 diff --git a/crates/ratex-render/src/main.rs b/crates/ratex-render/src/main.rs index c1f6cf1c..1ce5d4be 100644 --- a/crates/ratex-render/src/main.rs +++ b/crates/ratex-render/src/main.rs @@ -47,6 +47,13 @@ fn main() { .and_then(|s| s.parse::().ok()) .unwrap_or(40.0); + let padding = args + .iter() + .position(|a| a == "--padding") + .and_then(|i| args.get(i + 1)) + .and_then(|s| s.parse::().ok()) + .unwrap_or(10.0); + let color = args .iter() .position(|a| a == "--color") @@ -81,7 +88,7 @@ fn main() { let options = RenderOptions { font_size, - padding: 10.0, + padding, background_color, font_dir, device_pixel_ratio, @@ -204,6 +211,7 @@ Options: {font_dir_option} --output-dir Write PNGs to this directory [default: output] --dpr Render scale factor [default: 1.0] --font-size Base font size in pixels [default: 40.0] + --padding Base padding on each side in pixels [default: 10.0] --color Formula color: named, #rgb, #rgba, #rrggbb, #rrggbbaa, or [MODEL]value [default: black] diff --git a/crates/ratex-svg/src/bin/render_svg.rs b/crates/ratex-svg/src/bin/render_svg.rs index 1fc27991..72aac3f0 100644 --- a/crates/ratex-svg/src/bin/render_svg.rs +++ b/crates/ratex-svg/src/bin/render_svg.rs @@ -55,6 +55,13 @@ fn main() { .and_then(|s| s.parse::().ok()) .unwrap_or(40.0); + let padding = args + .iter() + .position(|a| a == "--padding") + .and_then(|i| args.get(i + 1)) + .and_then(|s| s.parse::().ok()) + .unwrap_or(10.0); + let color = args .iter() .position(|a| a == "--color") @@ -80,7 +87,7 @@ fn main() { let dpr = device_pixel_ratio.clamp(0.01, 16.0) as f64; let svg_opts = SvgOptions { font_size: font_size * dpr, - padding: 10.0 * dpr, + padding: padding * dpr, stroke_width: 1.5 * dpr, embed_glyphs: true, font_dir, @@ -218,6 +225,7 @@ Options: --stdout Write SVGs to stdout instead of files --dpr Scale font size, padding, and stroke width [default: 1.0] --font-size Base SVG font size in user units [default: 40.0] + --padding Base padding on each side in user units [default: 10.0] --color Formula color: named, #rgb, #rgba, #rrggbb, #rrggbbaa, or [MODEL]value [default: black]