1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/* GNU Ocrad - Optical Character Recognition program
Copyright (C) 2003-2024 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cctype>
#include "ucs.h"
int UCS::base_letter( const int code )
{
switch( code )
{
case CAGRAVE:
case CAACUTE:
case CACIRCU:
case CATILDE:
case CADIAER:
case CARING : return 'A';
case CCCEDI : return 'C';
case CEGRAVE:
case CEACUTE:
case CECIRCU:
case CEDIAER: return 'E';
case CGBREVE: return 'G';
case CIGRAVE:
case CIACUTE:
case CICIRCU:
case CIDIAER:
case CIDOT : return 'I';
case CNTILDE: return 'N';
case COGRAVE:
case COACUTE:
case COCIRCU:
case COTILDE:
case CODIAER: return 'O';
case CSCEDI :
case CSCARON: return 'S';
case CUGRAVE:
case CUACUTE:
case CUCIRCU:
case CUDIAER: return 'U';
case CYACUTE:
case CYDIAER: return 'Y';
case CZCARON: return 'Z';
case SAGRAVE:
case SAACUTE:
case SACIRCU:
case SATILDE:
case SADIAER:
case SARING : return 'a';
case SCCEDI : return 'c';
case SEGRAVE:
case SEACUTE:
case SECIRCU:
case SEDIAER: return 'e';
case SGBREVE: return 'g';
case SIGRAVE:
case SIACUTE:
case SICIRCU:
case SIDIAER:
case SINODOT: return 'i';
case SNTILDE: return 'n';
case SOGRAVE:
case SOACUTE:
case SOCIRCU:
case SOTILDE:
case SODIAER: return 'o';
case SSCEDI :
case SSCARON: return 's';
case SUGRAVE:
case SUACUTE:
case SUCIRCU:
case SUDIAER: return 'u';
case SYACUTE:
case SYDIAER: return 'y';
case SZCARON: return 'z';
default: return 0;
}
}
int UCS::compose( const int letter, const int accent )
{
switch( letter )
{
case 'A': if( accent == '\'') return CAACUTE;
if( accent == '`' ) return CAGRAVE;
if( accent == '^' ) return CACIRCU;
if( accent == ':' ) { return CADIAER; } break;
case 'E': if( accent == '\'') return CEACUTE;
if( accent == '`' ) return CEGRAVE;
if( accent == '^' ) return CECIRCU;
if( accent == ':' ) { return CEDIAER; } break;
case 'G': return CGBREVE;
case '[':
case 'I': if( accent == '\'') return CIACUTE;
if( accent == '`' ) return CIGRAVE;
if( accent == '^' ) return CICIRCU;
if( accent == ':' ) { return CIDIAER; } break;
case 'N': if( accent != ':' ) return CNTILDE; break;
case 'O': if( accent == '\'') return COACUTE;
if( accent == '`' ) return COGRAVE;
if( accent == '^' ) return COCIRCU;
if( accent == ':' ) { return CODIAER; } break;
case 'S': return CSCARON;
case 'U':
case 'V': if( accent == '\'') return CUACUTE;
if( accent == '`' ) return CUGRAVE;
if( accent == '^' ) return CUCIRCU;
if( accent == ':' ) { return CUDIAER; } break;
case 'Y': if( accent == '\'') return CYACUTE;
if( accent == ':' ) { return CYDIAER; } break;
case 'Z': return CZCARON;
case 'a': if( accent == '\'') return SAACUTE;
if( accent == '`' ) return SAGRAVE;
if( accent == '^' ) return SACIRCU;
if( accent == ':' ) { return SADIAER; } break;
case 'e': if( accent == '\'') return SEACUTE;
if( accent == '`' ) return SEGRAVE;
if( accent == '^' ) return SECIRCU;
if( accent == ':' ) { return SEDIAER; } break;
case '9':
case 'g': return SGBREVE;
case '|':
case ']':
case 'i':
case 'l': if( accent == '\'') return SIACUTE;
if( accent == '`' ) return SIGRAVE;
if( accent == '^' ) return SICIRCU;
if( accent == ':' ) { return SIDIAER; } break;
case 'n': if( accent != ':' ) return SNTILDE; break;
case 'o': if( accent == '\'') return SOACUTE;
if( accent == '`' ) return SOGRAVE;
if( accent == '^' ) return SOCIRCU;
if( accent == ':' ) { return SODIAER; } break;
case 's': return SSCARON;
case 'u':
case 'v': if( accent == '\'') return SUACUTE;
if( accent == '`' ) return SUGRAVE;
if( accent == '^' ) return SUCIRCU;
if( accent == ':' ) { return SUDIAER; } break;
case 'y': if( accent == '\'') return SYACUTE;
if( accent == ':' ) { return SYDIAER; } break;
case 'z': return SZCARON;
}
return 0;
}
bool UCS::isalnum( const int code )
{
return ( UCS::isalpha( code ) || UCS::isdigit( code ) );
}
bool UCS::isalpha( const int code )
{
return ( ( code < 128 && std::isalpha( code ) ) || base_letter( code ) );
}
bool UCS::ishigh( const int code )
{
if( isupper( code ) || isdigit( code ) ) return true;
switch( code )
{
case 'b': case 'd': case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'p': case 'q': case 't': case 'y': case '|':
return true;
default: return false;
}
}
bool UCS::islower( const int code )
{
if( code < 128 && std::islower( code ) ) return true;
const int base = base_letter( code );
return ( base && std::islower( base ) );
}
bool UCS::islower_ambiguous( const int code )
{
if( islower_small_ambiguous( code ) ) return true;
switch( code )
{
case 'k': case 'p': case SCCEDI:
case SIGRAVE: case SIACUTE: case SICIRCU: case SIDIAER:
case SOGRAVE: case SOACUTE: case SOCIRCU: case SOTILDE: case SODIAER:
case SUGRAVE: case SUACUTE: case SUCIRCU: case SUDIAER:
case SSCEDI: case SSCARON: case SZCARON: return true;
default: return false;
}
}
bool UCS::islower_small( const int code )
{
if( code >= 128 || !std::islower( code ) ) return false;
switch( code )
{
case 'a': case 'c': case 'e': case 'm': case 'n': case 'o': case 'r':
case 's': case 'u': case 'v': case 'w': case 'x': case 'z': return true;
default: return false;
}
}
bool UCS::islower_small_ambiguous( const int code )
{
if( code >= 128 || !std::islower( code ) ) return false;
switch( code )
{
case 'c': case 'o': case 's': case 'u': case 'v': case 'w':
case 'x': case 'z': return true;
default: return false;
}
}
bool UCS::isspace( const int code )
{
return ( code < 128 && std::isspace( code ) ) || code == 0xA0;
}
bool UCS::isupper( const int code )
{
if( code < 128 && std::isupper( code ) ) return true;
const int base = base_letter( code );
return ( base && std::isupper( base ) );
}
bool UCS::isupper_normal_width( const int code )
{
if( code >= 128 || !std::isupper( code ) ) return false;
switch( code )
{
case 'I': case 'J': case 'L': case 'M': case 'Q': case 'W': return false;
default: return true;
}
}
bool UCS::isvowel( int code )
{
if( code >= 128 ) code = base_letter( code );
if( !code || !std::isalpha( code ) ) return false;
code = std::tolower( code );
return ( code == 'a' || code == 'e' || code == 'i' ||
code == 'o' || code == 'u' );
}
unsigned char UCS::map_to_byte( const int code )
{
if( code < 0 ) return 0;
if( code < 256 ) return code;
switch( code )
{
case CGBREVE: return 0xD0;
case SGBREVE: return 0xF0;
case CIDOT : return 0xDD;
case SINODOT: return 0xFD;
case CSCEDI : return 0xDE;
case SSCEDI : return 0xFE;
case CSCARON: return 0xA6;
case SSCARON: return 0xA8;
case CYDIAER: return 0xBE;
case CZCARON: return 0xB4;
case SZCARON: return 0xB8;
case EURO : return 0xA4;
default : return 0;
}
}
int UCS::map_to_ucs( const unsigned char ch )
{
switch( ch )
{
case 0xA4: return EURO;
case 0xA6: return CSCARON;
case 0xA8: return SSCARON;
case 0xB4: return CZCARON;
case 0xB8: return SZCARON;
case 0xBC: return CLIGOE;
case 0xBD: return SLIGOE;
case 0xBE: return CYDIAER;
}
return ch;
}
// does not work for 'code' == 0
const char * UCS::ucs_to_utf8( const int code )
{
static char s[7];
if( code < 0 || code > 0x7FFFFFFF ) { s[0] = 0; return s; } // invalid code
if( code < 128 ) { s[0] = code; s[1] = 0; return s; } // plain ascii
int i, mask;
if( code < 0x800 ) { i = 2; mask = 0xC0; } // 110X XXXX
else if( code < 0x10000 ) { i = 3; mask = 0xE0; } // 1110 XXXX
else if( code < 0x200000 ) { i = 4; mask = 0xF0; } // 1111 0XXX
else if( code < 0x4000000 ) { i = 5; mask = 0xF8; } // 1111 10XX
else { i = 6; mask = 0xFC; } // 1111 110X
s[i] = 0; --i;
int d = 0;
for( ; i > 0; --i, d += 6 )
s[i] = 0x80 | ( ( code >> d ) & 0x3F ); // 10XX XXXX
s[0] = mask | ( code >> d );
return s;
}
int UCS::to_nearest_digit( const int code )
{
switch( code )
{
case 'D':
case 'O':
case 'Q':
case 'o': return '0';
case 'I':
case 'L':
case 'l':
case '|':
case SINODOT: return '1';
case 'Z':
case 'z': return '2';
case 'A':
case 'q': return '4';
case 'S':
case 's': return '5';
case 'G':
case 'b':
case SOACUTE: return '6';
case 'J':
case 'T': return '7';
case '&':
case 'B': return '8';
case 'g': return '9';
default: return code;
}
}
int UCS::to_nearest_letter( const int code )
{
switch( code )
{
case '0': return 'O';
case '1': return 'l';
case '2': return 'Z';
case '4': return 'q';
case '5': return 'S';
case '6': return SOACUTE;
case '7': return 'I';
case '8': return 'B';
case '9': return 'g';
default: return code;
}
}
int UCS::to_nearest_upper_num( const int code )
{
switch( code )
{
case '(':
case '[': return 'C';
case 'l':
case '|': return 'I';
case DEG: return 'O';
case MICRO: return 'U';
case POW1:
case SINODOT: return '1';
case POW2: return '2';
case POW3: return '3';
case 'q': return '4';
case 'b':
case SOACUTE: return '6';
case '&': return '8';
case 'g':
case MASCORD: return '9';
}
if( islower_ambiguous( code ) ) return toupper( code );
return code;
}
int UCS::toupper( const int code )
{
if( code < 128 ) return std::toupper( code );
switch( code )
{
case SAGRAVE: return CAGRAVE;
case SAACUTE: return CAACUTE;
case SACIRCU: return CACIRCU;
case SATILDE: return CATILDE;
case SADIAER: return CADIAER;
case SARING : return CARING;
case SCCEDI : return CCCEDI;
case SEGRAVE: return CEGRAVE;
case SEACUTE: return CEACUTE;
case SECIRCU: return CECIRCU;
case SEDIAER: return CEDIAER;
case SGBREVE: return CGBREVE;
case SIGRAVE: return CIGRAVE;
case SIACUTE: return CIACUTE;
case SICIRCU: return CICIRCU;
case SIDIAER: return CIDIAER;
case SNTILDE: return CNTILDE;
case SOGRAVE: return COGRAVE;
case SOACUTE: return COACUTE;
case SOCIRCU: return COCIRCU;
case SOTILDE: return COTILDE;
case SODIAER: return CODIAER;
case SSCEDI : return CSCEDI;
case SSCARON: return CSCARON;
case SUGRAVE: return CUGRAVE;
case SUACUTE: return CUACUTE;
case SUCIRCU: return CUCIRCU;
case SUDIAER: return CUDIAER;
case SYACUTE: return CYACUTE;
case SYDIAER: return CYDIAER;
case SZCARON: return CZCARON;
default: return code;
}
}
|