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