int oldCurrentChar = currentChar;
int uniC = 0;
PdfChunk ck = null;
float charWidth = 0;
PdfChunk lastValidChunk = null;
TabStop tabStop = null;
float tabStopAnchorPosition = Float.NaN;
float tabPosition = Float.NaN;
boolean surrogate = false;
for (; currentChar < totalTextLength; ++currentChar) {
ck = detailChunks[currentChar];
if (ck.isImage() && minY < yLine) {
Image img = ck.getImage();
if (img.isScaleToFitHeight() && yLine + 2 * descender - img.getScaledHeight() - ck.getImageOffsetY() - img.getSpacingBefore() < minY) {
float scalePercent = (yLine + 2 * descender - ck.getImageOffsetY() - img.getSpacingBefore() - minY) / img.getScaledHeight();
ck.setImageScalePercentage(scalePercent);
}
}
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 {
if (ck.isImage()) {
charWidth = ck.getImageWidth();
} else {
charWidth = ck.getCharWidth(text[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);
float scalePercent = width / img.getWidth();
ck.setImageScalePercentage(scalePercent);
charWidth = width;
}
}
}
if (ck.isTab()) {
if (ck.isAttribute(Chunk.TABSETTINGS)) {
lastSplit = currentChar;
if (tabStop != null) {
float tabStopPosition = tabStop.getPosition(tabPosition, originalWidth - width, tabStopAnchorPosition);
width = originalWidth - (tabStopPosition + (originalWidth - width - tabPosition));
if (width < 0) {
tabStopPosition += width;
width = 0;
}
tabStop.setPosition(tabStopPosition);
}
tabStop = PdfChunk.getTabStop(ck, originalWidth - width);
if (tabStop.getPosition() > originalWidth) {
tabStop = null;
break;
}
ck.setTabStop(tabStop);
if (tabStop.getAlignment() == TabStop.Alignment.LEFT) {
width = originalWidth - tabStop.getPosition();
tabStop = null;
tabPosition = Float.NaN;
tabStopAnchorPosition = Float.NaN;
} else {
tabPosition = originalWidth - width;
tabStopAnchorPosition = Float.NaN;
}
} else {
Object[] tab = (Object[])ck.getAttribute(Chunk.TAB);
//Keep deprecated tab logic for backward compatibility...
float tabStopPosition = ((Float)tab[1]).floatValue();
boolean newLine = ((Boolean)tab[2]).booleanValue();
if (newLine && tabStopPosition < originalWidth - width) {
return new PdfLine(0, originalWidth, width, alignment, true, createArrayOfPdfChunks(oldCurrentChar, currentChar - 1), isRTL);
}
detailChunks[currentChar].adjustLeft(leftX);
width = originalWidth - tabStopPosition;
}
}
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;
}
}
} else {
boolean splitChar = ck.isExtSplitCharacter(oldCurrentChar, currentChar, totalTextLength, text, detailChunks);
if (splitChar && Character.isWhitespace((char)uniC))
lastSplit = currentChar;
if (width - charWidth < 0)
break;
if (tabStop != null && tabStop.getAlignment() == TabStop.Alignment.ANCHOR && Float.isNaN(tabStopAnchorPosition) && tabStop.getAnchorChar() == (char) uniC) {
tabStopAnchorPosition = originalWidth - width;
}
width -= charWidth;
if (splitChar)
lastSplit = currentChar;
}
lastValidChunk = ck;
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 (tabStop != null) {
float tabStopPosition = tabStop.getPosition(tabPosition, originalWidth - width, tabStopAnchorPosition);
width = originalWidth - (tabStopPosition + (originalWidth - width - tabPosition));
if (width < 0) {
tabStopPosition += width;
width = 0;
}
tabStop.setPosition(tabStopPosition);
}
if (currentChar >= totalTextLength) {
// there was more line than text
return new PdfLine(0, originalWidth, width, alignment, true, createArrayOfPdfChunks(oldCurrentChar, totalTextLength - 1), isRTL);