From 7386932ec4da1dc1a8915a01a690a9ff0fa10441 Mon Sep 17 00:00:00 2001 From: Joseph Moon Date: Wed, 26 Jan 2022 10:41:38 -0800 Subject: [PATCH 1/5] Pass lineWidth down to addUnderline() --- lib/text.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/text.js b/lib/text.js index 799b647..0e53fb1 100644 --- a/lib/text.js +++ b/lib/text.js @@ -334,10 +334,10 @@ exports.text = function text(text = '', x, y, options = {}) { // console.log(textWidth, lineWidth, currentLineWidth) const underlineY = y - options.textHeight * 0.1; // TODO: fix the line with calculation - const width = currentLineWidth - ((isHTML || toWriteTextObject.text.endsWith(' ')) ? spaceWidth : 0); + // const width = currentLineWidth - ((isHTML || toWriteTextObject.text.endsWith(' ')) ? spaceWidth : 0); ctx .q() - .drawPath(x, underlineY, x + width, underlineY, options) + .drawPath(x, underlineY, x + options.lineWidth, underlineY, options) .Q(); } }; @@ -386,7 +386,7 @@ exports.text = function text(text = '', x, y, options = {}) { const writeText = (context, x, y, wto) => { const options = wto.writeOptions; - const { lineHeight, text, baseline } = wto; + const { lineHeight, text, baseline, lineWidth } = wto; let next_x = 0; if (text === '') { // nothing to write, so simply escape. @@ -446,7 +446,7 @@ exports.text = function text(text = '', x, y, options = {}) { .h().W().n(); } - emitTextObject(text, x, y + baseline, context, options); + emitTextObject(text, x, y + baseline, context, {...options, lineWidth}); } context.Q(); @@ -470,7 +470,7 @@ exports.text = function text(text = '', x, y, options = {}) { emitText(word.value, xx - nx, baseline, xObjectCtx, options); }); } else { - emitTextObject(text, x - nx, baseline, xObjectCtx, options); + emitTextObject(text, x - nx, baseline, xObjectCtx, {...options, lineWidth}); } xObjectCtx.Q(); From 1ec9939a68aa4fef215d5b247a2e6a0f03c1517c Mon Sep 17 00:00:00 2001 From: Joseph Moon Date: Wed, 26 Jan 2022 19:09:08 -0800 Subject: [PATCH 2/5] Pass in lineWidth to addUnderline() if options.underline is true --- lib/text.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/text.js b/lib/text.js index 0e53fb1..0d37c94 100644 --- a/lib/text.js +++ b/lib/text.js @@ -386,13 +386,17 @@ exports.text = function text(text = '', x, y, options = {}) { const writeText = (context, x, y, wto) => { const options = wto.writeOptions; - const { lineHeight, text, baseline, lineWidth } = wto; + const { lineHeight, text, baseline } = wto; let next_x = 0; if (text === '') { // nothing to write, so simply escape. return next_x; } + if (options.underline) { + options.lineWidth = wto.lineWidth; + } + // Produce a hilite under words? if (options.hilite) { const bgColor = options.hilite.color || '#ffff00'; @@ -446,7 +450,7 @@ exports.text = function text(text = '', x, y, options = {}) { .h().W().n(); } - emitTextObject(text, x, y + baseline, context, {...options, lineWidth}); + emitTextObject(text, x, y + baseline, context, options); } context.Q(); @@ -470,7 +474,7 @@ exports.text = function text(text = '', x, y, options = {}) { emitText(word.value, xx - nx, baseline, xObjectCtx, options); }); } else { - emitTextObject(text, x - nx, baseline, xObjectCtx, {...options, lineWidth}); + emitTextObject(text, x - nx, baseline, xObjectCtx, options); } xObjectCtx.Q(); From 1b0207bc1a10cecafb6d614c7e94cd5336704b65 Mon Sep 17 00:00:00 2001 From: Joseph Moon Date: Mon, 7 Feb 2022 19:00:56 -0800 Subject: [PATCH 3/5] Add strikethrough implementation --- lib/text.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/text.js b/lib/text.js index 799b647..dc1ad8d 100644 --- a/lib/text.js +++ b/lib/text.js @@ -334,14 +334,25 @@ exports.text = function text(text = '', x, y, options = {}) { // console.log(textWidth, lineWidth, currentLineWidth) const underlineY = y - options.textHeight * 0.1; // TODO: fix the line with calculation - const width = currentLineWidth - ((isHTML || toWriteTextObject.text.endsWith(' ')) ? spaceWidth : 0); + // const width = currentLineWidth - ((isHTML || toWriteTextObject.text.endsWith(' ')) ? spaceWidth : 0); ctx .q() - .drawPath(x, underlineY, x + width, underlineY, options) + .drawPath(x, underlineY, x + options.lineWidth, underlineY, options) .Q(); } }; + const addStrikeOut = (x, y, ctx, options) => { + // strikethrough implementation + if (options.strikeOut) { + const strikeOutY = y + options.textHeight * 0.2; + ctx + .q() + .drawPath(x, strikeOutY, x + options.lineWidth, strikeOutY, options) + .Q(); + } + } + const addTextTraits = (ctx, options) => { ctx.Tf(options.font, options.size); ctx.Tc(options.charSpace); @@ -364,6 +375,7 @@ exports.text = function text(text = '', x, y, options = {}) { ctx.ET(); addUnderline(x, y, ctx, options); + addStrikeOut(x, y, ctx, options); }; const justifyText = (left, x, wto, textBox, ctx, options, callback) => { @@ -393,6 +405,10 @@ exports.text = function text(text = '', x, y, options = {}) { return next_x; } + if (options.underline || options.strikeOut) { + options.lineWidth = wto.lineWidth; + } + // Produce a hilite under words? if (options.hilite) { const bgColor = options.hilite.color || '#ffff00'; @@ -968,6 +984,7 @@ function makeTextObjects(self, textObject = {}, pathOptions, textBox = {}) { color: textObject.styles.color, opacity: parseFloat(textObject.styles.opacity || pathOptions.opacity || 1), underline: textObject.underline || pathOptions.underline, + strikeOut: textObject.strikeOut || pathOptions.strikeOut, size: textObject.size, alignHorizontal: alignHorizontal, alignVertical: alignVertical, From 4b3d0cefe9e1c8e21a35c87226ee83e66c43ad2d Mon Sep 17 00:00:00 2001 From: Joseph Moon Date: Mon, 7 Feb 2022 19:27:52 -0800 Subject: [PATCH 4/5] Add node tag name del for strikethrough --- lib/htmlToTextObjects.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/htmlToTextObjects.js b/lib/htmlToTextObjects.js index d4c32fb..415cc70 100644 --- a/lib/htmlToTextObjects.js +++ b/lib/htmlToTextObjects.js @@ -85,6 +85,7 @@ function parseNode(node) { isBold: isBoldTag(node.tagName), isItalic: isItalicTag(node.tagName), underline: (node.tagName == 'u'), + strikeOut: (node.tagName == 'del'), attributes, styles, needsLineBreaker: needsLineBreaker(node.tagName), From 705bb6e5b8c7100e1c1dc148f3592b29bad5e6f0 Mon Sep 17 00:00:00 2001 From: Joseph Moon Date: Mon, 7 Feb 2022 19:37:13 -0800 Subject: [PATCH 5/5] Add strikeOut to writeValue method --- lib/text.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/text.js b/lib/text.js index dc1ad8d..21687df 100644 --- a/lib/text.js +++ b/lib/text.js @@ -736,6 +736,7 @@ exports._layoutText = function _layoutText(textObjects, textBox, pathOptions) { child.isBold = (textObject.isBold) ? textObject.isBold : child.isBold; child.isItalic = (textObject.isItalic) ? textObject.isItalic : child.isItalic; child.underline = (textObject.underline) ? textObject.underline : child.underline; + child.strikeOut = (textObject.strikeOut) ? textObject.strikeOut : child.strikeOut; child.lineID = textObject.lineID; writeValue(child);