1 /* Copyright (C) 2001-2005 Ghostgum Software Pty Ltd.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under licence and may not be copied,
7   modified or distributed except as expressly authorised under the terms
8   of the licence contained in the file LICENCE in this distribution.
9 */
10 
11 /* $Id: common.h,v 1.15 2005/06/10 09:39:24 ghostgum Exp $ */
12 /* Common header */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include <ctype.h>
19 #include "cplat.h"
20 #include "cfile.h"
21 
22 #define __PROTOTYPES__
23 
24 #define MAXSTR 256
25 #define COPY_BUF_SIZE 4096
26 
27 #define GS_REVISION_MIN 704
28 #define GS_REVISION     704
29 #define GS_REVISION_MAX 899
30 
31 #define return_error(val) return val
32 
33 #ifndef min
34 #define min(a,b) ((a) < (b) ? (a) : (b))
35 #endif
36 #ifndef max
37 #define max(a,b) ((a) > (b) ? (a) : (b))
38 #endif
39 
40 /* Opaque types */
41 
42 #ifndef CDSC_TYPEDEF
43 #define CDSC_TYPEDEF
44 typedef struct CDSC_s CDSC;
45 #endif
46 
47 #ifndef Doc_TYPEDEF
48 #define Doc_TYPEDEF
49 typedef struct Doc_s Doc;
50 #endif
51 
52 #ifndef GSDLL_TYPEDEF
53 #define GSDLL_TYPEDEF
54 typedef struct GSDLL_s GSDLL;
55 #endif
56 
57 #ifndef PLDLL_TYPEDEF
58 #define PLDLL_TYPEDEF
59 typedef struct PLDLL_s PLDLL;
60 #endif
61 
62 #ifndef GSIMAGE_TYPEDEF
63 #define GSIMAGE_TYPEDEF
64 typedef struct GSIMAGE_s GSIMAGE;
65 #endif
66 
67 #ifndef GSREQ_TYPEDEF
68 #define GSREQ_TYPEDEF
69 typedef struct GSREQ_s GSREQ;
70 #endif
71 
72 #ifndef GSSRV_TYPEDEF
73 #define GSSRV_TYPEDEF
74 typedef struct GSSRV_s GSSRV;
75 #endif
76 
77 #ifndef GSview_TYPEDEF
78 #define GSview_TYPEDEF
79 typedef struct GSview_s GSview;
80 #endif
81 
82 #ifndef HISTORY_TYPEDEF
83 #define HISTORY_TYPEDEF
84 typedef struct HISTORY_s HISTORY;
85 #endif
86 
87 #ifndef IMAGE_TYPEDEF
88 #define IMAGE_TYPEDEF
89 typedef struct IMAGE_s IMAGE;
90 #endif
91 
92 #ifndef MEDIA_TYPEDEF
93 #define MEDIA_TYPEDEF
94 typedef struct MEDIA_s MEDIA;
95 #endif
96 
97 #ifndef OPTION_TYPEDEF
98 #define OPTION_TYPEDEF
99 typedef struct OPTION_s OPTION;
100 #endif
101 
102 #ifndef PAGECACHE_TYPEDEF
103 #define PAGECACHE_TYPEDEF
104 typedef struct PAGECACHE_s PAGECACHE;
105 #endif
106 
107 #ifndef PDFLINK_TYPEDEF
108 #define PDFLINK_TYPEDEF
109 typedef struct PDFLINK_s PDFLINK;
110 #endif
111 
112 #ifndef View_TYPEDEF
113 #define View_TYPEDEF
114 typedef struct View_s View;
115 #endif
116 
117 
118 /* Public functions */
119 
120 /***************************************/
121 /* Unicode, Multiple Byte Character Set and narrow strings. */
122 
123 #ifdef UNICODE
124 #define CHARNEXT(x) ((x)+1)
125 #else
126 #define CHARNEXT(x) (global_codepage == CODEPAGE_SBCS ? ((x)+1) : ((x)+char_next(x)))
127 
128 typedef enum {
129     CODEPAGE_SBCS = 0,	/* single byte character set */
130     CODEPAGE_UTF8 = 1,
131     CODEPAGE_EUC = 2,
132     CODEPAGE_SJIS = 3
133 } CODEPAGE;
134 
135 extern CODEPAGE global_codepage; 	/* GLOBAL */
136 int char_next(const char *str);
137 #endif
138 
139 /* convert between cs (wide or multibyte) and narrow strings */
140 int cs_to_narrow(char *nstr, int nlen, LPCTSTR wstr, int wlen);
141 int narrow_to_cs(TCHAR *wstr, int wlen, const char *nstr, int nlen);
142 
143 /* Implementation that just copies the string */
144 int char_to_narrow(char *nstr, int nlen, LPCTSTR wstr, int wlen);
145 int narrow_to_char(TCHAR *wstr, int wlen, const char *nstr, int nlen);
146 
147 /* Convert ISO-Latin1 to UTF-8 */
148 /* Used where unknown text is passed to gtk+ GUI */
149 int latin1_to_utf8(char *ustr, int ulen, const char *str, int slen);
150 
151 /***************************************/
152 /* Debugging flags */
153 
154 #define DEBUG_GENERAL	0x01
155 #define DEBUG_GDI	0x02	/* GDI printing */
156 #define DEBUG_MEM	0x04	/* memory allocation */
157 #define DEBUG_LOG	0x08	/* write gs_addmess() to file c:\gsview.txt */
158 #define DEBUG_GSINPUT	0x10	/* log all input written to GS */
159 #define DEBUG_DEV	0x80	/* For debugging */
160 
161 extern int debug;			/* GLOBAL */
162 
163 /***************************************/
164 /* Debugging malloc */
165 /*
166 #define DEBUG_MALLOC
167 */
168 #ifdef DEBUG_MALLOC
169 # define malloc(x) debug_malloc(x,__FILE__,__LINE__)
170 # define calloc debug_calloc
171 # define realloc debug_realloc
172 # define free debug_free
173 void * debug_malloc(size_t size, const char *file, int line);
174 void * debug_realloc(void *block, size_t size);
175 void debug_free(void *block);
176 void debug_memory_report(void);
177 #endif
178 
179 
180 /***************************************/
181 
182 /* should put this in cpagelst.h */
183 
184 typedef struct PAGELIST_s {
185     int current;	/* index of current selection */
186     BOOL multiple;	/* true if multiple selection allowed */
187     int page_count;	/* number of entries in select */
188     BOOL *select;	/* array of selection flags */
189     BOOL reverse;	/* reverse pages when extracting or printing */
190 } PAGELIST;
191 
192 void page_list_free(PAGELIST *page_list);
193 void page_list_copy(PAGELIST *newlist, PAGELIST *oldlist);
194 void page_list_init(PAGELIST *page_list, int pages);
195 
196 
197 /***************************************/
198 
199 typedef enum ORIENT_e {
200     ORIENT_DEFAULT = -1,	/* use PDF/DSC default */
201     /* following values match setpagedevice /Orientation */
202     ORIENT_PORTRAIT = 0,
203     ORIENT_LANDSCAPE = 3,
204     ORIENT_UPSIDEDOWN = 2,
205     ORIENT_SEASCAPE = 1
206 } ORIENT;
207 
208 
209 typedef enum DISPLAY_FORMAT_e {
210     DISPLAY_FORMAT_AUTO = 0,
211     DISPLAY_FORMAT_GREY_1 = 1,
212     DISPLAY_FORMAT_GREY_8 = 2,
213     DISPLAY_FORMAT_COLOUR_4 = 3,
214     DISPLAY_FORMAT_COLOUR_8 = 4,
215     DISPLAY_FORMAT_COLOUR_24 = 5,
216     DISPLAY_FORMAT_CMYK_32 = 6,
217     DISPLAY_FORMAT_COUNT = 7
218 } DISPLAY_FORMAT;
219 
220 /***************************************/
221 /* PAGESPEC is used in rendering requests, and to describe
222  * the results of a render.
223  */
224 typedef struct PAGESPEC_s PAGESPEC;
225 
226 typedef enum PAGESIZE_METHOD_e {
227     PAGESIZE_GIVEN = 0,		/* use llx,lly,urx,ury */
228     PAGESIZE_CROPBOX = 1,	/* use PDF crop box */
229     PAGESIZE_MEDIABOX = 2,	/* use PDF media box */
230     PAGESIZE_VARIABLE = 3	/* default is llx,lly,urx,ury, but allow */
231 				/* user PostScript to change page size */
232 } PAGESIZE_METHOD;
233 
234 struct PAGESPEC_s {
235     TCHAR filename[MAXSTR];
236     int page;			/* page number */
237     PAGESIZE_METHOD pagesize;
238     /* If pagesize is not PAGESIZE_GIVEN, then the following dimensions
239      * do not need to be specified on request, but will be filled
240      * in just before rendering.  This is because we don't know th
241      * PDF page size until we start rendering.
242      */
243     float llx;	/* Points */
244     float lly;	/* Points */
245     float urx;	/* Points */
246     float ury;	/* Points */
247 
248     float xddpi;	/* display dpi */
249     float yddpi; 	/* display dpi */
250     float xrdpi;	/* render dpi */
251     float yrdpi; 	/* render dpi */
252     /* On request, set both req_orient and orient to the desired orientation
253      * After rendering, orient will be set to the actual orientation,
254      * while orient_request may be left as ORIENT_DEFAULT.
255      * This is needed because we don't know the actual PDF orientation
256      * until we start rendering.
257      */
258     ORIENT orient_request;
259     ORIENT orient;
260     unsigned int format;	/* display device format */
261     int alpha_text;		/* 1, 2 or 4 */
262     int alpha_graphics;		/* 1, 2 or 4 */
263 };
264 
265 
266