| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91,92 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.1.1.1 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoV1Text2 |
| 47 |
| |
| 48 |
| Author(s) : Gavin Bell |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include "SoV1Text2.h" |
| 55 |
#include <Inventor/nodes/SoText2.h> |
| 56 |
|
| 57 |
#include <iconv.h> |
| 58 |
#include <errno.h> |
| 59 |
|
| 60 |
|
| 61 |
SO_NODE_SOURCE(SoV1Text2); |
| 62 |
|
| 63 |
|
| 64 |
//////////////////////////////////////////////////////////////////////// |
| 65 |
// |
| 66 |
// Description: |
| 67 |
// Default constructor |
| 68 |
// |
| 69 |
// Use: public |
| 70 |
|
| 71 |
SoV1Text2::SoV1Text2() |
| 72 |
// |
| 73 |
//////////////////////////////////////////////////////////////////////// |
| 74 |
{ |
| 75 |
SO_NODE_CONSTRUCTOR(SoV1Text2); |
| 76 |
|
| 77 |
SO_NODE_ADD_FIELD(string, ("")); |
| 78 |
SO_NODE_ADD_FIELD(spacing, (1.0)); |
| 79 |
SO_NODE_ADD_FIELD(justification, (LEFT)); |
| 80 |
|
| 81 |
// Set up static info for enumerated type field |
| 82 |
SO_NODE_DEFINE_ENUM_VALUE(Justification, LEFT); |
| 83 |
SO_NODE_DEFINE_ENUM_VALUE(Justification, RIGHT); |
| 84 |
SO_NODE_DEFINE_ENUM_VALUE(Justification, CENTER); |
| 85 |
|
| 86 |
// Set up info in enumerated type field |
| 87 |
SO_NODE_SET_SF_ENUM_TYPE(justification, Justification); |
| 88 |
|
| 89 |
isBuiltIn = TRUE; |
| 90 |
} |
| 91 |
|
| 92 |
//////////////////////////////////////////////////////////////////////// |
| 93 |
// |
| 94 |
// Description: |
| 95 |
// Destructor. |
| 96 |
// |
| 97 |
// Use: private |
| 98 |
|
| 99 |
SoV1Text2::~SoV1Text2() |
| 100 |
// |
| 101 |
//////////////////////////////////////////////////////////////////////// |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
//////////////////////////////////////////////////////////////////////// |
| 106 |
// |
| 107 |
// Description: |
| 108 |
// Create a version 2.0 SoText2. |
| 109 |
// |
| 110 |
// Use: private |
| 111 |
|
| 112 |
SoNode * |
| 113 |
SoV1Text2::createNewNode() |
| 114 |
// |
| 115 |
//////////////////////////////////////////////////////////////////////// |
| 116 |
{ |
| 117 |
SoText2 *result = SO_UPGRADER_CREATE_NEW(SoText2); |
| 118 |
|
| 119 |
|
| 120 |
// if european characters present they are converted to UTF-8 |
| 121 |
for (int i = 0; i < string.getNum(); i++) { |
| 122 |
result->string.set1Value(i, *convertToUTF8(string[i])); |
| 123 |
} |
| 124 |
result->spacing.setValue(spacing.getValue()); |
| 125 |
result->justification.setValue(justification.getValue()); |
| 126 |
|
| 127 |
return result; |
| 128 |
} |
| 129 |
// Following is different for irix 6.2 |
| 130 |
static iconv_t codeConvert1 = NULL; |
| 131 |
static iconv_t codeConvert2 = NULL; |
| 132 |
//////////////////////////////////////////////////////////////////////// |
| 133 |
// |
| 134 |
// Description: |
| 135 |
// Convert a string to UTF-8. If it's ascii, leave it alone. |
| 136 |
// otherwise assume it's ISO-8859-1 (Western European) |
| 137 |
// |
| 138 |
// Use: static, public |
| 139 |
|
| 140 |
const SbString* |
| 141 |
SoV1Text2::convertToUTF8(const SbString &strng) |
| 142 |
// |
| 143 |
//////////////////////////////////////////////////////////////////////// |
| 144 |
{ |
| 145 |
const char* str = strng.getString(); |
| 146 |
SbBool ascii = TRUE; |
| 147 |
for (int i= 0; i< strng.getLength(); i++){ |
| 148 |
if ( str[i]& 0x80) { |
| 149 |
ascii = FALSE; |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
if (ascii) return &strng; |
| 154 |
|
| 155 |
//Do a conversion first to UCS-2, then UTF-8. That's because |
| 156 |
//not all Irix systems have iconv conversion table to go |
| 157 |
//directly to UTF-8 |
| 158 |
|
| 159 |
if( codeConvert1 == NULL){ |
| 160 |
codeConvert1 = iconv_open("UCS-2", "ISO8859-1"); |
| 161 |
codeConvert2 = iconv_open("UTF-8", "UCS-2"); |
| 162 |
#ifdef DEBUG |
| 163 |
if ( codeConvert1 == (iconv_t)-1 ){ |
| 164 |
SoDebugError::post("SoV1Text2::convertToUTF8", |
| 165 |
"Invalid ISO8859-1 to UCS-2 conversion"); |
| 166 |
} |
| 167 |
if ( codeConvert2 == (iconv_t)-1 ){ |
| 168 |
SoDebugError::post("SoV1Text2::convertToUTF8", |
| 169 |
"Invalid UCS-2 to UTF-8 conversion"); |
| 170 |
} |
| 171 |
#endif /*DEBUG*/ |
| 172 |
} |
| 173 |
// allocate a sufficiently large buffer: |
| 174 |
char * UCSBuf = new char[2*strng.getLength()+1]; |
| 175 |
char * UTFBuf = new char[2*strng.getLength()+1]; |
| 176 |
|
| 177 |
char* input = (char *)strng.getString(); |
| 178 |
size_t inbytes = strng.getLength(); |
| 179 |
size_t outbytes = 2*inbytes; |
| 180 |
char* output = (char*)UCSBuf; |
| 181 |
if ((iconv(codeConvert1, &input, &inbytes, &output, &outbytes) != NULL)){ |
| 182 |
#ifdef DEBUG |
| 183 |
SoDebugError::post("SoV1Text2::convertToUTF8", |
| 184 |
"Error converting text to UCS-2"); |
| 185 |
#endif /*DEBUG*/ |
| 186 |
} |
| 187 |
input = (char *)UCSBuf; |
| 188 |
outbytes = 2*strng.getLength()+1; |
| 189 |
inbytes = 2*strng.getLength(); |
| 190 |
output = (char*)UTFBuf; |
| 191 |
if ((iconv(codeConvert2, &input, &inbytes, &output, &outbytes) != 0)){ |
| 192 |
#ifdef DEBUG |
| 193 |
switch(errno){ |
| 194 |
case EILSEQ: printf("EILSEQ\n"); |
| 195 |
break; |
| 196 |
case EINVAL: printf("EINVAL\n"); |
| 197 |
break; |
| 198 |
case E2BIG: printf("E2BIG\n"); |
| 199 |
break; |
| 200 |
default: printf("errno %d\n", errno); |
| 201 |
} |
| 202 |
SoDebugError::post("SoV1Text2::convertToUTF8", |
| 203 |
"Error converting text to UTF-8"); |
| 204 |
|
| 205 |
#endif /*DEBUG*/ |
| 206 |
} |
| 207 |
|
| 208 |
delete [] UCSBuf; |
| 209 |
SbString* str1 = new SbString(UTFBuf, 0, 2*strng.getLength()-outbytes); |
| 210 |
return (str1); |
| 211 |
} |