mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 11:51:49 +09:00
gathering strings indices for each typesetted line for cursor navigation
This commit is contained in:
@@ -13,6 +13,8 @@ const MEM = system.maxmem()
|
||||
const NO_LINEHEAD_PUNCT = [33,34,39,41,44,46,58,59,62,63,93,125]
|
||||
const NO_LINELAST_PUNCT = [34,39,40,60,91,123]
|
||||
|
||||
const TYPESET_DEBUG_PRINT = true
|
||||
|
||||
let PAGE_HEIGHT = 60
|
||||
let PAGE_WIDTH = 80
|
||||
// 80x60 -> 720x1080 text area; with 72px margin for each side, paper resolution is 864x1224, which is quite close to 1:sqrt(2) ratio
|
||||
@@ -24,6 +26,7 @@ let paragraphs = [
|
||||
'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.',
|
||||
'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'
|
||||
]
|
||||
let typesetIndices = []
|
||||
let cursorRow = 0
|
||||
let cursorCol = 0
|
||||
let page = 0
|
||||
@@ -80,16 +83,20 @@ function drawPRC() {
|
||||
print(s)
|
||||
}
|
||||
|
||||
function drawTextbuffer(from, toExclusive) {
|
||||
function typesetAndPrint(from, toExclusive) {
|
||||
let lineStart = from || scroll
|
||||
let lineEnd = toExclusive || scroll + paintHeight
|
||||
|
||||
let printbuf = []
|
||||
typesetIndices = [lineStart]
|
||||
for (let i = 0; i < paintHeight; i++) { printbuf.push('') }
|
||||
let vr = 0; let vc = 0 // virtual row/column
|
||||
let text = paragraphs.slice(lineStart, lineEnd).join('\n')
|
||||
|
||||
serial.println(`paintWidth = ${paintWidth}`)
|
||||
function ln(i) {
|
||||
vr += 1;vc = 0
|
||||
typesetIndices.push(i)
|
||||
}
|
||||
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
let cM2 = text.charCodeAt(i-2)
|
||||
@@ -98,38 +105,41 @@ function drawTextbuffer(from, toExclusive) {
|
||||
let c1 = text.charCodeAt(i+1)
|
||||
let c2 = text.charCodeAt(i+2)
|
||||
|
||||
serial.println(`i:${i} char:'${String.fromCharCode(cM2,cM1,32,c,32,c1,c2)}' Ln ${vr} Col ${vc}`)
|
||||
//serial.println(`i:${i} char:'${String.fromCharCode(cM2,cM1,32,c,32,c1,c2)}' Ln ${vr} Col ${vc}`)
|
||||
|
||||
if (c == 10) {
|
||||
printbuf[vr] += String.fromCharCode(254)
|
||||
vr += 1;vc = 0
|
||||
ln(i+1)
|
||||
printbuf[vr] = ''
|
||||
}
|
||||
else if (vc == paintWidth - 1 && NO_LINEHEAD_PUNCT.includes(c1)) {
|
||||
printbuf[vr] += String.fromCharCode(c, c1)
|
||||
vr += 1;vc = 0
|
||||
ln(i)
|
||||
i += (32 == c2) ? 2 : 1
|
||||
typesetIndices[typesetIndices.length - 1] = i+1
|
||||
}
|
||||
else if (vc == paintWidth - 1 && NO_LINELAST_PUNCT.includes(c)) {
|
||||
vr += 1;vc = 0
|
||||
ln(i)
|
||||
printbuf[vr] += String.fromCharCode(c)
|
||||
}
|
||||
else if (vc == paintWidth - 1) {
|
||||
if (32 == c || 32 == c1) {
|
||||
printbuf[vr] += String.fromCharCode(c)
|
||||
vr += 1;vc = 0
|
||||
|
||||
if (c1 == 32) i += 1
|
||||
ln(i+1)
|
||||
if (c1 == 32) {
|
||||
i += 1
|
||||
typesetIndices[typesetIndices.length - 1] += 1
|
||||
}
|
||||
}
|
||||
// if the head-char of the word happens to sit on the rightmost side and the char right before is ' '
|
||||
else if (32 == cM2) {
|
||||
// todo delet last char
|
||||
printbuf[vr] = printbuf[vr].substring(0, printbuf[vr].length - 1)
|
||||
vr += 1; vc = 1
|
||||
ln(i-1); vc = 1
|
||||
printbuf[vr] += String.fromCharCode(cM1, c)
|
||||
}
|
||||
else {
|
||||
printbuf[vr] += (45 == cM1 || 32 == cM1) ? ' ' : '-'
|
||||
vr += 1;vc = 0
|
||||
ln(i)
|
||||
printbuf[vr] += String.fromCharCode(c)
|
||||
vc += 1
|
||||
}
|
||||
@@ -147,6 +157,13 @@ function drawTextbuffer(from, toExclusive) {
|
||||
print(printbuf[y] || '')
|
||||
}
|
||||
|
||||
if (TYPESET_DEBUG_PRINT) {
|
||||
for (let y = 0; y < paintHeight; y++) {
|
||||
con.move(3+y, 1)
|
||||
print(typesetIndices[y]+1 || '-')
|
||||
}
|
||||
}
|
||||
|
||||
gotoText()
|
||||
}
|
||||
|
||||
@@ -197,4 +214,4 @@ function drawMain() {
|
||||
|
||||
|
||||
drawMain()
|
||||
drawTextbuffer(0)
|
||||
typesetAndPrint()
|
||||
Reference in New Issue
Block a user