Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Copy: a space from the text size and the inline-code padding, not the line height (Noto Sans lost its spaces)

razorback16a655510470ff
diff --git a/hooks/lib/copy.ts b/hooks/lib/copy.ts
index 18d54eb..1684a73 100644
--- a/hooks/lib/copy.ts
+++ b/hooks/lib/copy.ts
@@ -131,12 +131,14 @@ export type Mark = {
prefix?: string
cx?: number
g?: number
+ pad?: number
}
// A word, or a whole code line (`adv`: its character advance), in PNG
// pixels: its box, the block it is in, and its place in the document (`i`).
// `glued`: a character of a long word or of CJK text after the first (it
-// joins the one before with no space).
-export type Item = { i: number; text: string; x0: number; y0: number; x1: number; y1: number; block: number; adv?: number; glued?: boolean }
+// joins the one before with no space). A word's `em` is its text size and
+// `pad` the padding beside inline code (a word of it), in pixels.
+export type Item = { i: number; text: string; x0: number; y0: number; x1: number; y1: number; block: number; adv?: number; glued?: boolean; em?: number; pad?: number }
// A block: paragraph, heading, code, quote, cell (`cx` its column), item
// (`prefix`); `first` its first item.
export type TextBlock = { kind: string; prefix: string; cx?: number; first: number }
@@ -198,6 +200,7 @@ export function positionsOf(marks: readonly Mark[], ppi: number): Positions {
: {
i: count, text: (m.t ?? '').replace(INVISIBLE, ''), block, x0: round(m.x * k), x1: round((m.x + (m.w ?? 0)) * k),
y0: round(m.y * k), y1: round((m.y + (m.h ?? m.s ?? 11)) * k), ...(m.g ? { glued: true } : {}),
+ ...(m.s ? { em: round(m.s * k) } : {}), ...(m.pad ? { pad: round(m.pad * k) } : {}),
}
if (!item.text) return
count++
@@ -372,10 +375,12 @@ export function textOfRun(
// Joins picked words: a new line between blocks (a tab between the cells of
// a table row), a block's prefix when its first word is in, a new line
// between code lines; inside a block a space between words, none between
-// the pieces of one word (boxes on one line closer than a fifth of the line
-// height: a space is wider, the padding beside inline code narrower; none
-// before a glued character that follows the one before it), none after a
-// - or / that ends a line.
+// the pieces of one word (boxes on one line closer than 0.15 of the text
+// size, not counting the padding beside inline code: a space is 0.22-0.32
+// of it; none before a glued character that follows the one before it),
+// none after a - or / that ends a line. Not of the line height: that is the
+// font's (Noto Sans: 1.36 of the size) or the grid's rows. Items with no
+// text size: a fifth of the line height.
export function joinItems(picked: readonly { item: Item; text: string }[], blocks: readonly TextBlock[]): string {
let out = ''
let prev: { item: Item; text: string } | null = null
@@ -395,7 +400,8 @@ export function joinItems(picked: readonly { item: Item; text: string }[], block
else {
const height = p.item.y1 - p.item.y0
const sameLine = Math.abs((p.item.y0 + p.item.y1) / 2 - (prev.item.y0 + prev.item.y1) / 2) < height / 2
- if (sameLine) out += p.item.x0 - prev.item.x1 > height * 0.2 ? ' ' : ''
+ const pad = (p.item.pad ?? 0) === (prev.item.pad ?? 0) ? 0 : Math.max(p.item.pad ?? 0, prev.item.pad ?? 0)
+ if (sameLine) out += p.item.x0 - prev.item.x1 - pad > (p.item.em ? p.item.em * 0.15 : height * 0.2) ? ' ' : ''
else out += /[-/]$/.test(prev.text) ? '' : ' '
}
out += p.text
diff --git a/render/md.typ b/render/md.typ
index d7fb050..5d399c1 100644
--- a/render/md.typ
+++ b/render/md.typ
@@ -461,6 +461,8 @@
// A long word (30+ characters) or CJK text breaks anywhere: each of
// its characters (clusters) is a word, boxed, `g` on all but the
// first (they join with no space).
+// A word of inline code has `pad`, the theme's padding beside the
+// code, when it is not 0.
// c: a block code line: x its start, y its baseline, a and d the code
// font's ascender and descender, w its width, n its characters.
// b: a block starts: paragraph, heading, code block, quote, table cell
@@ -484,6 +486,9 @@
// here: a page is as tall as its text, so no paragraph breaks across pages.
#let in-code(costs) = costs.orphan == 101%
#let no-marks(costs) = costs.orphan == 102%
+// Inline code sets this so its words carry the theme's padding beside it
+// (`pad`: a copy takes it off the gap to the text next to it).
+#let in-inline-code(costs) = costs.orphan == 104%
#let cjk = regex("[\\p{Han}\\p{Hiragana}\\p{Katakana}\\p{Hangul}]")
#let probe-rules(body) = {
// A word is cut after a hyphen or a slash, where a line may break; a
@@ -491,6 +496,8 @@
show regex("\\S{30,}|[^\\s/-]*[/-]+|[^\\s/-]+"): it => context if in-code(text.costs) or no-marks(text.costs) { it } else {
let size = measure(it)
let t = it.text
+ let pad = E.inline-code.pad-x-px * px
+ let pad = if in-inline-code(text.costs) and pad > 0pt { (pad: pad / 1pt) } else { (:) }
if t.clusters().len() >= 30 or t.match(cjk) != none {
// A long word or CJK text breaks anywhere: a box for each character
// (a line may break between boxes, as at the long-word rule's zws).
@@ -498,12 +505,12 @@
// with all the marks after it.
for (i, c) in t.clusters().enumerate() {
let one = measure(c)
- box({ mark("w", t: c, w: one.width / 1pt, h: one.height / 1pt, ..if i > 0 { (g: 1) } else { (:) }); c })
+ box({ mark("w", t: c, w: one.width / 1pt, h: one.height / 1pt, ..pad, ..if i > 0 { (g: 1) } else { (:) }); c })
}
} else {
// Boxed, so the mark is where the word is drawn (a bare mark at the
// start of a wrapped line stays at the end of the line above).
- box({ mark("w", t: t, w: size.width / 1pt, h: size.height / 1pt); it })
+ box({ mark("w", t: t, w: size.width / 1pt, h: size.height / 1pt, ..pad); it })
}
}
// A code line: a zero-size box at its start (its y is the baseline). An
@@ -520,6 +527,7 @@
it
}
show math.equation: set text(costs: (orphan: 102%))
+ show raw.where(block: false): it => context if no-marks(text.costs) { it } else { set text(costs: (orphan: 104%)); it }
show par: it => { mark("b", kind: "par"); it }
show heading: it => { mark("b", kind: "heading"); it }
show quote.where(block: true): it => { mark("b", kind: "quote"); it }
diff --git a/tests/copy.test.ts b/tests/copy.test.ts
index 0a1a9c9..1bc7a44 100644
--- a/tests/copy.test.ts
+++ b/tests/copy.test.ts
@@ -5,6 +5,7 @@ import {
placeholderCell, isSafePage, positionsOf, screenOf, textOfRun, WATCH_PL, type Drawn, type TextBlock, type Item, type Mark, type OnScreen,
type Place,
} from '../hooks/register'
+import { joinItems } from '../hooks/lib/copy'
import { WATCH_RESTART_MS } from '../hooks/lib/frame'
import { rowsAfter, type Under } from '../hooks/lib/pictures'
import { LONG_MARKS, LONG_MD, LONG_PAGE, SMALL_MARKS, SMALL_MD, SMALL_PAGE, SMALL_PPI } from './copy-fixture'
@@ -321,6 +322,48 @@ test('invisible marks and list markers of other themes are dropped', () => {
expect(got.pages[0]!.map(i => i.text)).toEqual(['with', 'code', 'end', 'one'])
})
+// A real probe on Linux (Ubuntu 24.04, theme claude-code, row grid on, ppi
+// 275): the body font is Noto Sans, its space 0.26 of the size, its
+// ascender to descender 1.36; on the grid a line is 2 rows (22.254 pt). A
+// fifth of either is more than the space.
+const NOTO_LINE: Mark[] = [
+ { k: 'w', p: 1, x: 20.9, y: 1541.09, s: 11, t: '-', w: 3.542, h: 22.254 },
+ { k: 'b', p: 1, x: 30.492, y: 1541.09, s: 11, kind: 'item', prefix: '- ' },
+ ...([[30.492, 'Geometric', 57.354], [90.706, 'resizing', 43.01], [136.576, 'keeps', 30.316], [169.752, 'inserts', 34.892], [207.504, 'at', 10.142],
+ [248.123, 'amortized,', 55.55], [306.533, 'even', 24.574], [333.967, 'though', 37.785], [374.612, 'a', 6.171], [383.643, 'single', 30.712],
+ [417.215, 'rehash', 35.563], [455.638, 'costs', 26.444], [510.908, '.', 2.948]] as const).map(([x, t, w]): Mark => ({ k: 'w', p: 1, x, y: 1541.09, s: 11, t, w, h: 22.254 })),
+]
+const joinAll = (marks: Mark[], ppi = 275) => {
+ const got = positionsOf(marks, ppi)
+ return joinItems(got.pages[0]!.map(item => ({ item, text: item.text })), got.blocks)
+}
+
+test('a space is found from the text size, not the line height: Noto Sans on the row grid and without it', () => {
+ expect(joinAll(NOTO_LINE)).toBe('- Geometric resizing keeps inserts at amortized, even though a single rehash costs .')
+ // Without the grid the line is the font's 1.36 of the size (14.982 pt).
+ expect(joinAll(NOTO_LINE.map(m => (m.h ? { ...m, h: 14.982 } : m)))).toBe('- Geometric resizing keeps inserts at amortized, even though a single rehash costs .')
+ // The pieces of one word ("**bo**ld") touch; a glued character joins.
+ const word = (x: number, t: string, w: number, more: Partial<Mark> = {}): Mark => ({ k: 'w', p: 1, x, y: 0, s: 11, t, w, h: 22.254, ...more })
+ const par: Mark = { k: 'b', p: 1, x: 0, y: 0, s: 11, kind: 'par' }
+ expect(joinAll([par, word(0, 'bo', 12.6), word(12.6, 'ld', 9.5), word(24.96, 'and', 19)])).toBe('bold and')
+ expect(joinAll([par, word(0, '中', 11), word(11, '文', 11, { g: 1 }), word(24, 'x', 6)])).toBe('中文 x')
+})
+
+test('the padding beside inline code is not a space; a space next to it is', () => {
+ // Theme vscode: 3 px of padding (2.357 pt), as wide as a space of 0.21 of the size.
+ const word = (x: number, t: string, w: number, code = false): Mark => ({ k: 'w', p: 1, x, y: 0, s: 11, t, w, h: 15, ...(code ? { pad: 2.357 } : {}) })
+ const par: Mark = { k: 'b', p: 1, x: 0, y: 0, s: 11, kind: 'par' }
+ // "a `list` b", "`x`y", "x`y`", "`b`," and "`a == b`" (the spaces of code are its own).
+ expect(joinAll([par, word(0, 'a', 6), word(11.217, 'list', 26.49, true), word(42.924, 'b', 6)])).toBe('a list b')
+ expect(joinAll([par, word(0, 'x', 6.6, true), word(8.957, 'y', 6)])).toBe('xy')
+ expect(joinAll([par, word(0, 'x', 6), word(8.357, 'y', 6.6, true)])).toBe('xy')
+ expect(joinAll([par, word(0, 'b', 6.6, true), word(8.957, ',', 3.3)])).toBe('b,')
+ expect(joinAll([par, word(0, 'a', 6.6, true), word(12.9, '==', 13.2, true), word(32.4, 'b', 6.6, true)])).toBe('a == b')
+ // Noto Sans (a space of 2.86 pt) on the grid's 2 rows.
+ const noto = (x: number, t: string, w: number, code = false): Mark => ({ ...word(x, t, w, code), h: 22.254 })
+ expect(joinAll([par, noto(0, 'forbids', 36), noto(41.217, 'list', 26.49, true), noto(72.924, 'as', 11)])).toBe('forbids list as')
+})
+
// ---- The command ------------------------------------------------------------
test('/iui grab and dump are commands with typeahead rows; dump with nothing seen says so', async $ => {