WIP - failing tests for box fitting multiple boxes

This commit is contained in:
Paul Campbell 2020-05-21 08:27:21 +01:00
parent 9f9797dcbd
commit 40b96a7662
3 changed files with 42 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package net.kemitix.text.fit;
import java.awt.*; import java.awt.*;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
public interface BoxFitter { public interface BoxFitter {
@ -11,4 +12,10 @@ public interface BoxFitter {
Graphics2D graphics2D, Graphics2D graphics2D,
Rectangle2D box Rectangle2D box
); );
int fit(
String text,
Function<Integer, Font> fontFactory,
Graphics2D graphics2D,
List<Rectangle2D> box
);
} }

View file

@ -33,6 +33,16 @@ class BoxFitterImpl implements BoxFitter {
return fit; return fit;
} }
@Override
public int fit(
String text,
Function<Integer, Font> fontFactory,
Graphics2D graphics2D,
List<Rectangle2D> box
) {
return fit(text, fontFactory, graphics2D, box.get(0));
}
private Integer fitMinMax( private Integer fitMinMax(
int min, int min,
int max, int max,

View file

@ -12,9 +12,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Arrays;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -101,6 +101,29 @@ public class BoxFitterTest
} }
} }
@Nested
@DisplayName("Overflow boxes")
public class OverflowBoxes {
private final int imageSize = 300;
private final Graphics2D graphics2D = graphics(imageSize, imageSize);
private Rectangle2D box = new Rectangle(imageSize, imageSize);
private List<Rectangle2D> boxes = Arrays.asList(box, box);
private int invoke(String longText) {
return boxFitter.fit(longText, fontFactory, graphics2D, boxes);
}
@Test
@DisplayName("Text too long to fit single box - fits into two")
public void tooLongThrows() {
String longText = longStringGenerator(197);
int result = invoke(longText);
assertThat(result).isGreaterThan(3);
}
}
private String longStringGenerator(int cycles) { private String longStringGenerator(int cycles) {
String text = "This is a long piece of text that should result in an " + 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."; "attempt to render it at a font size on less than 2.";