typesetting line at a time and I broke <span> again

This commit is contained in:
minjaesong
2024-04-30 19:15:55 +09:00
parent 3244ad4a8a
commit 1f633f5ca2
4 changed files with 31 additions and 69 deletions

Binary file not shown.

View File

@@ -318,18 +318,12 @@ interface BTeXTextDrawCall {
fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float) fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float)
} }
data class MovableTypeDrawCall(val movableType: MovableType, override val rowStart: Int, override val rows: Int): BTeXTextDrawCall { data class TypesetDrawCall(val movableType: MovableType, override val rowStart: Int, override val rows: Int): BTeXTextDrawCall {
override fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float) { override fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float) {
movableType.draw(batch, x, y, rowStart, minOf(rows, doc.pageLines)) movableType.draw(batch, x, y, rowStart, minOf(rows, doc.pageLines))
} }
} }
/*data class RaggedLeftDrawCall(val raggedType: RaggedType, override val rowStart: Int, override val rowEnd: Int): BTeXTextDrawCall {
override fun draw(batch: SpriteBatch, x: Float, y: Float) {
raggedType.draw(batch, x, y, rowStart, rowEnd)
}
}*/
class BTeXDrawCall( class BTeXDrawCall(
val doc: BTeXDocument, val doc: BTeXDocument,
var posX: Int, // position relative to the page start (excluding page margin) var posX: Int, // position relative to the page start (excluding page margin)
@@ -368,14 +362,14 @@ class BTeXDrawCall(
fun isNotBlank(): Boolean { fun isNotBlank(): Boolean {
if (text == null && texture == null) return false if (text == null && texture == null) return false
if (text is MovableTypeDrawCall && text.movableType.inputText.isBlank()) return false if (text is TypesetDrawCall && text.movableType.inputText.isBlank()) return false
// if (text is RaggedLeftDrawCall && text.raggedType.inputText.isBlank()) return false // if (text is RaggedLeftDrawCall && text.raggedType.inputText.isBlank()) return false
return true return true
} }
internal val width: Int internal val width: Int
get() = if (text != null) get() = if (text != null)
if (text is MovableTypeDrawCall) if (text is TypesetDrawCall)
text.movableType.width text.movableType.width
else else
TODO() TODO()

View File

@@ -10,7 +10,7 @@ import net.torvald.terrarum.btex.BTeXDocument
import net.torvald.terrarum.btex.BTeXDocument.Companion.DEFAULT_ORNAMENTS_COL import net.torvald.terrarum.btex.BTeXDocument.Companion.DEFAULT_ORNAMENTS_COL
import net.torvald.terrarum.btex.BTeXDocument.Companion.DEFAULT_PAGE_FORE import net.torvald.terrarum.btex.BTeXDocument.Companion.DEFAULT_PAGE_FORE
import net.torvald.terrarum.btex.BTeXDrawCall import net.torvald.terrarum.btex.BTeXDrawCall
import net.torvald.terrarum.btex.MovableTypeDrawCall import net.torvald.terrarum.btex.TypesetDrawCall
import net.torvald.terrarum.ceilToFloat import net.torvald.terrarum.ceilToFloat
import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.toHex import net.torvald.terrarum.toHex
@@ -29,6 +29,7 @@ import java.io.FileInputStream
import java.io.StringReader import java.io.StringReader
import javax.xml.parsers.SAXParserFactory import javax.xml.parsers.SAXParserFactory
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
import kotlin.math.roundToInt
import kotlin.reflect.KFunction import kotlin.reflect.KFunction
import kotlin.reflect.full.declaredFunctions import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.findAnnotation
@@ -218,14 +219,20 @@ object BTeXParser {
// printdbg("Characters [col:${spanColour}] \t\"$str\"") // printdbg("Characters [col:${spanColour}] \t\"$str\"")
if (spanColour != oldSpanColour || spanColour != null) { if (spanColour != oldSpanColour || spanColour != null) {
val col = getSpanColourOrNull().toInternalColourCodeStr()
if (spanColour != null && paragraphBuffer.isNotEmpty() && paragraphBuffer.last() == '\u000E') { val spanGdxCol = getSpanColourOrNull()
paragraphBuffer.deleteAt(paragraphBuffer.lastIndex)
if (spanGdxCol == null) {
paragraphBuffer.append(TerrarumSansBitmap.noColorCode)
}
else {
paragraphBuffer.append(TerrarumSansBitmap.toColorCode(
spanGdxCol.r.times(15f).roundToInt(),
spanGdxCol.g.times(15f).roundToInt(),
spanGdxCol.b.times(15f).roundToInt()
))
} }
paragraphBuffer.append(col)
paragraphBuffer.append(str.replace(" ", " $col"))
} }
else { else {
paragraphBuffer.append(str) paragraphBuffer.append(str)
@@ -235,7 +242,7 @@ object BTeXParser {
} }
} }
private fun Color?.toInternalColourCodeStr(): String { /*private fun Color?.toInternalColourCodeStr(): String {
return if (this == null) "\u000E" return if (this == null) "\u000E"
else { else {
val rgba = this.toRGBA() val rgba = this.toRGBA()
@@ -245,7 +252,7 @@ object BTeXParser {
return "\u000F" + Char(r) + Char(g) + Char(b) return "\u000F" + Char(r) + Char(g) + Char(b)
} }
} }*/
private fun getFont() = when (cover) { private fun getFont() = when (cover) {
"typewriter" -> TODO() "typewriter" -> TODO()
@@ -482,7 +489,7 @@ object BTeXParser {
private val ccEmph = "#C11"// TerrarumSansBitmap.toColorCode(0xfd44) private val ccEmph = "#C11"// TerrarumSansBitmap.toColorCode(0xfd44)
private val ccItemName = "#14C" // TerrarumSansBitmap.toColorCode(0xf37d) private val ccItemName = "#14C" // TerrarumSansBitmap.toColorCode(0xf37d)
private val ccTargetName = "#170" // TerrarumSansBitmap.toColorCode(0xf3c4) private val ccTargetName = "#170" // TerrarumSansBitmap.toColorCode(0xf3c4)
// private val ccReset = TerrarumSansBitmap.toColorCode(0) // private val ccReset = TerrarumSansBitmap.noColorCode
@OpenTag // reflective access is impossible with 'private' @OpenTag // reflective access is impossible with 'private'
@@ -950,7 +957,7 @@ object BTeXParser {
doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx, doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx,
handler.currentTheme, handler.currentTheme,
handler.getSpanColour(), handler.getSpanColour(),
MovableTypeDrawCall(slugs, subset.first, subset.second) TypesetDrawCall(slugs, subset.first, subset.second)
) )
doc.appendDrawCall(doc.pages[pageNum], drawCall); drawCalls.add(drawCall) doc.appendDrawCall(doc.pages[pageNum], drawCall); drawCalls.add(drawCall)
@@ -972,7 +979,7 @@ object BTeXParser {
doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx, doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx,
handler.currentTheme, handler.currentTheme,
handler.getSpanColour(), handler.getSpanColour(),
MovableTypeDrawCall(slugs, subset.first, subset.second) TypesetDrawCall(slugs, subset.first, subset.second)
) )
doc.appendDrawCall(doc.pages[pageNum], drawCall); drawCalls.add(drawCall) doc.appendDrawCall(doc.pages[pageNum], drawCall); drawCalls.add(drawCall)
@@ -985,25 +992,6 @@ object BTeXParser {
} }
} }
// apply internal colour codes
// grammar:
// <SI> [0-9A-F]{6,6} : apply colour code
// <SO> : remove colour code
drawCalls.forEach {
if (it.text != null && it.text is MovableTypeDrawCall) {
it.text.movableType.typesettedSlugs.forEach { slug -> slug.forEach { block ->
val blockText = block.block.text
// println(blockText.toReadable())
if (blockText.startsWithColourCode()) {
block.colour = blockText.getCCorNull()
}
} }
}
}
// println("---------------------------")
// if typesetting the paragraph leaves the first line of new page empty, move the "row cursor" back up // if typesetting the paragraph leaves the first line of new page empty, move the "row cursor" back up
if (doc.linesPrintedOnPage[pageNum] == 1 && doc.pages[pageNum].isEmpty()) doc.linesPrintedOnPage[pageNum] = 0 // '\n' adds empty draw call to the page, which makes isEmpty() to return false if (doc.linesPrintedOnPage[pageNum] == 1 && doc.pages[pageNum].isEmpty()) doc.linesPrintedOnPage[pageNum] = 0 // '\n' adds empty draw call to the page, which makes isEmpty() to return false
@@ -1027,23 +1015,6 @@ object BTeXParser {
Character.toString(it.toChar()) Character.toString(it.toChar())
} }
private fun CodepointSequence.startsWithColourCode() = (this.size > 5 &&
this[1] == 0x0F &&
(this[2] in 0xF800..0xF8FF) &&
(this[3] in 0xF800..0xF8FF) &&
(this[4] in 0xF800..0xF8FF)) ||
(this.size > 3 && this[0] == 0x0E)
private fun CodepointSequence.getCCorNull(): Color? {
if (this[1] == 0x0F) {
val r = this[2] - 0xF800
val g = this[3] - 0xF800
val b = this[4] - 0xF800
return Color(r / 255f, g / 255f, b / 255f, 1f)
}
else return null
}
private fun typesetTOCline(name: String, pageNum: Int, handler: BTeXHandler, indentation: Int = 0, pageToWrite: Int? = null) { private fun typesetTOCline(name: String, pageNum: Int, handler: BTeXHandler, indentation: Int = 0, pageToWrite: Int? = null) {
val pageNum = pageNum.plus(1).toString() val pageNum = pageNum.plus(1).toString()
val pageNumWidth = getFont().getWidth(pageNum) val pageNumWidth = getFont().getWidth(pageNum)
@@ -1060,12 +1031,9 @@ object BTeXParser {
val dotGap = 10 val dotGap = 10
val y = y + (call.lineCount - 1).coerceAtLeast(0) * doc.lineHeightInPx val y = y + (call.lineCount - 1).coerceAtLeast(0) * doc.lineHeightInPx
val textWidth = if (call.text is MovableTypeDrawCall) { val textWidth = if (call.text is TypesetDrawCall) {
call.text.movableType.typesettedSlugs.last().last().getEndPos() font.getWidth(call.text.movableType.typesettedSlugs.last())
} }
/*else if (call.text is RaggedTypeDrawCall) {
call.text.raggedType.typesettedSlugs.last().last().getEndPos()
}*/
else call.width else call.width
var dotCursor = (x + textWidth).div(dotGap).ceilToFloat() * dotGap var dotCursor = (x + textWidth).div(dotGap).ceilToFloat() * dotGap

View File

@@ -25,8 +25,8 @@ import kotlin.system.measureTimeMillis
*/ */
class BTeXTest : ApplicationAdapter() { class BTeXTest : ApplicationAdapter() {
// val filePath = "btex.btexbin" // val filePath = "btex.xml"
val filePath = "literature/en/daniel_defoe_robinson_crusoe.btexbin" val filePath = "literature/en/daniel_defoe_robinson_crusoe.xml"
// val filePath = "literature/ruRU/anton_chekhov_palata_no_6.xml" // val filePath = "literature/ruRU/anton_chekhov_palata_no_6.xml"
// val filePath = "literature/koKR/yisang_nalgae.xml" // val filePath = "literature/koKR/yisang_nalgae.xml"
@@ -58,18 +58,18 @@ class BTeXTest : ApplicationAdapter() {
println("Time spent on typesetting [ms]: $it") println("Time spent on typesetting [ms]: $it")
} }
measureTimeMillis { /*measureTimeMillis {
document.finalise() document.finalise()
documentHandler.dispose() documentHandler.dispose()
}.also { }.also {
println("Time spent on finalising [ms]: $it") println("Time spent on finalising [ms]: $it")
} }*/
measureTimeMillis { /*measureTimeMillis {
document.serialise(File("./assets/mods/basegame/books/${filePath.replace(".xml", ".btexbin")}")) document.serialise(File("./assets/mods/basegame/books/${filePath.replace(".xml", ".btexbin")}"))
}.also { }.also {
println("Time spent on serialisation [ms]: $it") println("Time spent on serialisation [ms]: $it")
} }*/
} }
else { else {
measureTimeMillis { measureTimeMillis {