2024-10-04 23:23:25 +01:00
|
|
|
structure Buffer =
|
|
|
|
|
struct
|
2024-10-05 02:03:17 +01:00
|
|
|
val xSpace = 12
|
|
|
|
|
val xSpace3 = xSpace * 3
|
|
|
|
|
val ySpace = 23
|
|
|
|
|
|
2024-10-04 23:23:25 +01:00
|
|
|
(* builds text from a string with char-wrap.
|
|
|
|
|
* char-wrap is a similar concept to word-wrap,
|
|
|
|
|
* but it breaks on character in the middle of a word.
|
|
|
|
|
* Todo:
|
|
|
|
|
* - Possibly add visual horizontal indentation when char-wrap occurs
|
|
|
|
|
* on an indented line *)
|
|
|
|
|
fun buildTextString
|
|
|
|
|
( pos, str, acc, posX, posY, startX
|
|
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
) =
|
|
|
|
|
if pos < String.size str then
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#" " =>
|
|
|
|
|
(* if space, then proceed forward one char
|
|
|
|
|
* without adding to acc *)
|
|
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, posX + xSpace, posY, startX
|
2024-10-05 00:17:11 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
|
|
|
|
, r, g, b, tl
|
|
|
|
|
)
|
2024-10-04 23:23:25 +01:00
|
|
|
| #"\t" =>
|
|
|
|
|
(* if tab, proceed forward one char,
|
|
|
|
|
* and jump visually forwards three chars *)
|
|
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, posX + xSpace3, posY, startX
|
2024-10-05 00:17:11 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
|
|
|
|
, r, g, b, tl
|
|
|
|
|
)
|
2024-10-04 23:23:25 +01:00
|
|
|
| #"\n" =>
|
|
|
|
|
(* if \n, move down vertically, and move to start horizontally
|
|
|
|
|
* assuming we have not exceeded the window's height.
|
|
|
|
|
* If we have exceeded the window's height, just return acc. *)
|
2024-10-05 02:03:17 +01:00
|
|
|
if posY + ySpace < windowHeight then
|
2024-10-04 23:23:25 +01:00
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, startX, posY + ySpace, startX
|
2024-10-04 23:23:25 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
)
|
|
|
|
|
else
|
2024-10-05 00:17:11 +01:00
|
|
|
(* return if there is no more vertical space after line break *)
|
2024-10-04 23:23:25 +01:00
|
|
|
acc
|
|
|
|
|
| #"\r" =>
|
|
|
|
|
(* same as \n, except we also check if we are in a \r\n pair,
|
|
|
|
|
* and proceed two characters forward if so *)
|
2024-10-05 02:03:17 +01:00
|
|
|
if posY + ySpace < windowHeight then
|
2024-10-04 23:23:25 +01:00
|
|
|
if
|
|
|
|
|
pos < String.size str - 1
|
|
|
|
|
andalso String.sub (str, pos + 1) = #"\n"
|
2024-10-05 00:17:11 +01:00
|
|
|
then
|
2024-10-04 23:23:25 +01:00
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 2, str, acc, startX, posY + ySpace, startX
|
2024-10-04 23:23:25 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
)
|
2024-10-05 00:17:11 +01:00
|
|
|
else
|
2024-10-04 23:23:25 +01:00
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, startX, posY + ySpace, startX
|
2024-10-04 23:23:25 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
)
|
|
|
|
|
else
|
2024-10-05 00:17:11 +01:00
|
|
|
(* return if there is no more vertical space after line break *)
|
2024-10-04 23:23:25 +01:00
|
|
|
acc
|
|
|
|
|
| chr =>
|
|
|
|
|
(* for any other character, add it to acc if there is space,
|
|
|
|
|
* and proceed forward one character in the string *)
|
|
|
|
|
let
|
|
|
|
|
val chrFun = Vector.sub (CozetteAscii.asciiTable, Char.ord chr)
|
|
|
|
|
in
|
2024-10-05 02:03:17 +01:00
|
|
|
if posX + xSpace < windowWidth then
|
2024-10-04 23:23:25 +01:00
|
|
|
(* if there is horizontal space, place char on the right *)
|
|
|
|
|
let
|
|
|
|
|
val chrVec = chrFun
|
|
|
|
|
(posX, posY, 25.0, 25.0, fWindowWidth, fWindowHeight, r, g, b)
|
|
|
|
|
val acc = chrVec :: acc
|
|
|
|
|
in
|
|
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, posX + xSpace, posY, startX
|
2024-10-04 23:23:25 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
)
|
|
|
|
|
end
|
2024-10-05 02:03:17 +01:00
|
|
|
else if posY + ySpace < windowHeight then
|
2024-10-04 23:23:25 +01:00
|
|
|
(* if there is vertical space, place char down below at startX *)
|
|
|
|
|
let
|
|
|
|
|
val chrVec = chrFun
|
|
|
|
|
( startX, posY + 25, 25.0, 25.0
|
|
|
|
|
, fWindowWidth, fWindowHeight
|
|
|
|
|
, r, g, b
|
|
|
|
|
)
|
|
|
|
|
val acc = chrVec :: acc
|
|
|
|
|
in
|
|
|
|
|
buildTextString
|
2024-10-05 02:03:17 +01:00
|
|
|
( pos + 1, str, acc, startX + xSpace, posY + ySpace, startX
|
2024-10-04 23:23:25 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b, tl
|
2024-10-04 23:23:25 +01:00
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
else
|
2024-10-05 00:17:11 +01:00
|
|
|
(* return if no space horizontally or vertically *)
|
2024-10-04 23:23:25 +01:00
|
|
|
acc
|
|
|
|
|
end
|
|
|
|
|
else
|
2024-10-05 00:17:11 +01:00
|
|
|
(* if we reached the end of the string,
|
|
|
|
|
* call function to build next string *)
|
|
|
|
|
continueBuildTextLineGap
|
|
|
|
|
( tl, acc, posX, posY, startX
|
2024-10-05 15:07:23 +01:00
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
2024-10-05 00:17:11 +01:00
|
|
|
, r, g, b
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
and continueBuildTextLineGap
|
|
|
|
|
( strList, acc, posX, posY, startX
|
|
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
|
|
|
|
, r, g, b
|
|
|
|
|
) =
|
|
|
|
|
case strList of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
buildTextString
|
|
|
|
|
( 0, hd, acc, posX, posY, startX
|
|
|
|
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
|
|
|
|
, r, g, b, tl
|
|
|
|
|
)
|
|
|
|
|
| [] => acc
|
|
|
|
|
|
|
|
|
|
(* todo:
|
|
|
|
|
* before calling continueBuildTextLineGap,
|
|
|
|
|
* find start position inside LineGap
|
|
|
|
|
* to start building from *)
|
|
|
|
|
fun startBuildTextLineGap (startLine, lineGap: LineGap.t, windowWidth, windowHeight) =
|
|
|
|
|
let
|
2024-10-05 23:16:32 +01:00
|
|
|
val lineGap = LineGap.goToLine (startLine, lineGap)
|
|
|
|
|
val {rightStrings, rightLines, line = curLine, ...} = lineGap
|
|
|
|
|
|
|
|
|
|
val acc = case (rightStrings, rightLines) of
|
|
|
|
|
(rStrHd::rStrTl, rLnHd::_) =>
|
|
|
|
|
let
|
|
|
|
|
(* get index of line to start building from *)
|
|
|
|
|
val lnPos = startLine - curLine
|
|
|
|
|
val startIdx = Vector.sub (rLnHd, lnPos)
|
|
|
|
|
val startIdx =
|
|
|
|
|
if String.sub (rStrHd, startIdx) = #"\r" andalso startIdx
|
|
|
|
|
< String.size rStrHd - 1 andalso String.sub (rStrHd,
|
|
|
|
|
startIdx + 1) = #"\n"
|
|
|
|
|
then
|
|
|
|
|
(* handle \r\n pair *)
|
|
|
|
|
startIdx + 2
|
|
|
|
|
else startIdx + 1
|
|
|
|
|
in
|
|
|
|
|
buildTextString ( startIdx, rStrHd, [], 5, 5, 5
|
|
|
|
|
, windowWidth, windowHeight,
|
|
|
|
|
Real32.fromInt windowWidth, Real32.fromInt windowHeight
|
|
|
|
|
, 0.0, 0.0, 0.0, rStrTl
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) =>
|
|
|
|
|
(* requested line goes beyond the buffer,
|
|
|
|
|
* so just return empty list as there is nothig
|
|
|
|
|
* else we can do. *)
|
|
|
|
|
[]
|
|
|
|
|
in
|
|
|
|
|
Vector.concat acc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun startBuildTextLineGap
|
|
|
|
|
(startLine, lineGap: LineGap.t, windowWidth, windowHeight) =
|
|
|
|
|
let
|
|
|
|
|
val lineGap = LineGap.goToLine (startLine, lineGap)
|
|
|
|
|
val {rightStrings, rightLines, line = curLine, ...} = lineGap
|
|
|
|
|
|
2024-10-05 00:17:11 +01:00
|
|
|
val acc =
|
2024-10-05 23:16:32 +01:00
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(rStrHd :: rStrTl, rLnHd :: _) =>
|
|
|
|
|
let
|
|
|
|
|
(* get index of line to start building from *)
|
|
|
|
|
val lnPos = startLine - curLine
|
|
|
|
|
val startIdx = Vector.sub (rLnHd, lnPos)
|
|
|
|
|
val startIdx =
|
|
|
|
|
if
|
|
|
|
|
String.sub (rStrHd, startIdx) = #"\r"
|
|
|
|
|
andalso startIdx < String.size rStrHd - 1
|
|
|
|
|
andalso String.sub (rStrHd, startIdx + 1) = #"\n"
|
|
|
|
|
then (* handle \r\n pair *) startIdx + 2
|
|
|
|
|
else startIdx + 1
|
|
|
|
|
in
|
|
|
|
|
buildTextString
|
|
|
|
|
( startIdx, rStrHd, []
|
|
|
|
|
, 5, 5, 5
|
|
|
|
|
, windowWidth, windowHeight
|
|
|
|
|
, Real32.fromInt windowWidth, Real32.fromInt windowHeight
|
|
|
|
|
, 0.0, 0.0, 0.0
|
|
|
|
|
, rStrTl
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) =>
|
|
|
|
|
(* requested line goes beyond the buffer,
|
|
|
|
|
* so just return empty list as there is nothig
|
|
|
|
|
* else we can do. *)
|
|
|
|
|
[]
|
2024-10-05 00:17:11 +01:00
|
|
|
in
|
2024-10-05 23:27:25 +01:00
|
|
|
(Vector.concat acc, lineGap)
|
2024-10-05 00:17:11 +01:00
|
|
|
end
|
2024-10-04 23:23:25 +01:00
|
|
|
end
|