1
/******************************************************************************
3
* utf8bidireorder - SWFilter descendant to perform reordering of UTF-8
4
* text to visual order according to Unicode BiDi
7
* Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
8
* CrossWire Bible Society
10
* Tempe, AZ 85280-2528
12
* This program is free software; you can redistribute it and/or modify it
13
* under the terms of the GNU General Public License as published by the
14
* Free Software Foundation version 2.
16
* This program is distributed in the hope that it will be useful, but
17
* WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* General Public License for more details.
29
#include <utf8bidireorder.h>
34
UTF8BiDiReorder::UTF8BiDiReorder() {
36
conv = ucnv_open("UTF-8", &err);
40
UTF8BiDiReorder::~UTF8BiDiReorder() {
44
char UTF8BiDiReorder::processText(SWBuf &text, const SWKey *key, const SWModule *module)
47
if ((unsigned long)key < 2) // hack, we're en(1)/de(0)ciphering
50
int32_t len = text.length();
51
ustr = new UChar[len]; //each char could become a surrogate pair
53
// Convert UTF-8 string to UTF-16 (UChars)
54
len = ucnv_toUChars(conv, ustr, len, text.c_str(), -1, &err);
55
ustr2 = new UChar[len];
57
UBiDi* bidi = ubidi_openSized(len + 1, 0, &err);
58
ubidi_setPara(bidi, ustr, len, UBIDI_DEFAULT_RTL, NULL, &err);
59
len = ubidi_writeReordered(bidi, ustr2, len,
60
UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &err);
63
// len = ubidi_writeReverse(ustr, len, ustr2, len,
64
// UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &err);
66
text.setSize(text.size()*2);
67
len = ucnv_fromUChars(conv, text.getRawData(), text.size(), ustr2, len, &err);