import { prepareWithSegments, layoutNextLine } from '@chenglou/pretext';
function layoutWithInset(
text: string,
font: string,
fullWidth: number,
insetTopPx: number,
insetBottomPx: number,
insetNarrowByPx: number,
lineHeight: number,
) {
const prepared = prepareWithSegments(text, font);
let cursor = { segmentIndex: 0, graphemeIndex: 0 };
let y = 0;
const lines: Array<{ text: string; x: number; y: number }> = [];
for (;;) {
const inInset = y >= insetTopPx && y < insetBottomPx;
const maxW = inInset ? fullWidth - insetNarrowByPx : fullWidth;
const xOffset = inInset ? insetNarrowByPx / 2 : 0;
const line = layoutNextLine(prepared, cursor, maxW);
if (!line) break;
lines.push({ text: line.text, x: xOffset, y });
cursor = line.end;
y += lineHeight;
}
return lines;
}
const lines = layoutWithInset(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(6),
'16px Inter',
400,
40,
200,
80,
22,
);
console.log(lines.length, 'lines');