Skip to content

Commit

Permalink
Merge pull request #10236 from unknownbrackets/text-wrap
Browse files Browse the repository at this point in the history
UI: Fix text wrapping issues
  • Loading branch information
hrydgard authored Dec 3, 2017
2 parents b795258 + 04c61ea commit 4c114c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
22 changes: 19 additions & 3 deletions android/src/org/ppsspp/ppsspp/TextRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void init(Context ctx) {
Log.e(TAG, "Failed to load Roboto Condensed");
}
}
private static Point measure(String string, double textSize) {
private static Point measureLine(String string, double textSize) {
p.setTextSize((float)textSize);
int w = (int)p.measureText(string);
int h = (int)(p.descent() - p.ascent() + 2.0f);
Expand All @@ -37,6 +37,17 @@ private static Point measure(String string, double textSize) {
p.y = h;
return p;
}
private static Point measure(String string, double textSize) {
String lines[] = string.replaceAll("\\r", "").split("\n");
Point total = new Point();
total.x = 0;
for (String line : lines) {
Point sz = measureLine(line, textSize);
total.x = Math.max(sz.x, total.x);
}
total.y = (int)(p.descent() - p.ascent()) * lines.length + 2;
return total;
}
public static int measureText(String string, double textSize) {
Point s = measure(string, textSize);
return (s.x << 16) | s.y;
Expand All @@ -50,8 +61,13 @@ public static int[] renderText(String string, double textSize) {
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawRect(0.0f, 0.0f, w, h, bg);
p.setColor(Color.WHITE);
canvas.drawText(string, 1, -p.ascent() + 1, p);

String lines[] = string.replaceAll("\\r", "").split("\n");
float y = 1.0f;
for (String line : lines) {
canvas.drawText(line, 1, -p.ascent() + y, p);
y += p.descent() - p.ascent();
}

int [] pixels = new int[w * h];
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
Expand Down
13 changes: 4 additions & 9 deletions ext/native/util/text/wrap_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ void WordWrapper::AppendWord(int endIndex, bool addNewline) {
lastLineStart_ = (int)out_.size();
} else {
// We may have appended a newline - check.
size_t pos = out_.find_last_of("\n", lastLineStart_);
size_t pos = out_.substr(lastLineStart_).find_last_of("\n");
if (pos != out_.npos) {
lastLineStart_ = (int)pos;
lastLineStart_ += (int)pos;
}
}
lastIndex_ = endIndex;
Expand Down Expand Up @@ -132,13 +132,8 @@ void WordWrapper::Wrap() {
continue;
}

float newWordWidth = 0.0f;
if (c == '\n') {
newWordWidth = wordWidth_;
} else {
// Measure the entire word for kerning purposes. May not be 100% perfect.
newWordWidth = MeasureWidth(str_ + lastIndex_, afterIndex - lastIndex_);
}
// Measure the entire word for kerning purposes. May not be 100% perfect.
float newWordWidth = MeasureWidth(str_ + lastIndex_, afterIndex - lastIndex_);

// Is this the end of a word (space)?
if (wordWidth_ > 0.0f && IsSpace(c)) {
Expand Down

0 comments on commit 4c114c1

Please sign in to comment.