Fixed a bug of break line.

sigtext
zboris12 2024-08-10 20:12:19 +09:00
parent 0505fd1e0e
commit de687d8bf7
4 changed files with 111 additions and 11 deletions

View File

@ -51,6 +51,10 @@ Just import the dependencies and this tool.
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/zgapdfsigner/dist/zgapdfsigner.min.js" type="text/javascript"></script>
```
When using feature of drawing text, importing the fontkit library is necessary.
```html
<script src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js" type="text/javascript"></script>
```
### [Google Apps Script](https://developers.google.com/apps-script)
Load the dependencies and this tool.
@ -64,6 +68,8 @@ function setTimeout(func, sleep){
var window = globalThis;
// Load pdf-lib
eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js").getContentText());
// It is necessary for drawing text feature.
eval(UrlFetchApp.fetch("https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js").getContentText());
// Load node-forge
eval(UrlFetchApp.fetch("https://unpkg.com/node-forge@1.3.1/dist/forge.min.js").getContentText());
// Load ZgaPdfSigner

View File

@ -1275,7 +1275,7 @@ z.SignatureCreator = class{
/** @type {number} */
var j = sarr2[0] ? parseInt(sarr2[0], 10) : 0;
/** @type {number} */
var ed = sarr2[sarr2.length - 1] ? parseInt(sarr2[sarr2.length - 1], 10) : (pgcnt ? pgcnt -1 : j);
var ed = sarr2[sarr2.length - 1] ? parseInt(sarr2[sarr2.length - 1], 10) : (pgcnt ? pgcnt - 1 : j);
while(j <= ed){
this.pgidxs.push(j);
j++;
@ -1721,7 +1721,18 @@ z.SignatureCreator = class{
}else{
/** @type {number} */
var width = computeWidthOfText(word);
if(currWidth + width > maxWidth){
if(width > maxWidth){
if(idx > 0){
lines.push(currLine);
currLine = "";
currWidth = 0;
}
/** @type {SplitLongWordResult} */
var slwr = this.splitLongWord(word, width, maxWidth, computeWidthOfText);
lines = lines.concat(slwr.words);
word = slwr.lastWord;
width = slwr.lastWidth;
}else if(currWidth + width > maxWidth){
lines.push(currLine);
currLine = "";
currWidth = 0;
@ -1736,6 +1747,58 @@ z.SignatureCreator = class{
return lines;
}
/**
* @private
* @param {string} word
* @param {number} wordWidth
* @param {number} maxWidth
* @param {function(string):number} computeWidthOfText
* @return {SplitLongWordResult}
*/
splitLongWord(word, wordWidth, maxWidth, computeWidthOfText){
/** @type {Array<string>} */
var splited = [];
/** @type {number} */
var wordLen = word.length;
while(wordWidth > maxWidth){
/** @type {number} */
var maxIdx = Math.floor(wordLen * maxWidth / wordWidth) - 1;
/** @type {number} */
var w = computeWidthOfText(word.substring(0, maxIdx + 1));
if(w > maxWidth){
while(w > maxWidth){
maxIdx--;
w -= computeWidthOfText(word.charAt(maxIdx));
}
maxIdx++;
}else{
while(w < maxWidth){
maxIdx++;
if(maxIdx < wordLen){
/** @type {number} */
var w2 = w + computeWidthOfText(word.charAt(maxIdx));
if(w2 > maxWidth){
break;
}else{
w = w2;
}
}else{
break;
}
}
}
splited.push(word.substring(0, maxIdx));
word = word.substring(maxIdx);
wordLen -= maxIdx;
wordWidth -= w;
}
return {
words: splited,
lastWord: word,
lastWidth: wordWidth,
};
}
/**
* @private
* @param {DrawLinesOfTextOptions} opts
@ -1771,10 +1834,21 @@ z.SignatureCreator = class{
newopts["y"] = w - x;
break;
}
return newopts;
}
};
/**
* @typedef
* {{
* words: Array<string>,
* lastWord: string,
* lastWidth: number,
* }}
*/
var SplitLongWordResult;
}
//Only for nodejs Start//

View File

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Test for ZgaPdfSigner </title>
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
<script src="dist/zgapdfsigner.min.js" type="text/javascript"></script>
<script type="text/javascript">
@ -63,6 +64,10 @@ async function testMe(){
if(img){
imgType = getFilExt("img");
}
/** @type {string} */
var txt = document.getElementById("txt").value;
/** @type {ArrayBuffer} */
var font = await readFile("font");
/** @type {ArrayBuffer} */
var pubcert = await readFile("pubcert");
@ -81,17 +86,30 @@ async function testMe(){
contact: document.getElementById("tContact").value,
debug: true,
};
if(img){
if(img || txt){
sopt.drawinf = {
area: {
x: 25, // left
y: 150, // top
w: 60,
h: 60,
w: txt ? undefined : 60,
h: txt ? undefined : 100,
},
// pageidx: 2,
imgData: img,
imgType: imgType,
pageidx: "-",
imgInfo: img ? {
imgData: img,
imgType: imgType,
} : undefined,
textInfo: txt ? {
text: txt,
fontData: font,
color: "f00",
size: 16,
align: 2,
wMax: 80,
yOffset: 10,
xOffset: 20,
noBreaks: "[あいうえおA-Za-z0-9]",
} : undefined,
};
}
}
@ -145,7 +163,7 @@ function test(){
});
}
function clearFiles(){
["fff","kkk","img","pubcert"].forEach((a_id) => {
["fff","kkk","img","font","pubcert"].forEach((a_id) => {
document.getElementById(a_id).value = "";
});
}
@ -187,6 +205,8 @@ span.header {
<label>certificate </label><input type="file" id="kkk" /><br />
<label>passphrase </label><input type="password" id="pwd" /><br />
<label>signature image </label><input type="file" id="img" /><br />
<label>signature text </label><input type="text" id="txt" /><br />
<label>text font </label><input type="file" id="font" /><br />
<label>permission </label>
<select id="sperm" onchange="changeSperm()">
<option value="0">No DocMDP</option>

View File

@ -125,7 +125,7 @@ async function addtsa(pdfPath){
/** @type {Uint8Array} */
var u8dat = await ser.sign(pdf);
/** @type {string} */
var outPath = m_path.join(__dirname, workpath+"test_tsa.pdf");
var outPath = m_path.join(__dirname, workpath+"tsa_"+m_path.basename(pdfPath));
m_fs.writeFileSync(outPath, u8dat);
console.log("Output file: " + outPath);
return outPath;
@ -159,7 +159,7 @@ async function main1(angle){
}
if(pfxPath){
await sign_protect(pdfPath, pfxPath, ps, 1, imgPath, "あいうえおか\r\n\nThis is a test of text!\n", fontPath);
await sign_protect(pdfPath, pfxPath, ps, 1, imgPath, "あいうえおあいうえおか\r\n\nThis is a test of text!\n", fontPath);
pdfPath = await sign_protect(pdfPath, pfxPath, ps, 2, undefined, "ありがとうご\r\n\nThis is an another test of text!\n", fontPath);
await addtsa(pdfPath);
}else{