vizmatic 0.1.17 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +118 -33
- package/dist/index.cjs +86 -20
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +85 -20
- package/docs/assets/examples/eval-dashboard_dark.png +0 -0
- package/docs/assets/examples/eval-dashboard_light.png +0 -0
- package/docs/assets/examples/rag-graph_dark.png +0 -0
- package/docs/assets/examples/rag-graph_light.png +0 -0
- package/docs/assets/examples/release-card_dark.png +0 -0
- package/docs/assets/examples/release-card_light.png +0 -0
- package/docs/assets/examples/spec-anatomy_dark.png +0 -0
- package/docs/assets/examples/spec-anatomy_light.png +0 -0
- package/docs/assets/examples/theme-background_dark.png +0 -0
- package/docs/assets/examples/theme-background_light.png +0 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -102,6 +102,7 @@ __export(index_exports, {
|
|
|
102
102
|
renderAnimatedGif: () => renderAnimatedGif,
|
|
103
103
|
renderToBuffer: () => renderToBuffer,
|
|
104
104
|
renderToPng: () => renderToPng,
|
|
105
|
+
renderToPngWithOutput: () => renderToPngWithOutput,
|
|
105
106
|
renderToSvg: () => renderToSvg,
|
|
106
107
|
styles: () => styles,
|
|
107
108
|
toneGradients: () => toneGradients,
|
|
@@ -633,6 +634,7 @@ function Panel({
|
|
|
633
634
|
width: "100%",
|
|
634
635
|
display: "flex",
|
|
635
636
|
flexDirection: "column",
|
|
637
|
+
gap,
|
|
636
638
|
...bodyStyle
|
|
637
639
|
}
|
|
638
640
|
}, children),
|
|
@@ -4048,16 +4050,16 @@ function BarChart({
|
|
|
4048
4050
|
yAxisLabel,
|
|
4049
4051
|
footer
|
|
4050
4052
|
}) {
|
|
4051
|
-
const margin = { top: 18, right:
|
|
4053
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
4052
4054
|
const plot = createPlotArea(width, height, margin);
|
|
4053
4055
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
4054
4056
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
4055
|
-
const
|
|
4056
|
-
const barWidth = Math.max(
|
|
4057
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
4058
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
4057
4059
|
const zeroY = yInPlot(0, domain, plot);
|
|
4058
4060
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
4059
4061
|
const barGeometries = data.map((item, index) => {
|
|
4060
|
-
const x = plot.x + index * (barWidth
|
|
4062
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
4061
4063
|
const valueY = yInPlot(item.value, domain, plot);
|
|
4062
4064
|
const y = Math.min(valueY, baselineY);
|
|
4063
4065
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -4946,7 +4948,7 @@ function cropPixels(pixels, srcWidth, region) {
|
|
|
4946
4948
|
}
|
|
4947
4949
|
return out;
|
|
4948
4950
|
}
|
|
4949
|
-
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold =
|
|
4951
|
+
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold = 10) {
|
|
4950
4952
|
const isBackground = createBackgroundMatcher(bgColor);
|
|
4951
4953
|
function isContentPixel(x, y) {
|
|
4952
4954
|
const idx = (y * width + x) * 4;
|
|
@@ -5172,10 +5174,35 @@ function sanitizeSvg(svg) {
|
|
|
5172
5174
|
}
|
|
5173
5175
|
var AUTOCROP_PADDING = 24;
|
|
5174
5176
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5177
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5178
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5179
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5180
|
+
return `${attrs} ${name}="${value}"`;
|
|
5181
|
+
}
|
|
5182
|
+
function cropSvgViewport(svg, region) {
|
|
5183
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5184
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5185
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5186
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5187
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5188
|
+
let nextAttrs = attrs;
|
|
5189
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5190
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5191
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5192
|
+
return `<svg${nextAttrs}>`;
|
|
5193
|
+
});
|
|
5194
|
+
}
|
|
5195
|
+
function cropSavesEnough(source, target) {
|
|
5196
|
+
if (source <= 0 || target >= source) return false;
|
|
5197
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5198
|
+
}
|
|
5175
5199
|
function resolveWatermark(options) {
|
|
5176
5200
|
return options.watermark ?? options.brand;
|
|
5177
5201
|
}
|
|
5178
5202
|
async function renderToPng(element, options, createFn, theme) {
|
|
5203
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5204
|
+
}
|
|
5205
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5179
5206
|
return withRenderContext(
|
|
5180
5207
|
{ background: options.background ?? "transparent" },
|
|
5181
5208
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5216,7 +5243,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5216
5243
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5217
5244
|
);
|
|
5218
5245
|
}
|
|
5219
|
-
|
|
5246
|
+
let finalWidth = options.width;
|
|
5220
5247
|
let finalHeight = options.height;
|
|
5221
5248
|
let finalElement = element;
|
|
5222
5249
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5228,20 +5255,57 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5228
5255
|
}
|
|
5229
5256
|
}
|
|
5230
5257
|
const watermark = resolveWatermark(options);
|
|
5231
|
-
const
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5241
|
-
|
|
5242
|
-
|
|
5243
|
-
|
|
5244
|
-
|
|
5258
|
+
const renderFinalSvg = async (targetElement, targetWidth, targetHeight) => {
|
|
5259
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
5260
|
+
targetElement,
|
|
5261
|
+
targetWidth,
|
|
5262
|
+
targetHeight,
|
|
5263
|
+
theme || "dark",
|
|
5264
|
+
watermark
|
|
5265
|
+
) : targetElement;
|
|
5266
|
+
const renderedSvg = await satori(outputElement, {
|
|
5267
|
+
width: targetWidth,
|
|
5268
|
+
height: targetHeight,
|
|
5269
|
+
fonts,
|
|
5270
|
+
loadAdditionalAsset
|
|
5271
|
+
});
|
|
5272
|
+
return sanitizeSvg(renderedSvg);
|
|
5273
|
+
};
|
|
5274
|
+
let finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5275
|
+
if (finalHeight < options.height) {
|
|
5276
|
+
const cropCheck = new Resvg(finalSvg, {
|
|
5277
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5278
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5279
|
+
}).render();
|
|
5280
|
+
const cropPixels2 = new Uint8Array(cropCheck.pixels.buffer);
|
|
5281
|
+
const cropBg = detectBackgroundColor(cropPixels2);
|
|
5282
|
+
const cropOverflow = detectOverflow(cropPixels2, finalWidth, cropCheck.height, cropBg);
|
|
5283
|
+
if (cropOverflow.overflows) {
|
|
5284
|
+
finalHeight = options.height;
|
|
5285
|
+
finalElement = element;
|
|
5286
|
+
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5287
|
+
}
|
|
5288
|
+
}
|
|
5289
|
+
if (options.crop !== false) {
|
|
5290
|
+
const viewportCheck = new Resvg(finalSvg, {
|
|
5291
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5292
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5293
|
+
}).render();
|
|
5294
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5295
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5296
|
+
const viewportBounds = detectContentBounds(
|
|
5297
|
+
viewportPixels,
|
|
5298
|
+
finalWidth,
|
|
5299
|
+
viewportCheck.height,
|
|
5300
|
+
viewportBg,
|
|
5301
|
+
AUTOCROP_PADDING
|
|
5302
|
+
);
|
|
5303
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5304
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5305
|
+
finalWidth = viewportBounds.width;
|
|
5306
|
+
finalHeight = viewportBounds.height;
|
|
5307
|
+
}
|
|
5308
|
+
}
|
|
5245
5309
|
const resvg = new Resvg(finalSvg, {
|
|
5246
5310
|
fitTo: {
|
|
5247
5311
|
mode: "width",
|
|
@@ -5253,6 +5317,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5253
5317
|
const pngBuffer = pngData.asPng();
|
|
5254
5318
|
await mkdir(dirname(options.outputPath), { recursive: true });
|
|
5255
5319
|
await writeFile(options.outputPath, pngBuffer);
|
|
5320
|
+
return { width: finalWidth, height: finalHeight };
|
|
5256
5321
|
}
|
|
5257
5322
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5258
5323
|
return withRenderContext(
|
|
@@ -5558,13 +5623,24 @@ function resolveSelfEntry() {
|
|
|
5558
5623
|
return void 0;
|
|
5559
5624
|
}
|
|
5560
5625
|
}
|
|
5561
|
-
function
|
|
5562
|
-
|
|
5626
|
+
function resolveSelfImportEntry() {
|
|
5627
|
+
const moduleDir = dirname3(fileURLToPath2(import.meta.url));
|
|
5628
|
+
const bundledEntry = join2(moduleDir, "index.js");
|
|
5629
|
+
if (existsSync2(bundledEntry)) return bundledEntry;
|
|
5630
|
+
const sourceEntry = join2(moduleDir, "index.ts");
|
|
5631
|
+
if (existsSync2(sourceEntry)) return sourceEntry;
|
|
5632
|
+
return void 0;
|
|
5633
|
+
}
|
|
5634
|
+
function resolveSelfImportSpecifier() {
|
|
5635
|
+
return resolveSelfImportEntry() ?? "vizmatic";
|
|
5563
5636
|
}
|
|
5564
5637
|
function resolveReactRequireSpecifier() {
|
|
5565
5638
|
const reactEntry = resolveFrameDependency("react");
|
|
5566
5639
|
return reactEntry ?? "react";
|
|
5567
5640
|
}
|
|
5641
|
+
function resolveReactImportSpecifier() {
|
|
5642
|
+
return resolveReactRequireSpecifier();
|
|
5643
|
+
}
|
|
5568
5644
|
function resolveFrameDependency(request) {
|
|
5569
5645
|
try {
|
|
5570
5646
|
if (request === "vizmatic") return resolveSelfEntry();
|
|
@@ -5637,10 +5713,10 @@ function bareFrameExportsForSource(source) {
|
|
|
5637
5713
|
});
|
|
5638
5714
|
}
|
|
5639
5715
|
function buildAutoImportStatement(names) {
|
|
5640
|
-
const specifiers = names.map((name) => ` ${name}
|
|
5641
|
-
return `
|
|
5716
|
+
const specifiers = names.map((name) => ` ${name} as ${bareFrameAlias(name)}`).join(",\n");
|
|
5717
|
+
return `import {
|
|
5642
5718
|
${specifiers}
|
|
5643
|
-
}
|
|
5719
|
+
} from ${JSON.stringify(resolveSelfImportSpecifier())}`;
|
|
5644
5720
|
}
|
|
5645
5721
|
function buildAutoImportDeclarations(names) {
|
|
5646
5722
|
return names.map((name) => {
|
|
@@ -5684,9 +5760,7 @@ function buildBareFrameModule(framePath, source) {
|
|
|
5684
5760
|
height ??= frameHeight ?? DEFAULT_FRAME_HEIGHT;
|
|
5685
5761
|
const autoImports = bareFrameExportsForSource(body);
|
|
5686
5762
|
return `/** @jsxRuntime classic */
|
|
5687
|
-
import
|
|
5688
|
-
const __require = __createRequire(${JSON.stringify(import.meta.url)})
|
|
5689
|
-
const React = __require(${JSON.stringify(resolveReactRequireSpecifier())})
|
|
5763
|
+
import React from ${JSON.stringify(resolveReactImportSpecifier())}
|
|
5690
5764
|
${buildAutoImportStatement(autoImports)}
|
|
5691
5765
|
${imports.join("\n")}
|
|
5692
5766
|
|
|
@@ -5710,7 +5784,7 @@ ${setup}
|
|
|
5710
5784
|
return (${jsx})
|
|
5711
5785
|
}
|
|
5712
5786
|
|
|
5713
|
-
export default create
|
|
5787
|
+
export default { create, width, height }
|
|
5714
5788
|
`;
|
|
5715
5789
|
}
|
|
5716
5790
|
function isBareFrameSource(source) {
|
|
@@ -5726,6 +5800,7 @@ async function importBareFrame(filePath, source) {
|
|
|
5726
5800
|
if (!moduleSource) return void 0;
|
|
5727
5801
|
const tempDir = await mkdtemp(join2(tmpdir(), "vizmatic-frame-"));
|
|
5728
5802
|
const tempPath = join2(tempDir, `${basename(filePath).replace(/\.[^.]+$/, "")}.generated.tsx`);
|
|
5803
|
+
await writeFile3(join2(tempDir, "package.json"), '{"type":"module"}\n');
|
|
5729
5804
|
await writeFile3(tempPath, `${moduleSource}
|
|
5730
5805
|
`);
|
|
5731
5806
|
try {
|
|
@@ -5949,12 +6024,12 @@ async function renderFrameToPng(mod, theme, options) {
|
|
|
5949
6024
|
let height = options.height;
|
|
5950
6025
|
for (let attempt = 0; attempt < AUTO_SIZE_ATTEMPTS; attempt += 1) {
|
|
5951
6026
|
try {
|
|
5952
|
-
await
|
|
6027
|
+
const output = await renderToPngWithOutput(frameElement(mod, theme), {
|
|
5953
6028
|
...options,
|
|
5954
6029
|
width,
|
|
5955
6030
|
height
|
|
5956
6031
|
}, mod.create, theme);
|
|
5957
|
-
return { width, height };
|
|
6032
|
+
return { width, height, outputWidth: output.width, outputHeight: output.height };
|
|
5958
6033
|
} catch (error) {
|
|
5959
6034
|
const edges = overflowEdges(error);
|
|
5960
6035
|
const next = edges ? nextAutoSize(width, height, mod.autoSize, edges) : void 0;
|
|
@@ -5963,7 +6038,7 @@ async function renderFrameToPng(mod, theme, options) {
|
|
|
5963
6038
|
height = next.height;
|
|
5964
6039
|
}
|
|
5965
6040
|
}
|
|
5966
|
-
return { width, height };
|
|
6041
|
+
return { width, height, outputWidth: width, outputHeight: height };
|
|
5967
6042
|
}
|
|
5968
6043
|
async function renderCommand(argv) {
|
|
5969
6044
|
const args = parseRenderArgs(argv);
|
|
@@ -5980,6 +6055,8 @@ async function renderCommand(argv) {
|
|
|
5980
6055
|
const moduleWatermark = await resolveWatermarkAssets(mod.watermark ?? mod.brand);
|
|
5981
6056
|
let renderWidth = mod.width;
|
|
5982
6057
|
let renderHeight = mod.height;
|
|
6058
|
+
let outputWidth = renderWidth;
|
|
6059
|
+
let outputHeight = renderHeight;
|
|
5983
6060
|
for (const theme of args.themes) {
|
|
5984
6061
|
const outputName = `${name}_${theme}.png`;
|
|
5985
6062
|
const outputPath = join2(args.outDir, outputName);
|
|
@@ -5994,6 +6071,8 @@ async function renderCommand(argv) {
|
|
|
5994
6071
|
});
|
|
5995
6072
|
renderWidth = rendered.width;
|
|
5996
6073
|
renderHeight = rendered.height;
|
|
6074
|
+
outputWidth = rendered.outputWidth;
|
|
6075
|
+
outputHeight = rendered.outputHeight;
|
|
5997
6076
|
outputs.push(outputName);
|
|
5998
6077
|
console.log(`rendered ${relative(process.cwd(), outputPath)}`);
|
|
5999
6078
|
}
|
|
@@ -6002,6 +6081,8 @@ async function renderCommand(argv) {
|
|
|
6002
6081
|
source: relative(process.cwd(), file),
|
|
6003
6082
|
width: renderWidth,
|
|
6004
6083
|
height: renderHeight,
|
|
6084
|
+
outputWidth,
|
|
6085
|
+
outputHeight,
|
|
6005
6086
|
outputs
|
|
6006
6087
|
});
|
|
6007
6088
|
}
|
|
@@ -6066,6 +6147,10 @@ async function main() {
|
|
|
6066
6147
|
}
|
|
6067
6148
|
}
|
|
6068
6149
|
main().catch((error) => {
|
|
6069
|
-
|
|
6150
|
+
if (process.env.VIZMATIC_DEBUG === "1" && error instanceof Error) {
|
|
6151
|
+
console.error(error.stack ?? error.message);
|
|
6152
|
+
} else {
|
|
6153
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
6154
|
+
}
|
|
6070
6155
|
process.exit(1);
|
|
6071
6156
|
});
|
package/dist/index.cjs
CHANGED
|
@@ -115,6 +115,7 @@ __export(index_exports, {
|
|
|
115
115
|
renderAnimatedGif: () => renderAnimatedGif,
|
|
116
116
|
renderToBuffer: () => renderToBuffer,
|
|
117
117
|
renderToPng: () => renderToPng,
|
|
118
|
+
renderToPngWithOutput: () => renderToPngWithOutput,
|
|
118
119
|
renderToSvg: () => renderToSvg,
|
|
119
120
|
styles: () => styles,
|
|
120
121
|
toneGradients: () => toneGradients,
|
|
@@ -647,6 +648,7 @@ function Panel({
|
|
|
647
648
|
width: "100%",
|
|
648
649
|
display: "flex",
|
|
649
650
|
flexDirection: "column",
|
|
651
|
+
gap,
|
|
650
652
|
...bodyStyle
|
|
651
653
|
}
|
|
652
654
|
}, children),
|
|
@@ -4062,16 +4064,16 @@ function BarChart({
|
|
|
4062
4064
|
yAxisLabel,
|
|
4063
4065
|
footer
|
|
4064
4066
|
}) {
|
|
4065
|
-
const margin = { top: 18, right:
|
|
4067
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
4066
4068
|
const plot = createPlotArea(width, height, margin);
|
|
4067
4069
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
4068
4070
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
4069
|
-
const
|
|
4070
|
-
const barWidth = Math.max(
|
|
4071
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
4072
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
4071
4073
|
const zeroY = yInPlot(0, domain, plot);
|
|
4072
4074
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
4073
4075
|
const barGeometries = data.map((item, index) => {
|
|
4074
|
-
const x = plot.x + index * (barWidth
|
|
4076
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
4075
4077
|
const valueY = yInPlot(item.value, domain, plot);
|
|
4076
4078
|
const y = Math.min(valueY, baselineY);
|
|
4077
4079
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -4960,7 +4962,7 @@ function cropPixels(pixels, srcWidth, region) {
|
|
|
4960
4962
|
}
|
|
4961
4963
|
return out;
|
|
4962
4964
|
}
|
|
4963
|
-
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold =
|
|
4965
|
+
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold = 10) {
|
|
4964
4966
|
const isBackground = createBackgroundMatcher(bgColor);
|
|
4965
4967
|
function isContentPixel(x, y) {
|
|
4966
4968
|
const idx = (y * width + x) * 4;
|
|
@@ -5187,10 +5189,35 @@ function sanitizeSvg(svg) {
|
|
|
5187
5189
|
}
|
|
5188
5190
|
var AUTOCROP_PADDING = 24;
|
|
5189
5191
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5192
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5193
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5194
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5195
|
+
return `${attrs} ${name}="${value}"`;
|
|
5196
|
+
}
|
|
5197
|
+
function cropSvgViewport(svg, region) {
|
|
5198
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5199
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5200
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5201
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5202
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5203
|
+
let nextAttrs = attrs;
|
|
5204
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5205
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5206
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5207
|
+
return `<svg${nextAttrs}>`;
|
|
5208
|
+
});
|
|
5209
|
+
}
|
|
5210
|
+
function cropSavesEnough(source, target) {
|
|
5211
|
+
if (source <= 0 || target >= source) return false;
|
|
5212
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5213
|
+
}
|
|
5190
5214
|
function resolveWatermark(options) {
|
|
5191
5215
|
return options.watermark ?? options.brand;
|
|
5192
5216
|
}
|
|
5193
5217
|
async function renderToPng(element, options, createFn, theme) {
|
|
5218
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5219
|
+
}
|
|
5220
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5194
5221
|
return withRenderContext(
|
|
5195
5222
|
{ background: options.background ?? "transparent" },
|
|
5196
5223
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5231,7 +5258,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5231
5258
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5232
5259
|
);
|
|
5233
5260
|
}
|
|
5234
|
-
|
|
5261
|
+
let finalWidth = options.width;
|
|
5235
5262
|
let finalHeight = options.height;
|
|
5236
5263
|
let finalElement = element;
|
|
5237
5264
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5243,20 +5270,57 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5243
5270
|
}
|
|
5244
5271
|
}
|
|
5245
5272
|
const watermark = resolveWatermark(options);
|
|
5246
|
-
const
|
|
5247
|
-
|
|
5248
|
-
|
|
5249
|
-
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5254
|
-
|
|
5255
|
-
|
|
5256
|
-
|
|
5257
|
-
|
|
5258
|
-
|
|
5259
|
-
|
|
5273
|
+
const renderFinalSvg = async (targetElement, targetWidth, targetHeight) => {
|
|
5274
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
5275
|
+
targetElement,
|
|
5276
|
+
targetWidth,
|
|
5277
|
+
targetHeight,
|
|
5278
|
+
theme || "dark",
|
|
5279
|
+
watermark
|
|
5280
|
+
) : targetElement;
|
|
5281
|
+
const renderedSvg = await satori(outputElement, {
|
|
5282
|
+
width: targetWidth,
|
|
5283
|
+
height: targetHeight,
|
|
5284
|
+
fonts,
|
|
5285
|
+
loadAdditionalAsset
|
|
5286
|
+
});
|
|
5287
|
+
return sanitizeSvg(renderedSvg);
|
|
5288
|
+
};
|
|
5289
|
+
let finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5290
|
+
if (finalHeight < options.height) {
|
|
5291
|
+
const cropCheck = new import_resvg_js.Resvg(finalSvg, {
|
|
5292
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5293
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5294
|
+
}).render();
|
|
5295
|
+
const cropPixels2 = new Uint8Array(cropCheck.pixels.buffer);
|
|
5296
|
+
const cropBg = detectBackgroundColor(cropPixels2);
|
|
5297
|
+
const cropOverflow = detectOverflow(cropPixels2, finalWidth, cropCheck.height, cropBg);
|
|
5298
|
+
if (cropOverflow.overflows) {
|
|
5299
|
+
finalHeight = options.height;
|
|
5300
|
+
finalElement = element;
|
|
5301
|
+
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5302
|
+
}
|
|
5303
|
+
}
|
|
5304
|
+
if (options.crop !== false) {
|
|
5305
|
+
const viewportCheck = new import_resvg_js.Resvg(finalSvg, {
|
|
5306
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5307
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5308
|
+
}).render();
|
|
5309
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5310
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5311
|
+
const viewportBounds = detectContentBounds(
|
|
5312
|
+
viewportPixels,
|
|
5313
|
+
finalWidth,
|
|
5314
|
+
viewportCheck.height,
|
|
5315
|
+
viewportBg,
|
|
5316
|
+
AUTOCROP_PADDING
|
|
5317
|
+
);
|
|
5318
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5319
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5320
|
+
finalWidth = viewportBounds.width;
|
|
5321
|
+
finalHeight = viewportBounds.height;
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5260
5324
|
const resvg = new import_resvg_js.Resvg(finalSvg, {
|
|
5261
5325
|
fitTo: {
|
|
5262
5326
|
mode: "width",
|
|
@@ -5268,6 +5332,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5268
5332
|
const pngBuffer = pngData.asPng();
|
|
5269
5333
|
await (0, import_promises.mkdir)((0, import_path.dirname)(options.outputPath), { recursive: true });
|
|
5270
5334
|
await (0, import_promises.writeFile)(options.outputPath, pngBuffer);
|
|
5335
|
+
return { width: finalWidth, height: finalHeight };
|
|
5271
5336
|
}
|
|
5272
5337
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5273
5338
|
return withRenderContext(
|
|
@@ -5560,6 +5625,7 @@ async function renderAnimatedGif(scenes, options) {
|
|
|
5560
5625
|
renderAnimatedGif,
|
|
5561
5626
|
renderToBuffer,
|
|
5562
5627
|
renderToPng,
|
|
5628
|
+
renderToPngWithOutput,
|
|
5563
5629
|
renderToSvg,
|
|
5564
5630
|
styles,
|
|
5565
5631
|
toneGradients,
|
package/dist/index.d.cts
CHANGED
|
@@ -1314,6 +1314,12 @@ interface RenderOptions {
|
|
|
1314
1314
|
/** Root background for Vizmatic primitives. Defaults to alpha-transparent PNG/SVG output. */
|
|
1315
1315
|
background?: RenderBackground;
|
|
1316
1316
|
}
|
|
1317
|
+
interface RenderOutput {
|
|
1318
|
+
/** Final logical output width before scale is applied. */
|
|
1319
|
+
width: number;
|
|
1320
|
+
/** Final logical output height before scale is applied. */
|
|
1321
|
+
height: number;
|
|
1322
|
+
}
|
|
1317
1323
|
/**
|
|
1318
1324
|
* Render a JSX element to a PNG file, with auto-crop and optional watermark.
|
|
1319
1325
|
*
|
|
@@ -1330,6 +1336,7 @@ interface RenderOptions {
|
|
|
1330
1336
|
* @param theme - Theme mode for re-rendering (if createFn provided)
|
|
1331
1337
|
*/
|
|
1332
1338
|
declare function renderToPng(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<void>;
|
|
1339
|
+
declare function renderToPngWithOutput(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<RenderOutput>;
|
|
1333
1340
|
/**
|
|
1334
1341
|
* Render a JSX element to a PNG buffer (useful for testing).
|
|
1335
1342
|
*/
|
|
@@ -1439,4 +1446,4 @@ interface OverflowResult {
|
|
|
1439
1446
|
*/
|
|
1440
1447
|
declare function detectOverflow(pixels: Uint8Array, width: number, height: number, bgColor: string, edgeTolerance?: number, minDensity?: number, alphaThreshold?: number): OverflowResult;
|
|
1441
1448
|
|
|
1442
|
-
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
|
1449
|
+
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, type RenderOutput, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToPngWithOutput, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
package/dist/index.d.ts
CHANGED
|
@@ -1314,6 +1314,12 @@ interface RenderOptions {
|
|
|
1314
1314
|
/** Root background for Vizmatic primitives. Defaults to alpha-transparent PNG/SVG output. */
|
|
1315
1315
|
background?: RenderBackground;
|
|
1316
1316
|
}
|
|
1317
|
+
interface RenderOutput {
|
|
1318
|
+
/** Final logical output width before scale is applied. */
|
|
1319
|
+
width: number;
|
|
1320
|
+
/** Final logical output height before scale is applied. */
|
|
1321
|
+
height: number;
|
|
1322
|
+
}
|
|
1317
1323
|
/**
|
|
1318
1324
|
* Render a JSX element to a PNG file, with auto-crop and optional watermark.
|
|
1319
1325
|
*
|
|
@@ -1330,6 +1336,7 @@ interface RenderOptions {
|
|
|
1330
1336
|
* @param theme - Theme mode for re-rendering (if createFn provided)
|
|
1331
1337
|
*/
|
|
1332
1338
|
declare function renderToPng(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<void>;
|
|
1339
|
+
declare function renderToPngWithOutput(element: ReactNode, options: RenderOptions, createFn?: (theme: 'dark' | 'light') => ReactNode, theme?: 'dark' | 'light'): Promise<RenderOutput>;
|
|
1333
1340
|
/**
|
|
1334
1341
|
* Render a JSX element to a PNG buffer (useful for testing).
|
|
1335
1342
|
*/
|
|
@@ -1439,4 +1446,4 @@ interface OverflowResult {
|
|
|
1439
1446
|
*/
|
|
1440
1447
|
declare function detectOverflow(pixels: Uint8Array, width: number, height: number, bgColor: string, edgeTolerance?: number, minDensity?: number, alphaThreshold?: number): OverflowResult;
|
|
1441
1448
|
|
|
1442
|
-
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
|
1449
|
+
export { type AnimatedScene, type AnimationOptions, Arrow, ArrowMarkerDef, AxisPlot, type AxisPlotPath, Badge, BadgePill, BarChart, type BarChartDatum, Box, CalloutCard, Canvas, Card, ChartFrame, type ChartLegendItem, type ChartMargins, type ChartValueFormat, CodeBlock, type CodeLineSpec, type ColorName, Column, Comparison, type ComparisonSideSpec, Connector, type CropRegion, DashedLine, DataTable, DetailList, DotPoint, EquationCard, Flow, FlowArrow, type FlowStageSpec, GradientChip, GraphDiagram, type GraphDiagramEdge, type GraphDiagramNode, Grid, Heatmap, type IllustrationBuilder, type IntervalDatum, IntervalPlot, KeyValueList, type KeyValueRow, LayeredNetwork, type LayeredNetworkLayer, Legend, LineChart, type LineChartSeries, MathText, Matrix, MetricCard, MiniBarChart, type MiniBarDatum, type OverflowResult, Panel, Pipeline, type PlotArea, ProgressList, ProgressRow, type ProgressRowSpec, QuadrantChart, type QuadrantRegion, type RenderBackground, type RenderOptions, type RenderOutput, Row, ScatterPlot, type ScatterPoint, Scene, Stack, StackedBar, type StackedBarSegment, type StatusKind, StatusList, StatusRow, type StatusRowSpec, StepCard, SvgFrame, SvgMathText, SvgPoint, TextLabel, type ThemeColors, type ThemeMode, Tile, TileGrid, type TileSpec, TiledMatrix, type TiledMatrixRegion, TitleBar, type ToneName, ToneStrip, type TransitionType, ValuePill, VectorArrow, VectorSegment, Watermark, type WatermarkElementProps, type WatermarkImageOptions, type WatermarkInput, type WatermarkOptions, type WatermarkPosition, WindowFrame, canvas, colors, createPlotArea, cropPixels, defineIllustration, detectBackgroundColor, detectContentBounds, detectOverflow, formatMathText, getColor, getFonts, getGradient, getStyles, getThemeColors, getToneColor, getToneGradient, gradients, heatColor, loadAdditionalAsset, normalizeWatermark, renderAnimatedGif, renderToBuffer, renderToPng, renderToPngWithOutput, renderToSvg, styles, toneGradients, typography, wrapWithBrand, wrapWithWatermark };
|
package/dist/index.js
CHANGED
|
@@ -521,6 +521,7 @@ function Panel({
|
|
|
521
521
|
width: "100%",
|
|
522
522
|
display: "flex",
|
|
523
523
|
flexDirection: "column",
|
|
524
|
+
gap,
|
|
524
525
|
...bodyStyle
|
|
525
526
|
}
|
|
526
527
|
}, children),
|
|
@@ -3936,16 +3937,16 @@ function BarChart({
|
|
|
3936
3937
|
yAxisLabel,
|
|
3937
3938
|
footer
|
|
3938
3939
|
}) {
|
|
3939
|
-
const margin = { top: 18, right:
|
|
3940
|
+
const margin = { top: 18, right: 38, bottom: 40, left: yAxisLabel ? 52 : 38 };
|
|
3940
3941
|
const plot = createPlotArea(width, height, margin);
|
|
3941
3942
|
const domain = chartDomain(data.map((item) => item.value), min, max);
|
|
3942
3943
|
const ticks = chartTicks(domain.min, domain.max, 4);
|
|
3943
|
-
const
|
|
3944
|
-
const barWidth = Math.max(
|
|
3944
|
+
const barSlot = plot.innerWidth / Math.max(data.length, 1);
|
|
3945
|
+
const barWidth = Math.max(8, Math.min(76, barSlot * 0.72));
|
|
3945
3946
|
const zeroY = yInPlot(0, domain, plot);
|
|
3946
3947
|
const baselineY = clamp(zeroY, plot.y, plot.bottom);
|
|
3947
3948
|
const barGeometries = data.map((item, index) => {
|
|
3948
|
-
const x = plot.x + index * (barWidth
|
|
3949
|
+
const x = plot.x + index * barSlot + (barSlot - barWidth) / 2;
|
|
3949
3950
|
const valueY = yInPlot(item.value, domain, plot);
|
|
3950
3951
|
const y = Math.min(valueY, baselineY);
|
|
3951
3952
|
const barHeight = Math.max(4, Math.abs(baselineY - valueY));
|
|
@@ -4834,7 +4835,7 @@ function cropPixels(pixels, srcWidth, region) {
|
|
|
4834
4835
|
}
|
|
4835
4836
|
return out;
|
|
4836
4837
|
}
|
|
4837
|
-
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold =
|
|
4838
|
+
function detectOverflow(pixels, width, height, bgColor, edgeTolerance = 3, minDensity = 0.05, alphaThreshold = 10) {
|
|
4838
4839
|
const isBackground = createBackgroundMatcher(bgColor);
|
|
4839
4840
|
function isContentPixel(x, y) {
|
|
4840
4841
|
const idx = (y * width + x) * 4;
|
|
@@ -5060,10 +5061,35 @@ function sanitizeSvg(svg) {
|
|
|
5060
5061
|
}
|
|
5061
5062
|
var AUTOCROP_PADDING = 24;
|
|
5062
5063
|
var CROP_THRESHOLD_PERCENT = 10;
|
|
5064
|
+
function replaceSvgAttribute(attrs, name, value) {
|
|
5065
|
+
const pattern = new RegExp(`\\s${name}="[^"]*"`);
|
|
5066
|
+
if (pattern.test(attrs)) return attrs.replace(pattern, ` ${name}="${value}"`);
|
|
5067
|
+
return `${attrs} ${name}="${value}"`;
|
|
5068
|
+
}
|
|
5069
|
+
function cropSvgViewport(svg, region) {
|
|
5070
|
+
const x = Math.max(0, Math.floor(region.x));
|
|
5071
|
+
const y = Math.max(0, Math.floor(region.y));
|
|
5072
|
+
const width = Math.max(1, Math.ceil(region.width));
|
|
5073
|
+
const height = Math.max(1, Math.ceil(region.height));
|
|
5074
|
+
return svg.replace(/^<svg\b([^>]*)>/, (_match, attrs) => {
|
|
5075
|
+
let nextAttrs = attrs;
|
|
5076
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "width", String(width));
|
|
5077
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "height", String(height));
|
|
5078
|
+
nextAttrs = replaceSvgAttribute(nextAttrs, "viewBox", `${x} ${y} ${width} ${height}`);
|
|
5079
|
+
return `<svg${nextAttrs}>`;
|
|
5080
|
+
});
|
|
5081
|
+
}
|
|
5082
|
+
function cropSavesEnough(source, target) {
|
|
5083
|
+
if (source <= 0 || target >= source) return false;
|
|
5084
|
+
return (source - target) / source * 100 >= CROP_THRESHOLD_PERCENT;
|
|
5085
|
+
}
|
|
5063
5086
|
function resolveWatermark(options) {
|
|
5064
5087
|
return options.watermark ?? options.brand;
|
|
5065
5088
|
}
|
|
5066
5089
|
async function renderToPng(element, options, createFn, theme) {
|
|
5090
|
+
await renderToPngWithOutput(element, options, createFn, theme);
|
|
5091
|
+
}
|
|
5092
|
+
async function renderToPngWithOutput(element, options, createFn, theme) {
|
|
5067
5093
|
return withRenderContext(
|
|
5068
5094
|
{ background: options.background ?? "transparent" },
|
|
5069
5095
|
() => renderToPngInContext(element, options, createFn, theme)
|
|
@@ -5104,7 +5130,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5104
5130
|
`Canvas overflow detected (${options.width}\xD7${options.height}): ${overflow.message}`
|
|
5105
5131
|
);
|
|
5106
5132
|
}
|
|
5107
|
-
|
|
5133
|
+
let finalWidth = options.width;
|
|
5108
5134
|
let finalHeight = options.height;
|
|
5109
5135
|
let finalElement = element;
|
|
5110
5136
|
const heightSaved = options.height - bounds.height;
|
|
@@ -5116,20 +5142,57 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5116
5142
|
}
|
|
5117
5143
|
}
|
|
5118
5144
|
const watermark = resolveWatermark(options);
|
|
5119
|
-
const
|
|
5120
|
-
|
|
5121
|
-
|
|
5122
|
-
|
|
5123
|
-
|
|
5124
|
-
|
|
5125
|
-
|
|
5126
|
-
|
|
5127
|
-
|
|
5128
|
-
|
|
5129
|
-
|
|
5130
|
-
|
|
5131
|
-
|
|
5132
|
-
|
|
5145
|
+
const renderFinalSvg = async (targetElement, targetWidth, targetHeight) => {
|
|
5146
|
+
const outputElement = watermark ? wrapWithWatermark(
|
|
5147
|
+
targetElement,
|
|
5148
|
+
targetWidth,
|
|
5149
|
+
targetHeight,
|
|
5150
|
+
theme || "dark",
|
|
5151
|
+
watermark
|
|
5152
|
+
) : targetElement;
|
|
5153
|
+
const renderedSvg = await satori(outputElement, {
|
|
5154
|
+
width: targetWidth,
|
|
5155
|
+
height: targetHeight,
|
|
5156
|
+
fonts,
|
|
5157
|
+
loadAdditionalAsset
|
|
5158
|
+
});
|
|
5159
|
+
return sanitizeSvg(renderedSvg);
|
|
5160
|
+
};
|
|
5161
|
+
let finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5162
|
+
if (finalHeight < options.height) {
|
|
5163
|
+
const cropCheck = new Resvg(finalSvg, {
|
|
5164
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5165
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5166
|
+
}).render();
|
|
5167
|
+
const cropPixels2 = new Uint8Array(cropCheck.pixels.buffer);
|
|
5168
|
+
const cropBg = detectBackgroundColor(cropPixels2);
|
|
5169
|
+
const cropOverflow = detectOverflow(cropPixels2, finalWidth, cropCheck.height, cropBg);
|
|
5170
|
+
if (cropOverflow.overflows) {
|
|
5171
|
+
finalHeight = options.height;
|
|
5172
|
+
finalElement = element;
|
|
5173
|
+
finalSvg = await renderFinalSvg(finalElement, finalWidth, finalHeight);
|
|
5174
|
+
}
|
|
5175
|
+
}
|
|
5176
|
+
if (options.crop !== false) {
|
|
5177
|
+
const viewportCheck = new Resvg(finalSvg, {
|
|
5178
|
+
fitTo: { mode: "width", value: finalWidth },
|
|
5179
|
+
background: "rgba(0, 0, 0, 0)"
|
|
5180
|
+
}).render();
|
|
5181
|
+
const viewportPixels = new Uint8Array(viewportCheck.pixels.buffer);
|
|
5182
|
+
const viewportBg = detectBackgroundColor(viewportPixels);
|
|
5183
|
+
const viewportBounds = detectContentBounds(
|
|
5184
|
+
viewportPixels,
|
|
5185
|
+
finalWidth,
|
|
5186
|
+
viewportCheck.height,
|
|
5187
|
+
viewportBg,
|
|
5188
|
+
AUTOCROP_PADDING
|
|
5189
|
+
);
|
|
5190
|
+
if (cropSavesEnough(finalWidth, viewportBounds.width) || cropSavesEnough(viewportCheck.height, viewportBounds.height)) {
|
|
5191
|
+
finalSvg = cropSvgViewport(finalSvg, viewportBounds);
|
|
5192
|
+
finalWidth = viewportBounds.width;
|
|
5193
|
+
finalHeight = viewportBounds.height;
|
|
5194
|
+
}
|
|
5195
|
+
}
|
|
5133
5196
|
const resvg = new Resvg(finalSvg, {
|
|
5134
5197
|
fitTo: {
|
|
5135
5198
|
mode: "width",
|
|
@@ -5141,6 +5204,7 @@ async function renderToPngInContext(element, options, createFn, theme) {
|
|
|
5141
5204
|
const pngBuffer = pngData.asPng();
|
|
5142
5205
|
await mkdir(dirname(options.outputPath), { recursive: true });
|
|
5143
5206
|
await writeFile(options.outputPath, pngBuffer);
|
|
5207
|
+
return { width: finalWidth, height: finalHeight };
|
|
5144
5208
|
}
|
|
5145
5209
|
async function renderToBuffer(element, width, height, options = {}) {
|
|
5146
5210
|
return withRenderContext(
|
|
@@ -5432,6 +5496,7 @@ export {
|
|
|
5432
5496
|
renderAnimatedGif,
|
|
5433
5497
|
renderToBuffer,
|
|
5434
5498
|
renderToPng,
|
|
5499
|
+
renderToPngWithOutput,
|
|
5435
5500
|
renderToSvg,
|
|
5436
5501
|
styles,
|
|
5437
5502
|
toneGradients,
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED