beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / tex / directions.w
blobb010eb4a9ccdef547e0a6a101fdd26116d505e76
1 % directions.w
3 % Copyright 2009-2014 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
7 % LuaTeX is free software; you can redistribute it and/or modify it under
8 % the terms of the GNU General Public License as published by the Free
9 % Software Foundation; either version 2 of the License, or (at your
10 % option) any later version.
12 % LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 % ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 % FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 % License for more details.
17 % You should have received a copy of the GNU General Public License along
18 % with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20 @ @c
22 #include "ptexlib.h"
24 @ @c
25 void scan_direction(void)
27 int save_cur_cmd = cur_cmd;
28 int save_cur_chr = cur_chr;
29 get_x_token();
30 if (cur_cmd == assign_dir_cmd) {
31 cur_val = eqtb[cur_chr].cint;
32 goto EXIT;
33 } else {
34 back_input();
36 if (scan_keyword("TLT")) {
37 cur_val = dir_TLT;
38 } else if (scan_keyword("TRT")) {
39 cur_val = dir_TRT;
40 } else if (scan_keyword("LTL")) {
41 cur_val = dir_LTL;
42 } else if (scan_keyword("RTT")) {
43 cur_val = dir_RTT;
44 } else {
45 tex_error("Bad direction", NULL);
46 cur_val = 0;
48 get_x_token();
49 if (cur_cmd != spacer_cmd)
50 back_input();
51 EXIT:
52 cur_cmd = save_cur_cmd;
53 cur_chr = save_cur_chr;
56 @ the next two are used by postlinebreak.c
59 halfword do_push_dir_node(halfword p, halfword a)
61 halfword n = copy_node(a);
62 vlink(n) = p;
63 return n;
66 halfword do_pop_dir_node(halfword p)
68 halfword n = vlink(p);
69 flush_node(p);
70 return n;
73 @ @c
74 halfword dir_ptr;
76 halfword text_dir_ptr;
78 @ There is no need to do anything here at the moment.
80 void initialize_directions(void)
84 @ @c
85 halfword new_dir(int s)
87 halfword p = new_node(dir_node, 0);
88 dir_dir(p) = s;
89 dir_dvi_ptr(p) = -1;
90 dir_level(p) = cur_level;
91 return p;
94 @ The global static array variable |dir_strings| is also used
95 by the lua nodelib interface, so it cannot be static. Putting
96 it here instead of there avoid the nodelib having to know
97 about the actual values of |dir_TRT| etc.
102 const char *dir_strings[128] = {
103 "-TLT","???", "???", "???", "-TRT","???", "???", "???",
104 "???", "-LTL","???", "???", "???", "???", "???", "???",
105 "???", "???", "???", "???", "???", "???", "???", "???",
106 "-RTT","???", "???", "???", "???", "???", "???", "???",
107 "???", "???", "???", "???", "???", "???", "???", "???",
108 "???", "???", "???", "???", "???", "???", "???", "???",
109 "???", "???", "???", "???", "???", "???", "???", "???",
110 "???", "???", "???", "???", "???", "???", "???", "???",
111 "+TLT","???", "???", "???", "+TRT","???", "???", "???",
112 "???", "+LTL","???", "???", "???", "???", "???", "???",
113 "???", "???", "???", "???", "???", "???", "???", "???",
114 "+RTT","???", "???", "???", "???", "???", "???", "???",
115 "???", "???", "???", "???", "???", "???", "???", "???",
116 "???", "???", "???", "???", "???", "???", "???", "???",
117 "???", "???", "???", "???", "???", "???", "???", "???",
118 "???", "???", "???", "???", "???", "???", "???", "???"
121 int dir_swap = 64;
124 const char *dir_strings[8] = {
125 "-TLT","-TRT","-LTL","-RTT",
126 "+TLT","+TRT","+LTL","+RTT",
129 int dir_swap = 4;
131 const char *string_dir(int d)
133 return (dir_strings[d+dir_swap]+1);
136 @ @c
137 void print_dir(int d)
139 tprint(string_dir(d));
142 @ @c
143 scaled pack_width(int curdir, int pdir, halfword p, boolean isglyph)
145 scaled wd = 0;
146 if (isglyph) {
147 if (textdir_parallel(curdir, pdir) == textglyphdir_orthogonal(pdir)) {
148 wd = glyph_width(p);
149 if (ex_glyph(p) != 0) {
150 /* wd = round_xn_over_d(wd, 1000 + ex_glyph(p)/1000, 1000); */
151 wd = ext_xn_over_d(wd, 1000000+ex_glyph(p), 1000000);
154 } else {
155 wd = glyph_depth(p) + glyph_height(p);
157 } else { /* hlist, vlist, image, form, rule */
158 if (textdir_parallel(pdir, curdir))
159 wd = width(p);
160 else
161 wd = depth(p) + height(p);
163 return wd;
166 @ @c
167 scaled_whd pack_width_height_depth(int curdir, int pdir, halfword p,
168 boolean isglyph)
170 scaled_whd whd = { 0, 0, 0 };
171 whd.wd = pack_width(curdir, pdir, p, isglyph);
172 if (isglyph) {
173 if (is_rotated(curdir)) {
174 if (textdir_parallel(curdir, pdir))
175 whd.ht = whd.dp = (glyph_height(p) + glyph_depth(p)) / 2;
176 else
177 whd.ht = whd.dp = glyph_width(p) / 2;
178 } else if (is_rotated(pdir)) {
179 if (textdir_parallel(curdir, pdir))
180 whd.ht = whd.dp = (glyph_height(p) + glyph_depth(p)) / 2;
181 else
182 whd.ht = glyph_width(p);
183 } else {
184 if (glyphdir_eq(curdir, pdir)) {
185 whd.ht = glyph_height(p);
186 whd.dp = glyph_depth(p);
187 } else if (glyphdir_opposite(curdir, pdir)) {
188 whd.ht = glyph_depth(p);
189 whd.dp = glyph_height(p);
190 } else
191 whd.ht = glyph_width(p);
193 } else {
194 if (is_rotated(curdir)) {
195 if (textdir_parallel(curdir, pdir))
196 whd.ht = whd.dp = (height(p) + depth(p)) / 2;
197 else
198 whd.ht = whd.dp = width(p) / 2;
199 } else if (pardir_eq(curdir, pdir)) {
200 whd.ht = height(p);
201 whd.dp = depth(p);
202 } else if (pardir_opposite(curdir, pdir)) {
203 whd.ht = depth(p);
204 whd.dp = height(p);
205 } else
206 whd.ht = width(p);
208 return whd;
211 @ @c
212 void update_text_dir_ptr(int val)
214 if (dir_level(text_dir_ptr) == cur_level) {
215 /* update */
216 dir_dir(text_dir_ptr) = val;
217 } else {
218 /* addition */
219 halfword text_dir_tmp = new_dir(val);
220 vlink(text_dir_tmp) = text_dir_ptr;
221 text_dir_ptr = text_dir_tmp;