Skip to content

Commit

Permalink
refactor: strikethrough
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Apr 10, 2024
1 parent 8caee25 commit 862b257
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
4 changes: 2 additions & 2 deletions examples/src/docs/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverUIPlugin } from '@univerjs/ui';

import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { DEFAULT_DOCUMENT_DATA_EN } from '../data';
import { DEFAULT_DOCUMENT_DATA_CN } from '../data';
import { DebuggerPlugin } from '../plugins/debugger';
import { locales } from './locales';

Expand Down Expand Up @@ -61,7 +61,7 @@ univer.registerPlugin(UniverDocsUIPlugin, {
},
});

univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_EN);
univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);

// use for console test
declare global {
Expand Down
13 changes: 7 additions & 6 deletions packages/engine-render/src/basics/font-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import type { IDocumentSkeletonBoundingBox, IDocumentSkeletonFontStyle } from '.
import type { IMeasureTextCache } from './interfaces';

const getDefaultBaselineOffset = (fontSize: number) => ({
sbr: 0.5,
sbr: 0.6,
sbo: fontSize,
spr: 0.5,
spr: 0.6,
spo: fontSize,
});

Expand Down Expand Up @@ -340,10 +340,11 @@ export class FontCache {
aba: actualBoundingBoxAscent,
abd: actualBoundingBoxDescent,
sp: (fontBoundingBoxAscent + fontBoundingBoxDescent) / 2,
sbr: 0.5,
sbo: fontSize,
spr: 0.5,
spo: fontSize,
sbr: 0.6,
spr: 0.6,
// https://en.wikipedia.org/wiki/Subscript_and_superscript Microsoft Word 2015
sbo: (fontBoundingBoxAscent + fontBoundingBoxDescent) * 0.141,
spo: (fontBoundingBoxAscent + fontBoundingBoxDescent) * 0.4,
};
}
}
6 changes: 5 additions & 1 deletion packages/engine-render/src/basics/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,13 @@ export function getFontStyleString(textStyle?: IStyleBase, localeService?: Local

const { va: baselineOffset } = textStyle;

if (baselineOffset === BaselineOffset.SUBSCRIPT || baselineOffset === BaselineOffset.SUPERSCRIPT) {
if (
baselineOffset === BaselineOffset.SUBSCRIPT ||
baselineOffset === BaselineOffset.SUPERSCRIPT
) {
const baselineOffsetInfo = FontCache.getBaselineOffsetInfo(fontFamilyResult, fontSize);
const { sbr, spr } = baselineOffsetInfo;

fontSize *= baselineOffset === BaselineOffset.SUBSCRIPT ? sbr : spr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,10 @@ export class FontAndBaseLine extends docExtension {
ctx.fillStyle = fontColor;
}

if (baselineOffset === BaselineOffset.SUBSCRIPT || baselineOffset === BaselineOffset.SUPERSCRIPT) {
// const { renderConfig = {} } = this.extensionOffset;

// const { centerAngle = 0 } = renderConfig;
let offset = 0;

if (baselineOffset === BaselineOffset.SUPERSCRIPT) {
offset = -bBox.sbo;
}

// const offsetSin = offset * Math.sin(centerAngle);
// const offsetCos = offset * Math.cos(centerAngle);

spanPointWithFont.y += offset;
if (baselineOffset === BaselineOffset.SUPERSCRIPT) {
spanPointWithFont.y += -bBox.spo;
} else if (baselineOffset === BaselineOffset.SUBSCRIPT) {
spanPointWithFont.y += bBox.sbo;
}

// console.log(content, spanPointWithFont.x, spanPointWithFont.y, startX, startY);
Expand Down
20 changes: 14 additions & 6 deletions packages/engine-render/src/components/docs/extensions/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Line extends docExtension {
return;
}

const { sp: strikeoutPosition, ba } = bBox;
const { sp: strikeoutPosition, spo, sbo } = bBox;

const scale = getScale(parentScale);

Expand All @@ -64,12 +64,20 @@ export class Line extends docExtension {
}

if (strikethrough) {
// We use the asc baseline to find the position of the strikethrough,
// and ba - strikeoutPosition is exactly the offset position of the strikethrough from the baseline
let startY = asc - (ba - strikeoutPosition) - DELTA;

// strikethrough position is the middle of bounding box ascent and descent.
let startY = strikeoutPosition - DELTA;

/**
* --------- superscript strikethrough position -------
* --------- superscript offset -----------------------
* --------- baseline -----------------------
* --------- subscript strikethrough position ---------
* --------- subscript offset -----------------------
*/
if (baselineOffset === BaselineOffset.SUPERSCRIPT) {
startY = strikeoutPosition - DELTA;
startY = asc - spo - strikeoutPosition;
} else if (baselineOffset === BaselineOffset.SUBSCRIPT) {
startY = asc + sbo - strikeoutPosition;
}

this._drawLine(ctx, span, strikethrough, startY, scale);
Expand Down

0 comments on commit 862b257

Please sign in to comment.