Skip to content

Commit

Permalink
fix(driver): text color and measure cursor index
Browse files Browse the repository at this point in the history
fixes #67
  • Loading branch information
atty303 committed May 26, 2024
1 parent 332cf04 commit 93bfd1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions packages/driver/src/js/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,6 @@ export class Renderer {

const subtext = text.substring(0, m.index);
text = text.substring(m.index + m[0].length);
if (m[1]) {
this.currentColor = colorEscape[Number.parseInt(m[1])];
} else {
const r = Number.parseInt(m[2].substring(0, 2), 16);
const g = Number.parseInt(m[2].substring(2, 4), 16);
const b = Number.parseInt(m[2].substring(4, 6), 16);
this.currentColor = [r / 255, g / 255, b / 255, 1];
}

if (subtext.length > 0) {
for (const render of this.textRasterizer.get(height, font, subtext)) {
Expand All @@ -244,6 +236,15 @@ export class Renderer {
});
}
}

if (m[1]) {
this.currentColor = colorEscape[Number.parseInt(m[1])];
} else {
const r = Number.parseInt(m[2].substring(0, 2), 16);
const g = Number.parseInt(m[2].substring(2, 4), 16);
const b = Number.parseInt(m[2].substring(4, 6), 16);
this.currentColor = [r / 255, g / 255, b / 255, 1];
}
}
if (text.length > 0) {
for (const render of this.textRasterizer.get(height, font, text)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/js/renderer/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class TextMetrics {

measureCursorIndex(size: number, fontNum: number, text: string, cursorX: number, cursorY: number) {
this.context.font = font(size, fontNum);
const lines = text.replaceAll(reColorGlobal, "").split("\n");
const lines = text.split("\n");
const y = Math.floor(Math.max(0, Math.min(lines.length - 1, cursorY / size)));
const line = lines[y];
let i = 0;
for (; i <= line.length; i++) {
const w = this.context.measureText(line.substring(0, i)).width;
const w = this.context.measureText(line.substring(0, i).replaceAll(reColorGlobal, "")).width;
if (w >= cursorX) {
break;
}
Expand Down

0 comments on commit 93bfd1d

Please sign in to comment.