From a7ea83af16d5ca47c7ee4f00725274029f69034a Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 27 Apr 2024 17:33:08 +0900 Subject: [PATCH] btex: toc and chapter opening position --- src/net/torvald/btex/BTeXParser.kt | 37 +++++++++++++++++----- src/net/torvald/terrarum/tests/BTeXTest.kt | 4 +-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/net/torvald/btex/BTeXParser.kt b/src/net/torvald/btex/BTeXParser.kt index c1389ce87..3dfcd59da 100644 --- a/src/net/torvald/btex/BTeXParser.kt +++ b/src/net/torvald/btex/BTeXParser.kt @@ -536,6 +536,7 @@ object BTeXParser { @OpenTag // reflective access is impossible with 'private' fun processElemTOCPAGE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap, siblingIndex: Int) { doc.addNewPage() + doc.addNewPage() // toc: openright typesetChapterHeading("Table of Contents", handler, 16) } @@ -574,6 +575,20 @@ object BTeXParser { @CloseTag fun closeElemMANUSCRIPT(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, siblingIndex: Int) { + // setup pages such that: + // toc: openright (done on processElemTOCPAGE) + // first chapter: openright + // second+ chapter: openany + if (cptSectMap.isNotEmpty() && cptSectMap.first().pagenum % 2 == 1) { + doc.addNewPageAt(cptSectMap.first().pagenum) + cptSectMap.forEach { it.pagenum += 1 } + } + else { + doc.addNewPageAt(cptSectMap.first().pagenum) + doc.addNewPageAt(cptSectMap.first().pagenum) + cptSectMap.forEach { it.pagenum += 2 } + } + // if tocPage != null, estimate TOC page size, renumber indexMap and cptSectMap if needed, then typeset the toc if (tocPage != null) { // TODO estimate the number of TOC pages @@ -585,9 +600,8 @@ object BTeXParser { indexMap.keys.forEach { indexMap[it] = indexMap[it]!! + pageDelta } - cptSectMap.forEach { - it.pagenum += pageDelta - } + + cptSectMap.forEach { it.pagenum += pageDelta } // insert new pages repeat(pageDelta) { @@ -730,14 +744,21 @@ object BTeXParser { spanColour = null } + + + private fun typesetBookTitle(thePar: String, handler: BTeXHandler) { val label = "\n" + thePar - typesetParagraphs(getTitleFont(), label, handler, doc.textWidth / 2).also { + typesetParagraphs(getTitleFont(), label, handler, (doc.textWidth - 16) / 2).also { val addedLines = it.sumOf { it.lineCount } doc.linesPrintedOnPage[doc.currentPage] += addedLines + 2 + it.forEach { + it.posX += 8 + } + it.last().extraDrawFun = { batch, x, y -> - val px = x + 8 + val px = x val py = y + doc.lineHeightInPx * (2 * addedLines) + 11 val pw = doc.textWidth - 16f Toolkit.fillArea(batch, px, py, pw, 2f) @@ -872,9 +893,9 @@ object BTeXParser { }*/ else call.width - var dotCursor = (x + textWidth + dotGap/2).div(dotGap).ceilToFloat() * dotGap - while (dotCursor < x + typeWidth - pageNumWidth - dotGap/2) { - font.draw(batch, "·", dotCursor, y) + var dotCursor = (x + textWidth).div(dotGap).ceilToFloat() * dotGap + while (dotCursor < x + typeWidth - pageNumWidth - dotGap*1.5f) { + font.draw(batch, "·", dotCursor + dotGap/2, y) dotCursor += dotGap } diff --git a/src/net/torvald/terrarum/tests/BTeXTest.kt b/src/net/torvald/terrarum/tests/BTeXTest.kt index 6b5a7b60a..51afb2d8d 100644 --- a/src/net/torvald/terrarum/tests/BTeXTest.kt +++ b/src/net/torvald/terrarum/tests/BTeXTest.kt @@ -28,7 +28,7 @@ class BTeXTest : ApplicationAdapter() { val csi0 = "\u001B[m" val tex = """ - + The Way to Mastery of Lorem Ipsum<br />Or, How To Write and Publish a Book Terran Publishing Test Edition @@ -56,7 +56,7 @@ class BTeXTest : ApplicationAdapter() { -

text emph and item on target

+

text emph and 􏃯item􀀀 on target