Skip to content

Commit

Permalink
Fixed a bug of break line.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Aug 10, 2024
1 parent 0505fd1 commit de687d8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Just import the dependencies and this tool.
<script src="https://unpkg.com/[email protected]/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.
Expand All @@ -64,6 +68,8 @@ function setTimeout(func, sleep){
var window = globalThis;
// Load pdf-lib
eval(UrlFetchApp.fetch("https://unpkg.com/[email protected]/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/[email protected]/dist/forge.min.js").getContentText());
// Load ZgaPdfSigner
Expand Down
78 changes: 76 additions & 2 deletions lib/zgapdfsigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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//
Expand Down
34 changes: 27 additions & 7 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/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/[email protected]/dist/forge.min.js" type="text/javascript"></script>
<script src="dist/zgapdfsigner.min.js" type="text/javascript"></script>
<script type="text/javascript">
Expand Down Expand Up @@ -63,6 +64,10 @@
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");
Expand All @@ -81,17 +86,30 @@
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,
};
}
}
Expand Down Expand Up @@ -145,7 +163,7 @@
});
}
function clearFiles(){
["fff","kkk","img","pubcert"].forEach((a_id) => {
["fff","kkk","img","font","pubcert"].forEach((a_id) => {
document.getElementById(a_id).value = "";
});
}
Expand Down Expand Up @@ -187,6 +205,8 @@
<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>
Expand Down
4 changes: 2 additions & 2 deletions test4node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit de687d8

Please sign in to comment.