| Code with Finding: |
class PdfChunk {
protected int getWord(String text, int start) {
int len = text.length();
while (start < len) {
if (!Character.isLetter(text.charAt(start)))
break;
++start;
}
return start;
}
}
class PdfChunk {
/**
* Truncates this <CODE>PdfChunk</CODE> if it's too long for the given width.
* <P>
* Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
*
* @param width a given width
* @return the <CODE>PdfChunk</CODE> that doesn't fit into the width.
*/
PdfChunk truncate(float width) {
if (image != null) {
if (image.getScaledWidth() > width) {
// Image does not fit the line, resize if requested
if (image.isScaleToFitLineWhenOverflow()) {
float scalePercent = width / image.getWidth() * 100;
image.scalePercent(scalePercent);
return null;
}
PdfChunk pc = new PdfChunk("", this);
value = "";
attributes.remove(Chunk.IMAGE);
image = null;
font = PdfFont.getDefaultFont();
return pc;
}
else
return null;
}
int currentPosition = 0;
float currentWidth = 0;
// it's no use trying to split if there isn't even enough place for a space
if (width < font.width()) {
String returnValue = value.substring(1);
value = value.substring(0, 1);
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
// loop over all the characters of a string
// or until the totalWidth is reached
int length = value.length();
boolean surrogate = false;
while (currentPosition < length) {
// the width of every character is added to the currentWidth
surrogate = Utilities.isSurrogatePair(value, currentPosition);
if (surrogate)
currentWidth += getCharWidth(Utilities.convertToUtf32(value, currentPosition));
else
currentWidth += getCharWidth(value.charAt(currentPosition));
if (currentWidth > width)
break;
if (surrogate)
currentPosition++;
currentPosition++;
}
// if all the characters fit in the total width, null is returned (there is no overflow)
if (currentPosition == length) {
return null;
}
// otherwise, the string has to be truncated
//currentPosition -= 2;
// we have to chop off minimum 1 character from the chunk
if (currentPosition == 0) {
currentPosition = 1;
if (surrogate)
++currentPosition;
}
String returnValue = value.substring(currentPosition);
value = value.substring(0, currentPosition);
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
}
|