From de687d8bf7e9691c86642ff926f6fc3fb8486224 Mon Sep 17 00:00:00 2001 From: zboris12 Date: Sat, 10 Aug 2024 20:12:19 +0900 Subject: [PATCH] Fixed a bug of break line. --- README.md | 6 ++++ lib/zgapdfsigner.js | 78 +++++++++++++++++++++++++++++++++++++++++++-- test.html | 34 ++++++++++++++++---- test4node.js | 4 +-- 4 files changed, 111 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 16ba4ed..fbb620d 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ Just import the dependencies and this tool. ``` +When using feature of drawing text, importing the fontkit library is necessary. +```html + +``` ### [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 diff --git a/lib/zgapdfsigner.js b/lib/zgapdfsigner.js index 001344b..5c8386e 100644 --- a/lib/zgapdfsigner.js +++ b/lib/zgapdfsigner.js @@ -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} */ + 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, + * lastWord: string, + * lastWidth: number, + * }} + */ +var SplitLongWordResult; + } //Only for nodejs Start// diff --git a/test.html b/test.html index e4fe26f..b3335e4 100644 --- a/test.html +++ b/test.html @@ -5,6 +5,7 @@ Test for ZgaPdfSigner +