From e2d6d3724ce5a0020d8bb4c37417ce14d10e61db Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 9 May 2024 15:59:30 +0900 Subject: [PATCH] fix: bad width calculation on raggedright --- .../torvald/terrarumsansbitmap/MovableType.kt | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/net/torvald/terrarumsansbitmap/MovableType.kt b/src/net/torvald/terrarumsansbitmap/MovableType.kt index 43b2623..b078f8f 100644 --- a/src/net/torvald/terrarumsansbitmap/MovableType.kt +++ b/src/net/torvald/terrarumsansbitmap/MovableType.kt @@ -179,17 +179,26 @@ class MovableType( if (box.isNotGlue()) { // deal with the hangables - val slugWidthForOverflowCalc = if (box.penultimateCharOrNull == null) - slugWidth - else if (hangable.contains(box.penultimateCharOrNull)) - slugWidth - hangWidth - else if (hangableFW.contains(box.penultimateCharOrNull)) - slugWidth - hangWidthFW - else - slugWidth + val slugWidthForOverflowCalc = when (strategy) { + TypesettingStrategy.JUSTIFIED -> if (box.penultimateCharOrNull == null) + slugWidth + else if (hangable.contains(box.penultimateCharOrNull)) + slugWidth - hangWidth + else if (hangableFW.contains(box.penultimateCharOrNull)) + slugWidth - hangWidthFW + else + slugWidth + + TypesettingStrategy.RAGGED_RIGHT -> slugWidth + } + + val truePaperWidth = when (strategy) { + TypesettingStrategy.JUSTIFIED -> paperWidth + TypesettingStrategy.RAGGED_RIGHT -> paperWidth + 2 + } // if adding the box would cause overflow - if (slugWidthForOverflowCalc + box.width > paperWidth) { + if (slugWidthForOverflowCalc + box.width > truePaperWidth) { // if adding the box would cause overflow (justified) if (strategy == TypesettingStrategy.JUSTIFIED) { // text overflow occured; set the width to the max value