File: drvmpost.cpp

package info (click to toggle)
pstoedit 3.50-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,656 kB
  • ctags: 3,110
  • sloc: cpp: 25,588; sh: 10,050; perl: 4,036; ansic: 955; java: 860; makefile: 450
file content (438 lines) | stat: -rw-r--r-- 13,854 bytes parent folder | download
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
   drvMPOST.cpp : This file is part of pstoedit
   Backend for MetaPost files
   Contributed by: Scott Pakin <scott+ps2ed_AT_pakin.org>

   Copyright (C) 1993 - 2009 Wolfgang Glunz, wglunz35_AT_geocities.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
#ifdef _MSC_VER
// #define _POSIX_					// for MSVC and limits.h
#endif

#include "drvmpost.h"


// #include I_fstream
#include I_stdio
#include I_stdlib

#include <math.h>				// Need definition of FLT_MAX
#include <float.h>				// FLT_MAX on MSVC

#include <limits.h>				// PATH_MAX for MSVC
#include <ctype.h>				// Need definition of ispunct()


#if !(defined(unix) || defined(__unix__) || defined(_unix) || defined(__unix) || defined(__EMX__) || defined (NetBSD)  )
#define LINE_MAX 2048			// for MSVC
#endif

static const string emptystring("");

// Constructor -- Initialize variables and take other per-document actions

drvMPOST::derivedConstructor(drvMPOST):
constructBase,
	// Initialize member variables
	prevFontName(""), prevR(0.0f), prevG(0.0f), prevB(0.0f),	// Black
	prevFontAngle(FLT_MAX), prevFontSize(-1.0), prevLineWidth(0.0f), prevLineCap(1),	// Round line caps
	prevLineJoin(1),			// Round joins
	prevDashPattern(""),		// Solid lines
	fillmode(false)
{
	// Prevent the use of scientific notation, which MetaPost
	// doesn't understand.	For now, we hardwire the precision to
	// 6 digits after the decimal point.
	outf.setf(ios::fixed);
	outf.precision(6);

	// Output copyright information
	outf << "% Converted from PostScript(TM) to MetaPost by pstoedit\n"
		<<
		"% MetaPost backend contributed by Scott Pakin <scott+ps2ed_AT_pakin.org>\n"
		<< "% pstoedit is Copyright (C) 1993 - 2009 Wolfgang Glunz" <<
		" <wglunz35_AT_pstoedit.net>\n\n";

	/*
	 * Output some useful assignments and macro defintions
	 *
	 * Thanks to S.J.Henriksen <eesjh_AT_ee.newcastle.edu.au> for the showtext
	 * macro
	 */
	outf << "% Generate structured PostScript\n"
		<< "prologues := 1;\n"
		<< "\n"
		<<
		"% Display a given string with its *baseline* at a given location\n"
		<< "% and with a given rotation angle\n" <<
		"vardef showtext(expr origin)(expr angle)(expr string) =\n" <<
		"  draw string infont defaultfont scaled defaultscale\n" <<
		"    rotated angle shifted origin;\n" << "enddef;\n" << endl;
}


// Destructor -- Tell MetaPost to end the file
drvMPOST::~drvMPOST()
{
	outf << "end" << endl;
	options=0;
}


// Output a path
void drvMPOST::print_coords()
{
	bool withinpath = false;	// true=already started plotting points
	unsigned int pointsOnLine = 0;	// Number of points on the current output line

	if (fillmode) {
		for (unsigned int n = 0; n < numberOfElementsInPath(); n++) {
			const basedrawingelement & elem = pathElement(n);
			switch (elem.getType()) {
			case moveto:
			case lineto:
				{
					const Point & p = elem.getPoint(0);
					if (withinpath)
						outf << "--";
					else
						outf << "fill ";
					outf << '(' << (p.x_ + x_offset) << ',' << (p.y_ + y_offset)
						<< ')';
					withinpath = true;
					pointsOnLine++;
				}
				break;

			case closepath:
				if (prevDashPattern == emptystring)
					outf << "--cycle;" << endl;
				else
					outf << "--cycle " << prevDashPattern << ';' << endl;
				withinpath = false;
				pointsOnLine = 0;
				break;

			case curveto:
				{
					if (!withinpath) {
						cerr << "curveto without a moveto; ignoring" << endl;
						break;
					}
					const Point & p0 = elem.getPoint(0);
					outf << "..controls (" << (p0.x_ + x_offset)
						<< ',' << (p0.y_ + y_offset) << ") and (";
					const Point & pt1 = elem.getPoint(1);
					outf << (pt1.x_ + x_offset) << ',' << (pt1.y_ + y_offset) << ")..(";
					const Point & pt2 = elem.getPoint(2);
					outf << (pt2.x_ + x_offset) << ',' << (pt2.y_ + y_offset) << ')';
					pointsOnLine += 3;
					withinpath = true;
				}
				break;

			default:
				errf << "\t\tFatal: unexpected path element in drvmpost" << endl;
				abort();
				break;
			}
			if (pointsOnLine >= 3) {
				outf << "\n\t";
				pointsOnLine = 0;
			}
		}
		if (withinpath) {			// Finish the final path
			if (prevDashPattern == emptystring)
				outf << "--cycle;" << endl;
			else
				outf << "--cycle " << prevDashPattern << ';' << endl;
		}
	} else {					// Stroking, not filling
		for (unsigned int n = 0; n < numberOfElementsInPath(); n++) {
			const basedrawingelement & elem = pathElement(n);
			switch (elem.getType()) {
			case moveto:
				{
					if (withinpath)
						outf << prevDashPattern << ';' << endl;
					outf << "draw ";
					const Point & p = elem.getPoint(0);
					outf << '(' << (p.x_ + x_offset) << ',' << (p.y_ + y_offset)
						<< ')';
					withinpath = true;
					pointsOnLine = 1;	// Contrast with the case where fillmode is set
				}
				break;

			case lineto:
				{
					const Point & p = elem.getPoint(0);
					if (withinpath) {
						outf << "--";
					} else {
						cerr << "lineto without a moveto; ignoring" << endl;
						break;
					}
					outf << '(' << (p.x_ + x_offset) << ',' << (p.y_ + y_offset)
						<< ')';
					withinpath = true;
					pointsOnLine++;
				}
				break;

			case closepath:
				if (prevDashPattern == emptystring ) {
					outf << "--cycle;" << endl;
				} else {
					outf << "--cycle " << prevDashPattern << ';' << endl;
				}
				withinpath = false;
				pointsOnLine = 0;
				break;

			case curveto:
				{
					if (!withinpath) {
						cerr << "curveto without a moveto; ignoring" << endl;
						break;
					}
					const Point & p0 = elem.getPoint(0);
					outf << "..controls (" << (p0.x_ + x_offset)
						<< ',' << (p0.y_ + y_offset) << ") and (";
					const Point & pt1 = elem.getPoint(1);
					outf << (pt1.x_ + x_offset) << ',' << (pt1.y_ + y_offset) << ")..(";
					const Point & pt2 = elem.getPoint(2);
					outf << (pt2.x_ + x_offset) << ',' << (pt2.y_ + y_offset) << ')';
					pointsOnLine += 3;
					withinpath = true;
				}
				break;

			default:
				errf << "\t\tFatal: unexpected path element in drvmpost" << endl;
				abort();
				break;
			}
			if (pointsOnLine >= 3 &&
				n + 1 < numberOfElementsInPath() && pathElement(n + 1).getType() != moveto) {
				outf << "\n\t";
				pointsOnLine = 0;
			}
		}
		if (withinpath)	{		// Finish the final path
			outf << prevDashPattern << ';' << endl;
		}
	}
}


// Each page will produce a different figure
void drvMPOST::open_page()
{
	outf << "beginfig(" << currentPageNumber << ");" << endl;
}

void drvMPOST::close_page()
{
	outf << "endfig;" << endl;
}

// Output a text string
void drvMPOST::show_text(const TextInfo & textinfo)
{
	static bool texshortchar = false;	// 0=ASCII; 1=TeX character set

	// Change fonts
	string thisFontName(textinfo.currentFontName.value());
	if (thisFontName ==  emptystring ) {	// If we're this brain-damaged, we must be a TeX font
		thisFontName = textinfo.currentFontFullName.value();
		if (!texshortchar) {
			outf << "shortchar := char(24);" << endl;	// Cedilla in TeX land
			texshortchar = true;
		}
		if (Verbose() && thisFontName != prevFontName)
			errf << "nameless font (" << thisFontName << "?) -- "
				<< "assuming TeX character set" << endl;
	} else if (texshortchar) {
		outf << "shortchar := \"_\";" << endl;
		texshortchar = false;
	}
	if (thisFontName != prevFontName) {
		outf << "defaultfont := \"" << thisFontName << "\";" << endl;
		prevFontName = thisFontName;
	}
	// Change colors
	if (textinfo.currentR != prevR || textinfo.currentG != prevG || textinfo.currentB != prevB) {
		outf << "drawoptions (withcolor ("
			<< textinfo.currentR << ',' << textinfo.
			currentG << ',' << textinfo.currentB << "));" << endl;
		prevR = textinfo.currentR;
		prevG = textinfo.currentG;
		prevB = textinfo.currentB;
	}
	// Change font size
	if (textinfo.currentFontSize != prevFontSize) {
		if (textinfo.currentFontSize > 0) {
			outf << "defaultscale := " << textinfo.currentFontSize
				<< "/fontsize defaultfont;" << endl;
			prevFontSize = textinfo.currentFontSize;
		} else {
			if (Verbose())
				errf << "warning: font size of " << textinfo.
					currentFontSize << "pt encountered; ignoring" << endl;
			outf << "%defaultscale := " << textinfo.
				currentFontSize << "/fontsize defaultfont;" << endl;
		}
	}
	// Change rotation
	if (textinfo.currentFontAngle != prevFontAngle)
		prevFontAngle = textinfo.currentFontAngle;

	// Output the text using macros defined in the constructor
	outf << "showtext ((" << textinfo.x << ',' << textinfo.y << "), " << prevFontAngle << ", \"";
	for (const char *c = textinfo.thetext.value(); *c; c++)
		if (*c == '"')
			outf << "\" & char(34) & \"";
		else
			outf << (*c);
	outf << "\");" << endl;
}


// Output a path
void drvMPOST::show_path()
{
	// Change colors
	if (currentR() != prevR || currentG() != prevG || currentB() != prevB) {
		prevR = currentR();
		prevG = currentG();
		prevB = currentB();
		outf << "drawoptions (withcolor ("
			<< prevR << ',' << prevG << ',' << prevB << "));" << endl;
	}
	// Change line width
	if (currentLineWidth() != prevLineWidth) {
		prevLineWidth = currentLineWidth();
		outf << "pickup pencircle scaled " << prevLineWidth << "bp;" << endl;
	}
	// Change line cap
	if (currentLineCap() != prevLineCap) {
		prevLineCap = currentLineCap();
		switch (prevLineCap) {
		case 0:
			outf << "linecap := butt;" << endl;
			break;
		case 1:
			outf << "linecap := rounded;" << endl;
			break;
		case 2:
			outf << "linecap := squared;" << endl;
			break;
		default:
			errf << "\t\tFatal: Unknown linecap \"" << prevLineCap << '"' << endl;
			abort();
			break;
		}
	}
	// Change line join
	if (currentLineJoin() != prevLineJoin) {
		prevLineJoin = currentLineJoin();
		switch (prevLineJoin) {
		case 0:
			outf << "linejoin := mitered;" << endl;
			break;
		case 1:
			outf << "linejoin := rounded;" << endl;
			break;
		case 2:
			outf << "linejoin := beveled;" << endl;
			break;
		default:
			errf << "\t\tFatal: Unknown linejoin \"" << prevLineJoin << '"' << endl;
			abort();
			break;
		}
	}
	// Change dash pattern
	const char *currentDashPattern = dashPattern();	// Current dash pattern string
	unsigned long lengthOn, lengthOff;	// Fraction on vs. off
	float dashOffset;			// Offset into dash pattern
	if (sscanf_s(currentDashPattern, "[ ] %f", &dashOffset) == 1)
		prevDashPattern = "";
	else {
		char temptext[100];

		if (sscanf_s(currentDashPattern, "[%lu] %f", &lengthOn, &dashOffset)
			== 2) {
			if (dashOffset)
				sprintf_s(TARGETWITHLEN(temptext,100),
						" dashed evenly scaled %lubp shifted -%fbp", lengthOn, dashOffset);
			else
				sprintf_s(TARGETWITHLEN(temptext,100), " dashed evenly scaled %lubp", lengthOn);
			prevDashPattern = temptext;
		} else
			if (sscanf_s
				(currentDashPattern, "[%lu %lu] %f", &lengthOn, &lengthOff, &dashOffset) == 3) {
			if (dashOffset)
				sprintf_s(TARGETWITHLEN(temptext,100),
						" dashed dashpattern(on %lubp off %lubp) shifted (-%f,0)",
						lengthOn, lengthOff, dashOffset);
			else
				sprintf_s(TARGETWITHLEN(temptext,100), " dashed dashpattern(on %lubp off %lubp)", lengthOn, lengthOff);
			prevDashPattern = temptext;
		} else {
			if (Verbose())
				cerr << "Dash pattern \"" << currentDashPattern <<
					"\" is too complex;\n" << "using a generic pattern instead" << endl;
			prevDashPattern = " dashed evenly";
		}
	}

	// Determine the fill mode
	if (Verbose() && currentShowType() == drvbase::eofill)
		errf << "MetaPost does not support eofill; using fill instead" << endl;
	fillmode = (currentShowType() == drvbase::eofill || currentShowType() == drvbase::fill);
	if (!fillmode && currentShowType() != drvbase::stroke) {
		errf << "\t\tFatal: unexpected show type " << (int)
			currentShowType() << " in drvmpost" << endl;
		abort();
	}
	// Draw the path
	print_coords();
}

static DriverDescriptionT < drvMPOST > D_mpost("mpost", "MetaPost Format", "","mp", true,	// if backend supports subpathes, else 0
											   // if subpathes are supported, the backend must deal with
											   // sequences of the following form
											   // moveto (start of subpath)
											   // lineto (a line segment)
											   // lineto 
											   // moveto (start of a new subpath)
											   // lineto (a line segment)
											   // lineto 
											   //
											   // If this argument is set to 0 each subpath is drawn 
											   // individually which might not necessarily represent
											   // the original drawing.
											   true,	// if backend supports curves, else 0
											   false,	// if backend supports elements with fill and edges
											   true,	// if backend supports text, else 0
											   DriverDescription::noimage,	// no support for PNG file images
											   DriverDescription::normalopen, true,	// if format supports multiple pages in one file
											   false  /*clipping */ 
											   );