ludic/packages/ludic.ui/layout_text.ludic

58 lines
2.3 KiB
Text

# layout_text.ludic - a text's size: one line, or as many as it takes to fit its room (white-space
# normal), breaking between words; nowrap keeps one line, and text-overflow: ellipsis cuts it
function ly_measure_text(ui_st: mut UiState, n: UiNode, avail: float) -> void {
n.lines = null
let lh = ly_line_h(n)
var one = ly_text_w(ui_st, n.text, n.rsize)
var room = avail - n.ml - n.mr - ly_hin(n)
if n.w >= 0.0 { room = n.w - ly_hin(n) }
let fits = n.ex != null and n.ex.fit_min > 0.0
if fits and room > 0.0 { one = Math.min(one, room) }
if n.nowrap or fits or room <= 0.0 or one <= room {
n.mw = n.mw + one
n.mh = n.mh + lh
return
}
n.lines = ly_wrap(ui_st, n.text, n.rsize, room)
var widest = 0.0
for i in 0 .. len(n.lines) { widest = Math.max(widest, ly_text_w(ui_st, n.lines[i], n.rsize)) }
n.mw = n.mw + widest
n.mh = n.mh + lh * float(len(n.lines))
}
@alloc_ok("a memo miss: once per text, size and room (memo_more.ludic), bounded by MM_CAP")
function ly_wrap_new(ui_st: mut UiState, s: string, size: float, room: float) -> []string {
let words = mm_split(ui_st, s, 32)
let out = new []string
var line = ""
for i in 0 .. len(words) {
if len(words[i]) == 0 { continue }
var next = words[i]
if len(line) > 0 { next = line + " " + words[i] }
if len(line) > 0 and ly_text_w(ui_st, next, size) > room {
push(out, line)
line = words[i]
} else { line = next }
}
if len(line) > 0 { push(out, line) }
return out
}
# a line cut to `room` with an ellipsis (it does not fit)
@alloc_ok("a memo miss: once per text, size and room (memo_more.ludic), bounded by MM_CAP")
function ly_ellipsis_new(ui_st: UiState, s: string, size: float, room: float) -> string {
var n = len(s)
while n > 0 and ly_text_w(ui_st, s[0..n] + "...", size) > room { n -= 1 }
return s[0..n] + "..."
}
function ly_text_w(ui_st: UiState, s: string, size: float) -> float {
if ui_st.ui_be != null and ui_st.ui_be.measure != null { return ui_st.ui_be.measure(s, size) }
return float(len(s)) * size * 0.5
}
# a child's size along and across a direction, margins included
function ly_main(c: UiNode, dir: int) -> float {
if dir == UI_ROW { return c.mw + c.ml + c.mr }
return c.mh + c.mt + c.mb
}
function ly_cross(c: UiNode, dir: int) -> float {
if dir == UI_ROW { return c.mh + c.mt + c.mb }
return c.mw + c.ml + c.mr
}