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.

19.2 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { IParagraphStyle } from '@univerjs/core';
import type {
    IDocumentSkeletonDivide,
    IDocumentSkeletonLine,
    IDocumentSkeletonPage,
} from '../../../../../basics/i-document-skeleton-cached';
import type { ISectionBreakConfig } from '../../../../../basics/interfaces';
import type { DataStreamTreeNode } from '../../../view-model/data-stream-tree-node';
import type { DocumentViewModel } from '../../../view-model/document-view-model';
import {
    BooleanNumber,
    characterSpacingControlType,
    DataStreamTreeTokenType,
    HorizontalAlign,
    resolveDocumentParagraphStyle,
    SpacingRule,
    WrapStrategy,
} from '@univerjs/core';
import { cjk } from '../../../../../basics/cjk-regexp';
import {
    isCjkLeftAlignedPunctuation,
    isCjkRightAlignedPunctuation,
} from '../../../../../basics/tools';
import { getDocumentCompatibilityPolicy, isTraditionalDocumentCompatibility } from '../../../document-compatibility';
import { BreakPointType } from '../../line-breaker/break';
import { isLetter } from '../../line-breaker/enhancers/utils';
import {
    createHyphenDashGlyph,
    getGlyphGroupFontBoundingBox,
    getGlyphGroupShrinkability,
    getGlyphGroupStretchability,
    glyphShrinkLeft,
    glyphShrinkRight,
    removeGlyphAutoSpacing,
    setGlyphGroupLeft,
} from '../../model/glyph';
import { getFontConfigFromLastGlyph, getGlyphGroupWidth, isParagraphEnd } from '../../tools';

// How much a character should hang into the end margin.
// For more discussion, see:
// https://recoveringphysicist.com/21/
// https://www.w3.org/TR/clreq/#hanging_punctuation_marks_at_line_end
function overhang(c: string): number {
    switch (c) {
        // Dashes.
        case '–':
        case '—': {
            return 0.2;
        }
        // Punctuation.
        case '.':
        case ',': {
            return 0.8;
        }
        case ':':
        case ';': {
            return 0.3;
        }
        // Arabic
        case '\u{60C}':
        case '\u{6D4}': {
            return 0.4;
        }
        default: {
            return 0;
        }
    }
}

function getJustifiables(divide: IDocumentSkeletonDivide): number {
    const justifiables = divide.glyphGroup.filter((glyph) => glyph.isJustifiable).length;
    const lastGlyph = divide.glyphGroup[divide.glyphGroup.length - 1];

    // CJK character at line end should not be adjusted.
    if (cjk.hasCJK(lastGlyph.content)) {
        return justifiables - 1;
    }

    return justifiables;
}

function adjustGlyphsInDivide(divide: IDocumentSkeletonDivide, justificationRatio: number, extraJustification: number) {
    const lastGlyph = divide.glyphGroup[divide.glyphGroup.length - 1];
    for (const glyph of divide.glyphGroup) {
        const adjustabilityLeft = justificationRatio < 0
            ? glyph.adjustability.shrinkability[0]
            : glyph.adjustability.stretchability[0];
        const adjustabilityRight = justificationRatio < 0
            ? glyph.adjustability.shrinkability[1]
            : glyph.adjustability.stretchability[1];

        const justificationLeft = adjustabilityLeft * justificationRatio;
        let justificationRight = adjustabilityRight * justificationRatio;

        if (glyph.isJustifiable && (glyph !== lastGlyph || !cjk.hasCJK(glyph.content))) {
            justificationRight += extraJustification;
        }

        glyph.width += justificationLeft + justificationRight;
        glyph.xOffset += justificationLeft;
    }

    setGlyphGroupLeft(divide.glyphGroup);
}

function distributeGlyphsInDivide(divide: IDocumentSkeletonDivide): boolean {
    const visibleGlyphs = divide.glyphGroup.filter((glyph) =>
        glyph.content !== '' &&
        glyph.width > 0 &&
        glyph.streamType !== DataStreamTreeTokenType.PARAGRAPH
    );

    if (visibleGlyphs.length < 2) {
        return false;
    }

    const lastGlyph = visibleGlyphs[visibleGlyphs.length - 1];
    const remaining = divide.width - lastGlyph.left - lastGlyph.width;
    if (remaining <= 0) {
        return false;
    }

    const extraGap = remaining / (visibleGlyphs.length - 1);

    for (let i = 0; i < visibleGlyphs.length - 1; i++) {
        visibleGlyphs[i].width += extraGap;
    }

    setGlyphGroupLeft(divide.glyphGroup);
    return true;
}

/**
 * When aligning text horizontally within a document,
 * it may be ineffective if the total line width is not initially calculated.
 * Therefore, multiple calculations are performed, which may impact performance.
 * Needs optimization for efficiency.
 */
function shouldAllowOverflowHorizontalOffset(sectionBreakConfig: ISectionBreakConfig): boolean {
    const wrapStrategy = sectionBreakConfig.renderConfig?.wrapStrategy;

    return wrapStrategy === WrapStrategy.OVERFLOW;
}

function getGlyphGroupInkBounds(divide: IDocumentSkeletonDivide): { left: number; right: number } | null {
    if (divide.glyphGroup.length === 0) {
        return null;
    }

    let left = Infinity;
    let right = -Infinity;

    for (const glyph of divide.glyphGroup) {
        const glyphLeft = glyph.left + glyph.xOffset;
        left = Math.min(left, glyphLeft);
        right = Math.max(right, glyphLeft + glyph.bBox.width);
    }

    if (!Number.isFinite(left) || !Number.isFinite(right)) {
        return null;
    }

    return { left, right };
}

function getAlignmentDivide(divide: IDocumentSkeletonDivide): IDocumentSkeletonDivide {
    const { glyphGroup } = divide;
    let end = glyphGroup.length;
    let hasTrailingSpace = false;
    while (end > 0) {
        const glyph = glyphGroup[end - 1];
        if (/^[ \u3000]+$/.test(glyph.content)) {
            hasTrailingSpace = true;
        } else if (glyph.width !== 0 || !['', '\r', '\n', '\u2028'].includes(glyph.content)) {
            break;
        }
        end--;
    }

    // Office hangs breakable spaces outside alignment, but retains tabs and
    // nonbreaking spaces. Share glyphs without removing editable source positions.
    return hasTrailingSpace ? { ...divide, glyphGroup: glyphGroup.slice(0, end) } : divide;
}

function horizontalAlignHandler(
    line: IDocumentSkeletonLine,
    horizontalAlign: HorizontalAlign,
    allowOverflowHorizontalOffset = false,
    hangingCjkPunctuation = false,
    excludeTrailingSpaces = false,
    preserveWordSpacing = false,
    isDrawingML = false,
    compressAlignedOverflow = false
) {
    const { divides } = line;

    for (let i = 0; i < divides.length; i++) {
        const divide = divides[i];
        const { width } = divide;

        if (divide.glyphGroup.length === 0) {
            divide.glyphGroupWidth = 0;
            divide.paddingLeft = 0;
            continue;
        }

        removeGlyphAutoSpacing(divide.glyphGroup[divide.glyphGroup.length - 1], 1);
        const alignmentDivide = isDrawingML || preserveWordSpacing ? getAlignmentDivide(divide) : divide;
        let glyphGroupWidth = getGlyphGroupWidth(alignmentDivide);

        divide.glyphGroupWidth = getGlyphGroupWidth(divide);

        if (width === Number.POSITIVE_INFINITY) {
            continue;
        }

        const justifiedWords = preserveWordSpacing
            && (horizontalAlign === HorizontalAlign.JUSTIFIED || horizontalAlign === HorizontalAlign.BOTH);
        const compressedAlignedText = compressAlignedOverflow && alignmentDivide.glyphGroup.some((glyph) => cjk.hasCJKText(glyph.content));
        if ((divide.isFull || ((justifiedWords || compressedAlignedText) && glyphGroupWidth > width)) && alignmentDivide.glyphGroup.length > 0) {
            let remaining = width - glyphGroupWidth;

            // Handle hanging punctuation to the right.
            // TODO: @jocs Handle hanging punctuation to the left if text dir is RTL.
            if (alignmentDivide.glyphGroup.length > 1) {
                const lastGlyph = alignmentDivide.glyphGroup[alignmentDivide.glyphGroup.length - 1];
                // A no-compression line can already contain a full CJK punctuation
                // glyph outside its text extent. Do not compress it back inside.
                const amount = hangingCjkPunctuation && glyphGroupWidth > width && isCjkLeftAlignedPunctuation(lastGlyph.content)
                    ? lastGlyph.width
                    : overhang(lastGlyph.content) * lastGlyph.width;

                remaining += preserveWordSpacing && !hangingCjkPunctuation ? 0 : amount;
            }

            let justificationRatio = 0;
            let extraJustification = 0;
            const shrink = getGlyphGroupShrinkability(alignmentDivide.glyphGroup);
            const stretch = getGlyphGroupStretchability(alignmentDivide.glyphGroup);

            if (remaining < 0 && shrink > 0 && (!preserveWordSpacing
                || horizontalAlign === HorizontalAlign.JUSTIFIED || horizontalAlign === HorizontalAlign.BOTH)) {
                // Attempt to reduce the length of the line, using shrinkability.
                justificationRatio = Math.max(remaining / shrink, -1.0);
                remaining = Math.min(remaining + shrink, 0);
            } else if (
                (horizontalAlign === HorizontalAlign.JUSTIFIED || horizontalAlign === HorizontalAlign.BOTH) &&
                !(i === divides.length - 1 && isParagraphEnd(line))
            ) {
                // Attempt to increase the length of the line, using stretchability.
                if (stretch > 0) {
                    justificationRatio = Math.min(remaining / stretch, 1.0);
                    remaining = Math.max(remaining - stretch, 0);
                }

                const justifiables = getJustifiables(alignmentDivide);

                if (justifiables > 0 && remaining > 0) {
                    extraJustification = remaining / justifiables;
                    remaining = 0;
                }
            }

            if (justificationRatio !== 0 || extraJustification !== 0) {
                // Extrude or stretch row so that they fit within a specified width,
                // or they can be squeezed or stretched to justify the row.
                adjustGlyphsInDivide(alignmentDivide, justificationRatio, extraJustification);
                if (alignmentDivide !== divide) {
                    setGlyphGroupLeft(divide.glyphGroup);
                }
                // Recalculate the glyph group width, because we adjust the width and xOffset of glyphs.
                glyphGroupWidth = getGlyphGroupWidth(alignmentDivide);
                divide.glyphGroupWidth = getGlyphGroupWidth(divide);
            }
        }

        const inkBounds = allowOverflowHorizontalOffset ? getGlyphGroupInkBounds(alignmentDivide) : null;
        const alignmentWidth = excludeTrailingSpaces ? getGlyphGroupWidth(divide, true) : glyphGroupWidth;

        if (horizontalAlign === HorizontalAlign.DISTRIBUTED) {
            if (distributeGlyphsInDivide(alignmentDivide)) {
                if (alignmentDivide !== divide) {
                    setGlyphGroupLeft(divide.glyphGroup);
                }
                divide.glyphGroupWidth = getGlyphGroupWidth(divide);
            }
            divide.paddingLeft = 0;
        } else if (horizontalAlign === HorizontalAlign.CENTER && inkBounds) {
            divide.paddingLeft = width / 2 - (inkBounds.left + inkBounds.right) / 2;
        } else if (horizontalAlign === HorizontalAlign.RIGHT && inkBounds) {
            divide.paddingLeft = width - inkBounds.right;
        } else if (horizontalAlign === HorizontalAlign.CENTER) {
            divide.paddingLeft = (width - alignmentWidth) / 2;
        } else if (horizontalAlign === HorizontalAlign.RIGHT) {
            divide.paddingLeft = width - alignmentWidth;
        }

        if (!allowOverflowHorizontalOffset) {
            // To fix https://github.com/dream-num/univer-pro/issues/2930
            divide.paddingLeft = Math.max(divide.paddingLeft, 0);
        }
    }
}

// If the first or last glyph is a CJK punctuation, we want to shrink it.
// See Requirements for Chinese Text Layout, Section 3.1.6.3
// Compression of punctuation marks at line start or line end
function shrinkStartAndEndCJKPunctuation(line: IDocumentSkeletonLine, preserveEndSpace = false) {
    for (const divide of line.divides) {
        const glyphGroupLength = divide.glyphGroup.length;
        if (glyphGroupLength < 2) {
            continue;
        }

        const firstGlyph = divide.glyphGroup[0];
        const lastGlyph = divide.glyphGroup[glyphGroupLength - 1];

        if (
            firstGlyph.ts?.textAdvance === undefined
            && isCjkRightAlignedPunctuation(firstGlyph.content)
        ) {
            const shrinkAmount = firstGlyph.adjustability.shrinkability[0];

            glyphShrinkLeft(firstGlyph, shrinkAmount);
        }

        if (
            !preserveEndSpace
            && lastGlyph.ts?.textAdvance === undefined
            && isCjkLeftAlignedPunctuation(lastGlyph.content)
        ) {
            const shrinkAmount = lastGlyph.adjustability.shrinkability[1];

            glyphShrinkRight(lastGlyph, shrinkAmount);
        }

        setGlyphGroupLeft(divide.glyphGroup);
    }
}

// Add dash to the end of divide when divide is break by Hyphen.
function addHyphenDash(
    line: IDocumentSkeletonLine,
    viewModel: DocumentViewModel,
    paragraphNode: DataStreamTreeNode,
    sectionBreakConfig: ISectionBreakConfig,
    paragraphStyle: IParagraphStyle
) {
    for (const divide of line.divides) {
        const { glyphGroup, breakType } = divide;
        const lastGlyph = glyphGroup[glyphGroup.length - 1];

        if (lastGlyph && isLetter(lastGlyph.content) && breakType === BreakPointType.Hyphen) {
            const config = getFontConfigFromLastGlyph(lastGlyph, sectionBreakConfig, paragraphStyle);

            const hyphenDashGlyph = createHyphenDashGlyph(config);
            hyphenDashGlyph.parent = lastGlyph.parent;
            hyphenDashGlyph.left = lastGlyph.left + lastGlyph.width;
            divide.glyphGroup.push(hyphenDashGlyph);
            // In latin paragraph layout, most lines end with spaces,
            // and when hyphens are added to some lines, the hyphens will bulge out,
            // and when the ends are aligned, they will not appear to be aligned,
            // so the hyphenated divide needs to be compressed
            divide.width -= hyphenDashGlyph.width;
        }
    }
}

export function lineAdjustment(
    pages: IDocumentSkeletonPage[],
    viewModel: DocumentViewModel,
    paragraphNode: DataStreamTreeNode,
    sectionBreakConfig: ISectionBreakConfig
) {
    const { endIndex } = paragraphNode;
    const paragraph = viewModel.getParagraph(endIndex) || { startIndex: 0, paragraphId: 'para_render_fallback' };

    const { paragraphStyle = {} } = paragraph;
    const { horizontalAlign = HorizontalAlign.UNSPECIFIED } = paragraphStyle;
    const snapshot = viewModel.getSnapshot();
    const documentStyle = snapshot.documentStyle;
    const policy = sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy(documentStyle.documentFlavor);
    const traditional = isTraditionalDocumentCompatibility(policy);
    const excludeTrailingSpaces = traditional && (horizontalAlign === HorizontalAlign.CENTER || horizontalAlign === HorizontalAlign.RIGHT);
    // Fitting can borrow punctuation space even on the terminal line of an
    // aligned paragraph. Apply that contraction before computing its offset.
    const compressAlignedOverflow = excludeTrailingSpaces
        && (sectionBreakConfig.characterSpacingControl ?? documentStyle.characterSpacingControl) !== characterSpacingControlType.doNotCompress;
    const resolvedParagraphStyle = resolveDocumentParagraphStyle(documentStyle, paragraphStyle, {
        styles: snapshot.styles,
        paragraphStyleId: paragraph.styleId,
    });
    const hangingCjkPunctuation = traditional && (sectionBreakConfig.characterSpacingControl ?? documentStyle.characterSpacingControl) === characterSpacingControlType.doNotCompress
        && resolvedParagraphStyle.hangingPunctuation === BooleanNumber.TRUE;
    for (const page of pages) {
        for (const section of page.sections) {
            for (const column of section.columns) {
                const { lines } = column;
                // Line breaking appends logical paragraph order; pagination moves
                // ordered suffixes. A long cell must not rescan its growing prefix
                // for every paragraph's punctuation and alignment pass.
                let low = 0;
                let high = lines.length;
                while (low < high) {
                    const middle = Math.floor((low + high) / 2);
                    if (lines[middle].paragraphIndex < paragraph.startIndex) {
                        low = middle + 1;
                    } else {
                        high = middle;
                    }
                }
                for (let index = low; index < lines.length && lines[index].paragraphIndex === paragraph.startIndex; index++) {
                    const line = lines[index];
                    if (traditional && resolvedParagraphStyle.spacingRule === SpacingRule.EXACT && line.contentHeight > 0.01 &&
                        sectionBreakConfig.renderConfig?.topAlignExactLineSpacing !== BooleanNumber.TRUE &&
                        !line.divides.some((divide) => divide.glyphGroup.some((glyph) => glyph.streamType === DataStreamTreeTokenType.CUSTOM_BLOCK && glyph.width > 0))) {
                        // Word places text at 80% of the exact line box, independent of font ascent.
                        // Keep the line box unchanged so pagination and following objects do not move.
                        const lineBoxHeight = line.paddingTop + line.contentHeight + line.paddingBottom;
                        const { boundingBoxAscent } = getGlyphGroupFontBoundingBox(policy, ...line.divides.map((divide) => divide.glyphGroup));
                        line.paddingTop = lineBoxHeight * 0.8 - boundingBoxAscent;
                        line.paddingBottom = lineBoxHeight - line.contentHeight - line.paddingTop;
                    }
                    shrinkStartAndEndCJKPunctuation(line, hangingCjkPunctuation);
                    addHyphenDash(line, viewModel, paragraphNode, sectionBreakConfig, paragraphStyle);
                    const preserveWordSpacing = traditional
                        && (horizontalAlign === HorizontalAlign.UNSPECIFIED || horizontalAlign === HorizontalAlign.LEFT
                            || horizontalAlign === HorizontalAlign.JUSTIFIED || horizontalAlign === HorizontalAlign.BOTH)
                        && !line.divides.some((divide) => divide.glyphGroup.some((glyph) => cjk.hasCJKText(glyph.content)));
                    horizontalAlignHandler(line, horizontalAlign, shouldAllowOverflowHorizontalOffset(sectionBreakConfig), hangingCjkPunctuation, excludeTrailingSpaces, preserveWordSpacing, sectionBreakConfig.documentCompatibilityPolicy?.mode === 'drawingml', compressAlignedOverflow);
                }
            }
        }
    }
}