code tag wip

This commit is contained in:
minjaesong
2024-05-03 14:19:56 +09:00
parent e51504f881
commit 6494d7c29b
3 changed files with 53 additions and 1 deletions

Binary file not shown.

View File

@@ -300,7 +300,14 @@ class BTeXPage(
if (drawCall.isNotBlank()) drawCalls.add(drawCall)
}
private var prerender = false
fun render(frameDelta: Float, batch: SpriteBatch, x: Int, y: Int, marginH: Int, marginV: Int) {
if (!prerender) {
prerender = true
drawCalls.sortBy { if (it.text != null) 16 else 0 }
}
batch.color = back.cpy().also { it.a = 0.93f }
Toolkit.fillArea(batch, x, y, width, height)
batch.color = Color.WHITE

View File

@@ -79,6 +79,7 @@ object BTeXParser {
private var currentTheme = ""
private var spanColour: String? = null
private var codeMode: Boolean = false
private val elemOpeners: HashMap<String, KFunction<*>> = HashMap()
@@ -175,6 +176,26 @@ object BTeXParser {
}
}
private fun getOrPutCodeTagRef(width: Int): ((BTeXDrawCall) -> BTeXBatchDrawCall)? {
val tagname = "TAG@CODE-$width"
if (!objDict.contains(tagname)) {
objWidthDict[tagname] = 0
objDict[tagname] = { text: BTeXDrawCall ->
object : BTeXBatchDrawCall(0, 0, text) {
override fun draw(doc: BTeXDocument, batch: SpriteBatch, x: Float, y: Float, font: TerrarumSansBitmap?) {
val oldcol = batch.color.cpy()
batch.color = Color(0xccccccff.toInt())
Toolkit.fillArea(batch, x - 2, y - 1, width + 4f, doc.lineHeightInPx + 2f)
batch.color = Color(0x999999ff.toInt())
Toolkit.drawBoxBorder(batch, x - 2, y - 1, width + 4f, doc.lineHeightInPx + 2f)
batch.color = oldcol
}
}
}
}
return objDict[tagname]
}
fun dispose() {
if (::testFont.isInitialized) testFont.tryDispose()
if (::titleFont.isInitialized) titleFont.tryDispose()
@@ -262,12 +283,14 @@ object BTeXParser {
}
private var oldSpanColour: String? = null
private var oldCodeMode = false
override fun characters(ch: CharArray, start: Int, length: Int) {
val str =
String(ch.sliceArray(start until start + length)).replace('\n', ' ').replace(Regex(" +"), " ")//.trim()
if (str.isNotEmpty()) {
// process span request
if (spanColour != oldSpanColour || spanColour != null) {
// printdbg("Characters [col:${spanColour}] \t\"$str\"")
@@ -283,12 +306,22 @@ object BTeXParser {
spanGdxCol.b.times(15f).roundToInt()
))
}
}
// process code request
if (codeMode != oldCodeMode && codeMode) {
println("CODE tag for str '$str'")
val w = getFont().getWidth(str)
getOrPutCodeTagRef(w)
paragraphBuffer.appendObjectPlaceholder("TAG@CODE-$w")
}
paragraphBuffer.append(str)
oldSpanColour = spanColour
oldCodeMode = codeMode
}
}
@@ -636,6 +669,15 @@ object BTeXParser {
handler.paragraphBuffer.appendObjectPlaceholder("TAG@TEX")
}
@OpenTag // reflective access is impossible with 'private'
fun processElemCODE(handler: BTeXHandler, doc: BTeXDocument, uri: String, attribs: HashMap<String, String>) {
handler.codeMode = true
}
@CloseTag
fun closeElemCODE(handler: BTeXHandler, doc: BTeXDocument, uri: String, siblingIndex: Int) {
handler.codeMode = false
}
@OpenTag // reflective access is impossible with 'private'
fun processElemCOVER(handler: BTeXHandler, doc: BTeXDocument, uri: String, attribs: HashMap<String, String>) {