mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
Before (grey area as rendered canvas bitmap): <img width="1114" alt="image" src="https://github.com/user-attachments/assets/9a209818-c388-4e55-af9b-116f24bd8027" /> After: <img width="1103" alt="image" src="https://github.com/user-attachments/assets/1102264a-ec21-4c0c-b4b6-e82a64b1a844" />
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { type SectionLayout } from './types.js';
|
|
|
|
const meta = {
|
|
emSize: 2048,
|
|
hHeadAscent: 1984,
|
|
hHeadDescent: -494,
|
|
};
|
|
|
|
const font = new FontFace(
|
|
'Inter',
|
|
`url(https://fonts.gstatic.com/s/inter/v18/UcCo3FwrK3iLTcviYwYZ8UA3.woff2)`
|
|
);
|
|
// @ts-expect-error worker env
|
|
self.fonts && self.fonts.add(font);
|
|
font.load().catch(console.error);
|
|
|
|
function getBaseline() {
|
|
const fontSize = 15;
|
|
const lineHeight = 1.2 * fontSize;
|
|
|
|
const A = fontSize * (meta.hHeadAscent / meta.emSize); // ascent
|
|
const D = fontSize * (meta.hHeadDescent / meta.emSize); // descent
|
|
const AD = A + Math.abs(D); // ascent + descent
|
|
const L = lineHeight - AD; // leading
|
|
const y = A + L / 2;
|
|
return y;
|
|
}
|
|
|
|
class CanvasWorkerManager {
|
|
private canvas: OffscreenCanvas | null = null;
|
|
private ctx: OffscreenCanvasRenderingContext2D | null = null;
|
|
|
|
init(width: number, height: number, dpr: number) {
|
|
this.canvas = new OffscreenCanvas(width * dpr, height * dpr);
|
|
this.ctx = this.canvas.getContext('2d')!;
|
|
this.ctx.scale(dpr, dpr);
|
|
this.ctx.fillStyle = 'lightgrey';
|
|
this.ctx.fillRect(0, 0, width, height);
|
|
}
|
|
|
|
draw(section: SectionLayout) {
|
|
const { canvas, ctx } = this;
|
|
if (!canvas || !ctx) return;
|
|
|
|
// Track rendered positions to avoid duplicate rendering across all paragraphs and sentences
|
|
const renderedPositions = new Set<string>();
|
|
|
|
section.paragraphs.forEach(paragraph => {
|
|
const scale = paragraph.scale ?? 1;
|
|
const fontSize = 15 * scale;
|
|
ctx.font = `${fontSize}px Inter`;
|
|
const baselineY = getBaseline() * scale;
|
|
|
|
paragraph.sentences.forEach(sentence => {
|
|
ctx.strokeStyle = 'yellow';
|
|
sentence.rects.forEach(textRect => {
|
|
const x = textRect.rect.left - section.rect.x;
|
|
const y = textRect.rect.top - section.rect.y;
|
|
|
|
const posKey = `${x},${y}`;
|
|
// Only render if we haven't rendered at this position before
|
|
if (renderedPositions.has(posKey)) return;
|
|
|
|
ctx.strokeRect(x, y, textRect.rect.width, textRect.rect.height);
|
|
ctx.fillStyle = 'black';
|
|
ctx.fillText(textRect.text, x, y + baselineY);
|
|
|
|
renderedPositions.add(posKey);
|
|
});
|
|
});
|
|
});
|
|
|
|
const bitmap = canvas.transferToImageBitmap();
|
|
self.postMessage({ type: 'render', bitmap }, { transfer: [bitmap] });
|
|
}
|
|
}
|
|
|
|
const manager = new CanvasWorkerManager();
|
|
|
|
self.onmessage = async (e: MessageEvent) => {
|
|
const { type, data } = e.data;
|
|
switch (type) {
|
|
case 'init': {
|
|
const { width, height, dpr } = data;
|
|
manager.init(width, height, dpr);
|
|
break;
|
|
}
|
|
case 'draw': {
|
|
await font.load();
|
|
const { section } = data;
|
|
manager.draw(section);
|
|
break;
|
|
}
|
|
}
|
|
};
|