Commit 559afe384 for llama.cpp
commit 559afe384fa4e2f55005a263699441faf6256b3b
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date: Mon Nov 30 16:51:06 2026 +0100
ui: scale inline svg previews to fit the message (#33182)
Assisted-by: Claude (Opus 5.5 Medium)
diff --git a/ui/apps/chat/src/lib/constants/svg-blocks.constants.ts b/ui/apps/chat/src/lib/constants/svg-blocks.constants.ts
index 785e5a57f..354a00b9c 100644
--- a/ui/apps/chat/src/lib/constants/svg-blocks.constants.ts
+++ b/ui/apps/chat/src/lib/constants/svg-blocks.constants.ts
@@ -15,9 +15,12 @@ export const SVG = {
/**
* Shadow root style for an inline svg block. Mirrors the centered, padded
* sizing the light dom used before the svg moved behind a shadow boundary.
+ * The svg box includes its padding and stays within the message width and the
+ * block's max height (less the scroll container's 4rem of padding), so the
+ * whole drawing shows without scrolling.
*/
INLINE_SHADOW_STYLE:
- ':host{display:block;width:100%;text-align:center}svg{display:block;margin:0 auto;width:auto;height:auto;max-width:100%;max-height:70vh;min-height:8rem;padding:3rem 1rem}',
+ ':host{display:block;width:100%;text-align:center}svg{box-sizing:border-box;display:block;margin:0 auto;width:auto;height:auto;max-width:100%;max-height:min(70vh,calc(var(--max-message-height,70vh) - 4rem));min-height:8rem;padding:3rem 1rem}',
// Languages that mark a code block as svg content.
LANGUAGE: 'svg',
/**
diff --git a/ui/apps/chat/src/lib/utils/svg-shadow.ts b/ui/apps/chat/src/lib/utils/svg-shadow.ts
index 38f1ef92d..cefbe5b2a 100644
--- a/ui/apps/chat/src/lib/utils/svg-shadow.ts
+++ b/ui/apps/chat/src/lib/utils/svg-shadow.ts
@@ -1,3 +1,44 @@
+const SVG_VIEWBOX_ATTR = 'viewBox';
+const SVG_CSS_DPI = 96;
+const SVG_DIMENSION_PATTERN = /^\s*(\d*\.?\d+)\s*(px|in|cm|mm|pt|pc)?\s*$/;
+// user units (css px) per absolute length unit, at the css dpi
+const SVG_ABSOLUTE_UNIT_PX: Record<string, number> = {
+ '': 1,
+ cm: SVG_CSS_DPI / 2.54,
+ in: SVG_CSS_DPI,
+ mm: SVG_CSS_DPI / 25.4,
+ pc: SVG_CSS_DPI / 6,
+ pt: SVG_CSS_DPI / 72,
+ px: 1
+};
+
+/**
+ * Reads an absolute svg width/height attribute (unitless, px, in, cm, mm, pt or pc)
+ * as user units. Relative lengths such as % or em return null.
+ */
+export function readSvgDimension(value: string | null): number | null {
+ const match = value?.match(SVG_DIMENSION_PATTERN);
+
+ if (!match) return null;
+
+ const size = Number(match[1]) * SVG_ABSOLUTE_UNIT_PX[match[2] ?? ''];
+
+ return size > 0 ? size : null;
+}
+
+/**
+ * Gives an svg with a fixed width and height but no viewBox a matching viewBox,
+ * so css max-width scales the drawing instead of clipping it.
+ */
+function ensureSvgViewBox(svg: SVGSVGElement): void {
+ if (svg.hasAttribute(SVG_VIEWBOX_ATTR)) return;
+
+ const width = readSvgDimension(svg.getAttribute('width'));
+ const height = readSvgDimension(svg.getAttribute('height'));
+
+ if (width && height) svg.setAttribute(SVG_VIEWBOX_ATTR, `0 0 ${width} ${height}`);
+}
+
/**
* Mounts svg markup inside an open shadow root on the host element.
* The shadow boundary scopes the svg <style> and its animations to the host,
@@ -8,4 +49,8 @@ export function mountSvgShadow(host: HTMLElement, markup: string, style: string)
const root = host.shadowRoot ?? host.attachShadow({ mode: 'open' });
root.innerHTML = markup ? `<style>${style}</style>${markup}` : '';
+
+ const svg = root.querySelector('svg');
+
+ if (svg) ensureSvgViewBox(svg);
}
diff --git a/ui/apps/chat/tests/unit/svg-shadow.test.ts b/ui/apps/chat/tests/unit/svg-shadow.test.ts
new file mode 100644
index 000000000..46e4d68d0
--- /dev/null
+++ b/ui/apps/chat/tests/unit/svg-shadow.test.ts
@@ -0,0 +1,27 @@
+import { readSvgDimension } from '#lib/utils/svg-shadow.ts';
+import { describe, expect, it } from 'vitest';
+
+describe('svg-shadow readSvgDimension', () => {
+ it('reads unitless and px lengths as user units', () => {
+ expect(readSvgDimension('800')).toBe(800);
+ expect(readSvgDimension('800px')).toBe(800);
+ expect(readSvgDimension(' 12.5 ')).toBe(12.5);
+ });
+
+ it('converts absolute units at 96 dpi', () => {
+ expect(readSvgDimension('1in')).toBe(96);
+ expect(readSvgDimension('72pt')).toBeCloseTo(96, 10);
+ expect(readSvgDimension('6pc')).toBe(96);
+ expect(readSvgDimension('2.54cm')).toBeCloseTo(96, 10);
+ expect(readSvgDimension('25.4mm')).toBeCloseTo(96, 10);
+ });
+
+ it('returns null for relative, missing, zero or malformed lengths', () => {
+ expect(readSvgDimension(null)).toBeNull();
+ expect(readSvgDimension('100%')).toBeNull();
+ expect(readSvgDimension('10em')).toBeNull();
+ expect(readSvgDimension('auto')).toBeNull();
+ expect(readSvgDimension('0')).toBeNull();
+ expect(readSvgDimension('')).toBeNull();
+ });
+});