From 7384d970d520e336f07ad173848cd64a9417d300 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 30 Apr 2024 04:59:08 +0900 Subject: [PATCH] better-behaving hyphenation penalty --- .../torvald/terrarumsansbitmap/MovableType.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/net/torvald/terrarumsansbitmap/MovableType.kt b/src/net/torvald/terrarumsansbitmap/MovableType.kt index 6b4f3f3..d380891 100644 --- a/src/net/torvald/terrarumsansbitmap/MovableType.kt +++ b/src/net/torvald/terrarumsansbitmap/MovableType.kt @@ -7,6 +7,7 @@ import net.torvald.terrarumsansbitmap.gdx.CodePoint import net.torvald.terrarumsansbitmap.gdx.CodepointSequence import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap.Companion.getHash +import java.lang.Math.log import java.lang.Math.pow import kotlin.math.* @@ -75,7 +76,7 @@ class MovableType( /////////////////////////////////////////////////////////////////////////////////////////////// // the slug is likely end with a glue, must take care of it (but don't modify the slug itself) - fun getBadnessW(box: NoTexGlyphLayout): Triple { + fun getBadnessW(box: NoTexGlyphLayout): Triple { val slug = slug.toMutableList() // remove the trailing glue(s?) in the slug copy @@ -95,7 +96,7 @@ class MovableType( return Triple(badness, difference, null) } - fun getBadnessT(box: NoTexGlyphLayout): Triple { + fun getBadnessT(box: NoTexGlyphLayout): Triple { val slug = slug.toMutableList() // add the box to the slug copy @@ -114,12 +115,12 @@ class MovableType( return Triple(badness, difference, null) } - fun getBadnessH(box: NoTexGlyphLayout, diff: Int): Triple { + fun getBadnessH(box: NoTexGlyphLayout, diff: Int): Triple { // don't hyphenate if: // - the word is too short (5 chars or less) // - the word is pre-hyphenated (ends with hyphen-null) if (box.text.size <= 8 || box.text.penultimate() == 0x2D) - return Triple(2147483647, 2147483647, null) + return Triple(2147483648.0, 2147483647, null) val slug = slug.toMutableList() val (hyphHead, hyphTail) = box.text.hyphenate(font, diff).toList().map { createGlyphLayout(font, it) } @@ -135,7 +136,7 @@ class MovableType( slugWidth -= hangWidthFW val difference = (paperWidth - slugWidth) - val badness = penaliseHyphenation(difference.absoluteValue) + val badness = penaliseHyphenation(difference.absoluteValue, paperWidth) return Triple(badness, difference, hyphHead to hyphTail) } @@ -678,9 +679,9 @@ class MovableType( return this[this.size - 2] } - private fun penaliseWidening(score: Int): Int = score*score - private fun penaliseTightening(score: Int): Int = score*score//0.0006f * score * score * score + 0.18f * score - private fun penaliseHyphenation(score: Int): Int = score*score//(10.0 * pow(score.toDouble(), 1.0/3.0) + 0.47*score).toFloat() + private fun penaliseWidening(score: Int): Double = pow(score.toDouble(), 2.0) + private fun penaliseTightening(score: Int): Double = pow(score.toDouble(), 2.0)//0.0006f * score * score * score + 0.18f * score + private fun penaliseHyphenation(score: Int, paperWidth: Int): Double = pow(score.toDouble().absoluteValue, log2(paperWidth.toDouble()) / 3.0)//(10.0 * pow(score.toDouble(), 1.0/3.0) + 0.47*score).toFloat() private fun isVowel(c: CodePoint) = vowels.contains(c)