pull in new version of cozette-ascii library, and fix resulting compile errors (which all occurred in text-builder.sml)

This commit is contained in:
2025-08-18 02:17:13 +01:00
parent 850d3b7bcb
commit af15b97400
5 changed files with 30 additions and 305 deletions

View File

@@ -1,264 +0,0 @@
structure TextBuilderUtils =
struct
type pos_data =
{chr: char, strIdx: int, absIdx: int, hd: string, tl: string list}
(* gets line start idx, relative to right hd *)
fun helpGetLineStartIdx (startLine, curLine, rLnHd) =
if startLine > curLine then
let val lnPos = startLine - curLine - 1
in Vector.sub (rLnHd, lnPos) + 1
end
else
0
(* gets line start idx, absolute *)
fun helpGetLineAbsIdx (curIdx, startLine, curLine, rLnHd) =
let
val startIdx =
if startLine > curLine then
let val lnPos = startLine - curLine - 1
in Vector.sub (rLnHd, lnPos) + 1
end
else
0
in
curIdx + startIdx
end
fun getLineAbsIdx (startLine, lineGap: LineGap.t) =
let
val {rightLines, line = curLine, idx = curIdx, ...} = lineGap
in
case rightLines of
rLnHd :: _ => helpGetLineAbsIdx (curIdx, startLine, curLine, rLnHd)
| [] => (* should never happen *) 0
end
end
signature MAKE_TEXT_BUILDER =
sig
type state
type env
val folder: TextBuilderUtils.pos_data * env * state -> state
val stopFold: env * state -> bool
end
functor MakeTextBuilder(Fn: MAKE_TEXT_BUILDER) =
struct
fun buildLoop (strIdx, absIdx, hd, tl, env, state) =
if Fn.stopFold (env, state) then
state
else if strIdx = String.size hd then
case tl of
hd :: tl => buildLoop (0, absIdx, hd, tl, env, state)
| [] => state
else
let
val chr = String.sub (hd, strIdx)
val posData =
{chr = chr, strIdx = strIdx, absIdx = absIdx, hd = hd, tl = tl}
val state = Fn.folder (posData, env, state)
in
buildLoop (strIdx + 1, absIdx + 1, hd, tl, env, state)
end
end
(* Text builder loop in normal mode, when there is no search to perform. *)
structure NormalTextBuilder =
MakeTextBuilder
(struct
type state =
{ cursorAcc: Real32.real vector
, bgAcc: Real32.real vector
, textAcc: Real32.real vector list
, posX: int
, posY: int
}
type env =
{ cursorPos: int
, startX: int
, r: Real32.real
, g: Real32.real
, b: Real32.real
, hr: Real32.real
, hg: Real32.real
, hb: Real32.real
, maxWidth: int
, maxHeight: int
, floatWidth: Real32.real
, floatHeight: Real32.real
}
fun stopFold ({maxWidth, maxHeight, ...}: env, {posX, posY, ...}: state) =
posX >= maxWidth andalso posY >= maxHeight
open TextConstants
fun makeSpace (env: env, state, absIdx) =
let
val {textAcc, cursorAcc, bgAcc, posX, posY} = state
val {cursorPos, r, g, b, floatWidth, floatHeight, ...} = env
(* if inside cursor, then create cursorAcc;
* else, just skip as usual *)
val cursorAcc =
if absIdx = cursorPos then
Rect.lerp
( posX
, posY
, fontSize
, fontSize
, floatWidth
, floatHeight
, r
, g
, b
)
else
cursorAcc
in
{ posX = posX + xSpace
, cursorAcc = cursorAcc
, posY = posY
, bgAcc = bgAcc
, textAcc = textAcc
}
end
fun makeNewLine (env: env, state, absIdx) =
let
val {textAcc, cursorAcc, bgAcc, posX, posY} = state
val {cursorPos, floatWidth, floatHeight, r, g, b, ...} = env
val cursorAcc =
if absIdx = cursorPos then
Rect.lerp
( posX
, posY
, fontSize
, fontSize
, floatWidth
, floatHeight
, r
, g
, b
)
else
cursorAcc
in
{ posY = posY + ySpace
, cursorAcc = cursorAcc
, posX = posX
, bgAcc = bgAcc
, textAcc = textAcc
}
end
fun makeChrHandlingNewLine
(posX, posY, textAcc, cursorAcc, bgAcc, chrFun, env: env, r, g, b) =
if posX + xSpace < #maxWidth env then
let
val {floatWidth, floatHeight, ...} = env
val textAcc =
chrFun
( posX
, posY
, fontSize
, fontSize
, floatWidth
, floatHeight
, r
, g
, b
) :: textAcc
val posX = posX + xSpace
in
{ posX = posX
, posY = posY
, textAcc = textAcc
, cursorAcc = cursorAcc
, bgAcc = bgAcc
}
end
else
(* posX >= maxWidth, so we draw on a new line.
* Since we reached the end of this one. *)
let
val {floatWidth, floatHeight, ...} = env
val startX = #startX env
val posY = posY + ySpace
val textAcc =
chrFun
( startX
, posY
, fontSize
, fontSize
, floatWidth
, floatHeight
, r
, g
, b
) :: textAcc
in
{ posX = startX + xSpace
, posY = posY
, textAcc = textAcc
, cursorAcc = cursorAcc
, bgAcc = bgAcc
}
end
fun makeChr (env: env, state: state, absIdx, chr) =
let
val chrFun = Vector.sub (CozetteAscii.asciiTable, Char.ord chr)
val {posX, posY, textAcc, cursorAcc, bgAcc} = state
in
if absIdx <> #cursorPos env then
let
val {r, g, b, ...} = env
in
makeChrHandlingNewLine
(posX, posY, textAcc, cursorAcc, bgAcc, chrFun, env, r, g, b)
end
else
(* at cursorPos *)
let
val {floatWidth, floatHeight, r, g, b, ...} = env
val cursorAcc = Rect.lerp
( posX
, posY
, fontSize
, fontSize
, floatWidth
, floatHeight
, r
, g
, b
)
val {hr, hg, hb, ...} = env
in
makeChrHandlingNewLine
( posX
, posY
, textAcc
, cursorAcc
, bgAcc
, chrFun
, env
, hr
, hg
, hb
)
end
end
fun folder (posData: TextBuilderUtils.pos_data, env, state) =
case #chr posData of
#" " => makeSpace (env, state, #absIdx posData)
| #"\n" => makeNewLine (env, state, #absIdx posData)
| chr => makeChr (env, state, #absIdx posData, chr)
end)

View File

@@ -70,6 +70,20 @@ struct
* on an indented line *)
(* same as buildTextStringAfterCursor, except this keeps track of absolute
* index and cursor pos too *)
fun makeChr (chr, posX, posY, windowWidth, windowHeight, r, g, b) =
CozetteAscii.make
( chr
, Real32.fromInt posX
, Real32.fromInt posY
, scale
, windowWidth
, windowHeight
, r
, g
, b
)
fun buildTextString
( pos
, str
@@ -172,17 +186,14 @@ struct
else
accToDrawMsg (acc, cursorAcc, bgAcc, env)
| chr =>
let
val chrFun = Vector.sub (CozetteAscii.asciiTable, Char.ord chr)
in
let in
if absIdx <> cursorPos then
(* not equal to cursor *)
if posX + xSpace < #w env then
let
val {r, g, b, fw, fh, ...} = env
val chrVec = chrFun
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val chrVec = makeChr (chr, posX, posY, fw, fh, r, g, b)
val acc = chrVec :: acc
in
buildTextString
@@ -204,8 +215,8 @@ struct
let
val {r, g, b, fw, fh, ...} = env
val chrVec = chrFun
(startX, posY + ySpace, fontSize, fontSize, fw, fh, r, g, b)
val chrVec = makeChr
(chr, startX, posY + ySpace, fw, fh, r, g, b)
val acc = chrVec :: acc
in
buildTextString
@@ -235,8 +246,7 @@ struct
in
if posX + xSpace < #w env then
let
val chrVec = chrFun
(posX, posY, fontSize, fontSize, fw, fh, hr, hg, hb)
val chrVec = makeChr (chr, posX, posY, fw, fh, hr, hg, hb)
val acc = chrVec :: acc
in
(* can start building after cursor now,
@@ -258,17 +268,8 @@ struct
end
else if posY + ySpace < #h env then
let
val chrVec = chrFun
( startX
, posY + ySpace
, fontSize
, fontSize
, fw
, fh
, hr
, hg
, hb
)
val chrVec = makeChr
(chr, startX, posY + ySpace, fw, fh, hr, hg, hb)
val acc = chrVec :: acc
in
(* can start building after cursor now,
@@ -503,8 +504,7 @@ struct
val b: Real32.real = 0.7
(* build char vec *)
val chrVec = chrFun
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val chrVec = makeChr (chr, posX, posY, fw, fh, r, g, b)
val acc = chrVec :: acc
(* build cursor (behind text) vec *)
@@ -538,8 +538,7 @@ struct
let
val {fw, fh, r, g, b, ...} = env
val chrVec = chrFun
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val chrVec = makeChr (chr, posX, posY, fw, fh, r, g, b)
val acc = chrVec :: acc
val searchPos =
if
@@ -570,8 +569,8 @@ struct
let
val {fw, fh, r, g, b, ...} = env
val chrVec = chrFun
(startX, posY + ySpace, fontSize, fontSize, fw, fh, r, g, b)
val chrVec = makeChr
(chr, startX, posY + ySpace, fw, fh, r, g, b)
val acc = chrVec :: acc
in
buildTextStringSearch
@@ -603,8 +602,7 @@ struct
in
if posX + xSpace < #w env then
let
val chrVec = chrFun
(posX, posY, fontSize, fontSize, fw, fh, hr, hg, hb)
val chrVec = makeChr (chr, posX, posY, fw, fh, hr, hg, hb)
val acc = chrVec :: acc
in
(* can start building after cursor now,
@@ -629,17 +627,8 @@ struct
end
else if posY + ySpace < #h env then
let
val chrVec = chrFun
( startX
, posY + ySpace
, fontSize
, fontSize
, fw
, fh
, hr
, hg
, hb
)
val chrVec = makeChr
(chr, startX, posY + ySpace, fw, fh, hr, hg, hb)
val acc = chrVec :: acc
in
(* can start building after cursor now,

View File

@@ -4,4 +4,5 @@ struct
val xSpace3 = xSpace * 3
val ySpace = 25
val fontSize: Real32.real = 30.0
val scale: Real32.real = 2.0
end