failed experiment.

This commit is contained in:
Andrew Lalis 2021-03-13 13:04:31 +01:00
parent 5776fa7f94
commit e6694bed23
3 changed files with 42 additions and 41 deletions

View File

@ -2,64 +2,65 @@ package nl.andrewlalis.blockbookbinder.model;
import nl.andrewlalis.blockbookbinder.util.ApplicationProperties; import nl.andrewlalis.blockbookbinder.util.ApplicationProperties;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List;
public class BookPage { public class BookPage {
private final List<String> lines; public static final int MAX_LINES = ApplicationProperties.getIntProp("book.page_max_lines");
private final String[] lines;
public BookPage() { public BookPage() {
this.lines = new ArrayList<>(ApplicationProperties.getIntProp("book.page_max_lines")); this.lines = new String[MAX_LINES];
Arrays.fill(this.lines, "");
} }
public boolean addLine(String line) { private BookPage(String[] lines) {
if (this.lines.size() >= ApplicationProperties.getIntProp("book.page_max_lines")) { this.lines = lines;
return false;
} }
this.lines.add(line);
return true; public void setLine(int index, String line) {
if (index < 0 || index >= this.lines.length) {
throw new IndexOutOfBoundsException(index);
}
this.lines[index] = line;
} }
public String getLine(int index) { public String getLine(int index) {
if (index < 0 || index >= this.lines.size()) { if (index < 0 || index >= this.lines.length) {
return null; throw new IndexOutOfBoundsException(index);
} }
return this.lines.get(index); return this.lines[index];
} }
/**
* Gets the index of the line at which this offset occurs.
* @param offset The offset, from the start of the page.
* @return The index of the line in which the given offset is placed.
*/
public int getLineIndexAtOffset(int offset) { public int getLineIndexAtOffset(int offset) {
int lineIndex = 0; int lineIndex = 0;
String line = this.getLine(lineIndex); String line = this.getLine(lineIndex);
if (line == null) return -1;
while (offset - line.length() > 0) { while (offset - line.length() > 0) {
offset-= line.length(); offset -= line.length();
line = this.getLine(++lineIndex); line = this.getLine(lineIndex++);
} }
return lineIndex; return lineIndex;
} }
public boolean hasContent() { public boolean hasContent() {
return !this.lines.isEmpty(); for (String line : this.lines) {
if (!line.isBlank()) {
return true;
}
}
return false;
} }
public BookPage copy() { public BookPage copy() {
BookPage c = new BookPage(); return new BookPage(Arrays.copyOf(this.lines, MAX_LINES));
for (String line : this.lines) {
c.addLine(line);
}
return c;
} }
@Override @Override
public String toString() { public String toString() {
return String.join("\n", this.lines); return String.join("\n", this.lines);
} }
public static BookPage fromString(String s) {
BookPage p = new BookPage();
for (String line : s.split("\n")) {
p.addLine(line);
}
return p;
}
} }

View File

@ -2,8 +2,8 @@ package nl.andrewlalis.blockbookbinder.model.build;
import nl.andrewlalis.blockbookbinder.model.Book; import nl.andrewlalis.blockbookbinder.model.Book;
import nl.andrewlalis.blockbookbinder.model.BookPage; import nl.andrewlalis.blockbookbinder.model.BookPage;
import nl.andrewlalis.blockbookbinder.util.CharWidthMapper;
import nl.andrewlalis.blockbookbinder.util.ApplicationProperties; import nl.andrewlalis.blockbookbinder.util.ApplicationProperties;
import nl.andrewlalis.blockbookbinder.util.CharWidthMapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -15,22 +15,22 @@ public class BookBuilder {
* @return A book containing the source text formatted for a minecraft book. * @return A book containing the source text formatted for a minecraft book.
*/ */
public Book build(String source) { public Book build(String source) {
final int maxLines = ApplicationProperties.getIntProp("book.page_max_lines");
List<String> lines = this.convertSourceToLines(this.cleanSource(source)); List<String> lines = this.convertSourceToLines(this.cleanSource(source));
Book book = new Book(); Book book = new Book();
BookPage page = new BookPage(); BookPage page = new BookPage();
int currentPageLineCount = 0; int currentPageLineCount = 0;
for (String line : lines) { for (String line : lines) {
page.addLine(line); page.setLine(currentPageLineCount, line);
currentPageLineCount++; currentPageLineCount++;
if (currentPageLineCount == maxLines) { if (currentPageLineCount == BookPage.MAX_LINES) {
book.addPage(page); book.addPage(page);
page = new BookPage(); page = new BookPage();
currentPageLineCount = 0; currentPageLineCount = 0;
} }
} }
// Check the last page, and only add it if it contains content.
if (page.hasContent()) { if (page.hasContent()) {
book.addPage(page); book.addPage(page);
} }

View File

@ -19,7 +19,7 @@ public class BookPreviewPanel extends JPanel {
private Book book; private Book book;
private int currentPage = 0; private int currentPage = 0;
private final JTextArea previewPageTextArea; private final JEditorPane pageEditorPane;
private final BookPageDocumentFilter documentFilter; private final BookPageDocumentFilter documentFilter;
private final JLabel titleLabel; private final JLabel titleLabel;
@ -35,11 +35,11 @@ public class BookPreviewPanel extends JPanel {
this.add(this.titleLabel, BorderLayout.NORTH); this.add(this.titleLabel, BorderLayout.NORTH);
this.setBorder(new EmptyBorder(5, 5, 5, 5)); this.setBorder(new EmptyBorder(5, 5, 5, 5));
this.previewPageTextArea = new JTextArea(); this.pageEditorPane = new JEditorPane();
this.documentFilter = new BookPageDocumentFilter(); this.documentFilter = new BookPageDocumentFilter();
AbstractDocument doc = (AbstractDocument) this.previewPageTextArea.getDocument(); AbstractDocument doc = (AbstractDocument) this.pageEditorPane.getDocument();
doc.setDocumentFilter(this.documentFilter); doc.setDocumentFilter(this.documentFilter);
this.previewPageTextArea.setEditable(true); this.pageEditorPane.setEditable(true);
try { try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("fonts/1_Minecraft-Regular.otf"); InputStream is = this.getClass().getClassLoader().getResourceAsStream("fonts/1_Minecraft-Regular.otf");
if (is == null) { if (is == null) {
@ -47,11 +47,11 @@ public class BookPreviewPanel extends JPanel {
} }
Font mcFont = Font.createFont(Font.TRUETYPE_FONT, is); Font mcFont = Font.createFont(Font.TRUETYPE_FONT, is);
mcFont = mcFont.deriveFont(24.0f); mcFont = mcFont.deriveFont(24.0f);
this.previewPageTextArea.setFont(mcFont); this.pageEditorPane.setFont(mcFont);
} catch (FontFormatException | IOException e) { } catch (FontFormatException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
JScrollPane previewPageScrollPane = new JScrollPane(this.previewPageTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); JScrollPane previewPageScrollPane = new JScrollPane(this.pageEditorPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.add(previewPageScrollPane, BorderLayout.CENTER); this.add(previewPageScrollPane, BorderLayout.CENTER);
JPanel previewButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JPanel previewButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
@ -99,7 +99,7 @@ public class BookPreviewPanel extends JPanel {
return; return;
} }
BookPage currentPage = this.book.getPages().get(this.currentPage); BookPage currentPage = this.book.getPages().get(this.currentPage);
this.previewPageTextArea.setText(currentPage.toString()); this.pageEditorPane.setText(currentPage.toString());
this.titleLabel.setText("Book Preview (Page " + (this.currentPage + 1) + " of " + this.book.getPageCount() + ")"); this.titleLabel.setText("Book Preview (Page " + (this.currentPage + 1) + " of " + this.book.getPageCount() + ")");
} }