Skip to content

Commit

Permalink
Fixed some formattings not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Nov 24, 2023
1 parent 5f66d96 commit 9ff064f
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions js/minecraft-tip.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ class TextRenderer {
TextFormatting.formattingCodes[textRenderingContext.char].formatFunction(textRenderingContext.formatting);
}
break;
// CanvasRenderingContext2D.wordSpacing doesn't seem to work on Chromium-based browsers, so we have a special condition for spaces
case ' ':
ctx.save();
textRenderingContext.formatting.applyFormatting(textRenderingContext, this);
textRenderingContext.left += parseFloat(ctx.wordSpacing.substring(0, ctx.wordSpacing.length - 2));
ctx.restore();
break;
default:
textRenderingContext.left += this.drawChar(textRenderingContext);
break;
Expand Down Expand Up @@ -223,10 +216,16 @@ class TextRenderer {

textRenderingContext.formatting.applyFormatting(textRenderingContext, this);

ctx.fillText(textRenderingContext.char, textRenderingContext.x + textRenderingContext.left, textRenderingContext.y + (textRenderingContext.line * this.getLineHeight()) + (textRenderingContext.line > 0 && settings.firstLineIsHigher ? 4 : 0));
const width = textRenderingContext.left - originalLeft + this.getWidth(textRenderingContext.char + ".") - this.getWidth(".");
ctx.restore();
return width;
// CanvasRenderingContext2D.wordSpacing doesn't seem to work on Chromium-based browsers, so we have a special condition for spaces
if (textRenderingContext.char === ' ') {
ctx.restore();
return parseFloat(ctx.wordSpacing.substring(0, ctx.wordSpacing.length - 2));
} else {
ctx.fillText(textRenderingContext.char, textRenderingContext.x + textRenderingContext.left, textRenderingContext.y + (textRenderingContext.line * this.getLineHeight()) + (textRenderingContext.line > 0 && settings.firstLineIsHigher ? 4 : 0));
const width = textRenderingContext.left - originalLeft + this.getWidth(textRenderingContext.char + ".") - this.getWidth(".");
ctx.restore();
return width;
}
}

getRandomTextFrom(originalText) {
Expand Down Expand Up @@ -314,21 +313,31 @@ class TextFormatting {
textRenderingContext.char = textRenderer.getRandomTextFrom(textRenderingContext.char);
}
}, false);
this.addFormattingOption(TextFormatting.FormattingOptions.BOLD, (textRenderingContext, textRenderer, value) => {
if (value) {
textRenderingContext.canvas.getContext('2d').wordSpacing = (settings.wordSpacing + 2) + "px";
const textRenderingContextCopy = textRenderingContext.copy();
textRenderingContextCopy.formatting.setFormattingOption(TextFormatting.FormattingOptions.BOLD, false);
textRenderingContextCopy.formatting.setFormattingOption(TextFormatting.FormattingOptions.OBFUSCATED, false);
textRenderer.drawChar(textRenderingContextCopy);
textRenderingContext.left += 2;
}
}, false);
this.addFormattingOption(TextFormatting.FormattingOptions.UNDERLINE, (textRenderingContext, textRenderer, value) => {
const ctx = textRenderingContext.canvas.getContext('2d');
if (value) {
let originalFillStyle = ctx.fillStyle;
ctx.fillStyle = "#" + ('000000' + this.color.toString(16).toUpperCase()).slice(-6);
ctx.fillRect(x - 2, y + 2, this.getWidth(char) + 2, 2);
ctx.fillStyle = "#" + ('000000' + this.getFormattingOption(TextFormatting.FormattingOptions.COLOR).toString(16).toUpperCase()).slice(-6);
ctx.fillRect((textRenderingContext.x + textRenderingContext.left) - 2, (textRenderingContext.y + (textRenderingContext.line * textRenderer.getLineHeight()) + (textRenderingContext.line > 0 && settings.firstLineIsHigher ? 4 : 0)) + 2, textRenderer.getWidth(textRenderingContext.char) + 2, 2);
ctx.fillStyle = originalFillStyle;
}
}, false);
this.addFormattingOption(TextFormatting.FormattingOptions.STRIKETHROUGH, (textRenderingContext, textRenderer, value) => {
const ctx = textRenderingContext.canvas.getContext('2d');
if (value) {
let originalFillStyle = ctx.fillStyle;
ctx.fillStyle = "#" + ('000000' + this.color.toString(16).toUpperCase()).slice(-6);
ctx.fillRect(x - 2, y - 7, this.getWidth(char) + 2, 2);
ctx.fillStyle = "#" + ('000000' + this.getFormattingOption(TextFormatting.FormattingOptions.COLOR).toString(16).toUpperCase()).slice(-6);
ctx.fillRect((textRenderingContext.x + textRenderingContext.left) - 2, (textRenderingContext.y + (textRenderingContext.line * textRenderer.getLineHeight()) + (textRenderingContext.line > 0 && settings.firstLineIsHigher ? 4 : 0)) - 7, textRenderer.getWidth(textRenderingContext.char) + 2, 2);
ctx.fillStyle = originalFillStyle;
}
}, false);
Expand All @@ -338,16 +347,6 @@ class TextFormatting {
ctx.font = "italic " + ctx.font;
}
}, false);
this.addFormattingOption(TextFormatting.FormattingOptions.BOLD, (textRenderingContext, textRenderer, value) => {
if (value) {
textRenderingContext.canvas.getContext('2d').wordSpacing = (settings.wordSpacing + 2) + "px";
const textRenderingContextCopy = textRenderingContext.copy();
textRenderingContextCopy.formatting.setFormattingOption(TextFormatting.FormattingOptions.BOLD, false);
textRenderingContextCopy.formatting.setFormattingOption(TextFormatting.FormattingOptions.OBFUSCATED, false);
textRenderer.drawChar(textRenderingContextCopy);
textRenderingContext.left += 2;
}
}, false);
this.addFormattingOption(TextFormatting.FormattingOptions.RESET, (textRenderingContext, textRenderer, value) => {
if (value) {
this.reset();
Expand Down

0 comments on commit 9ff064f

Please sign in to comment.