feat: an as-char frame sits where the file says it does

ODF style:vertical-pos/-rel on an inline frame, which every LibreOffice
formula image carries: baseline, character area, or a plain offset from the
baseline. The default moves from the line-box bottom to the baseline, so a
line with a picture in it keeps the descent below it, as LibreOffice and
Word both do.
This commit is contained in:
Steffen Becker committed 2026-08-10 20:48:31 +00:00
1 parent 970ad3776a
commit af144362fe
6 files changed
+133 -7

No files matched your search

+34
View File
@@ -81,6 +81,27 @@ export function frameMargins(wrap: WrapMode, offsetCm: unknown, boxWidthPx: numb
return `0 ${far} 0 14px`;
}
// Where an as-char frame sits against the line (ODF style:vertical-pos/-rel, probed
// against LibreOffice): its bottom on the baseline by default, `text-*` against the
// character area, `offset` its top wrapOffsetY cm below the baseline.
export type InlineVAlign = 'middle' | 'below' | 'text-top' | 'text-middle' | 'text-bottom' | 'offset';
export function inlineVerticalAlign(vAlign: unknown, boxHeightPx: number, offsetYCm: unknown): string {
if (!boxHeightPx) return '';
switch (vAlign) {
case 'middle': return `${-boxHeightPx / 2}px`;
case 'below': return `${-boxHeightPx}px`;
case 'text-top': return 'text-top';
case 'text-bottom': return 'text-bottom';
// No CSS keyword centres on the character area; 0.36em stands in for half of
// (ascent − descent), which Liberation Serif/Sans, Times and Arial all share.
case 'text-middle': return `calc(0.36em - ${boxHeightPx / 2}px)`;
case 'offset':
return typeof offsetYCm === 'number' ? `${-(cmToPx(offsetYCm) + boxHeightPx)}px` : '';
default: return '';
}
}
// The page text height in px, capping how tall an image can be stretched. Read live
// from the :root vars the editor maintains (orientation/margins change them).
export function pageContentHeightPx(): number {
@@ -157,6 +178,13 @@ export const Image = Node.create({
parseHTML: el => (el as HTMLElement).getAttribute('data-wrap-align') || null,
renderHTML: () => ({}),
},
// Where an as-char frame sits against the line (see inlineVerticalAlign).
// null = its bottom on the baseline, which is LibreOffice's and Word's default.
vAlign: {
default: null,
parseHTML: el => (el as HTMLElement).getAttribute('data-v-align') || null,
renderHTML: () => ({}),
},
};
},
@@ -171,10 +199,12 @@ export const Image = Node.create({
const wrap = (node.attrs.wrap as WrapMode) || 'inline';
const offset = node.attrs.wrapOffset as number | null;
const offsetY = node.attrs.wrapOffsetY as number | null;
const va = h ? inlineVerticalAlign(node.attrs.vAlign, h, offsetY) : '';
const style = [
w ? `width:${w}px` : '',
h ? `height:${h}px` : '',
rot ? `transform:rotate(${rot}deg)` : '',
va ? `vertical-align:${va}` : '',
].filter(Boolean).join(';');
return ['img', mergeAttributes(HTMLAttributes, {
...(style ? { style } : {}),
@@ -183,6 +213,7 @@ export const Image = Node.create({
...(offset != null ? { 'data-wrap-offset': String(offset) } : {}),
...(offsetY != null ? { 'data-wrap-offset-y': String(offsetY) } : {}),
...(node.attrs.wrapAlign ? { 'data-wrap-align': String(node.attrs.wrapAlign) } : {}),
...(node.attrs.vAlign ? { 'data-v-align': String(node.attrs.vAlign) } : {}),
...(node.attrs.anchorPage ? { 'data-anchor-page': String(node.attrs.anchorPage) } : {}),
})];
},
@@ -301,11 +332,14 @@ class ImageView {
const bh = Math.abs(w * Math.sin(rad)) + Math.abs(h * Math.cos(rad));
this.dom.style.width = `${bw}px`;
this.dom.style.height = `${bh}px`;
this.dom.style.verticalAlign =
inlineVerticalAlign(this.node.attrs.vAlign, bh, this.node.attrs.wrapOffsetY);
} else {
this.rotor.style.width = '';
this.rotor.style.height = '';
this.dom.style.width = '';
this.dom.style.height = '';
this.dom.style.verticalAlign = '';
}
this.rotor.style.transform = `translate(-50%, -50%) rotate(${deg}deg)`;
}
+25 -4
View File
@@ -379,7 +379,7 @@ function replaceSectionBreaks(doc: TiptapNode): TiptapNode {
// bytes is ArrayBuffer-backed to match fflate's zip entry map. rotationDeg is CW;
// wrap floats the frame at its anchor paragraph (left/right/top-bottom).
type WrapMode = 'inline' | 'left' | 'right' | 'topBottom';
type ImageExport = { path: string; bytes: Uint8Array<ArrayBuffer>; mimeType: string; widthCm: number; heightCm: number; alt: string; rotationDeg: number; wrap: WrapMode; wrapOffsetCm: number | null; wrapOffsetYCm: number | null; wrapAlign: string | null; anchorPage: number | null };
type ImageExport = { path: string; bytes: Uint8Array<ArrayBuffer>; mimeType: string; widthCm: number; heightCm: number; alt: string; rotationDeg: number; wrap: WrapMode; wrapOffsetCm: number | null; wrapOffsetYCm: number | null; wrapAlign: string | null; anchorPage: number | null; vAlign: string | null };
function base64ToBytes(b64: string): Uint8Array<ArrayBuffer> {
const bin = atob(b64);
@@ -424,6 +424,7 @@ function imageDescriptor(node: TiptapNode, index: number, namePrefix = 'image'):
wrapOffsetYCm: typeof node.attrs?.wrapOffsetY === 'number' ? round3(node.attrs.wrapOffsetY) : null,
wrapAlign: node.attrs?.wrapAlign === 'left' || node.attrs?.wrapAlign === 'right' ? node.attrs.wrapAlign : null,
anchorPage: typeof node.attrs?.anchorPage === 'number' && node.attrs.anchorPage > 0 ? node.attrs.anchorPage : null,
vAlign: typeof node.attrs?.vAlign === 'string' ? node.attrs.vAlign : null,
};
}
@@ -2798,6 +2799,17 @@ function imageWrapProps(wrap: WrapMode, offset: number | null, align?: string |
return `style:wrap="none" style:horizontal-pos="${align ?? pos ?? 'center'}"`;
}
// The inverse of the importer's as-char alignment map (import/odt.ts): a frame with no
// entry stands on the baseline, which is what a style-less as-char frame does anyway.
const INLINE_VALIGN_ODF: Record<string, string> = {
middle: 'style:vertical-pos="middle" style:vertical-rel="baseline"',
below: 'style:vertical-pos="bottom" style:vertical-rel="baseline"',
'text-top': 'style:vertical-pos="top" style:vertical-rel="text"',
'text-middle': 'style:vertical-pos="middle" style:vertical-rel="text"',
'text-bottom': 'style:vertical-pos="bottom" style:vertical-rel="text"',
offset: 'style:vertical-pos="from-top" style:vertical-rel="text"',
};
// Graphic style for a floating frame (wrap + side, anchored to the paragraph top).
// Inline images need none. Injected into content.xml automatic-styles by applyImages.
function imageGraphicStyle(img: ImageExport, index: number): string {
@@ -2809,7 +2821,13 @@ function imageGraphicStyle(img: ImageExport, index: number): string {
` style:vertical-rel="page" style:vertical-pos="from-top"/></style:style>`
);
}
if (img.wrap === 'inline') return '';
if (img.wrap === 'inline') {
const v = INLINE_VALIGN_ODF[img.vAlign ?? ''];
return v
? `<style:style style:name="ImgFr${index + 1}" style:family="graphic">` +
`<style:graphic-properties style:wrap="none" ${v}/></style:style>`
: '';
}
return (
`<style:style style:name="ImgFr${index + 1}" style:family="graphic">` +
`<style:graphic-properties ${imageWrapProps(img.wrap, img.wrapOffsetCm, img.wrapAlign)}` +
@@ -2833,9 +2851,12 @@ function imageFrameXml(img: ImageExport, index: number): string {
const anchor = img.anchorPage != null
? ` text:anchor-type="page" text:anchor-page-number="${img.anchorPage}"`
: ` text:anchor-type="${floats ? 'paragraph' : 'as-char'}"`;
const styleName = floats ? ` draw:style-name="ImgFr${index + 1}"` : '';
const named = floats || (img.vAlign != null && img.vAlign in INLINE_VALIGN_ODF);
const styleName = named ? ` draw:style-name="ImgFr${index + 1}"` : '';
const x = img.wrapOffsetCm != null && floats && !img.wrapAlign ? ` svg:x="${img.wrapOffsetCm}cm"` : '';
const y = img.wrapOffsetYCm != null && floats ? ` svg:y="${img.wrapOffsetYCm}cm"` : '';
// An as-char frame carries svg:y only for the offset alignment, which is what it means.
const y = img.wrapOffsetYCm != null && (floats || img.vAlign === 'offset')
? ` svg:y="${img.wrapOffsetYCm}cm"` : '';
return (
`<draw:frame draw:name="Image${index + 1}"${styleName}${anchor} draw:z-index="${index}"${dims}${x}${y}${imageTransform(img)}>` +
`${inner}</draw:frame>`
+21 -1
View File
@@ -149,11 +149,31 @@ function frameRotationDeg(el: Element): number {
// Rotation + wrap attrs, shared by images, text boxes and shapes. A non-as-char anchor or
// an explicit style:wrap floats the element (free x/y collapse to the nearest side). An
// explicit as-char anchor stays inline: LibreOffice's Graphics style carries a style:wrap.
// Where an as-char frame sits against the line. `baseline`/`char` measure from the text
// baseline, `text`/`line` from the character area, and `from-top` puts the frame's top
// svg:y below the baseline whatever the relation says (all probed against LibreOffice).
function applyInlineVAlign(el: Element, attrs: Record<string, unknown>, gp: PropMap): void {
const pos = gp['style:vertical-pos'];
const rel = gp['style:vertical-rel'];
const area = rel === 'text' || rel === 'line';
if (pos === 'from-top') {
const y = lengthToCm(el.getAttributeNS(NS.svg, 'y'));
if (y == null) return;
attrs.vAlign = 'offset';
attrs.wrapOffsetY = Math.round(y * 1000) / 1000;
} else if (pos === 'middle') attrs.vAlign = area ? 'text-middle' : 'middle';
else if (pos === 'bottom') attrs.vAlign = area ? 'text-bottom' : 'below';
else if (pos === 'top' && area) attrs.vAlign = 'text-top';
}
function applyFrameRotationAndWrap(el: Element, attrs: Record<string, unknown>, gp: PropMap): void {
const deg = frameRotationDeg(el);
if (deg) attrs.rotation = deg;
const anchor = el.getAttributeNS(NS.text, 'anchor-type');
if (anchor === 'as-char') return;
if (anchor === 'as-char') {
applyInlineVAlign(el, attrs, gp);
return;
}
const wrapVal = gp['style:wrap'];
if (anchor || wrapVal) {
attrs.wrap = wrapModeFromOdf(wrapVal, gp['style:horizontal-pos']);
+3 -1
View File
@@ -274,7 +274,9 @@
.paper .tiptap .image-node {
position: relative;
display: inline-block;
vertical-align: bottom;
/* An as-char frame stands on the text baseline in LibreOffice and Word alike; the
file may say otherwise, and image.ts then overrides this per frame. */
vertical-align: baseline;
line-height: 0;
}
/* While selected, lift the node (and its handles) above the header/footer layer