Repository navigation
Draw full-width characters in the two cells they take #79
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4bad8aa
Draw full-width characters in the two cells they take
sharamanie 1463e02
Address review: clear vacated cells, keep colours on their characters
sharamanie 0aad723
Read each character's colour from its place in its row
bas3line bc00859
regenerate the skill's piece list for gta vi
bas3line File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { test, type TestContext } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import type { Env, Piece } from "./types.ts"; | ||
|
|
||
| // Colours "#000000" to "#999999", so the colour a glyph is drawn in says which index it was given. | ||
| const palette = Array.from({ length: 10 }, (_, i) => "#" + String(i).repeat(6)); | ||
|
|
||
| // A piece of square cells showing `text(t)`, with `colors` written into env.color. | ||
| const piece = (cols: number, rows: number, text: (t: number) => string, colors: number[], fps = 0): Piece => ({ | ||
| meta: { name: "test", category: "ui", note: "test", cols, rows, fps, palette, ground: "#000000", cell: 1 }, | ||
| default: () => (t: number, env?: Env) => { | ||
| env?.color?.set(colors); | ||
| return text(t); | ||
| }, | ||
| }); | ||
|
|
||
| // mount() on a canvas 100px a cell, with the browser stubbed. The atlas records the glyph rasterised into each slot and | ||
| // its colour; the canvas records each glyph copied into it, as "glyph colour at column,row+cells", and each cell cleared. | ||
| async function onCanvas(t: TestContext, p: Piece) { | ||
| const g = globalThis as Record<string, unknown>; | ||
| const names = ["HTMLCanvasElement", "ResizeObserver", "IntersectionObserver", "document", "devicePixelRatio", "getComputedStyle", "matchMedia", "requestAnimationFrame", "cancelAnimationFrame"]; | ||
| const saved = Object.fromEntries(names.map((k) => [k, g[k]])); | ||
| const slots = new Map<string, string>(); | ||
| const drawn: string[] = []; | ||
| const cleared: string[] = []; | ||
| const frames: ((now: number) => void)[] = []; | ||
| let seen: ((e: { isIntersecting: boolean }[]) => void) | undefined; | ||
| let slot = ""; | ||
| class Canvas { | ||
| style = {}; | ||
| clientWidth = 100 * p.meta.cols; | ||
| width = 0; | ||
| height = 0; | ||
| atlas: boolean; | ||
| constructor(atlas = false) { | ||
| this.atlas = atlas; | ||
| } | ||
| getContext() { | ||
| if (this.atlas) | ||
| return { | ||
| fillStyle: "", | ||
| rect: (x: number, y: number) => (slot = `${x + 1},${y + 1}`), | ||
| fillText(this: { fillStyle: string }, ch: string) { | ||
| slots.set(slot, `${ch} ${this.fillStyle}`); | ||
| }, | ||
| save() {}, | ||
| restore() {}, | ||
| beginPath() {}, | ||
| clip() {}, | ||
| clearRect() {}, | ||
| }; | ||
| return { | ||
| fillStyle: "", | ||
| fillRect: (x: number, y: number, w: number) => cleared.push(`${x / 100},${y / 100}+${w / 100}`), | ||
| clearRect() {}, | ||
| drawImage: (_: unknown, sx: number, sy: number, _sw: number, _sh: number, dx: number, dy: number, dw: number) => | ||
| drawn.push(`${slots.get(`${sx},${sy}`)} at ${dx / 100},${dy / 100}+${dw / 100}`), | ||
| }; | ||
| } | ||
| } | ||
| Object.assign(g, { | ||
| HTMLCanvasElement: Canvas, | ||
| ResizeObserver: class { | ||
| observe() {} | ||
| disconnect() {} | ||
| }, | ||
| IntersectionObserver: class { | ||
| constructor(cb: typeof seen) { | ||
| seen = cb; | ||
| } | ||
| observe() {} | ||
| disconnect() {} | ||
| }, | ||
| document: { hidden: false, createElement: () => new Canvas(true), addEventListener() {}, removeEventListener() {} }, | ||
| devicePixelRatio: 1, | ||
| getComputedStyle: () => ({ color: "rgb(0, 0, 0)" }), | ||
| matchMedia: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }), | ||
| requestAnimationFrame: (cb: (now: number) => void) => frames.push(cb), | ||
| cancelAnimationFrame: () => {}, | ||
| }); | ||
| const { mount } = await import("./mount.ts"); | ||
| const stop = mount(new Canvas() as unknown as HTMLElement, p); | ||
| t.after(() => { | ||
| stop(); | ||
| Object.assign(g, saved); | ||
| }); | ||
| return { | ||
| drawn, | ||
| cleared, | ||
| /** Plays the next frame, a tenth of a second on. */ | ||
| tick() { | ||
| seen?.([{ isIntersecting: true }]); | ||
| frames.shift()?.(performance.now() + 100); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("a canvas draws each character in its own cell's colour, on every row", async (t) => { | ||
| const { drawn } = await onCanvas(t, piece(3, 3, () => "abc\ndef\nghi", [0, 1, 2, 3, 4, 5, 6, 7, 8])); | ||
| assert.deepEqual( | ||
| drawn, | ||
| [..."abcdefghi"].map((ch, i) => `${ch} ${palette[i]} at ${i % 3},${Math.floor(i / 3)}+1`), | ||
| ); | ||
| }); | ||
|
|
||
| test("a full-width character takes two cells, and the characters after it keep their own colours", async (t) => { | ||
| // meta.cols counts characters: 新 takes two cells, so the row's last character, a space, does not fit and is dropped. | ||
| const { drawn } = await onCanvas(t, piece(4, 2, () => "新AB \nabcd", [1, 2, 3, 4, 5, 6, 7, 8])); | ||
| assert.deepEqual(drawn, [ | ||
| `新 ${palette[1]} at 0,0+2`, | ||
| `A ${palette[2]} at 2,0+1`, | ||
| `B ${palette[3]} at 3,0+1`, | ||
| `a ${palette[5]} at 0,1+1`, | ||
| `b ${palette[6]} at 1,1+1`, | ||
| `c ${palette[7]} at 2,1+1`, | ||
| `d ${palette[8]} at 3,1+1`, | ||
| ]); | ||
| }); | ||
|
|
||
| test("a cell a character moves out of is cleared when a wide character before it narrows", async (t) => { | ||
| const c = await onCanvas(t, piece(3, 1, (t) => (t ? "aA " : "新A "), [1, 1, 1], 30)); | ||
| assert.deepEqual(c.drawn, [`新 ${palette[1]} at 0,0+2`, `A ${palette[1]} at 2,0+1`]); | ||
| c.drawn.length = c.cleared.length = 0; | ||
| c.tick(); | ||
| assert.deepEqual(c.drawn, [`a ${palette[1]} at 0,0+1`, `A ${palette[1]} at 1,0+1`]); | ||
| assert.deepEqual(c.cleared, ["0,0+1", "1,0+1", "2,0+1"]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { cellWidth } from "./width.ts"; | ||
|
|
||
| test("ascii and box drawing take one cell", () => { | ||
| for (const ch of ["a", "Z", "0", " ", "[", "+", "\u2500", "\u256d", "\u2591"]) { | ||
| assert.equal(cellWidth(ch.charCodeAt(0)), 1, JSON.stringify(ch)); | ||
| } | ||
| }); | ||
|
|
||
| test("full-width CJK takes two cells", () => { | ||
| for (const ch of ["新", "会", "话", "中", "A", "ア", "한"]) { | ||
| assert.equal(cellWidth(ch.charCodeAt(0)), 2, JSON.stringify(ch)); | ||
| } | ||
| }); | ||
|
|
||
| test("the wide ranges' own edges are counted", () => { | ||
| assert.equal(cellWidth(0x1100), 2); | ||
| assert.equal(cellWidth(0x115f), 2); | ||
| assert.equal(cellWidth(0x2e80), 2); | ||
| assert.equal(cellWidth(0xac00), 2); | ||
| assert.equal(cellWidth(0xf900), 2); | ||
| assert.equal(cellWidth(0xff01), 2); | ||
| assert.equal(cellWidth(0xff60), 2); | ||
| assert.equal(cellWidth(0x10ff), 1); | ||
| assert.equal(cellWidth(0xff61), 1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * How many cells a character takes on screen. A piece's frame is counted in | ||
| * cells, not in characters: a renderer that advanced one cell per character | ||
| * would draw a full-width character (CJK) into half a cell and crush every | ||
| * glyph after it. | ||
| * | ||
| * These are East Asian Width's Wide and Fullwidth classes, Basic Multilingual | ||
| * Plane only. Characters outside it are two UTF-16 units, which the cell model | ||
| * does not carry: a frame's columns are counted in characters. | ||
| */ | ||
|
|
||
| /** Cells `code` occupies: 2 when it is drawn full-width, 1 otherwise. */ | ||
| export function cellWidth(code: number): 1 | 2 { | ||
| return code >= 0x1100 && | ||
| (code <= 0x115f || // Hangul Jamo initials | ||
| code === 0x2329 || | ||
| code === 0x232a || | ||
| (code >= 0x2e80 && code <= 0xa4cf && code !== 0x303f) || // CJK radicals through Yi | ||
| (code >= 0xac00 && code <= 0xd7a3) || // Hangul syllables | ||
| (code >= 0xf900 && code <= 0xfaff) || // CJK compatibility ideographs | ||
| (code >= 0xfe10 && code <= 0xfe19) || | ||
| (code >= 0xfe30 && code <= 0xfe6f) || | ||
| (code >= 0xff00 && code <= 0xff60) || // fullwidth forms | ||
| (code >= 0xffe0 && code <= 0xffe6)) | ||
| ? 2 | ||
| : 1; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.