Skip to content

Commit

Permalink
See if we can better handle book paging to fit more context on a line…
Browse files Browse the repository at this point in the history
…, without causing text loss after reaching character limit.
  • Loading branch information
khobbits committed Mar 29, 2014
1 parent 104caea commit f272a7e
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions Essentials/src/com/earth2me/essentials/textreader/BookPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
public class BookPager
{
private final transient IText text;
final double pageMax = 254;
final double charMax = 18.5;
final int lineMax = 12;

public BookPager(final IText text)
{
Expand Down Expand Up @@ -41,16 +44,20 @@ public List<String> getPages(final String pageStr) throws Exception
}
}

List<String> pages = new ArrayList<String>();
double pageLength = 0;

for (int lineNo = chapterstart; lineNo < chapterend; lineNo += 1)
{
String pageLine = "\u00a70" + lines.get(lineNo);
String pageLine = lines.get(lineNo);
String tempLine;
final double max = 18;

final int lineLength = pageLine.length();
double length = 0;
int pointer = 0;
int start = 0;
double weight = 1;
boolean forcePageEnd = false;

while (pointer < lineLength)
{
Expand All @@ -63,24 +70,40 @@ public List<String> getPages(final String pageStr) throws Exception
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 rollback = 0;
while (pos > start && pageLine.charAt(pos) != ' ' && pageLine.charAt(pos) != "\n".charAt(0))
{
rollback++;
pos--;
}
if (pos != start)
{
pointer = pos;
pageLength -= rollback;
}

tempLine = pageLine.substring(start, pointer);
pageLines.add(tempLine);
buildPage(pages, pageLines, forcePageEnd);
forcePageEnd = false;

start = pointer;
length = 0;
pageLength += 1;
}

pageLength++;

if (letter == '\u00a7' && pointer + 1 < lineLength)
{
Character nextLetter = pageLine.charAt(pointer + 1);
Expand All @@ -94,17 +117,26 @@ public List<String> getPages(final String pageStr) throws Exception
}
pointer++;
}
else if (letter == 'i' || letter == '.' || letter == ',')
else if (letter == 'i' || letter == '.' || letter == ',' || letter == '!' || letter == ':' || letter == ';' || letter == '|')
{
length += (0.34 * weight);
}
else if (letter == 'l' || letter == '\'' || letter == '`')
{
length += (0.4 * weight);
length += (0.53 * weight);
}
else if (letter == 'l')
else if (letter == ' ' || letter == 't' || letter == 'I' || letter == '[' || letter == ']')
{
length += (0.6 * weight);
length += (0.69 * weight);
}
else if (letter == ' ' || letter == 't')
else if (letter == 'f' || letter == 'k' || letter == '"' || letter == '*'
|| letter == '(' || letter == ')' || letter == '{' || letter == '}' || letter == '<' || letter == '>')
{
length += (0.7 * weight);
length += (0.85 * weight);
}
else if (letter == '@' || letter == '~')
{
length += (1.2 * weight);
}
else
{
Expand All @@ -117,21 +149,25 @@ else if (letter == ' ' || letter == 't')
{
tempLine = pageLine.substring(start, lineLength);
pageLines.add(tempLine);
buildPage(pages, pageLines, false);
}
}

List<String> pages = new ArrayList<String>();
for (int count = 0; count < pageLines.size(); count += 12)
buildPage(pages, pageLines, true);
return pages;
}

void buildPage(List<String> pages, List<String> lines, boolean override)
{
if (override || lines.size() > lineMax)
{
StringBuilder newPage = new StringBuilder();
for (int i = count; i < count + 12 && i < pageLines.size(); i++)
for (String aline : lines)
{
newPage.append(pageLines.get(i)).append("\n");
newPage.append(aline).append("\n");
}

pages.add(newPage.toString());
lines.clear();
}

return pages;
}
}

0 comments on commit f272a7e

Please sign in to comment.