1
/******************************************************************************
3
* unicodertf - SWFilter descendant to convert a double byte unicode file
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.
25
#include <unicodertf.h>
30
UnicodeRTF::UnicodeRTF() {
34
char UnicodeRTF::processText(SWBuf &text, const SWKey *key, const SWModule *module)
36
const unsigned char *from;
40
unsigned char from2[7];
44
from = (const unsigned char *)orig.c_str();
46
// -------------------------------
47
for (text = ""; *from; from++) {
50
if ((*from & 128) != 128) {
54
//case: Invalid UTF-8 (illegal continuing byte in initial position)
55
if ((*from & 128) && ((*from & 64) != 64)) {
58
//case: 2+ byte codepoint
62
for (subsequent = 1; (from2[0] & 128) && (subsequent < 7); subsequent++) {
64
from2[subsequent] = from[subsequent];
65
from2[subsequent] &= 63;
67
ch |= from2[subsequent];
71
char significantFirstBits = 8 - (2+subsequent);
73
ch |= (((short)from2[0]) << (((6*subsequent)+significantFirstBits)-8));
76
utf16 = (signed short)ch;
79
sprintf(digit, "%d", utf16);
84
utf16 = (signed short)((ch - 0x10000) / 0x400 + 0xD800);
87
sprintf(digit, "%d", utf16);
90
utf16 = (signed short)((ch - 0x10000) % 0x400 + 0xDC00);
93
sprintf(digit, "%d", utf16);