J'ai un problème (développement informatique) : je suis incapable de faire un rendu de texte (dans une image) pour les langues qui s'écrivent de droite à gauche (comme l'arabe ou l'hébreu ...

).
Le texte est trop grand et non centré, et ne rentre pas dans l'image !
J'EN AI MARRE ! JE SUIS PERDU ...
Au cas où quelqu'un pourrait m'aider ... (Pour java, Pour Android)
(Cela marche parfaitement pour toutes les langues sauf pour celles qui s'écrivent de droite à gauche.)
Code : Tout sélectionner
public TextUIElement(final float letterSize, Game game, int fontResourceId) {
bounds = new Rect();
final Typeface font = game == null ? null : ResourcesCompat.getFont(game.getContext(), fontResourceId);
textPaint = new TextPaint() {
{
setColor(Color.WHITE);
setTextAlign(Align.CENTER);
setTextSize(letterSize);
setAntiAlias(true);
}
};
if (font != null) textPaint.setTypeface(font);
}
public void setText(String text) {
textPaint.getTextBounds(text, 0, text.length(), bounds);
StaticLayout textLayout = new StaticLayout(text, textPaint, bounds.width(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int maxWidth = -1;
for (int i = 0; i < textLayout.getLineCount(); i++)
maxWidth = Math.max((int)textLayout.getLineWidth(i), maxWidth);
Bitmap bitmap = Bitmap.createBitmap(maxWidth , textLayout.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
canvas.translate(maxWidth / 2.0f, 0.0f);
textLayout.draw(canvas);
int [] textureId = new int[1];
GLES20.glGenTextures(1, textureId, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
texture = new Texture(textureId[0], bitmap.getWidth(), bitmap.getHeight());
spriteBatch = new SpriteBatch(texture.getWidth(), texture.getHeight(), 1, 1);
}
public void draw(int shaderProgram, float [] projectionViewMatrix, float xCenter, float yCenter, float scaleWidth, float scaleHeight, float rotationDegrees, com.niviz.Color color) {
Game.bindTexture(texture);
spriteBatch.clear();
spriteBatch.addSprite(0, 0, xCenter, yCenter, scaleWidth * texture.getWidth(), scaleHeight * texture.getHeight(), rotationDegrees, color);
spriteBatch.draw(shaderProgram, projectionViewMatrix);
}
Merci.
[EDIT]
Re bonjour.
Pour les langues qui s'écrivent de gauche à droite :
Code : Tout sélectionner
StaticLayout textLayout = new StaticLayout(text, textPaint, bounds.width(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
Pour les langues qui s'écrivent de droite à gauche :
Code : Tout sélectionner
StaticLayout textLayout = new StaticLayout(text, textPaint, bounds.width(), Layout.Alignment.ALIGN_OPPOSITE, 1.0f, 0.0f, false);
Cela règle le problème.
Mais un autre surgit : comment savoir si un texte est dans un sens ou dans l'autre ?
Je pourrais demander explicitement à l'utilisateur de définir le sens, mais il y a moyen de définir une heuristique qui détecte en fonction du texte le sens d'affichage.
J'en suis là. Mais avant de me précipiter à coder, et faire n'importe quoi, je préfère attendre un peu. Et surtout réfléchir tranquillement.