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.

refactor: optimization of text layout when ends with lots of white space (#1893)

Ran Luo9b6879dd06cb
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 b9adf5eadc..e222f2f17b 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
@@ -81,6 +81,28 @@ export function layoutParagraph(
return [...pages];
}
+function isGlyphGroupEndWithWhiteSpaces(glyphGroup: IDocumentSkeletonGlyph[]) {
+ if (glyphGroup.length <= 1) {
+ return false;
+ }
+
+ let isInWhiteSpace = false;
+
+ for (const g of glyphGroup) {
+ if (g.content === DataStreamTreeTokenType.SPACE) {
+ isInWhiteSpace = true;
+ }
+
+ if (isInWhiteSpace &&
+ g.content !== DataStreamTreeTokenType.SPACE && g.content !== DataStreamTreeTokenType.PARAGRAPH && g.streamType !== DataStreamTreeTokenType.SECTION_BREAK
+ ) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
function _divideOperator(
glyphGroup: IDocumentSkeletonGlyph[],
pages: IDocumentSkeletonPage[],
@@ -118,6 +140,11 @@ function _divideOperator(
glyphGroup[0].content === DataStreamTreeTokenType.PARAGRAPH)
) {
addGlyphToDivide(divide, glyphGroup, preOffsetLeft);
+ } else if (
+ // If a line of text ends with consecutive spaces, the spaces should not be placed on the second line.
+ divideInfo.isLast && isGlyphGroupEndWithWhiteSpaces(glyphGroup)
+ ) {
+ addGlyphToDivide(divide, glyphGroup, preOffsetLeft);
} else if (divide?.glyphGroup.length === 0) {
const sliceGlyphGroup: IDocumentSkeletonGlyph[] = [];
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/layout.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/layout.ts
index fbbfbdd884..6bc313a57c 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/layout.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/layout.ts
@@ -14,14 +14,9 @@
* limitations under the License.
*/
-import type {
- IDocumentSkeletonPage,
- ISkeletonResourceReference,
-} from '../../../../../basics/i-document-skeleton-cached';
+import type { IDocumentSkeletonPage, ISkeletonResourceReference } from '../../../../../basics/i-document-skeleton-cached';
import type { ISectionBreakConfig } from '../../../../../basics/interfaces';
-import {
- clearFontCreateConfigCache,
-} from '../../tools';
+import { clearFontCreateConfigCache } from '../../tools';
import type { DataStreamTreeNode } from '../../../view-model/data-stream-tree-node';
import type { DocumentViewModel } from '../../../view-model/document-view-model';
import { shaping } from './shaping';
@@ -41,6 +36,7 @@ export function dealWidthParagraph(
const { endIndex, content = '' } = paragraphNode;
const paragraph = bodyModel.getParagraph(endIndex) || { startIndex: 0 };
+
const { paragraphStyle = {} } = paragraph;
const shapedTextList = shaping(