The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utf8bidireorder.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utf8bidireorder.cpp - SWFilter descendant to perform reordering of
4  * UTF-8 text to visual order according to the
5  * Unicode Bidirectional Algorithm (UBA)
6  *
7  * $Id: utf8bidireorder.cpp 2980 2013-09-14 21:51:47Z scribe $
8  *
9  * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
10  * CrossWire Bible Society
11  * P. O. Box 2528
12  * Tempe, AZ 85280-2528
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  */
24 
25 #ifdef _ICU_
26 
27 #include <stdlib.h>
28 
29 #include <utilstr.h>
30 
31 #include <utf8bidireorder.h>
32 #include <swbuf.h>
33 
34 
36 
37 
38 UTF8BiDiReorder::UTF8BiDiReorder() : err(U_ZERO_ERROR) {
39 
40  conv = ucnv_open("UTF-8", &err);
41 
42 }
43 
44 
46  ucnv_close(conv);
47 }
48 
49 
50 char UTF8BiDiReorder::processText(SWBuf &text, const SWKey *key, const SWModule *module)
51 {
52  UChar *ustr, *ustr2;
53  if ((unsigned long)key < 2) // hack, we're en(1)/de(0)ciphering
54  return -1;
55 
56  int32_t len = text.length();
57  ustr = new UChar[len]; //each char could become a surrogate pair
58 
59  // Convert UTF-8 string to UTF-16 (UChars)
60  len = ucnv_toUChars(conv, ustr, len, text.c_str(), -1, &err);
61  ustr2 = new UChar[len];
62 
63  UBiDi* bidi = ubidi_openSized(len + 1, 0, &err);
64  ubidi_setPara(bidi, ustr, len, UBIDI_DEFAULT_RTL, NULL, &err);
65  len = ubidi_writeReordered(bidi, ustr2, len,
66  UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &err);
67  ubidi_close(bidi);
68 
69 // len = ubidi_writeReverse(ustr, len, ustr2, len,
70 // UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &err);
71 
72  text.setSize(text.size()*2);
73  len = ucnv_fromUChars(conv, text.getRawData(), text.size(), ustr2, len, &err);
74  text.setSize(len);
75 
76  delete [] ustr2;
77  delete [] ustr;
78  return 0;
79 }
80 
81 
83 #endif
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
SWText * module
Definition: osis2mod.cpp:105
return NULL
Definition: regex.c:7953
char * getRawData()
Definition: swbuf.h:379
const char * c_str() const
Definition: swbuf.h:158
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
unsigned long size() const
Definition: swbuf.h:185
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
UConverter * conv
void setSize(unsigned long len)
Definition: swbuf.h:255