Word wrap text to fit a Graphics2D rectangle with options to find the optimum font size and/or overflow into additional rectangles
900ed6be93
Bumps [lombok](https://github.com/projectlombok/lombok) from 1.18.20 to 1.18.24. - [Release notes](https://github.com/projectlombok/lombok/releases) - [Changelog](https://github.com/projectlombok/lombok/blob/master/doc/changelog.markdown) - [Commits](https://github.com/projectlombok/lombok/compare/v1.18.20...v1.18.24) --- updated-dependencies: - dependency-name: org.projectlombok:lombok dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> |
||
---|---|---|
.github | ||
src | ||
.gitignore | ||
LICENSE | ||
pom.xml | ||
README.md |
text-wrap-fit-box
Word wrap text to fit a Graphics2D rectangle with options to find the optimum font size and/or overflow into additional rectangles.
<dependency>
<groupId>net.kemitix</groupId>
<artifactId>text-wrap-fit-box</artifactId>
<version>${text-wrap-fit-box.version}</version>
</dependency>
Requirements
- JDK 11+
Usage
Word Wrap
Splits the input String
into a List
of String
s that fits within the
imageWidth
when drawn using the font
onto the graphics2D
.
import net.kemitix.fit.TextFit;
import net.kemitix.fit.WordWrapper;
Font font = ...
Graphics2D graphics2D = ...
int imageWidth = ...
String inputString = ....
WordWrapper wrapper = TextFit.wrapper();
List<String> lines = wrapper.wrap(inputString, font, graphics2D, imageWidth);
Box Fit
Finds the optimum fontSize
to fill the box
with the inputString
when drawn
using the graphics2D
.
import net.kemitix.fit.TextFit;
import net.kemitix.fit.BoxFitter;
Function<Integer, Font> fontFactory = size -> ....
Graphics2D graphics2D = ...
Rectangle2D box = new Rectangle(....);
String inputString = ....
BoxFitter fitter = TextFit.fitter();
int fontSize = fitter.fit(inputString, fontFactory, graphics2D, box);
Font font = fontFactory.apply(fontSize);
int imageWidth = (int) box.getWidth();
WordWrapper wrapper = TextFit.wrapper();
List<String> lines = wrapper.wrap(inputString, font, graphics2D, imageWidth);