81 lines
2.9 KiB
Text
81 lines
2.9 KiB
Text
# calc.ludic - CSS's calc(), min(), max() and clamp(): + - * / and brackets over lengths (px, em, rem,
|
|
# vw, vh, a bare number is px), percentages and plain numbers. A width, height or inset keeps its
|
|
# percentage apart, to be taken of the room it is given; elsewhere a percentage has nothing to share.
|
|
function ca_is(s: string) -> bool { return ca_fn_at(s, 0) != "" }
|
|
# calc(...) worked out into ca_px and ca_pct; false (with the reason said) when it cannot be
|
|
function ca_eval(ui_st: mut UiState, s: string) -> bool {
|
|
ui_st.ca_s = s
|
|
ui_st.ca_i = 0
|
|
ui_st.ca_bad = ""
|
|
ui_st.ca_used = 0 # this evaluation's terms come from the pool, from the start
|
|
ui_st.ca_depth = 0
|
|
let v = ca_factor(ui_st)
|
|
ca_ws(ui_st)
|
|
if ui_st.ca_bad == "" and ui_st.ca_i < len(ui_st.ca_s) { ui_st.ca_bad = "nothing after its closing )" }
|
|
if ui_st.ca_bad != "" {
|
|
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
|
|
ui_err(ui_st, `{s}: calc() expected {ui_st.ca_bad}`)
|
|
return false
|
|
}
|
|
ui_st.ca_px = v.px
|
|
ui_st.ca_pct = v.pct
|
|
ui_st.ca_num = v.n
|
|
ui_st.ca_bare = v.bare
|
|
if v.bare { ui_st.ca_px = el_px(ui_st, v.n) }
|
|
return true
|
|
}
|
|
property CaVal {
|
|
px: float = 0.0
|
|
pct: float = 0.0
|
|
n: float = 0.0
|
|
bare: bool = true # a plain number, which may scale a length
|
|
}
|
|
function ca_ws(ui_st: mut UiState) -> void {
|
|
while ui_st.ca_i < len(ui_st.ca_s) and (ui_st.ca_s[ui_st.ca_i] == 32 or ui_st.ca_s[ui_st.ca_i] == 9 or ui_st.ca_s[ui_st.ca_i] == 10) { ui_st.ca_i += 1 }
|
|
}
|
|
function ca_sum(ui_st: mut UiState) -> CaVal {
|
|
var a = ca_product(ui_st)
|
|
while ui_st.ca_bad == "" {
|
|
ca_ws(ui_st)
|
|
if ui_st.ca_i >= len(ui_st.ca_s) or (ui_st.ca_s[ui_st.ca_i] != 43 and ui_st.ca_s[ui_st.ca_i] != 45) { return a }
|
|
let minus = ui_st.ca_s[ui_st.ca_i] == 45
|
|
ui_st.ca_i += 1
|
|
let b = ca_product(ui_st)
|
|
a = ca_add(ui_st, a, b, minus)
|
|
}
|
|
return a
|
|
}
|
|
function ca_add(ui_st: mut UiState, a: CaVal, b: CaVal, minus: bool) -> CaVal {
|
|
var sign = 1.0
|
|
if minus { sign = -1.0 }
|
|
let r = ca_new(ui_st)
|
|
if a.bare and b.bare {
|
|
r.n = a.n + sign * b.n
|
|
return r
|
|
}
|
|
r.bare = false
|
|
r.px = ca_len(ui_st, a).px + sign * ca_len(ui_st, b).px
|
|
r.pct = a.pct + sign * b.pct
|
|
return r
|
|
}
|
|
# a bare number beside a length is a length in px
|
|
function ca_len(ui_st: mut UiState, a: CaVal) -> CaVal {
|
|
if not a.bare { return a }
|
|
let r = ca_new(ui_st)
|
|
r.bare = false
|
|
r.px = el_px(ui_st, a.n)
|
|
return r
|
|
}
|
|
# a term, blank, from the pool: an evaluation is over before the next begins, and what it works out is
|
|
# copied into ca_px / ca_pct, so its records are used again by the next one (never made a frame)
|
|
function ca_new(ui_st: mut UiState) -> CaVal {
|
|
@alloc_ok("a pool miss: past the most terms one evaluation has had")
|
|
if ui_st.ca_used >= len(ui_st.ca_pool) { push(ui_st.ca_pool, new CaVal) }
|
|
let r = ui_st.ca_pool[ui_st.ca_used]
|
|
ui_st.ca_used += 1
|
|
r.px = 0.0
|
|
r.pct = 0.0
|
|
r.n = 0.0
|
|
r.bare = true
|
|
return r
|
|
}
|