| 1 |
/* |
| 2 |
|
| 3 |
hyphen.c |
| 4 |
|
| 5 |
This file is derived from libhnj which is is dual licensed under LGPL and MPL. |
| 6 |
Boilerplate for both licenses follows. |
| 7 |
|
| 8 |
LibHnj - a library for high quality hyphenation and justification |
| 9 |
|
| 10 |
(C) 1998 Raph Levien, |
| 11 |
(C) 2001 ALTLinux, Moscow (http://www.alt-linux.org), |
| 12 |
(C) 2001 Peter Novodvorsky ([email protected]) |
| 13 |
|
| 14 |
This library is free software; you can redistribute it and/or modify it under the |
| 15 |
terms of the GNU Library General Public License as published by the Free Software |
| 16 |
Foundation; either version 2 of the License, or (at your option) any later |
| 17 |
version. |
| 18 |
|
| 19 |
This library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 20 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 21 |
PARTICULAR PURPOSE. See the GNU Library General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU Library General Public License along |
| 24 |
with this library; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 25 |
Place - Suite 330, Boston, MA 02111-1307 USA. |
| 26 |
|
| 27 |
The contents of this file are subject to the Mozilla Public License Version 1.0 |
| 28 |
(the "MPL"); you may not use this file except in compliance with the MPL. You may |
| 29 |
obtain a copy of the MPL at http://www.mozilla.org/MPL/ |
| 30 |
|
| 31 |
Software distributed under the MPL is distributed on an "AS IS" basis, WITHOUT |
| 32 |
WARRANTY OF ANY KIND, either express or implied. See the MPL for the specific |
| 33 |
language governing rights and limitations under the MPL. |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
#include "ptexlib.h" |
| 38 |
#include "lua/luatex-api.h" |
| 39 |
|
| 40 |
#include <stdlib.h> /* for NULL, malloc */ |
| 41 |
#include <stdio.h> /* for fprintf */ |
| 42 |
#include <string.h> /* for strdup */ |
| 43 |
#include <stdlib.h> /* for malloc used by substring inclusion */ |
| 44 |
|
| 45 |
#define MAXPATHS 40960 |
| 46 |
|
| 47 |
#ifdef UNX |
| 48 |
# include <unistd.h> /* for exit */ |
| 49 |
#endif |
| 50 |
|
| 51 |
#include <kpathsea/c-ctype.h> |
| 52 |
|
| 53 |
#define noVERBOSE |
| 54 |
|
| 55 |
#include "lang/hnjalloc.h" |
| 56 |
|
| 57 |
/*tex This could be moved to separate library. */ |
| 58 |
|
| 59 |
static unsigned char *hnj_strdup(const unsigned char *s) |
| 60 |
{ |
| 61 |
unsigned char *new; |
| 62 |
size_t l; |
| 63 |
|
| 64 |
l = strlen((const char *) s); |
| 65 |
new = hnj_malloc((int) l + 1); |
| 66 |
memcpy(new, s, l); |
| 67 |
new[l] = 0; |
| 68 |
return new; |
| 69 |
} |
| 70 |
|
| 71 |
/*tex |
| 72 |
|
| 73 |
First some type definitions and a little bit of a hash table implementation. |
| 74 |
This simply maps strings to state numbers |
| 75 |
|
| 76 |
*/ |
| 77 |
|
| 78 |
typedef struct _HashTab HashTab; |
| 79 |
typedef struct _HashEntry HashEntry; |
| 80 |
typedef struct _HashIter HashIter; |
| 81 |
typedef union _HashVal HashVal; |
| 82 |
|
| 83 |
/*tex A cheap, but effective, hack. */ |
| 84 |
|
| 85 |
#define HASH_SIZE 31627 |
| 86 |
|
| 87 |
struct _HashTab { |
| 88 |
HashEntry *entries[HASH_SIZE]; |
| 89 |
}; |
| 90 |
|
| 91 |
union _HashVal { |
| 92 |
int state; |
| 93 |
char *hyppat; |
| 94 |
}; |
| 95 |
|
| 96 |
struct _HashEntry { |
| 97 |
HashEntry *next; |
| 98 |
unsigned char *key; |
| 99 |
HashVal u; |
| 100 |
}; |
| 101 |
|
| 102 |
struct _HashIter { |
| 103 |
HashEntry **e; |
| 104 |
HashEntry *cur; |
| 105 |
int ndx; |
| 106 |
}; |
| 107 |
|
| 108 |
/* The state state machine. */ |
| 109 |
|
| 110 |
typedef struct _HyphenState HyphenState; |
| 111 |
typedef struct _HyphenTrans HyphenTrans; |
| 112 |
|
| 113 |
#define MAX_CHARS 256 |
| 114 |
#define MAX_NAME 20 |
| 115 |
|
| 116 |
struct _HyphenDict { |
| 117 |
int num_states; |
| 118 |
int pat_length; |
| 119 |
char cset[MAX_NAME]; |
| 120 |
HyphenState *states; |
| 121 |
HashTab *patterns; |
| 122 |
HashTab *merged; |
| 123 |
HashTab *state_num; |
| 124 |
}; |
| 125 |
|
| 126 |
struct _HyphenState { |
| 127 |
char *match; |
| 128 |
/*tex Removed: |
| 129 |
|
| 130 |
\starttyping |
| 131 |
char *repl; |
| 132 |
signed char replindex; |
| 133 |
signed char replcut; |
| 134 |
\stoptyping |
| 135 |
*/ |
| 136 |
int fallback_state; |
| 137 |
int num_trans; |
| 138 |
HyphenTrans *trans; |
| 139 |
}; |
| 140 |
|
| 141 |
struct _HyphenTrans { |
| 142 |
int uni_ch; |
| 143 |
int new_state; |
| 144 |
}; |
| 145 |
|
| 146 |
/*tex |
| 147 |
|
| 148 |
Combine two right-aligned number patterns, 04000 + 020 becomes 04020. This |
| 149 |
works also for utf8 sequences because the substring is identical to the last |
| 150 |
substring-length bytes of expr except for the (single byte) hyphenation |
| 151 |
encoders |
| 152 |
|
| 153 |
*/ |
| 154 |
|
| 155 |
static char *combine(char *expr, const char *subexpr) |
| 156 |
{ |
| 157 |
size_t l1 = strlen(expr); |
| 158 |
size_t l2 = strlen(subexpr); |
| 159 |
size_t off = l1 - l2; |
| 160 |
unsigned j; |
| 161 |
for (j = 0; j < l2; j++) { |
| 162 |
if (expr[off + j] < subexpr[j]) |
| 163 |
expr[off + j] = subexpr[j]; |
| 164 |
} |
| 165 |
return expr; |
| 166 |
} |
| 167 |
|
| 168 |
/*tex Some original code: */ |
| 169 |
|
| 170 |
static HashIter *new_HashIter(HashTab * h) |
| 171 |
{ |
| 172 |
HashIter *i = hnj_malloc(sizeof(HashIter)); |
| 173 |
i->e = h->entries; |
| 174 |
i->cur = NULL; |
| 175 |
i->ndx = -1; |
| 176 |
return i; |
| 177 |
} |
| 178 |
|
| 179 |
static int nextHashStealPattern(HashIter * i, unsigned char **word, char **pattern) |
| 180 |
{ |
| 181 |
while (i->cur == NULL) { |
| 182 |
if (i->ndx >= HASH_SIZE - 1) |
| 183 |
return 0; |
| 184 |
i->cur = i->e[++i->ndx]; |
| 185 |
} |
| 186 |
*word = i->cur->key; |
| 187 |
*pattern = i->cur->u.hyppat; |
| 188 |
i->cur->u.hyppat = NULL; |
| 189 |
i->cur = i->cur->next; |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
static int nextHash(HashIter * i, unsigned char **word) |
| 195 |
{ |
| 196 |
while (i->cur == NULL) { |
| 197 |
if (i->ndx >= HASH_SIZE - 1) |
| 198 |
return 0; |
| 199 |
i->cur = i->e[++i->ndx]; |
| 200 |
} |
| 201 |
*word = i->cur->key; |
| 202 |
i->cur = i->cur->next; |
| 203 |
return 1; |
| 204 |
} |
| 205 |
|
| 206 |
static int eachHash(HashIter * i, unsigned char **word, char **pattern) |
| 207 |
{ |
| 208 |
while (i->cur == NULL) { |
| 209 |
if (i->ndx >= HASH_SIZE - 1) |
| 210 |
return 0; |
| 211 |
i->cur = i->e[++i->ndx]; |
| 212 |
} |
| 213 |
*word = i->cur->key; |
| 214 |
*pattern = i->cur->u.hyppat; |
| 215 |
i->cur = i->cur->next; |
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
static void delete_HashIter(HashIter * i) |
| 220 |
{ |
| 221 |
hnj_free(i); |
| 222 |
} |
| 223 |
|
| 224 |
/*tex |
| 225 |
|
| 226 |
A |char*| hash function from ASU, adapted from |Gtk+|: |
| 227 |
|
| 228 |
*/ |
| 229 |
|
| 230 |
static unsigned int hnj_string_hash(const unsigned char *s) |
| 231 |
{ |
| 232 |
const unsigned char *p; |
| 233 |
unsigned int h = 0, g; |
| 234 |
|
| 235 |
for (p = s; *p != '\0'; p += 1) { |
| 236 |
h = (h << 4) + *p; |
| 237 |
if ((g = (h & 0xf0000000))) { |
| 238 |
h = h ^ (g >> 24); |
| 239 |
h = h ^ g; |
| 240 |
} |
| 241 |
} |
| 242 |
return h; |
| 243 |
} |
| 244 |
|
| 245 |
/*tex This assumes that key is not already present! */ |
| 246 |
|
| 247 |
static void state_insert(HashTab * hashtab, unsigned char *key, int state) |
| 248 |
{ |
| 249 |
int i; |
| 250 |
HashEntry *e; |
| 251 |
i = (int) (hnj_string_hash(key) % HASH_SIZE); |
| 252 |
e = hnj_malloc(sizeof(HashEntry)); |
| 253 |
e->next = hashtab->entries[i]; |
| 254 |
e->key = key; |
| 255 |
e->u.state = state; |
| 256 |
hashtab->entries[i] = e; |
| 257 |
} |
| 258 |
|
| 259 |
/*tex This also assumes that key is not already present! */ |
| 260 |
|
| 261 |
static void hyppat_insert(HashTab * hashtab, unsigned char *key, char *hyppat) |
| 262 |
{ |
| 263 |
int i; |
| 264 |
HashEntry *e; |
| 265 |
i = (int) (hnj_string_hash(key) % HASH_SIZE); |
| 266 |
for (e = hashtab->entries[i]; e; e = e->next) { |
| 267 |
if (strcmp((char *) e->key, (char *) key) == 0) { |
| 268 |
if (e->u.hyppat) { |
| 269 |
if (hyppat && strcmp((char *) e->u.hyppat, (char *) hyppat) != 0) { |
| 270 |
normal_warning("hyphenation","a conflicting pattern has been ignored"); |
| 271 |
} |
| 272 |
hnj_free(e->u.hyppat); |
| 273 |
} |
| 274 |
e->u.hyppat = hyppat; |
| 275 |
hnj_free(key); |
| 276 |
return; |
| 277 |
} |
| 278 |
} |
| 279 |
e = hnj_malloc(sizeof(HashEntry)); |
| 280 |
e->next = hashtab->entries[i]; |
| 281 |
e->key = key; |
| 282 |
e->u.hyppat = hyppat; |
| 283 |
hashtab->entries[i] = e; |
| 284 |
} |
| 285 |
|
| 286 |
/*tex We return |state| if found, otherwise |-1|. */ |
| 287 |
|
| 288 |
static int state_lookup(HashTab * hashtab, const unsigned char *key) |
| 289 |
{ |
| 290 |
int i; |
| 291 |
HashEntry *e; |
| 292 |
i = (int) (hnj_string_hash(key) % HASH_SIZE); |
| 293 |
for (e = hashtab->entries[i]; e; e = e->next) { |
| 294 |
if (!strcmp((const char *) key, (const char *) e->key)) { |
| 295 |
return e->u.state; |
| 296 |
} |
| 297 |
} |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
/*tex We return |state| if found, otherwise |-1|. */ |
| 302 |
|
| 303 |
static char *hyppat_lookup(HashTab * hashtab, const unsigned char *chars, int l) |
| 304 |
{ |
| 305 |
int i; |
| 306 |
HashEntry *e; |
| 307 |
/*tex The 256 should be enough. */ |
| 308 |
unsigned char key[256]; |
| 309 |
strncpy((char *) key, (const char *) chars, (size_t) l); |
| 310 |
key[l] = 0; |
| 311 |
i = (int) (hnj_string_hash(key) % HASH_SIZE); |
| 312 |
for (e = hashtab->entries[i]; e; e = e->next) { |
| 313 |
if (!strcmp((char *) key, (char *) e->key)) { |
| 314 |
return e->u.hyppat; |
| 315 |
} |
| 316 |
} |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
/*tex Get the state number, allocating a new state if necessary. */ |
| 321 |
|
| 322 |
static int hnj_get_state(HyphenDict * dict, const unsigned char *str, int *state_num) |
| 323 |
{ |
| 324 |
*state_num = state_lookup(dict->state_num, str); |
| 325 |
if (*state_num >= 0) |
| 326 |
return *state_num; |
| 327 |
state_insert(dict->state_num, hnj_strdup(str), dict->num_states); |
| 328 |
/*tex The predicate is true if |dict->num_states| is a power of two: */ |
| 329 |
if (!(dict->num_states & (dict->num_states - 1))) { |
| 330 |
dict->states = hnj_realloc(dict->states, |
| 331 |
(int) ((dict->num_states << 1) * (int) sizeof(HyphenState))); |
| 332 |
} |
| 333 |
dict->states[dict->num_states].match = NULL; |
| 334 |
dict->states[dict->num_states].fallback_state = -1; |
| 335 |
dict->states[dict->num_states].num_trans = 0; |
| 336 |
dict->states[dict->num_states].trans = NULL; |
| 337 |
return dict->num_states++; |
| 338 |
} |
| 339 |
|
| 340 |
/*tex |
| 341 |
|
| 342 |
Add a transition from state1 to state2 through ch - assumes that the |
| 343 |
transition does not already exist. |
| 344 |
|
| 345 |
*/ |
| 346 |
|
| 347 |
static void hnj_add_trans(HyphenDict * dict, int state1, int state2, int uni_ch) |
| 348 |
{ |
| 349 |
int num_trans; |
| 350 |
/* |
| 351 |
TH: this test was a bit too strict, it is quite normal for old patterns |
| 352 |
to have chars in the range 0-31 or 127-159 (inclusive). To ease the |
| 353 |
transition, let's only disallow |NUL| for now, which probably is a |
| 354 |
requirement of the code anyway. |
| 355 |
*/ |
| 356 |
if (uni_ch == 0) { |
| 357 |
formatted_error("hyphenation","a character is out of bounds: u%04x", uni_ch); |
| 358 |
} |
| 359 |
num_trans = dict->states[state1].num_trans; |
| 360 |
if (num_trans == 0) { |
| 361 |
dict->states[state1].trans = hnj_malloc(sizeof(HyphenTrans)); |
| 362 |
} else { |
| 363 |
/* |
| 364 |
TH: The old version did: |
| 365 |
|
| 366 |
\starttyping |
| 367 |
} else if (!(num_trans & (num_trans - 1))) { |
| 368 |
... =hnj_realloc(dict->states[state1].trans, |
| 369 |
(int) ((num_trans << 1) * sizeof(HyphenTrans))); |
| 370 |
\stoptyping |
| 371 |
|
| 372 |
but that is incredibly nasty when adding patters one-at-a-time. |
| 373 |
Controlled growth would be nicer than the current +1, but if no one |
| 374 |
complains, and no one did in a decade, this is good enough. |
| 375 |
|
| 376 |
*/ |
| 377 |
dict->states[state1].trans = hnj_realloc(dict->states[state1].trans, |
| 378 |
(int) ((num_trans + 1) * sizeof(HyphenTrans))); |
| 379 |
} |
| 380 |
dict->states[state1].trans[num_trans].uni_ch = uni_ch; |
| 381 |
dict->states[state1].trans[num_trans].new_state = state2; |
| 382 |
dict->states[state1].num_trans++; |
| 383 |
} |
| 384 |
|
| 385 |
/*tex |
| 386 |
|
| 387 |
We did change the semantics a bit here: |hnj_hyphen_load| used to operate on |
| 388 |
a file, but now the argument is a string buffer. |
| 389 |
|
| 390 |
*/ |
| 391 |
|
| 392 |
static const unsigned char *next_pattern(size_t * length, const unsigned char **buf) |
| 393 |
{ |
| 394 |
const unsigned char *here, *rover = *buf; |
| 395 |
while (*rover && isspace(*rover)) |
| 396 |
rover++; |
| 397 |
here = rover; |
| 398 |
while (*rover) { |
| 399 |
if (isspace(*rover)) { |
| 400 |
*length = (size_t) (rover - here); |
| 401 |
*buf = rover; |
| 402 |
return here; |
| 403 |
} |
| 404 |
rover++; |
| 405 |
} |
| 406 |
*length = (size_t) (rover - here); |
| 407 |
*buf = rover; |
| 408 |
/*tex Zero sensed: */ |
| 409 |
return *length ? here : NULL; |
| 410 |
} |
| 411 |
|
| 412 |
static void init_hash(HashTab ** h) |
| 413 |
{ |
| 414 |
int i; |
| 415 |
if (*h) { |
| 416 |
return; |
| 417 |
} |
| 418 |
*h = hnj_malloc(sizeof(HashTab)); |
| 419 |
for (i = 0; i < HASH_SIZE; i++) { |
| 420 |
(*h)->entries[i] = NULL; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
static void clear_state_hash(HashTab ** h) |
| 425 |
{ |
| 426 |
int i; |
| 427 |
if (*h == NULL) { |
| 428 |
return; |
| 429 |
} |
| 430 |
for (i = 0; i < HASH_SIZE; i++) { |
| 431 |
HashEntry *e, *next; |
| 432 |
for (e = (*h)->entries[i]; e; e = next) { |
| 433 |
next = e->next; |
| 434 |
hnj_free(e->key); |
| 435 |
hnj_free(e); |
| 436 |
} |
| 437 |
} |
| 438 |
hnj_free(*h); |
| 439 |
*h = NULL; |
| 440 |
} |
| 441 |
|
| 442 |
static void clear_hyppat_hash(HashTab ** h) |
| 443 |
{ |
| 444 |
int i; |
| 445 |
if (*h == NULL) { |
| 446 |
return; |
| 447 |
} |
| 448 |
for (i = 0; i < HASH_SIZE; i++) { |
| 449 |
HashEntry *e, *next; |
| 450 |
for (e = (*h)->entries[i]; e; e = next) { |
| 451 |
next = e->next; |
| 452 |
hnj_free(e->key); |
| 453 |
if (e->u.hyppat) { |
| 454 |
hnj_free(e->u.hyppat); |
| 455 |
} |
| 456 |
hnj_free(e); |
| 457 |
} |
| 458 |
} |
| 459 |
hnj_free(*h); |
| 460 |
*h = NULL; |
| 461 |
} |
| 462 |
|
| 463 |
static void init_dict(HyphenDict * dict) |
| 464 |
{ |
| 465 |
dict->num_states = 1; |
| 466 |
dict->pat_length = 0; |
| 467 |
dict->states = hnj_malloc(sizeof(HyphenState)); |
| 468 |
dict->states[0].match = NULL; |
| 469 |
dict->states[0].fallback_state = -1; |
| 470 |
dict->states[0].num_trans = 0; |
| 471 |
dict->states[0].trans = NULL; |
| 472 |
dict->patterns = NULL; |
| 473 |
dict->merged = NULL; |
| 474 |
dict->state_num = NULL; |
| 475 |
init_hash(&dict->patterns); |
| 476 |
} |
| 477 |
|
| 478 |
static void clear_dict(HyphenDict * dict) |
| 479 |
{ |
| 480 |
int state_num; |
| 481 |
for (state_num = 0; state_num < dict->num_states; state_num++) { |
| 482 |
HyphenState *hstate = &dict->states[state_num]; |
| 483 |
if (hstate->match) |
| 484 |
hnj_free(hstate->match); |
| 485 |
if (hstate->trans) |
| 486 |
hnj_free(hstate->trans); |
| 487 |
} |
| 488 |
hnj_free(dict->states); |
| 489 |
clear_hyppat_hash(&dict->patterns); |
| 490 |
clear_hyppat_hash(&dict->merged); |
| 491 |
clear_state_hash(&dict->state_num); |
| 492 |
} |
| 493 |
|
| 494 |
HyphenDict *hnj_hyphen_new(void) |
| 495 |
{ |
| 496 |
HyphenDict *dict = hnj_malloc(sizeof(HyphenDict)); |
| 497 |
init_dict(dict); |
| 498 |
return dict; |
| 499 |
} |
| 500 |
|
| 501 |
void hnj_hyphen_clear(HyphenDict * dict) |
| 502 |
{ |
| 503 |
clear_dict(dict); |
| 504 |
init_dict(dict); |
| 505 |
} |
| 506 |
|
| 507 |
void hnj_hyphen_free(HyphenDict * dict) |
| 508 |
{ |
| 509 |
clear_dict(dict); |
| 510 |
hnj_free(dict); |
| 511 |
} |
| 512 |
|
| 513 |
unsigned char *hnj_serialize(HyphenDict * dict) |
| 514 |
{ |
| 515 |
HashIter *v; |
| 516 |
unsigned char *word; |
| 517 |
char *pattern; |
| 518 |
unsigned char *buf = hnj_malloc(dict->pat_length); |
| 519 |
unsigned char *cur = buf; |
| 520 |
v = new_HashIter(dict->patterns); |
| 521 |
while (eachHash(v, &word, &pattern)) { |
| 522 |
int i = 0, e = 0; |
| 523 |
while (word[e + i]) { |
| 524 |
if (pattern[i] != '0') |
| 525 |
*cur++ = (unsigned char) pattern[i]; |
| 526 |
*cur++ = word[e + i++]; |
| 527 |
while (is_utf8_follow(word[e + i])) |
| 528 |
*cur++ = word[i + e++]; |
| 529 |
} |
| 530 |
if (pattern[i] != '0') |
| 531 |
*cur++ = (unsigned char) pattern[i]; |
| 532 |
*cur++ = ' '; |
| 533 |
} |
| 534 |
delete_HashIter(v); |
| 535 |
*cur = 0; |
| 536 |
return buf; |
| 537 |
} |
| 538 |
|
| 539 |
void hnj_free_serialize(unsigned char *c) |
| 540 |
{ |
| 541 |
hnj_free(c); |
| 542 |
} |
| 543 |
|
| 544 |
/*tex |
| 545 |
|
| 546 |
In hyphenation patterns we use signed bytes where |0|, or actually any |
| 547 |
negative number, indicates end: |
| 548 |
|
| 549 |
\starttyping |
| 550 |
prio(1+),startpos,length,len1,[replace],len2,[replace] |
| 551 |
\starttyping |
| 552 |
|
| 553 |
A basic example is: |
| 554 |
|
| 555 |
\starttyping |
| 556 |
p n 0 0 0 |
| 557 |
\starttyping |
| 558 |
|
| 559 |
for a hyphenation point between characters. |
| 560 |
|
| 561 |
*/ |
| 562 |
|
| 563 |
void hnj_hyphen_load(HyphenDict * dict, const unsigned char *f) |
| 564 |
{ |
| 565 |
int state_num, last_state; |
| 566 |
int ch; |
| 567 |
int found; |
| 568 |
HashEntry *e; |
| 569 |
HashIter *v; |
| 570 |
unsigned char *word; |
| 571 |
char *pattern; |
| 572 |
size_t l = 0; |
| 573 |
const unsigned char *format; |
| 574 |
const unsigned char *begin = f; |
| 575 |
unsigned char *pat; |
| 576 |
char *org; |
| 577 |
while ((format = next_pattern(&l, &f)) != NULL) { |
| 578 |
int i, j, e1; |
| 579 |
if (l>=255) { |
| 580 |
normal_warning("hyphenation","a pattern of more than 254 bytes ignored"); |
| 581 |
continue; |
| 582 |
} |
| 583 |
for (i = 0, j = 0, e1 = 0; (unsigned) i < l; i++) { |
| 584 |
if (format[i] >= '0' && format[i] <= '9') |
| 585 |
j++; |
| 586 |
if (is_utf8_follow(format[i])) |
| 587 |
e1++; |
| 588 |
} |
| 589 |
/*tex |
| 590 |
Here |l-e1| is the number of {\em characters} not {\em bytes}, |l-j| |
| 591 |
the number of pattern bytes and |l-e1-j| the number of pattern |
| 592 |
characters. |
| 593 |
*/ |
| 594 |
pat = (unsigned char *) malloc((1 + l - (size_t) j)); |
| 595 |
org = (char *) malloc((size_t) (2 + l - (size_t) e1 - (size_t) j)); |
| 596 |
/*tex Remove hyphenation encoders (digits) from pat. */ |
| 597 |
org[0] = '0'; |
| 598 |
for (i = 0, j = 0, e1 = 0; (unsigned) i < l; i++) { |
| 599 |
unsigned char c = format[i]; |
| 600 |
if (is_utf8_follow(c)) { |
| 601 |
pat[j + e1++] = c; |
| 602 |
} else if (c < '0' || c > '9') { |
| 603 |
pat[e1 + j++] = c; |
| 604 |
org[j] = '0'; |
| 605 |
} else { |
| 606 |
org[j] = (char) c; |
| 607 |
} |
| 608 |
} |
| 609 |
pat[e1 + j] = 0; |
| 610 |
org[j + 1] = 0; |
| 611 |
hyppat_insert(dict->patterns, pat, org); |
| 612 |
} |
| 613 |
/*tex We add 2 bytes for spurious spaces. */ |
| 614 |
dict->pat_length += (int) ((f - begin) + 2); |
| 615 |
init_hash(&dict->merged); |
| 616 |
v = new_HashIter(dict->patterns); |
| 617 |
while (nextHash(v, &word)) { |
| 618 |
int wordsize = (int) strlen((char *) word); |
| 619 |
int j1, l1; |
| 620 |
for (l1 = 1; l1 <= wordsize; l1++) { |
| 621 |
if (is_utf8_follow(word[l1])) { |
| 622 |
/*tex Do not clip an utf8 sequence. */ |
| 623 |
continue; |
| 624 |
} |
| 625 |
for (j1 = 1; j1 <= l1; j1++) { |
| 626 |
char *subpat_pat; |
| 627 |
int i1 = l1 - j1; |
| 628 |
if (is_utf8_follow(word[i1])) { |
| 629 |
/*tex Do not start halfway an utf8 sequence. */ |
| 630 |
continue; |
| 631 |
} |
| 632 |
if ((subpat_pat = |
| 633 |
hyppat_lookup(dict->patterns, word + i1, j1)) != NULL) { |
| 634 |
char *newpat_pat; |
| 635 |
if ((newpat_pat = |
| 636 |
hyppat_lookup(dict->merged, word, l1)) == NULL) { |
| 637 |
char *neworg; |
| 638 |
unsigned char *newword = |
| 639 |
(unsigned char *) malloc((size_t) (l1 + 1)); |
| 640 |
int e1 = 0; |
| 641 |
strncpy((char *) newword, (char *) word, (size_t) l1); |
| 642 |
newword[l1] = 0; |
| 643 |
for (i1 = 0; i1 < l1; i1++) |
| 644 |
if (is_utf8_follow(newword[i1])) |
| 645 |
e1++; |
| 646 |
neworg = malloc((size_t) (l1 + 2 - e1)); |
| 647 |
/*tex Fill with right amount of zeros: */ |
| 648 |
sprintf(neworg, "%0*d", l1 + 1 - e1, 0); |
| 649 |
hyppat_insert(dict->merged, newword, combine(neworg, subpat_pat)); |
| 650 |
} else { |
| 651 |
combine(newpat_pat, subpat_pat); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
delete_HashIter(v); |
| 658 |
init_hash(&dict->state_num); |
| 659 |
state_insert(dict->state_num, hnj_strdup((const unsigned char *) ""), 0); |
| 660 |
v = new_HashIter(dict->merged); |
| 661 |
while (nextHashStealPattern(v, &word, &pattern)) { |
| 662 |
static unsigned char mask[] = { 0x3F, 0x1F, 0xF, 0x7 }; |
| 663 |
int j1 = (int) strlen((char *) word); |
| 664 |
state_num = hnj_get_state(dict, word, &found); |
| 665 |
dict->states[state_num].match = pattern; |
| 666 |
/*tex Now, put in the prefix transitions. */ |
| 667 |
while (found < 0) { |
| 668 |
j1--; |
| 669 |
last_state = state_num; |
| 670 |
ch = word[j1]; |
| 671 |
if (ch >= 0x80) { |
| 672 |
int m; |
| 673 |
int i1 = 1; |
| 674 |
while (is_utf8_follow(word[j1 - i1])) |
| 675 |
i1++; |
| 676 |
ch = word[j1 - i1] & mask[i1]; |
| 677 |
m = j1 - i1; |
| 678 |
while (i1--) { |
| 679 |
ch = (ch << 6) + (0x3F & word[j1 - i1]); |
| 680 |
} |
| 681 |
j1 = m; |
| 682 |
} |
| 683 |
word[j1] = '\0'; |
| 684 |
state_num = hnj_get_state(dict, word, &found); |
| 685 |
hnj_add_trans(dict, state_num, last_state, ch); |
| 686 |
} |
| 687 |
} |
| 688 |
delete_HashIter(v); |
| 689 |
clear_hyppat_hash(&dict->merged); |
| 690 |
/*tex Put in the fallback states. */ |
| 691 |
{ |
| 692 |
int i, j = 0; |
| 693 |
for (i = 0; i < HASH_SIZE; i++) { |
| 694 |
for (e = dict->state_num->entries[i]; e; e = e->next) { |
| 695 |
/*tex Do not do |state==0| otherwise things get confused. */ |
| 696 |
if (e->u.state) { |
| 697 |
for (j = 1; 1; j++) { |
| 698 |
state_num = state_lookup(dict->state_num, e->key + j); |
| 699 |
if (state_num >= 0) |
| 700 |
break; |
| 701 |
} |
| 702 |
dict->states[e->u.state].fallback_state = state_num; |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
clear_state_hash(&dict->state_num); |
| 708 |
} |
| 709 |
|
| 710 |
extern halfword insert_syllable_discretionary(halfword t, lang_variables * lan); |
| 711 |
|
| 712 |
void hnj_hyphen_hyphenate(HyphenDict * dict, halfword first1, halfword last1, |
| 713 |
int length, halfword left, halfword right, lang_variables * lan) |
| 714 |
{ |
| 715 |
int char_num; |
| 716 |
halfword here; |
| 717 |
int state = 0; |
| 718 |
/*tex +2 for dots at each end, +1 for points outside characters. */ |
| 719 |
int ext_word_len = length + 2; |
| 720 |
int hyphen_len = ext_word_len + 1; |
| 721 |
char *hyphens = hnj_malloc(hyphen_len + 1); |
| 722 |
/*tex Add a '.' to beginning and end to facilitate matching. */ |
| 723 |
vlink(begin_point) = first1; |
| 724 |
vlink(end_point) = vlink(last1); |
| 725 |
vlink(last1) = end_point; |
| 726 |
for (char_num = 0; char_num < hyphen_len; char_num++) { |
| 727 |
hyphens[char_num] = '0'; |
| 728 |
} |
| 729 |
hyphens[hyphen_len] = 0; |
| 730 |
/*tex Now, run the finite state machine. */ |
| 731 |
for (char_num = 0, here = begin_point; here != vlink(end_point); here = vlink(here)) { |
| 732 |
int ch; |
| 733 |
if (here == begin_point || here == end_point) { |
| 734 |
ch = '.'; |
| 735 |
} else { |
| 736 |
ch = get_hj_code(char_lang(here),character(here)); |
| 737 |
if (ch <= 32) { |
| 738 |
ch = character(here); |
| 739 |
} |
| 740 |
} |
| 741 |
while (state != -1) { |
| 742 |
HyphenState *hstate = &dict->states[state]; |
| 743 |
int k; |
| 744 |
for (k = 0; k < hstate->num_trans; k++) { |
| 745 |
if (hstate->trans[k].uni_ch == ch) { |
| 746 |
char *match; |
| 747 |
state = hstate->trans[k].new_state; |
| 748 |
match = dict->states[state].match; |
| 749 |
if (match) { |
| 750 |
/*tex |
| 751 |
|
| 752 |
We add +2 because 1 string length is one bigger than offset |
| 753 |
and 1 hyphenation starts before first character. |
| 754 |
*/ |
| 755 |
int offset = (int) (char_num + 2 - (int) strlen(match)); |
| 756 |
int m; |
| 757 |
for (m = 0; match[m]; m++) { |
| 758 |
if (hyphens[offset + m] < match[m]) |
| 759 |
hyphens[offset + m] = match[m]; |
| 760 |
} |
| 761 |
} |
| 762 |
goto try_next_letter; |
| 763 |
} |
| 764 |
} |
| 765 |
state = hstate->fallback_state; |
| 766 |
} |
| 767 |
/*tex Nothing worked, let's go to the next character. */ |
| 768 |
state = 0; |
| 769 |
try_next_letter:; |
| 770 |
char_num++; |
| 771 |
} |
| 772 |
/*tex Restore the correct pointers. */ |
| 773 |
vlink(last1) = vlink(end_point); |
| 774 |
/*tex |
| 775 |
|
| 776 |
Pattern is \.{\^.\^w\^o\^r\^d\^.\^} and |word_len|=4, |ext_word_len|=6, |
| 777 |
|hyphens|=7; check \.{ \^ \^ \^ } and therefore drop first two and stop |
| 778 |
after |word_len-1| |
| 779 |
*/ |
| 780 |
for (here = first1, char_num = 2; here != left; here = vlink(here)) |
| 781 |
char_num++; |
| 782 |
for (; here != right; here = vlink(here)) { |
| 783 |
if (hyphens[char_num] & 1) |
| 784 |
here = insert_syllable_discretionary(here, lan); |
| 785 |
char_num++; |
| 786 |
} |
| 787 |
hnj_free(hyphens); |
| 788 |
} |