1 /*	$Id$ */
2 /*
3  * Copyright (c) 1993-1996 Sam Leffler
4  * Copyright (c) 1993-1996 Silicon Graphics, Inc.
5  * HylaFAX is a trademark of Silicon Graphics
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 #ifndef _TextFormat_
27 #define	_TextFormat_
28 /*
29  * Simple Text To PostScript Conversion Support.
30  */
31 #include "Str.h"
32 #include "StrDict.h"
33 #include "FaxConfig.h"
34 
35 typedef long TextCoord;		// local coordinates
36 
37 class TextFont {
38 private:
39     enum { maxaliases = 10 };		// max no of aliases to find a font
40     fxStr	family;			// font family name
41     fxStr	setproc;		// PostScript setfont procedure
42     fxStr	showproc;		// PostScript show procedure
43     TextCoord	widths[256];		// width table
44 
45     static fxStr	fontMap;	// location of Fontmap file
46     static fxStr	fontPath;	// path for afm files
47     static u_int	fontID;		// font identifier number
48     static fxStrDict	fontMapDict;	// Font maps dictionary
49     static bool		fontMapsLoaded;	// Font maps have been loaded in memory
50 
51     friend class TextFormat;
52 
53     void loadFixedMetrics(TextCoord w);
54     FILE* openAFMFile(fxStr& pathname);
55     bool getAFMLine(FILE* fp, char* buf, int bsize);
56 
57     static void error(const char* fmt ...);
58 
59     static void loadFontMap(const char* fontMapFile);
60     static void loadFontMaps(void);
61     static bool decodeFontName(const char*, fxStr&, fxStr&);
62     static bool findAFMFile(const char* name, fxStr& filename, fxStr& emsg,
63 	bool noExtension = false);
64 
65 public:
66     TextFont(const char*);
67     ~TextFont();
68 
69     static bool findFont(const char* name);
70 
71     void defFont(FILE*, TextCoord pointSize, bool useISO8859) const;
72     void setfont(FILE*) const;
73     TextCoord show(FILE*, const char*, int len) const;
74     TextCoord show(FILE*, const fxStr&) const;
75     TextCoord strwidth(const char*) const;
76     TextCoord charwidth(const char c) const;
77 
78     const char* getFamily(void) const;
79 
80     bool readMetrics(TextCoord pointsize, bool useISO8859, fxStr& emsg);
81 };
82 /*
83  * we have to use const u_char
84  */
charwidth(const char c)85 inline TextCoord TextFont::charwidth(const char c) const { return widths[(u_char) c]; }
getFamily(void)86 inline const char* TextFont::getFamily(void) const	 { return family; }
87 
88 class FontDict;
89 class OfftArray;
90 
91 class TextFormat : public FaxConfig {
92 public:
93     enum {			// page orientation
94 	LANDSCAPE,
95 	PORTRAIT
96     };
97     enum {			// page collation
98 	FORWARD,
99 	REVERSE
100     };
101 private:
102     bool	gaudy;		// emit gaudy headers
103     bool	landscape;	// horizontal landscape mode output
104     bool	useISO8859;	// use the ISO 8859-1 character encoding
105     bool	reverse;	// page reversal flag
106     bool	wrapLines;	// wrap/truncate lines
107     bool	headers;	// emit page headers
108     bool	workStarted;	// formatting work begun
109     FILE*	output;		// output file stream
110     FILE*	tf;		// temporary output file
111     OfftArray*	pageOff;	// page offset table
112     int		firstPageNum;	// starting page number
113     fxStr	curFile;	// current input filename (for header)
114     fxStr	modDate;	// last modification date for input file
115     fxStr	modTime;	// last modification time for input file
116     fxStr	title;		// document title information
117     FontDict*	fonts;		// font dictionary
118     TextFont*	curFont;	// current font for imaging text
119 
120     float	physPageHeight;	// physical page height (inches)
121     float	physPageWidth;	// physical page width (inches)
122     TextCoord	pointSize;	// font point size in big points
123     TextCoord	lm, rm;		// left, right margins in local coordinates
124     TextCoord	tm, bm;		// top, bottom margin in local coordinates
125     TextCoord	lineHeight;	// inter-line spacing
126     bool	boc;		// at beginning of a column
127     bool	bop;		// at beginning of a page
128     bool	bol;		// at beginning of a line
129     bool	bot;		// at beginning of a text string
130     int		numcol;		// number of text columns
131     int		column;		// current text column # (1..numcol)
132     TextCoord	col_margin;	// inter-column margin
133     TextCoord	col_width;	// column width in local coordinates
134     int		level;		// PS string parenthesis level
135     TextCoord	outline;	// page and column outline linewidth
136     TextCoord	pageHeight;	// page height in local coordinates
137     int		pageNum;	// current page number
138     TextCoord	pageWidth;	// page width in local coordinates
139     TextCoord	right_x;	// column width (right hand side x)
140     int		tabStop;	// n-column tab stop
141     TextCoord	tabWidth;	// tab stop width in local units
142     TextCoord	x, y;		// current coordinate
143     TextCoord	xoff;		// current x offset on line
144 
145     void putISOPrologue(void);
146     void emitPrologue(void);
147     void emitTrailer(void);
148 
149     void Copy_Block(off_t ,off_t);
150 protected:
151     virtual void emitClientComments(FILE*);
152     virtual void emitClientPrologue(FILE*);
153 
154     virtual void warning(const char* fmt ...) const;
155     virtual void error(const char* fmt ...) const;
156     virtual void fatal(const char* fmt ...) const;
157 
158     virtual void setupConfig(void);
159     virtual bool setConfigItem(const char* tag, const char* value);
160     virtual void configError(const char* fmt ...);
161     virtual void configTrace(const char* fmt ...);
162 public:
163     TextFormat();
164     virtual ~TextFormat();
165 
166     virtual void resetConfig(void);
167 
168     static TextCoord inch(const char*);
169 
170     void setNumberOfColumns(u_int n);
171     void setPageHeaders(bool);
172     bool getPageHeaders(void) const;
173     void setLineWrapping(bool);
174     bool getLineWrapping(void) const;
175     void setISO8859(bool);
176     bool getISO8859(void) const;
177     bool setTextFont(const char* fontName);
178     void setGaudyHeaders(bool);
179     bool setPageMargins(const char*);
180     void setPageMargins(TextCoord l, TextCoord r, TextCoord b, TextCoord t);
181     void setOutlineMargin(TextCoord);
182     void setTextPointSize(TextCoord);
183     TextCoord getTextPointSize(void) const;
184     void setPageOrientation(u_int);
185     bool setPageSize(const char*);
186     void setPageWidth(float);
187     void setPageHeight(float);
188     void setPageCollation(u_int);
189     void setTextLineHeight(TextCoord);
190     TextCoord getTextLineHeight(void) const;
191     void setTitle(const char*);
192 
193     void setFilename(const char*);
194     void setModTimeAndDate(time_t);
195     void setModTime(const char*);
196     void setModDate(const char*);
197 
198     void setOutputFile(FILE*);
199     FILE* getOutputFile(void);
200     void flush(void);
201 
202     void beginFormatting(FILE* output);
203     void endFormatting(void);
204     void beginFile(void);
205     void formatFile(const char*);
206     void formatFile(FILE*);
207     void endFile(void);
208     void format(FILE*);
209     void format(const char*, u_int cc);
210 
211     void newPage(void);
212     void newCol(void);
213     void beginCol(void);
214     void endCol(void);
215     void beginLine();
216     void endLine(void);
217     void beginText(void);
218     void endTextLine(void);
219     void endTextCol(void);
220     void closeStrings(const char* cmd);
221 
222     TextFont* addFont(const char* name, const char* family);
223     const TextFont* getFont(void) const;
224     const TextFont* getFont(const char* name) const;
225     void setFont(TextFont*);
226     void setFont(const char*);
227     void setFontPath(const char*);
228 
229     void hrMove(TextCoord);
230     TextCoord getXOff(void) const;
231     TextCoord getRHS(void) const;
232     void reserveVSpace(TextCoord);
233 };
234 
getLineWrapping(void)235 inline bool TextFormat::getLineWrapping(void) const	{ return wrapLines; }
getPageHeaders(void)236 inline bool TextFormat::getPageHeaders(void) const	{ return headers; }
getTextPointSize(void)237 inline TextCoord TextFormat::getTextPointSize(void) const	{ return pointSize; }
getXOff(void)238 inline TextCoord TextFormat::getXOff(void) const		{ return xoff; }
getRHS(void)239 inline TextCoord TextFormat::getRHS(void) const		{ return right_x; }
getTextLineHeight(void)240 inline TextCoord TextFormat::getTextLineHeight(void) const	{ return lineHeight; }
getISO8859(void)241 inline bool TextFormat::getISO8859(void) const		{ return useISO8859; }
getFont(void)242 inline const TextFont* TextFormat::getFont(void) const	{ return curFont; }
getOutputFile(void)243 inline FILE* TextFormat::getOutputFile(void)		{ return tf; }
244 #endif /* _TextFormat_ */
245