// Chapter settings
final String chapterPath = booklet.getChapter().getPath();
final char[] buffer = booklet.getTextBuffer();
final int bufferSize;
final Archive bookFile = booklet.bookArchive;
final Vector images = ip.images;
byte style;
boolean center;
byte color;
AlbiteFont font;
HyphenatedTextRegion lastHyphenatedWord;
boolean startsNewParagraph;
TextParser parser = ip.parser;
int wordPixelWidth; //word width in pixels
Vector wordsOnThisLine = new Vector(20); //RegionTexts
boolean firstWord;
int posX = 0;
int posY = 0;
Vector regionsTemp;
if (images.isEmpty()) {
//text mode
regionsTemp = new Vector(300);
parser.position = end = start = ip.position;
parser.length = ip.length;
bufferSize = buffer.length;
style = ip.style;
center = ip.center;
lastHyphenatedWord = ip.lastHyphenatedWord;
startsNewParagraph = ip.startsNewParagraph;
} else {
//image mode
ImageRegion ri = (ImageRegion) images.firstElement();
images.removeElementAt(0);
imageRegion = ri;
regionsTemp = new Vector(40);
posY = 0;
bufferSize = ri.altTextBufferPosition + ri.altTextBufferLength;
parser.position = end = start = ri.altTextBufferPosition;
parser.length = 0;
style = ITALIC;
center = true;
lastHyphenatedWord = null;
startsNewParagraph = true;
}
/*
* Setup font & color, based on style value from previous page.
*/
font = chooseFont(fontPlain, fontItalic, style);
color = chooseTextColor(style);
boolean lastLine = false;
boolean firstLine = true;
boolean lineBreak = false;
page:
while (true) {
/*
* There is no more space for new lines,
* so the page is done.
*/
if (posY >= height - fontHeight) {
break;
}
/*
* Check if it is the last line of the page
*/
if (posY >= height - (fontHeightX2)) {
lastLine = true;
}
/*
* NB: posX & posY are in pixels, pos is in chars.
*/
posX = 0;
firstWord = true;
/*
* Clear the cache that will hold all the elements on the line
*/
wordsOnThisLine.removeAllElements();
/*
* Indent the line, if it starts a new paragraph.
*/
if (startsNewParagraph) {
posX = fontIndent;
}
line:
while (true) {
/*
* Parse on
*/
if (!parser.parseNext(buffer, bufferSize)) {
//#ifdef DEBUG_PARSER
//# AlbiteMIDlet.LOGGER.log("parser done");
//#endif
/* No more chars to read */
if (imageRegion == null) {
ip.bufferRead = true;
}
lineBreak = true;
if (wordsOnThisLine.size() > 0) {
positionWordsOnLine(
wordsOnThisLine, regionsTemp, width,
posY, spaceWidth, fontIndent, lineBreak,
startsNewParagraph, center);
}
break page;
}
//#ifdef DEBUG_PARSER
//# AlbiteMIDlet.LOGGER.log(
//# "parser: _"
//# + new String(
//# buffer, parser.position, parser.length)
//# + "_, "
//# + parser.position + " / "
//# + parser.length
//# + " state: " + parser.state + "\n");
//#endif
/*
* Logic for possible parsing states.
*/
final int state = parser.state;
switch (state) {
case TextParser.STATE_PASS:
continue line;
case TextParser.STATE_NEW_SOFT_LINE:
if (posX == 0) {
/*
* Only if it's on the next line
*/
startsNewParagraph = true;
}
if (!(posX > (startsNewParagraph ? fontIndent : 0))) {
continue line;
}
case TextParser.STATE_NEW_LINE: //linebreak
if (!firstLine || (posX >
(startsNewParagraph ? fontIndent : 0)
)) {
lineBreak = true;
break line;
} else {
/* don't start a page with blank lines */
continue line;
}
case TextParser.STATE_STYLING:
/* enable styling */
if (parser.enableBold) {
style |= BOLD;
}
if (parser.enableItalic) {
style |= ITALIC;
}
if (parser.enableHeading) {
style |= HEADING;
}
if (parser.enableCenterAlign) {
center = true;
}
if (parser.disableCenterAlign) {
center = false;
}
/* disable styling */
if (parser.disableBold) {
style &= ~BOLD;
}
if (parser.disableItalic) {
style &= ~ITALIC;
}
if (parser.disableHeading) {
style &= ~HEADING;
}
/* setup font & color */
font = chooseFont(fontPlain,
fontItalic, style);
color = chooseTextColor(style);
continue line;
case TextParser.STATE_IMAGE:
if (booklet.renderImages) {
ImageRegion ri = new ImageRegion(
(bookFile == null
? null
: bookFile.getEntry(
RandomReadingFile
.relativeToAbsoluteURL(
chapterPath +
new String(buffer,
parser.imageURLPosition,