From 40be60865b8e4c58ed73f186ffa27c2c4be32c13 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 25 Apr 2024 17:04:15 +0900 Subject: [PATCH] more btex tests --- src/net/torvald/btex/BTeXDocument.kt | 24 +++++++---- src/net/torvald/btex/BTeXParser.kt | 47 +++++++++++++++++----- src/net/torvald/terrarum/tests/BTeXTest.kt | 38 ++++++++++++++++- 3 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src/net/torvald/btex/BTeXDocument.kt b/src/net/torvald/btex/BTeXDocument.kt index 06e0cbf7d..0d66cdc64 100644 --- a/src/net/torvald/btex/BTeXDocument.kt +++ b/src/net/torvald/btex/BTeXDocument.kt @@ -15,10 +15,18 @@ class BTeXDocument { var inner = "standard" var papersize = "standard" - var pageWidth = 420 + var textWidth = 450 var lineHeightInPx = 24 - var pageLines = 25 - var pageHeight = pageLines * lineHeightInPx + var pageLines = 24 + var textHeight = pageLines * lineHeightInPx + + val pageMarginH = 15 + val pageMarginV = 12 + + val pageWidth: Int + get() = 2 * pageMarginH + textWidth + val pageHeight: Int + get() = 2 * pageMarginV + textHeight companion object { val DEFAULT_PAGE_BACK = Color(0xe1e1d7ff.toInt()) @@ -30,8 +38,10 @@ class BTeXDocument { val currentPage: Int get() = pages.size - 1 + val pageIndices: IntRange + get() = pages.indices + var currentLine: Int = 0 - private set fun addNewPage(back: Color = DEFAULT_PAGE_BACK) { pages.add(BTeXPage(back, pageWidth, pageHeight)) @@ -50,7 +60,7 @@ class BTeXDocument { } fun render(frameDelta: Float, batch: SpriteBatch, page: Int, x: Int, y: Int) { - pages[page].render(frameDelta, batch, x, y) + pages[page].render(frameDelta, batch, x, y, pageMarginH, pageMarginV) } } @@ -65,11 +75,11 @@ class BTeXPage( drawCalls.add(drawCall) } - fun render(frameDelta: Float, batch: SpriteBatch, x: Int, y: Int) { + fun render(frameDelta: Float, batch: SpriteBatch, x: Int, y: Int, marginH: Int, marginV: Int) { batch.color = back Toolkit.fillArea(batch, x, y, width, height) drawCalls.forEach { - it.draw(batch, x, y) + it.draw(batch, x + marginH, y + marginV) } } } diff --git a/src/net/torvald/btex/BTeXParser.kt b/src/net/torvald/btex/BTeXParser.kt index 681431a15..dc05aa524 100644 --- a/src/net/torvald/btex/BTeXParser.kt +++ b/src/net/torvald/btex/BTeXParser.kt @@ -55,9 +55,9 @@ object BTeXParser { private var btexOpened = false - private var pageWidth = doc.pageWidth + private var pageWidth = doc.textWidth private var pageLines = doc.pageLines - private var pageHeight = doc.pageHeight + private var pageHeight = doc.textHeight private val blockLut = HashMap() @@ -396,10 +396,10 @@ object BTeXParser { ) private val pageWidthMap = hashMapOf( - "standard" to 420 + "standard" to 450 ) private val pageHeightMap = hashMapOf( - "standard" to 25 + "standard" to 24 ) @@ -421,8 +421,8 @@ object BTeXParser { handler.pageLines = pageHeightMap[papersize]!! handler.pageHeight = pageLines * LINE_HEIGHT - doc.pageWidth = pageWidth - doc.pageHeight = pageHeight + doc.textWidth = pageWidth + doc.textHeight = pageHeight } handler.btexOpened = true @@ -460,6 +460,7 @@ object BTeXParser { @OpenTag // reflective access is impossible with 'private' fun processElemCOVER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap) { doc.addNewPage(Color(0x6f4a45ff)) + handler.spanColour = "white" } @OpenTag // reflective access is impossible with 'private' @@ -478,38 +479,62 @@ object BTeXParser { - @OpenTag // reflective access is impossible with 'private' + @OpenTag // reflective access is impossible with 'private' fun processElemBR(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap) { handler.paragraphBuffer.append("\n") } @OpenTag // reflective access is impossible with 'private' fun processElemNEWPAGE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap) { + doc.addNewPage() + } + @CloseTag // reflective access is impossible with 'private' + fun closeElemFULLPAGEBOX(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) { + doc.addNewPage() } @OpenTag // reflective access is impossible with 'private' fun processElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap) { } + @CloseTag + fun closeElemCOVER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) { + handler.spanColour = null + } + + @CloseTag // reflective access is impossible with 'private' + fun closeElemTITLE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri) + @CloseTag // reflective access is impossible with 'private' + fun closeElemAUTHOR(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri) + @CloseTag // reflective access is impossible with 'private' + fun closeElemEDITION(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri) + @CloseTag // reflective access is impossible with 'private' + fun closeElemCHAPTER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri) + @CloseTag // reflective access is impossible with 'private' + fun closeElemSECTION(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri) + @CloseTag // reflective access is impossible with 'private' fun closeElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) { - printdbg("Par: ${handler.paragraphBuffer}") + val thePar = handler.paragraphBuffer.toString().trim() + "\n" + printdbg("Par: '$thePar'") val font = getFont() - val slugs = MovableType(font, handler.paragraphBuffer.toString(), doc.pageWidth) + val slugs = MovableType(font, thePar, doc.textWidth) var remainder = doc.pageLines - doc.currentLine var slugHeight = slugs.height var linesOut = 0 + printdbg("Page: ${doc.currentPage+1}, Line: ${doc.currentLine}") + if (slugHeight > remainder) { val subset = linesOut to linesOut + remainder val drawCall = BTeXDrawCall( 0, - remainder * doc.pageLines, + doc.currentLine * doc.lineHeightInPx, handler.currentTheme, handler.getSpanColour(), MovableTypeDrawCall(slugs, subset.first, subset.second) @@ -530,7 +555,7 @@ object BTeXParser { val drawCall = BTeXDrawCall( 0, - 0, + doc.currentLine * doc.lineHeightInPx, handler.currentTheme, handler.getSpanColour(), MovableTypeDrawCall(slugs, subset.first, subset.second) diff --git a/src/net/torvald/terrarum/tests/BTeXTest.kt b/src/net/torvald/terrarum/tests/BTeXTest.kt index a96211214..31a30cf9c 100644 --- a/src/net/torvald/terrarum/tests/BTeXTest.kt +++ b/src/net/torvald/terrarum/tests/BTeXTest.kt @@ -1,11 +1,18 @@ package net.torvald.terrarum.tests import com.badlogic.gdx.ApplicationAdapter +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.Input import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration +import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.glutils.ShaderProgram import net.torvald.btex.BTeXParser +import net.torvald.terrarum.FlippingSpriteBatch import net.torvald.terrarum.btex.BTeXDocument +import net.torvald.terrarum.ceilToInt +import net.torvald.terrarum.gdxClearAndEnableBlend +import net.torvald.terrarum.inUse /** @@ -41,11 +48,11 @@ class BTeXTest : ApplicationAdapter() { - + @@ -90,11 +97,38 @@ class BTeXTest : ApplicationAdapter() { """ private lateinit var document: BTeXDocument + private lateinit var batch: FlippingSpriteBatch + private lateinit var camera: OrthographicCamera override fun create() { + batch = FlippingSpriteBatch(1000) + camera = OrthographicCamera(1280f, 720f) + camera.setToOrtho(true) // some elements are pre-flipped, while some are not. The statement itself is absolutely necessary to make edge of the screen as the origin + camera.update() + batch.projectionMatrix = camera.combined + document = BTeXParser.invoke(tex) } + private var scroll = 0 + + + override fun render() { + gdxClearAndEnableBlend(.063f, .070f, .086f, 1f) + + batch.inUse { + if (scroll - 1 in document.pageIndices) + document.render(0f, batch, scroll - 1, 12, 12) + if (scroll in document.pageIndices) + document.render(0f, batch, scroll, 12 + (6 + document.pageWidth), 12) + } + + + if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) + scroll = (scroll - 2).coerceAtLeast(0) + else if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) + scroll = (scroll + 2).coerceAtMost(document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2)) + } }