Skip to content

Commit 33af223

Browse files
committed
fix: consecutive modifiers not applying
1 parent 0833029 commit 33af223

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $ npm install minecraft-text-canvas
6262
The following example simply renders an example input text with some basic markup:
6363

6464
```javascript
65-
const render = require('minecraft-text-canvas');
65+
import render from 'minecraft-text-canvas';
6666
const result = render('&cExample &r&btext');
6767
```
6868

TODO.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Escaping markup
2-
Full markup support
1+
Support underline, strikethrough, italic
2+
Escaping markup

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const FONT_SIZE = 8;
22
export const FONT_OFFSET = 1;
3-
export const SUPPORTED_MODIFIERS = /&(?:0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|l|r)/g;
3+
export const SUPPORTED_MODIFIERS_GLOBAL = /&(?:0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|l|r)/g;
44
export const TEXT_COLORS = {
55
'0': 0x000,
66
'1': 0x00a,

src/lib/getFormattedWidth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CanvasRenderingContext2D } from 'canvas';
2-
import { FONT_OFFSET, SUPPORTED_MODIFIERS } from '../constants';
2+
import { FONT_OFFSET, SUPPORTED_MODIFIERS_GLOBAL } from '../constants';
33

44
const BOLD_REGEX = /&l(.*?)(?:&r|$)/g;
55

@@ -10,7 +10,7 @@ export default function getFormattedWidth(unformattedText: string, ctx: CanvasRe
1010
// Remove all modifiers and split by line break to calculate text width
1111
const wrappedText = unformattedText.split(/\r\n|\r|\n/g);
1212
wrappedText.forEach((line) => {
13-
let lineWidth = ctx.measureText(line.replaceAll(SUPPORTED_MODIFIERS, '')).width;
13+
let lineWidth = ctx.measureText(line.replaceAll(SUPPORTED_MODIFIERS_GLOBAL, '')).width;
1414

1515
// If bold carries over from previous line, prefix the line with '&l' so it fits the regex
1616
const boldSubstrings = isBold ? `&l${line}`.match(BOLD_REGEX) : line.match(BOLD_REGEX);
@@ -20,7 +20,7 @@ export default function getFormattedWidth(unformattedText: string, ctx: CanvasRe
2020

2121
boldSubstrings.forEach((subStr) => {
2222
// Remove all modifiers in the text
23-
const subStrRemovedModifiers = subStr.replaceAll(SUPPORTED_MODIFIERS, '');
23+
const subStrRemovedModifiers = subStr.replaceAll(SUPPORTED_MODIFIERS_GLOBAL, '');
2424
lineWidth += subStrRemovedModifiers.length * FONT_OFFSET;
2525
});
2626
}

src/lib/renderText.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CanvasRenderingContext2D } from 'canvas';
22
import {
33
FONT_SIZE,
44
FONT_OFFSET,
5-
SUPPORTED_MODIFIERS,
5+
SUPPORTED_MODIFIERS_GLOBAL,
66
TEXT_COLORS,
77
TEXT_SHADOW_COLORS,
88
} from '../constants';
@@ -19,7 +19,11 @@ export default function renderText(text: string, ctx: CanvasRenderingContext2D)
1919
const nextChar = text[i + 1];
2020

2121
// Check if this character and the next one are a supported modifier
22-
if (char === '&' && SUPPORTED_MODIFIERS.test(char + nextChar)) {
22+
// WARNING: JavaScript has inconsistent RegEx test behaviour with the global flag enabled -
23+
// On subsequent test() calls, it begins searching from the index of the previous match.
24+
// For this reason, we have to create a new, non-global RegEx to test our expression.
25+
// I hate JS.
26+
if (char === '&' && new RegExp(SUPPORTED_MODIFIERS_GLOBAL.source).test(char + nextChar)) {
2327
if (nextChar && nextChar in TEXT_COLORS) {
2428
currentColor = TEXT_COLORS[nextChar as HexDigit];
2529
currentShadowColor = TEXT_SHADOW_COLORS[nextChar as HexDigit];
@@ -33,7 +37,7 @@ export default function renderText(text: string, ctx: CanvasRenderingContext2D)
3337
continue;
3438
}
3539

36-
// Skip the next character as it is modifying
40+
// Skip the next character as it is part of a modifying sequence
3741
i += 1;
3842
} else if (char === '\n') {
3943
cursorX = 0;

0 commit comments

Comments
 (0)