#include <stdlib.h>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <unicode/ubidi.h>
#include <unicode/ushape.h>
int main () {
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
UErrorCode errorCode = U_ZERO_ERROR;
std::u16string inputStr(u"سلام۳۹ is not English");
const UChar* input = reinterpret_cast<const UChar*>(inputStr.c_str());
uint32_t input_length = inputStr.length();
int32_t arabic_length = u_shapeArabic(input, input_length, NULL, 0,
(U_SHAPE_LETTERS_SHAPE & U_SHAPE_LETTERS_MASK) |
(U_SHAPE_TEXT_DIRECTION_LOGICAL & U_SHAPE_TEXT_DIRECTION_MASK),
&errorCode);
errorCode = U_ZERO_ERROR;
UChar* arabic = (UChar*)malloc(arabic_length * sizeof(UChar));
u_shapeArabic(input, input_length, arabic, arabic_length,
(U_SHAPE_LETTERS_SHAPE & U_SHAPE_LETTERS_MASK) |
(U_SHAPE_TEXT_DIRECTION_LOGICAL & U_SHAPE_TEXT_DIRECTION_MASK),
&errorCode);
if (U_FAILURE(errorCode)) {
free(arabic);
}
std::u16string arabicStr(arabic, arabic_length);
for (const auto& c : arabicStr) {
printf("%#d ", c);
}
std::cout << std::endl << converter.to_bytes(arabicStr) << std::endl;
UBiDi* bidiLine = ubidi_open();
ubidi_setPara(bidiLine, arabic, arabic_length, UBIDI_DEFAULT_LTR, NULL, &errorCode);
if (U_FAILURE(errorCode)) {
ubidi_close(bidiLine);
free(arabic);
}
int32_t output_length = ubidi_writeReordered(bidiLine, NULL, 0, UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &errorCode);
errorCode = U_ZERO_ERROR;
UChar* output = (UChar*)malloc(output_length * sizeof(UChar));
ubidi_writeReordered(bidiLine, output, output_length, UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &errorCode);
if (U_FAILURE(errorCode)) {
ubidi_close(bidiLine);
free(output);
free(arabic);
}
std::u16string outputStr(output, output_length);
for (const auto& c : outputStr) {
printf("%#d ", c);
}
std::cout << std::endl << converter.to_bytes(outputStr) << std::endl;
free(output);
free(arabic);
return 0;
}