BoxFitter: avoid clipping right-hand edge

This commit is contained in:
Paul Campbell 2020-05-22 07:54:31 +01:00
parent 7476722062
commit 935e2b966a
2 changed files with 17 additions and 12 deletions

View file

@ -53,11 +53,15 @@ class BoxFitterImpl implements BoxFitter {
return mid; return mid;
} }
Font font = e.getFont(mid); Font font = e.getFont(mid);
List<String> lines = wrapLines(font, e); try {
List<Rectangle2D> lineSizes = List<String> lines = wrapLines(font, e);
lineSizes(font, lines, e.fontRenderContext()); List<Rectangle2D> lineSizes =
if (sumLineHeights(lineSizes) > e.boxHeight() || lineSizes(font, lines, e.fontRenderContext());
maxLineWidth(lineSizes) > e.boxWidth()) { if (sumLineHeights(lineSizes) > e.boxHeight() ||
maxLineWidth(lineSizes) > e.boxWidth()) {
return fitMinMax(min, mid, e);
}
} catch (WordTooLong err) {
return fitMinMax(min, mid, e); return fitMinMax(min, mid, e);
} }
return fitMinMax(mid, max, e); return fitMinMax(mid, max, e);

View file

@ -48,9 +48,9 @@ public class BoxFitterTest
Map<String, Integer> wordMap = Map.of( Map<String, Integer> wordMap = Map.of(
".", 263, ".", 263,
"a", 263, "a", 263,
"Word", 121, "Word", 110,
"longer", 104, "longer", 96,
"extralongword", 44 "extralongword", 43
); );
wordMap.forEach((word, expectedSize) -> wordMap.forEach((word, expectedSize) ->
assertThat(fit(word)) assertThat(fit(word))
@ -64,8 +64,8 @@ public class BoxFitterTest
Map<String, Integer> wordMap = Map.of( Map<String, Integer> wordMap = Map.of(
". .", 263, ". .", 263,
"a a", 208, "a a", 208,
"Another Word", 81, "Another Word", 76,
longStringGenerator(1), 100, longStringGenerator(1), 93,
longStringGenerator(2), 36, longStringGenerator(2), 36,
longStringGenerator(3), 27, longStringGenerator(3), 27,
longStringGenerator(4), 22, longStringGenerator(4), 22,
@ -118,8 +118,9 @@ public class BoxFitterTest
@DisplayName("Text too long to fit single box - fits into two") @DisplayName("Text too long to fit single box - fits into two")
public void tooLongThrows() { public void tooLongThrows() {
String longText = longStringGenerator(197); String longText = longStringGenerator(197);
int result = fit(longText); //TODO: should overflow into second box
assertThat(result).isGreaterThan(3); assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> fit(longText));
} }
} }