Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.1

0.19.1 / src / text / cells.lucb

## Monospace grid cells for UI text. The font sets each scalar in 0, 1 or 2 cells
## (luce-fonts' `cells`: wide East Asian scalars take two, combining marks none),
## so columns, carets, hit tests, wrapping and truncation all count with these
## helpers rather than one cell per scalar.
from luce_fonts import fonts
from luce_std import utf8
from text.document import Document

## The cells `scalar` takes at display `column` of the editor grid: a tab runs to
## the next stop of four, anything else takes its font cells.
pub func grid(scalar: u32, column: usize) -> usize:
    if scalar == 9:
        return 4 - column % 4
    return (usize)fonts.cells(scalar)

## The cells of `text`. Malformed bytes count a cell each, as the font would refuse
## them anyway; callers that need validation measure with the Font.
pub func width(text: str) -> usize:
    var total: usize = 0
    var at: usize = 0
    while at < text.length:
        if let decoded = utf8.decode(text.bytes, at):
            total += (usize)fonts.cells((u32)decoded.scalar)
            at += decoded.width
        else:
            total += 1
            at += 1
    return total

## The longest prefix of `text` that fits in `room` cells. A wide scalar that would
## straddle the edge is left out whole, and zero-width marks stay with their base.
pub func prefix(text: str, room: usize) -> str:
    var used: usize = 0
    var at: usize = 0
    while at < text.length:
        let decoded = utf8.decode(text.bytes, at) else break
        let taken = (usize)fonts.cells((u32)decoded.scalar)
        if used + taken > room:
            break
        used += taken
        at += decoded.width
    return (str)text.bytes[..<at]

## The cells of document scalars `start..<end` laid on one line from column 0.
pub func span(document: const Document*, start: usize, end: usize) -> usize:
    var total: usize = 0
    for index in start..<end:
        total += grid(document.scalar(index), total)
    return total

## The offset in `start..end` whose caret boundary lies nearest `x` cells right of
## `start`'s left edge. A click past a scalar's middle lands after it; zero-width
## marks never take the caret from between them and their base.
pub func nearest(document: const Document*, start: usize, end: usize, x: f64) -> usize:
    var column: usize = 0
    var index = start
    while index < end:
        let cells = grid(document.scalar(index), column)
        if cells > 0 and x < (f64)column + (f64)cells * 0.5:
            break
        column += cells
        index += 1
    return index

test "grid cells: tabs to stops, wide scalars two, marks none":
    assert(grid(9, 0) == 4 and grid(9, 3) == 1 and grid(9, 5) == 3)
    assert(grid((u32)'a', 7) == 1 and grid(0x7530, 0) == 2 and grid(0x301, 3) == 0)
    assert(width("田a") == 3 and width("会議の件") == 8 and width("e\u{301}") == 1)

test "a prefix never cuts a wide scalar in half and keeps marks with their base":
    assert(prefix("田中a", 4) == "田中")
    assert(prefix("田中a", 3) == "田")
    assert(prefix("田中a", 1) == "")
    assert(prefix("ae\u{301}b", 2) == "ae\u{301}")
    assert(prefix("abc", 10) == "abc")

test "spans and nearest boundaries count cells, not scalars":
    var document = try Document("田a\tb")
    defer document.close()
    assert(span(&document, 0, 1) == 2 and span(&document, 0, 2) == 3 and span(&document, 0, 4) == 5)
    assert(nearest(&document, 0, 4, 0.4) == 0)
    assert(nearest(&document, 0, 4, 1.2) == 1)
    assert(nearest(&document, 0, 4, 2.4) == 1)
    assert(nearest(&document, 0, 4, 2.6) == 2)
    assert(nearest(&document, 0, 4, 3.4) == 2)
    assert(nearest(&document, 0, 4, 9.0) == 4)
    var marked = try Document("e\u{301}x")
    defer marked.close()
    assert(nearest(&marked, 0, 3, 0.7) == 2)