Compare commits
19 commits
Author | SHA1 | Date | |
---|---|---|---|
57d3760bfd | |||
e9b3ba39e1 | |||
57625a1ea9 | |||
fd9a0a2199 | |||
2d58712047 | |||
935e2b966a | |||
7476722062 | |||
c82151c3e3 | |||
3cdfa2fc0e | |||
ac9c497937 | |||
40b96a7662 | |||
9f9797dcbd | |||
b215021eee | |||
4af761ae33 | |||
fd53930980 | |||
6c645a5264 | |||
e3547f26d3 | |||
ccae310017 | |||
0af994b707 |
12 changed files with 712 additions and 135 deletions
4
pom.xml
4
pom.xml
|
@ -14,6 +14,10 @@
|
|||
<version>DEV-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
|
||||
<tiles-maven-plugin.version>2.16</tiles-maven-plugin.version>
|
||||
<kemitix-maven-tiles.version>2.6.0</kemitix-maven-tiles.version>
|
||||
<kemitix-checkstyle.version>5.4.0</kemitix-checkstyle.version>
|
||||
|
|
|
@ -2,6 +2,7 @@ package net.kemitix.text.fit;
|
|||
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface BoxFitter {
|
||||
|
@ -11,4 +12,10 @@ public interface BoxFitter {
|
|||
Graphics2D graphics2D,
|
||||
Rectangle2D box
|
||||
);
|
||||
int fit(
|
||||
String text,
|
||||
Function<Integer, Font> fontFactory,
|
||||
Graphics2D graphics2D,
|
||||
List<Rectangle2D> box
|
||||
);
|
||||
}
|
||||
|
|
|
@ -5,9 +5,12 @@ import lombok.RequiredArgsConstructor;
|
|||
import java.awt.*;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Fit the text to a box by finding the best font size and word wrapping to fill
|
||||
|
@ -16,6 +19,7 @@ import java.util.stream.Collectors;
|
|||
@RequiredArgsConstructor
|
||||
class BoxFitterImpl implements BoxFitter {
|
||||
|
||||
public static final int MAX_FONT_SIZE = 10_000;
|
||||
private final WordWrapper wordWrapper;
|
||||
|
||||
@Override
|
||||
|
@ -25,10 +29,22 @@ class BoxFitterImpl implements BoxFitter {
|
|||
Graphics2D graphics2D,
|
||||
Rectangle2D box
|
||||
) {
|
||||
int fit = fitMinMax(0, (int) box.getHeight(),
|
||||
new FitEnvironment(text, fontFactory, graphics2D, box));
|
||||
return fit(text, fontFactory, graphics2D,
|
||||
Collections.singletonList(box));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fit(
|
||||
String text,
|
||||
Function<Integer, Font> fontFactory,
|
||||
Graphics2D graphics2D,
|
||||
List<Rectangle2D> boxes
|
||||
) {
|
||||
int fit = fitMinMax(0, MAX_FONT_SIZE,
|
||||
new FitEnvironment(text, fontFactory, graphics2D,
|
||||
boxes));
|
||||
if (fit <= 2) {
|
||||
throw new IllegalArgumentException("The text is too long to fit");
|
||||
throw new NotEnoughSpace(0);
|
||||
}
|
||||
return fit;
|
||||
}
|
||||
|
@ -39,25 +55,39 @@ class BoxFitterImpl implements BoxFitter {
|
|||
FitEnvironment e
|
||||
) {
|
||||
int mid = (max + min) / 2;
|
||||
if (mid == min){
|
||||
if (mid == min && mid + 1 == max) {
|
||||
return mid;
|
||||
}
|
||||
Font font = e.getFont(mid);
|
||||
List<String> lines = wrapLines(font, e);
|
||||
List<Rectangle2D> lineSizes =
|
||||
lineSizes(font, lines, e.fontRenderContext());
|
||||
if (sumLineHeights(lineSizes) > e.boxHeight() ||
|
||||
maxLineWidth(lineSizes) > e.boxWidth()) {
|
||||
try {
|
||||
List<List<Rectangle2D>> linesPerBox = wrapLines(font, e);
|
||||
if (tooManyLines(linesPerBox, e.boxes)) {
|
||||
return fitMinMax(min, mid, e);
|
||||
}
|
||||
} catch (WordTooLong | NotEnoughSpace err) {
|
||||
return fitMinMax(min, mid, e);
|
||||
}
|
||||
return fitMinMax(mid, max, e);
|
||||
}
|
||||
|
||||
private List<String> wrapLines(
|
||||
private boolean tooManyLines(
|
||||
List<List<Rectangle2D>> linesPerBox,
|
||||
List<Rectangle2D> boxes
|
||||
) {
|
||||
return StreamZipper.zip(linesPerBox, boxes,
|
||||
(stringBounds, box) ->
|
||||
sumLineHeights(stringBounds) > box.getHeight()
|
||||
).anyMatch(b -> b);
|
||||
}
|
||||
|
||||
private List<List<Rectangle2D>> wrapLines(
|
||||
Font font,
|
||||
FitEnvironment e
|
||||
) {
|
||||
return wordWrapper.wrap(e.text, font, e.graphics2D, e.boxWidth());
|
||||
return wordWrapper.wrap(e.text, font, e.graphics2D, e.boxes)
|
||||
.stream()
|
||||
.map(l -> lineSizes(font, l, e.fontRenderContext()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Rectangle2D> lineSizes(
|
||||
|
@ -71,13 +101,10 @@ class BoxFitterImpl implements BoxFitter {
|
|||
}
|
||||
|
||||
private int sumLineHeights(List<Rectangle2D> lineSizes) {
|
||||
return lineSizes.stream().map(Rectangle2D::getHeight)
|
||||
.mapToInt(Double::intValue).sum();
|
||||
}
|
||||
|
||||
private int maxLineWidth(List<Rectangle2D> lineSizes) {
|
||||
return lineSizes.stream().map(Rectangle2D::getWidth)
|
||||
.mapToInt(Double::intValue).max().orElse(0);
|
||||
return lineSizes.stream()
|
||||
.map(Rectangle2D::getHeight)
|
||||
.mapToInt(Double::intValue)
|
||||
.sum();
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
@ -85,18 +112,11 @@ class BoxFitterImpl implements BoxFitter {
|
|||
private final String text;
|
||||
private final Function<Integer, Font> fontFactory;
|
||||
private final Graphics2D graphics2D;
|
||||
private final Rectangle2D box;
|
||||
private final List<Rectangle2D> boxes;
|
||||
|
||||
public Font getFont(int size) {
|
||||
return fontFactory.apply(size);
|
||||
}
|
||||
public int boxWidth() {
|
||||
return (int) box.getWidth();
|
||||
}
|
||||
|
||||
public int boxHeight() {
|
||||
return (int) box.getHeight();
|
||||
}
|
||||
|
||||
public FontRenderContext fontRenderContext() {
|
||||
return graphics2D.getFontRenderContext();
|
||||
|
|
10
src/main/java/net/kemitix/text/fit/NotEnoughSpace.java
Normal file
10
src/main/java/net/kemitix/text/fit/NotEnoughSpace.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class NotEnoughSpace extends RuntimeException {
|
||||
private final int excessWordCount;
|
||||
}
|
60
src/main/java/net/kemitix/text/fit/StreamZipper.java
Normal file
60
src/main/java/net/kemitix/text/fit/StreamZipper.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.lang.Math.min;
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
/**
|
||||
* Utility to zip two {@link Stream}s together.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public final class StreamZipper {
|
||||
|
||||
private StreamZipper() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Zip two {@link Stream}s together.
|
||||
*
|
||||
* <p>The resulting stream will contain only as many items as the shortest of the two lists.</p>
|
||||
*
|
||||
* @param a the first List
|
||||
* @param b the second List
|
||||
* @param zipper the function to zip an item from each list
|
||||
* @param <A> the type of the first list
|
||||
* @param <B> the type of the second list
|
||||
* @param <C> the type of the joined items
|
||||
* @return a Stream of the joined items
|
||||
*/
|
||||
public static <A, B, C> Stream<C> zip(
|
||||
final List<A> a,
|
||||
final List<B> b,
|
||||
final BiFunction<A, B, C> zipper
|
||||
) {
|
||||
return range(0, limit(a, b))
|
||||
.mapToObj(tuple(a, b, zipper));
|
||||
}
|
||||
|
||||
private static <A, B> int limit(
|
||||
final List<A> a,
|
||||
final List<B> b
|
||||
) {
|
||||
return min(a.size(), b.size());
|
||||
}
|
||||
|
||||
private static <A, B, C> IntFunction<C> tuple(
|
||||
final List<A> a,
|
||||
final List<B> b,
|
||||
final BiFunction<A, B, C> zipper
|
||||
) {
|
||||
return i -> zipper.apply(a.get(i), b.get(i));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -17,30 +18,77 @@ class TextLineWrapImpl implements WordWrapper {
|
|||
Graphics2D graphics2D,
|
||||
int width
|
||||
) {
|
||||
String source = String.join(" ", text.split("\n"));
|
||||
List<Word> words = wordLengths(source.split(" "), font, graphics2D);
|
||||
return wrapWords(words, width);
|
||||
return wrap(text, font, graphics2D,
|
||||
Collections.singletonList(
|
||||
new Rectangle(width, Integer.MAX_VALUE)))
|
||||
.get(0);
|
||||
}
|
||||
|
||||
private List<String> wrapWords(List<Word> words, int width) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
int end = 0;
|
||||
List<String> line = new ArrayList<>();
|
||||
for (Word word : words) {
|
||||
if ((end + word.width) > width) {
|
||||
lines.add(String.join(" ", line));
|
||||
line.clear();
|
||||
end = 0;
|
||||
}
|
||||
line.add(word.word);
|
||||
end += word.width;
|
||||
@Override
|
||||
public List<List<String>> wrap(
|
||||
String text,
|
||||
Font font,
|
||||
Graphics2D graphics2D,
|
||||
List<Rectangle2D> boxes
|
||||
) {
|
||||
String source = String.join(" ", text.split("\n"));
|
||||
List<Word> words = wordLengths(source.split(" "), font, graphics2D);
|
||||
return wrapWords(words, boxes);
|
||||
}
|
||||
|
||||
private List<List<String>> wrapWords(
|
||||
List<Word> words,
|
||||
List<Rectangle2D> boxes
|
||||
) {
|
||||
Deque<Word> wordQ = new ArrayDeque<>(words);
|
||||
List<List<String>> wrappings = boxes.stream()
|
||||
.map(rectangle2D -> {
|
||||
double width = rectangle2D.getWidth();
|
||||
double height = rectangle2D.getHeight();
|
||||
List<String> lines = new ArrayList<>();
|
||||
int bottom = 0;
|
||||
int end = 0;
|
||||
Deque<Word> lineQ = new ArrayDeque<>();
|
||||
while (!wordQ.isEmpty()) {
|
||||
Word word = wordQ.pop();
|
||||
if ((bottom + word.height) > height) {
|
||||
wordQ.add(word);
|
||||
lineQ.forEach(wordQ::push);
|
||||
return removeBlankLines(lines);
|
||||
}
|
||||
if (end == 0 && word.width > width) {
|
||||
throw new WordTooLong(word.word);
|
||||
}
|
||||
if ((end + word.width) > width) {
|
||||
lines.add(wordsAsString((Deque<Word>) lineQ));
|
||||
lineQ.clear();
|
||||
end = 0;
|
||||
bottom += word.height;
|
||||
}
|
||||
lineQ.add(word);
|
||||
end += word.width;
|
||||
}
|
||||
lines.add(wordsAsString(lineQ));
|
||||
return removeBlankLines(lines);
|
||||
}).collect(Collectors.toList());
|
||||
if (wordQ.isEmpty()) {
|
||||
return wrappings;
|
||||
}
|
||||
lines.add(String.join(" ", line));
|
||||
throw new NotEnoughSpace(wordQ.size());
|
||||
}
|
||||
|
||||
private List<String> removeBlankLines(List<String> lines) {
|
||||
return lines.stream()
|
||||
.filter(l -> l.length() > 0)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String wordsAsString(Deque<Word> lineQ) {
|
||||
return lineQ.stream()
|
||||
.map(Word::getWord)
|
||||
.collect(Collectors.joining(" "));
|
||||
}
|
||||
|
||||
private List<Word> wordLengths(String[] words, Font font, Graphics2D graphics2D) {
|
||||
FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
|
||||
return Arrays.stream(words)
|
||||
|
@ -49,13 +97,16 @@ class TextLineWrapImpl implements WordWrapper {
|
|||
}
|
||||
|
||||
private static class Word {
|
||||
@Getter
|
||||
private final String word;
|
||||
private final int width;
|
||||
private final int height;
|
||||
|
||||
public Word(String word, Font font, FontRenderContext fontRenderContext) {
|
||||
this.word = word;
|
||||
Rectangle2D stringBounds = font.getStringBounds(word + " ", fontRenderContext);
|
||||
this.width = Double.valueOf(stringBounds.getWidth()).intValue();
|
||||
this.height = Double.valueOf(stringBounds.getHeight()).intValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
24
src/main/java/net/kemitix/text/fit/Tuple.java
Normal file
24
src/main/java/net/kemitix/text/fit/Tuple.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
public class Tuple<A, B> {
|
||||
|
||||
private final A partA;
|
||||
private final B partB;
|
||||
|
||||
private Tuple(final A partA, final B partB) {
|
||||
this.partA = partA;
|
||||
this.partB = partB;
|
||||
}
|
||||
|
||||
public static <A, B> Tuple<A, B> of(final A a, final B b) {
|
||||
return new Tuple<>(a, b);
|
||||
}
|
||||
|
||||
public A get1() {
|
||||
return partA;
|
||||
}
|
||||
|
||||
public B get2() {
|
||||
return partB;
|
||||
}
|
||||
}
|
10
src/main/java/net/kemitix/text/fit/WordTooLong.java
Normal file
10
src/main/java/net/kemitix/text/fit/WordTooLong.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class WordTooLong extends RuntimeException {
|
||||
private final String longWord;
|
||||
}
|
|
@ -1,13 +1,49 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.List;
|
||||
|
||||
public interface WordWrapper {
|
||||
/**
|
||||
* Wraps the text using the font in the graphics context to fit within the
|
||||
* width.
|
||||
*
|
||||
* @param text the text to be line wrapped
|
||||
* @param font the font to calculate character widths from
|
||||
* @param graphics2D the context into which the font would be rendered
|
||||
* @param width the maximum width of each line in pixels
|
||||
* @return a list of the each line of text
|
||||
* @throws NotEnoughSpace if there are more than {@link Integer#MAX_VALUE}
|
||||
* lines - so not likely.
|
||||
* @throws WordTooLong if there is a word that is too long to fit on a line
|
||||
* by itself.
|
||||
*/
|
||||
List<String> wrap(
|
||||
String text,
|
||||
Font font,
|
||||
Graphics2D graphics2D,
|
||||
int width
|
||||
);
|
||||
|
||||
/**
|
||||
* Wraps the text using the font in the graphics context to fit within the
|
||||
* boxes, filling them in order.
|
||||
*
|
||||
* @param text the text to be line wrapped
|
||||
* @param font the font to calculate character widths from
|
||||
* @param graphics2D the context into which the font would be rendered
|
||||
* @param boxes the list of rectangles to fit each line within.
|
||||
* @return a list of the each line of text
|
||||
* @throws NotEnoughSpace if there are more lines than can be fitted in the
|
||||
* boxes provided.
|
||||
* @throws WordTooLong if there is a word that is too long to fit on a line
|
||||
* by itself. Not likely as this would simply force smaller fonts.
|
||||
*/
|
||||
List<List<String>> wrap(
|
||||
String text,
|
||||
Font font,
|
||||
Graphics2D graphics2D,
|
||||
List<Rectangle2D> boxes
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package net.kemitix.text.fit;
|
|||
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.awt.*;
|
||||
|
@ -11,9 +12,10 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
@ -22,82 +24,161 @@ public class BoxFitterTest
|
|||
implements WithAssertions {
|
||||
|
||||
private final BoxFitter boxFitter = TextFit.fitter();
|
||||
private final int imageSize = 300;
|
||||
private final int fontSize = 20;
|
||||
private final Graphics2D graphics2D = graphics(imageSize, imageSize);
|
||||
private final Font font;
|
||||
private Function<Integer, Font> fontFactory;
|
||||
private final int imageSize = 300;
|
||||
private final Graphics2D graphics2D = graphics(imageSize, imageSize);
|
||||
private Rectangle2D box = new Rectangle(imageSize, imageSize);
|
||||
|
||||
public BoxFitterTest() throws URISyntaxException, IOException, FontFormatException {
|
||||
URL resource = this.getClass().getResource("alice/Alice-Regular.ttf");
|
||||
int initialFontSize = 20;
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, new File(resource.toURI()))
|
||||
.deriveFont(Font.PLAIN, fontSize);
|
||||
.deriveFont(Font.PLAIN, initialFontSize);
|
||||
fontFactory = size -> font.deriveFont(Font.PLAIN, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit single words")
|
||||
public void fitSingleWord() {
|
||||
Map<String, Integer> wordMap = Map.of(
|
||||
".", 263,
|
||||
"a", 263,
|
||||
"Word", 121,
|
||||
"longer", 104,
|
||||
"extralongword", 44
|
||||
);
|
||||
wordMap.forEach((word, expectedSize) ->
|
||||
assertThat(invoke(word))
|
||||
.as(word)
|
||||
.isEqualTo(expectedSize));
|
||||
interface FitTests {
|
||||
int fit(String text);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit two words")
|
||||
public void fitTwoWords() {
|
||||
Map<String, Integer> wordMap = Map.of(
|
||||
". .", 263,
|
||||
"a a", 208,
|
||||
"Another Word", 81,
|
||||
longStringGenerator(1), 100,
|
||||
longStringGenerator(2), 36,
|
||||
longStringGenerator(3), 27,
|
||||
longStringGenerator(4), 22,
|
||||
longStringGenerator(5), 20,
|
||||
longStringGenerator(100), 4,
|
||||
longStringGenerator(196), 3
|
||||
);
|
||||
wordMap.forEach((word, expectedSize) ->
|
||||
assertThat(invoke(word))
|
||||
.as(word)
|
||||
.isEqualTo(expectedSize));
|
||||
@Nested
|
||||
@DisplayName("Single Box API")
|
||||
public class SingleBoxAPI implements FitTests {
|
||||
|
||||
@Override
|
||||
public int fit(String longText) {
|
||||
return boxFitter.fit(longText, fontFactory, graphics2D, box);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit single words")
|
||||
public void fitSingleWord() {
|
||||
Map<String, Integer> wordMap = Map.of(
|
||||
".", 263,
|
||||
"a", 263,
|
||||
"Word", 110,
|
||||
"longer", 96,
|
||||
"extralongword", 43
|
||||
);
|
||||
wordMap.forEach((word, expectedSize) ->
|
||||
assertThat(fit(word))
|
||||
.as(word)
|
||||
.isEqualTo(expectedSize));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit various lengths")
|
||||
public void fitVariousLengths() {
|
||||
Map<String, Integer> wordMap = Map.of(
|
||||
". .", 263,
|
||||
"a a", 208,
|
||||
"Another Word", 76,
|
||||
longStringGenerator(1), 93,
|
||||
longStringGenerator(2), 36,
|
||||
longStringGenerator(3), 27,
|
||||
longStringGenerator(4), 22,
|
||||
longStringGenerator(5), 20,
|
||||
longStringGenerator(100), 4,
|
||||
longStringGenerator(196), 3
|
||||
);
|
||||
wordMap.forEach((word, expectedSize) ->
|
||||
assertThat(fit(word))
|
||||
.as(word)
|
||||
.isEqualTo(expectedSize));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Text too long to fit throws")
|
||||
// too long to fit means it would need to be rendered at a font size of <2
|
||||
public void tooLongThrows() {
|
||||
String longText = longStringGenerator(197);
|
||||
assertThatExceptionOfType(NotEnoughSpace.class)
|
||||
.isThrownBy(() -> fit(longText));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Long text can be fitted down to a font size of 3")
|
||||
public void veryLongFits() {
|
||||
String longText = longStringGenerator(196);
|
||||
assertThatCode(() -> fit(longText))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Text too long to fit throws and exception")
|
||||
// too long to fit means it would need to be rendered at a font size of <2
|
||||
public void tooLongThrows() {
|
||||
String longText = longStringGenerator(197);
|
||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||
.isThrownBy(() -> invoke(longText));
|
||||
}
|
||||
@Nested
|
||||
@DisplayName("List of Boxes API")
|
||||
public class BoxListAPI {
|
||||
|
||||
@Test
|
||||
@DisplayName("Long text can be fitted down to a font size of 3")
|
||||
public void veryLongFits() {
|
||||
String longText = longStringGenerator(196);
|
||||
assertThatCode(() -> invoke(longText))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
@Nested
|
||||
@DisplayName("Single Box")
|
||||
// different API, but should have same behaviour as using single box API
|
||||
public class SingleBox implements FitTests {
|
||||
|
||||
private final List<Rectangle2D> boxes = Collections.singletonList(box);
|
||||
|
||||
@Override
|
||||
public int fit(String longText) {
|
||||
return boxFitter.fit(longText, fontFactory, graphics2D, boxes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit various lengths")
|
||||
public void fitVariousLengths() {
|
||||
Map<String, Integer> wordMap = Map.of(
|
||||
". .", 263,
|
||||
"a a", 208,
|
||||
"Another Word", 76,
|
||||
longStringGenerator(1), 93,
|
||||
longStringGenerator(2), 36,
|
||||
longStringGenerator(3), 27,
|
||||
longStringGenerator(4), 22,
|
||||
longStringGenerator(5), 20,
|
||||
longStringGenerator(100), 4,
|
||||
longStringGenerator(196), 3
|
||||
);
|
||||
wordMap.forEach((word, expectedSize) ->
|
||||
assertThat(fit(word))
|
||||
.as(word)
|
||||
.isEqualTo(expectedSize));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Excess text for one box - throws")
|
||||
public void tooMuchForOneBox() {
|
||||
assertThatExceptionOfType(NotEnoughSpace.class)
|
||||
.isThrownBy(() ->
|
||||
fit(longStringGenerator(197)));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Two Boxes")
|
||||
public class TwoBoxes implements FitTests{
|
||||
|
||||
private final List<Rectangle2D> boxes = Arrays.asList(box, box);
|
||||
|
||||
@Override
|
||||
public int fit(String longText) {
|
||||
return boxFitter.fit(longText, fontFactory, graphics2D, boxes);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Excess text for one box - fits into two")
|
||||
public void tooLongThrows() {
|
||||
assertThatCode(() ->
|
||||
fit(longStringGenerator(197)))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private int invoke(String longText) {
|
||||
return boxFitter.fit(longText, fontFactory, graphics2D, box);
|
||||
}
|
||||
|
||||
private String longStringGenerator(int cycles) {
|
||||
String text = "This is a long piece of text that should result in an " +
|
||||
"attempt to render it at a font size on less than 2.";
|
||||
return "cycles: " + cycles + IntStream.range(0, cycles)
|
||||
return "cycles: " + cycles + IntStream.range(0, cycles)
|
||||
.mapToObj(x -> "\n").collect(Collectors.joining(text));
|
||||
}
|
||||
|
||||
|
|
40
src/test/java/net/kemitix/text/fit/StreamZipperTest.java
Normal file
40
src/test/java/net/kemitix/text/fit/StreamZipperTest.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class StreamZipperTest implements WithAssertions {
|
||||
|
||||
@Test
|
||||
void privateUtilityConstructor() throws NoSuchMethodException {
|
||||
//given
|
||||
final Constructor<StreamZipper> constructor = StreamZipper.class.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
//then
|
||||
assertThatCode(constructor::newInstance)
|
||||
.hasCauseInstanceOf(UnsupportedOperationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pair two lists together")
|
||||
void pairItems() {
|
||||
//when
|
||||
final List<String> strings = Arrays.asList("One", "Two", "Three");
|
||||
final List<Integer> integers = Arrays.asList(3, 2, 1);
|
||||
final List<Tuple<String, Integer>> zipped =
|
||||
StreamZipper.zip(strings, integers, Tuple::of).collect(Collectors.toList());
|
||||
//then
|
||||
assertThat(zipped)
|
||||
.extracting(Tuple::get1)
|
||||
.containsExactlyElementsOf(strings.subList(0, zipped.size()));
|
||||
assertThat(zipped)
|
||||
.extracting(Tuple::get2)
|
||||
.containsExactlyElementsOf(integers.subList(0, zipped.size()));
|
||||
}
|
||||
}
|
|
@ -1,25 +1,41 @@
|
|||
package net.kemitix.text.fit;
|
||||
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class TextLineWrapTest
|
||||
implements WithAssertions {
|
||||
|
||||
private static final String WORD = "word";
|
||||
private static final String SPACE = " ";
|
||||
|
||||
private final WordWrapper textLineWrap = TextFit.wrapper();
|
||||
private final int imageSize = 300;
|
||||
private final int fontSize = 20;
|
||||
private final Graphics2D graphics2D = graphics(imageSize, imageSize);
|
||||
private final Font font;
|
||||
private final int spaceWidth;
|
||||
private final int wordWidth;
|
||||
private final int wordsPerLine;
|
||||
private final int wordHeight;
|
||||
private final int linesPerBox;
|
||||
|
||||
public TextLineWrapTest()
|
||||
throws FontFormatException,
|
||||
|
@ -28,46 +44,87 @@ public class TextLineWrapTest
|
|||
URL resource = this.getClass().getResource("alice/Alice-Regular.ttf");
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, new File(resource.toURI()))
|
||||
.deriveFont(Font.PLAIN, fontSize);
|
||||
Rectangle2D spaceBounds = stringBounds(SPACE, font, graphics2D);
|
||||
Rectangle2D wordBounds = stringBounds(WORD, font, graphics2D);
|
||||
spaceWidth = (int) spaceBounds.getWidth();
|
||||
wordWidth = (int) wordBounds.getWidth();
|
||||
wordHeight = (int) wordBounds.getHeight();
|
||||
wordsPerLine = imageSize / (wordWidth + spaceWidth);
|
||||
linesPerBox = imageSize / wordHeight;
|
||||
}
|
||||
|
||||
private List<String> invoke(String in) {
|
||||
return textLineWrap.wrap(in, font, graphics2D, imageSize);
|
||||
private Rectangle2D stringBounds(
|
||||
String string,
|
||||
Font font,
|
||||
Graphics2D graphics2D
|
||||
) {
|
||||
return font.getStringBounds(string, graphics2D.getFontRenderContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty String give empty List")
|
||||
public void emptyStringEmptyList() {
|
||||
assertThat(invoke("")).isEmpty();
|
||||
@Nested
|
||||
@DisplayName("Single box")
|
||||
public class SingleBox {
|
||||
|
||||
private List<String> invoke(String in) {
|
||||
return textLineWrap.wrap(in, font, graphics2D, imageSize);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty String give empty List")
|
||||
public void emptyStringEmptyList() {
|
||||
assertThat(invoke("")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Short string fits on one line")
|
||||
public void shortStringOnOneLine() {
|
||||
assertThat(invoke("x")).containsExactly("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fits max 'words' per line on one line")
|
||||
public void fitMaxWordsOneLine() {
|
||||
String input = words(wordsPerLine, WORD);
|
||||
assertThat(invoke(input)).containsExactly(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps just over max 'words' per line onto two lines")
|
||||
public void wrapOneWordTooManyOntoTwoLines() {
|
||||
assertThat(invoke(words(wordsPerLine + 1, WORD)))
|
||||
.containsExactly(
|
||||
words(wordsPerLine, WORD),
|
||||
WORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps onto three lines")
|
||||
public void longerStringOnThreeLines() {
|
||||
String oneLinesWorthOfWords = words(wordsPerLine, WORD);
|
||||
assertThat(invoke(words(wordsPerLine + wordsPerLine + 1, WORD)))
|
||||
.containsExactly(
|
||||
oneLinesWorthOfWords,
|
||||
oneLinesWorthOfWords,
|
||||
WORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("A word that can't fit on a line by itself throws")
|
||||
public void overLongWordThrows() {
|
||||
String longWord = words(wordsPerLine * 2, WORD)
|
||||
.replace(" ", "");
|
||||
assertThatExceptionOfType(WordTooLong.class)
|
||||
.isThrownBy(() -> invoke(longWord))
|
||||
.satisfies(error ->
|
||||
assertThat(error.getLongWord())
|
||||
.isEqualTo(longWord));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Short string fits on one line")
|
||||
public void shortStringOnOneLine() {
|
||||
assertThat(invoke("x")).containsExactly("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Longer string fits on two lines")
|
||||
public void longerStringOnTwoLines() {
|
||||
assertThat(invoke(
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx " +
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx"))
|
||||
.containsExactly(
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx",
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Longer string fits on three lines")
|
||||
public void longerStringOnThreeLines() {
|
||||
assertThat(invoke(
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx " +
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx " +
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx"))
|
||||
.containsExactly(
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx",
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx",
|
||||
"xxxxxxxxxxx xxxxxxxxxxxx");
|
||||
private String words(int number, String word) {
|
||||
return IntStream.range(0, number)
|
||||
.mapToObj(i -> word + SPACE)
|
||||
.collect(Collectors.joining()).trim();
|
||||
}
|
||||
|
||||
private Graphics2D graphics(int width, int height) {
|
||||
|
@ -78,4 +135,181 @@ public class TextLineWrapTest
|
|||
private BufferedImage image(int width, int height) {
|
||||
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Overflowing Boxes")
|
||||
public class OverflowingBoxes {
|
||||
|
||||
private List<Rectangle2D> boxes = new ArrayList<>();
|
||||
|
||||
private List<List<String>> wrap(String in) {
|
||||
return textLineWrap.wrap(in, font, graphics2D, boxes);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Single box")
|
||||
public class SingleBox {
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
boxes.add(new Rectangle(imageSize, imageSize));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty String give empty List")
|
||||
public void emptyStringEmptyList() {
|
||||
assertThat(wrap("")).containsExactly(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Short string fits on one line")
|
||||
public void shortStringOnOneLine() {
|
||||
assertThat(wrap("x"))
|
||||
.containsExactly(Collections.singletonList("x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fits max 'words' per line on one line")
|
||||
public void fitMaxWordsOneLine() {
|
||||
String input = words(wordsPerLine, WORD);
|
||||
assertThat(wrap(input))
|
||||
.containsExactly(Collections.singletonList(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps just over max 'words' per line onto two lines")
|
||||
public void wrapOneWordTooManyOntoTwoLines() {
|
||||
String input = words(wordsPerLine + 1, WORD);
|
||||
assertThat(wrap(input)).containsExactly(Arrays.asList(
|
||||
words(wordsPerLine, WORD),
|
||||
WORD));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps onto three lines")
|
||||
public void longerStringOnThreeLines() {
|
||||
String input = words(wordsPerLine + wordsPerLine + 1, WORD);
|
||||
String oneLinesWorthOfWords = words(wordsPerLine, WORD);
|
||||
assertThat(wrap(input)).containsExactly(Arrays.asList(
|
||||
oneLinesWorthOfWords,
|
||||
oneLinesWorthOfWords,
|
||||
WORD));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Text fills the box")
|
||||
public void textFillsBox() {
|
||||
String lineOfWords = words(wordsPerLine, WORD);
|
||||
String words = words(linesPerBox, lineOfWords);
|
||||
assertThat(wrap(words).get(0))
|
||||
.hasSize(linesPerBox)
|
||||
.allSatisfy(line ->
|
||||
assertThat(line).isEqualTo(lineOfWords));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Text overflows the box")
|
||||
public void tooManyLinesForBox() {
|
||||
String lineOfWords = words(wordsPerLine, WORD);
|
||||
String input = words(linesPerBox + 1, lineOfWords);
|
||||
assertThatExceptionOfType(NotEnoughSpace.class)
|
||||
.isThrownBy(() -> wrap(input))
|
||||
.satisfies(error ->
|
||||
assertThat(error.getExcessWordCount())
|
||||
.isEqualTo(wordsPerLine));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Two boxes of equal width")
|
||||
public class TwoEqualWidthBoxes {
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
Rectangle box = new Rectangle(imageSize, imageSize);
|
||||
boxes.add(box);
|
||||
boxes.add(box);
|
||||
}
|
||||
|
||||
// Check that even with two boxes the shorted strings are unaffected
|
||||
@Test
|
||||
@DisplayName("Empty String give empty List")
|
||||
public void emptyStringEmptyList() {
|
||||
assertThat(wrap("")).containsExactly(
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Short string fits on one line")
|
||||
public void shortStringOnOneLine() {
|
||||
assertThat(wrap("x"))
|
||||
.containsExactly(
|
||||
Collections.singletonList("x"),
|
||||
Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fits max 'words' per line on one line")
|
||||
public void fitMaxWordsOneLine() {
|
||||
String input = words(wordsPerLine, WORD);
|
||||
assertThat(wrap(input))
|
||||
.containsExactly(
|
||||
Collections.singletonList(input),
|
||||
Collections.emptyList() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps just over max 'words' per line onto two lines")
|
||||
public void wrapOneWordTooManyOntoTwoLines() {
|
||||
String input = words(wordsPerLine + 1, WORD);
|
||||
assertThat(wrap(input)).containsExactly(Arrays.asList(
|
||||
words(wordsPerLine, WORD),
|
||||
WORD),
|
||||
Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Wraps onto three lines")
|
||||
public void longerStringOnThreeLines() {
|
||||
String input = words(wordsPerLine + wordsPerLine + 1, WORD);
|
||||
String oneLinesWorthOfWords = words(wordsPerLine, WORD);
|
||||
assertThat(wrap(input)).containsExactly(Arrays.asList(
|
||||
oneLinesWorthOfWords,
|
||||
oneLinesWorthOfWords,
|
||||
WORD),
|
||||
Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Fit max lines into the first box")
|
||||
public void fitMaxLinesInFirstBox() {
|
||||
String oneLinesWorthOfWords = words(wordsPerLine, WORD);
|
||||
String input = words(linesPerBox, oneLinesWorthOfWords);
|
||||
List<List<String>> result = wrap(input);
|
||||
List<String> linesBoxOne = result.get(0);
|
||||
assertThat(linesBoxOne)
|
||||
.hasSize(linesPerBox)
|
||||
.allSatisfy(line ->
|
||||
assertThat(line)
|
||||
.isEqualTo(oneLinesWorthOfWords));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Overflow just over max lines into the second box")
|
||||
public void overflowMaxPlusOneLinesIntoSecondBox() {
|
||||
String oneLinesWorthOfWords = words(wordsPerLine, WORD);
|
||||
String input = words(linesPerBox + 1, oneLinesWorthOfWords);
|
||||
List<List<String>> result = wrap(input);
|
||||
List<String> linesBoxOne = result.get(0);
|
||||
assertThat(linesBoxOne)
|
||||
.hasSize(linesPerBox)
|
||||
.allSatisfy(line ->
|
||||
assertThat(line)
|
||||
.isEqualTo(oneLinesWorthOfWords));
|
||||
assertThat(result.get(1))
|
||||
.containsExactly(oneLinesWorthOfWords);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue