btex: fixed the paragraph overflows

This commit is contained in:
minjaesong
2024-04-28 18:23:50 +09:00
parent 54663657bd
commit 45b1a344f2
6 changed files with 120 additions and 34 deletions

View File

@@ -21,14 +21,15 @@ class BTeXDocument {
var textWidth = 480
var lineHeightInPx = 24
var pageLines = 25
var textHeight = pageLines * lineHeightInPx
val textHeight: Int
get() = pageLines * lineHeightInPx
val pageMarginH = 15
val pageMarginV = 12
val pageWidth: Int
val pageDimensionWidth: Int
get() = 2 * pageMarginH + textWidth
val pageHeight: Int
val pageDimensionHeight: Int
get() = 2 * pageMarginV + textHeight
companion object {
@@ -50,12 +51,12 @@ class BTeXDocument {
internal val linesPrintedOnPage = ArrayList<Int>()
fun addNewPage(back: Color = DEFAULT_PAGE_BACK) {
pages.add(BTeXPage(back, pageWidth, pageHeight))
pages.add(BTeXPage(back, pageDimensionWidth, pageDimensionHeight))
linesPrintedOnPage.add(0)
}
fun addNewPageAt(index: Int, back: Color = DEFAULT_PAGE_BACK) {
pages.add(index, BTeXPage(back, pageWidth, pageHeight))
pages.add(index, BTeXPage(back, pageDimensionWidth, pageDimensionHeight))
linesPrintedOnPage.add(index, 0)
}
@@ -109,13 +110,13 @@ class BTeXPage(
interface BTeXTextDrawCall {
val rowStart: Int
val rowEnd: Int
fun draw(batch: SpriteBatch, x: Float, y: Float)
val rows: Int
fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float)
}
data class MovableTypeDrawCall(val movableType: MovableType, override val rowStart: Int, override val rowEnd: Int): BTeXTextDrawCall {
override fun draw(batch: SpriteBatch, x: Float, y: Float) {
movableType.draw(batch, x, y, rowStart, rowEnd)
data class MovableTypeDrawCall(val movableType: MovableType, override val rowStart: Int, override val rows: Int): BTeXTextDrawCall {
override fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float) {
movableType.draw(batch, x, y, rowStart, minOf(rows, doc.pageLines))
}
}
@@ -126,6 +127,7 @@ data class MovableTypeDrawCall(val movableType: MovableType, override val rowSta
}*/
class BTeXDrawCall(
val doc: BTeXDocument,
var posX: Int, // position relative to the page start (excluding page margin)
var posY: Int, // position relative to the page start (excluding page margin)
val theme: String,
@@ -150,7 +152,7 @@ class BTeXDrawCall(
batch.color = colour
if (text != null && texture == null) {
text.draw(batch, px, py)
text.draw(doc, batch, px, py)
}
else if (text == null && texture != null) {
batch.draw(texture, px, py)
@@ -178,7 +180,7 @@ class BTeXDrawCall(
internal var extraDrawFun: (SpriteBatch, Float, Float) -> Unit = { _, _, _ ->}
internal val lineCount = if (text != null)
text.rowEnd - text.rowStart
text.rows
else
TODO()

View File

@@ -67,9 +67,9 @@ object BTeXParser {
private var btexOpened = false
private var pageWidth = doc.textWidth
private var pageLines = doc.pageLines
private var pageHeight = doc.textHeight
// private var pageWidth = doc.textWidth
// private var pageLines = doc.pageLines
// private var pageHeight = doc.textHeight
private val blockLut = HashMap<String, ItemID>()
@@ -489,17 +489,12 @@ object BTeXParser {
handler.papersize = attribs["papersize"] ?: "standard"
//change the "default values" of the document
handler.pageWidth = pageWidthMap[papersize]!!
handler.pageLines = pageHeightMap[papersize]!!
handler.pageHeight = pageLines * LINE_HEIGHT
doc.textWidth = pageWidth
doc.textHeight = pageHeight
doc.textWidth = pageWidthMap[papersize]!!
doc.pageLines = pageHeightMap[papersize]!!
}
handler.btexOpened = true
printdbg("BTeX document: def=${handler.def}, cover=${handler.cover}, inner=${handler.inner}, papersize=${handler.papersize}, dim=${handler.pageWidth}x${handler.pageHeight} (${handler.pageLines} lines)")
}
@OpenTag // reflective access is impossible with 'private'
@@ -516,6 +511,10 @@ object BTeXParser {
fun processElemTITLE(handler: BTeXHandler, doc: BTeXDocument, uri: String, attribs: HashMap<String, String>) {
handler.paragraphBuffer.clear()
}
@OpenTag // reflective access is impossible with 'private'
fun processElemSUBTITLE(handler: BTeXHandler, doc: BTeXDocument, uri: String, attribs: HashMap<String, String>) {
handler.paragraphBuffer.clear()
}
@OpenTag // reflective access is impossible with 'private'
fun processElemAUTHOR(handler: BTeXHandler, doc: BTeXDocument, uri: String, attribs: HashMap<String, String>) {
@@ -738,6 +737,12 @@ object BTeXParser {
handler.paragraphBuffer.clear()
}
@CloseTag // reflective access is impossible with 'private'
fun closeElemSUBTITLE(handler: BTeXHandler, doc: BTeXDocument, uri: String, siblingIndex: Int) {
val thePar = handler.paragraphBuffer.toString().trim()
typesetBookEdition(thePar, handler)
handler.paragraphBuffer.clear()
}
@CloseTag // reflective access is impossible with 'private'
fun closeElemAUTHOR(handler: BTeXHandler, doc: BTeXDocument, uri: String, siblingIndex: Int) {
val thePar = handler.paragraphBuffer.toString().trim()
typesetBookAuthor(thePar, handler)
@@ -746,7 +751,7 @@ object BTeXParser {
@CloseTag // reflective access is impossible with 'private'
fun closeElemEDITION(handler: BTeXHandler, doc: BTeXDocument, uri: String, siblingIndex: Int) {
val thePar = handler.paragraphBuffer.toString().trim()
typesetBookAuthor(thePar, handler)
typesetBookEdition(thePar, handler)
handler.paragraphBuffer.clear()
}
@@ -834,25 +839,33 @@ object BTeXParser {
val label = "\n" + thePar
typesetParagraphs(getTitleFont(), label, handler, (doc.textWidth - 16) / 2).also {
val addedLines = it.sumOf { it.lineCount }
doc.linesPrintedOnPage[doc.currentPage] += addedLines + 2
doc.linesPrintedOnPage[doc.currentPage] += addedLines
it.forEach {
it.posX += 8
}
}
}
private fun typesetBookAuthor(thePar: String, handler: BTeXHandler) {
typesetParagraphs(getSubtitleFont(), "\n\n" + thePar, handler, doc.textWidth - 16).also {
it.last().extraDrawFun = { batch, x, y ->
val px = x
val py = y + doc.lineHeightInPx * (2 * addedLines) + 8 + 11
val py = y + 23
val pw = doc.textWidth - 16f
batch.color = Color(1f,1f,1f,.5f)
Toolkit.fillArea(batch, px, py, pw+1, 2f)
batch.color = Color.WHITE
Toolkit.fillArea(batch, px, py, pw, 1f)
}
it.forEach {
it.posX += 8
}
}
}
private fun typesetBookAuthor(thePar: String, handler: BTeXHandler) {
private fun typesetBookEdition(thePar: String, handler: BTeXHandler) {
typesetParagraphs(getSubtitleFont(), thePar, handler, doc.textWidth - 16).also {
it.forEach {
it.posX += 8
@@ -908,9 +921,10 @@ object BTeXParser {
// printdbg("Page: ${doc.currentPage+1}, Line: ${doc.currentLine}")
if (slugHeight > remainder) {
val subset = linesOut to linesOut + remainder
val subset = linesOut to remainder
val drawCall = BTeXDrawCall(
doc,
0,
doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx,
handler.currentTheme,
@@ -929,9 +943,10 @@ object BTeXParser {
while (slugHeight > 0) {
remainder = minOf(slugHeight, doc.pageLines)
val subset = linesOut to linesOut + remainder
val subset = linesOut to remainder
val drawCall = BTeXDrawCall(
doc,
0,
doc.linesPrintedOnPage[pageNum] * doc.lineHeightInPx,
handler.currentTheme,

View File

@@ -16,7 +16,6 @@ import net.torvald.terrarum.btex.BTeXDocument
import net.torvald.terrarum.ceilToInt
import net.torvald.terrarum.gdxClearAndEnableBlend
import net.torvald.terrarum.inUse
import java.io.FileReader
/**
@@ -26,6 +25,7 @@ class BTeXTest : ApplicationAdapter() {
// val filePath = "btex.xml"
val filePath = "literature/koKR/yisang_nalgae.xml"
// val filePath = "test.xml"
private lateinit var document: BTeXDocument
@@ -53,7 +53,7 @@ class BTeXTest : ApplicationAdapter() {
override fun render() {
gdxClearAndEnableBlend(.063f, .070f, .086f, 1f)
val drawX = (1280 - (pageGap + document.pageWidth*2)) / 2
val drawX = (1280 - (pageGap + document.pageDimensionWidth*2)) / 2
val drawY = 24
batch.inUse {
@@ -63,7 +63,7 @@ class BTeXTest : ApplicationAdapter() {
if (scroll - 1 in document.pageIndices)
document.render(0f, batch, scroll - 1, drawX, drawY)
if (scroll in document.pageIndices)
document.render(0f, batch, scroll, drawX + (6 + document.pageWidth), drawY)
document.render(0f, batch, scroll, drawX + (6 + document.pageDimensionWidth), drawY)
}