Adds a new helper class responsible for book formatting
This commit is contained in:
parent
491462c132
commit
3364337b14
@ -0,0 +1,92 @@
|
||||
package net.knarcraft.bookswithoutborders.utility;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A class for formatting text to fit books
|
||||
*
|
||||
* <p>The book formatter</p>
|
||||
*/
|
||||
public class BookFormatter {
|
||||
|
||||
/**
|
||||
* Formats the last page of a set of pages
|
||||
*
|
||||
* <p>If the last page is non-empty and has space, a newline is added. If the last two pages' contents can fit on
|
||||
* the same page, they will be combined.</p>
|
||||
*
|
||||
* @param rawPages <p>A list of pages</p>
|
||||
*/
|
||||
public static void formatLastPage(List<String> rawPages) {
|
||||
int maxPageText = 256;
|
||||
int fitsNewline = maxPageText - 2;
|
||||
|
||||
//Adds newline if the contents of the last page does not exceed page limit and is non-empty
|
||||
formatLastPageAddNewline(rawPages, fitsNewline);
|
||||
|
||||
//Combines the two last pages if they can fit on one page
|
||||
if (rawPages.size() > 1) {
|
||||
formatLastPageCombinePages(rawPages, maxPageText);
|
||||
}
|
||||
|
||||
//Splits the last page if it is too long
|
||||
if (rawPages.get(rawPages.size() - 1).length() > maxPageText) {
|
||||
formatLastPageSplitOverflow(rawPages, maxPageText, fitsNewline);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the last page if it overflows
|
||||
* @param rawPages <p>The raw pages to format</p>
|
||||
* @param maxPageText <p>The max number of characters which fit on a page</p>
|
||||
* @param fitsNewline <p>The max number of characters on a page which still fits a newline character</p>
|
||||
*/
|
||||
public static void formatLastPageSplitOverflow(List<String> rawPages, int maxPageText, int fitsNewline) {
|
||||
while (rawPages.get(rawPages.size() - 1).length() > maxPageText) {
|
||||
int splitPosition;
|
||||
String fittingText = rawPages.get(rawPages.size() - 1).substring(0, maxPageText);
|
||||
int lastSpaceBeforePageBreak = fittingText.lastIndexOf(" ");
|
||||
|
||||
if (lastSpaceBeforePageBreak > -1) {
|
||||
//Split at space if it exists
|
||||
rawPages.add(rawPages.get(rawPages.size() - 1).substring(lastSpaceBeforePageBreak + 1));
|
||||
splitPosition = lastSpaceBeforePageBreak;
|
||||
} else {
|
||||
//Split at text limit
|
||||
rawPages.add(rawPages.get(rawPages.size() - 1).substring(maxPageText));
|
||||
splitPosition = maxPageText;
|
||||
}
|
||||
//Adds newline if the contents of the last page does not exceed page limit and is non-empty
|
||||
formatLastPageAddNewline(rawPages, fitsNewline);
|
||||
//Removes excess page content
|
||||
rawPages.set(rawPages.size() - 2, rawPages.get(rawPages.size() - 2).substring(0, splitPosition));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines the two last pages if they can fit on the same page
|
||||
* @param rawPages <p>The raw pages to format</p>
|
||||
* @param maxPageText <p>The max number of characters which fit on a page</p>
|
||||
*/
|
||||
public static void formatLastPageCombinePages(List<String> rawPages, int maxPageText) {
|
||||
int lastPageIndex = rawPages.size() - 1;
|
||||
int nextToLastIndex = rawPages.size() - 2;
|
||||
if (rawPages.get(nextToLastIndex).length() + rawPages.get(lastPageIndex).length() <= maxPageText) {
|
||||
rawPages.set(nextToLastIndex, (rawPages.get(nextToLastIndex)) + (rawPages.get(lastPageIndex)));
|
||||
rawPages.remove(lastPageIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds newline if the contents of the last page does not exceed page limit and is non-empty
|
||||
* @param rawPages <p>The raw pages to format</p>
|
||||
* @param fitsNewline <p>The max number of characters on a page which still fits a newline character</p>
|
||||
*/
|
||||
public static void formatLastPageAddNewline(List<String> rawPages, int fitsNewline) {
|
||||
int pageIndex = rawPages.size() - 1;
|
||||
if (rawPages.get(pageIndex).length() <= fitsNewline && !rawPages.get(pageIndex).isEmpty()) {
|
||||
rawPages.set(pageIndex, (rawPages.get(pageIndex)) + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user