tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 09:51:56 -03:00
1 parent a2bff3baeb
commit 0121f67c65
1 file changed
+10 -30
+10 -30
View File
@@ -392,36 +392,20 @@ export class PdfService {
}
}
// Reformulação: recuo de parágrafo como citação (blockquote) e recuo consistente só na primeira linha
// Reformulação: sem recuo de parágrafo (nem blockquote, nem primeira linha)
let lines: { tokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] }[] = [];
let currentTokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = [];
let currentWidth = 0;
// Recuo de citação: se indent, recua todo o parágrafo (blockquote); se não, recua só a primeira linha
const blockRecuo = indent ? fontSize * 2 : 0;
const firstLineRecuo = !indent && fontSize * 2; // só para parágrafo normal com recuo na primeira linha
for (const [i, word] of words.entries()) {
const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize);
// Se for a primeira linha, aplica recuo de bloco OU recuo de primeira linha
if (lines.length === 0 && currentTokens.length === 0) {
let recuo = blockRecuo || firstLineRecuo;
if (wordWidth + recuo + currentWidth > maxWidth) {
lines.push({ tokens: currentTokens });
currentTokens = [word];
currentWidth = wordWidth;
} else {
currentTokens.push(word);
currentWidth += wordWidth;
}
if (currentWidth + wordWidth > maxWidth && currentTokens.length > 0) {
lines.push({ tokens: currentTokens });
currentTokens = [word];
currentWidth = wordWidth;
} else {
if (currentWidth + wordWidth > maxWidth && currentTokens.length > 0) {
lines.push({ tokens: currentTokens });
currentTokens = [word];
currentWidth = wordWidth;
} else {
currentTokens.push(word);
currentWidth += wordWidth;
}
currentTokens.push(word);
currentWidth += wordWidth;
}
}
if (currentTokens.length > 0) {
@@ -436,21 +420,17 @@ export class PdfService {
let drawX = x;
let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);
// Aplica recuo de bloco (blockquote) em todas as linhas se indent, ou só na primeira linha se não for indent
let lineRecuo = indent ? blockRecuo : (i === 0 ? firstLineRecuo : 0);
drawX += lineRecuo;
// Alinhamento
if (align === 'center') {
drawX = x + (maxWidth - totalWidth) / 2 + lineRecuo;
drawX = x + (maxWidth - totalWidth) / 2;
} else if (align === 'right') {
drawX = x + (maxWidth - totalWidth) + lineRecuo;
drawX = x + (maxWidth - totalWidth);
} else if (align === 'justify' && i !== lines.length - 1 && line.tokens.length > 1) {
// Justify: distribui espaço entre palavras (menos na última linha)
const spaces = line.tokens.filter(t => /^\s+$/.test(t.text));
const numSpaces = spaces.length;
const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);
const extraSpace = maxWidth - lineTextWidth - lineRecuo;
const extraSpace = maxWidth - lineTextWidth;
let spaceWidth = 0;
if (numSpaces > 0) {
spaceWidth = extraSpace / numSpaces;