Skip to content

Commit

Permalink
Chrome compatibility sort of
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Nov 23, 2023
1 parent 3f8b4e0 commit f40c1e2
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions js/minecraft-tip.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,34 @@ class TextRenderer {
}

drawText(string, x, y, shadow, formatting = new TextFormatting()) {
const ctx = this.canvas.getContext('2d');

let cursor = 0;
let left = 0;
let line = 0;

while (cursor < string.length) {
let char = string[cursor];
if (char == '\n') {
line++;
left = 0;
formatting.reset();
} else if (char == '§') {
cursor++;
char = string[cursor];
if (TextFormatting.formattingCodes[char] && (formatting.isFormatting(TextFormatting.formattingCodes[char].type) || !formatting.isFormatted())) {
TextFormatting.formattingCodes[char].formatFunction(formatting);
}
} else {
left += this.drawChar(char, x + left, y + (line * this.getLineHeight()), shadow, formatting);
switch (char) {
case '\n':
line++;
left = 0;
formatting.reset();
break;
case '§':
cursor++;
char = string[cursor];
if (TextFormatting.formattingCodes[char] && (formatting.isFormatting(TextFormatting.formattingCodes[char].type) || !formatting.isFormatted())) {
TextFormatting.formattingCodes[char].formatFunction(formatting);
}
break;
// CanvasRenderingContext2D.wordSpacing doesn't seem to work on Chromium-based browsers, so we have a special condition for spaces
case ' ':
left += parseFloat(ctx.wordSpacing.substring(0, ctx.wordSpacing.length - 2));
break;
default:
left += this.drawChar(char, x + left, y + (line * this.getLineHeight()), shadow, formatting);
break;
}
cursor++;
}
Expand Down Expand Up @@ -272,7 +282,6 @@ class TextFormatting {
this.addFormattingOption(TextFormatting.FormattingOptions.BOLD, (text, ctx, textRenderer, value) => {
if (value && !ctx.font.includes("bold")) {
ctx.font = "bold " + ctx.font;
ctx.wordSpacing = "4px";
}
return text;
}, false);
Expand Down

0 comments on commit f40c1e2

Please sign in to comment.