Skip to content

Commit 79992bd

Browse files
fix(barcode-generator-web): size Data Matrix SVG by viewBox aspect ratio
bwip-js SVGs carry only a viewBox, no width/height attributes, so the rendered element defaulted to intrinsic sizing instead of the configured size. Derive pixel dimensions from the viewBox and apply them via inline style.
1 parent c224451 commit 79992bd

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

packages/pluggableWidgets/barcode-generator-web/src/components/DataMatrix.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ function encodeDataMatrix(config: DataMatrixTypeConfig): string {
3939
: datamatrix({ ...opts, bcid: "datamatrix" }, drawingSVG());
4040
}
4141

42+
/** bwip-js SVGs only carry a viewBox, no width/height attributes; derive pixel dimensions from it. */
43+
function getSvgPixelSize(svg: string, size: number): { width: number; height: number } {
44+
const match = svg.match(/viewBox="0 0 (\d+(?:\.\d+)?) (\d+(?:\.\d+)?)"/);
45+
if (!match) {
46+
return { width: size, height: size };
47+
}
48+
const [viewBoxWidth, viewBoxHeight] = [parseFloat(match[1]), parseFloat(match[2])];
49+
return viewBoxWidth >= viewBoxHeight
50+
? { width: size, height: (size * viewBoxHeight) / viewBoxWidth }
51+
: { width: (size * viewBoxWidth) / viewBoxHeight, height: size };
52+
}
53+
4254
export function DataMatrixRenderer({ config }: DataMatrixRendererProps): ReactElement {
4355
const containerRef = useRef<HTMLDivElement>(null);
4456
const { codeValue, downloadButton, size, gs1Mode } = config;
@@ -96,10 +108,17 @@ export function DataMatrixRenderer({ config }: DataMatrixRendererProps): ReactEl
96108
/>
97109
);
98110

111+
const { width, height } = getSvgPixelSize(svg, size);
112+
99113
return (
100114
<div className="barcode-renderer datamatrix-renderer">
101115
{buttonPosition === "top" && button}
102-
<div ref={containerRef} className="datamatrix-svg" dangerouslySetInnerHTML={{ __html: svg }} />
116+
<div
117+
ref={containerRef}
118+
className="datamatrix-svg"
119+
style={{ width, height }}
120+
dangerouslySetInnerHTML={{ __html: svg }}
121+
/>
103122
{buttonPosition === "bottom" && button}
104123
</div>
105124
);

packages/pluggableWidgets/barcode-generator-web/src/ui/BarcodeGenerator.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ $widget-prefix: "barcode-generator";
2828
height: auto;
2929
}
3030

31+
.datamatrix-svg {
32+
max-width: 100%;
33+
34+
svg {
35+
display: block;
36+
width: 100%;
37+
height: 100%;
38+
}
39+
}
40+
3141
.qrcode-renderer-title {
3242
font-weight: var(--font-weight-normal);
3343
font-size: var(--font-size-small);
@@ -98,3 +108,13 @@ $widget-prefix: "barcode-generator";
98108
position: absolute;
99109
object-fit: contain;
100110
}
111+
112+
.datamatrix-preview-image {
113+
max-width: 100%;
114+
115+
svg {
116+
display: block;
117+
width: 100%;
118+
height: 100%;
119+
}
120+
}

0 commit comments

Comments
 (0)