| 1 |
/* |
| 2 |
|
| 3 |
mathnodes.w |
| 4 |
|
| 5 |
Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org> |
| 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 |
|
| 26 |
/*tex |
| 27 |
|
| 28 |
We support the traditional math codes as well as larger ones suitable for |
| 29 |
\UNICODE\ input and fonts. |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
static sa_tree mathcode_head = NULL; |
| 34 |
|
| 35 |
/*tex the |0xFFFFFFFF| is a flag value. */ |
| 36 |
|
| 37 |
#define MATHCODESTACK 8 |
| 38 |
#define MATHCODEDEFAULT 0xFFFFFFFF |
| 39 |
#define MATHCODEACTIVE 0xFFFFFFFE |
| 40 |
|
| 41 |
/*tex |
| 42 |
|
| 43 |
Delcodes are also went larger. |
| 44 |
|
| 45 |
*/ |
| 46 |
|
| 47 |
static sa_tree delcode_head = NULL; |
| 48 |
|
| 49 |
#define DELCODESTACK 4 |
| 50 |
#define DELCODEDEFAULT 0xFFFFFFFF |
| 51 |
|
| 52 |
/*tex |
| 53 |
|
| 54 |
We now get lots of helpers for definitions and printing. The storage model |
| 55 |
that we use is different because we can have many more so we need to be |
| 56 |
sparse. Therefore we use trees. |
| 57 |
|
| 58 |
*/ |
| 59 |
|
| 60 |
|
| 61 |
#define print_hex_digit(A) do { \ |
| 62 |
if ((A)>=10) print_char('A'+(A)-10); \ |
| 63 |
else print_char('0'+(A)); \ |
| 64 |
} while (0) |
| 65 |
|
| 66 |
#define two_hex(A) do { \ |
| 67 |
print_hex_digit((A)/16); \ |
| 68 |
print_hex_digit((A)%16); \ |
| 69 |
} while (0) |
| 70 |
|
| 71 |
#define four_hex(A) do { \ |
| 72 |
two_hex((A)/256); \ |
| 73 |
two_hex((A)%256); \ |
| 74 |
} while (0) |
| 75 |
|
| 76 |
#define six_hex(A) do { \ |
| 77 |
two_hex((A)/65536); \ |
| 78 |
two_hex(((A)%65536)/256); \ |
| 79 |
two_hex((A)%256); \ |
| 80 |
} while (0) |
| 81 |
|
| 82 |
mathcodeval mathchar_from_integer(int value, int extcode) |
| 83 |
{ |
| 84 |
mathcodeval mval; |
| 85 |
if (extcode == tex_mathcode) { |
| 86 |
mval.class_value = (value / 0x1000); |
| 87 |
mval.family_value = ((value % 0x1000) / 0x100); |
| 88 |
mval.character_value = (value % 0x100); |
| 89 |
} else { |
| 90 |
int mfam = (value / 0x200000) & 0x7FF; |
| 91 |
mval.class_value = mfam % 0x08; |
| 92 |
mval.family_value = mfam / 0x08; |
| 93 |
mval.character_value = value & 0x1FFFFF; |
| 94 |
} |
| 95 |
return mval; |
| 96 |
} |
| 97 |
|
| 98 |
void show_mathcode_value_old(int value) |
| 99 |
{ |
| 100 |
print_char('"'); |
| 101 |
four_hex(value); |
| 102 |
} |
| 103 |
|
| 104 |
void show_mathcode_value(mathcodeval c) |
| 105 |
{ |
| 106 |
print_char('"'); |
| 107 |
print_hex_digit(c.class_value); |
| 108 |
print_char('"'); |
| 109 |
two_hex(c.family_value); |
| 110 |
print_char('"'); |
| 111 |
six_hex(c.character_value); |
| 112 |
} |
| 113 |
|
| 114 |
static void show_mathcode(int n) |
| 115 |
{ |
| 116 |
mathcodeval c = get_math_code(n); |
| 117 |
tprint_esc("Umathcode"); |
| 118 |
print_int(n); |
| 119 |
print_char('='); |
| 120 |
show_mathcode_value(c); |
| 121 |
} |
| 122 |
|
| 123 |
static void unsavemathcode(quarterword gl) |
| 124 |
{ |
| 125 |
sa_stack_item st; |
| 126 |
if (mathcode_head->stack == NULL) |
| 127 |
return; |
| 128 |
while (mathcode_head->stack_ptr > 0 && abs(mathcode_head->stack[mathcode_head->stack_ptr].level) >= gl) { |
| 129 |
st = mathcode_head->stack[mathcode_head->stack_ptr]; |
| 130 |
if (st.level > 0) { |
| 131 |
rawset_sa_item(mathcode_head, st.code, st.value); |
| 132 |
if (tracing_restores_par > 1) { |
| 133 |
begin_diagnostic(); |
| 134 |
print_char('{'); |
| 135 |
tprint("restoring"); |
| 136 |
print_char(' '); |
| 137 |
show_mathcode(st.code); |
| 138 |
print_char('}'); |
| 139 |
end_diagnostic(false); |
| 140 |
} |
| 141 |
} |
| 142 |
(mathcode_head->stack_ptr)--; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void set_math_code(int n, int mathclass, int mathfamily, int mathcharacter, quarterword level) |
| 147 |
{ |
| 148 |
sa_tree_item v; |
| 149 |
if (mathclass == 8 && mathfamily == 0 && mathcharacter == 0) { |
| 150 |
v.uint_value = MATHCODEACTIVE; |
| 151 |
} else { |
| 152 |
v.math_code_value.class_value = mathclass; |
| 153 |
v.math_code_value.family_value = mathfamily; |
| 154 |
v.math_code_value.character_value = mathcharacter; |
| 155 |
} |
| 156 |
set_sa_item(mathcode_head, n, v, level); |
| 157 |
if (tracing_assigns_par > 1) { |
| 158 |
begin_diagnostic(); |
| 159 |
print_char('{'); |
| 160 |
tprint("assigning"); |
| 161 |
print_char(' '); |
| 162 |
show_mathcode(n); |
| 163 |
print_char('}'); |
| 164 |
end_diagnostic(false); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
mathcodeval get_math_code(int n) |
| 169 |
{ |
| 170 |
mathcodeval d; |
| 171 |
sa_tree_item v = get_sa_item(mathcode_head, n); |
| 172 |
if (v.uint_value == MATHCODEDEFAULT) { |
| 173 |
d.class_value = 0; |
| 174 |
d.family_value = 0; |
| 175 |
d.character_value = n; |
| 176 |
} else if (v.uint_value == MATHCODEACTIVE) { |
| 177 |
d.class_value = 8; |
| 178 |
d.family_value = 0; |
| 179 |
d.character_value = 0; |
| 180 |
} else { |
| 181 |
d.class_value = v.math_code_value.class_value; |
| 182 |
if (d.class_value == 8) { |
| 183 |
d.family_value = 0; |
| 184 |
d.character_value = n; |
| 185 |
} else { |
| 186 |
d.family_value = v.math_code_value.family_value; |
| 187 |
d.character_value = v.math_code_value.character_value; |
| 188 |
} |
| 189 |
} |
| 190 |
return d; |
| 191 |
} |
| 192 |
|
| 193 |
int get_math_code_num(int n) |
| 194 |
{ |
| 195 |
mathcodeval d = get_math_code(n); |
| 196 |
return (d.class_value + (d.family_value * 8)) * (65536 * 32) + d.character_value; |
| 197 |
} |
| 198 |
|
| 199 |
static void initializemathcode(void) |
| 200 |
{ |
| 201 |
sa_tree_item sa_value = { 0 }; |
| 202 |
sa_value.uint_value = MATHCODEDEFAULT; |
| 203 |
mathcode_head = new_sa_tree(MATHCODESTACK, 1, sa_value); |
| 204 |
} |
| 205 |
|
| 206 |
static void dumpmathcode(void) |
| 207 |
{ |
| 208 |
dump_sa_tree(mathcode_head,"mathcodes"); |
| 209 |
} |
| 210 |
|
| 211 |
static void undumpmathcode(void) |
| 212 |
{ |
| 213 |
mathcode_head = undump_sa_tree("mathcodes"); |
| 214 |
} |
| 215 |
|
| 216 |
static void show_delcode(int n) |
| 217 |
{ |
| 218 |
delcodeval c; |
| 219 |
c = get_del_code(n); |
| 220 |
tprint_esc("Udelcode"); |
| 221 |
print_int(n); |
| 222 |
print_char('='); |
| 223 |
if (c.small_family_value < 0) { |
| 224 |
print_char('-'); |
| 225 |
print_char('1'); |
| 226 |
} else { |
| 227 |
print_char('"'); |
| 228 |
two_hex(c.small_family_value); |
| 229 |
six_hex(c.small_character_value); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static void unsavedelcode(quarterword gl) |
| 234 |
{ |
| 235 |
sa_stack_item st; |
| 236 |
if (delcode_head->stack == NULL) |
| 237 |
return; |
| 238 |
while (delcode_head->stack_ptr > 0 && abs(delcode_head->stack[delcode_head->stack_ptr].level) >= gl) { |
| 239 |
st = delcode_head->stack[delcode_head->stack_ptr]; |
| 240 |
if (st.level > 0) { |
| 241 |
rawset_sa_item(delcode_head, st.code, st.value); |
| 242 |
if (tracing_restores_par > 1) { |
| 243 |
begin_diagnostic(); |
| 244 |
print_char('{'); |
| 245 |
tprint("restoring"); |
| 246 |
print_char(' '); |
| 247 |
show_delcode(st.code); |
| 248 |
print_char('}'); |
| 249 |
end_diagnostic(false); |
| 250 |
} |
| 251 |
} |
| 252 |
(delcode_head->stack_ptr)--; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
void set_del_code(int n, int smathfamily, int smathcharacter, int lmathfamily, int lmathcharacter, quarterword gl) |
| 257 |
{ |
| 258 |
sa_tree_item v; |
| 259 |
v.del_code_value.class_value = 0; |
| 260 |
v.del_code_value.small_family_value = smathfamily; |
| 261 |
v.del_code_value.small_character_value = smathcharacter; |
| 262 |
v.del_code_value.dummy_value = 0; |
| 263 |
v.del_code_value.large_family_value = lmathfamily; |
| 264 |
v.del_code_value.large_character_value = lmathcharacter; |
| 265 |
/*tex Always global! */ |
| 266 |
set_sa_item(delcode_head, n, v, gl); |
| 267 |
if (tracing_assigns_par > 1) { |
| 268 |
begin_diagnostic(); |
| 269 |
print_char('{'); |
| 270 |
tprint("assigning"); |
| 271 |
print_char(' '); |
| 272 |
show_delcode(n); |
| 273 |
print_char('}'); |
| 274 |
end_diagnostic(false); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
delcodeval get_del_code(int n) |
| 279 |
{ |
| 280 |
delcodeval d; |
| 281 |
sa_tree_item v = get_sa_item(delcode_head, n); |
| 282 |
if (v.uint_value == DELCODEDEFAULT) { |
| 283 |
d.class_value = 0; |
| 284 |
d.small_family_value = -1; |
| 285 |
d.small_character_value = 0; |
| 286 |
d.large_family_value = 0; |
| 287 |
d.large_character_value = 0; |
| 288 |
} else { |
| 289 |
d.class_value = v.del_code_value.class_value; |
| 290 |
d.small_family_value = v.del_code_value.small_family_value; |
| 291 |
d.small_character_value = v.del_code_value.small_character_value; |
| 292 |
d.large_family_value = v.del_code_value.large_family_value; |
| 293 |
d.large_character_value = v.del_code_value.large_character_value; |
| 294 |
} |
| 295 |
return d; |
| 296 |
} |
| 297 |
|
| 298 |
/*tex |
| 299 |
|
| 300 |
This really only works for old-style delcodes! |
| 301 |
|
| 302 |
*/ |
| 303 |
|
| 304 |
int get_del_code_num(int n) |
| 305 |
{ |
| 306 |
delcodeval d = get_del_code(n); |
| 307 |
if (d.small_family_value < 0) { |
| 308 |
return -1; |
| 309 |
} else { |
| 310 |
return ((d.small_family_value * 256 + d.small_character_value) * 4096 + |
| 311 |
(d.large_family_value * 256) + d.large_character_value); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static void initializedelcode(void) |
| 316 |
{ |
| 317 |
sa_tree_item sa_value = { 0 }; |
| 318 |
sa_value.uint_value = DELCODEDEFAULT; |
| 319 |
delcode_head = new_sa_tree(DELCODESTACK, 2, sa_value); |
| 320 |
} |
| 321 |
|
| 322 |
static void dumpdelcode(void) |
| 323 |
{ |
| 324 |
dump_sa_tree(delcode_head,"delcodes"); |
| 325 |
} |
| 326 |
|
| 327 |
static void undumpdelcode(void) |
| 328 |
{ |
| 329 |
delcode_head = undump_sa_tree("delcodes"); |
| 330 |
} |
| 331 |
|
| 332 |
void unsave_math_codes(quarterword grouplevel) |
| 333 |
{ |
| 334 |
unsavemathcode(grouplevel); |
| 335 |
unsavedelcode(grouplevel); |
| 336 |
} |
| 337 |
|
| 338 |
void initialize_math_codes(void) |
| 339 |
{ |
| 340 |
initializemathcode(); |
| 341 |
initializedelcode(); |
| 342 |
} |
| 343 |
|
| 344 |
void free_math_codes(void) |
| 345 |
{ |
| 346 |
destroy_sa_tree(mathcode_head); |
| 347 |
destroy_sa_tree(delcode_head); |
| 348 |
} |
| 349 |
|
| 350 |
void dump_math_codes(void) |
| 351 |
{ |
| 352 |
dumpmathcode(); |
| 353 |
dumpdelcode(); |
| 354 |
} |
| 355 |
|
| 356 |
void undump_math_codes(void) |
| 357 |
{ |
| 358 |
undumpmathcode(); |
| 359 |
undumpdelcode(); |
| 360 |
} |