我正在尝试对jsPDF库进行一些更改,以便能够调整文本对齐。
我遇到了一些困难,无法找到Tw的正确值(单词间距)。
在jspdf.js (L:1413)中,我添加了以下代码:
if (align) {
...
else if (align === 'justify') {
left = x;
}
else {
throw new Error('Unrecognized alignment option, use "center" or "right".');
}
prevX = x;
text = '(' + da[0];
let pdfPageWidth = this.internal.pageSize.width;
let wordSpacing;
if( align === 'justify' ) {
let fontSize = this.internal.getFontSize();
let nWords = da[0].trim().split(/\s+/).length;
let textWidth = this.getStringUnitWidth(da[0].replace(/\s+/g, '')) / this.internal.scaleFactor;
wordSpacing = (Math.max(0, (pdfPageWidth - textWidth) / Math.max(1, nWords - 1));
wordSpacing += ' Tw\n';
text = wordSpacing + text;
}
...
}
这个想法是通过执行(pageWidth - textWidth) / numberOfWords -1来提取空格宽度。我无法获得正确的单词间距。
BT
/F1 16 Tf
18.4 TL
0 g
28.35 756.85 Td
19.00357142857142 Tw
(And a little bit it adélkfjalké.) Tj
ET
有编码问题吗?
谢谢你的帮助。