@json-to-office/core-docx 0.38.0 → 0.38.1
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.
|
@@ -2157,6 +2157,7 @@ import { ImageRun } from "docx";
|
|
|
2157
2157
|
var SVG_RASTER_SCALE = 3;
|
|
2158
2158
|
var SVG_MAX_EDGE_PX = 4096;
|
|
2159
2159
|
var SVG_MIN_EDGE_PX = 16;
|
|
2160
|
+
var SVG_MAX_TOTAL_PX = 1e6;
|
|
2160
2161
|
var resvgModule;
|
|
2161
2162
|
function loadResvg() {
|
|
2162
2163
|
resvgModule ??= import("@resvg/resvg-js");
|
|
@@ -2168,6 +2169,13 @@ function toRasterPx(px) {
|
|
|
2168
2169
|
Math.max(SVG_MIN_EDGE_PX, Math.round(px * SVG_RASTER_SCALE))
|
|
2169
2170
|
);
|
|
2170
2171
|
}
|
|
2172
|
+
function capToPixelBudget(edge, aspect) {
|
|
2173
|
+
if (!Number.isFinite(aspect) || aspect <= 0) return void 0;
|
|
2174
|
+
const total = edge * edge * aspect;
|
|
2175
|
+
if (total <= SVG_MAX_TOTAL_PX) return edge;
|
|
2176
|
+
const scaled = Math.floor(edge * Math.sqrt(SVG_MAX_TOTAL_PX / total));
|
|
2177
|
+
return scaled >= SVG_MIN_EDGE_PX ? scaled : void 0;
|
|
2178
|
+
}
|
|
2171
2179
|
async function rasterizeSvgFallback(svg, transformation) {
|
|
2172
2180
|
let Resvg;
|
|
2173
2181
|
try {
|
|
@@ -2184,8 +2192,22 @@ async function rasterizeSvgFallback(svg, transformation) {
|
|
|
2184
2192
|
const markup = svg.toString("utf-8");
|
|
2185
2193
|
const probeImage = new Resvg(markup);
|
|
2186
2194
|
const wide = probeImage.width / probeImage.height > transformation.width / transformation.height;
|
|
2187
|
-
const
|
|
2188
|
-
|
|
2195
|
+
const mode = wide ? "height" : "width";
|
|
2196
|
+
const value = capToPixelBudget(
|
|
2197
|
+
toRasterPx(wide ? transformation.height : transformation.width),
|
|
2198
|
+
wide ? probeImage.width / probeImage.height : probeImage.height / probeImage.width
|
|
2199
|
+
);
|
|
2200
|
+
if (value === void 0) {
|
|
2201
|
+
reportWarning(
|
|
2202
|
+
"image",
|
|
2203
|
+
"IMAGE_SVG_RASTER_SKIPPED",
|
|
2204
|
+
"Inline SVG is too far from square to rasterize within the fallback pixel budget, so its fallback only renders in Word 2016+."
|
|
2205
|
+
);
|
|
2206
|
+
return void 0;
|
|
2207
|
+
}
|
|
2208
|
+
return Buffer.from(
|
|
2209
|
+
new Resvg(markup, { fitTo: { mode, value } }).render().asPng()
|
|
2210
|
+
);
|
|
2189
2211
|
} catch (error) {
|
|
2190
2212
|
reportWarning(
|
|
2191
2213
|
"image",
|