Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strikethrough implementation #226

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/htmlToTextObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
22 changes: 20 additions & 2 deletions lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -720,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);
Expand Down Expand Up @@ -968,6 +985,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,
Expand Down