dymokomi/luce-ui
Source of release 0.19.2
0.19.2 / src / text / drawing.lucb
## Visible-line rendering consumes generic style ranges. Layout and language
## analysis stay outside this module, and every draw uses the portable GPU target.
from luce_gpu import gpu
import interop
from luce_std import math, utf8
from font import Font
from painter import Painter
from geometry import Point
from text.cells import grid
from text.document import Document
from text.projection import Projection
from text.folding import Folding
from text.diff import LineDiff, ChangeKind
from text.style import Highlight, EditorTheme, Swatch, swatch_span
from text.diagnostics import Diagnostics
import text.diagnostic_drawing as marks
import strings
pub func vertical_padding(font: interop.Reference[Font]) -> f64:
return (font.value().line_height() - font.value().height()) * 0.5
pub func gutter_width(numbers: bool, font: interop.Reference[Font], line_count: usize, folding: bool = false) -> f64:
# Size from the physical line count so scrolling or folding cannot shift
# the text. Reserve one cell on each side of the widest line-number label.
var digits: usize = 1
var largest = line_count
while largest >= 10:
largest //= 10
digits += 1
return font.value().advance() * (((f64)digits + 2.0 if numbers else 0.0) + (2.0 if folding else 0.0))
## Fill a rectangle with thin 45° stripes clipped to [x, x+w], for marking a gutter
## region the AI is working on. Each diagonal runs up-right and is trimmed to the rect.
func stripes(painter: Painter*, x: f64, y: f64, w: f64, h: f64, spacing: f64, color: gpu.Color) -> !:
let bottom = y + h
var base = x - h
while base <= x + w:
let start = math.max(0.0, x - base)
let finish = math.min(h, x + w - base)
if finish > start:
try painter.line(Point(base + start, bottom - start), Point(base + finish, bottom - finish), 1.0, color)
base += spacing
pub func paint(document: const Document*, painter: Painter*, width: f64, height: f64,
scroll_x: f64, scroll_y: f64, numbers: bool, focused: bool,
caret_visible: bool, font: interop.Reference[Font], theme: EditorTheme,
highlights: const Highlight[], projection: const Projection*, folds: const Folding*, diff: const LineDiff*, hovered_row: usize?,
ai_first: usize, ai_last: usize, swatches: const Swatch[], reserve: bool,
diagnostics: const Diagnostics*) -> !:
let folding = folds.values().length > 0
let gutter = gutter_width(numbers, font, projection.line_count(), folding)
try painter.rectangle(gpu.Rect(width = width, height = height), theme.background)
if numbers or folding:
try painter.rectangle(gpu.Rect(width = gutter, height = height), theme.gutter)
let rows = projection.visible_count()
let first_row = (usize)(scroll_y / font.value().line_height())
var offset = projection.row(first_row if first_row < rows else 0).start
var row = first_row
var y = vertical_padding(font) + (f64)row * font.value().line_height() - scroll_y
let current = projection.visual_row(document.caret())
var style: usize = 0
while style < highlights.length and highlights[style].end <= (i64)offset:
style += 1
var swatch: usize = 0
while swatch < swatches.length and swatches[swatch].offset < (i64)offset:
swatch += 1
while y < height and row < rows:
let segment = projection.row(row)
let line = projection.line(segment.physical)
let row_top = y - vertical_padding(font)
if row == current:
try painter.rectangle(gpu.Rect(0.0, row_top, gutter, font.value().line_height()), theme.gutter_active)
elif hovered_row == row:
try painter.rectangle(gpu.Rect(0.0, row_top, gutter, font.value().line_height()), theme.gutter_hover)
if ai_first > 0 and segment.physical + 1 >= ai_first and segment.physical + 1 <= ai_last and gutter > 0.0:
try stripes(painter, 0.0, row_top, gutter, font.value().line_height(),
math.max(4.0, font.value().advance() * 0.8), theme.line_number)
if focused and row == current:
try painter.rectangle(gpu.Rect(gutter, row_top, math.max(0.0, width - gutter), font.value().line_height()), theme.current_line)
if diff.present():
let bar = math.max(2.0, font.value().advance() * 0.32)
let mark = diff.mark(segment.physical)
if mark != ChangeKind.unchanged:
let color = theme.diff_added if mark == ChangeKind.added else theme.diff_modified
try painter.rectangle(gpu.Rect(gutter - bar, row_top, bar, font.value().line_height()), color)
if segment.leading and diff.deletion_above(segment.physical):
try painter.rectangle(gpu.Rect(gutter - bar * 2.4, row_top - 1.5, bar * 2.4, 3.0), theme.diff_deleted)
if segment.end == line.end and segment.physical + 1 == projection.line_count() and diff.tail_deletion():
try painter.rectangle(gpu.Rect(gutter - bar * 2.4, row_top + font.value().line_height() - 1.5, bar * 2.4, 3.0), theme.diff_deleted)
if numbers and segment.leading:
if let worst = diagnostics.worst(line.start, line.end):
try marks.gutter_mark(painter, row_top, font, marks.severity_color(theme, worst))
let number_color = theme.active_line_number if row == current or hovered_row == row else theme.line_number
if numbers and segment.leading:
var label: u8[32]
let text = try strings.format(label, f"{segment.physical + 1}")
let extent = try font.value().measure(text)
try painter.text(text, gutter - extent - font.value().advance() * (3.0 if folding else 1.0), y, number_color, font)
try paint_line(document, painter, segment.start, segment.end, segment.column, segment.end == line.end,
y, width, scroll_x, gutter, focused, caret_visible, font, theme, highlights, &style, swatches, &swatch, reserve)
if diagnostics.values().length > 0:
try marks.underline(document, painter, diagnostics, segment.start, segment.end, segment.column, segment.end == line.end,
y, width, scroll_x, gutter, font, theme, swatches if reserve else swatches[..<0])
if segment.leading:
if let index = folds.at(line.start):
let fold = folds.values()[index]
try painter.text("+" if fold.collapsed else "-", gutter - font.value().advance() * 1.5, y, number_color, font)
if fold.collapsed:
let x = gutter + font.value().advance() * (f64)(line.columns + 2) - scroll_x
if x >= gutter and x < width - 8.0:
try painter.text("...", x, y, theme.line_number, font)
row += 1
y += font.value().line_height()
let total = (f64)rows * font.value().line_height() + vertical_padding(font) * 2.0
if total > height and height > 0.0:
let thumb = math.max(24.0, height * height / total)
let position = scroll_y / math.max(1.0, total - height) * math.max(0.0, height - thumb)
try painter.rectangle(gpu.Rect(math.max(0.0, width - 6.0), position, 4.0, thumb), theme.scrollbar)
## Paint one visual row: the scalar range [start, end). `start_column` is the
## display column at `start`, so a wrapped segment draws left-aligned yet keeps
## its tab stops; `last` marks a physical line's final segment, which alone draws
## the trailing newline selection and accepts a caret at `end`.
func paint_line(document: const Document*, painter: Painter*, start: usize, end: usize,
start_column: usize, last: bool, y: f64, width: f64, scroll_x: f64,
gutter: f64, focused: bool, caret_visible: bool, font: interop.Reference[Font],
theme: EditorTheme, highlights: const Highlight[], style: usize*,
swatches: const Swatch[], swatch: usize*, reserve: bool) -> !:
let advance = font.value().advance()
let origin = gutter + advance - scroll_x - (f64)start_column * advance
var column = start_column
var bytes: u8[2048]
var used: usize = 0
var run_x: f64 = 0.0
var run_color = theme.foreground
var cursor_x = gutter + advance - scroll_x
for index in start..<end:
# Reserving mode (no soft wrap): a chip claims `swatch_span` cells before the
# scalar, shifting the rest of the line right so `#rrggbb` stays intact. Column
# math in editing.column/at_column matches this, so caret and clicks stay true.
if reserve:
while *swatch < swatches.length and swatches[*swatch].offset < (i64)index:
*swatch += 1
if *swatch < swatches.length and swatches[*swatch].offset == (i64)index:
let chip_x = origin + (f64)column * advance
if used > 0:
try painter.text((str)bytes[..<used], run_x, y, run_color, font)
used = 0
if chip_x + (f64)swatch_span * advance > gutter and chip_x < width - 8.0:
try draw_swatch(painter, chip_x, y, advance, swatch_span, font, theme, swatches[*swatch].color)
column += swatch_span
*swatch += 1
let scalar = document.scalar(index)
let cells = grid(scalar, column)
let x = origin + (f64)column * advance
let visible = x + (f64)cells * advance > gutter and x < width - 8.0
if index == document.caret():
cursor_x = x
if visible and index >= document.first() and index < document.last():
let left = math.max(gutter, x)
try painter.rectangle(gpu.Rect(left, y - 2.0, math.min(width - left, x + (f64)cells * advance - left), font.value().line_height()), theme.selection)
# Wrapping mode: draw a compact chip in the scalar's own cell, no shift, so the
# projection's column layout stays valid.
if not reserve:
while *swatch < swatches.length and swatches[*swatch].offset < (i64)index:
*swatch += 1
if *swatch < swatches.length and swatches[*swatch].offset == (i64)index:
if used > 0:
try painter.text((str)bytes[..<used], run_x, y, run_color, font)
used = 0
if visible and x >= gutter:
try draw_swatch(painter, x, y, advance, 1, font, theme, swatches[*swatch].color)
*swatch += 1
column += cells
if index + 1 == document.caret():
cursor_x = origin + (f64)column * advance
continue
while *style < highlights.length and highlights[*style].end <= (i64)index:
*style += 1
var color = theme.foreground
if *style < highlights.length and highlights[*style].start <= (i64)index:
color = highlights[*style].color
if used > 0 and (not same_color(color, run_color) or scalar == 9 or not visible or used + 4 > bytes.length):
try painter.text((str)bytes[..<used], run_x, y, run_color, font)
used = 0
if visible and scalar != 9 and x >= gutter:
if used == 0:
run_x = x
run_color = color
used += utf8.encode(scalar, bytes[used..]) else trap("invalid document scalar")
column += cells
if index + 1 == document.caret():
cursor_x = origin + (f64)column * advance
if used > 0:
try painter.text((str)bytes[..<used], run_x, y, run_color, font)
if last and document.last() > end and document.first() <= end:
let x = origin + (f64)column * advance
if x >= gutter and x < width:
try painter.rectangle(gpu.Rect(x, y - 2.0, advance, font.value().line_height()), theme.selection)
if focused and caret_visible and document.caret() >= start and (document.caret() < end or (last and document.caret() == end)) and cursor_x >= gutter and cursor_x < width - 6.0:
try painter.rectangle(gpu.Rect(cursor_x, y - 2.0, theme.caret_width, font.value().height() + 2.0), theme.caret)
func same_color(a: gpu.Color, b: gpu.Color) -> bool:
return a.red == b.red and a.green == b.green and a.blue == b.blue
## Draw a color chip in the `span` cells starting at `x`: a square the font's
## height (capped to the slot's width) less a small margin, centered in the slot
## so it never touches the neighboring glyphs, filled with `color` inside a
## one-point frame in the line-number color that keeps a chip on a matching
## background legible.
func draw_swatch(painter: Painter*, x: f64, y: f64, advance: f64, span: usize, font: interop.Reference[Font], theme: EditorTheme, color: gpu.Color) -> !:
let extent = font.value().height()
let slot = (f64)span * advance
let side = math.floor(math.min(extent, slot) - 2.0)
if side < 2.0:
return
let sx = x + (slot - side) * 0.5
let sy = y + (extent - side) * 0.5
try painter.rectangle(gpu.Rect(sx - 1.0, sy - 1.0, side + 2.0, side + 2.0), theme.line_number)
try painter.rectangle(gpu.Rect(sx, sy, side, side), color)