mirror of
https://github.com/hrydgard/ppsspp.git
synced 2025-04-02 11:01:50 -04:00
Fix vertical positioning, eliminate double-&
This commit is contained in:
parent
0d3d642371
commit
0af19719cb
3 changed files with 17 additions and 11 deletions
|
@ -17,11 +17,10 @@ public class TextRenderer {
|
|||
p.setTextSize((float)textSize);
|
||||
p.getTextBounds(string, 0, string.length(), bound);
|
||||
int w = bound.width();
|
||||
int h = bound.height();
|
||||
int h = (int)(p.descent() - p.ascent() + 2.0f);
|
||||
// Round width up to even already here to avoid annoyances from odd-width 16-bit textures which
|
||||
// OpenGL does not like - each line must be 4-byte aligned
|
||||
w = (w + 3) & ~1;
|
||||
h += 2;
|
||||
return (w << 16) | h;
|
||||
}
|
||||
public static int[] renderText(String string, double textSize) {
|
||||
|
@ -29,18 +28,17 @@ public class TextRenderer {
|
|||
p.setTextSize((float)textSize);
|
||||
p.getTextBounds(string, 0, string.length(), bound);
|
||||
int w = bound.width();
|
||||
int h = bound.height();
|
||||
int h = (int)(p.descent() - p.ascent() + 2.0f);
|
||||
// Round width up to even already here to avoid annoyances from odd-width 16-bit textures which
|
||||
// OpenGL does not like - each line must be 4-byte aligned
|
||||
w = (w + 3) & ~1;
|
||||
h += 2;
|
||||
|
||||
float baseline = -p.ascent();
|
||||
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, -bound.top + 1, p);
|
||||
canvas.drawText(string, 1, -p.ascent() + 1, p);
|
||||
|
||||
int [] pixels = new int[w * h];
|
||||
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
|
||||
|
|
|
@ -73,13 +73,17 @@ void TextDrawerAndroid::SetFont(uint32_t fontHandle) {
|
|||
}
|
||||
}
|
||||
|
||||
std::string TextDrawerAndroid::NormalizeString(std::string str) {
|
||||
return ReplaceAll(str, "&&", "&");
|
||||
}
|
||||
|
||||
void TextDrawerAndroid::RecreateFonts() {
|
||||
|
||||
}
|
||||
|
||||
void TextDrawerAndroid::MeasureString(const char *str, size_t len, float *w, float *h) {
|
||||
std::string stdstring(str, len);
|
||||
jstring jstr = env_->NewStringUTF(stdstring.c_str());
|
||||
std::string text(NormalizeString(std::string(str, len)));
|
||||
jstring jstr = env_->NewStringUTF(text.c_str());
|
||||
uint32_t size = env_->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, curSize_);
|
||||
env_->DeleteLocalRef(jstr);
|
||||
*w = (size >> 16) * fontScaleX_ * dpiScale_;
|
||||
|
@ -87,7 +91,7 @@ void TextDrawerAndroid::MeasureString(const char *str, size_t len, float *w, flo
|
|||
}
|
||||
|
||||
void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) {
|
||||
std::string toMeasure = std::string(str, len);
|
||||
std::string toMeasure(NormalizeString(std::string(str, len)));
|
||||
if (align & FLAG_WRAP_TEXT) {
|
||||
bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0;
|
||||
WrapString(toMeasure, toMeasure.c_str(), rotated ? bounds.h : bounds.w);
|
||||
|
@ -102,13 +106,14 @@ void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bou
|
|||
|
||||
void TextDrawerAndroid::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) {
|
||||
using namespace Draw;
|
||||
if (!strlen(str))
|
||||
std::string text(NormalizeString(std::string(str)));
|
||||
if (text.empty())
|
||||
return;
|
||||
JNIEnv *env;
|
||||
int result = javaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
|
||||
assert(env == env_);
|
||||
|
||||
uint32_t stringHash = hash::Fletcher((const uint8_t *)str, strlen(str));
|
||||
uint32_t stringHash = hash::Fletcher((const uint8_t *)text.data(), text.size());
|
||||
uint32_t entryHash = stringHash ^ fontHash_ ^ (align << 24);
|
||||
|
||||
target.Flush(true);
|
||||
|
@ -121,7 +126,7 @@ void TextDrawerAndroid::DrawString(DrawBuffer &target, const char *str, float x,
|
|||
entry->lastUsedFrame = frameCount_;
|
||||
draw_->BindTexture(0, entry->texture);
|
||||
} else {
|
||||
jstring jstr = env_->NewStringUTF(str);
|
||||
jstring jstr = env_->NewStringUTF(text.c_str());
|
||||
int len = (int)env_->GetStringUTFLength(jstr);
|
||||
ILOG("UTF len: %d", len);
|
||||
uint32_t size = env_->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, curSize_);
|
||||
|
|
|
@ -32,6 +32,9 @@ protected:
|
|||
void ClearCache() override;
|
||||
void RecreateFonts() override; // On DPI change
|
||||
|
||||
private:
|
||||
std::string NormalizeString(std::string str);
|
||||
|
||||
// JNI functions
|
||||
JNIEnv *env_;
|
||||
jclass cls_textRenderer;
|
||||
|
|
Loading…
Add table
Reference in a new issue