Code with Finding: |
class BidiLine {
public PdfLine processLine(float leftX, float width, int alignment, int runDirection, int arabicOptions) {
this.arabicOptions = arabicOptions;
save();
boolean isRTL = runDirection == PdfWriter.RUN_DIRECTION_RTL;
if (currentChar >= totalTextLength) {
boolean hasText = getParagraph(runDirection);
if (!hasText)
return null;
if (totalTextLength == 0) {
ArrayList<PdfChunk> ar = new ArrayList<PdfChunk>();
PdfChunk ck = new PdfChunk("", detailChunks[0]);
ar.add(ck);
return new PdfLine(0, 0, 0, alignment, true, ar, isRTL);
}
}
float originalWidth = width;
int lastSplit = -1;
if (currentChar != 0)
currentChar = trimLeftEx(currentChar, totalTextLength - 1);
int oldCurrentChar = currentChar;
int uniC = 0;
PdfChunk ck = null;
float charWidth = 0;
PdfChunk lastValidChunk = null;
boolean splitChar = false;
boolean surrogate = false;
for (; currentChar < totalTextLength; ++currentChar) {
ck = detailChunks[currentChar];
surrogate = Utilities.isSurrogatePair(text, currentChar);
if (surrogate)
uniC = ck.getUnicodeEquivalent(Utilities.convertToUtf32(text, currentChar));
else
uniC = ck.getUnicodeEquivalent(text[currentChar]);
if (PdfChunk.noPrint(uniC))
continue;
if (surrogate)
charWidth = ck.getCharWidth(uniC);
else
charWidth = ck.getCharWidth(text[currentChar]);
splitChar = ck.isExtSplitCharacter(oldCurrentChar, currentChar, totalTextLength, text, detailChunks);
if (splitChar && Character.isWhitespace((char)uniC))
lastSplit = currentChar;
if (width - charWidth < 0) {
// If the chunk is an image and it is the first one in line, check if resize requested
// If so, resize to fit the current line width
if (lastValidChunk == null && ck.isImage()) {
Image img = ck.getImage();
if (img.isScaleToFitLineWhenOverflow()) {
float scalePercent = width / img.getWidth() * 100;
img.scalePercent(scalePercent);
charWidth = width;
}
}
}
if (width - charWidth < 0)
break;
if (splitChar)
lastSplit = currentChar;
width -= charWidth;
lastValidChunk = ck;
if (ck.isTab()) {
Object[] tab = (Object[])ck.getAttribute(Chunk.TAB);
float tabPosition = ((Float)tab[1]).floatValue();
boolean newLine = ((Boolean)tab[2]).booleanValue();
if (newLine && tabPosition < originalWidth - width) {
return new PdfLine(0, originalWidth, width, alignment, true, createArrayOfPdfChunks(oldCurrentChar, currentChar - 1), isRTL);
}
detailChunks[currentChar].adjustLeft(leftX);
width = originalWidth - tabPosition;
} else if (ck.isSeparator()) {
Object[] sep = (Object[])ck.getAttribute(Chunk.SEPARATOR);
DrawInterface di = (DrawInterface)sep[0];
Boolean vertical = (Boolean)sep[1];
if (vertical && di instanceof LineSeparator) {
float separatorWidth = originalWidth * ((LineSeparator) di).getPercentage() / 100f;
width -= separatorWidth;
if (width < 0) {
width = 0;
}
}
}
if (surrogate)
++currentChar;
}
if (lastValidChunk == null) {
// not even a single char fit; must output the first char
++currentChar;
if (surrogate)
++currentChar;
return new PdfLine(0, originalWidth, 0, alignment, false, createArrayOfPdfChunks(currentChar - 1, currentChar - 1), isRTL);
}
if (currentChar >= totalTextLength) {
// there was more line than text
return new PdfLine(0, originalWidth, width, alignment, true, createArrayOfPdfChunks(oldCurrentChar, totalTextLength - 1), isRTL);
}
int newCurrentChar = trimRightEx(oldCurrentChar, currentChar - 1);
if (newCurrentChar < oldCurrentChar) {
// only WS
return new PdfLine(0, originalWidth, width, alignment, false, createArrayOfPdfChunks(oldCurrentChar, currentChar - 1), isRTL);
}
if (newCurrentChar == currentChar - 1) { // middle of word
HyphenationEvent he = (HyphenationEvent)lastValidChunk.getAttribute(Chunk.HYPHENATION);
if (he != null) {
int word[] = getWord(oldCurrentChar, newCurrentChar);
if (word != null) {
float testWidth = width + getWidth(word[0], currentChar - 1);
String pre = he.getHyphenatedWordPre(new String(text, word[0], word[1] - word[0]), lastValidChunk.font().getFont(), lastValidChunk.font().size(), testWidth);
String post = he.getHyphenatedWordPost();
if (pre.length() > 0) {
PdfChunk extra = new PdfChunk(pre, lastValidChunk);
currentChar = word[1] - post.length();
return new PdfLine(0, originalWidth, testWidth - lastValidChunk.font().width(pre), alignment, false, createArrayOfPdfChunks(oldCurrentChar, word[0] - 1, extra), isRTL);
}
}
}
}
if (lastSplit == -1 || lastSplit >= newCurrentChar) {
// no split point or split point ahead of end
return new PdfLine(0, originalWidth, width + getWidth(newCurrentChar + 1, currentChar - 1), alignment, false, createArrayOfPdfChunks(oldCurrentChar, newCurrentChar), isRTL);
}
// standard split
currentChar = lastSplit + 1;
newCurrentChar = trimRightEx(oldCurrentChar, lastSplit);
if (newCurrentChar < oldCurrentChar) {
// only WS again
newCurrentChar = currentChar - 1;
}
return new PdfLine(0, originalWidth, originalWidth - getWidth(oldCurrentChar, newCurrentChar), alignment, false, createArrayOfPdfChunks(oldCurrentChar, newCurrentChar), isRTL);
}
}
|