Debian Codegraph @

texk/xdvipsk/xdvips.h

raw
1 /* $Id$
2 * Copyright 1986-2026 Tomas Rokicki.
3 * This is dvips, a freely redistributable PostScript driver
4 * for dvi files. You may freely use, modify and/or distribute this
5 * program or any portion thereof.
6 */
7
8 /* This file is the header for dvips's global data structures. */
9
10 #define CREATIONDATE
11
12 #define MAX_CODE 0x110000
13 #define MAX_VF_CODE 0x1000000
14 #define MAX_2BYTES_CODE 0x10000
15 #define VF_MEM_UNIT 0x10000
16 #define CD_IDX(i) ((i>=MAX_2BYTES_CODE ? MAX_2BYTES_CODE : i))
17
18 #ifdef XDVIPSK
19 #define BANNER \
20 "This is xdvips(k)+lua+freetype " VTEX_VERSION " (" TL_VERSION ") Copyright 2026 Radical Eye Software"
21 #define BANNER2 "(www.vtex.lt)"
22 #define BANNER3 \
23 "Based on dvips(k) " VERSION " (" TL_VERSION ") Copyright 2026 Radical Eye Software"
24 #else
25 #define BANNER \
26 "This is dvips(k) " VERSION " (" TL_VERSION ") Copyright 2026 Radical Eye Software"
27 #define BANNER2 "(www.radicaleye.com)"
28 #endif /* XDVIPSK */
29 #ifdef KPATHSEA
30 #include "config.h"
31 #include "debug.h"
32 #endif
33 /* Please don't turn debugging off! */
34 #ifndef DEBUG
35 #define DEBUG
36 #endif
37
38 #ifdef XDVIPSK
39 // #define GLYPH_CVT_TO_UNI /* conversion of glyph names to "uXXXXX" format */
40 #define CHARCODE_G2U /* generate GlyphNames2Unicode for TeX charcoded otf's */
41 #endif /* XDVIPSK */
42 #ifndef KPATHSEA
43 #include <stdio.h>
44 #if defined(SYSV) || defined(VMS) || defined(__THINK__) || defined(MSDOS) || defined(OS2) || defined(ATARIST) || defined(WIN32)
45 #include <string.h>
46 #else
47 #include <strings.h>
48 #endif
49 #endif
50 #if defined(lint) && defined(sun)
51 extern char *sprintf();
52 #endif
53 #include "paths.h"
54 #include "debug.h"
55 #ifdef XDVIPSK
56 #include <time.h>
57 #include "uthash.h"
58 extern void get_seconds_and_micros(int *, int *);
59 #endif /* XDVIPSK */
60 #ifdef VMS
61 #include "[.vms]vms.h"
62 #endif /* VMS */
63 #include <stdlib.h>
64 /*
65 * Is your malloc big?
66 */
67 #if defined(MSDOS) && !defined(__EMX__) && !defined(DJGPP)
68 #define SMALLMALLOC
69 #endif
70 #if defined(OS2) && defined(_MSC_VER)
71 #define SMALLMALLOC
72 #endif
73 /*
74 * Constants, used to increase or decrease the capacity of this program.
75 *
76 * Strings are now more dynamically allocated, so STRINGSIZE is not the
77 * strict limit it once was. However, it still sets the maximum size
78 * of a string that can be handled in specials, so it should not be
79 * set too small.
80 */
81 #define STRINGSIZE (200000) /* maximum total chars in strings in program */
82 #define RASTERCHUNK (8192) /* size of chunk of raster */
83 #define MINCHUNK (240) /* minimum size char to get own raster */
84 #define STACKSIZE (500) /* maximum stack size for dvi files */
85 #define MAXFRAME (50) /* maximum depth of virtual font recursion */
86 #define MAXFONTHD (1024) /* number of unique names of included fonts */
87 #define STDOUTSIZE (75) /* width of a standard output line */
88 #define DOWNLOADEDPSSIZE (1000) /* max number of downloaded fonts to check */
89 /*
90 * Other constants, which define printer-dependent stuff.
91 */
92 #define SWMEM (180000) /* available virtual memory in PostScript printer */
93 #define DPI (actualdpi) /* dots per inch */
94 #define VDPI (vactualdpi) /* dots per inch */
95 #define RES_TOLERANCE(dpi) ((int)(1+dpi/500))
96 /* error in file name resolution versus desired */
97 #define FONTCOST (298) /* overhead cost of each sw font */
98 #define PSFONTCOST (1100) /* overhead cost for PostScript fonts */
99 #define PSCHARCOST (20) /* overhead cost for PostScript font chars */
100 #define DNFONTCOST (35000) /* overhead cost for downloaded PS font */
101 #define CHARCOST (15) /* overhead cost for each character */
102 #define OVERCOST (30000) /* cost of overhead */
103 #define DICTITEMCOST (20) /* cost per key, value in dictionary */
104 #define NAMECOST (40) /* overhead cost of each new name */
105 /*
106 * Type declarations. integer must be a 32-bit signed; shalfword must
107 * be a sixteen-bit signed; halfword must be a sixteen-bit unsigned;
108 * quarterword must be an eight-bit unsigned.
109 */
110 #if (defined(MSDOS) && !defined(DJGPP)) || (defined(OS2) && defined(_MSC_VER)) || defined(ATARIST)
111 typedef long integer;
112 typedef unsigned long uinteger;
113 #else
114 typedef int integer;
115 typedef unsigned int uinteger;
116 #endif
117 #ifndef KPATHSEA
118 typedef char boolean;
119 #endif
120 typedef double real;
121 typedef short shalfword;
122 typedef unsigned short halfword;
123 typedef unsigned char quarterword;
124 #ifdef WIN32
125 #define Boolean boolean
126 #else
127 #ifndef __THINK__
128 typedef short Boolean;
129 #endif
130 #endif
131 /*
132 * If the machine has a default integer size of 16 bits, and 32-bit
133 * integers must be manipulated with %ld, set the macro SHORTINT.
134 */
135 #ifdef XENIX
136 #define SHORTINT
137 #else
138 #undef SHORTINT
139 #endif
140 #if defined(MSDOS) && !defined(__EMX__) && !defined(DJGPP) || defined(ATARIST)
141 #define SHORTINT
142 #endif
143 #if defined(OS2) && defined(_MSC_VER)
144 #define SHORTINT
145 #endif
146
147 /*
148 * This is the structure definition for resident fonts. We use
149 * a small and simple hash table to handle these. We don't need
150 * a big hash table.
151 */
152 #ifdef XDVIPSK
153 typedef enum
154 { /* Values for resfont::otftype */
155 None_font, /* 0 */ /* Type1 */
156 TrueType_font, /* 1 */
157 PostScript_font, /* 2 */
158 TTC_font, /* 3 */
159 DFONT_font, /* 4 */
160 OTF_font = 100,
161 /* additional values for search functions lookup_ps(), findPSname(), lookup_charused() and otftype_conforms() */
162 Any_type1_font = 126,
163 Any_otf_font = 127
164 } otftype_enum;
165 #endif /* XDVIPSK */
166 #define RESHASHPRIME (73)
167 struct resfont {
168 struct resfont *next;
169 char *Keyname, *PSname, *TeXname, *Fontfile, *Vectfile;
170 char *specialinstructions;
171 char *downloadheader; /* possibly multiple files */
172 quarterword sent;
173 #ifdef XDVIPSK
174 boolean partialdownload;
175 quarterword /* otftype_enum */ otftype; /* 1 - TrueType, 2 - PostScript, 3 - TTC, 4 - DFONT */
176 quarterword index; /* For TTC & DFONT */
177 quarterword embolden; /* embolden percent for OTF fonts */
178 halfword cmap_fmt;
179 integer luamap_idx;
180 char **enc;
181 #endif /* XDVIPSK */
182 };
183
184 #ifdef XDVIPSK
185 typedef struct resfont_ref_rec
186 {
187 struct resfont* resfont_ptr;
188 struct resfont_ref_rec* next;
189 } resfont_ref;
190
191 /* Encoding of the .dvi file */
192 typedef enum
193 {
194 enc_charcode, /* 0 - TeX charcodes */
195 enc_gid /* 1 - gid's for the opentype font characters */
196 } enc_type;
197 extern enc_type Otf_Enc_Type; /* encoding of opentype chars */
198 extern Boolean charcode_otf_g2u; /* flag to generate GlyphNames2Unicode for TeX charcoded otf's as well */
199 #endif /* XDVIPSK */
200 /*
201 * A chardesc describes an individual character. Before the fonts are
202 * downloaded, the flags indicate that the character has already been used
203 * with the following meanings:
204 */
205 typedef struct tcd {
206 #ifdef XDVIPSK
207 integer charcode;
208 #endif /* XDVIPSK */
209 integer TFMwidth;
210 quarterword *packptr;
211 shalfword pixelwidth;
212 quarterword flags, flags2;
213 #ifdef XDVIPSK
214 halfword cid;
215 UT_hash_handle hh; /* makes this structure hashable */
216 #endif /* XDVIPSK */
217 } chardesctype;
218 #define EXISTS (1)
219 #define PREVPAGE (2)
220 #define THISPAGE (4)
221 #define TOOBIG (8) /* not used at the moment */
222 #define REPACKED (16)
223 #define BIGCHAR (32)
224 #define STATUSFLAGS (EXISTS|REPACKED|BIGCHAR)
225 /*
226 * The new field flags2 above is now an immutable field (once a font is
227 * loaded); for now it only indicates whether a character EXISTS or not.
228 * This fixes a problem with -G.
229 */
230 /*
231 * A fontdesc describes a font. The name, area, and scalename are located in
232 * the string pool. The nextsize pointer is used to link fonts that are used
233 * in included psfiles and differ only in scaledsize. Such fonts also have
234 * a non-NULL scalename that gives the scaledsize as found in the included
235 * file. The psflag indicates that the font has been used in an included
236 * psfile. It can be 0, PREVPAGE, THISPAGE, or EXISTS.
237 */
238 typedef struct tfd {
239 integer checksum, scaledsize, designsize, thinspace, dir;
240 halfword dpi, loadeddpi;
241 halfword alreadyscaled;
242 halfword psname;
243 halfword loaded;
244 quarterword psflag;
245 quarterword codewidth;
246 integer maxchars;
247 integer llx, lly, urx, ury ;
248 char *name, *area;
249 struct resfont *resfont;
250 struct tft *localfonts;
251 struct tfd *next;
252 struct tfd *nextsize;
253 char *scalename;
254 #ifndef XDVIPSK
255 chardesctype *chardesc;
256 #else
257 chardesctype *chardesc_hh;
258 #endif /* XDVIPSK */
259 int iswide, kind;
260 } fontdesctype;
261
262 #define VF_TEX (1)
263 #define VF_OMEGA (2)
264 #define VF_PTEX (3)
265 #define TFM_TEX (0x000)
266 #define OFM_OMEGA (0x100)
267 #define JFM_PTEX (0x200)
268
269 /* A fontmap associates a fontdesc with a font number.
270 */
271 typedef struct tft {
272 integer fontnum;
273 fontdesctype *desc;
274 struct tft *next;
275 } fontmaptype;
276
277 /* Virtual fonts require a `macro' capability that is implemented by
278 * using a stack of `frames'.
279 */
280 typedef struct {
281 quarterword *curp, *curl;
282 fontdesctype *curf;
283 fontmaptype *ff;
284 } frametype;
285
286 #ifdef XDVIPSK
287 typedef struct lua_t {
288 integer luamap_idx;
289 struct lua_t *next;
290 } luacharmap;
291 #endif /* XDVIPSK */
292
293 /*
294 * The next type holds the font usage information in a 256-bit table;
295 * there's a 1 for each character that was used in a section.
296 */
297 #ifdef XDVIPSK
298 typedef halfword UsedMapElem;
299 /* 4096 -- buffer size for a bitmap, enough to keep used glyph flags corresponding to all 0x10000 16-bit char code values */
300 #define USED_CHARS_BUF_SIZE (0x10000 / sizeof(UsedMapElem) / 8)
301 #endif /* XDVIPSK */
302 typedef struct {
303 fontdesctype *fd;
304 halfword psfused;
305 #ifndef XDVIPSK
306 halfword bitmap[16];
307 #else
308 UsedMapElem bitmap[USED_CHARS_BUF_SIZE];
309 luacharmap *map_chain;
310 #endif /* XDVIPSK */
311 } charusetype;
312 #ifdef XDVIPSK
313 typedef struct charusetype_entry_rec
314 {
315 charusetype *charused_ptr;
316 struct charusetype_entry_rec *next;
317 } charusetype_entry;
318 typedef struct charusetype_ref_rec
319 {
320 charusetype_entry *head;
321 struct charusetype_ref_rec *next;
322 } charusetype_ref;
323 extern charusetype_ref *charused_hash[RESHASHPRIME];
324 extern charusetype_ref *lookup_charused(const char *ps_name, quarterword /* otftype_enum */ otftype); /* search by resfont::PSname and resfont::otftype */
325
326 extern struct resfont *reshash[RESHASHPRIME];
327 extern resfont_ref *reshash_ps[RESHASHPRIME]; /* parallel alternative of reshash for PSname keys */
328 extern resfont_ref *reshash_v[RESHASHPRIME]; /* parallel alternative of reshash for Vectfile keys */
329 #endif /* XDVIPSK */
330
331 /* Next we want to record the relevant data for a section. A section is
332 * a largest portion of the document whose font usage does not overflow
333 * the capacity of the printer. (If a single page does overflow the
334 * capacity all by itself, it is made into its own section and a warning
335 * message is printed; the page is still printed.)
336 *
337 * The sections are in a linked list, built during the prescan phase and
338 * processed in proper order (so that pages stack correctly on output) during
339 * the second phase.
340 */
341 typedef struct t {
342 integer bos;
343 struct t *next;
344 halfword numpages;
345 } sectiontype;
346
347 /*
348 * Sections are actually represented not by sectiontype but by a more
349 * complex data structure of variable size, having the following layout:
350 * sectiontype sect;
351 * charusetype charuse[numfonts];
352 * fontdesctype *sentinel = NULL;
353 * (Here numfonts is the number of bitmap fonts currently defined.)
354 * Since we can't declare this or take a sizeof it, we build it and
355 * manipulate it ourselves (see the end of the prescan routine).
356 */
357 /*
358 * This is how we build up headers and other lists.
359 */
360 struct header_list {
361 struct header_list *next;
362 const char *Hname;
363 char *precode;
364 char *postcode;
365 char *name;
366 };
367
368 #ifdef XDVIPSK
369 /*
370 * Charcodes mapping to glyphs from Luatex
371 */
372 #define GLYPH_NAME_LEN 63
373 #define TEX_NAME_LEN 200
374 /* "tfm:pzdr/a105", "pfb:msbm10/A" */
375 #define FULL_GLYPH_NAME_LEN (TEX_NAME_LEN + GLYPH_NAME_LEN + 4 + 1) /* strlen("tfm:") + strlen("/") */
376
377 #define MAX_TU_COUNT 10
378 typedef struct {
379 int charcode;
380 int gid;
381 halfword tu_count;
382 unsigned int *tounicode;
383 UT_hash_handle hh; /* makes this structure hashable */
384 } luamaptype;
385 /*
386 * Relation between OpenType font names and
387 * ToUnicode cmaps is realized by this structure
388 */
389 typedef struct {
390 int index;
391 char *fontname;
392 char *cmapname;
393 char *cmapext;
394 UT_hash_handle hh; /* makes this structure hashable */
395 } otfcmaptype;
396 #endif /* XDVIPSK */
397
398 /*
399 * Some machines define putlong in their library.
400 * We get around this here.
401 */
402 #define putlong was_putlong
403 /*
404 * Information on available paper sizes is stored here.
405 */
406 struct papsiz {
407 struct papsiz *next;
408 integer xsize, ysize;
409 const char *name;
410 const char *specdat;
411 };
412 #ifdef MVSXA /* IBM: MVS/XA */
413 /* this is where we fix problems with conflicts for truncation
414 of long names (we might only do this if LONGNAME not set but ...) */
415 # define pprescanpages pprscnpgs /* confict with pprescan */
416 # define flushDashedPath flshDshdPth /* conflict with flushDash */
417 # define PageList PgList /* re-definition conflict with pagelist */
418 /* adding ascii2ebcdic conversion table to extern */
419 extern char ascii2ebcdic[];
420 #endif /* IBM: MVS/XA */
421 #ifdef VMCMS /* IBM: VM/CMS */
422 /* this is where we fix problems with conflicts for truncation
423 of long names (we might only do this if LONGNAME not set but ...) */
424 # define pprescanpages pprscnpgs /* confict with pprescan */
425 # define flushDashedPath flshDshdPth /* conflict with flushDash */
426 /* adding ascii2ebcdic conversion table to extern */
427 extern char ascii2ebcdic[];
428 /* redefining fopen for VMCMS, see DVIPSCMS.C */
429 extern FILE *cmsfopen();
430 # ifdef fopen
431 # undef fopen
432 # endif
433 # define fopen cmsfopen
434 #define downloadpspk dnldpspk
435 #endif /* IBM: VM/CMS */
436 /*
437 * Remove namespace conflict with standard library on Macintosh
438 */
439 #ifdef __THINK__
440 #define newstring newpoolstring
441 #endif
442
443 #ifndef VOID
444 #define VOID void
445 #endif
446
447 #define USE_PCLOSE (801)
448 #define USE_FCLOSE (802)
449
450 #ifdef XDVIPSK
451 #define BITS_PER_USED_ELEM (sizeof(UsedMapElem) * 8)
452 #define ADD_TO_USED_CHARS(b, c) { \
453 int ix = (c) / BITS_PER_USED_ELEM; \
454 if (ix >= USED_CHARS_BUF_SIZE) { PRINTF_PR("\nError: %s", "Used chars buffer overflow"); } \
455 else (b)[ix] |= (1 << (BITS_PER_USED_ELEM - 1 - ((c) % BITS_PER_USED_ELEM))); \
456 }
457 #define REMOVE_FROM_USED_CHARS(b, c) { \
458 int ix = (c) / BITS_PER_USED_ELEM; \
459 if (ix >= USED_CHARS_BUF_SIZE) { PRINTF_PR("\nError: %s", "Used chars buffer overflow"); } \
460 else (b)[ix] &= ~(1 << (BITS_PER_USED_ELEM - 1 - ((c) % BITS_PER_USED_ELEM))); \
461 }
462 #define IS_USED_CHAR(b, c) (((c) / BITS_PER_USED_ELEM < USED_CHARS_BUF_SIZE) ? (((b)[(c) / BITS_PER_USED_ELEM]) & (1 << (BITS_PER_USED_ELEM - 1 - ((c) % BITS_PER_USED_ELEM)))) : TRUE)
463 #endif /* XDVIPSK */
464 /* output Unicode string on console in windows */
465 #if defined(KPATHSEA) && defined(WIN32)
466 #undef perror
467 #define fprintf_str win32_fprintf
468 #define fputs_str win32_fputs
469 #define putc_str win32_putc
470 #define perror win32_perror
471 #else
472 #define fprintf_str fprintf
473 #define fputs_str fputs
474 #define putc_str putc
475 #endif
476
477 /* Things that KPATHSEA knows, and are useful even without it. */
478 #if !defined(KPATHSEA)
479
480 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
481 #define FOPEN_ABIN_MODE "ab"
482 #define FOPEN_RBIN_MODE "rb"
483 #else
484 #define FOPEN_ABIN_MODE "a"
485 #define FOPEN_RBIN_MODE "r"
486 #endif
487
488 #if (defined MSDOS || defined OS2 || defined WIN32)
489 #define WRITEBIN "wb"
490 #else
491 #ifdef VMCMS
492 #define WRITEBIN "wb, lrecl=1024, recfm=f"
493 #else
494 #define WRITEBIN "w"
495 #endif
496 #endif
497
498 #if defined(WIN32)
499 #define STDC_HEADERS
500 #include <io.h>
501 #include <fcntl.h>
502 #define O_BINARY _O_BINARY
503 #define register
504 #define SET_BINARY(fd) (void)_setmode((fd), _O_BINARY)
505 #else /* !WIN32 */
506 #define SET_BINARY(fd) (void)0
507 #endif
508
509 #if defined(DEVICESEP)
510 #define IS_DEVICE_SEP(c) ((c) == DEVICESEP)
511 #else
512 #define IS_DEVICE_SEP(c) 0
513 #endif
514 #define STREQ(s1, s2) (((s1) != NULL) && ((s2) != NULL) && !strcmp((s1), (s2)))
515
516 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
517 #define NAME_BEGINS_WITH_DEVICE(name) (*(name) && IS_DEVICE_SEP((name)[1]))
518 #define IS_DIR_SEP(c) ((c) == '/' || (c) == '\\')
519 #define FILESTRCASEEQ(s1, s2) (strcasecmp (s1, s2) == 0)
520 #else
521 #define NAME_BEGINS_WITH_DEVICE(name) 0
522 #define IS_DIR_SEP(c) ((c) == DIRSEP)
523 #define FILESTRCASEEQ STREQ
524 #endif
525
526 #define TOLOWER(c) tolower(c)
527 #define ISALNUM(c) isalnum(c)
528 #define ISXDIGIT(c) (isascii (c) && isxdigit(c))
529
530 #endif