Started work on modernizing stuff.
This commit is contained in:
parent
deb10b1099
commit
e32976fb8b
13
pom.xml
13
pom.xml
|
@ -6,11 +6,12 @@
|
|||
|
||||
<groupId>nl.andrewlalis</groupId>
|
||||
<artifactId>BlockBookBinder</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.3.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>12</maven.compiler.source>
|
||||
<maven.compiler.target>12</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
@ -46,7 +47,7 @@
|
|||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.14</version>
|
||||
<version>1.18.28</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -60,13 +61,13 @@
|
|||
<dependency>
|
||||
<groupId>com.formdev</groupId>
|
||||
<artifactId>flatlaf</artifactId>
|
||||
<version>1.0-rc3</version>
|
||||
<version>3.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-model</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<version>3.9.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -14,7 +14,7 @@ public class BlockBookBinder {
|
|||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
FlatDarkLaf.install();
|
||||
FlatDarkLaf.setup();
|
||||
var mainFrame = new MainFrame();
|
||||
mainFrame.setupAndShow();
|
||||
});
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
package nl.andrewlalis.blockbookbinder.model.build;
|
||||
|
||||
import nl.andrewlalis.blockbookbinder.model.Book;
|
||||
import nl.andrewlalis.blockbookbinder.model.BookPage;
|
||||
import nl.andrewlalis.blockbookbinder.model.CharWidthMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BookBuilder2 {
|
||||
private final int MAX_LINES_PER_PAGE;
|
||||
private final int MAX_CHARS_PER_PAGE;
|
||||
private final int MAX_LINE_PIXEL_WIDTH;
|
||||
|
||||
private List<String> lines;
|
||||
|
||||
private StringBuilder lineBuilder;
|
||||
private StringBuilder wordBuilder;
|
||||
private int currentLine;
|
||||
private int currentLinePixelWidth;
|
||||
private int currentWordPixelWidth;
|
||||
|
||||
public BookBuilder2(int maxLinesPerPage, int maxCharsPerPage, int maxLinePixelWidth) {
|
||||
this.MAX_LINES_PER_PAGE = maxLinesPerPage;
|
||||
this.MAX_CHARS_PER_PAGE = maxCharsPerPage;
|
||||
this.MAX_LINE_PIXEL_WIDTH = maxLinePixelWidth;
|
||||
this.lines = new ArrayList<>();
|
||||
this.lineBuilder = new StringBuilder(64);
|
||||
this.wordBuilder = new StringBuilder(64);
|
||||
this.currentLine = 0;
|
||||
this.currentLinePixelWidth = 0;
|
||||
this.currentWordPixelWidth = 0;
|
||||
}
|
||||
|
||||
public BookBuilder2 addText(String text) {
|
||||
int idx = 0;
|
||||
while (idx < text.length()) {
|
||||
final char c = text.charAt(idx++);
|
||||
if (c == '\n') {
|
||||
appendLine();
|
||||
} else if (c == ' ' && lineBuilder.length() == 0) {
|
||||
continue; // Skip spaces at the start of lines.
|
||||
} else { // Read a continuous word.
|
||||
int charsRead = readWord(text, idx - 1);
|
||||
idx += charsRead - 1;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Book build() {
|
||||
Book book = new Book();
|
||||
BookPage page = new BookPage();
|
||||
int currentPageLineCount = 0;
|
||||
|
||||
for (String line : lines) {
|
||||
page.addLine(line);
|
||||
currentPageLineCount++;
|
||||
if (currentPageLineCount == MAX_LINES_PER_PAGE) {
|
||||
book.addPage(page);
|
||||
page = new BookPage();
|
||||
currentPageLineCount = 0;
|
||||
}
|
||||
}
|
||||
if (page.hasContent()) {
|
||||
book.addPage(page);
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
private int readWord(String text, int firstCharIdx) {
|
||||
currentWordPixelWidth = 0;
|
||||
wordBuilder.setLength(0);
|
||||
int idx = firstCharIdx;
|
||||
while (idx < text.length()) {
|
||||
char c = text.charAt(idx++);
|
||||
if (!Character.isWhitespace(c)) {
|
||||
currentWordPixelWidth += CharWidthMapper.getInstance().getWidth(c) + 1;
|
||||
wordBuilder.append(c);
|
||||
|
||||
// If we notice that our word will cause the current line to exceed max width, go to a newline.
|
||||
if (currentLinePixelWidth + currentWordPixelWidth > MAX_LINE_PIXEL_WIDTH) {
|
||||
appendLine();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
String word = wordBuilder.toString();
|
||||
return word.length();
|
||||
}
|
||||
|
||||
private void appendLine() {
|
||||
this.lines.add(this.lineBuilder.toString());
|
||||
this.lineBuilder.setLength(0);
|
||||
this.currentLine++;
|
||||
this.currentLinePixelWidth = 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue