Skip to content

Draw full-width characters in the two cells they take - #79

Merged
bas3line merged 4 commits into
bas3line:mainfrom
sharamanie:fix/cjk-wide-cells
Nov 8, 2026
Merged

bas3line merged 4 commits into
bas3line:mainfrom
sharamanie:fix/cjk-wide-cells

Conversation

@sharamanie

Copy link
Copy Markdown
Contributor

What this fixes

On a canvas, mount() lays a frame out in cells but advanced one cell per character. A full-width character therefore drew into a single cell, and every character after it overlapped the previous one by half a cell — a label like 新会话 came out as one compressed smudge.

The New Session button in dsh-theme-ascetic renders the page's own label, so anyone running that skin on a Chinese (or Japanese/Korean) UI sees it.

What changed

  • src/width.ts — cellWidth(code): the East Asian Wide and Fullwidth ranges. BMP only; astral characters are two UTF-16 units and the cell model counts characters, so they stay out of scope.
  • src/mount.ts
    • the draw loop advances by cellWidth(c) instead of 1;
    • glyph atlas slots are two cells wide, so a wide glyph has room for the two cells it covers;
    • a wide glyph is rasterised at the font size whose 1em spans those two cells. A monospace face draws ASCII at 0.6em but CJK at 1em, so reusing the 0.6em formula made wide glyphs 3.3 cells tall and the slot clip squashed them;
    • a wide character starting in the last column is clipped to its row rather than overrunning into the next line's first glyph.

Narrow characters are unchanged: same one-cell slot geometry, same source rectangle, so existing pieces are pixel-identical. Verified by npm run check staying at 240/240.

The contract, and what I did not change

meta.cols still counts characters, and scripts/check.ts still asserts [...line].length === cols. So a piece that pads a label to a fixed grid must pad by cells, not by String.length — otherwise its row overflows and the last glyphs wrap. That is what the theme's own piece does, and it is out of scope here: telling piece authors to count cells is a contract change, and folding it into a rendering regression fix would hide it.

If you'd rather this PR also make cols cell-based, say so and I'll extend it.

Verification

  • npm run typecheck — clean
  • npm test — 525/525 (522 existing + 3 new in src/width.test.ts)
  • npm run check — 240/240
  • npm run kit:docs / npm run kit:examples — all ok
  • npm run gen — no drift in src/pieces/index.ts, src/library.ts, skills/

npm run check occasionally reports lake-pichola: slow: worst frame ... (budget 40ms) on my machine. Pre-existing flakiness, not this change: an unpatched checkout failed 3 of 6 runs, this branch failed 1 of 5.

@vercel

vercel Bot commented Nov 2, 2026

Copy link
Copy Markdown

@sharamanie is attempting to deploy a commit to the Shubham Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@bas3line

bas3line commented Nov 2, 2026

Copy link
Copy Markdown
Owner

@greptile-apps review

@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Make sure you do the changes Sensei

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

The PR gives full-width BMP characters two canvas cells.

  • Wide characters take two canvas cells, and later glyphs stay in place.
  • The piece list shows GTA VI’s sunset artwork at 76×32.

The four supplied, unnumbered previous findings are addressed in the current code. No new actionable issues were found.

Reviews (4) · Last reviewed commit: "regenerate the skill's piece list for gt..." · Reviewed by Greptile

Comment thread src/mount.ts Resolved
Comment thread src/mount.ts Resolved
Comment thread src/mount.ts Resolved
@sharamanie

Copy link
Copy Markdown
Contributor Author

Both faults Greptile found are fixed in b707322, plus a third one I found while checking them.

Vacated cells were never cleared. The frame was compared to the previous one by string index. A character keeps its index while moving to another cell whenever a wide character earlier on the line changes width, so the cell it left kept its glyph. Measured on 新A -> aA at a 26px cell: ink in three cells where two are drawn. The comparison is now per cell, over a grid of what each cell of the canvas holds (-1 cleared, -2 the second cell of a wide glyph, else the character).

Colours were read by display column. A piece's colours are indexed by character position, so past a wide glyph every character took the colour of one further along the row. On a row of 新/A/B in red/green/blue, A rendered blue. Colours now travel with their character to the cell it is drawn in.

Narrow glyphs stay their full size. The cell-sized copy rectangle is restored; the padded-slot source it replaced was scaling every narrow glyph down by about 12% (20px to 17-18px at a 21px cell) and opening gaps in box drawing lines.

One overflow case is now handled explicitly: meta.cols counts characters, so a line carrying wide ones can run past the last column. What does not fit is dropped rather than wrapped onto the next row.

npm run check 240/240, npm test 525/525, npm run typecheck clean.

Comment thread src/mount.ts Resolved
TABmk pushed a commit to TABmk/dsh-theme-ascetic that referenced this pull request Nov 4, 2026
The framed button pads its label to the sidebar's width with
`String.length`, which counts a full-width character as one. A label
with CJK characters therefore came up short by one cell per character
and pushed the right border past the frame; the ellipsis cut long
labels at the wrong place for the same reason.

Measure in cells instead: two for an East Asian Wide or Fullwidth
character, one for the rest, and cut to a cell budget rather than to a
character count. English labels render exactly as before.

This is the label half of the CJK rendering problem. The glyphs
themselves were also being drawn a cell wide, which is ascii.rest's
mount(): see bas3line/ascii#79.
sharamanie and others added 4 commits November 8, 2026 10:36
A canvas frame is laid out in cells, and mount() advanced one cell per
character, so a full-width character (CJK, and the other East Asian
Wide and Fullwidth characters) was drawn into a single cell and the
characters after it each overlapped the previous one by half a cell.

Advance by the character's cell width instead, and give the glyph atlas
slots two cells across so a wide glyph is rasterised at the right size
rather than squeezed into one cell. Narrow characters keep the one-cell
slot they had, so their pixels are unchanged.

This is what a piece rendering a label from a page in Chinese hits:
dsh-theme-ascetic's framed New Session button drew 新会话 as one
compressed smudge.
Three faults in the incremental canvas redraw, all reported in review.

A frame was compared to the last one by string index. A character can keep its
index while moving to another cell, which happens whenever a wide character
before it on the line changes width; the cell it left was then never cleared and
its glyph stayed behind. The comparison is now by cell, over a grid of what each
cell of the canvas shows.

Colours were read by display column. A piece's colours are indexed by character
position, so past a wide glyph every colour was taken from further along the row.
Each colour now travels with its character to the cell it is drawn in.

The glyph was copied from the cell-sized rectangle only because an earlier
mistake had it copying the padded slot; restoring that also restores narrow
glyphs to their full width.

A line carrying wide characters can now run past the last column, since
`meta.cols` counts characters. What does not fit is dropped rather than wrapped
onto the next row.
env.color holds one colour a character, row by row, with no entry for the
newlines between rows. The draw loop read it at the character's index in the
frame string, which counts those newlines, so row 1 took the colour of the cell
after its own, row 2 two cells on, and the last row ran off the end of the
buffer into palette colour 0. Every coloured piece on a canvas was off, wide
characters or not.

Count each character's place in its row and read its colour at y * cols + j.

src/mount.test.ts drives mount() on a stubbed canvas: colours on every row,
a wide glyph's two cells and the colours after it, and the cell a character
leaves when a wide one before it narrows.

Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
@bas3line
bas3line merged commit b9e5af0 into bas3line:main Nov 8, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants