tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 09:46:30 -03:00
1 parent defd9b33f5
commit a2bff3baeb
1 file changed
+10 -7
+10 -7
View File
@@ -392,15 +392,19 @@ export class PdfService {
} }
} }
// NOVA ABORDAGEM: calcula o recuo apenas na PRIMEIRA linha do parágrafo, nunca nas demais // Reformulação: recuo de parágrafo como citação (blockquote) e recuo consistente só na primeira linha
let lines: { tokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] }[] = []; 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 currentTokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = [];
let currentWidth = 0; let currentWidth = 0;
const recuo = indent ? fontSize * 2 : 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()) { for (const [i, word] of words.entries()) {
const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize);
// Só aplica recuo na primeira linha do parágrafo // Se for a primeira linha, aplica recuo de bloco OU recuo de primeira linha
if (lines.length === 0 && currentTokens.length === 0 && recuo > 0) { if (lines.length === 0 && currentTokens.length === 0) {
let recuo = blockRecuo || firstLineRecuo;
if (wordWidth + recuo + currentWidth > maxWidth) { if (wordWidth + recuo + currentWidth > maxWidth) {
lines.push({ tokens: currentTokens }); lines.push({ tokens: currentTokens });
currentTokens = [word]; currentTokens = [word];
@@ -432,8 +436,8 @@ export class PdfService {
let drawX = x; let drawX = x;
let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);
// Aplica recuo de parágrafo SOMENTE na primeira linha // Aplica recuo de bloco (blockquote) em todas as linhas se indent, ou só na primeira linha se não for indent
let lineRecuo = (indent && i === 0) ? recuo : 0; let lineRecuo = indent ? blockRecuo : (i === 0 ? firstLineRecuo : 0);
drawX += lineRecuo; drawX += lineRecuo;
// Alinhamento // Alinhamento
@@ -443,7 +447,6 @@ export class PdfService {
drawX = x + (maxWidth - totalWidth) + lineRecuo; drawX = x + (maxWidth - totalWidth) + lineRecuo;
} else if (align === 'justify' && i !== lines.length - 1 && line.tokens.length > 1) { } else if (align === 'justify' && i !== lines.length - 1 && line.tokens.length > 1) {
// Justify: distribui espaço entre palavras (menos na última linha) // Justify: distribui espaço entre palavras (menos na última linha)
const wordsOnly = line.tokens.filter(t => t.text.trim() !== '');
const spaces = line.tokens.filter(t => /^\s+$/.test(t.text)); const spaces = line.tokens.filter(t => /^\s+$/.test(t.text));
const numSpaces = spaces.length; const numSpaces = spaces.length;
const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);