1 /* libwmf ("ipa/eps/device.h"): library for wmf conversion
2    Copyright (C) 2000 - various; see CREDITS, ChangeLog, and sources
3 
4    The libwmf Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8 
9    The libwmf Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public
15    License along with the libwmf Library; see the file COPYING.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18 
19 
20 /* This is called by wmf_play() the *first* time the meta file is played
21  */
wmf_eps_device_open(wmfAPI * API)22 static void wmf_eps_device_open (wmfAPI* API)
23 {	/* wmf_eps_t* ddata = WMF_EPS_GetData (API); */
24 
25 	WMF_DEBUG (API,"~~~~~~~~wmf_[eps_]device_open");
26 
27 
28 }
29 
30 /* This is called by wmf_api_destroy()
31  */
wmf_eps_device_close(wmfAPI * API)32 static void wmf_eps_device_close (wmfAPI* API)
33 {	/* wmf_eps_t* ddata = WMF_EPS_GetData (API); */
34 
35 	WMF_DEBUG (API,"~~~~~~~~wmf_[eps_]device_close");
36 
37 
38 }
39 
40 /* This is called from the beginning of each play for initial page setup
41  */
wmf_eps_device_begin(wmfAPI * API)42 static void wmf_eps_device_begin (wmfAPI* API)
43 {	wmf_eps_t* ddata = WMF_EPS_GetData (API);
44 
45 	wmfStream* out = ddata->out;
46 
47 	float Ox;
48 	float Oy;
49 
50 	float xScale;
51 	float yScale;
52 
53 	time_t t;
54 
55 	WMF_DEBUG (API,"~~~~~~~~wmf_[eps_]device_begin");
56 
57 	if (out == 0) return;
58 
59 	if ((out->reset (out->context)) && ((API->flags & WMF_OPT_IGNORE_NONFATAL) == 0))
60 	{	WMF_ERROR (API,"unable to reset output stream!");
61 		API->err = wmf_E_DeviceError;
62 		return;
63 	}
64 
65 	if ((ddata->bbox.BR.x <= ddata->bbox.TL.x) || (ddata->bbox.BR.y <= ddata->bbox.TL.y))
66 	{	WMF_ERROR (API,"~~~~~~~~wmf_[eps_]device_begin: bounding box has null or negative size!");
67 		API->err = wmf_E_Glitch;
68 		return;
69 	}
70 
71 	if ((ddata->eps_width == 0) || (ddata->eps_height == 0))
72 	{	ddata->eps_width  = (unsigned int) ceil (ddata->bbox.BR.x - ddata->bbox.TL.x);
73 		ddata->eps_height = (unsigned int) ceil (ddata->bbox.BR.y - ddata->bbox.TL.y);
74 	}
75 
76 	if (ddata->flags & WMF_EPS_STYLE_PS) /* Output in full postscript style */
77 	{	wmf_stream_printf (API,out,"%%!PS-Adobe-2.0\n");
78 		wmf_stream_printf (API,out,"%%%%BoundingBox: ");
79 		wmf_stream_printf (API,out," 0 0 %u %u\n",ddata->page_width,ddata->page_height);
80 
81 		if (ddata->Title)
82 		{	wmf_stream_printf (API,out,"%%%%Title: ");
83 			wmf_stream_printf (API,out,"%s\n",ddata->Title);
84 		}
85 		if (ddata->Creator)
86 		{	wmf_stream_printf (API,out,"%%%%Creator: ");
87 			wmf_stream_printf (API,out,"%s\n",ddata->Creator);
88 		}
89 		if (ddata->Date)
90 		{	wmf_stream_printf (API,out,"%%%%Date: ");
91 			wmf_stream_printf (API,out,"%s\n",ddata->Date);
92 		}
93 		else
94 		{	t = time (0);
95 			wmf_stream_printf (API,out,"%%%%Date: ");
96 			wmf_stream_printf (API,out,"%s\n",ctime (&t));
97 		}
98 		if (ddata->For)
99 		{	wmf_stream_printf (API,out,"%%%%For: ");
100 			wmf_stream_printf (API,out,"%s\n",ddata->For);
101 		}
102 
103 		wmf_stream_printf (API,out,"%%%%Pages: 1\n");
104 		wmf_stream_printf (API,out,"%%%%PageOrder: Ascend\n");
105 		wmf_stream_printf (API,out,"%%%%EndComments\n");
106 
107 		wmf_stream_printf (API,out,"%%%%BeginProlog\n");
108 
109 		out->sputs (WMF_PS_DEFS,out->context);
110 
111 		wmf_stream_printf (API,out,"%%%%EndProlog\n");
112 
113 		wmf_stream_printf (API,out,"%%%%BeginSetup\n");
114 		wmf_stream_printf (API,out,"%%%%EndSetup\n");
115 
116 		wmf_stream_printf (API,out,"%%Page: 1 1\n");
117 
118 		if (ddata->flags & WMF_EPS_LANDSCAPE) wmf_stream_printf (API,out,"%%%%PageOrientation: Landscape\n");
119 		else                                  wmf_stream_printf (API,out,"%%%%PageOrientation: Portrait\n" );
120 
121 		wmf_stream_printf (API,out,"%%%%BeginPageSetup\n");
122 
123 		out->sputs (WMF_PS_HEAD,out->context);
124 
125 		wmf_stream_printf (API,out,"gsave\n");
126 
127 		if (ddata->flags & WMF_EPS_LANDSCAPE)
128 		{	wmf_stream_printf (API,out,"%u 0 translate\n",ddata->page_width);
129 			wmf_stream_printf (API,out,"90 rotate\n");
130 		}
131 
132 		wmf_stream_printf (API,out,"%%%%EndPageSetup\n\n");
133 
134 		wmf_stream_printf (API,out,"%d %d translate\n",ddata->eps_x,ddata->eps_y);
135 	}
136 	else                   /* Output in basic encapsulated postscript style */
137 	{	wmf_stream_printf (API,out,"%%!PS-Adobe-2.0 EPSF-2.0\n");
138 		wmf_stream_printf (API,out,"%%%%BoundingBox: ");
139 		wmf_stream_printf (API,out," 0 0 %d %d\n",ddata->eps_width,ddata->eps_height);
140 
141 		out->sputs (WMF_PS_DEFS,out->context);
142 		out->sputs (WMF_PS_HEAD,out->context);
143 
144 		wmf_stream_printf (API,out,"gsave\n");
145 	}
146 
147 	wmf_stream_printf (API,out,"0 %d translate\n",ddata->eps_height);
148 
149 	wmf_stream_printf (API,out,"1 -1 scale\n");
150 
151 	Ox = - ((float) ddata->eps_width  * ddata->bbox.TL.x) / (ddata->bbox.BR.x - ddata->bbox.TL.x);
152 	Oy = - ((float) ddata->eps_height * ddata->bbox.TL.y) / (ddata->bbox.BR.y - ddata->bbox.TL.y);
153 
154 	wmf_stream_printf (API,out,"%f %f translate\n",Ox,Oy);
155 
156 	xScale = (float) ddata->eps_width  / (ddata->bbox.BR.x - ddata->bbox.TL.x);
157 	yScale = (float) ddata->eps_height / (ddata->bbox.BR.y - ddata->bbox.TL.y);
158 
159 	wmf_stream_printf (API,out,"%f %f scale\n",xScale,yScale);
160 
161 	wmf_stream_printf (API,out,"gsave %% begin clip\n");
162 }
163 
164 /* This is called from the end of each play for page termination
165  */
wmf_eps_device_end(wmfAPI * API)166 static void wmf_eps_device_end (wmfAPI* API)
167 {	wmf_eps_t* ddata = WMF_EPS_GetData (API);
168 
169 	wmfStream* out = ddata->out;
170 
171 	WMF_DEBUG (API,"~~~~~~~~wmf_[eps_]device_end");
172 
173 	if (out == 0) return;
174 
175 	wmf_stream_printf (API,out,"grestore %% end clip\n");
176 
177 	if (ddata->flags & WMF_EPS_STYLE_PS) /* Output in full postscript style */
178 	{	wmf_stream_printf (API,out,"%%%%PageTrailer\n");
179 
180 		wmf_stream_printf (API,out,"grestore\n");
181 
182 		out->sputs (WMF_PS_TAIL,out->context);
183 
184 		wmf_stream_printf (API,out,"showpage\n");
185 
186 		wmf_stream_printf (API,out,"%%%%Trailer\n");
187 		wmf_stream_printf (API,out,"%%%%EOF\n");
188 	}
189 	else                   /* Output in basic encapsulated postscript style */
190 	{	wmf_stream_printf (API,out,"grestore\n");
191 
192 		out->sputs (WMF_PS_TAIL,out->context);
193 
194 		wmf_stream_printf (API,out,"showpage\n");
195 	}
196 }
197