| 1 |
/* |
| 2 |
* Copyright (C) 2005 to 2013 by Jonathan Duddington |
| 3 |
* email: [email protected] |
| 4 |
* Copyright (C) 2013-2017 Reece H. Dunn |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, see: <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "espeak__config.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <locale.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include "wctype_portable.h" |
| 32 |
|
| 33 |
#include "espeak_ng.h" |
| 34 |
#include "speak_lib.h" |
| 35 |
#include "encoding.h" |
| 36 |
#include "ucd.h" |
| 37 |
|
| 38 |
#include "common.h" |
| 39 |
#include "translate.h" |
| 40 |
|
| 41 |
#include "espeak_praat.h" // for FileInMemory |
| 42 |
|
| 43 |
#pragma GCC visibility push(default) |
| 44 |
|
| 45 |
int GetFileLength(const char *filename) |
| 46 |
{ |
| 47 |
struct stat statbuf; |
| 48 |
if (FileInMemorySet_stat(theEspeakPraatFileInMemorySet(), filename, &statbuf) != 0) |
| 49 |
return -errno; |
| 50 |
if (S_ISDIR(statbuf.st_mode)) |
| 51 |
return -EISDIR; |
| 52 |
return statbuf.st_size; |
| 53 |
} |
| 54 |
|
| 55 |
void strncpy0(char *to, const char *from, int size) |
| 56 |
{ |
| 57 |
// strcpy with limit, ensures a zero terminator |
| 58 |
strncpy(to, from, size); |
| 59 |
to[size-1] = 0; |
| 60 |
} |
| 61 |
|
| 62 |
int utf8_in(int *c, const char *buf) |
| 63 |
{ |
| 64 |
/* Read a unicode characater from a UTF8 string |
| 65 |
* Returns the number of UTF8 bytes used. |
| 66 |
* buf: position of buffer is moved, if character is read |
| 67 |
* c: holds UTF-16 representation of multibyte character by |
| 68 |
* skipping UTF-8 header bits of bytes in following way: |
| 69 |
* 2-byte character "ā": |
| 70 |
* hex binary |
| 71 |
* c481 1100010010000001 |
| 72 |
* | 11000100 000001 |
| 73 |
* V \ \ | | |
| 74 |
* 0101 0000000100000001 |
| 75 |
* 3-byte character "ꙅ": |
| 76 |
* ea9985 111010101001100110000101 |
| 77 |
* 1010 011001 000101 |
| 78 |
* | + +--.\ \ | | |
| 79 |
* V `--. \`. `.| | |
| 80 |
* A645 1010011001000101 |
| 81 |
* 4-byte character "𠜎": |
| 82 |
* f0a09c8e 11110000101000001001110010001110 |
| 83 |
* V 000 100000 011100 001110 |
| 84 |
* 02070e 000000100000011100001110 |
| 85 |
*/ |
| 86 |
return utf8_in2(c, buf, 0); |
| 87 |
} |
| 88 |
|
| 89 |
int utf8_out(unsigned int c, char *buf) |
| 90 |
{ |
| 91 |
// write a UTF-16 character into a buffer as UTF-8 |
| 92 |
// returns the number of bytes written |
| 93 |
|
| 94 |
int n_bytes; |
| 95 |
int j; |
| 96 |
int shift; |
| 97 |
static const char unsigned code[4] = { 0, 0xc0, 0xe0, 0xf0 }; |
| 98 |
|
| 99 |
if (c < 0x80) { |
| 100 |
buf[0] = c; |
| 101 |
return 1; |
| 102 |
} |
| 103 |
if (c >= 0x110000) { |
| 104 |
buf[0] = ' '; // out of range character code |
| 105 |
return 1; |
| 106 |
} |
| 107 |
if (c < 0x0800) |
| 108 |
n_bytes = 1; |
| 109 |
else if (c < 0x10000) |
| 110 |
n_bytes = 2; |
| 111 |
else |
| 112 |
n_bytes = 3; |
| 113 |
|
| 114 |
shift = 6*n_bytes; |
| 115 |
buf[0] = code[n_bytes] | (c >> shift); |
| 116 |
for (j = 0; j < n_bytes; j++) { |
| 117 |
shift -= 6; |
| 118 |
buf[j+1] = 0x80 + ((c >> shift) & 0x3f); |
| 119 |
} |
| 120 |
return n_bytes+1; |
| 121 |
} |
| 122 |
|
| 123 |
int utf8_in2(int *c, const char *buf, int backwards) |
| 124 |
{ |
| 125 |
// Reads a unicode characater from a UTF8 string |
| 126 |
// Returns the number of UTF8 bytes used. |
| 127 |
// c: holds integer representation of multibyte character |
| 128 |
// buf: position of buffer is moved, if character is read |
| 129 |
// backwards: set if we are moving backwards through the UTF8 string |
| 130 |
|
| 131 |
int c1; |
| 132 |
int n_bytes; |
| 133 |
static const unsigned char mask[4] = { 0xff, 0x1f, 0x0f, 0x07 }; |
| 134 |
|
| 135 |
// find the start of the next/previous character |
| 136 |
while ((*buf & 0xc0) == 0x80) { |
| 137 |
// skip over non-initial bytes of a multi-byte utf8 character |
| 138 |
if (backwards) |
| 139 |
buf--; |
| 140 |
else |
| 141 |
buf++; |
| 142 |
} |
| 143 |
|
| 144 |
n_bytes = 0; |
| 145 |
|
| 146 |
if ((c1 = *buf++) & 0x80) { |
| 147 |
if ((c1 & 0xe0) == 0xc0) |
| 148 |
n_bytes = 1; |
| 149 |
else if ((c1 & 0xf0) == 0xe0) |
| 150 |
n_bytes = 2; |
| 151 |
else if ((c1 & 0xf8) == 0xf0) |
| 152 |
n_bytes = 3; |
| 153 |
|
| 154 |
c1 &= mask[n_bytes]; |
| 155 |
int ix; |
| 156 |
for (ix = 0; ix < n_bytes; ix++) |
| 157 |
{ |
| 158 |
if (!*buf) |
| 159 |
/* Oops, truncated */ |
| 160 |
break; |
| 161 |
c1 = (c1 << 6) + (*buf++ & 0x3f); |
| 162 |
} |
| 163 |
n_bytes = ix; |
| 164 |
} |
| 165 |
*c = c1; |
| 166 |
return n_bytes+1; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
int IsAlpha(unsigned int c) |
| 171 |
{ |
| 172 |
// Replacement for iswalph() which also checks for some in-word symbols |
| 173 |
|
| 174 |
static const unsigned short extra_indic_alphas[] = { |
| 175 |
0xa70, 0xa71, // Gurmukhi: tippi, addak |
| 176 |
0 |
| 177 |
}; |
| 178 |
|
| 179 |
if (iswalpha_portable(c)) |
| 180 |
return 1; |
| 181 |
|
| 182 |
if (c < 0x300) |
| 183 |
return 0; |
| 184 |
|
| 185 |
if ((c >= 0x901) && (c <= 0xdf7)) { |
| 186 |
// Indic scripts: Devanagari, Tamil, etc |
| 187 |
if ((c & 0x7f) < 0x64) |
| 188 |
return 1; |
| 189 |
if (lookupwchar(extra_indic_alphas, c) != 0) |
| 190 |
return 1; |
| 191 |
if ((c >= 0xd7a) && (c <= 0xd7f)) |
| 192 |
return 1; // malaytalam chillu characters |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
if ((c >= 0x5b0) && (c <= 0x5c2)) |
| 198 |
return 1; // Hebrew vowel marks |
| 199 |
|
| 200 |
if (c == 0x0605) |
| 201 |
return 1; |
| 202 |
|
| 203 |
if ((c == 0x670) || ((c >= 0x64b) && (c <= 0x65e))) |
| 204 |
return 1; // arabic vowel marks |
| 205 |
|
| 206 |
if ((c >= 0x300) && (c <= 0x36f)) |
| 207 |
return 1; // combining accents |
| 208 |
|
| 209 |
if ((c >= 0xf40) && (c <= 0xfbc)) |
| 210 |
return 1; // tibetan |
| 211 |
|
| 212 |
if ((c >= 0x1100) && (c <= 0x11ff)) |
| 213 |
return 1; // Korean jamo |
| 214 |
|
| 215 |
if ((c >= 0x2800) && (c <= 0x28ff)) |
| 216 |
return 1; // braille |
| 217 |
|
| 218 |
if ((c > 0x3040) && (c <= 0xa700)) |
| 219 |
return 1; // Chinese/Japanese. Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
// brackets, also 0x2014 to 0x021f which don't need to be in this list |
| 225 |
static const unsigned short brackets[] = { |
| 226 |
'(', ')', '[', ']', '{', '}', '<', '>', '"', '\'', '`', |
| 227 |
0xab, 0xbb, // double angle brackets |
| 228 |
0x300a, 0x300b, // double angle brackets (ideograph) |
| 229 |
0xe000+'<', // private usage area |
| 230 |
0 |
| 231 |
}; |
| 232 |
|
| 233 |
int IsBracket(int c) |
| 234 |
{ |
| 235 |
if ((c >= 0x2014) && (c <= 0x201f)) |
| 236 |
return 1; |
| 237 |
return lookupwchar(brackets, c); |
| 238 |
} |
| 239 |
|
| 240 |
int IsDigit09(unsigned int c) |
| 241 |
{ |
| 242 |
if ((c >= '0') && (c <= '9')) |
| 243 |
return 1; |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
int IsDigit(unsigned int c) |
| 248 |
{ |
| 249 |
if (iswdigit_portable(c)) |
| 250 |
return 1; |
| 251 |
|
| 252 |
if ((c >= 0x966) && (c <= 0x96f)) |
| 253 |
return 1; |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
int IsSpace(unsigned int c) |
| 259 |
{ |
| 260 |
if (c == 0) |
| 261 |
return 0; |
| 262 |
if ((c >= 0x2500) && (c < 0x25a0)) |
| 263 |
return 1; // box drawing characters |
| 264 |
if ((c >= 0xfff9) && (c <= 0xffff)) |
| 265 |
return 1; // unicode specials |
| 266 |
return iswspace_portable(c); |
| 267 |
} |
| 268 |
|
| 269 |
int isspace2(unsigned int c) |
| 270 |
{ |
| 271 |
// can't use isspace() because on Windows, isspace(0xe1) gives TRUE ! |
| 272 |
if ( ((c & 0xff) == 0) || (c > ' ')) |
| 273 |
return 0; |
| 274 |
return 1; |
| 275 |
} |
| 276 |
|
| 277 |
int is_str_totally_null(const char* str, int size) { |
| 278 |
// Tests if all bytes of str are null up to size |
| 279 |
// This should never be reimplemented with integers, because |
| 280 |
// this function has to work with unaligned char* |
| 281 |
// (casting to int when unaligned may result in ungaranteed behaviors) |
| 282 |
return (*str == 0 && memcmp(str, str+1, size-1) == 0); |
| 283 |
} |
| 284 |
|
| 285 |
int Read4Bytes(FileInMemory f) |
| 286 |
{ |
| 287 |
// Read 4 bytes (least significant first) into a word |
| 288 |
int ix; |
| 289 |
int acc = 0; |
| 290 |
|
| 291 |
for (ix = 0; ix < 4; ix++) { |
| 292 |
unsigned char c; |
| 293 |
c = FileInMemory_fgetc(f) & 0xff; |
| 294 |
acc += (c << (ix*8)); |
| 295 |
} |
| 296 |
return acc; |
| 297 |
} |
| 298 |
|
| 299 |
unsigned int StringToWord(const char *string) |
| 300 |
{ |
| 301 |
// Pack 4 characters into a word |
| 302 |
int ix; |
| 303 |
unsigned char c; |
| 304 |
unsigned int word; |
| 305 |
|
| 306 |
if (string == NULL) |
| 307 |
return 0; |
| 308 |
|
| 309 |
word = 0; |
| 310 |
for (ix = 0; ix < 4; ix++) { |
| 311 |
if (string[ix] == 0) break; |
| 312 |
c = string[ix]; |
| 313 |
word |= (c << (ix*8)); |
| 314 |
} |
| 315 |
return word; |
| 316 |
} |
| 317 |
|
| 318 |
int towlower2(unsigned int c, Translator *trans) |
| 319 |
{ |
| 320 |
// check for non-standard upper to lower case conversions |
| 321 |
if (c == 'I' && trans->langopts.dotless_i) |
| 322 |
return 0x131; // I -> ı |
| 323 |
|
| 324 |
return ucd_tolower(c); |
| 325 |
} |
| 326 |
|
| 327 |
static uint32_t espeak_rand_state = 0; |
| 328 |
|
| 329 |
long espeak_rand(long min, long max) { |
| 330 |
// Ref: https://github.com/bminor/glibc/blob/glibc-2.36/stdlib/random_r.c#L364 |
| 331 |
#if 1 |
| 332 |
espeak_rand_state = (((uint64_t)espeak_rand_state * 1103515245) + 12345) % 0x7fffffff; |
| 333 |
#endif |
| 334 |
long res = (long)espeak_rand_state; |
| 335 |
return (res % (max-min+1))-min; |
| 336 |
} |
| 337 |
|
| 338 |
void espeak_srand(long seed) { |
| 339 |
#if 1 |
| 340 |
espeak_rand_state = (uint32_t)(seed); |
| 341 |
(void)espeak_rand(0, 1); // Dummy flush a generator |
| 342 |
#endif |
| 343 |
} |
| 344 |
|
| 345 |
#pragma GCC visibility push(default) |
| 346 |
ESPEAK_NG_API espeak_ng_STATUS |
| 347 |
espeak_ng_SetRandSeed(long seed) { |
| 348 |
espeak_srand(seed); |
| 349 |
return ENS_OK; |
| 350 |
} |
| 351 |
#pragma GCC visibility pop |