Newer
Older
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <fstream.h>
#include <strstream.h>
#include <stdio.h> /* ANSI: utility functions */
#include <stdlib.h> /* ANSI: utility functions */
#include <string.h> /* ANSI: string operations */
//----- string constants
const char VERSION[] = "1.0";
const char EPS_HEADER[] = "%!PS-Adobe-3.0 EPSF-3.0";
const char EPS_BOUNDING_BOX[] = "%%BoundingBox:";
const int LENGTH_EPS_BOUNDING_BOX = strlen( EPS_BOUNDING_BOX ) ;
//----- point / mm
const double ptOverMm = 72.0 / 25.4 ;
//----- A4 size (mm)
const double A4_WIDTH = 210.0 ; // mm
const double A4_HEIGHT = 297.0 ; // mm
//----- A4 size (pt)
const double A4_WIDTH_pt = A4_WIDTH * ptOverMm ;
const double A4_HEIGHT_pt = A4_HEIGHT * ptOverMm ;
//----- size of DAWN picture (mm)
const double DAWN_W_MARGIN = 20.0;
const double DAWN_H_MARGIN = 20.0;
const double DAWN_WIDTH = A4_WIDTH - 2.0 * DAWN_W_MARGIN ;
const double DAWN_HEIGHT = A4_HEIGHT - 2.0 * DAWN_H_MARGIN ;
//----- default shift of bounding box
const double DEFAULT_H_SHIFT = 0.0 ;
const double DEFAULT_V_SHIFT = 0.0 ;
//----- size of line buffer
const int BUFSIZE = 1024 ;
//----- output functions
void output( const char* string , ostream* out_p = &cout )
{
(*out_p) << string << flush ;
}
void output( char* string , ostream* out_p = &cout )
{
(*out_p) << string << flush ;
}
void output( int val , ostream* out_p = &cout )
{
(*out_p) << val << flush ;
}
void output( double val , ostream* out_p = &cout )
{
(*out_p) << val << flush ;
}
void output( double val1 , double val2 , char* string , ostream* out_p = &cout )
{
(*out_p) << val1 << " " << val2 << " " << string << flush ;
}
void output_line( char* string , ostream* out_p = &cout )
{
(*out_p) << string << endl;
}
//----- macro --> value
int analyse_macro( char* string, double* val_p )
{
//----- local
char buf[64];
double ratio ;
int status = 1 ; // initialize to NORMAL
int status_tmp = 0 ;
//----- a4w
if ( !strncmp( string, "a4w", 3 ) )
{
if( strlen(string) == 3 ) {
ratio = 1.0 ;
} else {
ratio = 0.0 ; // initialized to dummy value
strcpy( buf, &(string[3]) ) ;
status_tmp = sscanf( buf, "%lf", &ratio );
if( status_tmp == EOF || ratio <= 0.0 ) {
ratio = 1.0 ;
}
}
*val_p = ratio * A4_WIDTH ;
}
//----- a4 height
else if ( !strncmp( string, "a4h", 3 ) )
{
if( strlen(string) == 3 ) {
ratio = 1.0 ;
} else {
ratio = 0.0 ; // initialized to dummy value
strcpy( buf, &(string[3]) ) ;
status_tmp = sscanf( buf, "%lf", &ratio );
if( status_tmp == EOF || ratio <= 0.0 ) {
ratio = 1.0 ;
}
}
*val_p = ratio * A4_HEIGHT ;
}
//----- dawn width
else if ( !strcmp(string, "dw") ) {
*val_p = DAWN_WIDTH ;
}
//----- dawn height
else if ( !strcmp(string, "dh") ) {
*val_p = DAWN_HEIGHT ;
}
else {
status = 0 ; // undefined macro
}
//----- return value
return status ;
}// analyse_macro()
void usage ()
{
cerr << endl;
cerr << "***** DAWN EPS CLIPPER (ver " << VERSION << ") *****" << endl;
cerr << " Jan 27, 1997 " << endl;
cerr << " Fukui University, S.TANAKA" << endl;
cerr << endl;
cerr << "USAGE: dawn_epsclipper input_eps_file width_mm height_mm [output_eps_file]" << endl;
cerr << endl;
cerr << " This tool clips EPS figure generated by DAWN" << endl;
cerr << " with a \"width_mm x height_mm\" rectangle." << endl;
cerr << " By default, center of the rectangle is the center of the figure." << endl;
cerr << " You can shift the center after invoking this tool. " << endl;
cerr << " If the last argument is not given, the result is output to stdout. " << endl;
cerr << endl;
cerr << "MACROS:" << endl;
cerr << " a4w[val] = 210 x val (= A4_WIDTH (mm) x val)" << endl;
cerr << " a4h[val] = 297 x val (= A4_HEIGHT (mm) x val)" << endl;
cerr << " dw = 170 (= width of DAWN figure (mm))" << endl;
cerr << " dh = 257 (= width of DAWN figure (mm))" << endl;
cerr << endl;
cerr << "EXAMPLE:" << endl;
cerr << " If you want to clip your figure of g4.eps with " << endl;
cerr << " half-A4-width x half-A4-height rectangle " << endl;
cerr << " and output the result into a file g4_clipped.eps, " << endl;
cerr << " do as follows:" << endl;
cerr << " \% dawn_epsclipper g4.eps a4w0.5 a4h0.5 g4_clipped.eps" << endl;
cerr << endl;
}
//----- main
int main( int argc, char* argv[] )
{
//----- variables
double clip_left_pt = 0.0 , clip_bottom_pt = 0.0 ;
double clip_right_pt = 0.0 , clip_top_pt = 0.0 ;
double clip_width_pt = 0.0 , clip_height_pt = 0.0 ;
double h_shift_pt = 0.0 , v_shift_pt = 0.0 ;
double clip_width_mm = 0.0 , clip_height_mm = 0.0 ;
double h_shift_mm = 0.0 , v_shift_mm = 0.0 ;
double value_tmp ;
ifstream fin ;
char buf[BUFSIZE] ;
char s_2[BUFSIZE] ;
char word[2][BUFSIZE] ;
ostream* out_p = NULL ;
ofstream fout ;
//----- define output stream
if( argc == 5 ) {
//----- stream to given file in argv[4]
fout.open( argv[4] );
if( !fout ) {
cerr << "Error: Cannot open file " << argv[4] << "." << endl;
exit(1) ;
}
out_p = &fout ;
} else if ( argc == 4 ) {
//----- stream to stdout
out_p = &cout ;
} else {
usage();
exit(1) ;
}
//----- check validity of input file
fin.open( argv[1] );
if( !fin ) {
cerr << "Error: Cannot open file " << argv[1] << "." << endl;
exit(1) ;
} else {
fin.getline( buf, BUFSIZE );
if( buf[0] != '%' || buf[1] != '!' ) {
cerr << "Error: Given file does not have a header comment." << endl;
exit(1) ;
}
fin.close();
}
//----- read input width
if( analyse_macro( argv[2], &value_tmp ) )
{
clip_width_mm = value_tmp ;
} else if( sscanf( argv[2], "%lg", &clip_width_mm ) == EOF )
{
cerr << "ERROR in arg[2] " << endl;
exit(1) ;
} else {
if( clip_width_mm > A4_WIDTH ) {
cerr << "WARNING: Given clipping width exceeds A4 width." << endl;
cerr << " (Clipping width is reset to A4 width.) " << endl;
clip_width_mm = A4_WIDTH ;
}
if( clip_width_mm <= 0.0 ) {
cerr << "ERROR: Given clipping width is non-positive," << endl;
cerr << " or undefined macro is used." << endl;
exit(1);
}
}
clip_width_pt = clip_width_mm * ptOverMm ;
//----- read height
if( analyse_macro( argv[3], &value_tmp ) )
{
clip_height_mm = value_tmp ;
} else if( sscanf( argv[3], "%lg", &clip_height_mm ) == EOF )
{
cerr << "ERROR in arg[3] " << endl;
exit(1) ;
} else {
if( clip_height_mm > A4_HEIGHT ) {
cerr << "WARNING: Given clipping height exceeds A4 height." << endl;
cerr << " (Clipping height is reset to A4 height.) " << endl;
clip_height_mm = A4_HEIGHT ;
}
if( clip_height_mm <= 0.0 ) {
cerr << "ERROR: Given clipping height is non-positive," << endl;
cerr << " or undefined macro is used." << endl;
exit(1);
}
}
clip_height_pt = clip_height_mm * ptOverMm ;
//----- set shift of bounding box (mm)
h_shift_mm = DEFAULT_H_SHIFT ; // initialize to default value
v_shift_mm = DEFAULT_V_SHIFT ; // initialize to default value
cerr << "Input shift of bounding box in units of mm " ;
cerr << "( default is " << h_shift_mm << " " ;
cerr << v_shift_mm << "): " << flush;
gets(s_2); // accept input from keyboard
if(strlen(s_2) != 0 )
{
//----- performed if non-null string is input
//----- local
istrstream str_in(s_2) ;
//----- analyze input string
if( !(str_in >> word[0] >> word[1]) )
{
//----- if error, keep default values
h_shift_mm = DEFAULT_H_SHIFT ; // set default
v_shift_mm = DEFAULT_V_SHIFT ; // set default
} else {
//----- set h_shift_mm
if( analyse_macro( word[0], &value_tmp ) )
{
//----- set value analyzed from macro
h_shift_mm = value_tmp ;
} else if( sscanf( word[0], "%lg", &value_tmp ) != EOF ) {
//----- Since input string is a value (not a macro), set it.
h_shift_mm = value_tmp ;
} else {
//----- sscanf failed: set default values
cerr << "WARNING: Cannot accept h_shift (use default value and continue)\n";
h_shift_mm = DEFAULT_H_SHIFT ; // set default
}
//----- v_shift_mm
if( analyse_macro( word[1], &value_tmp ) )
{
//----- set value analyzed from macro
v_shift_mm = value_tmp ;
} else if( sscanf( word[1], "%lg", &value_tmp ) != EOF ) {
//----- Since input string is a value (not a macro), set it.
v_shift_mm = value_tmp ;
} else {
//----- sscanf failed: set default values
cerr << "WARNING: Cannot accept v_shift (use default value and continue)\n";
v_shift_mm = DEFAULT_V_SHIFT ; // set default
}
}
} else {
// do nothing and keep dafault values
}
//----- set shift of bounding box (pt)
h_shift_pt = h_shift_mm * ptOverMm ;
v_shift_pt = v_shift_mm * ptOverMm ;
//----- calc clipping area
clip_left_pt = 0.5 * ( A4_WIDTH_pt - clip_width_pt ) + h_shift_pt ;
clip_bottom_pt = 0.5 * ( A4_HEIGHT_pt - clip_height_pt ) + v_shift_pt ;
clip_right_pt = clip_left_pt + clip_width_pt ;
clip_top_pt = clip_bottom_pt + clip_height_pt ;
//----- read input file and modify BoundingBox comment
fin.open( argv[1] );
if( !fin ) {
cerr << "Error: Cannot open file " << argv[1] << "." << endl;
exit(1) ;
}
if ( argc == 5 ) {
cerr << "\nMaking a new EPS file \"" << argv[4] << "\" ...\n" << endl;
} else if ( argc == 4 ) {
cerr << "\nOutputting result to \"stdout\" ...\n" << endl;
}
while( fin.getline( buf, BUFSIZE ) ) {
if( !strncmp( buf, EPS_BOUNDING_BOX , LENGTH_EPS_BOUNDING_BOX ) )
{
//----- output new BoundingBox comment
output ( EPS_BOUNDING_BOX , out_p ) ; output(" " , out_p ) ;
output ( clip_left_pt , out_p ) ; output(" " , out_p ) ;
output ( clip_bottom_pt , out_p ) ; output(" " , out_p ) ;
output ( clip_right_pt , out_p ) ; output(" " , out_p ) ;
output ( clip_top_pt , out_p ) ; output("\n", out_p ) ;
//----- output original BoundingBox comment
output( "%*** Original Bounding Box is: " , out_p ) ; output_line ( buf , out_p ) ;
//----- clip
output("newpath\n" , out_p ) ;
output(clip_left_pt , clip_bottom_pt , "moveto\n", out_p );
output(clip_right_pt , clip_bottom_pt , "lineto\n", out_p );
output(clip_right_pt , clip_top_pt , "lineto\n", out_p );
output(clip_left_pt , clip_top_pt , "lineto\n", out_p );
output("closepath clip\n" , out_p ) ;
} else {
//----- output a line without modification
output_line( buf , out_p ) ;
}
}
fin.close();
//----- ending message
output ( "DONE:\t", &cerr ) ;
output ( EPS_BOUNDING_BOX , &cerr ) ; output(" " , &cerr ) ;
output ( clip_left_pt , &cerr ) ; output(" " , &cerr ) ;
output ( clip_bottom_pt , &cerr ) ; output(" " , &cerr ) ;
output ( clip_right_pt , &cerr ) ; output(" " , &cerr ) ;
output ( clip_top_pt , &cerr ) ; output("\n\n", &cerr ) ;
} // main()