| 1 |
/* |
| 2 |
|
| 3 |
texlang.w |
| 4 |
|
| 5 |
Copyright 2006-2012 Taco Hoekwater <[email protected]> |
| 6 |
|
| 7 |
This file is part of LuaTeX. |
| 8 |
|
| 9 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 10 |
the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 2 of the License, or (at your |
| 12 |
option) any later version. |
| 13 |
|
| 14 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 15 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 |
License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
#include "ptexlib.h" |
| 25 |
#include <string.h> |
| 26 |
#include "lua/luatex-api.h" |
| 27 |
|
| 28 |
/*tex Low-level helpers */ |
| 29 |
|
| 30 |
#define unVERBOSE |
| 31 |
|
| 32 |
#define MAX_TEX_LANGUAGES 16384 |
| 33 |
|
| 34 |
static struct tex_language *tex_languages[MAX_TEX_LANGUAGES] = { NULL }; |
| 35 |
|
| 36 |
static int next_lang_id = 0; |
| 37 |
|
| 38 |
struct tex_language *new_language(int n) |
| 39 |
{ |
| 40 |
struct tex_language *lang; |
| 41 |
unsigned l; |
| 42 |
if (n >= 0) { |
| 43 |
l = (unsigned) n; |
| 44 |
if (l != (MAX_TEX_LANGUAGES - 1)) |
| 45 |
if (next_lang_id <= n) |
| 46 |
next_lang_id = n + 1; |
| 47 |
} else { |
| 48 |
while (tex_languages[next_lang_id] != NULL) |
| 49 |
next_lang_id++; |
| 50 |
l = (unsigned) next_lang_id++; |
| 51 |
} |
| 52 |
if (l < (MAX_TEX_LANGUAGES - 1) && tex_languages[l] == NULL) { |
| 53 |
lang = xmalloc(sizeof(struct tex_language)); |
| 54 |
tex_languages[l] = lang; |
| 55 |
lang->id = (int) l; |
| 56 |
lang->exceptions = 0; |
| 57 |
lang->patterns = NULL; |
| 58 |
lang->pre_hyphen_char = '-'; |
| 59 |
lang->post_hyphen_char = 0; |
| 60 |
lang->pre_exhyphen_char = 0; |
| 61 |
lang->post_exhyphen_char = 0; |
| 62 |
lang->hyphenation_min = -1; |
| 63 |
if (saving_hyph_codes_par) { |
| 64 |
/*tex |
| 65 |
For now, we might just use specific value for whatever task. |
| 66 |
*/ |
| 67 |
hj_codes_from_lc_codes(l); |
| 68 |
} |
| 69 |
return lang; |
| 70 |
} else { |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
struct tex_language *get_language(int n) |
| 76 |
{ |
| 77 |
if (n >= 0 && n < MAX_TEX_LANGUAGES) { |
| 78 |
if (tex_languages[n] != NULL) { |
| 79 |
return tex_languages[n]; |
| 80 |
} else { |
| 81 |
return new_language(n); |
| 82 |
} |
| 83 |
} else { |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void set_pre_hyphen_char(int n, int v) |
| 89 |
{ |
| 90 |
struct tex_language *l = get_language((int) n); |
| 91 |
if (l != NULL) |
| 92 |
l->pre_hyphen_char = (int) v; |
| 93 |
} |
| 94 |
|
| 95 |
void set_post_hyphen_char(int n, int v) |
| 96 |
{ |
| 97 |
struct tex_language *l = get_language((int) n); |
| 98 |
if (l != NULL) |
| 99 |
l->post_hyphen_char = (int) v; |
| 100 |
} |
| 101 |
|
| 102 |
void set_pre_exhyphen_char(int n, int v) |
| 103 |
{ |
| 104 |
struct tex_language *l = get_language((int) n); |
| 105 |
if (l != NULL) |
| 106 |
l->pre_exhyphen_char = (int) v; |
| 107 |
} |
| 108 |
|
| 109 |
void set_post_exhyphen_char(int n, int v) |
| 110 |
{ |
| 111 |
struct tex_language *l = get_language((int) n); |
| 112 |
if (l != NULL) |
| 113 |
l->post_exhyphen_char = (int) v; |
| 114 |
} |
| 115 |
|
| 116 |
int get_pre_hyphen_char(int n) |
| 117 |
{ |
| 118 |
struct tex_language *l = get_language((int) n); |
| 119 |
if (l == NULL) |
| 120 |
return -1; |
| 121 |
return (int) l->pre_hyphen_char; |
| 122 |
} |
| 123 |
|
| 124 |
int get_post_hyphen_char(int n) |
| 125 |
{ |
| 126 |
struct tex_language *l = get_language((int) n); |
| 127 |
if (l == NULL) |
| 128 |
return -1; |
| 129 |
return (int) l->post_hyphen_char; |
| 130 |
} |
| 131 |
|
| 132 |
int get_pre_exhyphen_char(int n) |
| 133 |
{ |
| 134 |
struct tex_language *l = get_language((int) n); |
| 135 |
if (l == NULL) |
| 136 |
return -1; |
| 137 |
return (int) l->pre_exhyphen_char; |
| 138 |
} |
| 139 |
|
| 140 |
int get_post_exhyphen_char(int n) |
| 141 |
{ |
| 142 |
struct tex_language *l = get_language((int) n); |
| 143 |
if (l == NULL) |
| 144 |
return -1; |
| 145 |
return (int) l->post_exhyphen_char; |
| 146 |
} |
| 147 |
|
| 148 |
void set_hyphenation_min(int n, int v) |
| 149 |
{ |
| 150 |
struct tex_language *l = get_language((int) n); |
| 151 |
if (l != NULL) |
| 152 |
l->hyphenation_min = (int) v; |
| 153 |
} |
| 154 |
|
| 155 |
int get_hyphenation_min(int n) |
| 156 |
{ |
| 157 |
struct tex_language *l = get_language((int) n); |
| 158 |
if (l == NULL) |
| 159 |
return -1; |
| 160 |
return (int) l->hyphenation_min; |
| 161 |
} |
| 162 |
|
| 163 |
void load_patterns(struct tex_language *lang, const unsigned char *buff) |
| 164 |
{ |
| 165 |
if (lang == NULL || buff == NULL || strlen((const char *) buff) == 0) |
| 166 |
return; |
| 167 |
if (lang->patterns == NULL) { |
| 168 |
lang->patterns = hnj_hyphen_new(); |
| 169 |
} |
| 170 |
hnj_hyphen_load(lang->patterns, buff); |
| 171 |
} |
| 172 |
|
| 173 |
void clear_patterns(struct tex_language *lang) |
| 174 |
{ |
| 175 |
if (lang == NULL) |
| 176 |
return; |
| 177 |
if (lang->patterns != NULL) { |
| 178 |
hnj_hyphen_clear(lang->patterns); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
void load_tex_patterns(int curlang, halfword head) |
| 183 |
{ |
| 184 |
char *s = tokenlist_to_cstring(head, 1, NULL); |
| 185 |
load_patterns(get_language(curlang), (unsigned char *) s); |
| 186 |
} |
| 187 |
|
| 188 |
#define STORE_CHAR(l,x) do { \ |
| 189 |
unsigned xx = get_hj_code(l,x); \ |
| 190 |
if (!xx || xx <= 32) { \ |
| 191 |
xx = x; \ |
| 192 |
} \ |
| 193 |
uindex = uni2string(uindex, xx); \ |
| 194 |
} while (0) |
| 195 |
|
| 196 |
/* |
| 197 |
|
| 198 |
This cleans one word which is returned in |cleaned|, returns the new offset |
| 199 |
into |buffer|. |
| 200 |
|
| 201 |
*/ |
| 202 |
|
| 203 |
const char *clean_hyphenation(int id, const char *buff, char **cleaned) |
| 204 |
{ |
| 205 |
int items = 0; |
| 206 |
/*tex Work buffer for bytes: */ |
| 207 |
unsigned char word[MAX_WORD_LEN + 1]; |
| 208 |
/*tex Work buffer for \UNICODE: */ |
| 209 |
unsigned uword[MAX_WORD_LEN + 1] = { 0 }; |
| 210 |
/*tex The \UNICODE\ buffer value: */ |
| 211 |
int u = 0; |
| 212 |
/*tex The index into buffer: */ |
| 213 |
int i = 0; |
| 214 |
char *uindex = (char *)word; |
| 215 |
const char *s = buff; |
| 216 |
while (*s && !isspace((unsigned char)*s)) { |
| 217 |
word[i++] = (unsigned)*s; |
| 218 |
s++; |
| 219 |
if ((s-buff)>MAX_WORD_LEN) { |
| 220 |
/*tex Todo: this is too strict, should count \UNICODE, not bytes. */ |
| 221 |
*cleaned = NULL; |
| 222 |
tex_error("exception too long", NULL); |
| 223 |
return s; |
| 224 |
} |
| 225 |
} |
| 226 |
/*tex Now convert the input to \UNICODE. */ |
| 227 |
word[i] = '\0'; |
| 228 |
utf2uni_strcpy(uword, (const char *)word); |
| 229 |
/*tex Build the new word string. */ |
| 230 |
i = 0; |
| 231 |
while (uword[i]>0) { |
| 232 |
u = uword[i++]; |
| 233 |
if (u == '-') { |
| 234 |
/*tex Skip. */ |
| 235 |
} else if (u == '=') { |
| 236 |
STORE_CHAR(id,'-'); |
| 237 |
} else if (u == '{') { |
| 238 |
u = uword[i++]; |
| 239 |
items = 0; |
| 240 |
while (u && u != '}') { |
| 241 |
u = uword[i++]; |
| 242 |
} |
| 243 |
if (u == '}') { |
| 244 |
items++; |
| 245 |
u = uword[i++]; |
| 246 |
} |
| 247 |
while (u && u != '}') { |
| 248 |
u = uword[i++]; |
| 249 |
} |
| 250 |
if (u == '}') { |
| 251 |
items++; |
| 252 |
u = uword[i++]; |
| 253 |
} |
| 254 |
if (u == '{') { |
| 255 |
u = uword[i++]; |
| 256 |
} |
| 257 |
while (u && u != '}') { |
| 258 |
STORE_CHAR(id,u); |
| 259 |
u = uword[i++]; |
| 260 |
} |
| 261 |
if (u == '}') { |
| 262 |
items++; |
| 263 |
} |
| 264 |
if (items != 3) { |
| 265 |
*cleaned = NULL; |
| 266 |
tex_error("exception syntax error", NULL); |
| 267 |
return s; |
| 268 |
} |
| 269 |
if (uword[i] == '[' && uword[i+1] >= '0' && uword[i+1] <= '9' && uword[i+2] == ']') { |
| 270 |
i += 3; |
| 271 |
} |
| 272 |
} else { |
| 273 |
STORE_CHAR(id,u); |
| 274 |
} |
| 275 |
} |
| 276 |
*uindex = '\0'; |
| 277 |
*cleaned = xstrdup((char *) word); |
| 278 |
return s; |
| 279 |
} |
| 280 |
|
| 281 |
void load_hyphenation(struct tex_language *lang, const unsigned char *buff) |
| 282 |
{ |
| 283 |
const char *s; |
| 284 |
const char *value; |
| 285 |
char *cleaned; |
| 286 |
int id ; |
| 287 |
if (lang == NULL) |
| 288 |
return; |
| 289 |
lua_checkstack(Luas, 3); |
| 290 |
if (lang->exceptions == 0) { |
| 291 |
lua_newtable(Luas); |
| 292 |
lang->exceptions = luaL_ref(Luas, LUA_REGISTRYINDEX); |
| 293 |
} |
| 294 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, lang->exceptions); |
| 295 |
s = (const char *) buff; |
| 296 |
id = lang->id; |
| 297 |
while (*s) { |
| 298 |
while (isspace((unsigned char)*s)) |
| 299 |
s++; |
| 300 |
if (*s) { |
| 301 |
value = s; |
| 302 |
s = clean_hyphenation(id, s, &cleaned); |
| 303 |
if (cleaned != NULL) { |
| 304 |
if ((s - value) > 0) { |
| 305 |
lua_pushstring(Luas, cleaned); |
| 306 |
lua_pushlstring(Luas, value, (size_t) (s - value)); |
| 307 |
lua_rawset(Luas, -3); |
| 308 |
} |
| 309 |
free(cleaned); |
| 310 |
} else { |
| 311 |
#ifdef VERBOSE |
| 312 |
formatted_warning("hyphenation","skipping invalid hyphenation exception: %s", value); |
| 313 |
#endif |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
lua_pop(Luas, 1); |
| 318 |
} |
| 319 |
|
| 320 |
void clear_hyphenation(struct tex_language *lang) |
| 321 |
{ |
| 322 |
if (lang == NULL) |
| 323 |
return; |
| 324 |
if (lang->exceptions != 0) { |
| 325 |
luaL_unref(Luas, LUA_REGISTRYINDEX, lang->exceptions); |
| 326 |
lang->exceptions = 0; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void load_tex_hyphenation(int curlang, halfword head) |
| 331 |
{ |
| 332 |
char *s = tokenlist_to_cstring(head, 1, NULL); |
| 333 |
load_hyphenation(get_language(curlang), (unsigned char *) s); |
| 334 |
} |
| 335 |
|
| 336 |
static halfword insert_discretionary(halfword t, halfword pre, halfword post, halfword replace, int penalty) |
| 337 |
{ |
| 338 |
halfword g; |
| 339 |
int f; |
| 340 |
halfword d = new_node(disc_node, syllable_disc); |
| 341 |
int attr = node_attr(t) ; |
| 342 |
disc_penalty(d) = penalty; |
| 343 |
if (t == replace) { |
| 344 |
/*tex We have |prev disc next-next|. */ |
| 345 |
try_couple_nodes(d, vlink(t)); |
| 346 |
try_couple_nodes(alink(t), d); |
| 347 |
alink(t) = null; |
| 348 |
vlink(t) = null; |
| 349 |
replace = t ; |
| 350 |
} else { |
| 351 |
/*tex We have |prev disc next|. */ |
| 352 |
try_couple_nodes(d, vlink(t)); |
| 353 |
couple_nodes(t, d); |
| 354 |
} |
| 355 |
if (replace != null) { |
| 356 |
f = font(replace); |
| 357 |
} else { |
| 358 |
/*tex For compound words following explicit hyphens. */ |
| 359 |
f = get_cur_font(); |
| 360 |
} |
| 361 |
for (g = pre; g != null; g = vlink(g)) { |
| 362 |
if (! font(g)) { |
| 363 |
font(g) = f; |
| 364 |
} |
| 365 |
if (attr != null) { |
| 366 |
delete_attribute_ref(node_attr(g)); |
| 367 |
node_attr(g) = attr; |
| 368 |
attr_list_ref(attr) += 1; |
| 369 |
} |
| 370 |
} |
| 371 |
for (g = post; g != null; g = vlink(g)) { |
| 372 |
if (! font(g)) { |
| 373 |
font(g) = f; |
| 374 |
} |
| 375 |
if (attr != null) { |
| 376 |
delete_attribute_ref(node_attr(g)); |
| 377 |
node_attr(g) = attr; |
| 378 |
attr_list_ref(attr) += 1; |
| 379 |
} |
| 380 |
} |
| 381 |
if (attr != null) { |
| 382 |
for (g = replace; g != null; g = vlink(g)) { |
| 383 |
delete_attribute_ref(node_attr(g)); |
| 384 |
node_attr(g) = attr; |
| 385 |
attr_list_ref(attr) += 1; |
| 386 |
} |
| 387 |
delete_attribute_ref(node_attr(d)); |
| 388 |
node_attr(d) = attr; |
| 389 |
attr_list_ref(attr) += 1; |
| 390 |
} |
| 391 |
set_disc_field(pre_break(d), pre); |
| 392 |
set_disc_field(post_break(d), post); |
| 393 |
set_disc_field(no_break(d), replace); |
| 394 |
return d; |
| 395 |
} |
| 396 |
|
| 397 |
halfword insert_syllable_discretionary(halfword t, lang_variables * lan) |
| 398 |
{ |
| 399 |
halfword g, n; |
| 400 |
n = new_node(disc_node, syllable_disc); |
| 401 |
disc_penalty(n) = hyphen_penalty_par; |
| 402 |
couple_nodes(n, vlink(t)); |
| 403 |
couple_nodes(t, n); |
| 404 |
delete_attribute_ref(node_attr(n)); |
| 405 |
if (node_attr(t) != null) { |
| 406 |
node_attr(n) = node_attr(t); |
| 407 |
attr_list_ref(node_attr(t))++; |
| 408 |
} else { |
| 409 |
node_attr(n) = null; |
| 410 |
} |
| 411 |
if (lan->pre_hyphen_char > 0) { |
| 412 |
g = raw_glyph_node(); |
| 413 |
set_to_character(g); |
| 414 |
character(g) = lan->pre_hyphen_char; |
| 415 |
font(g) = font(t); |
| 416 |
lang_data(g) = lang_data(t); |
| 417 |
if (node_attr(t) != null) { |
| 418 |
node_attr(g) = node_attr(t); |
| 419 |
attr_list_ref(node_attr(t))++; |
| 420 |
} |
| 421 |
set_disc_field(pre_break(n), g); |
| 422 |
} |
| 423 |
if (lan->post_hyphen_char > 0) { |
| 424 |
t = vlink(n); |
| 425 |
g = raw_glyph_node(); |
| 426 |
set_to_character(g); |
| 427 |
character(g) = lan->post_hyphen_char; |
| 428 |
font(g) = font(t); |
| 429 |
lang_data(g) = lang_data(t); |
| 430 |
if (node_attr(t) != null) { |
| 431 |
node_attr(g) = node_attr(t); |
| 432 |
attr_list_ref(node_attr(t)) += 1; |
| 433 |
} |
| 434 |
set_disc_field(post_break(n), g); |
| 435 |
} |
| 436 |
return n; |
| 437 |
} |
| 438 |
|
| 439 |
static halfword insert_character(halfword t, int c) |
| 440 |
{ |
| 441 |
halfword p; |
| 442 |
p = new_node(glyph_node, 0); |
| 443 |
set_to_character(p); |
| 444 |
character(p) = c; |
| 445 |
if (t != null) { |
| 446 |
couple_nodes(t, p); |
| 447 |
} |
| 448 |
return p; |
| 449 |
} |
| 450 |
|
| 451 |
static halfword compound_word_break(halfword t, int clang) |
| 452 |
{ |
| 453 |
halfword disc = null; |
| 454 |
halfword pre = null; |
| 455 |
halfword pos = null; |
| 456 |
halfword pre_exhyphen_char = get_pre_exhyphen_char(clang); |
| 457 |
halfword post_exhyphen_char = get_post_exhyphen_char(clang); |
| 458 |
if (pre_exhyphen_char > 0) { |
| 459 |
pre = insert_character(null,pre_exhyphen_char); |
| 460 |
} else { |
| 461 |
pre = insert_character(null,ex_hyphen_char_par); |
| 462 |
} |
| 463 |
if (post_exhyphen_char > 0) |
| 464 |
pos = insert_character(null,post_exhyphen_char); |
| 465 |
disc = insert_discretionary(t,pre,pos,t,ex_hyphen_penalty_par); |
| 466 |
subtype(disc) = automatic_disc; |
| 467 |
set_automatic_disc_penalty(disc); |
| 468 |
return disc; |
| 469 |
} |
| 470 |
|
| 471 |
void set_disc_field(halfword f, halfword t) |
| 472 |
{ |
| 473 |
if (t != null) { |
| 474 |
alink(t) = null; /* don't expose this one! */ |
| 475 |
vlink(f) = t; |
| 476 |
tlink(f) = tail_of_list(t); |
| 477 |
} else { |
| 478 |
vlink(f) = null; |
| 479 |
tlink(f) = null; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
static char *hyphenation_exception(int exceptions, char *w) |
| 484 |
{ |
| 485 |
char *ret = NULL; |
| 486 |
lua_checkstack(Luas, 2); |
| 487 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, exceptions); |
| 488 |
if (lua_istable(Luas, -1)) { |
| 489 |
/*tex Word table: */ |
| 490 |
lua_pushstring(Luas, w); |
| 491 |
lua_rawget(Luas, -2); |
| 492 |
if (lua_type(Luas, -1) == LUA_TSTRING) { |
| 493 |
ret = xstrdup(lua_tostring(Luas, -1)); |
| 494 |
} |
| 495 |
lua_pop(Luas, 2); |
| 496 |
} else { |
| 497 |
lua_pop(Luas, 1); |
| 498 |
} |
| 499 |
return ret; |
| 500 |
} |
| 501 |
|
| 502 |
char *exception_strings(struct tex_language *lang) |
| 503 |
{ |
| 504 |
const char *value; |
| 505 |
size_t size = 0, current = 0; |
| 506 |
size_t l = 0; |
| 507 |
char *ret = NULL; |
| 508 |
if (lang->exceptions == 0) |
| 509 |
return NULL; |
| 510 |
lua_checkstack(Luas, 2); |
| 511 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, lang->exceptions); |
| 512 |
if (lua_istable(Luas, -1)) { |
| 513 |
/*tex Iterate and join. */ |
| 514 |
lua_pushnil(Luas); |
| 515 |
while (lua_next(Luas, -2) != 0) { |
| 516 |
value = lua_tolstring(Luas, -1, &l); |
| 517 |
if (current + 2 + l > size) { |
| 518 |
ret = xrealloc(ret, (unsigned) ((size + size / 5) + current + l + 1024)); |
| 519 |
size = (size + size / 5) + current + l + 1024; |
| 520 |
} |
| 521 |
*(ret + current) = ' '; |
| 522 |
strcpy(ret + current + 1, value); |
| 523 |
current += l + 1; |
| 524 |
lua_pop(Luas, 1); |
| 525 |
} |
| 526 |
} |
| 527 |
return ret; |
| 528 |
} |
| 529 |
|
| 530 |
/*tex |
| 531 |
|
| 532 |
The sequence from |wordstart| to |r| can contain only normal characters it |
| 533 |
could be faster to modify a halfword pointer and return an integer |
| 534 |
|
| 535 |
We now take the font from the wordstart (as in \LUAMETATEX) but leave the |
| 536 |
rest as it is, because we don't want to break compatibility (end June 2022). |
| 537 |
We make a copy now of the parent and hope for the best. Backporting would be |
| 538 |
too intrusive so this has to do. It went unnoticed for ages anyway. |
| 539 |
|
| 540 |
*/ |
| 541 |
|
| 542 |
static halfword find_exception_part(unsigned int *j, unsigned int *uword, int len, halfword parent) |
| 543 |
{ |
| 544 |
halfword g = null, gg = null; |
| 545 |
register unsigned i = *j; |
| 546 |
/*tex This puts uword[i] on the |{|. */ |
| 547 |
i++; |
| 548 |
while (i < (unsigned) len && uword[i + 1] != '}') { |
| 549 |
if (g == null) { |
| 550 |
/* gg = new_char(font(parent), (int) uword[i + 1]); */ |
| 551 |
gg = copy_node(parent); |
| 552 |
g = gg; |
| 553 |
} else { |
| 554 |
/* halfword s = new_char(font(parent), (int) uword[i + 1]); */ |
| 555 |
halfword s = copy_node(parent); |
| 556 |
couple_nodes(g, s); |
| 557 |
g = s; |
| 558 |
} |
| 559 |
character(g) = (int) uword[i + 1]; |
| 560 |
i++; |
| 561 |
} |
| 562 |
*j = ++i; |
| 563 |
return gg; |
| 564 |
} |
| 565 |
|
| 566 |
static int count_exception_part(unsigned int *j, unsigned int *uword, int len) |
| 567 |
{ |
| 568 |
int ret = 0; |
| 569 |
register unsigned i = *j; |
| 570 |
/*tex This puts uword[i] on the |{|. */ |
| 571 |
i++; |
| 572 |
while (i < (unsigned) len && uword[i + 1] != '}') { |
| 573 |
ret++; |
| 574 |
i++; |
| 575 |
} |
| 576 |
*j = ++i; |
| 577 |
return ret; |
| 578 |
} |
| 579 |
|
| 580 |
static const char *PAT_ERROR[] = { |
| 581 |
"Exception discretionaries should contain three pairs of braced items.", |
| 582 |
"No intervening spaces are allowed.", |
| 583 |
NULL |
| 584 |
}; |
| 585 |
|
| 586 |
/*tex |
| 587 |
|
| 588 |
The exceptions are taken as-is: no min values are taken into account. One can |
| 589 |
add normal patterns on-the-fly if needed. |
| 590 |
|
| 591 |
*/ |
| 592 |
|
| 593 |
static void do_exception(halfword wordstart, halfword r, char *replacement) |
| 594 |
{ |
| 595 |
unsigned i; |
| 596 |
halfword t, pen; |
| 597 |
unsigned len; |
| 598 |
int clang; |
| 599 |
lang_variables langdata; |
| 600 |
unsigned uword[MAX_WORD_LEN + 1] = { 0 }; |
| 601 |
utf2uni_strcpy(uword, replacement); |
| 602 |
len = u_length(uword); |
| 603 |
i = 0; |
| 604 |
t = wordstart; |
| 605 |
clang = char_lang(wordstart); |
| 606 |
langdata.pre_hyphen_char = get_pre_hyphen_char(clang); |
| 607 |
langdata.post_hyphen_char = get_post_hyphen_char(clang); |
| 608 |
for (i = 0; i < len; i++) { |
| 609 |
if (uword[i + 1] == 0 ) { |
| 610 |
/*tex We ran out of the exception pattern. */ |
| 611 |
break; |
| 612 |
} else if (uword[i + 1] == '-') { |
| 613 |
/*tex A hyphen follows. */ |
| 614 |
if (vlink(t) == r) |
| 615 |
break; |
| 616 |
insert_syllable_discretionary(t, &langdata); |
| 617 |
t = vlink(t); /* skip the new disc */ |
| 618 |
} else if (uword[i + 1] == '=') { |
| 619 |
/*tex We skip a disc. */ |
| 620 |
t = vlink(t); |
| 621 |
} else if (uword[i + 1] == '{') { |
| 622 |
/*tex We ran into an exception |{}{}{}| or |{}{}{}[]|. */ |
| 623 |
halfword gg, hh, replace = null; |
| 624 |
int repl; |
| 625 |
/*tex |pre| */ |
| 626 |
gg = find_exception_part(&i, uword, (int) len, wordstart); |
| 627 |
if (i == len || uword[i + 1] != '{') { |
| 628 |
tex_error("broken pattern 1", PAT_ERROR); |
| 629 |
} |
| 630 |
/*tex |post| */ |
| 631 |
hh = find_exception_part(&i, uword, (int) len, wordstart); |
| 632 |
if (i == len || uword[i + 1] != '{') { |
| 633 |
tex_error("broken pattern 2", PAT_ERROR); |
| 634 |
} |
| 635 |
/*tex |replace| */ |
| 636 |
repl = count_exception_part(&i, uword, (int) len); |
| 637 |
if (i == len) { |
| 638 |
tex_error("broken pattern 3", PAT_ERROR); |
| 639 |
} |
| 640 |
/*tex Play safe. */ |
| 641 |
if (vlink(t) == r) |
| 642 |
break; |
| 643 |
/*tex Let's deal with an (optional) replacement. */ |
| 644 |
if (repl > 0) { |
| 645 |
/*tex Assemble the replace stream. */ |
| 646 |
halfword q = t; |
| 647 |
replace = vlink(q); |
| 648 |
while (repl > 0 && q != null) { |
| 649 |
q = vlink(q); |
| 650 |
if (type(q) == glyph_node || type(q) == disc_node) { |
| 651 |
repl--; |
| 652 |
} else { |
| 653 |
break ; |
| 654 |
} |
| 655 |
} |
| 656 |
/*tex Remove it from the main stream */ |
| 657 |
try_couple_nodes(t, vlink(q)); |
| 658 |
/*tex and finish it in the replace. */ |
| 659 |
vlink(q) = null; |
| 660 |
/*tex Sanitize the replace stream (we could use the flattener instead). */ |
| 661 |
q = replace ; |
| 662 |
while (q != null) { |
| 663 |
halfword n = vlink(q); |
| 664 |
if (type(q) == disc_node) { |
| 665 |
/*tex Beware: the replacement starts after the no_break pointer. */ |
| 666 |
halfword r = vlink(no_break(q)); |
| 667 |
vlink(no_break(q)) = null; |
| 668 |
alink(r) = null ; |
| 669 |
/*tex Insert the replacement glyph. */ |
| 670 |
if (q == replace) { |
| 671 |
replace = r; |
| 672 |
} else { |
| 673 |
try_couple_nodes(alink(q),r); |
| 674 |
} |
| 675 |
/*tex Append the glyph (one). */ |
| 676 |
try_couple_nodes(r,n); |
| 677 |
/*tex Flush the disc. */ |
| 678 |
flush_node(q); |
| 679 |
} |
| 680 |
q = n ; |
| 681 |
} |
| 682 |
} |
| 683 |
/*tex Let's check if we have a penalty spec. */ |
| 684 |
if (((i+3) < len) && uword[i+1] == '[' && uword[i+2] >= '0' && uword[i+2] <= '9' && uword[i+3] == ']') { |
| 685 |
if (exception_penalty_par > 0) { |
| 686 |
if (exception_penalty_par > 10000) { |
| 687 |
pen = exception_penalty_par; |
| 688 |
} else { |
| 689 |
pen = (uword[i+2] - '0') * exception_penalty_par ; |
| 690 |
} |
| 691 |
} else { |
| 692 |
pen = hyphen_penalty_par; |
| 693 |
} |
| 694 |
i += 3; |
| 695 |
} else { |
| 696 |
pen = hyphen_penalty_par; |
| 697 |
} |
| 698 |
/*tex And now we insert a disc node. */ |
| 699 |
t = insert_discretionary(t, gg, hh, replace, pen); |
| 700 |
/*tex We skip the new disc node. */ |
| 701 |
t = vlink(t); |
| 702 |
/*tex check if we have two exceptions in a row */ |
| 703 |
if (uword[i + 1] == '{') { |
| 704 |
i--; |
| 705 |
t = alink(t); |
| 706 |
} |
| 707 |
} else { |
| 708 |
t = vlink(t); |
| 709 |
} |
| 710 |
/*tex Again we play safe. */ |
| 711 |
if (t == null || vlink(t) == r) { |
| 712 |
break; |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
/*tex |
| 718 |
|
| 719 |
This is a documentation section from the pascal web file. It is not true any |
| 720 |
more, but I (Taco) do not have time right now to rewrite it. |
| 721 |
|
| 722 |
When the line-breaking routine is unable to find a feasible sequence of |
| 723 |
breakpoints, it makes a second pass over the paragraph, attempting to |
| 724 |
hyphenate the hyphenatable words. The goal of hyphenation is to insert |
| 725 |
discretionary material into the paragraph so that there are more potential |
| 726 |
places to break. |
| 727 |
|
| 728 |
The general rules for hyphenation are somewhat complex and technical, because |
| 729 |
we want to be able to hyphenate words that are preceded or followed by |
| 730 |
punctuation marks, and because we want the rules to work for languages other |
| 731 |
than English. We also must contend with the fact that hyphens might radically |
| 732 |
alter the ligature and kerning structure of a word. |
| 733 |
|
| 734 |
A sequence of characters will be considered for hyphenation only if it |
| 735 |
belongs to a ``potentially hyphenatable part'' of the current paragraph. This |
| 736 |
is a sequence of nodes $p_0p_1\ldots p_m$ where $p_0$ is a glue node, |
| 737 |
$p_1\ldots p_{m-1}$ are either character or ligature or whatsit or implicit |
| 738 |
kern nodes, and $p_m$ is a glue or penalty or insertion or adjust or mark or |
| 739 |
whatsit or explicit kern node. (Therefore hyphenation is disabled by boxes, |
| 740 |
math formulas, and discretionary nodes already inserted by the user.) The |
| 741 |
ligature nodes among $p_1\ldots p_{m-1}$ are effectively expanded into the |
| 742 |
original non-ligature characters; the kern nodes and whatsits are ignored. |
| 743 |
Each character |c| is now classified as either a nonletter (if |
| 744 |
|lc_code(c)=0|), a lowercase letter (if |lc_code(c)=c|), or an uppercase |
| 745 |
letter (otherwise); an uppercase letter is treated as if it were |lc_code(c)| |
| 746 |
for purposes of hyphenation. The characters generated by $p_1\ldots p_{m-1}$ |
| 747 |
may begin with nonletters; let $c_1$ be the first letter that is not in the |
| 748 |
middle of a ligature. Whatsit nodes preceding $c_1$ are ignored; a whatsit |
| 749 |
found after $c_1$ will be the terminating node $p_m$. All characters that do |
| 750 |
not have the same font as $c_1$ will be treated as nonletters. The |
| 751 |
|hyphen_char| for that font must be between 0 and 255, otherwise hyphenation |
| 752 |
will not be attempted. \TeX\ looks ahead for as many consecutive letters |
| 753 |
$c_1\ldots c_n$ as possible; however, |n| must be less than 64, so a |
| 754 |
character that would otherwise be $c_{64}$ is effectively not a letter. |
| 755 |
Furthermore $c_n$ must not be in the middle of a ligature. In this way we |
| 756 |
obtain a string of letters $c_1\ldots c_n$ that are generated by nodes |
| 757 |
$p_a\ldots p_b$, where |1<=a<=b+1<=m|. If |n>=l_hyf+r_hyf|, this string |
| 758 |
qualifies for hyphenation; however, |uc_hyph| must be positive, if $c_1$ is |
| 759 |
uppercase. |
| 760 |
|
| 761 |
The hyphenation process takes place in three stages. First, the candidate |
| 762 |
sequence $c_1\ldots c_n$ is found; then potential positions for hyphens are |
| 763 |
determined by referring to hyphenation tables; and finally, the nodes |
| 764 |
$p_a\ldots p_b$ are replaced by a new sequence of nodes that includes the |
| 765 |
discretionary breaks found. |
| 766 |
|
| 767 |
Fortunately, we do not have to do all this calculation very often, because of |
| 768 |
the way it has been taken out of \TeX's inner loop. For example, when the |
| 769 |
second edition of the author's 700-page book {\sl Seminumerical Algorithms} |
| 770 |
was typeset by \TeX, only about 1.2 hyphenations needed to be tried per |
| 771 |
paragraph, since the line breaking algorithm needed to use two passes on only |
| 772 |
about 5 per cent of the paragraphs. |
| 773 |
|
| 774 |
When a word been set up to contain a candidate for hyphenation, \TeX\ first |
| 775 |
looks to see if it is in the user's exception dictionary. If not, hyphens are |
| 776 |
inserted based on patterns that appear within the given word, using an |
| 777 |
algorithm due to Frank~M. Liang. |
| 778 |
|
| 779 |
This is incompatible with \TEX\ because the first word of a paragraph can be |
| 780 |
hyphenated, but most European users seem to agree that prohibiting |
| 781 |
hyphenation there was not the best idea ever. |
| 782 |
|
| 783 |
We have some variants available that are controlled by the paremeter |
| 784 |
\type {\hyphenationbounds}: |
| 785 |
|
| 786 |
\starttabulate |
| 787 |
\NC \type {0} \NC not strict \NC \NR |
| 788 |
\NC \type {1} \NC strict start \NC \NR |
| 789 |
\NC \type {2} \NC strict end \NC \NR |
| 790 |
\NC \type {3} \NC strict start and strict end \NC \NR |
| 791 |
\stoptabulate |
| 792 |
|
| 793 |
\startbuffer |
| 794 |
\parindent0pt \hsize=1.1cm |
| 795 |
12-34-56 \par |
| 796 |
12-34-\hbox{56} \par |
| 797 |
12-34-\vrule width 1em height 1.5ex \par |
| 798 |
12-\hbox{34}-56 \par |
| 799 |
12-\vrule width 1em height 1.5ex-56 \par |
| 800 |
\hjcode`\1=`\1 \hjcode`\2=`\2 \hjcode`\3=`\3 \hjcode`\4=`\4 \vskip.5cm |
| 801 |
12-34-56 \par |
| 802 |
12-34-\hbox{56} \par |
| 803 |
12-34-\vrule width 1em height 1.5ex \par |
| 804 |
12-\hbox{34}-56 \par |
| 805 |
12-\vrule width 1em height 1.5ex-56 \par |
| 806 |
\stopbuffer |
| 807 |
|
| 808 |
\typebuffer |
| 809 |
|
| 810 |
\startpacked \getbuffer \stopbuffer |
| 811 |
|
| 812 |
We only accept an explicit hyphen when there is a preceding glyph and we skip |
| 813 |
a sequence of explicit hyphens as that normally indicates a \type {--} or |
| 814 |
\type {---} ligature in which case we can in a worse case usage get bad node |
| 815 |
lists later on due to messed up ligature building as these dashes are |
| 816 |
ligatures in base fonts. This is a side effect of the separating the |
| 817 |
hyphenation, ligaturing and kerning steps. A test is cmr with \type {------}. |
| 818 |
|
| 819 |
A font handler can collapse successive hyphens but it's not nice to put the |
| 820 |
burden there. A somewhat messy border case is \type {----} but in \LUATEX\ we |
| 821 |
don't treat \type {--} and \type {---} special. Also, traditional \TEX\ will |
| 822 |
break a line at \type {-foo} but this can be disabled by setting the |
| 823 |
automatic mode to \type {1}. |
| 824 |
|
| 825 |
*/ |
| 826 |
|
| 827 |
static halfword find_next_wordstart(halfword r, halfword first_language, halfword strict_bound) |
| 828 |
{ |
| 829 |
register int l; |
| 830 |
register int start_ok = 1; |
| 831 |
int mathlevel = 1; |
| 832 |
int chr ; |
| 833 |
halfword t ; |
| 834 |
while (r != null) { |
| 835 |
switch (type(r)) { |
| 836 |
case boundary_node: |
| 837 |
if (subtype(r) == word_boundary) { |
| 838 |
start_ok = 1; |
| 839 |
} |
| 840 |
break; |
| 841 |
case hlist_node: |
| 842 |
case vlist_node: |
| 843 |
case rule_node: |
| 844 |
case dir_node: |
| 845 |
case whatsit_node: |
| 846 |
if (strict_bound == 1 || strict_bound == 3) { |
| 847 |
start_ok = 0; |
| 848 |
} |
| 849 |
break; |
| 850 |
case glue_node: |
| 851 |
start_ok = 1; |
| 852 |
break; |
| 853 |
case math_node: |
| 854 |
while (mathlevel > 0) { |
| 855 |
r = vlink(r); |
| 856 |
if (r == null) |
| 857 |
return r; |
| 858 |
if (type(r) == math_node) { |
| 859 |
if (subtype(r) == before) { |
| 860 |
mathlevel++; |
| 861 |
} else { |
| 862 |
mathlevel--; |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
break; |
| 867 |
case glyph_node: |
| 868 |
if (is_simple_character(r)) { |
| 869 |
chr = character(r) ; |
| 870 |
if (chr == ex_hyphen_char_par) { |
| 871 |
t = vlink(r) ; |
| 872 |
if ((automatic_hyphen_mode_par == 0) && (t != null) && (type(t) == glyph_node) && (character(t) != ex_hyphen_char_par)) { |
| 873 |
/*tex We have no word yet and the next character is a non hyphen. */ |
| 874 |
r = compound_word_break(r, char_lang(r)); |
| 875 |
} else { |
| 876 |
/*tex We jump over the sequence of hyphens. */ |
| 877 |
while ((t != null) && (type(t) == glyph_node) && (character(t) == ex_hyphen_char_par)) { |
| 878 |
r = t ; |
| 879 |
t = vlink(r) ; |
| 880 |
} |
| 881 |
if (t == null) { |
| 882 |
/*tex We reached the end of the list so we have no word start. */ |
| 883 |
return null; |
| 884 |
} |
| 885 |
} |
| 886 |
/*tex We need a restart. */ |
| 887 |
start_ok = 0; |
| 888 |
} else if (start_ok && (char_lang(r)>=first_language) && ((l = get_hj_code(char_lang(r),chr)) > 0)) { |
| 889 |
if (char_uchyph(r) || l == chr || l <= 32) { |
| 890 |
return r; |
| 891 |
} else { |
| 892 |
start_ok = 0; |
| 893 |
} |
| 894 |
} else { |
| 895 |
/*tex We go on. */ |
| 896 |
} |
| 897 |
} |
| 898 |
break; |
| 899 |
default: |
| 900 |
start_ok = 0; |
| 901 |
break; |
| 902 |
} |
| 903 |
r = vlink(r); |
| 904 |
} |
| 905 |
return r; |
| 906 |
} |
| 907 |
|
| 908 |
static int valid_wordend(halfword s, halfword strict_bound) |
| 909 |
{ |
| 910 |
register halfword r = s; |
| 911 |
register int clang = char_lang(s); |
| 912 |
if (r == null) |
| 913 |
return 1; |
| 914 |
while ( (r != null) && |
| 915 |
( (type(r) == glyph_node && is_simple_character(r) && clang == char_lang(r)) |
| 916 |
|| (type(r) == kern_node && (subtype(r) == normal)) |
| 917 |
) |
| 918 |
) { |
| 919 |
r = vlink(r); |
| 920 |
} |
| 921 |
if (r == null || (type(r) == glyph_node && is_simple_character(r) && clang != char_lang(r)) |
| 922 |
|| type(r) == glue_node |
| 923 |
|| type(r) == penalty_node |
| 924 |
|| (type(r) == kern_node && (subtype(r) == explicit_kern || |
| 925 |
subtype(r) == italic_kern || |
| 926 |
subtype(r) == accent_kern )) |
| 927 |
|| ((type(r) == hlist_node || |
| 928 |
type(r) == vlist_node || |
| 929 |
type(r) == rule_node || |
| 930 |
type(r) == dir_node || |
| 931 |
type(r) == whatsit_node || |
| 932 |
type(r) == ins_node || |
| 933 |
type(r) == adjust_node |
| 934 |
) && ! (strict_bound == 2 || strict_bound == 3)) |
| 935 |
|| type(r) == boundary_node |
| 936 |
) |
| 937 |
return 1; |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
void hnj_hyphenation(halfword head, halfword tail) |
| 942 |
{ |
| 943 |
int lchar, i; |
| 944 |
struct tex_language *lang; |
| 945 |
lang_variables langdata; |
| 946 |
char utf8word[(4 * MAX_WORD_LEN) + 1] = { 0 }; |
| 947 |
int wordlen = 0; |
| 948 |
char *hy = utf8word; |
| 949 |
char *replacement = NULL; |
| 950 |
boolean explicit_hyphen = false; |
| 951 |
boolean valid_word = false; |
| 952 |
halfword first_language = first_valid_language_par; |
| 953 |
halfword strict_bound = hyphenation_bounds_par; |
| 954 |
halfword s, r = head, wordstart = null, save_tail1 = null, left = null, right = null; |
| 955 |
halfword expstart = null; |
| 956 |
boolean compound_hyphen = compound_hyphen_mode_par; |
| 957 |
/*tex Let's play safe: */ |
| 958 |
if (tail == null) |
| 959 |
return; |
| 960 |
/*tex |
| 961 |
This first movement assures two things: |
| 962 |
|
| 963 |
\startitemize |
| 964 |
\startitem |
| 965 |
That we won't waste lots of time on something that has been |
| 966 |
handled already (in that case, none of the glyphs match |
| 967 |
|simple_character|). |
| 968 |
\stopitem |
| 969 |
\startitem |
| 970 |
That the first word can be hyphenated. if the movement was not |
| 971 |
explicit, then the indentation at the start of a paragraph list |
| 972 |
would make |find_next_wordstart()| look too far ahead. |
| 973 |
\stopitem |
| 974 |
\stopitemize |
| 975 |
*/ |
| 976 |
while (r != null && (type(r) != glyph_node || !is_simple_character(r))) { |
| 977 |
r = vlink(r); |
| 978 |
} |
| 979 |
/*tex |
| 980 |
This will make |r| a glyph node with subtype character. |
| 981 |
*/ |
| 982 |
r = find_next_wordstart(r,first_language,strict_bound); |
| 983 |
if (r == null) |
| 984 |
return; |
| 985 |
if (tail == null) |
| 986 |
return; |
| 987 |
save_tail1 = vlink(tail); |
| 988 |
s = new_penalty(0,word_penalty); |
| 989 |
couple_nodes(tail, s); |
| 990 |
while (r != null) { |
| 991 |
/*tex This could be |while(1)|, but let's be paranoid: */ |
| 992 |
int clang, lhmin, rhmin, hmin; |
| 993 |
halfword hyf_font; |
| 994 |
halfword end_word = r; |
| 995 |
wordstart = r; |
| 996 |
/*assert(is_simple_character(wordstart));*/ |
| 997 |
if (!(is_simple_character(wordstart))){ |
| 998 |
if (!(is_character(wordstart))) { |
| 999 |
tex_error("the word doesn't start with a character", NULL); |
| 1000 |
} else if (is_ligature(wordstart)) { |
| 1001 |
tex_error("the word starts with a ligature", NULL); |
| 1002 |
} else if (is_ghost(wordstart)) { |
| 1003 |
tex_error("the word starts with a ghost glyph", NULL); |
| 1004 |
} else { |
| 1005 |
tex_error("the word doesn't start with a simple character", NULL); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
hyf_font = font(wordstart); |
| 1009 |
if (hyphen_char(hyf_font) < 0) { |
| 1010 |
/*tex For backward compatibility we set: */ |
| 1011 |
hyf_font = 0; |
| 1012 |
} |
| 1013 |
clang = char_lang(wordstart); |
| 1014 |
lhmin = char_lhmin(wordstart); |
| 1015 |
rhmin = char_rhmin(wordstart); |
| 1016 |
hmin = get_hyphenation_min(clang); |
| 1017 |
langdata.pre_hyphen_char = get_pre_hyphen_char(clang); |
| 1018 |
langdata.post_hyphen_char = get_post_hyphen_char(clang); |
| 1019 |
while ( r != null |
| 1020 |
&& type(r) == glyph_node |
| 1021 |
&& is_simple_character(r) |
| 1022 |
&& clang == char_lang(r) |
| 1023 |
&& ( ( (clang >= first_language) |
| 1024 |
&& (lchar = get_hj_code(clang,character(r))) > 0 |
| 1025 |
) |
| 1026 |
|| ( character(r) == ex_hyphen_char_par |
| 1027 |
&& (lchar = ex_hyphen_char_par) |
| 1028 |
) |
| 1029 |
) |
| 1030 |
) { |
| 1031 |
if (character(r) == ex_hyphen_char_par) { |
| 1032 |
explicit_hyphen = true; |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
wordlen++; |
| 1036 |
if (wordlen >= MAX_WORD_LEN) { |
| 1037 |
while (r && type(r) == glyph_node) { |
| 1038 |
r = vlink(r); |
| 1039 |
} |
| 1040 |
goto PICKUP; |
| 1041 |
} |
| 1042 |
if (lchar <= 32) { |
| 1043 |
if (lchar == 32) { |
| 1044 |
lchar = 0 ; |
| 1045 |
} |
| 1046 |
if (wordlen <= lhmin) { |
| 1047 |
lhmin = lhmin - lchar + 1 ; |
| 1048 |
if (lhmin < 0) |
| 1049 |
lhmin = 1; |
| 1050 |
} |
| 1051 |
if (wordlen >= rhmin) { |
| 1052 |
rhmin = rhmin - lchar + 1 ; |
| 1053 |
if (rhmin < 0) |
| 1054 |
rhmin = 1; |
| 1055 |
} |
| 1056 |
hmin = hmin - lchar + 1 ; |
| 1057 |
if (hmin < 0) |
| 1058 |
rhmin = 1; |
| 1059 |
lchar = character(r) ; |
| 1060 |
} |
| 1061 |
hy = uni2string(hy, (unsigned) lchar); |
| 1062 |
end_word = r; |
| 1063 |
r = vlink(r); |
| 1064 |
} |
| 1065 |
if (explicit_hyphen == true) { |
| 1066 |
/*tex we are not at the start, so we only need to look ahead */ |
| 1067 |
halfword t = vlink(r) ; |
| 1068 |
if ((automatic_hyphen_mode_par == 0 || automatic_hyphen_mode_par == 1) && (t != null) && ((type(t) == glyph_node) && is_simple_character(t) && (character(t) != ex_hyphen_char_par))) { |
| 1069 |
/*tex we have a word already but the next character may not be a hyphen too */ |
| 1070 |
r = compound_word_break(r, char_lang(r)); |
| 1071 |
if (compound_hyphen) { |
| 1072 |
if (expstart == null) { |
| 1073 |
expstart = wordstart; |
| 1074 |
} |
| 1075 |
explicit_hyphen = false; |
| 1076 |
hy = uni2string(hy, '-'); |
| 1077 |
r = t; |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
} else { |
| 1081 |
/*tex we jump over the sequence of hyphens */ |
| 1082 |
while ((t != null) && (type(t) == glyph_node) && is_simple_character(t) && (character(t) == ex_hyphen_char_par)) { |
| 1083 |
r = t ; |
| 1084 |
t = vlink(r) ; |
| 1085 |
} |
| 1086 |
if (t == null) { |
| 1087 |
/*tex we reached the end of the list and will quit the loop later */ |
| 1088 |
r = null; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} else if ( valid_wordend(r,strict_bound) |
| 1092 |
&& clang >= first_language |
| 1093 |
&& wordlen >= lhmin + rhmin |
| 1094 |
&& (hmin <= 0 || wordlen >= hmin) |
| 1095 |
&& (hyf_font != 0) |
| 1096 |
&& (lang = tex_languages[clang]) != NULL |
| 1097 |
) { |
| 1098 |
*hy = 0; |
| 1099 |
/*tex |
| 1100 |
this is messy and nasty: we can have a word with a - in it which |
| 1101 |
is why we have two branches |
| 1102 |
*/ |
| 1103 |
if (lang->exceptions != 0 && (replacement = hyphenation_exception(lang->exceptions, utf8word)) != NULL) { |
| 1104 |
/*tex handle the exception and go on to the next word */ |
| 1105 |
if (expstart == null) { |
| 1106 |
do_exception(wordstart, r, replacement); |
| 1107 |
} else { |
| 1108 |
do_exception(expstart,r,replacement); |
| 1109 |
} |
| 1110 |
free(replacement); |
| 1111 |
} else if (expstart != null) { |
| 1112 |
/*tex We're done already */ |
| 1113 |
} else if (lang->patterns != NULL) { |
| 1114 |
/*tex A safeguard, not needed but doesn't hurt either: */ |
| 1115 |
valid_word = true; |
| 1116 |
left = wordstart; |
| 1117 |
for (i = lhmin; i > 1; i--) { |
| 1118 |
left = vlink(left); |
| 1119 |
if (left == null) { |
| 1120 |
valid_word = false; |
| 1121 |
break ; |
| 1122 |
} |
| 1123 |
while (!is_simple_character(left)) { |
| 1124 |
left = vlink(left); |
| 1125 |
if (left == null) { |
| 1126 |
valid_word = false; |
| 1127 |
break ; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
if (valid_word) { |
| 1132 |
right = r; |
| 1133 |
if (right == left) { |
| 1134 |
valid_word = false; |
| 1135 |
break ; |
| 1136 |
} |
| 1137 |
for (i = rhmin; i > 0; i--) { |
| 1138 |
right = alink(right); |
| 1139 |
if (right == null || right == left) { |
| 1140 |
valid_word = false; |
| 1141 |
break ; |
| 1142 |
} |
| 1143 |
while (!is_simple_character(right)) { |
| 1144 |
right = alink(right); |
| 1145 |
if (right == null || right == left) { |
| 1146 |
valid_word = false; |
| 1147 |
break ; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
} |
| 1151 |
if (valid_word && expstart == null) { |
| 1152 |
(void) hnj_hyphen_hyphenate(lang->patterns, wordstart, end_word, wordlen, left, right, &langdata); |
| 1153 |
} else { |
| 1154 |
/*tex nothing yet */ |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
PICKUP: |
| 1160 |
expstart = null ; |
| 1161 |
explicit_hyphen = false; |
| 1162 |
wordlen = 0; |
| 1163 |
hy = utf8word; |
| 1164 |
if (r == null) |
| 1165 |
break; |
| 1166 |
r = find_next_wordstart(r,first_language,strict_bound); |
| 1167 |
} |
| 1168 |
flush_node(vlink(tail)); |
| 1169 |
vlink(tail) = save_tail1; |
| 1170 |
} |
| 1171 |
|
| 1172 |
void new_hyphenation(halfword head, halfword tail) |
| 1173 |
{ |
| 1174 |
int i, top; |
| 1175 |
register int callback_id = 0; |
| 1176 |
if (head == null || vlink(head) == null) |
| 1177 |
return; |
| 1178 |
fix_node_list(head); |
| 1179 |
callback_id = callback_defined(hyphenate_callback); |
| 1180 |
if (callback_id > 0) { |
| 1181 |
top = lua_gettop(Luas); |
| 1182 |
if (!get_callback(Luas, callback_id)) { |
| 1183 |
lua_settop(Luas, top); |
| 1184 |
return; |
| 1185 |
} |
| 1186 |
nodelist_to_lua(Luas, head); |
| 1187 |
nodelist_to_lua(Luas, tail); |
| 1188 |
if ((i=lua_pcall(Luas, 2, 0, 0)) != 0) { |
| 1189 |
formatted_warning("hyphenation","bad specification: %s",lua_tostring(Luas, -1)); |
| 1190 |
lua_settop(Luas, top); |
| 1191 |
luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
lua_settop(Luas, top); |
| 1195 |
} else if (callback_id == 0) { |
| 1196 |
hnj_hyphenation(head, tail); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
/*tex |
| 1201 |
|
| 1202 |
Dumping and undumping languages: |
| 1203 |
|
| 1204 |
*/ |
| 1205 |
|
| 1206 |
#define dump_string(a) \ |
| 1207 |
if (a!=NULL) { \ |
| 1208 |
x = (int)strlen(a)+1; \ |
| 1209 |
dump_int(x); dump_things(*a, x); \ |
| 1210 |
} else { \ |
| 1211 |
x = 0; dump_int(x); \ |
| 1212 |
} |
| 1213 |
|
| 1214 |
static void dump_one_language(int i) |
| 1215 |
{ |
| 1216 |
char *s = NULL; |
| 1217 |
int x = 0; |
| 1218 |
struct tex_language *lang; |
| 1219 |
lang = tex_languages[i]; |
| 1220 |
dump_int(lang->id); |
| 1221 |
dump_int(lang->pre_hyphen_char); |
| 1222 |
dump_int(lang->post_hyphen_char); |
| 1223 |
dump_int(lang->pre_exhyphen_char); |
| 1224 |
dump_int(lang->post_exhyphen_char); |
| 1225 |
dump_int(lang->hyphenation_min); |
| 1226 |
if (lang->patterns != NULL) { |
| 1227 |
s = (char *) hnj_serialize(lang->patterns); |
| 1228 |
} |
| 1229 |
dump_string(s); |
| 1230 |
if (s != NULL) { |
| 1231 |
free(s); |
| 1232 |
s = NULL; |
| 1233 |
} |
| 1234 |
if (lang->exceptions != 0) |
| 1235 |
s = exception_strings(lang); |
| 1236 |
dump_string(s); |
| 1237 |
if (s != NULL) { |
| 1238 |
free(s); |
| 1239 |
} |
| 1240 |
free(lang); |
| 1241 |
} |
| 1242 |
|
| 1243 |
void dump_language_data(void) |
| 1244 |
{ |
| 1245 |
int i; |
| 1246 |
dump_int(next_lang_id); |
| 1247 |
for (i = 0; i < next_lang_id; i++) { |
| 1248 |
if (tex_languages[i]) { |
| 1249 |
dump_int(1); |
| 1250 |
dump_one_language(i); |
| 1251 |
} else { |
| 1252 |
dump_int(0); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
static void undump_one_language(int i) |
| 1258 |
{ |
| 1259 |
char *s = NULL; |
| 1260 |
int x = 0; |
| 1261 |
struct tex_language *lang = get_language(i); |
| 1262 |
undump_int(x); |
| 1263 |
lang->id = x; |
| 1264 |
undump_int(x); |
| 1265 |
lang->pre_hyphen_char = x; |
| 1266 |
undump_int(x); |
| 1267 |
lang->post_hyphen_char = x; |
| 1268 |
undump_int(x); |
| 1269 |
lang->pre_exhyphen_char = x; |
| 1270 |
undump_int(x); |
| 1271 |
lang->post_exhyphen_char = x; |
| 1272 |
undump_int(x); |
| 1273 |
lang->hyphenation_min = x; |
| 1274 |
/*tex patterns */ |
| 1275 |
undump_int(x); |
| 1276 |
if (x > 0) { |
| 1277 |
s = xmalloc((unsigned) x); |
| 1278 |
undump_things(*s, x); |
| 1279 |
load_patterns(lang, (unsigned char *) s); |
| 1280 |
free(s); |
| 1281 |
} |
| 1282 |
/*tex exceptions */ |
| 1283 |
undump_int(x); |
| 1284 |
if (x > 0) { |
| 1285 |
s = xmalloc((unsigned) x); |
| 1286 |
undump_things(*s, x); |
| 1287 |
load_hyphenation(lang, (unsigned char *) s); |
| 1288 |
free(s); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
void undump_language_data(void) |
| 1293 |
{ |
| 1294 |
int i, x, numlangs; |
| 1295 |
undump_int(numlangs); |
| 1296 |
next_lang_id = numlangs; |
| 1297 |
for (i = 0; i < numlangs; i++) { |
| 1298 |
undump_int(x); |
| 1299 |
if (x == 1) { |
| 1300 |
undump_one_language(i); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
/*tex |
| 1306 |
|
| 1307 |
When \TeX\ has scanned `\.{\\hyphenation}', it calls on a procedure named |
| 1308 |
|new_hyph_exceptions| to do the right thing. |
| 1309 |
|
| 1310 |
*/ |
| 1311 |
|
| 1312 |
void new_hyph_exceptions(void) |
| 1313 |
{ |
| 1314 |
(void) scan_toks(false, true); |
| 1315 |
load_tex_hyphenation(language_par, def_ref); |
| 1316 |
flush_list(def_ref); |
| 1317 |
} |
| 1318 |
|
| 1319 |
/* |
| 1320 |
Similarly, when \TeX\ has scanned `\.{\\patterns}', it calls on a procedure |
| 1321 |
named |new_patterns|. |
| 1322 |
|
| 1323 |
*/ |
| 1324 |
|
| 1325 |
void new_patterns(void) |
| 1326 |
{ |
| 1327 |
(void) scan_toks(false, true); |
| 1328 |
load_tex_patterns(language_par, def_ref); |
| 1329 |
flush_list(def_ref); |
| 1330 |
} |
| 1331 |
|
| 1332 |
/*tex |
| 1333 |
|
| 1334 |
`\.{\\prehyphenchar}', sets the |pre_break| character, and |
| 1335 |
`\.{\\posthyphenchar}' the |post_break| character. Their respective defaults |
| 1336 |
are ascii hyphen ("-") and zero (nul). |
| 1337 |
|
| 1338 |
*/ |
| 1339 |
|
| 1340 |
void new_pre_hyphen_char(void) |
| 1341 |
{ |
| 1342 |
scan_optional_equals(); |
| 1343 |
scan_int(); |
| 1344 |
set_pre_hyphen_char(language_par, cur_val); |
| 1345 |
} |
| 1346 |
|
| 1347 |
void new_post_hyphen_char(void) |
| 1348 |
{ |
| 1349 |
scan_optional_equals(); |
| 1350 |
scan_int(); |
| 1351 |
set_post_hyphen_char(language_par, cur_val); |
| 1352 |
} |
| 1353 |
|
| 1354 |
/*tex |
| 1355 |
|
| 1356 |
`\.{\\preexhyphenchar}', sets the |pre_break| character, and |
| 1357 |
`\.{\\postexhyphenchar}' the |post_break| character. Their defaults are both |
| 1358 |
zero (nul). |
| 1359 |
|
| 1360 |
*/ |
| 1361 |
|
| 1362 |
void new_pre_exhyphen_char(void) |
| 1363 |
{ |
| 1364 |
scan_optional_equals(); |
| 1365 |
scan_int(); |
| 1366 |
set_pre_exhyphen_char(language_par, cur_val); |
| 1367 |
} |
| 1368 |
|
| 1369 |
void new_post_exhyphen_char(void) |
| 1370 |
{ |
| 1371 |
scan_optional_equals(); |
| 1372 |
scan_int(); |
| 1373 |
set_post_exhyphen_char(language_par, cur_val); |
| 1374 |
} |
| 1375 |
|
| 1376 |
void new_hyphenation_min(void) |
| 1377 |
{ |
| 1378 |
scan_optional_equals(); |
| 1379 |
scan_int(); |
| 1380 |
set_hyphenation_min(language_par, cur_val); |
| 1381 |
} |
| 1382 |
|
| 1383 |
void new_hj_code(void) |
| 1384 |
{ |
| 1385 |
int i ; |
| 1386 |
scan_int(); |
| 1387 |
i = cur_val; |
| 1388 |
scan_optional_equals(); |
| 1389 |
scan_int(); |
| 1390 |
set_hj_code(language_par, i, cur_val, -1); |
| 1391 |
} |