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,6 +53,7 @@ class BoxFitterImpl implements BoxFitter {
return mid;
}
Font font = e.getFont(mid);
try {
List<String> lines = wrapLines(font, e);
List<Rectangle2D> lineSizes =
lineSizes(font, lines, e.fontRenderContext());
@ -60,6 +61,9 @@ class BoxFitterImpl implements BoxFitter {
maxLineWidth(lineSizes) > e.boxWidth()) {
return fitMinMax(min, mid, e);
}
} catch (WordTooLong err) {
return fitMinMax(min, mid, e);
}
return fitMinMax(mid, max, e);
}

View file

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