aboutsummaryrefslogtreecommitdiff
path: root/generic/PostScript.c
blob: 3f2b7a7da533c31d8eff70d560bba9b44b85f3d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * PostScript.c -- Implementation of PostScript driver.
 *
 * Authors		: Patrick Lecoanet.
 * Creation date	: Tue Jan 3 13:17:17 1995
 *
 * $Id$
 */

/*
 *  Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet --
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this code; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


/*
 **********************************************************************************
 *
 * Included files
 *
 **********************************************************************************
 */

#include <malloc.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <time.h>

#include "Item.h"
#include "PostScript.h"
#include "WidgetInfo.h"
#include "Geo.h"


/*
 **********************************************************************************
 *
 * Constants.
 * 
 **********************************************************************************
 */

static	const char	rcsid[] = "$Id$";
static const char compile_id[]="$Compile: " __FILE__ " " __DATE__ " " __TIME__ " $";


#define PROLOG_VERSION	1.0
#define PROLOG_REVISION	0

static	char	ps_prolog[] = "";


/*
 **********************************************************************************
 *
 * SetPostScriptFont --
 *
 **********************************************************************************
 */
static void
SetPostScriptFont(WidgetInfo		*wi,
		  PostScriptInfo	ps_info,
		  XFontStruct		*fs)
{
}


/*
 **********************************************************************************
 *
 * EmitPostScript --
 *
 **********************************************************************************
 */
static void
EmitPostScript(WidgetInfo	*wi,
	       FILE		*file,
	       char		*title,
	       RadarBool	landscape,
	       int		color_mode,
	       int		x_world,
	       int		y_world,
	       int		world_width,
	       int		world_height,
	       int		bbox_ox,
	       int		bbox_oy,
	       int		bbox_cx,
	       int		bbox_cy)
{
  PostScriptInfo	ps_info;
  /*  double		scale;*/
  RadarBBox		damaged_area, bbox;
  Item			current_item;
  struct passwd		*pwd_info;
  time_t		now;
  char			*s;
  XFontStruct		*fs;
  int			i;

  ps_info = (PostScriptInfo) RadarMalloc(sizeof(PostScriptStruct));
  ps_info->file = file;
  ps_info->title = title;
  ps_info->landscape = landscape;
  ps_info->color_mode = color_mode;
  ps_info->x_world = x_world;
  ps_info->y_world = y_world;
  ps_info->world_width = world_width;
  ps_info->world_height = world_height;
  ps_info->page_bbox.orig.x = bbox_ox;
  ps_info->page_bbox.orig.y = bbox_oy;
  ps_info->page_bbox.corner.x = bbox_cx;
  ps_info->page_bbox.corner.y = bbox_cy;
  ps_info->fonts = RadarListNew(8, sizeof(XFontStruct *));

  /*
   * Setup the new transform.
   */
  /*  scale = wi->scale;
  damaged_area = wi->damaged_area;
  wi->scale = ps_info->world_width /
    (ps_info->page_bbox.orig.x - ps_info->page_bbox.corner.x);
    ITEM_P.InvalidateItems(wi, RadarAny);*/

  /*
   * Emit Encapsulated PostScript Header.
   */
  fprintf(ps_info->file, "%%!PS-Adobe-3.0 EPSF-3.0\n");
  fprintf(ps_info->file, "%%%%Creator: Radar Widget\n");
  pwd_info = getpwuid(getuid());
  fprintf(ps_info->file, "%%%%For: %s\n", pwd_info ? pwd_info->pw_gecos : "Unknown");
  fprintf(ps_info->file, "%%%%Title: (%s)\n", ps_info->title);
  time(&now);
  fprintf(ps_info->file, "%%%%CreationDate: %s\n", ctime(&now));
  if (ps_info->landscape) {
    fprintf(ps_info->file, "%%%%BoundingBox: %d %d %d %d\n", 1, 1, 1, 1);
  }
  else {
    fprintf(ps_info->file, "%%%%BoundingBox: %d %d %d %d\n", 1, 1, 1, 1);
  }
  fprintf(ps_info->file, "%%%%Pages: 1\n");
  fprintf(ps_info->file, "%%%%DocumentData: Clean7Bit\n");
  fprintf(ps_info->file, "%%%%Orientation: %s\n",
	  ps_info->landscape ? "Landscape" : "Portrait");
  fprintf(ps_info->file, "%%%%LanguageLevel: 1\n");
  fprintf(ps_info->file, "%%%%DocumentNeededResources: (atend)\n");
  fprintf(ps_info->file,
	  "%%%%DocumentSuppliedResources: procset Radar-Widget-Prolog %f %d\n",
	  PROLOG_VERSION, PROLOG_REVISION);
  fprintf(ps_info->file, "%%%%EndComments\n\n\n");

  /*
   * Emit the prolog.
   */
  fprintf(ps_info->file, "%%%%BeginProlog\n");
  fprintf(ps_info->file, "%%%%BeginResource: procset Radar-Widget-Prolog %f %d\n",
	  PROLOG_VERSION, PROLOG_REVISION);
  fwrite(ps_prolog, 1, sizeof(ps_prolog), ps_info->file);
  fprintf(ps_info->file, "%%%%EndResource\n");
  fprintf(ps_info->file, "%%%%EndProlog\n");

  /*
   * Emit the document setup.
   */
  fprintf(ps_info->file, "%%%%BeginSetup\n");
  fprintf(ps_info->file, "%%%%EndSetup\n");

  /*
   * Emit the page setup.
   */
  fprintf(ps_info->file, "%%%%Page: 0 1\n");
  fprintf(ps_info->file, "%%%%BeginPageSetup\n");
  fprintf(ps_info->file, "%%%%EndPageSetup\n");

  /*
   * Iterate through all items emitting PostScript for each.
   */
  current_item = ((GroupItem) wi->top_group)->tail;
  while (current_item != RADAR_NO_ITEM) {
    if (ISSET(current_item->flags, VISIBLE_BIT)) {
      IntersectBBox(&ps_info->page_bbox, &current_item->item_bounding_box, &bbox);
      if (!IsEmptyBBox(&bbox)) {
	current_item->class->PostScript(current_item, ps_info);
      }
    }
    current_item = current_item->previous;
  }

  /*
   * Emit the page trailer.
   */
  fprintf(ps_info->file, "%%%%PageTrailer\n");

  /*
   * Emit the document trailer.
   */
  fprintf(ps_info->file, "%%%%Trailer\n");
  s = "%%DocumentNeededResources: font ";
  for (fs = (XFontStruct *) RadarListArray(ps_info->fonts),
	 i = RadarListSize(ps_info->fonts); i > 0; i--, fs++) {
    fprintf(ps_info->file, "%s", s);
    s = "%%+ font";
  }
  fprintf(ps_info->file, "%%%%EOF\n");

  /*
   * Restore the original transform.
   */
  /*wi->scale = scale;
    ITEM_P.InvalidateItems(wi, RadarAny);*/
  wi->damaged_area = damaged_area;


  RadarListFree(ps_info->fonts);
  RadarFree(ps_info);
}


/*
 **********************************************************************************
 *
 * Exported functions struct --
 *
 **********************************************************************************
 */

struct _POSTSCRIPT_P POSTSCRIPT_P = {
  EmitPostScript,
  SetPostScriptFont
};