mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-20 07:24:06 +09:00
btex: fixed the paragraph overflows
This commit is contained in:
69
assets/mods/basegame/books/test.xml
Normal file
69
assets/mods/basegame/books/test.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<btexdoc cover="hardcover" inner="standard" papersize="standard">
|
||||
<cover>
|
||||
<title>The life and strange surprizing adventures of Robinson Crusoe, of York, mariner: who lived eight and twenty years all alone in an un-inhabited island on the coast of America, near the mouth of the great river of Oroonoque; having been cast on shore by shipwreck, wherein all the men perished but himself, with an account how he was at last as strangely deliver'd by pyrates</title>
|
||||
<author>Daniel Defoe</author>
|
||||
<edition>The Fourth Edition</edition>
|
||||
</cover>
|
||||
|
||||
<tocpage><tableofcontents /></tocpage>
|
||||
|
||||
<manuscript>
|
||||
|
||||
|
||||
|
||||
<chapter>What Is a Book</chapter>
|
||||
|
||||
<p>This example book is designed to give you the example of the Book Language.</p>
|
||||
|
||||
<section>What Really Is a Book</section>
|
||||
|
||||
<p>A book is a collection of texts printed in a special way that allows them to be read easily, with
|
||||
enumerable pages and insertion of other helpful resources, such as illustrations and <a href="btex language">hyperlinks</a>.</p>
|
||||
|
||||
<newpage />
|
||||
|
||||
<!--<fullpagebox>
|
||||
<p><span colour="grey">
|
||||
this page is intentionally left blank
|
||||
</span></p>
|
||||
</fullpagebox>-->
|
||||
|
||||
|
||||
|
||||
|
||||
<chapter>Writing a Book Using Pen and Papers</chapter>
|
||||
|
||||
<p><index id="pen and paper" />If you open a book on a writing table, you will be welcomed with a
|
||||
toolbar used to put other book elements, such as chapters and sections.</p>
|
||||
|
||||
|
||||
|
||||
|
||||
<chapter>Writing a Book Using a Typewriter</chapter>
|
||||
|
||||
<p><index id="typewriter" />Typewriters can only write in a single style of font, chapters and
|
||||
sections are not available.</p>
|
||||
|
||||
|
||||
|
||||
|
||||
<chapter>Writing a Book Using a Computer</chapter>
|
||||
|
||||
<p>Writing book using a computer requires the use of the Book Typesetting Engine Extended, or <btex />.</p>
|
||||
|
||||
<section>Full Control of the Shape</section>
|
||||
|
||||
<p><index id="btex language" />With <btex /> you can fully control how your publishing would look like,
|
||||
from a pile of papers that look like they have been typed out using typewriter, a pile of printouts
|
||||
that have pictures in it, to a true hardcover book.</p>
|
||||
|
||||
<p><index id="cover" />This style is controlled using the <code>cover</code> attribute on the root tag,
|
||||
with following values: <code>typewriter</code>, <code>printout</code> and <code>hardcover</code>.</p>
|
||||
|
||||
<p>Typewriter and Printout are considered not-bound and readers will only see one page at a time,
|
||||
while Hardcover is considered bound and two pages are presented to the readers.</p>
|
||||
|
||||
</manuscript>
|
||||
|
||||
<indexpage><tableofindices /></indexpage>
|
||||
</btexdoc>
|
||||
BIN
lib/TerrarumSansBitmap.jar
LFS
BIN
lib/TerrarumSansBitmap.jar
LFS
Binary file not shown.
@@ -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()
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user