Word wrap text to fit a Graphics2D rectangle with options to find the optimum font size and/or overflow into additional rectangles
Find a file
dependabot-preview[bot] d5dfccab17
Bump lombok from 1.18.14 to 1.18.16 (#12)
Bumps [lombok](https://github.com/rzwitserloot/lombok) from 1.18.14 to 1.18.16.
- [Release notes](https://github.com/rzwitserloot/lombok/releases)
- [Changelog](https://github.com/rzwitserloot/lombok/blob/master/doc/changelog.markdown)
- [Commits](https://github.com/rzwitserloot/lombok/compare/v1.18.14...v1.18.16)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-10-16 09:25:55 +01:00
.github Update github actions (#3) 2020-07-11 10:11:52 +01:00
src Add BoxFitter 2020-05-19 11:14:32 +01:00
.gitignore Initial Commit 2020-05-18 23:09:50 +01:00
LICENSE Initial commit 2020-05-18 19:54:53 +01:00
pom.xml Bump lombok from 1.18.14 to 1.18.16 (#12) 2020-10-16 09:25:55 +01:00
README.md Update github actions (#3) 2020-07-11 10:11:52 +01:00

text-wrap-fit-box

Sonatype Nexus (Release) Maven Central

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 Strings 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);