mirror of https://github.com/apache/poi.git
TextLayout.getAdvance() for " " returns 0 in some cases / JDKs, which caused an div by 0 -> Infinity problem
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1873494 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1fccbd332e
commit
5acb7d6c47
|
@ -31,6 +31,7 @@ import java.text.AttributedCharacterIterator;
|
|||
import java.text.AttributedCharacterIterator.Attribute;
|
||||
import java.text.AttributedString;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
|
@ -383,7 +384,14 @@ public class DrawTextParagraph implements Drawable {
|
|||
|
||||
@Internal
|
||||
public String getRenderableText(final TextRun tr) {
|
||||
final String txtSpace = tr.getRawText().replace("\t", tab2space(tr)).replace('\u000b', '\n');
|
||||
String txtSpace = tr.getRawText();
|
||||
if (txtSpace == null) {
|
||||
return null;
|
||||
}
|
||||
if (txtSpace.contains("\t")) {
|
||||
txtSpace = txtSpace.replace("\t", tab2space(tr));
|
||||
}
|
||||
txtSpace = txtSpace.replace('\u000b', '\n');
|
||||
final Locale loc = LocaleUtil.getUserLocale();
|
||||
|
||||
switch (tr.getTextCap()) {
|
||||
|
@ -414,19 +422,23 @@ public class DrawTextParagraph implements Drawable {
|
|||
string.addAttribute(TextAttribute.SIZE, fs.floatValue());
|
||||
|
||||
TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true));
|
||||
// some JDK versions return 0 here
|
||||
double wspace = l.getAdvance();
|
||||
|
||||
final int numSpaces;
|
||||
Double tabSz = paragraph.getDefaultTabSize();
|
||||
if (wspace <= 0) {
|
||||
numSpaces = 4;
|
||||
} else {
|
||||
if (tabSz == null) {
|
||||
tabSz = wspace*4;
|
||||
}
|
||||
|
||||
int numSpaces = (int)Math.ceil(tabSz / wspace);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for(int i = 0; i < numSpaces; i++) {
|
||||
buf.append(' ');
|
||||
numSpaces = (int)Math.min(Math.ceil(tabSz / wspace), 20);
|
||||
}
|
||||
return buf.toString();
|
||||
|
||||
char[] buf = new char[numSpaces];
|
||||
Arrays.fill(buf, ' ');
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue