See if we can better handle book paging to fit more context on a line, without causing text loss after reaching character limit.

This commit is contained in:
KHobbits 2014-03-29 16:07:32 +00:00
parent 104caea3c9
commit f272a7ecd6

View File

@ -10,6 +10,9 @@ import java.util.Map;
public class BookPager public class BookPager
{ {
private final transient IText text; private final transient IText text;
final double pageMax = 254;
final double charMax = 18.5;
final int lineMax = 12;
public BookPager(final IText text) public BookPager(final IText text)
{ {
@ -41,16 +44,20 @@ public class BookPager
} }
} }
List<String> pages = new ArrayList<String>();
double pageLength = 0;
for (int lineNo = chapterstart; lineNo < chapterend; lineNo += 1) for (int lineNo = chapterstart; lineNo < chapterend; lineNo += 1)
{ {
String pageLine = "\u00a70" + lines.get(lineNo); String pageLine = lines.get(lineNo);
String tempLine; String tempLine;
final double max = 18;
final int lineLength = pageLine.length(); final int lineLength = pageLine.length();
double length = 0; double length = 0;
int pointer = 0; int pointer = 0;
int start = 0; int start = 0;
double weight = 1; double weight = 1;
boolean forcePageEnd = false;
while (pointer < lineLength) while (pointer < lineLength)
{ {
@ -63,24 +70,40 @@ public class BookPager
continue; continue;
} }
if (length >= max || (letter == '\u00a7' && length + 1 >= max)) if (pageLength >= pageMax)
{
length = charMax;
pageLength = 0;
forcePageEnd = true;
}
if (length >= charMax || (letter == '\u00a7' && length + 1 >= charMax))
{ {
int pos = pointer; int pos = pointer;
int rollback = 0;
while (pos > start && pageLine.charAt(pos) != ' ' && pageLine.charAt(pos) != "\n".charAt(0)) while (pos > start && pageLine.charAt(pos) != ' ' && pageLine.charAt(pos) != "\n".charAt(0))
{ {
rollback++;
pos--; pos--;
} }
if (pos != start) if (pos != start)
{ {
pointer = pos; pointer = pos;
pageLength -= rollback;
} }
tempLine = pageLine.substring(start, pointer); tempLine = pageLine.substring(start, pointer);
pageLines.add(tempLine); pageLines.add(tempLine);
buildPage(pages, pageLines, forcePageEnd);
forcePageEnd = false;
start = pointer; start = pointer;
length = 0; length = 0;
pageLength += 1;
} }
pageLength++;
if (letter == '\u00a7' && pointer + 1 < lineLength) if (letter == '\u00a7' && pointer + 1 < lineLength)
{ {
Character nextLetter = pageLine.charAt(pointer + 1); Character nextLetter = pageLine.charAt(pointer + 1);
@ -94,17 +117,26 @@ public class BookPager
} }
pointer++; pointer++;
} }
else if (letter == 'i' || letter == '.' || letter == ',') else if (letter == 'i' || letter == '.' || letter == ',' || letter == '!' || letter == ':' || letter == ';' || letter == '|')
{ {
length += (0.4 * weight); length += (0.34 * weight);
} }
else if (letter == 'l') else if (letter == 'l' || letter == '\'' || letter == '`')
{ {
length += (0.6 * weight); length += (0.53 * weight);
} }
else if (letter == ' ' || letter == 't') else if (letter == ' ' || letter == 't' || letter == 'I' || letter == '[' || letter == ']')
{ {
length += (0.7 * weight); length += (0.69 * weight);
}
else if (letter == 'f' || letter == 'k' || letter == '"' || letter == '*'
|| letter == '(' || letter == ')' || letter == '{' || letter == '}' || letter == '<' || letter == '>')
{
length += (0.85 * weight);
}
else if (letter == '@' || letter == '~')
{
length += (1.2 * weight);
} }
else else
{ {
@ -117,21 +149,25 @@ public class BookPager
{ {
tempLine = pageLine.substring(start, lineLength); tempLine = pageLine.substring(start, lineLength);
pageLines.add(tempLine); pageLines.add(tempLine);
buildPage(pages, pageLines, false);
} }
} }
List<String> pages = new ArrayList<String>(); buildPage(pages, pageLines, true);
for (int count = 0; count < pageLines.size(); count += 12)
{
StringBuilder newPage = new StringBuilder();
for (int i = count; i < count + 12 && i < pageLines.size(); i++)
{
newPage.append(pageLines.get(i)).append("\n");
}
pages.add(newPage.toString());
}
return pages; return pages;
} }
void buildPage(List<String> pages, List<String> lines, boolean override)
{
if (override || lines.size() > lineMax)
{
StringBuilder newPage = new StringBuilder();
for (String aline : lines)
{
newPage.append(aline).append("\n");
}
pages.add(newPage.toString());
lines.clear();
}
}
} }