Debian Codegraph @

texk/web2c/luatexdir/tex/texdeffont.c

raw
1 /*
2
3 texdeffont.w
4
5 Copyright 2008-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 When the user defines \.{\\font\\f}, say, \TeX\ assigns an internal number to the
29 user's font~\.{\\f}. Adding this number to |font_id_base| gives the |eqtb|
30 location of a ``frozen'' control sequence that will always select the font.
31
32 The variable |a| in the following code indicates the global nature of the value
33 to be set. It's used in the |define| macro. Here we're never global.
34
35 There's not much scanner code here because the other scanners are defined where
36 they make most sense.
37
38 */
39
40 int font_bytes;
41
42 void set_cur_font(internal_font_number f)
43 {
44 int a = 0;
45 define(cur_font_loc, data_cmd, f);
46 }
47
48 /*tex
49
50 This prints a scaled real, rounded to five digits.
51
52 */
53
54 static char *scaled_to_string(scaled s)
55 {
56 static char result[16];
57 int n, k;
58 /*tex The amount of allowable inaccuracy: */
59 scaled delta;
60 k = 0;
61 if (s < 0) {
62 /*tex Only print the sign, if negative */
63 result[k++] = '-';
64 s = -s;
65 }
66 {
67 int l = 0;
68 char digs[8] = { 0 };
69 n = s / unity;
70 /*tex Process the integer part: */
71 do {
72 digs[l++] = (char) (n % 10);
73 n = n / 10;;
74 } while (n > 0);
75 while (l > 0) {
76 result[k++] = (char) (digs[--l] + '0');
77 }
78 }
79 result[k++] = '.';
80 s = 10 * (s % unity) + 5;
81 delta = 10;
82 do {
83 if (delta > unity) {
84 /*tex Round the last digit: */
85 s = s + 0100000 - 050000;
86 }
87 result[k++] = (char) ('0' + (s / unity));
88 s = 10 * (s % unity);
89 delta = delta * 10;
90 } while (s > delta);
91 result[k] = 0;
92 return (char *) result;
93 }
94
95 void tex_def_font(small_number a)
96 {
97 /*tex The user's font identifier. */
98 pointer u;
99 /*tex This runs through existing fonts. */
100 internal_font_number f;
101 /*tex The name for the frozen font identifier. */
102 str_number t, d;
103 /*tex Thos holds the |selector| setting. */
104 int old_setting;
105 /*tex Stated `at' size, or negative of scaled magnification. */
106 scaled s = -1000;
107 /*tex The natural direction of the font. */
108 int natural_dir = -1;
109 char *fn;
110 if (job_name == 0) {
111 /*tex Avoid confusing \.{texput} with the font name. */
112 open_log_file();
113 }
114 get_r_token();
115 u = cur_cs;
116 if (a >= 4) {
117 geq_define(u, set_font_cmd, null_font);
118 } else {
119 eq_define(u, set_font_cmd, null_font);
120 }
121 scan_optional_equals();
122 /*tex Get the next non-blank non-call token. */
123 do {
124 get_x_token();
125 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
126
127 if (cur_cmd != left_brace_cmd) {
128 back_input();
129 scan_file_name();
130 if (cur_area != get_nullstr() || cur_ext != get_nullstr()) {
131 /*tex
132 Have to do some rescue-ing here, fonts only have a name, no area
133 nor extension.
134 */
135 old_setting = selector;
136 selector = new_string;
137 if (cur_area != get_nullstr()) {
138 print(cur_area);
139 flush_str(cur_area);
140 }
141 if (cur_name != get_nullstr()) {
142 print(cur_name);
143 flush_str(cur_name);
144 }
145 if (cur_ext != get_nullstr()) {
146 print(cur_ext);
147 flush_str(cur_ext);
148 }
149 selector = old_setting;
150 cur_area = get_nullstr();
151 cur_name = make_string();
152 cur_ext = get_nullstr();
153 }
154 } else {
155 back_input();
156 (void) scan_toks(false, true);
157 old_setting = selector;
158 selector = new_string;
159 token_show(def_ref);
160 selector = old_setting;
161 flush_list(def_ref);
162 cur_area = get_nullstr();
163 cur_name = make_string();
164 cur_ext = get_nullstr();
165 }
166 /*tex
167 Scan the font size specification. The next variable keeps |cur_name| from
168 being changed
169 */
170 name_in_progress = true;
171 if (scan_keyword("at")) {
172 /*tex Put the positive `at' size into |s|. */
173 scan_normal_dimen();
174 s = cur_val;
175 if ((s <= 0) || (s >= 01000000000)) {
176 char err[256];
177 const char *errhelp[] = {
178 "I can only handle fonts at positive sizes that are",
179 "less than 2048pt, so I've changed what you said to 10pt.",
180 NULL
181 };
182 snprintf(err, 255, "Improper `at' size (%spt), replaced by 10pt", scaled_to_string(s));
183 tex_error(err, errhelp);
184 s = 10 * unity;
185 }
186 } else if (scan_keyword("scaled")) {
187 scan_int();
188 s = -cur_val;
189 if ((cur_val <= 0) || (cur_val > 32768)) {
190 char err[256];
191 const char *errhelp[] = {
192 "The magnification ratio must be between 1 and 32768.",
193 NULL
194 };
195 snprintf(err, 255, "Illegal magnification has been changed to 1000 (%d)", (int) cur_val);
196 tex_error(err, errhelp);
197 s = -1000;
198 }
199 }
200 /*tex
201 There is no real reason to support this obsolete key as there are no useful
202 fonts out there so let's get rid of this overhead. This also means that
203 |natural_dir| can go away.
204 */
205 /*
206 if (scan_keyword("naturaldir")) {
207 scan_direction();
208 natural_dir = cur_val;
209 }
210 */
211 name_in_progress = false;
212 fn = makecstring(cur_name);
213 f = read_font_info(u, fn, s, natural_dir);
214 xfree(fn);
215 equiv(u) = f;
216 eqtb[font_id_base + f] = eqtb[u];
217 /*tex
218
219 This is tricky: when we redefine a string we loose the old one. So this
220 will change as it's only used to display the |\fontname| so we can store
221 that with the font. To be consistent with the rest of the code, unlike
222 |pdftex| the text of |null_cs| (i.e. |\csname\endcsname|) is the empty
223 string |""|.
224
225 */
226 d = cs_text(font_id_base + f);
227 t = (u >= null_cs) ? ((u==null_cs) ? get_nullstr() : cs_text(u)) : maketexstring("FONT");
228 if (!d) {
229 /*tex We have a new string. */
230 cs_text(font_id_base + f) = t;
231 } else if ((d!=t) && str_eq_str(d,t)){
232 /*tex We have a duplicate string. */
233 flush_str(t);
234 } else if (d!=t){
235 d = search_string(t) ;
236 if (d) {
237 /*tex We have already such a string. */
238 cs_text(font_id_base + f) = d;
239 flush_str(t);
240 } else {
241 /*tex The old value is lost but still in the pool. */
242 cs_text(font_id_base + f) = t;
243 }
244 }
245 if (cur_name == str_ptr-1) {
246 flush_str(cur_name);
247 cur_name = get_nullstr();
248 }
249 }