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:
Andreas Beeker 2020-02-02 06:04:14 +00:00
parent 1fccbd332e
commit 5acb7d6c47
1 changed files with 26 additions and 14 deletions

View File

@ -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);
}