aboutsummaryrefslogtreecommitdiff
path: root/src/gerber-output-dev.cc
blob: f4b4aabd6db01096851ee140339416a5226f34ad (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
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*-  */
/*
 * gerber-output-dev.cc
 * Copyright (C) 2018 David Barksdale <amatus@amat.us>
 *
 * pdf2gerber 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 3 of the License, or
 * (at your option) any later version.
 * 
 * pdf2gerber 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, see <http://www.gnu.org/licenses/>.
 */

#include <cmath>
#include <iostream>
#include <GfxState.h>
#include "gerber-output-dev.h"

static double midpoint(double a, double b, double c, double d)
{
	return (a + 3 * (b + c) + d) / 8;
}

static void center(double x0, double y0,
                   double x1, double y1,
                   double x2, double y2,
                   double *x, double *y)
{
	double idet = 1 / ((x0 - x1) * (y1 - y2) - (x1 - x2) * (y0 - y1));
	double offset = x1 * x1 + y1 * y1;
  double m01 = (x0 * x0 + y0 * y0 - offset) / 2;
  double m12 = (offset - x2 * x2 - y2 * y2) / 2;
   
  *x = (m01 * (y1 - y2) - m12 * (y0 - y1)) * idet;
  *y = (m12 * (x0 - x1) - m01 * (x1 - x2)) * idet;
}

static bool clockwise(double x0, double y0,
                      double x1, double y1,
                      double x2, double y2)
{
	return (x1 - x0) * (y2 - y0) < (x2 - x0) * (y1 - y0);
}

// Constructor.
GerberOutputDev::GerberOutputDev()
{
	// Set format to 6.6
	std::cout << "%FSLAX66Y66*%" << std::endl
	// Set units to mm
		<< "%MOMM*%" << std::endl
	// Set multi-quadrant, why would you ever not want this?
		<< "G75*" << std::endl;
}

// Does this device use upside-down coordinates?
// (Upside-down means (0,0) is the top left corner of the page.)
GBool GerberOutputDev::upsideDown()
{
	return gFalse;
}

// Does this device use drawChar() or drawString()?
GBool GerberOutputDev::useDrawChar()
{
	return gTrue;
}

// Does this device use beginType3Char/endType3Char?  Otherwise,
// text in Type 3 fonts will be drawn with drawChar/drawString.
GBool GerberOutputDev::interpretType3Chars()
{
	return gFalse;
}

// Start a page.
void GerberOutputDev::startPage(int pageNum, GfxState *state, XRef *xref)
{
	std::cout << "G04 startPage "
		<< pageNum << "*" << std::endl;
}

// End a page.
void GerberOutputDev::endPage()
{
	std::cout << "G04 endPage*" << std::endl;
	std::cout << "M02*" << std::endl;
}

// Dump page contents to display.
void GerberOutputDev::dump()
{
	//std::cerr << "dump" << std::endl;
}

//----- path painting
void GerberOutputDev::stroke(GfxState *state)
{
	GfxPath *path = state->getPath();
	double width = state->getTransformedLineWidth();
	GfxGray gray;
	
  state->getStrokeGray(&gray);
	std::cout << "G04 stroke width " << width
		<< " gray "	<< colToDbl(gray) << "*" << std::endl;
	// McCAD seems to always generate vertical or horizontal strokes.
	// Since stoking an aperature will add stuff to the ends of the stroke
	// we're just going to make a region.
	if (1 != path->getNumSubpaths()) {
		std::cerr << "Stroke with more than 1 sub-path" << std::endl;
		return;
	}
	GfxSubpath *subpath = path->getSubpath(0);

	if (0 != state->getLineCap()) {
		std::cerr << "Stoke with non-but line cap" << std::endl;
		return;
	}
	if (colToDbl(gray) < 0.5) {
		// Set polarity to dark
		std::cout << "%LPD*%" << std::endl;
	} else {
		// Set polarity to clear
		std::cout << "%LPC*%" << std::endl;
	}
	std::cout << "G36*" << std::endl;
	if (2 == subpath->getNumPoints()) {
		double x0, y0, x1, y1;

		state->transform(subpath->getX(0), subpath->getY(0), &x0, &y0);
		state->transform(subpath->getX(1), subpath->getY(1), &x1, &y1);
		double dx = x1 - x0;
		double dy = y1 - y0;
		double l = sqrt(dx * dx + dy * dy);
		// half-width perpendicular vector
		double hwx = dy / l * width / 2;
		double hwy = -dx / l * width / 2;
		
		std::cout << "X" << coord(x0 + hwx) << "Y" << coord(y0 + hwy)
			<< "D02*" << std::endl;
		std::cout << "G01*" << std::endl;
		std::cout << "X" << coord(x0 - hwx) << "Y" << coord(y0 - hwy)
			<< "D01*" << std::endl;
		std::cout << "X" << coord(x1 - hwx) << "Y" << coord(y1 - hwy)
			<< "D01*" << std::endl;
		std::cout << "X" << coord(x1 + hwx) << "Y" << coord(y1 + hwy)
			<< "D01*" << std::endl;	
		
	} else if (13 == subpath->getNumPoints()) {
		// I lied, sometimes there are circles
		double x0, y0, x1, y1;

		state->transform(subpath->getX(0), subpath->getY(0), &x0, &y0);
		state->transform(subpath->getX(6), subpath->getY(6), &x1, &y1);
		double cx = (x0 + x1) / 2;
		double cy = (y0 + y1) / 2;
		double hw = width / 2;

		std::cout << "G04 x0 " << x0 << " y0 " << y0
			<< " x1 " << x1 << " y1 " << y1 << "*" << std::endl;
		// Make outer circle
		std::cout << "X" << coord(x0 - hw) << "Y" << coord(y0)
			<< "D02*" << std::endl;
		std::cout << "G02*" << std::endl;
		std::cout << "X" << coord(x1 + hw) << "Y" << coord(y1)
					<< "I" << coord(cx - (x0 - hw)) << "J" << coord(cy - y0)
					<< "D01*" << std::endl;
		std::cout << "X" << coord(x0 - hw) << "Y" << coord(y0)
					<< "I" << coord(cx - (x1 + hw)) << "J" << coord(cy - y1)
					<< "D01*" << std::endl;
		// Make inner circle
		std::cout << "G01*" << std::endl;
		std::cout << "X" << coord(x0 + hw) << "Y" << coord(y0)
					<< "D01*" << std::endl;
		std::cout << "G03*" << std::endl;
		std::cout << "X" << coord(x1 - hw) << "Y" << coord(y1)
					<< "I" << coord(cx - (x0 + hw)) << "J" << coord(cy - y0)
					<< "D01*" << std::endl;
		std::cout << "X" << coord(x0 + hw) << "Y" << coord(y0)
					<< "I" << coord(cx - (x1 - hw)) << "J" << coord(cy - y1)
					<< "D01*" << std::endl;
	} else {
		std::cerr << "Stroke with points I can't handle" << std::endl;
	}
	std::cout << "G37*" << std::endl;
}

void GerberOutputDev::fill(GfxState *state)
{
	// McCAD seems to generate fills for any shape that's not a rectangle,
	// rectangles are made with either vertical or horizontal strokes.
	GfxPath *path = state->getPath();
	int nsubpaths = path->getNumSubpaths();
	GfxGray gray;
	
  state->getFillGray(&gray);
	std::cout << "G04 fill " << nsubpaths << " subpath(s) gray "
		<< colToDbl(gray) << "*" << std::endl;

	if (colToDbl(gray) < 0.5) {
		// Set polarity to dark
		std::cout << "%LPD*%" << std::endl;
	} else {
		// Set polarity to clear
		std::cout << "%LPC*%" << std::endl;
	}

	// Use a filled region
	std::cout << "G36*" << std::endl;
	for (int i = 0; i < nsubpaths; ++i) {
		GfxSubpath *subpath = path->getSubpath(i);
		int npoints = subpath->getNumPoints();
		int j = 1;
		double x, y;

		state->transform(subpath->getX(0), subpath->getY(0), &x, &y);
		std::cout << "X" << coord(x) << "Y" << coord(y) << "D02*" << std::endl;
		
		while (j < npoints) {
			if (subpath->getCurve(j)) {
				// This is a bezier curve, approximate it with a single arc!
				double x0, y0, x1, y1, x2, y2;

				state->transform(subpath->getX(j), subpath->getY(j), &x0, &y0);
				state->transform(subpath->getX(j + 1), subpath->getY(j + 1), &x1, &y1);
				state->transform(subpath->getX(j + 2), subpath->getY(j + 2), &x2, &y2);
				j += 3;

				double mx = midpoint(x, x0, x1, x2);
				double my = midpoint(y, y0, y1, y2);
				double cx, cy;
				
				center(x, y, mx, my, x2, y2, &cx, &cy);
				if (clockwise(x0, y0, mx, my, x2, y2)) {
					// Set clockwise circular interpolation
					std::cout << "G02*" << std::endl;
				} else {
					// Set counterclockwise circular interpolation
					std::cout << "G03*" << std::endl;
				}
				// Arc to x,y with center offset i,j
				std::cout << "X" << coord(x2) << "Y" << coord(y2)
					<< "I" << coord(cx - x) << "J" << coord(cy - y)
					<< "D01*" << std::endl; 
				x = x2;
				y = y2;
			} else {
				state->transform(subpath->getX(j), subpath->getY(j), &x, &y);
				// Set linear interpolation
				std::cout << "G01*" << std::endl
					// Line to x,y
					<< "X" << coord(x) << "Y" << coord(y) << "D01*" << std::endl;
				++j;
			}
		}
	}
	std::cout << "G37*" << std::endl;
}

//----- text drawing
void GerberOutputDev::drawChar(GfxState *state, double x, double y,
			double dx, double dy,
			double originX, double originY,
			CharCode code, int nBytes, Unicode *u, int uLen)
{
	std::cout << "G04 drawChar("
		<< x << ", "
		<< y << ", "
		<< dx << ", "
		<< dy << ", "
		<< originX << ", "
		<< originY << ", ...)*" << std::endl;
}

int GerberOutputDev::coord(double decimal)
{
	return round(decimal * 1000000);
}