Debian Codegraph @

texk/xdvik/vf.c

raw
1 /*========================================================================*\
2
3 Copyright (c) 1992-2004 Paul Vojta
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to
7 deal in the Software without restriction, including without limitation the
8 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 sell copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
19 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22
23 \*========================================================================*/
24
25 /*
26 * VF font reading routines.
27 * Public routine is read_index---because virtual characters are presumed
28 * to be short, we read the whole virtual font in at once, instead of
29 * faulting in characters as needed.
30 */
31
32 #include "xdvi-config.h"
33 #include "dvi.h"
34 #include "xdvi.h"
35 #include "util.h"
36 #include "dvi-init.h"
37 #include "dvi-draw.h"
38
39 #define LONG_CHAR 242
40
41 /*
42 * These are parameters which determine whether macros are combined for
43 * storage allocation purposes. Small macros ( <= VF_PARM_1 bytes) are
44 * combined into chunks of size VF_PARM_2.
45 */
46
47 #ifndef VF_PARM_1
48 #define VF_PARM_1 20
49 #endif
50 #ifndef VF_PARM_2
51 #define VF_PARM_2 256
52 #endif
53
54 /*
55 * The main routine
56 */
57
58 unsigned long
59 read_VF_index(struct font *fontp, wide_bool hushcs)
60 {
61 FILE *VF_file = fontp->file;
62 ubyte cmnd;
63 ubyte *avail, *availend; /* available space for macros */
64 long checksum;
65 struct macro *newmacro;
66 unsigned long maxcc = 0;
67 Boolean dummy_success = False;
68
69 fontp->read_char = NULL;
70 fontp->flags |= FONT_VIRTUAL;
71 fontp->set_char_p = set_vf_char;
72 if (globals.debug & DBG_PK)
73 printf("Reading VF file %s\n", fontp->filename);
74 /*
75 * Read preamble.
76 */
77 fseek(VF_file, (long)get_byte(VF_file), SEEK_CUR); /* skip comment */
78 checksum = get_bytes(VF_file, 4);
79 if (checksum != fontp->checksum && checksum != 0 && fontp->checksum != 0
80 && !hushcs)
81 XDVI_WARNING((stderr, "Checksum mismatch (dvi = %lu, vf = %lu) in font file %s",
82 fontp->checksum, checksum, fontp->filename));
83 (void)get_bytes(VF_file, 4); /* skip design size */
84 /*
85 * Read the fonts.
86 */
87 fontp->vf_table = xmalloc(VFTABLELEN * sizeof(struct font *));
88 memset((char *)fontp->vf_table, 0, VFTABLELEN * sizeof(struct font *));
89 fontp->vf_chain = NULL;
90 fontp->first_font = NULL;
91 while ((cmnd = get_byte(VF_file)) >= FNTDEF1 && cmnd <= FNTDEF4) {
92 struct font *newfontp = define_font(True,
93 #if DELAYED_MKTEXPK
94 True,
95 #endif
96 VF_file, cmnd, fontp,
97 fontp->vf_table, VFTABLELEN,
98 &fontp->vf_chain,
99 &dummy_success);
100 if (fontp->first_font == NULL)
101 fontp->first_font = newfontp;
102 }
103 /*
104 * Prepare macro array.
105 */
106 if (resource.omega) {
107 fontp->maxchar = 65535;
108 fontp->macro = xmalloc(65536 * sizeof(struct macro));
109 memset((char *)fontp->macro, 0, 65536 * sizeof(struct macro));
110 }
111 else {
112 fontp->macro = xmalloc(256 * sizeof(struct macro));
113 memset((char *)fontp->macro, 0, 256 * sizeof(struct macro));
114 }
115 /*
116 * Read macros.
117 */
118 avail = availend = NULL;
119 for (; cmnd <= LONG_CHAR; cmnd = get_byte(VF_file)) {
120 struct macro *m;
121 int len;
122 unsigned long cc;
123 long width;
124
125 if (cmnd == LONG_CHAR) { /* long form packet */
126 len = get_bytes(VF_file, 4);
127 cc = get_bytes(VF_file, 4);
128 width = get_bytes(VF_file, 4);
129 if ((resource.omega && cc >= 65536)
130 || (!resource.omega && cc >= 256)) {
131 XDVI_WARNING((stderr, "Virtual character %lu in font %s ignored.",
132 cc, fontp->fontname));
133 fseek(VF_file, (long)len, SEEK_CUR);
134 continue;
135 }
136 }
137 else { /* short form packet */
138 len = cmnd;
139 cc = get_byte(VF_file);
140 width = get_bytes(VF_file, 3);
141 }
142 if (resource.omega) {
143 maxcc = (cc > maxcc) ? cc : maxcc;
144 }
145 m = &fontp->macro[cc];
146 m->dvi_adv = width * fontp->dimconv;
147 if (len > 0) {
148 if (len <= availend - avail) {
149 m->pos = avail;
150 avail += len;
151 }
152 else {
153 m->free_me = True;
154 if (len <= VF_PARM_1) {
155 m->pos = avail = xmalloc(VF_PARM_2);
156 availend = avail + VF_PARM_2;
157 avail += len;
158 }
159 else
160 m->pos = xmalloc((unsigned)len);
161 }
162 (void)fread((char *)m->pos, 1, len, VF_file);
163 m->end = m->pos + len;
164 }
165 if (globals.debug & DBG_PK)
166 printf("Read VF macro for character %lu; dy = %ld, length = %d\n",
167 cc, m->dvi_adv, len);
168 }
169 if (cmnd != POST)
170 XDVI_FATAL((stderr, "Wrong command byte found in VF macro list: %d", cmnd));
171
172 fclose(VF_file);
173 fontp->file = NULL;
174 if (resource.omega) {
175 size_t i;
176 newmacro = xmalloc((maxcc + 1) * sizeof(struct macro));
177 for (i = 0; i <= maxcc; i++) {
178 newmacro[i] = fontp->macro[i];
179 }
180 free(fontp->macro);
181 fontp->macro = newmacro;
182 fontp->maxchar = maxcc;
183 return maxcc;
184 }
185 else
186 return 0; /* dummy */
187 }