feat(book): complete paginated character inspection
This commit is contained in:
1 parent
9ddc139544
commit
6f42082383
4 files changed
+211
-9
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package games.dmg.fantasystick.book;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
/** Plain native book pages, conservatively bounded for the vanilla font (no styled/bold text). */
|
||||
public final class BookPagination {
|
||||
private BookPagination() { }
|
||||
|
||||
public static List<Component> pages(String text) {
|
||||
var lines = new ArrayList<String>();
|
||||
var line = new StringBuilder();
|
||||
int width = 0;
|
||||
for (int offset = 0; offset < text.length();) {
|
||||
int codePoint = text.codePointAt(offset);
|
||||
offset += Character.charCount(codePoint);
|
||||
if (codePoint == '\n') {
|
||||
lines.add(line.toString()); line.setLength(0); width = 0;
|
||||
continue;
|
||||
}
|
||||
// ASCII is at most six pixels; vanilla's Unicode fallback is at most nine.
|
||||
// Leave six pixels of horizontal slack and one line of vertical slack.
|
||||
int advance = codePoint < 128 ? 6 : 9;
|
||||
if (width + advance > 108) {
|
||||
lines.add(line.toString()); line.setLength(0); width = 0;
|
||||
}
|
||||
line.appendCodePoint(codePoint); width += advance;
|
||||
}
|
||||
lines.add(line.toString());
|
||||
var pages = new ArrayList<Component>();
|
||||
for (int start = 0; start < lines.size(); start += 13) {
|
||||
pages.add(Component.text(String.join("\n", lines.subList(start, Math.min(start + 13, lines.size())))));
|
||||
}
|
||||
return List.copyOf(pages);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user