Add text alignment support to drawText().
This change also integrates better support for RTL text.
Change-Id: I6da8f5cf5dc28ca7cf1b22e27b0d853c919e8481
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index c6a2e33..d30d718 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -525,7 +525,26 @@
drawColorRect(left, top, right, bottom, color, mode);
}
-void OpenGLRenderer::drawText(const char* text, int count, float x, float y, SkPaint* paint) {
+void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
+ float x, float y, SkPaint* paint) {
+ if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
+ return;
+ }
+
+ float length;
+ switch (paint->getTextAlign()) {
+ case SkPaint::kCenter_Align:
+ length = paint->measureText(text, bytesCount);
+ x -= length / 2.0f;
+ break;
+ case SkPaint::kRight_Align:
+ length = paint->measureText(text, bytesCount);
+ x -= length;
+ break;
+ default:
+ break;
+ }
+
int alpha;
SkXfermode::Mode mode;
getAlphaAndMode(paint, &alpha, &mode);
@@ -551,7 +570,7 @@
const Rect& clip = mSnapshot->getLocalClip();
mFontRenderer.setFont(SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
- mFontRenderer.renderText(paint, &clip, text, 0, count, count, x, y);
+ mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}