# How to get Label::createWithTTF to support RTL languages such as Arabic

Firstly, I'm using Cocos2d-x v3.6, Visual Studio 2013 uses(UTF-8 without signature option) and C++.

When using Label::createWithTTF method to write Arabic text, It appear like the following.

The previous arabic text has not been written correctly, It was supposed to look like this السلام عليكم, and it also supposed the Arabic language belong to Unicode encoding.

My code:

auto label = Label::createWithTTF("السلام عليكم", "fonts/arial.ttf", 34);

Is there any way to solve that problem ?

Edit:

I have applied what in this link [ Right-to-left mark ] and the result was disappearance the Arabic text as in the following picture.

The code:

auto label = Label::createWithTTF("\u200fالسلام عليكم", "fonts/arial.ttf", 34);
label->setAlignment(TextHAlignment::RIGHT);

The problem still exists.

Cocos2d seems missing Unicode BiDi and Shaping. Please, Submit a bug report to its development team. Or make it yourself.

High Level: Pango is a multi-platfom open source text layout library.

If you want more control on text processing, see:

Low Level: HarfBuzz is a multi-platfom open source text shaping library. Low Level: There are multiple BiDi libraries. example: Fribidi.

There are more options out there including the native Unicode shaping engines of (Windows, MacOS).

You can reverse UTF8 characters in cocos2d like this, but still, Persian and Arabic words won't connect. I am actually working on localization for both Arabic and Persian texts. But for quick fix use createWithSystemFont instead. I will send a complete fix for .ttf fonts in Github soon.

Edit : Hi, this is my Persian and Arabic support for cocos2d: It works 100% fine for Persian but it may have an issue for Arabic cause I don't know Arabic well.

#include <cocos2d.h>
#include <string>
using namespace cocos2d::StringUtils;

std::string PersianLanguageSupport::reverseWord(const std::string& rawWord) {
assert(rawWord.length() > 0);
StringUTF8 utf8RawWord = StringUTF8(rawWord);
std::string reversedWord = "";
for (int i = utf8RawWord.getString().size() - 1; i >= 0; i--) {
reversedWord += utf8RawWord.getString().at(i)._char;
}
StringUTF8 utf8ReversedWord = StringUTF8(reversedWord);
return utf8ReversedWord.getAsCharSequence();
}
$$`$$

Rotate vice-versa input arabic text.

#define COCOS2D_DEBUG 1 // At the beginning of file

CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils();
std::string text = fileUtils->getStringFromFile("data.txt");
std::string label_text = "";
char *c;
c = (char *)&(*text.end());
c--;c--;
while (c > (char *)&(*text.begin())){
while (*c == '\x20'){
label_text+='\x20';
c--;
}
label_text+=(char)*(c-1);
CCLOG("%d",*(c-1));
label_text+=(char)*c;
CCLOG("%d",*(c));
c--;c--;
}
label_text+='\x0a';
label_text+='\0';
auto label = Label::createWithTTF(label_text, "fonts/arial.ttf", 34);
• Sorry, but I don't understand you. – Lion King Jun 16 '15 at 8:29
• Create a label with one letter and see if it displayed right (that this symbol is on the screen), if it is, then okay, but if not then your text that is in the string(characters) not corresponding to TTF file map and you need to create a custom font. – Atlantic-sys Jun 16 '15 at 9:32
• I wrote the letter as you told me, and the result was the letter has been displayed on the screen correctly, Also you can see a [ screenShot ] . What next ? – Lion King Jun 16 '15 at 10:52
• Use std::reverse(str, str + strlen(str)); stackoverflow.com/questions/24372480/… You need to simply reverse the string (put your text in string variable and reverse it). – Atlantic-sys Jun 16 '15 at 14:11
• codedisqus.com/CxVVqVgqUV/reverse-of-an-arabic-string-in-c.html – Atlantic-sys Jun 16 '15 at 14:21