1 /*
2  * Copyright (c) 1990-2013  Paul Vojta and the xdvik development team
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef DVI_INIT_H_
25 #define DVI_INIT_H_
26 
27 #include "xdvi-config.h"
28 #include "xdvi.h"
29 
30 #if FREETYPE
31 # include <ft2build.h>
32 # include FT_FREETYPE_H
33 #endif
34 
35 typedef enum {
36     NO_ERROR = 0,
37     WRONG_DVI_VERSION,
38     DVI_CORRUPTED,
39     NOT_A_DVI_FILE,
40     POSTAMBLE_NO_POST,
41     POSTAMBLE_NO_MATCH,
42     POSTAMBLE_NON_FNTDEF,
43     NOT_ALL_PIXEL_FILES_FOUND,
44     NO_BOP_AT_PAGEDESC,
45     FILE_HAS_ZERO_SIZE,
46     FILE_DOESNT_EXIST,
47     FILE_IS_DIRECTORY,
48     PS_CONVERSION_FAILED,
49     PDF_CONVERSION_FAILED,
50     UNKNOWN_ERROR
51 } dviErrFlagT;
52 
53 extern const char *get_dvi_error(dviErrFlagT flag);
54 
55 extern Boolean internal_open_dvi(const char *path, dviErrFlagT *errmsg,
56 #if DELAYED_MKTEXPK
57 				 Boolean read_fonts, Boolean initialize_fonts
58 #else
59 				 Boolean load_fonts
60 #endif
61 				 );
62 extern char *open_dvi_file_wrapper(const char *filename,
63 				   Boolean from_command_line,
64 				   Boolean open_new_instance,
65 				   Boolean *tried_dvi_ext,
66 				   Boolean from_file_history);
67 extern char *get_tmp_dvi_name(void);
68 
69 /* font stuff */
70 
71 /*
72  * Bitmap structure for raster ops.
73  */
74 struct bitmap {
75     unsigned short  w, h;	/* width and height in pixels */
76     short     bytes_wide;	/* scan line width in bytes */
77     char     *bits;		/* pointer to the bits */
78 };
79 
80 /*
81  * Per-character information.
82  * There is one of these for each character in a font (non-virtual fonts only).
83  * All fields are filled in at font definition time,
84  * except for the bitmap, which is "faulted in"
85  * when the character is first referenced.
86  */
87 struct glyph {
88     long addr;		    /* address of bitmap in font file */
89     long dvi_adv;	    /* DVI units to move reference point */
90     short x, y;		    /* x and y offset in pixels */
91     struct bitmap bitmap;   /* bitmap for character */
92     short x2, y2;	    /* x and y offset in pixels for shrunken bitmap */
93 #if GREY
94 # if COLOR
95     struct fgrec *fg;	/* fgrec for which these pixmaps are valid */
96 # endif
97     /* `2' means `shrunken' here */
98     XImage *image2;         /* shrunken pixmap for antialiased character */
99     char *pixmap2;	    /* image data pointer for image2 */
100     char *pixmap2_gc2;	    /* separate image data for drawing image to globals.gc.fore2 */
101 #endif /* GREY */
102     struct bitmap bitmap2;  /* shrunken bitmap for character */
103 #ifdef PTEX
104     int     tdir;
105 #endif /* PTEX */
106     short x3, y3;		/* x and y offset in pixels (backup) */
107     struct bitmap bitmap3;	/* bitmap for character (backup) */
108     float matrix[2][2];
109 };
110 
111 /*
112  * Per-character information for virtual fonts
113  */
114 struct macro {
115 	ubyte	*pos;		/* address of first byte of macro */
116 	ubyte	*end;		/* address of last+1 byte */
117 	long	dvi_adv;	/* DVI units to move reference point */
118 	Boolean	free_me;	/* if free(pos) should be called when */
119 				/* freeing space */
120 };
121 
122 /*
123  * The layout of a font information block.
124  * There is one of these for every loaded font or magnification thereof.
125  * Duplicates are eliminated:  this is necessary because of possible recursion
126  * in virtual fonts.
127  *
128  * Also note the strange units.  The design size is in 1/2^20 point
129  * units (also called micro-points), and the individual character widths
130  * are in the TFM file in 1/2^20 ems units, i.e., relative to the design size.
131  *
132  * We then change the sizes to SPELL units (unshrunk pixel / 2^16).
133  */
134 
135 #define	NOMAGSTP (-29999)
136 
137 #define	FONT_IN_USE	1	/* used for housekeeping */
138 #define	FONT_LOADED	2	/* if font file has been read */
139 #define	FONT_VIRTUAL	4	/* if font is virtual */
140 #ifdef PTEX
141 #define	FONT_KANJI	8	/* if font is kanji */
142 #endif
143 
144 /* forward declarations */
145 struct font;
146 struct tn;
147 
148 typedef	void (*read_char_proc) (struct font *, wide_ubyte);
149 
150 struct font {
151     struct font *next;		/* link to next font info block */
152     char *fontname;		/* name of font */
153     float fsize;		/* size information (dots per inch) */
154     int magstepval;		/* magstep number * two, or NOMAGSTP */
155     FILE *file;			/* open font file or NULL */
156     const char *filename;	/* name of font file */
157     long checksum;		/* checksum */
158     unsigned short timestamp;	/* for LRU management of fonts */
159     ubyte flags;		/* flags byte (see values below) */
160     wide_ubyte maxchar;		/* largest character code */
161     double dimconv;		/* size conversion factor */
162     set_char_proc set_char_p;	/* proc used to set char */
163     /* these fields are used by (loaded) non-virtual fonts */
164     read_char_proc read_char;	/* function to read bitmap */
165     struct glyph *glyph;
166     /* these fields are used by (loaded) virtual fonts */
167     struct font **vf_table;	/* list of fonts used by this vf */
168 #ifdef PTEX
169     struct glyph **kglyph;
170     int ft2_index;
171     int dir;
172 #endif /* PTEX */
173     struct tn *vf_chain;	/* ditto, if TeXnumber >= VFTABLELEN */
174     struct font *first_font;	/* first font defined */
175     struct macro *macro;
176 #if FREETYPE
177     /* these fields are used by (loaded) FreeType fonts */
178     struct ftfont *ft;		/* master record for font (all sizes) */
179     double pixsize;		/* scaled size of the font in pixels */
180     FT_Size size;
181     struct font *next_size;	/* next font from same face */
182 #endif
183 };
184 
185 struct tn {
186     struct tn *next;		/* link to next TeXnumber info block */
187     unsigned long TeXnumber;	/* font number (in DVI file) */
188     struct font *fontp;		/* pointer to the rest of the info */
189 };
190 
191 
192 extern void free_bitmap2(struct glyph *g);
193 extern void reset_fonts(void);
194 #if COLOR
195 extern void reset_colors(void);
196 extern void full_reset_colors(void);
197 #endif
198 extern void realloc_font(struct font *, wide_ubyte);
199 extern void realloc_virtual_font(struct font *, wide_ubyte);
200 extern Boolean load_font(struct font *
201 #if DELAYED_MKTEXPK
202 			 , Boolean load_font_now
203 #endif
204 			 );
205 
206 extern struct font *define_font(
207 #if DELAYED_MKTEXPK
208 				Boolean read_fonts,
209 				Boolean initialize_fonts,
210 #else
211 				Boolean load_font_now,
212 #endif
213 				FILE *,
214 				wide_ubyte,
215 				struct font *,
216 				struct font **,
217 				unsigned int,
218 				struct tn **,
219 				Boolean *not_found_flag);
220 extern void init_page(void);
221 extern void form_dvi_property(void);
222 extern Boolean dvi_file_changed(void);
223 extern Boolean load_dvi_file(
224 #if !DELAYED_MKTEXPK
225 			     Boolean load_fonts,
226 #endif
227 			     dviErrFlagT *errflag);
228 extern void read_PK_index(struct font *, wide_bool);
229 extern void read_GF_index(struct font *, wide_bool);
230 extern unsigned long read_VF_index(struct font *, wide_bool);
231 
232 #if FREETYPE
233 extern Boolean load_ft_font(struct font *fontp);
234 #endif
235 
236 extern Boolean set_paper_type(const char *arg);
237 
238 extern Boolean find_postamble(FILE *fp, dviErrFlagT *errflag);
239 extern Boolean read_postamble(FILE *fp, dviErrFlagT *errflag,
240 #if DELAYED_MKTEXPK
241 			      Boolean read_fonts, Boolean initialize_fonts
242 #else
243 			      Boolean load_fonts
244 #endif
245 			      );
246 extern void close_old_filep(void);
247 extern Boolean process_preamble(FILE *fp, dviErrFlagT *errflag);
248 
249 extern FILE *file_exists(const char *path, dviErrFlagT *errflag);
250 extern char *find_dvi_file(const char *filename, Boolean *tried_dvi_ext, Boolean from_file_history);
251 #endif /* DVI_INIT_H_ */
252