Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(docs): line space between image and line (#3101)

Ran Luo32915ff8efc5
diff --git a/packages/engine-render/src/components/docs/document.ts b/packages/engine-render/src/components/docs/document.ts
index dac3eeb2f3..4dd3a19007 100644
--- a/packages/engine-render/src/components/docs/document.ts
+++ b/packages/engine-render/src/components/docs/document.ts
@@ -313,7 +313,7 @@ export class Documents extends DocComponent {
for (let i = 0; i < linesCount; i++) {
const line = lines[i];
- const { divides, asc = 0, type, lineHeight = 0, paddingTop } = line;
+ const { divides, asc = 0, type, lineHeight = 0 } = line;
const maxLineAsc = asc;
@@ -331,9 +331,22 @@ export class Documents extends DocComponent {
}
}
} else {
+ // let { x, y } = this._drawLiquid;
+ // x += horizontalOffsetNoAngle;
+ // y += verticalOffsetNoAngle + line.top;
+ // ctx.save();
+ // ctx.strokeStyle = 'rgb(245, 90, 34)';
+ // ctx.moveTo(x, y);
+ // ctx.lineTo(line.width ?? 0 + x, y);
+ // ctx.lineTo(line.width ?? 0 + x, lineHeight + y);
+ // ctx.lineTo(x, lineHeight + y);
+ // ctx.lineTo(x, y);
+ // ctx.stroke();
+ // ctx.restore();
+
this._drawLiquid.translateSave();
+ this._drawLiquid.translateLine(line, true, true);
- this._drawLiquid.translateLine(line, true);
rotateTranslateXListApply && this._drawLiquid.translate(rotateTranslateXListApply[i]); // x axis offset
const divideLength = divides.length;
@@ -360,7 +373,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft, paddingTop),
+ originTranslate.addByPoint(spanLeft, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -392,7 +405,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft + xOffset, paddingTop),
+ originTranslate.addByPoint(spanLeft + xOffset, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -402,7 +415,7 @@ export class Documents extends DocComponent {
const spanPointWithFont = calculateRectRotate(
originTranslate.addByPoint(
spanLeft + maxLineAscSin + xOffset,
- maxLineAscCos + paddingTop
+ maxLineAscCos
),
centerPoint,
centerAngle,
@@ -571,7 +584,7 @@ export class Documents extends DocComponent {
for (let i = 0; i < linesCount; i++) {
const line = lines[i];
- const { divides, asc = 0, type, lineHeight = 0, paddingTop } = line;
+ const { divides, asc = 0, type, lineHeight = 0 } = line;
const maxLineAsc = asc;
@@ -590,7 +603,7 @@ export class Documents extends DocComponent {
}
} else {
this._drawLiquid.translateSave();
- this._drawLiquid.translateLine(line, true);
+ this._drawLiquid.translateLine(line, true, true);
const divideLength = divides.length;
@@ -616,7 +629,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft, paddingTop),
+ originTranslate.addByPoint(spanLeft, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -648,7 +661,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft + xOffset, paddingTop),
+ originTranslate.addByPoint(spanLeft + xOffset, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -658,7 +671,7 @@ export class Documents extends DocComponent {
const spanPointWithFont = calculateRectRotate(
originTranslate.addByPoint(
spanLeft + maxLineAscSin + xOffset,
- maxLineAscCos + paddingTop
+ maxLineAscCos
),
centerPoint,
centerAngle,
@@ -777,7 +790,7 @@ export class Documents extends DocComponent {
for (let i = 0; i < linesCount; i++) {
const line = lines[i];
- const { divides, asc = 0, type, lineHeight = 0, paddingTop } = line;
+ const { divides, asc = 0, type, lineHeight = 0 } = line;
const maxLineAsc = asc;
@@ -796,7 +809,7 @@ export class Documents extends DocComponent {
}
} else {
this._drawLiquid.translateSave();
- this._drawLiquid.translateLine(line, true);
+ this._drawLiquid.translateLine(line, true, true);
const { y } = this._drawLiquid;
if (isHeader) {
@@ -835,7 +848,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft, paddingTop),
+ originTranslate.addByPoint(spanLeft, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -867,7 +880,7 @@ export class Documents extends DocComponent {
const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(spanLeft + xOffset, paddingTop),
+ originTranslate.addByPoint(spanLeft + xOffset, 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -877,7 +890,7 @@ export class Documents extends DocComponent {
const spanPointWithFont = calculateRectRotate(
originTranslate.addByPoint(
spanLeft + maxLineAscSin + xOffset,
- maxLineAscCos + paddingTop
+ maxLineAscCos
),
centerPoint,
centerAngle,
diff --git a/packages/engine-render/src/components/docs/extensions/line.ts b/packages/engine-render/src/components/docs/extensions/line.ts
index 1c6fd68dcc..f66c28b751 100644
--- a/packages/engine-render/src/components/docs/extensions/line.ts
+++ b/packages/engine-render/src/components/docs/extensions/line.ts
@@ -45,25 +45,25 @@ export class Line extends docExtension {
return;
}
- const { asc, dsc, paddingTop } = line;
+ const { asc, dsc } = line;
const { sp: strikeoutPosition, spo, sbo, bd } = bBox;
const scale = getScale(parentScale);
const DELTA = 0.5;
const { ul: underline, st: strikethrough, ol: overline, va: baselineOffset, bbl: bottomBorderLine } = textStyle;
if (underline) {
- const startY = asc + dsc + paddingTop;
+ const startY = asc + dsc;
this._drawLine(ctx, glyph, underline, startY, scale);
}
if (bottomBorderLine) {
- const startY = asc + dsc + 3 + paddingTop;
+ const startY = asc + dsc + 3;
this._drawLine(ctx, glyph, bottomBorderLine, startY, scale, 2);
}
if (strikethrough) {
// strikethrough position is the middle of bounding box ascent and descent.
- let startY = asc + bd - strikeoutPosition - DELTA + paddingTop;
+ let startY = asc + bd - strikeoutPosition - DELTA;
/**
* --------- superscript strikethrough position -------
@@ -82,7 +82,7 @@ export class Line extends docExtension {
}
if (overline) {
- const startY = -DEFAULT_OFFSET_SPACING - DELTA + paddingTop;
+ const startY = -DEFAULT_OFFSET_SPACING - DELTA;
this._drawLine(ctx, glyph, overline, startY, scale);
}
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
index 4db6660780..0e3a598fdc 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
@@ -354,16 +354,16 @@ function _divideOperator(
// If the height of the new content exceeds the height of the added row,
// the entire row needs to be recalculated according to the new height
// in order to handle the mixing of graphics and text
- const spanGroupCached = __getSpanGroupByLine(currentLine);
+ const spanGroupCached = __getGlyphGroupByLine(currentLine);
const spanGroupCachedLen = spanGroupCached.length;
- let newSpanGroup = [];
+ let newGlyphGroup = [];
let startIndex = 1;
if (spanGroupCachedLen > 2 && spanGroupCached[0].glyphType === GlyphType.LIST) {
- newSpanGroup = [spanGroupCached[0], spanGroupCached[1]];
+ newGlyphGroup = [spanGroupCached[0], spanGroupCached[1]];
startIndex = 2;
} else {
- newSpanGroup = [spanGroupCached[0]];
+ newGlyphGroup = [spanGroupCached[0]];
}
const column = currentLine.parent;
@@ -372,7 +372,7 @@ function _divideOperator(
_lineOperator(
ctx,
- newSpanGroup,
+ newGlyphGroup,
pages,
sectionBreakConfig,
paragraphConfig,
@@ -415,7 +415,7 @@ function _lineOperator(
paragraphConfig: IParagraphConfig,
paragraphStart: boolean = false,
breakPointType: BreakPointType = BreakPointType.Normal,
- defaultSpanLineHeight?: number
+ defaultGlyphLineHeight?: number
) {
let lastPage = getLastPage(pages);
let columnInfo = getLastNotFullColumnInfo(lastPage);
@@ -441,7 +441,7 @@ function _lineOperator(
const ascent = Math.max(...glyphGroup.map((glyph) => glyph.bBox.ba));
const descent = Math.max(...glyphGroup.map((glyph) => glyph.bBox.bd));
- const spanLineHeight = defaultSpanLineHeight || ascent + descent;
+ const glyphLineHeight = defaultGlyphLineHeight || (ascent + descent);
const {
paragraphStyle = {},
@@ -463,13 +463,15 @@ function _lineOperator(
indentEnd,
} = paragraphStyle;
- const { paragraphLineGapDefault, linePitch, lineSpacing, spacingRule, snapToGrid, gridType } = getLineHeightConfig(
+ const {
+ paragraphLineGapDefault, linePitch, lineSpacing, spacingRule, snapToGrid, gridType,
+ } = getLineHeightConfig(
sectionBreakConfig,
paragraphConfig
);
const { paddingTop, paddingBottom, contentHeight, lineSpacingApply } = __getLineHeight(
- spanLineHeight,
+ glyphLineHeight,
paragraphLineGapDefault,
linePitch,
gridType,
@@ -542,7 +544,7 @@ function _lineOperator(
// 行高超过Col高度,且列中已存在一行以上,且section大于一个;
// console.log('_lineOperator', { glyphGroup, pages, lineHeight, newLineTop, sectionHeight: section.height, lastPage });
setColumnFullState(column, true);
- _columnOperator(ctx, glyphGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, breakPointType, defaultSpanLineHeight);
+ _columnOperator(ctx, glyphGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, breakPointType, defaultGlyphLineHeight);
if (paragraphStart && paragraphAffectSkeDrawings && paragraphAffectSkeDrawings.size > 0) {
for (const drawing of paragraphAffectSkeDrawings.values()) {
@@ -607,7 +609,7 @@ function _lineOperator(
column.lines.push(newLine);
newLine.parent = column;
createAndUpdateBlockAnchor(paragraphIndex, newLine, lineTop, pDrawingAnchor);
- _divideOperator(ctx, glyphGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, breakPointType, defaultSpanLineHeight);
+ _divideOperator(ctx, glyphGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, breakPointType, defaultGlyphLineHeight);
}
function __updateAndPositionDrawings(
@@ -1004,6 +1006,7 @@ function __getParagraphSpace(
if (paragraphStart) {
let marginTop = getNumberUnitValue(spaceAbove, lineSpacing);
+
if (preLine) {
const { spaceBelowApply: preSpaceBelowApply } = preLine;
if (marginTop < preSpaceBelowApply) {
@@ -1021,6 +1024,7 @@ function __getParagraphSpace(
spaceBelowApply,
};
}
+
return {
marginTop: 0,
spaceBelowApply,
@@ -1036,7 +1040,7 @@ function __makeColumnsFull(columns: IDocumentSkeletonColumn[] = []) {
}
function __getLineHeight(
- spanLineHeight: number,
+ glyphLineHeight: number,
paragraphLineGapDefault: number,
linePitch: number,
gridType: GridType,
@@ -1054,15 +1058,15 @@ function __getLineHeight(
return {
paddingTop,
paddingBottom,
- contentHeight: lineSpacing * spanLineHeight,
- lineSpacingApply: spanLineHeight,
+ contentHeight: lineSpacing * glyphLineHeight,
+ lineSpacingApply: glyphLineHeight,
};
}
return {
paddingTop,
paddingBottom,
- contentHeight: Math.max(lineSpacing, spanLineHeight),
+ contentHeight: Math.max(lineSpacing, glyphLineHeight),
lineSpacingApply: lineSpacing,
};
}
@@ -1070,20 +1074,22 @@ function __getLineHeight(
// open xml $17.18.14 ST_DocGrid (Document Grid Types)
let lineSpacingApply = 0;
if (spacingRule === SpacingRule.AUTO) {
- // auto的情况下,lineSpacing代表行数
+ // auto 的情况下,lineSpacing代表行数
lineSpacingApply = lineSpacing * linePitch;
} else {
lineSpacingApply = lineSpacing;
}
- if (spanLineHeight + paragraphLineGapDefault * 2 < lineSpacingApply) {
- paddingTop = paddingBottom = (lineSpacingApply - spanLineHeight) / 2;
+ if (glyphLineHeight + paragraphLineGapDefault * 2 < lineSpacingApply) {
+ paddingTop = paddingBottom = (lineSpacingApply - glyphLineHeight) / 2;
+ } else {
+ lineSpacingApply = glyphLineHeight;
}
return {
paddingTop,
paddingBottom,
- contentHeight: spanLineHeight,
+ contentHeight: glyphLineHeight,
lineSpacingApply,
};
}
@@ -1129,7 +1135,7 @@ export function updateInlineDrawingPosition(
const glyphHeight = glyph.bBox.bd + glyph.bBox.ba;
drawing.aLeft = divide.left + divide.paddingLeft + glyph.left + 0.5 * glyph.width - 0.5 * width || 0;
- drawing.aTop = top + lineHeight - 0.5 * glyphHeight - 0.5 * height || 0;
+ drawing.aTop = top + lineHeight - 0.5 * glyphHeight - 0.5 * height;
drawing.width = width;
drawing.height = height;
drawing.angle = angle;
@@ -1298,7 +1304,7 @@ function __maxFontBoundingBoxByGlyphGroup(glyphGroup: IDocumentSkeletonGlyph[])
return maxBox;
}
-function __getSpanGroupByLine({ divides }: IDocumentSkeletonLine) {
+function __getGlyphGroupByLine({ divides }: IDocumentSkeletonLine) {
return divides.flatMap((divide) => divide.glyphGroup);
}
diff --git a/packages/engine-render/src/components/docs/liquid.ts b/packages/engine-render/src/components/docs/liquid.ts
index 4dc28ef4c5..8050cb2210 100644
--- a/packages/engine-render/src/components/docs/liquid.ts
+++ b/packages/engine-render/src/components/docs/liquid.ts
@@ -151,7 +151,7 @@ export class Liquid {
};
}
- translateLine(line: IDocumentSkeletonLine, isDraw = false) {
+ translateLine(line: IDocumentSkeletonLine, includeMarginTop = false, includePaddingTop = false) {
const {
top: lineTop,
marginBottom: _lineMarginBottom = 0,
@@ -159,7 +159,11 @@ export class Liquid {
paddingTop: linePaddingTop = 0,
paddingBottom: _linePaddingBottom = 0,
} = line;
- const lineOffset = lineTop + (isDraw === true ? lineMarginTop : 0) + linePaddingTop;
+
+ const lineOffset = lineTop +
+ (includeMarginTop ? lineMarginTop : 0) +
+ (includePaddingTop ? linePaddingTop : 0);
+
this.translate(0, lineOffset);
return {
x: 0,
diff --git a/packages/engine-render/src/components/docs/text-selection/convert-text-range.ts b/packages/engine-render/src/components/docs/text-selection/convert-text-range.ts
index 6765d8b3fa..2dba4658ee 100644
--- a/packages/engine-render/src/components/docs/text-selection/convert-text-range.ts
+++ b/packages/engine-render/src/components/docs/text-selection/convert-text-range.ts
@@ -290,7 +290,7 @@ export class NodePositionConvertToCursor {
const { start, end } = compareNodePosition(startOrigin, endOrigin);
this._selectionIterator(start, end, (start_sp, end_sp, isFirst, isLast, divide, line) => {
- const { lineHeight, marginTop, asc, paddingTop } = line;
+ const { lineHeight, asc, paddingTop, marginTop } = line;
const { glyphGroup, st } = divide;
if (glyphGroup.length === 0) {
// The divide is empty, and no need to set selection.
@@ -328,14 +328,14 @@ export class NodePositionConvertToCursor {
startX: startX + firstGlyphLeft + (isCurrentList ? firstGlyphWidth : 0),
startY,
endX: startX + lastGlyphLeft + lastGlyphWidth,
- endY: startY + lineHeight,
+ endY: startY + lineHeight - marginTop,
};
contentBoxPosition = {
startX: startX + firstGlyphLeft + (isCurrentList ? firstGlyphWidth : 0),
- startY: startY + marginTop + paddingTop + asc - anchorGlyph.bBox.ba,
+ startY: startY + paddingTop + asc - anchorGlyph.bBox.ba,
endX: startX + lastGlyphLeft + lastGlyphWidth,
- endY: startY + marginTop + paddingTop + asc + anchorGlyph.bBox.bd,
+ endY: startY + paddingTop + asc + anchorGlyph.bBox.bd,
};
} else {
const isStartBackFin = isStartBack && !isCurrentList;
@@ -344,14 +344,14 @@ export class NodePositionConvertToCursor {
startX: startX + firstGlyphLeft + (isStartBackFin ? 0 : firstGlyphWidth),
startY,
endX: startX + lastGlyphLeft + (isEndBack ? 0 : lastGlyphWidth),
- endY: startY + lineHeight,
+ endY: startY + lineHeight - marginTop,
};
contentBoxPosition = {
startX: startX + firstGlyphLeft + (isStartBackFin ? 0 : firstGlyphWidth),
- startY: startY + marginTop + paddingTop + asc - anchorGlyph.bBox.ba,
+ startY: startY + paddingTop + asc - anchorGlyph.bBox.ba,
endX: startX + lastGlyphLeft + (isEndBack ? 0 : lastGlyphWidth),
- endY: startY + marginTop + paddingTop + asc + anchorGlyph.bBox.bd,
+ endY: startY + paddingTop + asc + anchorGlyph.bBox.bd,
};
}
@@ -640,7 +640,7 @@ export class NodePositionConvertToCursor {
l
);
this._liquid.translateSave();
- this._liquid.translateLine(line);
+ this._liquid.translateLine(line, true, false);
for (let d = start_d; d <= end_d; d++) {
const divide = divides[d];