Debian Codegraph @

texk/web2c/euptexdir/pdfutils.ch

raw
1 %% Support for some primitives defined in pdfTeX
2 %%
3 %% \pdfstrcmp: need for LaTeX3
4 %% In comparison, Japanese characters will be always encoded in UTF-8.
5 %%
6 %% \pdffilemoddate and co.: for standalone package
7 %% (\pdfcreationdate, \pdffilemoddate, \pdffilesize)
8 %%
9 %% \pdfsavepos and co.
10 %% (\pdfsavepos, \pdfpage{width,height}, \pdflast{x,y}pos)
11 %%
12 %% \pdffiledump: for bmpsize package by Heiko Oberdiek
13 %%
14 %% \pdfshellescape: by doraTeX's request
15 %%
16 %% \pdfmdfivesum: by Akira's request
17 %% As \pdfstrcmp, Japanese characters will be always encoded in UTF-8 in
18 %% \pdfmdfivesum {...}. (no conversion for \pdfmdfivesum file <filename>)
19 %%
20 %% \pdfprimitive and \ifpdfprimitive: for LaTeX3 (2015/07/15)
21 %%
22 %% \pdfuniformdeviate and co.:
23 %% (\pdfnormaldeviate, \pdfrandomseed, \pdfsetrandomseed)
24 %%
25 %% \pdfelapsedtime and \pdfresettimer
26 %%
27 %% \expanded
28 %%
29 %% \ifincsname
30 %%
31 %% \Uchar, \Ucharcat
32 %%
33 %% \vadjust pre (2021-07-01)
34
35 @x
36 @* \[8] Packed data.
37 @y
38 @* \[7b] Random numbers.
39
40 \font\tenlogo=logo10 % font used for the METAFONT logo
41 \def\MP{{\tenlogo META}\-{\tenlogo POST}}
42 \def\pdfTeX{pdf\TeX}
43
44 This section is (almost) straight from MetaPost. I had to change
45 the types (use |integer| instead of |fraction|), but that should
46 not have any influence on the actual calculations (the original
47 comments refer to quantities like |fraction_four| ($2^{30}$), and
48 that is the same as the numeric representation of |maxdimen|).
49
50 I've copied the low-level variables and routines that are needed, but
51 only those (e.g. |m_log|), not the accompanying ones like |m_exp|. Most
52 of the following low-level numeric routines are only needed within the
53 calculation of |norm_rand|. I've been forced to rename |make_fraction|
54 to |make_frac| because TeX already has a routine by that name with
55 a wholly different function (it creates a |fraction_noad| for math
56 typesetting) -- Taco
57
58 And now let's complete our collection of numeric utility routines
59 by considering random number generation.
60 \MP\ generates pseudo-random numbers with the additive scheme recommended
61 in Section 3.6 of {\sl The Art of Computer Programming}; however, the
62 results are random fractions between 0 and |fraction_one-1|, inclusive.
63
64 There's an auxiliary array |randoms| that contains 55 pseudo-random
65 fractions. Using the recurrence $x_n=(x_{n-55}-x_{n-31})\bmod 2^{28}$,
66 we generate batches of 55 new $x_n$'s at a time by calling |new_randoms|.
67 The global variable |j_random| tells which element has most recently
68 been consumed.
69
70 @<Glob...@>=
71 @!randoms:array[0..54] of integer; {the last 55 random values generated}
72 @!j_random:0..54; {the number of unused |randoms|}
73 @!random_seed:scaled; {the default random seed}
74
75 @ A small bit of metafont is needed.
76
77 @d fraction_half==@'1000000000 {$2^{27}$, represents 0.50000000}
78 @d fraction_one==@'2000000000 {$2^{28}$, represents 1.00000000}
79 @d fraction_four==@'10000000000 {$2^{30}$, represents 4.00000000}
80 @d el_gordo == @'17777777777 {$2^{31}-1$, the largest value that \MP\ likes}
81 @d halfp(#)==(#) div 2
82 @d double(#) == #:=#+# {multiply a variable by two}
83
84 @ The |make_frac| routine produces the |fraction| equivalent of
85 |p/q|, given integers |p| and~|q|; it computes the integer
86 $f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are
87 positive. If |p| and |q| are both of the same scaled type |t|,
88 the ``type relation'' |make_frac(t,t)=fraction| is valid;
89 and it's also possible to use the subroutine ``backwards,'' using
90 the relation |make_frac(t,fraction)=t| between scaled types.
91
92 If the result would have magnitude $2^{31}$ or more, |make_frac|
93 sets |arith_error:=true|. Most of \MP's internal computations have
94 been designed to avoid this sort of error.
95
96 If this subroutine were programmed in assembly language on a typical
97 machine, we could simply compute |(@t$2^{28}$@>*p)div q|, since a
98 double-precision product can often be input to a fixed-point division
99 instruction. But when we are restricted to \PASCAL\ arithmetic it
100 is necessary either to resort to multiple-precision maneuvering
101 or to use a simple but slow iteration. The multiple-precision technique
102 would be about three times faster than the code adopted here, but it
103 would be comparatively long and tricky, involving about sixteen
104 additional multiplications and divisions.
105
106 This operation is part of \MP's ``inner loop''; indeed, it will
107 consume nearly 10\pct! of the running time (exclusive of input and output)
108 if the code below is left unchanged. A machine-dependent recoding
109 will therefore make \MP\ run faster. The present implementation
110 is highly portable, but slow; it avoids multiplication and division
111 except in the initial stage. System wizards should be careful to
112 replace it with a routine that is guaranteed to produce identical
113 results in all cases.
114 @^system dependencies@>
115
116 As noted below, a few more routines should also be replaced by machine-dependent
117 code, for efficiency. But when a procedure is not part of the ``inner loop,''
118 such changes aren't advisable; simplicity and robustness are
119 preferable to trickery, unless the cost is too high.
120 @^inner loop@>
121
122 @p function make_frac(@!p,@!q:integer):integer;
123 var @!f:integer; {the fraction bits, with a leading 1 bit}
124 @!n:integer; {the integer part of $\vert p/q\vert$}
125 @!negative:boolean; {should the result be negated?}
126 @!be_careful:integer; {disables certain compiler optimizations}
127 begin if p>=0 then negative:=false
128 else begin negate(p); negative:=true;
129 end;
130 if q<=0 then
131 begin debug if q=0 then confusion("/");@;@+gubed@;@/
132 @:this can't happen /}{\quad \./@>
133 negate(q); negative:=not negative;
134 end;
135 n:=p div q; p:=p mod q;
136 if n>=8 then
137 begin arith_error:=true;
138 if negative then make_frac:=-el_gordo@+else make_frac:=el_gordo;
139 end
140 else begin n:=(n-1)*fraction_one;
141 @<Compute $f=\lfloor 2^{28}(1+p/q)+{1\over2}\rfloor$@>;
142 if negative then make_frac:=-(f+n)@+else make_frac:=f+n;
143 end;
144 end;
145
146 @ The |repeat| loop here preserves the following invariant relations
147 between |f|, |p|, and~|q|:
148 (i)~|0<=p<q|; (ii)~$fq+p=2^k(q+p_0)$, where $k$ is an integer and
149 $p_0$ is the original value of~$p$.
150
151 Notice that the computation specifies
152 |(p-q)+p| instead of |(p+p)-q|, because the latter could overflow.
153 Let us hope that optimizing compilers do not miss this point; a
154 special variable |be_careful| is used to emphasize the necessary
155 order of computation. Optimizing compilers should keep |be_careful|
156 in a register, not store it in memory.
157 @^inner loop@>
158
159 @<Compute $f=\lfloor 2^{28}(1+p/q)+{1\over2}\rfloor$@>=
160 f:=1;
161 repeat be_careful:=p-q; p:=be_careful+p;
162 if p>=0 then f:=f+f+1
163 else begin double(f); p:=p+q;
164 end;
165 until f>=fraction_one;
166 be_careful:=p-q;
167 if be_careful+p>=0 then incr(f)
168
169 @
170
171 @p function take_frac(@!q:integer;@!f:integer):integer;
172 var @!p:integer; {the fraction so far}
173 @!negative:boolean; {should the result be negated?}
174 @!n:integer; {additional multiple of $q$}
175 @!be_careful:integer; {disables certain compiler optimizations}
176 begin @<Reduce to the case that |f>=0| and |q>0|@>;
177 if f<fraction_one then n:=0
178 else begin n:=f div fraction_one; f:=f mod fraction_one;
179 if q<=el_gordo div n then n:=n*q
180 else begin arith_error:=true; n:=el_gordo;
181 end;
182 end;
183 f:=f+fraction_one;
184 @<Compute $p=\lfloor qf/2^{28}+{1\over2}\rfloor-q$@>;
185 be_careful:=n-el_gordo;
186 if be_careful+p>0 then
187 begin arith_error:=true; n:=el_gordo-p;
188 end;
189 if negative then take_frac:=-(n+p)
190 else take_frac:=n+p;
191 end;
192
193 @ @<Reduce to the case that |f>=0| and |q>0|@>=
194 if f>=0 then negative:=false
195 else begin negate(f); negative:=true;
196 end;
197 if q<0 then
198 begin negate(q); negative:=not negative;
199 end;
200
201 @ The invariant relations in this case are (i)~$\lfloor(qf+p)/2^k\rfloor
202 =\lfloor qf_0/2^{28}+{1\over2}\rfloor$, where $k$ is an integer and
203 $f_0$ is the original value of~$f$; (ii)~$2^k\L f<2^{k+1}$.
204 @^inner loop@>
205
206 @<Compute $p=\lfloor qf/2^{28}+{1\over2}\rfloor-q$@>=
207 p:=fraction_half; {that's $2^{27}$; the invariants hold now with $k=28$}
208 if q<fraction_four then
209 repeat if odd(f) then p:=halfp(p+q)@+else p:=halfp(p);
210 f:=halfp(f);
211 until f=1
212 else repeat if odd(f) then p:=p+halfp(q-p)@+else p:=halfp(p);
213 f:=halfp(f);
214 until f=1
215
216 @ The subroutines for logarithm and exponential involve two tables.
217 The first is simple: |two_to_the[k]| equals $2^k$. The second involves
218 a bit more calculation, which the author claims to have done correctly:
219 |spec_log[k]| is $2^{27}$ times $\ln\bigl(1/(1-2^{-k})\bigr)=
220 2^{-k}+{1\over2}2^{-2k}+{1\over3}2^{-3k}+\cdots\,$, rounded to the
221 nearest integer.
222
223 @<Glob...@>=
224 @!two_to_the:array[0..30] of integer; {powers of two}
225 @!spec_log:array[1..28] of integer; {special logarithms}
226
227
228 @ @<Set init...@>=
229 two_to_the[0]:=1;
230 for k:=1 to 30 do two_to_the[k]:=2*two_to_the[k-1];
231 spec_log[1]:=93032640;
232 spec_log[2]:=38612034;
233 spec_log[3]:=17922280;
234 spec_log[4]:=8662214;
235 spec_log[5]:=4261238;
236 spec_log[6]:=2113709;
237 spec_log[7]:=1052693;
238 spec_log[8]:=525315;
239 spec_log[9]:=262400;
240 spec_log[10]:=131136;
241 spec_log[11]:=65552;
242 spec_log[12]:=32772;
243 spec_log[13]:=16385;
244 for k:=14 to 27 do spec_log[k]:=two_to_the[27-k];
245 spec_log[28]:=1;
246
247 @
248
249 @p function m_log(@!x:integer):integer;
250 var @!y,@!z:integer; {auxiliary registers}
251 @!k:integer; {iteration counter}
252 begin if x<=0 then @<Handle non-positive logarithm@>
253 else begin y:=1302456956+4-100; {$14\times2^{27}\ln2\approx1302456956.421063$}
254 z:=27595+6553600; {and $2^{16}\times .421063\approx 27595$}
255 while x<fraction_four do
256 begin double(x); y:=y-93032639; z:=z-48782;
257 end; {$2^{27}\ln2\approx 93032639.74436163$
258 and $2^{16}\times.74436163\approx 48782$}
259 y:=y+(z div unity); k:=2;
260 while x>fraction_four+4 do
261 @<Increase |k| until |x| can be multiplied by a
262 factor of $2^{-k}$, and adjust $y$ accordingly@>;
263 m_log:=y div 8;
264 end;
265 end;
266
267 @ @<Increase |k| until |x| can...@>=
268 begin z:=((x-1) div two_to_the[k])+1; {$z=\lceil x/2^k\rceil$}
269 while x<fraction_four+z do
270 begin z:=halfp(z+1); k:=k+1;
271 end;
272 y:=y+spec_log[k]; x:=x-z;
273 end
274
275 @ @<Handle non-positive logarithm@>=
276 begin print_err("Logarithm of ");
277 @.Logarithm...replaced by 0@>
278 print_scaled(x); print(" has been replaced by 0");
279 help2("Since I don't take logs of non-positive numbers,")@/
280 ("I'm zeroing this one. Proceed, with fingers crossed.");
281 error; m_log:=0;
282 end
283
284 @ The following somewhat different subroutine tests rigorously if $ab$ is
285 greater than, equal to, or less than~$cd$,
286 given integers $(a,b,c,d)$. In most cases a quick decision is reached.
287 The result is $+1$, 0, or~$-1$ in the three respective cases.
288
289 @d return_sign(#)==begin ab_vs_cd:=#; return;
290 end
291
292 @p function ab_vs_cd(@!a,b,c,d:integer):integer;
293 label exit;
294 var @!q,@!r:integer; {temporary registers}
295 begin @<Reduce to the case that |a,c>=0|, |b,d>0|@>;
296 loop@+ begin q := a div d; r := c div b;
297 if q<>r then
298 if q>r then return_sign(1)@+else return_sign(-1);
299 q := a mod d; r := c mod b;
300 if r=0 then
301 if q=0 then return_sign(0)@+else return_sign(1);
302 if q=0 then return_sign(-1);
303 a:=b; b:=q; c:=d; d:=r;
304 end; {now |a>d>0| and |c>b>0|}
305 exit:end;
306
307 @ @<Reduce to the case that |a...@>=
308 if a<0 then
309 begin negate(a); negate(b);
310 end;
311 if c<0 then
312 begin negate(c); negate(d);
313 end;
314 if d<=0 then
315 begin if b>=0 then
316 if ((a=0)or(b=0))and((c=0)or(d=0)) then return_sign(0)
317 else return_sign(1);
318 if d=0 then
319 if a=0 then return_sign(0)@+else return_sign(-1);
320 q:=a; a:=c; c:=q; q:=-b; b:=-d; d:=q;
321 end
322 else if b<=0 then
323 begin if b<0 then if a>0 then return_sign(-1);
324 if c=0 then return_sign(0) else return_sign(-1);
325 end
326
327 @ To consume a random integer, the program below will say `|next_random|'
328 and then it will fetch |randoms[j_random]|.
329
330 @d next_random==if j_random=0 then new_randoms
331 else decr(j_random)
332
333 @p procedure new_randoms;
334 var @!k:0..54; {index into |randoms|}
335 @!x:integer; {accumulator}
336 begin for k:=0 to 23 do
337 begin x:=randoms[k]-randoms[k+31];
338 if x<0 then x:=x+fraction_one;
339 randoms[k]:=x;
340 end;
341 for k:=24 to 54 do
342 begin x:=randoms[k]-randoms[k-24];
343 if x<0 then x:=x+fraction_one;
344 randoms[k]:=x;
345 end;
346 j_random:=54;
347 end;
348
349 @ To initialize the |randoms| table, we call the following routine.
350
351 @p procedure init_randoms(@!seed:integer);
352 var @!j,@!jj,@!k:integer; {more or less random integers}
353 @!i:0..54; {index into |randoms|}
354 begin j:=abs(seed);
355 while j>=fraction_one do j:=halfp(j);
356 k:=1;
357 for i:=0 to 54 do
358 begin jj:=k; k:=j-k; j:=jj;
359 if k<0 then k:=k+fraction_one;
360 randoms[(i*21)mod 55]:=j;
361 end;
362 new_randoms; new_randoms; new_randoms; {``warm up'' the array}
363 end;
364
365 @ To produce a uniform random number in the range |0<=u<x| or |0>=u>x|
366 or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here.
367
368 Note that the call of |take_frac| will produce the values 0 and~|x|
369 with about half the probability that it will produce any other particular
370 values between 0 and~|x|, because it rounds its answers.
371
372 @p function unif_rand(@!x:integer):integer;
373 var @!y:integer; {trial value}
374 begin next_random; y:=take_frac(abs(x),randoms[j_random]);
375 if y=abs(x) then unif_rand:=0
376 else if x>0 then unif_rand:=y
377 else unif_rand:=-y;
378 end;
379
380 @ Finally, a normal deviate with mean zero and unit standard deviation
381 can readily be obtained with the ratio method (Algorithm 3.4.1R in
382 {\sl The Art of Computer Programming\/}).
383
384 @p function norm_rand:integer;
385 var @!x,@!u,@!l:integer; {what the book would call $2^{16}X$, $2^{28}U$,
386 and $-2^{24}\ln U$}
387 begin repeat
388 repeat next_random;
389 x:=take_frac(112429,randoms[j_random]-fraction_half);
390 {$2^{16}\sqrt{8/e}\approx 112428.82793$}
391 next_random; u:=randoms[j_random];
392 until abs(x)<u;
393 x:=make_frac(x,u);
394 l:=139548960-m_log(u); {$2^{24}\cdot12\ln2\approx139548959.6165$}
395 until ab_vs_cd(1024,l,x,x)>=0;
396 norm_rand:=x;
397 end;
398 @* \[8] Packed data.
399 @z
400
401 % [10] \vadjust pre
402 @x
403 @d adjust_ptr(#)==mem[#+1].int
404 {vertical list to be moved out of horizontal list}
405 @y
406 @d adjust_pre == subtype {<>0 => pre-adjustment}
407 @#{|append_list| is used to append a list to |tail|}
408 @d append_list(#) == begin link(tail) := link(#); append_list_end
409 @d append_list_end(#) == tail := #; end
410
411 @d adjust_ptr(#)==mem[#+1].int
412 {vertical list to be moved out of horizontal list}
413 @z
414
415 % [11] \vadjust pre
416 @x
417 @d hi_mem_stat_min==mem_top-13 {smallest statically allocated word in
418 the one-word |mem|}
419 @d hi_mem_stat_usage=14 {the number of one-word nodes always present}
420 @y
421 @d pre_adjust_head==mem_top-14 {head of pre-adjustment list returned by |hpack|}
422 @d hi_mem_stat_min==mem_top-14 {smallest statically allocated word in
423 the one-word |mem|}
424 @d hi_mem_stat_usage=15 {the number of one-word nodes always present}
425 @z
426
427 @x
428 @* \[12] Displaying boxes.
429 @y
430 @<Declare procedures that need to be declared forward for \pdfTeX@>@;
431
432 @* \[12] Displaying boxes.
433 @z
434
435 % [12] \vadjust pre
436 @x
437 @ @<Display adjustment |p|@>=
438 begin print_esc("vadjust"); node_list_display(adjust_ptr(p)); {recursive call}
439 end
440 @y
441 @ @<Display adjustment |p|@>=
442 begin print_esc("vadjust"); if adjust_pre(p) <> 0 then print(" pre ");
443 node_list_display(adjust_ptr(p)); {recursive call}
444 end
445 @z
446
447 @x \[if]pdfprimitive
448 @d frozen_special=frozen_control_sequence+10
449 {permanent `\.{\\special}'}
450 @d frozen_null_font=frozen_control_sequence+11
451 {permanent `\.{\\nullfont}'}
452 @y
453 @d frozen_special=frozen_control_sequence+10
454 {permanent `\.{\\special}'}
455 @d frozen_primitive=frozen_control_sequence+11
456 {permanent `\.{\\pdfprimitive}'}
457 @d prim_eqtb_base=frozen_primitive+1
458 @d prim_size=2100 {maximum number of primitives }
459 @d frozen_null_font=prim_eqtb_base+prim_size+1
460 {permanent `\.{\\nullfont}'}
461 @z
462
463 @x
464 @d dimen_pars=23 {total number of dimension parameters}
465 @y
466 @d pdf_page_width_code=23 {page width}
467 @d pdf_page_height_code=24 {page height}
468 @d dimen_pars=25 {total number of dimension parameters}
469 @z
470
471 @x \pdfpage{width,height}
472 @d emergency_stretch==dimen_par(emergency_stretch_code)
473 @y
474 @d emergency_stretch==dimen_par(emergency_stretch_code)
475 @d pdf_page_width==dimen_par(pdf_page_width_code)
476 @d pdf_page_height==dimen_par(pdf_page_height_code)
477 @z
478
479 @x \pdfpage{width,height}
480 emergency_stretch_code:print_esc("emergencystretch");
481 @y
482 emergency_stretch_code:print_esc("emergencystretch");
483 pdf_page_width_code: print_esc("pdfpagewidth");
484 pdf_page_height_code: print_esc("pdfpageheight");
485 @z
486
487 @x \[if]pdfprimitive
488 @!cs_count:integer; {total number of known identifiers}
489 @y
490 @!cs_count:integer; {total number of known identifiers}
491
492 @ Primitive support needs a few extra variables and definitions
493
494 @d prim_prime=1777 {about 85\pct! of |primitive_size|}
495 @d prim_base=1
496 @d prim_next(#) == prim[#].lh {link for coalesced lists}
497 @d prim_text(#) == prim[#].rh {string number for control sequence name, plus one}
498 @d prim_is_full == (prim_used=prim_base) {test if all positions are occupied}
499 @d prim_eq_level_field(#)==#.hh.b1
500 @d prim_eq_type_field(#)==#.hh.b0
501 @d prim_equiv_field(#)==#.hh.rh
502 @d prim_eq_level(#)==prim_eq_level_field(eqtb[prim_eqtb_base+#]) {level of definition}
503 @d prim_eq_type(#)==prim_eq_type_field(eqtb[prim_eqtb_base+#]) {command code for equivalent}
504 @d prim_equiv(#)==prim_equiv_field(eqtb[prim_eqtb_base+#]) {equivalent value}
505 @d undefined_primitive=0
506 @d biggest_char=255 { 65535 in XeTeX }
507
508 @<Glob...@>=
509 @!prim: array [0..prim_size] of two_halves; {the primitives table}
510 @!prim_used:pointer; {allocation pointer for |prim|}
511 @z
512
513 @x \[if]pdfprimitive
514 @ @<Set init...@>=
515 no_new_control_sequence:=true; {new identifiers are usually forbidden}
516 @y
517 @ @<Set init...@>=
518 no_new_control_sequence:=true; {new identifiers are usually forbidden}
519 prim_next(0):=0; prim_text(0):=0;
520 for k:=1 to prim_size do prim[k]:=prim[0];
521 @z
522
523 @x \[if]pdfprimitive
524 text(frozen_dont_expand):="notexpanded:";
525 @.notexpanded:@>
526 @y
527 prim_used:=prim_size; {nothing is used}
528 text(frozen_dont_expand):="notexpanded:";
529 @.notexpanded:@>
530 eq_type(frozen_primitive):=ignore_spaces;
531 equiv(frozen_primitive):=1;
532 eq_level(frozen_primitive):=level_one;
533 text(frozen_primitive):="pdfprimitive";
534 @z
535
536 @x \[if]pdfprimitive
537 @ Single-character control sequences do not need to be looked up in a hash
538 table, since we can use the character code itself as a direct address.
539 @y
540 @ Here is the subroutine that searches the primitive table for an identifier
541
542 @p function prim_lookup(@!s:str_number):pointer; {search the primitives table}
543 label found; {go here if you found it}
544 var h:integer; {hash code}
545 @!p:pointer; {index in |hash| array}
546 @!k:pointer; {index in string pool}
547 @!j,@!l:integer;
548 begin
549 if s<=biggest_char then begin
550 if s<0 then begin p:=undefined_primitive; goto found; end
551 else p:=(s mod prim_prime)+prim_base; {we start searching here}
552 l:=1
553 end
554 else begin
555 j:=str_start[s];
556 if s = str_ptr then l := cur_length else l := length(s);
557 @<Compute the primitive code |h|@>;
558 p:=h+prim_base; {we start searching here; note that |0<=h<prim_prime|}
559 end;
560 loop@+begin
561 if prim_text(p)>1+biggest_char then { |p| points a multi-letter primitive }
562 begin if length(prim_text(p)-1)=l then
563 if str_eq_str(prim_text(p)-1,s) then goto found;
564 end
565 else if prim_text(p)=1+s then goto found; { |p| points a single-letter primitive }
566 if prim_next(p)=0 then
567 begin if no_new_control_sequence then
568 p:=undefined_primitive
569 else @<Insert a new primitive after |p|, then make
570 |p| point to it@>;
571 goto found;
572 end;
573 p:=prim_next(p);
574 end;
575 found: prim_lookup:=p;
576 end;
577
578 @ @<Insert a new primitive...@>=
579 begin if prim_text(p)>0 then
580 begin repeat if prim_is_full then overflow("primitive size",prim_size);
581 @:TeX capacity exceeded primitive size}{\quad primitive size@>
582 decr(prim_used);
583 until prim_text(prim_used)=0; {search for an empty location in |prim|}
584 prim_next(p):=prim_used; p:=prim_used;
585 end;
586 prim_text(p):=s+1;
587 end
588
589 @ The value of |prim_prime| should be roughly 85\pct! of
590 |prim_size|, and it should be a prime number.
591
592 @<Compute the primitive code |h|@>=
593 h:=str_pool[j];
594 for k:=j+1 to j+l-1 do
595 begin h:=h+h+str_pool[k];
596 while h>=prim_prime do h:=h-prim_prime;
597 end
598
599 @ Single-character control sequences do not need to be looked up in a hash
600 table, since we can use the character code itself as a direct address.
601 @z
602
603 @x print_cs: \pdfprimitive
604 else begin l:=text(p);
605 @y
606 else begin
607 if (p>=prim_eqtb_base)and(p<frozen_null_font) then
608 l:=prim_text(p-prim_eqtb_base)-1 else l:=text(p);
609 @z
610
611 @x
612 else print_esc(text(p));
613 @y
614 else if (p>=prim_eqtb_base)and(p<frozen_null_font) then
615 print_esc(prim_text(p-prim_eqtb_base)-1)
616 else print_esc(text(p));
617 @z
618
619
620 @x \[if]pdfprimitive
621 @p @!init procedure primitive(@!s:str_number;@!c:quarterword;@!o:halfword);
622 var k:pool_pointer; {index into |str_pool|}
623 @y
624 @p @!init procedure primitive(@!s:str_number;@!c:quarterword;@!o:halfword);
625 var k:pool_pointer; {index into |str_pool|}
626 @!prim_val:integer; {needed to fill |prim_eqtb|}
627 @z
628
629 @x \[if]pdfprimitive
630 begin if s<256 then cur_val:=s+single_base
631 @y
632 begin if s<256 then begin
633 cur_val:=s+single_base;
634 prim_val:=prim_lookup(s);
635 end
636 @z
637
638 @x \[if]pdfprimitive
639 flush_string; text(cur_val):=s; {we don't want to have the string twice}
640 end;
641 eq_level(cur_val):=level_one; eq_type(cur_val):=c; equiv(cur_val):=o;
642 end;
643 tini
644 @y
645 flush_string; text(cur_val):=s; {we don't want to have the string twice}
646 prim_val:=prim_lookup(s);
647 end;
648 eq_level(cur_val):=level_one; eq_type(cur_val):=c; equiv(cur_val):=o;
649 prim_eq_level(prim_val):=level_one;
650 prim_eq_type(prim_val):=c;
651 prim_equiv(prim_val):=o;
652 end;
653 tini
654 @z
655
656 @x \[if]pdfprimitive
657 ignore_spaces: print_esc("ignorespaces");
658 @y
659 ignore_spaces: if chr_code=0 then print_esc("ignorespaces") else print_esc("pdfprimitive");
660 @z
661
662 @x \[if]pdfprimitive
663 no_expand: print_esc("noexpand");
664 @y
665 no_expand: if chr_code=0 then print_esc("noexpand")
666 else print_esc("pdfprimitive");
667 @z
668
669 @x \ifincsname
670 var t:halfword; {token that is being ``expanded after''}
671 @!p,@!q,@!r:pointer; {for list manipulation}
672 @y
673 var t:halfword; {token that is being ``expanded after''}
674 @!b:boolean; {keep track of nested csnames}
675 @!p,@!q,@!r:pointer; {for list manipulation}
676 @z
677
678 @x
679 @ @<Expand a nonmacro@>=
680 @y
681 @ @<Glob...@>=
682 @!is_in_csname: boolean;
683
684 @ @<Set init...@>=
685 is_in_csname := false;
686
687 @ @<Expand a nonmacro@>=
688 @z
689
690 @x
691 no_expand:@<Suppress expansion of the next token@>;
692 @y
693 no_expand: if cur_chr=0 then @<Suppress expansion of the next token@>
694 else @<Implement \.{\\pdfprimitive}@>;
695 @z
696
697 @x
698 @<Suppress expansion...@>=
699 begin save_scanner_status:=scanner_status; scanner_status:=normal;
700 get_token; scanner_status:=save_scanner_status; t:=cur_tok;
701 back_input; {now |start| and |loc| point to the backed-up token |t|}
702 if (t>=cs_token_flag)and(t<>end_write_token) then
703 begin p:=get_avail; info(p):=cs_token_flag+frozen_dont_expand;
704 link(p):=loc; start:=p; loc:=p;
705 end;
706 end
707 @y
708 @<Suppress expansion...@>=
709 begin save_scanner_status:=scanner_status; scanner_status:=normal;
710 get_token; scanner_status:=save_scanner_status; t:=cur_tok;
711 back_input; {now |start| and |loc| point to the backed-up token |t|}
712 if (t>=cs_token_flag)and(t<>end_write_token) then
713 begin p:=get_avail; info(p):=cs_token_flag+frozen_dont_expand;
714 link(p):=loc; start:=p; loc:=p;
715 end;
716 end
717
718 @ The \.{\\pdfprimitive} handling. If the primitive meaning of the next
719 token is an expandable command, it suffices to replace the current
720 token with the primitive one and restart |expand|/
721
722 Otherwise, the token we just read has to be pushed back, as well
723 as a token matching the internal form of \.{\\pdfprimitive}, that is
724 sneaked in as an alternate form of |ignore_spaces|.
725 @!@:pdfprimitive_}{\.{\\pdfprimitive} primitive (internalized)@>
726
727 Simply pushing back a token that matches the correct internal command
728 does not work, because approach would not survive roundtripping to a
729 temporary file.
730
731 @<Implement \.{\\pdfprimitive}@>=
732 begin save_scanner_status := scanner_status; scanner_status:=normal;
733 get_token; scanner_status:=save_scanner_status;
734 if cur_cs < hash_base then
735 cur_cs := prim_lookup(cur_cs-single_base)
736 else
737 cur_cs := prim_lookup(text(cur_cs));
738 if cur_cs<>undefined_primitive then begin
739 t := prim_eq_type(cur_cs);
740 if t>max_command then begin
741 cur_cmd := t;
742 cur_chr := prim_equiv(cur_cs);
743 cur_tok := (cur_cmd*max_char_val)+cur_chr;
744 cur_cs := 0;
745 goto reswitch;
746 end
747 else begin
748 back_input; { now |loc| and |start| point to a one-item list }
749 p:=get_avail; info(p):=cs_token_flag+frozen_primitive;
750 link(p):=loc; loc:=p; start:=p;
751 end;
752 end;
753 end
754
755 @ This block deals with unexpandable \.{\\primitive} appearing at a spot where
756 an integer or an internal values should have been found. It fetches the
757 next token then resets |cur_cmd|, |cur_cs|, and |cur_tok|, based on the
758 primitive value of that token. No expansion takes place, because the
759 next token may be all sorts of things. This could trigger further
760 expansion creating new errors.
761
762 @<Reset |cur_tok| for unexpandable primitives, goto restart @>=
763 begin
764 get_token;
765 if cur_cs < hash_base then
766 cur_cs := prim_lookup(cur_cs-single_base)
767 else
768 cur_cs := prim_lookup(text(cur_cs));
769 if cur_cs<>undefined_primitive then begin
770 cur_cmd := prim_eq_type(cur_cs);
771 cur_chr := prim_equiv(cur_cs);
772 cur_cs := prim_eqtb_base+cur_cs;
773 cur_tok := cs_token_flag+cur_cs;
774 end
775 else begin
776 cur_cmd := relax;
777 cur_chr := 0;
778 cur_tok := cs_token_flag+frozen_relax;
779 cur_cs := frozen_relax;
780 end;
781 goto restart;
782 end
783 @z
784
785 @x
786 begin r:=get_avail; p:=r; {head of the list of characters}
787 repeat get_x_token;
788 @y
789 begin r:=get_avail; p:=r; {head of the list of characters}
790 b := is_in_csname; is_in_csname := true;
791 repeat get_x_token;
792 @z
793
794 @x
795 @<Look up the characters of list |r| in the hash table, and set |cur_cs|@>;
796 @y
797 is_in_csname := b;
798 @<Look up the characters of list |r| in the hash table, and set |cur_cs|@>;
799 @z
800
801 @x scan_keyword
802 @!k:pool_pointer; {index into |str_pool|}
803 begin p:=backup_head; link(p):=null; k:=str_start[s];
804 @y
805 @!k:pool_pointer; {index into |str_pool|}
806 @!save_cur_cs:pointer; {to save |cur_cs|}
807 begin p:=backup_head; link(p):=null; k:=str_start[s];
808 save_cur_cs:=cur_cs;
809 @z
810
811 @x scan_keyword
812 scan_keyword:=false; return;
813 @y
814 cur_cs:=save_cur_cs;
815 scan_keyword:=false; return;
816 @z
817
818 @x \[if]pdfprimitive : scan_something_internal
819 procedure scan_something_internal(@!level:small_number;@!negative:boolean);
820 {fetch an internal parameter}
821 label exit;
822 @y
823 procedure scan_something_internal(@!level:small_number;@!negative:boolean);
824 {fetch an internal parameter}
825 label exit, restart;
826 @z
827
828 @x \[if]pdfprimitive : scan_something_internal
829 begin m:=cur_chr;
830 @y
831 begin restart: m:=cur_chr;
832 @z
833
834 @x \[if]pdfprimitive : scan_something_internal
835 last_item: @<Fetch an item in the current node, if appropriate@>;
836 @y
837 last_item: @<Fetch an item in the current node, if appropriate@>;
838 ignore_spaces: {trap unexpandable primitives}
839 if cur_chr=1 then @<Reset |cur_tok| for unexpandable primitives, goto restart@>;
840 @z
841
842 @x
843 @d ptex_minor_version_code=eptex_version_code+1 {code for \.{\\ptexminorversion}}
844 @y
845 @d ptex_minor_version_code=eptex_version_code+1 {code for \.{\\ptexminorversion}}
846 @d pdf_last_x_pos_code=ptex_minor_version_code+1 {code for \.{\\pdflastxpos}}
847 @d pdf_last_y_pos_code=pdf_last_x_pos_code+1 {code for \.{\\pdflastypos}}
848 @d pdf_shell_escape_code=pdf_last_y_pos_code+1 {code for \.{\\pdflastypos}}
849 @d elapsed_time_code=pdf_shell_escape_code+1 {code for \.{\\pdfelapsedtime}}
850 @d random_seed_code=elapsed_time_code+1 {code for \.{\\pdfrandomseed}}
851 @z
852
853 @x
854 @d eTeX_int=ptex_minor_version_code+1 {first of \eTeX\ codes for integers}
855 @y
856 @d eTeX_int=random_seed_code+1 {first of \eTeX\ codes for integers}
857 @z
858
859 @x \[if]pdfprimitive: scan_int
860 @p procedure scan_int; {sets |cur_val| to an integer}
861 label done;
862 @y
863 @p procedure scan_int; {sets |cur_val| to an integer}
864 label done, restart;
865 @z
866
867 @x \[if]pdfprimitive: scan_int
868 if cur_tok=alpha_token then @<Scan an alphabetic character code into |cur_val|@>
869 @y
870 restart:
871 if cur_tok=alpha_token then @<Scan an alphabetic character code into |cur_val|@>
872 else if cur_tok=cs_token_flag+frozen_primitive then
873 @<Reset |cur_tok| for unexpandable primitives, goto restart@>
874 @z
875
876 @x \Ucharcat: str_toks_cat
877 function str_toks(@!b:pool_pointer):pointer;
878 @y
879 function str_toks_cat(@!b:pool_pointer;@!cat:small_number):pointer;
880 @z
881
882 @x
883 if (cc=not_cjk) then cc:=other_kchar;
884 @y
885 if (cat>=kanji)and(cat<=modifier) then cc:=cat else if (cc=not_cjk) then cc:=other_kchar;
886 @z
887
888 @x \Ucharcat: str_toks_cat
889 if t=" " then t:=space_token
890 else t:=other_token+t;
891 @y
892 if (t=" ")and(cat=0) then t:=space_token
893 else if (cat=0)or((cat>=kanji)and(cat<=modifier)) then t:=other_token+t
894 else if cat=active_char then t:= cs_token_flag + active_base + t
895 else t:=left_brace_token*cat+t;
896 @z
897
898 @x \Ucharcat: str_toks_cat
899 pool_ptr:=b; str_toks:=p;
900 end;
901 @y
902 pool_ptr:=b; str_toks_cat:=p;
903 end;
904
905 function str_toks(@!b:pool_pointer):pointer;
906 begin str_toks:=str_toks_cat(b,0); end;
907 @z
908
909 @x
910 @d etex_convert_codes=etex_convert_base+1 {end of \eTeX's command codes}
911 @d job_name_code=etex_convert_codes {command code for \.{\\jobname}}
912 @y
913 @d etex_convert_codes=etex_convert_base+1 {end of \eTeX's command codes}
914 @d expanded_code = etex_convert_codes {command code for \.{\\expanded}}
915 @d pdf_first_expand_code = expanded_code + 1 {base for \pdfTeX-like command codes}
916 @d pdf_strcmp_code = pdf_first_expand_code+0 {command code for \.{\\pdfstrcmp}}
917 @d pdf_creation_date_code = pdf_first_expand_code+1 {command code for \.{\\pdfcreationdate}}
918 @d pdf_file_mod_date_code = pdf_first_expand_code+2 {command code for \.{\\pdffilemoddate}}
919 @d pdf_file_size_code = pdf_first_expand_code+3 {command code for \.{\\pdffilesize}}
920 @d pdf_mdfive_sum_code = pdf_first_expand_code+4 {command code for \.{\\pdfmdfivesum}}
921 @d pdf_file_dump_code = pdf_first_expand_code+5 {command code for \.{\\pdffiledump}}
922 @d uniform_deviate_code = pdf_first_expand_code+6 {command code for \.{\\pdfuniformdeviate}}
923 @d normal_deviate_code = pdf_first_expand_code+7 {command code for \.{\\pdfnormaldeviate}}
924 @d pdf_convert_codes = pdf_first_expand_code+8 {end of \pdfTeX-like command codes}
925 @d Uchar_convert_code = pdf_convert_codes {command code for \.{\\Uchar}}
926 @d Ucharcat_convert_code = pdf_convert_codes+1 {command code for \.{\\Ucharcat}}
927 @d eptex_convert_codes = pdf_convert_codes+2 {end of \epTeX's command codes}
928 @d job_name_code=eptex_convert_codes {command code for \.{\\jobname}}
929 @z
930
931 @x
932 primitive("jobname",convert,job_name_code);@/
933 @y
934 @#
935 primitive("expanded",convert,expanded_code);@/
936 @!@:expanded_}{\.{\\expanded} primitive@>
937 @#
938 primitive("jobname",convert,job_name_code);@/
939 @z
940
941 @x
942 eTeX_revision_code: print_esc("eTeXrevision");
943 @y
944 eTeX_revision_code: print_esc("eTeXrevision");
945 expanded_code: print_esc("expanded");
946 pdf_strcmp_code: print_esc("pdfstrcmp");
947 pdf_creation_date_code: print_esc("pdfcreationdate");
948 pdf_file_mod_date_code: print_esc("pdffilemoddate");
949 pdf_file_size_code: print_esc("pdffilesize");
950 pdf_mdfive_sum_code: print_esc("pdfmdfivesum");
951 pdf_file_dump_code: print_esc("pdffiledump");
952 uniform_deviate_code: print_esc("pdfuniformdeviate");
953 normal_deviate_code: print_esc("pdfnormaldeviate");
954 Uchar_convert_code: print_esc("Uchar");
955 Ucharcat_convert_code: print_esc("Ucharcat");
956 @z
957
958 @x
959 @p procedure conv_toks;
960 @y
961
962 The extra temp string |u| is needed because |pdf_scan_ext_toks| incorporates
963 any pending string in its output. In order to save such a pending string,
964 we have to create a temporary string that is destroyed immediately after.
965
966 @d save_cur_string==if str_start[str_ptr]<pool_ptr then u:=make_string else u:=0
967 @d restore_cur_string==if u<>0 then decr(str_ptr)
968
969 @ Not all catcode values are allowed by \.{\\Ucharcat}:
970 @d illegal_Ucharcat_ascii_catcode(#)==(#<left_brace)or(#>active_char)or(#=out_param)or(#=ignore)
971 @d illegal_Ucharcat_wchar_catcode(#)==(#<kanji)or(#>modifier)
972
973 @p procedure conv_toks;
974 @z
975
976 @x
977 @!save_scanner_status:small_number; {|scanner_status| upon entry}
978 @y
979 @!save_scanner_status:small_number; {|scanner_status| upon entry}
980 @!save_def_ref: pointer; {|def_ref| upon entry, important if inside `\.{\\message}'}
981 @!save_warning_index: pointer;
982 @!booltemp: boolean; {temp boolean}
983 @!u: str_number; {saved current string string}
984 @!s: str_number; {first temp string}
985 @!i: integer;
986 @!j: integer;
987 @!cat:small_number; {desired catcode, or 0 for automatic |spacer|/|other_char| selection}
988 @z
989
990 @x
991 begin c:=cur_chr; @<Scan the argument for command |c|@>;
992 @y
993 begin cat:=0; c:=cur_chr;
994 u:=0; { will become non-nil if a string is already being built}
995 @<Scan the argument for command |c|@>;
996 @z
997
998 @x
999 selector:=old_setting; link(garbage):=str_toks(b); ins_list(link(temp_head));
1000 @y
1001 selector:=old_setting; link(garbage):=str_toks_cat(b,cat); ins_list(link(temp_head));
1002 @z
1003
1004 @x
1005 eTeX_revision_code: do_nothing;
1006 @y
1007 eTeX_revision_code: do_nothing;
1008 expanded_code:
1009 begin
1010 save_scanner_status := scanner_status;
1011 save_warning_index := warning_index;
1012 save_def_ref := def_ref;
1013 save_cur_string;
1014 scan_pdf_ext_toks;
1015 warning_index := save_warning_index;
1016 scanner_status := save_scanner_status;
1017 ins_list(link(def_ref));
1018 free_avail(def_ref);
1019 def_ref := save_def_ref;
1020 restore_cur_string;
1021 return;
1022 end;
1023 pdf_strcmp_code:
1024 begin
1025 save_scanner_status := scanner_status;
1026 save_warning_index := warning_index;
1027 save_def_ref := def_ref;
1028 save_cur_string;
1029 compare_strings;
1030 def_ref := save_def_ref;
1031 warning_index := save_warning_index;
1032 scanner_status := save_scanner_status;
1033 restore_cur_string;
1034 end;
1035 pdf_creation_date_code:
1036 begin
1037 b := pool_ptr;
1038 getcreationdate;
1039 link(garbage) := str_toks(b);
1040 ins_list(link(temp_head));
1041 return;
1042 end;
1043 pdf_file_mod_date_code:
1044 begin
1045 save_scanner_status := scanner_status;
1046 save_warning_index := warning_index;
1047 save_def_ref := def_ref;
1048 save_cur_string;
1049 scan_pdf_ext_toks;
1050 s := tokens_to_string(def_ref);
1051 delete_token_ref(def_ref);
1052 def_ref := save_def_ref;
1053 warning_index := save_warning_index;
1054 scanner_status := save_scanner_status;
1055 b := pool_ptr;
1056 getfilemoddate(s);
1057 link(garbage) := str_toks(b);
1058 flush_str(s);
1059 ins_list(link(temp_head));
1060 restore_cur_string;
1061 return;
1062 end;
1063 pdf_file_size_code:
1064 begin
1065 save_scanner_status := scanner_status;
1066 save_warning_index := warning_index;
1067 save_def_ref := def_ref;
1068 save_cur_string;
1069 scan_pdf_ext_toks;
1070 s := tokens_to_string(def_ref);
1071 delete_token_ref(def_ref);
1072 def_ref := save_def_ref;
1073 warning_index := save_warning_index;
1074 scanner_status := save_scanner_status;
1075 b := pool_ptr;
1076 getfilesize(s);
1077 link(garbage) := str_toks(b);
1078 flush_str(s);
1079 ins_list(link(temp_head));
1080 restore_cur_string;
1081 return;
1082 end;
1083 pdf_mdfive_sum_code:
1084 begin
1085 save_scanner_status := scanner_status;
1086 save_warning_index := warning_index;
1087 save_def_ref := def_ref;
1088 save_cur_string;
1089 booltemp := scan_keyword("file");
1090 scan_pdf_ext_toks;
1091 if booltemp then s := tokens_to_string(def_ref)
1092 else begin
1093 isprint_utf8:=true; s := tokens_to_string(def_ref); isprint_utf8:=false;
1094 end;
1095 delete_token_ref(def_ref);
1096 def_ref := save_def_ref;
1097 warning_index := save_warning_index;
1098 scanner_status := save_scanner_status;
1099 b := pool_ptr;
1100 getmd5sum(s, booltemp);
1101 link(garbage) := str_toks(b);
1102 flush_str(s);
1103 ins_list(link(temp_head));
1104 restore_cur_string;
1105 return;
1106 end;
1107 pdf_file_dump_code:
1108 begin
1109 save_scanner_status := scanner_status;
1110 save_warning_index := warning_index;
1111 save_def_ref := def_ref;
1112 save_cur_string;
1113 {scan offset}
1114 cur_val := 0;
1115 if (scan_keyword("offset")) then begin
1116 scan_int;
1117 if (cur_val < 0) then begin
1118 print_err("Bad file offset");
1119 @.Bad file offset@>
1120 help2("A file offset must be between 0 and 2^{31}-1,")@/
1121 ("I changed this one to zero.");
1122 int_error(cur_val);
1123 cur_val := 0;
1124 end;
1125 end;
1126 i := cur_val;
1127 {scan length}
1128 cur_val := 0;
1129 if (scan_keyword("length")) then begin
1130 scan_int;
1131 if (cur_val < 0) then begin
1132 print_err("Bad dump length");
1133 @.Bad dump length@>
1134 help2("A dump length must be between 0 and 2^{31}-1,")@/
1135 ("I changed this one to zero.");
1136 int_error(cur_val);
1137 cur_val := 0;
1138 end;
1139 end;
1140 j := cur_val;
1141 {scan file name}
1142 scan_pdf_ext_toks;
1143 s := tokens_to_string(def_ref);
1144 delete_token_ref(def_ref);
1145 def_ref := save_def_ref;
1146 warning_index := save_warning_index;
1147 scanner_status := save_scanner_status;
1148 b := pool_ptr;
1149 getfiledump(s, i, j);
1150 link(garbage) := str_toks(b);
1151 flush_str(s);
1152 ins_list(link(temp_head));
1153 restore_cur_string;
1154 return;
1155 end;
1156 uniform_deviate_code: scan_int;
1157 normal_deviate_code: do_nothing;
1158 Uchar_convert_code: begin scan_char_num;
1159 if not check_echar_range(cur_val) then
1160 if kcat_code(kcatcodekey(cur_val))=not_cjk then cat:=other_kchar;
1161 end;
1162 Ucharcat_convert_code:
1163 begin
1164 scan_char_num;
1165 i:=cur_val;
1166 scan_int;
1167 if i<=@"7F then { no |wchar_token| }
1168 begin if illegal_Ucharcat_ascii_catcode(cur_val) then
1169 begin print_err("Invalid code ("); print_int(cur_val);
1170 @.Invalid code@>
1171 print("), should be in the ranges 1..4, 6..8, 10..13");
1172 help1("I'm going to use 12 instead of that illegal code value.");@/
1173 error; cat:=12;
1174 end else cat:=cur_val;
1175 end else if check_echar_range(i) then
1176 begin if (illegal_Ucharcat_ascii_catcode(cur_val))
1177 and (illegal_Ucharcat_wchar_catcode(cur_val)) then
1178 begin print_err("Invalid code ("); print_int(cur_val);
1179 @.Invalid code@>
1180 print("), should be in the ranges 1..4, 6..8, 10..13, 16..20");
1181 help1("I'm going to use 12 instead of that illegal code value.");@/
1182 error; cat:=12;
1183 end else cat:=cur_val;
1184 end else { |wchar_token| only }
1185 begin if illegal_Ucharcat_wchar_catcode(cur_val) then
1186 begin print_err("Invalid code ("); print_int(cur_val);
1187 @.Invalid code@>
1188 print("), should be in the ranges 16..20");
1189 help1("I'm going to use 18 instead of that illegal code value.");@/
1190 error; cat:=other_kchar;
1191 end else cat:=cur_val;
1192 end;
1193 cur_val:=i;
1194 end;
1195 @z
1196
1197 @x
1198 eTeX_revision_code: print(eTeX_revision);
1199 @y
1200 eTeX_revision_code: print(eTeX_revision);
1201 pdf_strcmp_code: print_int(cur_val);
1202 uniform_deviate_code: print_int(unif_rand(cur_val));
1203 normal_deviate_code: print_int(norm_rand);
1204 Uchar_convert_code:
1205 if check_echar_range(cur_val)>1 then print_char(cur_val) else print_kanji(cur_val);
1206 Ucharcat_convert_code:
1207 if (cat<kanji)and(check_echar_range(cur_val)>1) then print_char(cur_val) else print_kanji(cur_val);
1208 @z
1209
1210 @x e-pTeX: if primitives - leave room for \ifincsname
1211 @d if_tdir_code=if_case_code+4 { `\.{\\iftdir}' }
1212 @y
1213 @d if_in_csname_code=20 { `\.{\\ifincsname}'; |if_font_char_code| + 1 }
1214 @d if_pdfprimitive_code=21 { `\.{\\ifpdfprimitive}' }
1215 @#
1216 @d if_tdir_code=if_pdfprimitive_code+1 { `\.{\\iftdir}' }
1217 @z
1218
1219 @x \[if]pdfprimitive
1220 if_mbox_code:print_esc("ifmbox");
1221 @y
1222 if_mbox_code:print_esc("ifmbox");
1223 if_pdfprimitive_code:print_esc("ifpdfprimitive");
1224 @z
1225
1226 @x \ifincsname
1227 var b:boolean; {is the condition true?}
1228 @!r:"<"..">"; {relation to be evaluated}
1229 @y
1230 var b:boolean; {is the condition true?}
1231 @!e:boolean; {keep track of nested csnames}
1232 @!r:"<"..">"; {relation to be evaluated}
1233 @z
1234
1235 @x \[if]pdfprimitive
1236 if_void_code, if_hbox_code, if_vbox_code: @<Test box register status@>;
1237 @y
1238 if_void_code, if_hbox_code, if_vbox_code: @<Test box register status@>;
1239 if_pdfprimitive_code: begin
1240 save_scanner_status:=scanner_status;
1241 scanner_status:=normal;
1242 get_next;
1243 scanner_status:=save_scanner_status;
1244 if cur_cs < hash_base then
1245 m := prim_lookup(cur_cs-single_base)
1246 else
1247 m := prim_lookup(text(cur_cs));
1248 b :=((cur_cmd<>undefined_cs) and
1249 (m<>undefined_primitive) and
1250 (cur_cmd=prim_eq_type(m)) and
1251 (cur_chr=prim_equiv(m)));
1252 end;
1253 @z
1254
1255 @x
1256 @ @<Initialize variables as |ship_out| begins@>=
1257 @y
1258 @ @<Initialize variables as |ship_out| begins@>=
1259 @<Calculate DVI page dimensions and margins@>;
1260 @z
1261
1262 % [33] \vadjust pre
1263 @x
1264 if adjust_tail<>null then link(adjust_tail):=null;
1265 @y
1266 if adjust_tail<>null then link(adjust_tail):=null;
1267 if pre_adjust_tail<>null then link(pre_adjust_tail):=null;
1268 @z
1269
1270 % [33] \vadjust pre
1271 @x
1272 ins_node,mark_node,adjust_node: if adjust_tail<>null then
1273 @y
1274 ins_node,mark_node,adjust_node: if (adjust_tail<>null) or (pre_adjust_tail<> null) then
1275 @z
1276
1277 % [33] \vadjust pre
1278 @x
1279 to make a deletion.
1280 @^inner loop@>
1281 @y
1282 to make a deletion.
1283 @^inner loop@>
1284
1285 @<Glob...@>=
1286 @!pre_adjust_tail: pointer;
1287
1288 @ @<Set init...@>=
1289 pre_adjust_tail := null;
1290
1291 @ Materials in \.{\\vadjust} used with \.{pre} keyword will be appended to
1292 |pre_adjust_tail| instead of |adjust_tail|.
1293
1294 @d update_adjust_list(#) == begin
1295 if # = null then
1296 confusion("pre vadjust");
1297 link(#) := adjust_ptr(p);
1298 while link(#) <> null do
1299 # := link(#);
1300 end
1301 @z
1302
1303 % [33] \vadjust pre
1304 @x
1305 @<Transfer node |p| to the adjustment list@>=
1306 begin while link(q)<>p do q:=link(q);
1307 if type(p)=adjust_node then
1308 begin link(adjust_tail):=adjust_ptr(p);
1309 while link(adjust_tail)<>null do adjust_tail:=link(adjust_tail);
1310 p:=link(p); free_node(link(q),small_node_size);
1311 end
1312 else begin link(adjust_tail):=p; adjust_tail:=p; p:=link(p);
1313 end;
1314 link(q):=p; p:=q;
1315 @y
1316 @<Transfer node |p| to the adjustment list@>=
1317 begin while link(q)<>p do q:=link(q);
1318 if type(p) = adjust_node then begin
1319 if adjust_pre(p) <> 0 then
1320 update_adjust_list(pre_adjust_tail)
1321 else
1322 update_adjust_list(adjust_tail);
1323 p := link(p); free_node(link(q), small_node_size);
1324 end
1325 else begin link(adjust_tail):=p; adjust_tail:=p; p:=link(p);
1326 end;
1327 link(q):=p; p:=q;
1328 @z
1329
1330 % [37] \vadjust pre
1331 @x
1332 @d align_stack_node_size=5 {number of |mem| words to save alignment states}
1333 @y
1334 @d align_stack_node_size=6 {number of |mem| words to save alignment states}
1335 @z
1336
1337 % [37] \vadjust pre
1338 @x
1339 @!cur_head,@!cur_tail:pointer; {adjustment list pointers}
1340 @y
1341 @!cur_head,@!cur_tail:pointer; {adjustment list pointers}
1342 @!cur_pre_head,@!cur_pre_tail:pointer; {pre-adjustment list pointers}
1343 @z
1344
1345 % [37] \vadjust pre
1346 @x
1347 cur_head:=null; cur_tail:=null;
1348 @y
1349 cur_head:=null; cur_tail:=null;
1350 cur_pre_head:=null; cur_pre_tail:=null;
1351 @z
1352
1353 % [37] procedure |push_alignment|: \vadjust pre
1354 @x
1355 info(p+4):=cur_head; link(p+4):=cur_tail;
1356 align_ptr:=p;
1357 cur_head:=get_avail;
1358 @y
1359 info(p+4):=cur_head; link(p+4):=cur_tail;
1360 info(p+5):=cur_pre_head; link(p+5):=cur_pre_tail;
1361 align_ptr:=p;
1362 cur_head:=get_avail;
1363 cur_pre_head:=get_avail;
1364 @z
1365
1366 % [37] procedure |pop_alignment|: \vadjust pre
1367 @x
1368 begin free_avail(cur_head);
1369 p:=align_ptr;
1370 cur_tail:=link(p+4); cur_head:=info(p+4);
1371 @y
1372 begin free_avail(cur_head);
1373 free_avail(cur_pre_head);
1374 p:=align_ptr;
1375 cur_tail:=link(p+4); cur_head:=info(p+4);
1376 cur_pre_tail:=link(p+5); cur_pre_head:=info(p+5);
1377 @z
1378
1379 % [37] \vadjust pre
1380 @x
1381 cur_align:=link(preamble); cur_tail:=cur_head; init_span(cur_align);
1382 @y
1383 cur_align:=link(preamble); cur_tail:=cur_head; cur_pre_tail:=cur_pre_head;
1384 init_span(cur_align);
1385 @z
1386
1387 % [37] \vadjust pre + pTeX
1388 @x
1389 begin adjust_tail:=cur_tail; adjust_hlist(head,false);
1390 @y
1391 begin adjust_tail:=cur_tail; pre_adjust_tail:=cur_pre_tail;
1392 adjust_hlist(head,false);
1393 @z
1394
1395 % [37] \vadjust pre
1396 @x
1397 cur_tail:=adjust_tail; adjust_tail:=null;
1398 @y
1399 cur_tail:=adjust_tail; adjust_tail:=null;
1400 cur_pre_tail:=pre_adjust_tail; pre_adjust_tail:=null;
1401 @z
1402
1403 % [37] \vadjust pre
1404 @x
1405 pop_nest; append_to_vlist(p);
1406 if cur_head<>cur_tail then
1407 begin link(tail):=link(cur_head); tail:=cur_tail;
1408 end;
1409 @y
1410 pop_nest;
1411 if cur_pre_head <> cur_pre_tail then
1412 append_list(cur_pre_head)(cur_pre_tail);
1413 append_to_vlist(p);
1414 if cur_head <> cur_tail then
1415 append_list(cur_head)(cur_tail);
1416 @z
1417
1418 % [39] \vadjust pre
1419 @x
1420 @ @<Append the new box to the current vertical list...@>=
1421 append_to_vlist(just_box);
1422 if adjust_head<>adjust_tail then
1423 begin link(tail):=link(adjust_head); tail:=adjust_tail;
1424 end;
1425 adjust_tail:=null
1426 @y
1427 @ @<Append the new box to the current vertical list...@>=
1428 if pre_adjust_head <> pre_adjust_tail then
1429 append_list(pre_adjust_head)(pre_adjust_tail);
1430 pre_adjust_tail := null;
1431 append_to_vlist(just_box);
1432 if adjust_head <> adjust_tail then
1433 append_list(adjust_head)(adjust_tail);
1434 adjust_tail := null
1435 @z
1436
1437 % [39] \vadjust pre
1438 @x
1439 adjust_tail:=adjust_head; just_box:=hpack(q,cur_width,exactly);
1440 @y
1441 adjust_tail:=adjust_head;
1442 pre_adjust_tail := pre_adjust_head;
1443 just_box:=hpack(q,cur_width,exactly);
1444 @z
1445
1446 @x \[if]pdfprimitive: main_loop
1447 any_mode(ignore_spaces): begin @<Get the next non-blank non-call...@>;
1448 goto reswitch;
1449 end;
1450 @y
1451 any_mode(ignore_spaces): begin
1452 if cur_chr = 0 then begin
1453 @<Get the next non-blank non-call...@>;
1454 goto reswitch;
1455 end
1456 else begin
1457 t:=scanner_status;
1458 scanner_status:=normal;
1459 get_next;
1460 scanner_status:=t;
1461 if cur_cs < hash_base then
1462 cur_cs := prim_lookup(cur_cs-single_base)
1463 else
1464 cur_cs := prim_lookup(text(cur_cs));
1465 if cur_cs<>undefined_primitive then begin
1466 cur_cmd := prim_eq_type(cur_cs);
1467 cur_chr := prim_equiv(cur_cs);
1468 cur_tok := cs_token_flag+prim_eqtb_base+cur_cs;
1469 goto reswitch;
1470 end;
1471 end;
1472 end;
1473 @z
1474
1475 % [47] \vadjust pre
1476 @x
1477 if abs(mode)=vmode then
1478 begin append_to_vlist(cur_box);
1479 if adjust_tail<>null then
1480 begin if adjust_head<>adjust_tail then
1481 begin link(tail):=link(adjust_head); tail:=adjust_tail;
1482 end;
1483 adjust_tail:=null;
1484 end;
1485 if mode>0 then build_page;
1486 end
1487 @y
1488 if abs(mode)=vmode then
1489 begin
1490 if pre_adjust_tail <> null then begin
1491 if pre_adjust_head <> pre_adjust_tail then
1492 append_list(pre_adjust_head)(pre_adjust_tail);
1493 pre_adjust_tail := null;
1494 end;
1495 append_to_vlist(cur_box);
1496 if adjust_tail <> null then begin
1497 if adjust_head <> adjust_tail then
1498 append_list(adjust_head)(adjust_tail);
1499 adjust_tail := null;
1500 end;
1501 if mode>0 then build_page;
1502 end
1503 @z
1504
1505 % [47] \vadjust pre + pTeX
1506 @x
1507 adjusted_hbox_group: begin adjust_hlist(head,false);
1508 adjust_tail:=adjust_head; package(0);
1509 @y
1510 adjusted_hbox_group: begin adjust_hlist(head,false);
1511 adjust_tail:=adjust_head;
1512 pre_adjust_tail:=pre_adjust_head; package(0);
1513 @z
1514
1515 % [47] \vadjust pre
1516 @x
1517 saved(0):=cur_val; incr(save_ptr);
1518 @y
1519 saved(0) := cur_val;
1520 if (cur_cmd = vadjust) and scan_keyword("pre") then
1521 saved(1) := 1
1522 else
1523 saved(1) := 0;
1524 save_ptr := save_ptr + 2;
1525 @z
1526
1527 % [47] \vadjust pre
1528 @x
1529 d:=split_max_depth; f:=floating_penalty; unsave; decr(save_ptr);
1530 @y
1531 d:=split_max_depth; f:=floating_penalty; unsave; save_ptr := save_ptr - 2;
1532 @z
1533
1534 % [47] \vadjust pre + pTeX
1535 @x
1536 r:=get_node(small_node_size); type(r):=adjust_node;@/
1537 subtype(r):=0; {the |subtype| is not used}
1538 adjust_ptr(r):=list_ptr(p); delete_glue_ref(q);
1539 @y
1540 r:=get_node(small_node_size); type(r):=adjust_node;@/
1541 adjust_pre(r) := saved(1); {the |subtype| is used for |adjust_pre|}
1542 adjust_ptr(r):=list_ptr(p); delete_glue_ref(q);
1543 @z
1544
1545 % [48] \vadjust pre
1546 @x
1547 @!t:pointer; {tail of adjustment list}
1548 @y
1549 @!t:pointer; {tail of adjustment list}
1550 @!pre_t:pointer; {tail of pre-adjustment list}
1551 @z
1552
1553 % [48] \vadjust pre
1554 @x
1555 adjust_tail:=adjust_head; b:=hpack(p,natural); p:=list_ptr(b);
1556 t:=adjust_tail; adjust_tail:=null;@/
1557 @y
1558 adjust_tail:=adjust_head; pre_adjust_tail:=pre_adjust_head;
1559 b:=hpack(p,natural); p:=list_ptr(b);
1560 t:=adjust_tail; adjust_tail:=null;@/
1561 pre_t:=pre_adjust_tail; pre_adjust_tail:=null;@/
1562 @z
1563
1564 % [48] \vadjust pre
1565 @x
1566 if t<>adjust_head then {migrating material comes after equation number}
1567 begin link(tail):=link(adjust_head); tail:=t;
1568 end;
1569 @y
1570 if t<>adjust_head then {migrating material comes after equation number}
1571 begin link(tail):=link(adjust_head); tail:=t;
1572 end;
1573 if pre_t<>pre_adjust_head then
1574 begin link(tail):=link(pre_adjust_head); tail:=pre_t;
1575 end;
1576 @z
1577
1578 @x \[if]pdfprimitive: dump prim table
1579 @<Dump the hash table@>=
1580 @y
1581 @<Dump the hash table@>=
1582 for p:=0 to prim_size do dump_hh(prim[p]);
1583 @z
1584
1585 @x \[if]pdfprimitive: undump prim table
1586 @ @<Undump the hash table@>=
1587 @y
1588 @ @<Undump the hash table@>=
1589 for p:=0 to prim_size do undump_hh(prim[p]);
1590 @z
1591
1592 @x
1593 fix_date_and_time;@/
1594 @y
1595 fix_date_and_time;@/
1596 isprint_utf8:=false;
1597 random_seed:=(microseconds*1000)+(epochseconds mod 1000000);@/
1598 init_randoms(random_seed);@/
1599 @z
1600
1601 @x
1602 @d language_node=4 {|subtype| in whatsits that change the current language}
1603 @y
1604 @d latespecial_node=4 {|subtype| in whatsits that represent \.{\\special} things}
1605 @d language_node=5 {|subtype| in whatsits that change the current language}
1606 @z
1607
1608 @x
1609 @d immediate_code=4 {command modifier for \.{\\immediate}}
1610 @d set_language_code=5 {command modifier for \.{\\setlanguage}}
1611 @d epTeX_input_encoding_code=6 {command modifier for \.{\\epTeXinputencoding}}
1612 @y
1613 @d immediate_code=5 {command modifier for \.{\\immediate}}
1614 @d set_language_code=6 {command modifier for \.{\\setlanguage}}
1615 @d epTeX_input_encoding_code=7 {command modifier for \.{\\epTeXinputencoding}}
1616 @d pdf_save_pos_node=epTeX_input_encoding_code+1
1617 @d set_random_seed_code=pdf_save_pos_node+1
1618 @d reset_timer_code=set_random_seed_code+1
1619 @z
1620
1621 @x
1622 set_language_code:print_esc("setlanguage");
1623 @y
1624 set_language_code:print_esc("setlanguage");
1625 pdf_save_pos_node: print_esc("pdfsavepos");
1626 set_random_seed_code: print_esc("pdfsetrandomseed");
1627 reset_timer_code: print_esc("pdfresettimer");
1628 @z
1629
1630 @x
1631 set_language_code:@<Implement \.{\\setlanguage}@>;
1632 @y
1633 set_language_code:@<Implement \.{\\setlanguage}@>;
1634 pdf_save_pos_node: @<Implement \.{\\pdfsavepos}@>;
1635 set_random_seed_code: @<Implement \.{\\pdfsetrandomseed}@>;
1636 reset_timer_code: @<Implement \.{\\pdfresettimer}@>;
1637 @z
1638
1639 @x
1640 @<Implement \.{\\special}@>=
1641 begin new_whatsit(special_node,write_node_size); write_stream(tail):=null;
1642 p:=scan_toks(false,true); write_tokens(tail):=def_ref;
1643 inhibit_glue_flag:=false;
1644 end
1645 @y
1646 @<Implement \.{\\special}@>=
1647 begin if scan_keyword("shipout") then
1648 begin new_whatsit(latespecial_node,write_node_size); write_stream(tail):=null;
1649 p:=scan_toks(false,false); write_tokens(tail):=def_ref;
1650 end else
1651 begin new_whatsit(special_node,write_node_size); write_stream(tail):=null;
1652 p:=scan_toks(false,true); write_tokens(tail):=def_ref;
1653 end;
1654 inhibit_glue_flag:=false;
1655 end
1656 @z
1657
1658 @x
1659 special_node:begin print_esc("special");
1660 print_mark(write_tokens(p));
1661 end;
1662 @y
1663 special_node:begin print_esc("special");
1664 print_mark(write_tokens(p));
1665 end;
1666 latespecial_node:begin print_esc("special"); print(" shipout");
1667 print_mark(write_tokens(p));
1668 end;
1669 @z
1670
1671 @x \pdfsavepos
1672 print_int(what_lhm(p)); print_char(",");
1673 print_int(what_rhm(p)); print_char(")");
1674 end;
1675 @y
1676 print_int(what_lhm(p)); print_char(",");
1677 print_int(what_rhm(p)); print_char(")");
1678 end;
1679 pdf_save_pos_node: print_esc("pdfsavepos");
1680 set_random_seed_code: print_esc("pdfsetrandomseed");
1681 reset_timer_code: print_esc("pdfresettimer");
1682 @z
1683
1684 @x
1685 write_node,special_node: begin r:=get_node(write_node_size);
1686 @y
1687 write_node,special_node,latespecial_node: begin r:=get_node(write_node_size);
1688 @z
1689
1690 @x \pdfsavepos
1691 close_node,language_node: begin r:=get_node(small_node_size);
1692 words:=small_node_size;
1693 end;
1694 @y
1695 close_node,language_node: begin r:=get_node(small_node_size);
1696 words:=small_node_size;
1697 end;
1698 pdf_save_pos_node:
1699 r := get_node(small_node_size);
1700 @z
1701
1702 @x
1703 write_node,special_node: begin delete_token_ref(write_tokens(p));
1704 @y
1705 write_node,special_node,latespecial_node: begin delete_token_ref(write_tokens(p));
1706 @z
1707
1708 @x \pdfsavepos
1709 close_node,language_node: free_node(p,small_node_size);
1710 @y
1711 close_node,language_node: free_node(p,small_node_size);
1712 pdf_save_pos_node: free_node(p, small_node_size);
1713 @z
1714
1715 @x
1716 procedure special_out(@!p:pointer);
1717 var old_setting:0..max_selector; {holds print |selector|}
1718 @!k:pool_pointer; {index into |str_pool|}
1719 begin synch_h; synch_v;@/
1720 old_setting:=selector; selector:=new_string;
1721 show_token_list(link(write_tokens(p)),null,pool_size-pool_ptr);
1722 @y
1723 procedure special_out(@!p:pointer);
1724 label done;
1725 var old_setting:0..max_selector; {holds print |selector|}
1726 @!h:halfword;
1727 @!k:pool_pointer; {index into |str_pool|}
1728 @!q,@!r:pointer; {temporary variables for list manipulation}
1729 @!old_mode:integer; {saved |mode|}
1730 @!s,@!t,@!cw, @!num, @!denom: scaled;
1731 @!bl: boolean;
1732 @!i: small_number;
1733 begin synch_h; synch_v;@/
1734 old_setting:=selector;
1735 if subtype(p)=latespecial_node then
1736 begin @<Expand macros in the token list
1737 and make |link(def_ref)| point to the result@>;
1738 h:=def_ref;
1739 end
1740 else h:=write_tokens(p);
1741 selector:=new_string;
1742 show_token_list(link(h),null,pool_size-pool_ptr);
1743 @z
1744
1745 @x
1746 pool_ptr:=str_start[str_ptr]; {erase the string}
1747 @y
1748 if read_papersize_special>0 then
1749 @<Determine whether this \.{\\special} is a papersize special@>;
1750 done: pool_ptr:=str_start[str_ptr]; {erase the string}
1751 if subtype(p)=latespecial_node then
1752 flush_list(def_ref);
1753 @z
1754
1755 @x
1756 special_node:special_out(p);
1757 language_node:do_nothing;
1758 @y
1759 special_node,latespecial_node:special_out(p);
1760 language_node:do_nothing;
1761 pdf_save_pos_node:
1762 @<Save current position in DVI mode@>;
1763 @z
1764
1765 @x
1766 primitive("eTeXrevision",convert,eTeX_revision_code);@/
1767 @!@:eTeX_revision_}{\.{\\eTeXrevision} primitive@>
1768 @y
1769 primitive("eTeXrevision",convert,eTeX_revision_code);@/
1770 @!@:eTeX_revision_}{\.{\\eTeXrevision} primitive@>
1771 primitive("pdfprimitive",no_expand,1);@/
1772 @!@:pdfprimitive_}{\.{\\pdfprimitive} primitive@>
1773 primitive("pdfstrcmp",convert,pdf_strcmp_code);@/
1774 @!@:pdf_strcmp_}{\.{\\pdfstrcmp} primitive@>
1775 primitive("pdfcreationdate",convert,pdf_creation_date_code);@/
1776 @!@:pdf_creation_date_}{\.{\\pdfcreationdate} primitive@>
1777 primitive("pdffilemoddate",convert,pdf_file_mod_date_code);@/
1778 @!@:pdf_file_mod_date_}{\.{\\pdffilemoddate} primitive@>
1779 primitive("pdffilesize",convert,pdf_file_size_code);@/
1780 @!@:pdf_file_size_}{\.{\\pdffilesize} primitive@>
1781 primitive("pdfmdfivesum",convert,pdf_mdfive_sum_code);@/
1782 @!@:pdf_mdfive_sum_}{\.{\\pdfmdfivesum} primitive@>
1783 primitive("pdffiledump",convert,pdf_file_dump_code);@/
1784 @!@:pdf_file_dump_}{\.{\\pdffiledump} primitive@>
1785 primitive("pdfsavepos",extension,pdf_save_pos_node);@/
1786 @!@:pdf_save_pos_}{\.{\\pdfsavepos} primitive@>
1787 primitive("pdfpagewidth",assign_dimen,dimen_base+pdf_page_width_code);@/
1788 @!@:pdf_page_width_}{\.{\\pdfpagewidth} primitive@>
1789 primitive("pdfpageheight",assign_dimen,dimen_base+pdf_page_height_code);@/
1790 @!@:pdf_page_height_}{\.{\\pdfpageheight} primitive@>
1791 primitive("pdflastxpos",last_item,pdf_last_x_pos_code);@/
1792 @!@:pdf_last_x_pos_}{\.{\\pdflastxpos} primitive@>
1793 primitive("pdflastypos",last_item,pdf_last_y_pos_code);@/
1794 @!@:pdf_last_y_pos_}{\.{\\pdflastypos} primitive@>
1795 primitive("pdfshellescape",last_item,pdf_shell_escape_code);
1796 @!@:pdf_shell_escape_}{\.{\\pdfshellescape} primitive@>
1797 primitive("ifpdfprimitive",if_test,if_pdfprimitive_code);
1798 @!@:if_pdfprimitive_}{\.{\\ifpdfprimitive} primitive@>
1799 primitive("pdfuniformdeviate",convert,uniform_deviate_code);@/
1800 @!@:uniform_deviate_}{\.{\\pdfuniformdeviate} primitive@>
1801 primitive("pdfnormaldeviate",convert,normal_deviate_code);@/
1802 @!@:normal_deviate_}{\.{\\pdfnormaldeviate} primitive@>
1803 primitive("pdfrandomseed",last_item,random_seed_code);
1804 @!@:random_seed_}{\.{\\pdfrandomseed} primitive@>
1805 primitive("pdfsetrandomseed",extension,set_random_seed_code);@/
1806 @!@:set_random_seed_code}{\.{\\pdfsetrandomseed} primitive@>
1807 primitive("pdfelapsedtime",last_item,elapsed_time_code);
1808 @!@:elapsed_time_}{\.{\\pdfelapsedtime} primitive@>
1809 primitive("pdfresettimer",extension,reset_timer_code);@/
1810 @!@:reset_timer_}{\.{\\pdfresettimer} primitive@>
1811 primitive("Uchar",convert,Uchar_convert_code);@/
1812 @!@:Uchar_}{\.{\\Uchar} primitive@>
1813 primitive("Ucharcat",convert,Ucharcat_convert_code);@/
1814 @!@:Ucharcat_}{\.{\\Ucharcat} primitive@>
1815 @z
1816
1817 @x
1818 eTeX_version_code: print_esc("eTeXversion");
1819 @y
1820 eTeX_version_code: print_esc("eTeXversion");
1821 pdf_last_x_pos_code: print_esc("pdflastxpos");
1822 pdf_last_y_pos_code: print_esc("pdflastypos");
1823 elapsed_time_code: print_esc("pdfelapsedtime");
1824 pdf_shell_escape_code: print_esc("pdfshellescape");
1825 random_seed_code: print_esc("pdfrandomseed");
1826 @z
1827
1828 @x
1829 eTeX_version_code: cur_val:=eTeX_version;
1830 @y
1831 eTeX_version_code: cur_val:=eTeX_version;
1832 pdf_last_x_pos_code: cur_val := pdf_last_x_pos;
1833 pdf_last_y_pos_code: cur_val := pdf_last_y_pos;
1834 pdf_shell_escape_code:
1835 begin
1836 if shellenabledp then begin
1837 if restrictedshell then cur_val :=2
1838 else cur_val := 1;
1839 end
1840 else cur_val := 0;
1841 end;
1842 elapsed_time_code: cur_val := get_microinterval;
1843 random_seed_code: cur_val := random_seed;
1844 @z
1845
1846 @x
1847 primitive("iffontchar",if_test,if_font_char_code);
1848 @!@:if_font_char_}{\.{\\iffontchar} primitive@>
1849 @y
1850 primitive("iffontchar",if_test,if_font_char_code);
1851 @!@:if_font_char_}{\.{\\iffontchar} primitive@>
1852 primitive("ifincsname",if_test,if_in_csname_code);
1853 @!@:if_in_csname_}{\.{\\ifincsname} primitive@>
1854 @z
1855
1856 @x
1857 if_font_char_code:print_esc("iffontchar");
1858 @y
1859 if_font_char_code:print_esc("iffontchar");
1860 if_in_csname_code:print_esc("ifincsname");
1861 @z
1862
1863 @x
1864 if_cs_code:begin n:=get_avail; p:=n; {head of the list of characters}
1865 repeat get_x_token;
1866 @y
1867 if_cs_code:begin n:=get_avail; p:=n; {head of the list of characters}
1868 e := is_in_csname; is_in_csname := true;
1869 repeat get_x_token;
1870 @z
1871
1872 @x
1873 b:=(eq_type(cur_cs)<>undefined_cs);
1874 @y
1875 b:=(eq_type(cur_cs)<>undefined_cs);
1876 is_in_csname := e;
1877 @z
1878
1879 @x
1880 if_font_char_code:begin scan_font_ident; n:=cur_val;
1881 @y
1882 if_in_csname_code: b := is_in_csname;
1883 if_font_char_code:begin scan_font_ident; n:=cur_val;
1884 @z
1885
1886 @x
1887 procedure print_kanji(@!s:KANJI_code); {prints a single character}
1888 @y
1889 procedure print_kanji(@!s:KANJI_code); {prints a single character}
1890 @z
1891
1892 @x
1893 else begin
1894 s:=toBUFF(s mod max_cjk_val);
1895 @y
1896 else begin
1897 if isprint_utf8 then s:=UCStoUTF8(toUCS(s mod max_cjk_val))
1898 else s:=toBUFF(s mod max_cjk_val);
1899 @z
1900
1901 @x
1902 @* \[54] System-dependent changes.
1903 @y
1904 @* \[54/pdf\TeX] System-dependent changes for {\tt\char"5Cpdfstrcmp}.
1905 @d call_func(#) == begin if # <> 0 then do_nothing end
1906 @d flushable(#) == (# = str_ptr - 1)
1907
1908 @<Glob...@>=
1909 @!isprint_utf8: boolean;
1910 @!epochseconds: integer;
1911 @!microseconds: integer;
1912
1913 @
1914 @d max_integer == @"7FFFFFFF {$2^{31}-1$}
1915
1916 @<Declare procedures that need to be declared forward for \pdfTeX@>=
1917 procedure pdf_error(t, p: str_number);
1918 begin
1919 normalize_selector;
1920 print_err("pdfTeX error");
1921 if t <> 0 then begin
1922 print(" (");
1923 print(t);
1924 print(")");
1925 end;
1926 print(": "); print(p);
1927 succumb;
1928 end;
1929
1930 function get_microinterval:integer;
1931 var s,@!m:integer; {seconds and microseconds}
1932 begin
1933 seconds_and_micros(s,m);
1934 if (s-epochseconds)>32767 then
1935 get_microinterval := max_integer
1936 else if (microseconds>m) then
1937 get_microinterval := ((s-1-epochseconds)*65536)+ (((m+1000000-microseconds)/100)*65536)/10000
1938 else
1939 get_microinterval := ((s-epochseconds)*65536) + (((m-microseconds)/100)*65536)/10000;
1940 end;
1941
1942 @ @<Declare procedures needed in |do_ext...@>=
1943
1944 procedure compare_strings; {to implement \.{\\pdfstrcmp}}
1945 label done;
1946 var s1, s2: str_number;
1947 i1, i2, j1, j2: pool_pointer;
1948 c1, c2: integer;
1949 save_cur_cs: pointer;
1950 begin
1951 save_cur_cs:=cur_cs; call_func(scan_toks(false, true));
1952 isprint_utf8:=true; s1 := tokens_to_string(def_ref); isprint_utf8:=false;
1953 delete_token_ref(def_ref);
1954 cur_cs:=save_cur_cs; call_func(scan_toks(false, true));
1955 isprint_utf8:=true; s2 := tokens_to_string(def_ref); isprint_utf8:=false;
1956 delete_token_ref(def_ref);
1957 i1 := str_start[s1];
1958 j1 := str_start[s1 + 1];
1959 i2 := str_start[s2];
1960 j2 := str_start[s2 + 1];
1961 while (i1 < j1) and (i2 < j2) do begin
1962 if str_pool[i1]>=@"100 then c1:=str_pool[i1]-@"100 else c1:=str_pool[i1];
1963 if str_pool[i2]>=@"100 then c2:=str_pool[i2]-@"100 else c2:=str_pool[i2];
1964 if c1<c2 then begin cur_val := -1; goto done; end
1965 else if c1>c2 then begin cur_val := 1; goto done; end;
1966 incr(i1);
1967 incr(i2);
1968 end;
1969 if (i1 = j1) and (i2 = j2) then
1970 cur_val := 0
1971 else if i1 < j1 then
1972 cur_val := 1
1973 else
1974 cur_val := -1;
1975 done:
1976 flush_str(s2);
1977 flush_str(s1);
1978 cur_val_level := int_val;
1979 end;
1980
1981 @ Next, we implement \.{\\pdfsavepos} and related primitives.
1982
1983 @<Glob...@>=
1984 @!cur_page_width: scaled; {"physical" width of page being shipped}
1985 @!cur_page_height: scaled; {"physical" height of page being shipped}
1986 @!pdf_last_x_pos: integer;
1987 @!pdf_last_y_pos: integer;
1988
1989 @ @<Implement \.{\\pdfsavepos}@>=
1990 begin
1991 new_whatsit(pdf_save_pos_node, small_node_size);
1992 inhibit_glue_flag:=false;
1993 end
1994
1995 @ @<Save current position in DVI mode@>=
1996 begin
1997 case dvi_dir of
1998 dir_yoko: begin pdf_last_x_pos := cur_h; pdf_last_y_pos := cur_v; end;
1999 dir_tate: begin pdf_last_x_pos := -cur_v; pdf_last_y_pos := cur_h; end;
2000 dir_dtou: begin pdf_last_x_pos := cur_v; pdf_last_y_pos := -cur_h; end;
2001 endcases;
2002 pdf_last_x_pos := pdf_last_x_pos + 4736286;
2003 pdf_last_y_pos := cur_page_height - pdf_last_y_pos - 4736286;
2004 {4736286 = 1in, the funny DVI origin offset}
2005 end
2006
2007 @ @<Calculate DVI page dimensions and margins@>=
2008 if pdf_page_height <> 0 then
2009 cur_page_height := pdf_page_height
2010 else if (box_dir(p)=dir_tate)or(box_dir(p)=dir_dtou) then
2011 cur_page_height := width(p) + 2*v_offset + 2*4736286
2012 else
2013 cur_page_height := height(p) + depth(p) + 2*v_offset + 2*4736286;
2014 {4736286 = 1in, the funny DVI origin offset}
2015 if pdf_page_width <> 0 then
2016 cur_page_width := pdf_page_width
2017 else if (box_dir(p)=dir_tate)or(box_dir(p)=dir_dtou) then
2018 cur_page_width := height(p) + depth(p) + 2*h_offset + 2*4736286
2019 else
2020 cur_page_width := width(p) + 2*h_offset + 2*4736286
2021 {4736286 = 1in, the funny DVI origin offset}
2022
2023
2024 @ Of course \epTeX\ can produce a \.{DVI} file only, not a PDF file.
2025 A \.{DVI} file does not have the information of the page height,
2026 which is needed to implement \.{\\pdflastypos} correctly.
2027 To keep the information of the page height, I (H.~Kitagawa)
2028 adopted \.{\\pdfpageheight} primitive from pdf\TeX.
2029
2030 In \pTeX (and \hbox{\epTeX}), the papersize special
2031 \.{\\special\{papersize=\<width>,\<height>\}} is commonly used
2032 for specifying page width/height.
2033 If \.{\\readpapersizespecial} is greater than~0, the papersize special also
2034 changes the value of \.{\\pdfpagewidth} and \.{\\pdfpageheight}.
2035 This process is done in the following routine.
2036
2037 {\def\<#1>{\langle\hbox{#1\/}\rangle}
2038 In present implementation, the papersize special $\<special>$,
2039 which can be interpreted by this routine, is defined as follows.
2040 $$\eqalign{%
2041 \<special> &\longrightarrow \.{papersize=}\<length>\.{,}\<length>\cr
2042 \<length> &\longrightarrow \<decimal>
2043 \<optional~\.{true}>\<physical unit>\cr
2044 \<decimal> &\longrightarrow \.{.} \mid \<digit>\<decimal> \mid
2045 \<decimal>\<digit>\cr
2046 }$$}
2047 Note that any space, ``\.{,}'' as a decimal separator, minus~symbol
2048 are neither permitted.
2049
2050 @d ifps(#)==@+if k+(#)>pool_ptr then goto done @+ else @+ if
2051 @d sop(#)==so(str_pool[#])
2052 @f ifps==if
2053
2054 @<Determine whether this \.{\\special} is a papersize special@>=
2055 begin k:=str_start[str_ptr];@/
2056 ifps(10) @,
2057 (sop(k+0)<>'p')or(sop(k+1)<>'a')or(sop(k+2)<>'p')or
2058 (sop(k+3)<>'e')@|or(sop(k+4)<>'r')or(sop(k+5)<>'s')or
2059 (sop(k+6)<>'i')or(sop(k+7)<>'z')@|or(sop(k+8)<>'e')or
2060 (sop(k+9)<>'=') then goto done;
2061 k:=k+10;
2062 @<Read dimensions in the argument in the papersize special@>;
2063 ifps(1) @, sop(k)=',' then begin
2064 incr(k); cw:=s;
2065 @<Read dimensions in the argument in the papersize special@>;
2066 if pool_ptr>k then goto done;
2067 geq_word_define(dimen_base+pdf_page_width_code,cw);
2068 geq_word_define(dimen_base+pdf_page_height_code,s);@|
2069 cur_page_height := s; cur_page_width := cw;
2070 end;
2071 end;
2072
2073 @
2074
2075 @d if_ps_unit(#)==if bl then @+ begin @+ ifps(2) sop(k)=(#) @, if_ps_unit_two
2076 @d if_ps_unit_two(#)==and (sop(k+1)=(#)) then begin bl:=false; k:=k+2; if_ps_unit_end
2077 @d if_ps_unit_end(#)==# @+ end @+ end;
2078
2079 @d do_ps_conversion(#)==num:=#; do_ps_conversion_end
2080 @d do_ps_conversion_end(#)==
2081 s:=xn_over_d(s,num,#); s:=s*unity+((num*t+@'200000*remainder) div #)
2082
2083 @<Read dimensions in the argument in the papersize special@>=
2084 s:=0; t:=0; bl:=true;
2085 while (k<pool_ptr)and bl do
2086 if (sop(k)>='0')and (sop(k)<='9') then begin s:=10*s+sop(k)-'0'; incr(k); @+end
2087 else bl:=false;
2088 ifps(1) sop(k)='.' then
2089 begin incr(k); bl:=true; i:=0; dig[0]:=0;
2090 while (k<pool_ptr)and bl do begin
2091 if (sop(k)>='0')and (sop(k)<='9') then
2092 begin if i<17 then begin dig[i]:=sop(k)-'0'; incr(i); @+end;
2093 incr(k); end
2094 else bl:=false;
2095 end;
2096 t:=round_decimals(i);
2097 end;
2098 if k+4>pool_ptr then
2099 if (sop(k)='t')and(sop(k+1)='r')and(sop(k+2)='u')and(sop(k+3)='e') then
2100 k:=k+4;
2101 if mag<>1000 then
2102 begin s:=xn_over_d(s,1000,mag);
2103 t:=(1000*t+@'200000*remainder) div mag;
2104 s:=s+(t div @'200000); t:=t mod @'200000;
2105 end;
2106 bl:=true;@/
2107 if_ps_unit('p')('t')(s:=s*unity+t)@/
2108 if_ps_unit('i')('n')(do_ps_conversion(7227)(100))@/
2109 if_ps_unit('p')('c')(do_ps_conversion(12)(1))@/
2110 if_ps_unit('c')('m')(do_ps_conversion(7227)(254))@/
2111 if_ps_unit('m')('m')(do_ps_conversion(7227)(2540))@/
2112 if_ps_unit('b')('p')(do_ps_conversion(7227)(7200))@/
2113 if_ps_unit('d')('d')(do_ps_conversion(1238)(1157))@/
2114 if_ps_unit('c')('c')(do_ps_conversion(14856)(1157))@/
2115 if_ps_unit('s')('p')(do_nothing)
2116
2117 @ Finally, we declare some routine needed for \.{\\pdffilemoddate}.
2118
2119 @<Glob...@>=
2120 @!isprint_utf8: boolean;
2121 @!last_tokens_string: str_number; {the number of the most recently string
2122 created by |tokens_to_string|}
2123
2124 @ @<Declare procedures needed in |do_ext...@>=
2125 procedure scan_pdf_ext_toks;
2126 begin
2127 call_func(scan_toks(false, true)); {like \.{\\special}}
2128 end;
2129
2130 @ @<Declare procedures that need to be declared forward for \pdfTeX@>=
2131 function tokens_to_string(p: pointer): str_number; {return a string from tokens
2132 list}
2133 begin
2134 if selector = new_string then
2135 pdf_error("tokens", "tokens_to_string() called while selector = new_string");
2136 old_setting:=selector; selector:=new_string;
2137 show_token_list(link(p),null,pool_size-pool_ptr);
2138 selector:=old_setting;
2139 last_tokens_string := make_string;
2140 tokens_to_string := last_tokens_string;
2141 end;
2142 procedure flush_str(s: str_number); {flush a string if possible}
2143 begin
2144 if flushable(s) then
2145 flush_string;
2146 end;
2147
2148 @ @<Set initial values of key variables@>=
2149 seconds_and_micros(epochseconds,microseconds);
2150 init_start_time;
2151
2152 @ Negative random seed values are silently converted to positive ones
2153
2154 @<Implement \.{\\pdfsetrandomseed}@>=
2155 begin
2156 scan_int;
2157 if cur_val<0 then negate(cur_val);
2158 random_seed := cur_val;
2159 init_randoms(random_seed);
2160 end
2161
2162 @ @<Implement \.{\\pdfresettimer}@>=
2163 begin
2164 seconds_and_micros(epochseconds,microseconds);
2165 end
2166
2167 @* \[54] System-dependent changes.
2168 @z