mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-09 22:54:03 +09:00
justification fixup
This commit is contained in:
@@ -280,21 +280,21 @@ function typesetJustified(lineStart, lineEnd) {
|
|||||||
|
|
||||||
|
|
||||||
function tryJustify(recDepth, adjust, fuckit) {
|
function tryJustify(recDepth, adjust, fuckit) {
|
||||||
let spacesRemoved = 0
|
|
||||||
let isLineEnd = (words.last().type == "ct_lf")
|
let isLineEnd = (words.last().type == "ct_lf")
|
||||||
// trim spaces at the end of the line
|
// trim spaces at the end of the line
|
||||||
while ("sp" == words.last().type) {
|
while ("sp" == words.last().type) {
|
||||||
spacesRemoved += getWordLen(words.pop())
|
words.pop()
|
||||||
}
|
}
|
||||||
// trim spaces at the head of the line
|
// trim spaces at the head of the line
|
||||||
|
// "pull" the words when removing preceding spaces
|
||||||
|
let pullLen = 0
|
||||||
while (!isParHead && "sp" == words.head().type) {
|
while (!isParHead && "sp" == words.head().type) {
|
||||||
let rlen = getWordLen(words.shift())
|
pullLen += getWordLen(words.shift())
|
||||||
spacesRemoved -= rlen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printdbg(`spacesRemoved = ${spacesRemoved}`)
|
printdbg(`Pulled ${pullLen} characters`)
|
||||||
//printdbg(`Space trim-nugding ${-spacesRemoved} characters`)
|
adjust += pullLen
|
||||||
//adjust -= spacesRemoved
|
|
||||||
|
|
||||||
let spcAfterPunct = [] // indices in the WORDS
|
let spcAfterPunct = [] // indices in the WORDS
|
||||||
words.forEach((o,i,a) => {
|
words.forEach((o,i,a) => {
|
||||||
@@ -325,19 +325,20 @@ function typesetJustified(lineStart, lineEnd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printbuf.push(justBuf.slice(0))
|
printbuf.push(justBuf.slice(0))
|
||||||
printdbg(`Cursor advance: ${justLen + adjust}`)
|
|
||||||
|
|
||||||
// NOTE: a dangling-lette-r simply does not happen; do the math! *tapping forehead with index finder*
|
|
||||||
|
|
||||||
printdbg(`(${justLen})[${words.flatMap(o => o.value.split('').map(s => typesetSymToVisual(s.charCodeAt(0)))).reduce((a,c) => a + String.fromCharCode(c),'')}${(isLineEnd) ? "\\\\" : "]"}<${adjust}>`)
|
|
||||||
|
|
||||||
let justedTextLen = 0
|
let justedTextLen = 0
|
||||||
let lastLine = printbuf.last()
|
let lastLine = printbuf.last()
|
||||||
for (let i = 0; i < lastLine.length; i++) {
|
for (let i = 0; i < lastLine.length; i++) {
|
||||||
justedTextLen += 1 - NO_PRINT_CHAR.includes(lastLine.charCodeAt(i))
|
justedTextLen += 1 - NO_PRINT_CHAR.includes(lastLine.charCodeAt(i))
|
||||||
}
|
}
|
||||||
printdbg(`justedTextLen = ${justedTextLen}`)
|
let cursorAdvance = justedTextLen + adjust
|
||||||
return justedTextLen + adjust
|
|
||||||
|
printdbg(`Cursor advance: ${cursorAdvance}`)
|
||||||
|
|
||||||
|
// NOTE: a dangling-lette-r simply does not happen; do the math! *tapping forehead with index finder*
|
||||||
|
|
||||||
|
printdbg(`(${justLen}/${cursorAdvance})[${words.flatMap(o => o.value.split('').map(s => typesetSymToVisual(s.charCodeAt(0)))).reduce((a,c) => a + String.fromCharCode(c),'')}${(isLineEnd) ? "\\\\" : "]"}<${adjust}>`)
|
||||||
|
return cursorAdvance
|
||||||
}
|
}
|
||||||
// try hyphenation
|
// try hyphenation
|
||||||
else if (justLen > paintWidth && getWordLen(words.last()) >= 4 && justLen - getWordLen(words.last()) <= paintWidth - 3 && !words.last().value.includes(SYM_HYPHEN)) {
|
else if (justLen > paintWidth && getWordLen(words.last()) >= 4 && justLen - getWordLen(words.last()) <= paintWidth - 3 && !words.last().value.includes(SYM_HYPHEN)) {
|
||||||
@@ -383,8 +384,8 @@ function typesetJustified(lineStart, lineEnd) {
|
|||||||
let old = words[expandTargets[i]].value
|
let old = words[expandTargets[i]].value
|
||||||
words[expandTargets[i]].value = (SYM_SPC == old) ? SYM_TWOSPC :
|
words[expandTargets[i]].value = (SYM_SPC == old) ? SYM_TWOSPC :
|
||||||
(SYM_TWOSPC == old) ? `\x00${SYM_SPC}\x00` :
|
(SYM_TWOSPC == old) ? `\x00${SYM_SPC}\x00` :
|
||||||
(` ${SYM_SPC} ` == old) ? `\x00${SYM_TWOSPC}\x00` :
|
(`\x00${SYM_SPC}\x00` == old) ? `\x00${SYM_TWOSPC}\x00` :
|
||||||
(` ${SYM_SPC} ` == old) ? `\x00\x00${SYM_SPC}\x00\x00` :
|
(`\x00${SYM_TWOSPC}\x00` == old) ? `\x00\x00${SYM_SPC}\x00\x00` :
|
||||||
(old.length % 2 == 0) ? ('\x00' + old) : (old + '\x00')
|
(old.length % 2 == 0) ? ('\x00' + old) : (old + '\x00')
|
||||||
//adjust += 1
|
//adjust += 1
|
||||||
}
|
}
|
||||||
@@ -413,7 +414,7 @@ function typesetJustified(lineStart, lineEnd) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (printbuf.length > 5) break
|
//if (printbuf.length > 10) break
|
||||||
if (printbuf.length > paintHeight || textCursor >= text.length) break
|
if (printbuf.length > paintHeight || textCursor >= text.length) break
|
||||||
|
|
||||||
printdbg("======================")
|
printdbg("======================")
|
||||||
|
|||||||
Reference in New Issue
Block a user