update textrenderer (#5641)

try to prevent null
This commit is contained in:
kevlahnota
2024-07-18 14:27:44 +08:00
committed by GitHub
parent a24a810b16
commit 031fcf0fc9

View File

@@ -100,7 +100,11 @@ public class TextRenderer {
lastSpaceIdx = text.length(); lastSpaceIdx = text.length();
nextSpaceIdx = boundary.next(); nextSpaceIdx = boundary.next();
} }
ch = fullText.charAt(i); try {
ch = fullText.charAt(i);
} catch (StringIndexOutOfBoundsException e) {
ch = Character.MIN_VALUE;
}
switch (ch) { switch (ch) {
case '\r': case '\r':
continue; //skip '\r' character continue; //skip '\r' character
@@ -231,53 +235,53 @@ public class TextRenderer {
} }
} }
if (inKeywordCount > 0) { if (inKeywordCount > 0) {
inKeywordCount--; try {
if (inKeywordCount == 0 && text.length() > 0) { inKeywordCount--;
String keyword, value; if (inKeywordCount == 0 && text.length() > 0) {
text.deleteCharAt(0); //trim leading '<' String keyword, value;
if (text.charAt(0) == '/') { text.deleteCharAt(0); //trim leading '<'
keyword = text.substring(1); if (text.charAt(0) == '/') {
value = null; keyword = text.substring(1);
}
else {
int idx = text.indexOf(" ");
if (idx != -1) {
keyword = text.substring(0, idx);
value = text.substring(idx + 1);
}
else {
keyword = text.toString();
value = null; value = null;
}
}
boolean validKeyword = true;
switch (keyword) {
case "clr":
colorOverride = value != null ? new Color(Integer.parseInt(value)) : null;
break;
case "span":
// <span style="color:gray;">
if (value != null && value.contains("color:")) {
int startIdx = value.indexOf(':') + 1;
int endIdx = value.indexOf(';');
String colorName = value.substring(startIdx, endIdx);
if (colorName.equals("gray")) {
colorOverride = Color.GRAY;
}
} else { } else {
colorOverride = null; int idx = text.indexOf(" ");
if (idx != -1) {
keyword = text.substring(0, idx);
value = text.substring(idx + 1);
} else {
keyword = text.toString();
value = null;
}
}
boolean validKeyword = true;
switch (keyword) {
case "clr":
colorOverride = value != null ? new Color(Integer.parseInt(value)) : null;
break;
case "span":
// <span style="color:gray;">
if (value != null && value.contains("color:")) {
int startIdx = value.indexOf(':') + 1;
int endIdx = value.indexOf(';');
String colorName = value.substring(startIdx, endIdx);
if (colorName.equals("gray")) {
colorOverride = Color.GRAY;
}
} else {
colorOverride = null;
}
break;
default:
validKeyword = false;
break;
}
if (validKeyword) {
text.setLength(0);
lastSpaceIdx = -1;
continue; //skip '>' character
} }
break;
default:
validKeyword = false;
break;
} }
if (validKeyword) { } catch (Exception e) {}
text.setLength(0);
lastSpaceIdx = -1;
continue; //skip '>' character
}
}
} }
break; break;
case '(': case '(':
@@ -415,26 +419,31 @@ public class TextRenderer {
} }
} }
if (lastPieceIdx >= 0) { if (lastPieceIdx >= 0) {
Piece lastPiece = pieces.get(lastPieceIdx); try {
lineWidths.add(lastPiece.x + lastPiece.w); Piece lastPiece = pieces.get(lastPieceIdx);
x = 0; lineWidths.add(lastPiece.x + lastPiece.w);
for (int j = lastPieceIdx + 1; j < pieces.size(); j++) { x = 0;
Piece piece = pieces.get(j); int size = pieces.size();
piece.x = x; for (int j = lastPieceIdx + 1; j < size; j++) {
piece.y += lineHeight; Piece piece = pieces.get(j);
piece.lineNum++; piece.x = x;
x += piece.w; piece.y += lineHeight;
} piece.lineNum++;
y += lineHeight; x += piece.w;
totalHeight += lineHeight;
lineNum++;
if (totalHeight > height) {
//try next font size down if out of space
if (font.canShrink()) {
updatePieces(font.shrink());
return;
} }
needClip = true; y += lineHeight;
totalHeight += lineHeight;
lineNum++;
if (totalHeight > height) {
//try next font size down if out of space
if (font.canShrink()) {
updatePieces(font.shrink());
return;
}
needClip = true;
}
} catch (Exception e) {
//e.printStackTrace();
} }
} else { } else {
if (font.canShrink()) { if (font.canShrink()) {
@@ -483,7 +492,10 @@ public class TextRenderer {
private void setProps(String text, FSkinFont skinFont, float w, float h, boolean wrap0) { private void setProps(String text, FSkinFont skinFont, float w, float h, boolean wrap0) {
boolean needUpdate = false; boolean needUpdate = false;
if (!fullText.equals(text)) { if (fullText == null) {
fullText = text;
needUpdate = true;
} else if (!fullText.equals(text)) {
fullText = text; fullText = text;
needUpdate = true; needUpdate = true;
} }
@@ -538,35 +550,40 @@ public class TextRenderer {
if (needClip) { //prevent text flowing outside region if couldn't shrink it to fit if (needClip) { //prevent text flowing outside region if couldn't shrink it to fit
g.startClip(x, y, w, h); g.startClip(x, y, w, h);
} }
if (height > totalHeight && centerVertically) { try {
y += (height - totalHeight) / 2; if (height > totalHeight && centerVertically) {
} y += (height - totalHeight) / 2;
float[] alignmentOffsets = new float[lineWidths.size()]; }
for (int i = 0; i < lineWidths.size(); i++) { float[] alignmentOffsets = new float[lineWidths.size()];
switch (horzAlignment) { int size = lineWidths.size();
case Align.left: for (int i = 0; i < size; i++) {
alignmentOffsets[i] = 0; switch (horzAlignment) {
break; case Align.left:
case Align.center: alignmentOffsets[i] = 0;
alignmentOffsets[i] = Math.max((width - lineWidths.get(i)) / 2, 0); break;
break; case Align.center:
case Align.right: alignmentOffsets[i] = Math.max((width - lineWidths.get(i)) / 2, 0);
alignmentOffsets[i] = Math.max(width - lineWidths.get(i), 0); break;
break; case Align.right:
alignmentOffsets[i] = Math.max(width - lineWidths.get(i), 0);
break;
}
} }
}
visibleStartY -= y; //subtract y to make calculation quicker visibleStartY -= y; //subtract y to make calculation quicker
float visibleEndY = visibleStartY + visibleHeight; float visibleEndY = visibleStartY + visibleHeight;
for (Piece piece : pieces) { for (Piece piece : pieces) {
if (piece.y + piece.h < visibleStartY) { if (piece.y + piece.h < visibleStartY) {
continue; continue;
}
if (piece.y >= visibleEndY) {
break;
}
piece.draw(g, color, x + alignmentOffsets[piece.lineNum], y);
} }
if (piece.y >= visibleEndY) { } catch (Exception e) {
break; //e.printStackTrace();
}
piece.draw(g, color, x + alignmentOffsets[piece.lineNum], y);
} }
if (needClip) { if (needClip) {
g.endClip(); g.endClip();