forked from erichVK5/translate2geda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FootprintText.java
225 lines (193 loc) · 8.5 KB
/
FootprintText.java
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
// FootprintText.java v1.0
// Copyright (C) 2015 Erich S. Heinzle, [email protected]
// see LICENSE-gpl-v2.txt for software license
// see README.txt
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// FootprintText Copyright (C) 2015 Erich S. Heinzle [email protected]
//
// A utility for turning text strings into silkscreen line elements which can
// be added to footprints for labelling purposes.
// v1.0 of the utility uses the free Hershey Sans 1 Stroke Font and outputs
// 0.01mil (imperial, square bracketed) units.
//
public class FootprintText extends FootprintElementArchetype {
// String output = "";
String kicadTextDescriptor = "";
long xCoordNm = 0;
long yCoordNm = 0;
long kicadMheightNm = 0;
long kicadMwidthNm = 0;
long kicadRotation = 0;
long defaultGEDAlineThickness = 1000; // this is 10 mil in 0.01 mil units
long defaultLineThicknessNm = 254000; // which is 254000 nanometres
// which is 254 microns, which is 0.254 mm
// which is 0.01 inches, which is 10 mil = 10 thou
long kicadLineThicknessNm = defaultLineThicknessNm;
boolean textVisibility = true;
int kicadBottomSilkLayer = 20;
int kicadTopSilkLayer = 21;
int kicadBottomCopperLayer = 0;
int kicadTopCopperLayer = 15;
int textLayer = 21;
String displayedTextField = "DefaultText-SpaceForRent";
// default text to convert if nothing supplied
// using chars 32 -> 126 = 95 in total
double magnificationRatio = 1.0; // default value of 1.0 yields default sized font text in gEDA PCB
long yLayoutOffsetNm = 0; // these are used to position the text relative to the module or layout centroid
long xLayoutOffsetNm = 0;
boolean metricFlag = false; // not really needed for text, if we output silk strokes in decimil format regardless
long kicadTextHeightNm = 0;
long kicadTextWidthNm = 1320000;
//
// 1786000 -> ?70.37mil -> 67.3 actual
// default value of 1.829mm for testing = 70.6mil, or
// 1327000 = 83.7mil
public FootprintText(long offsetX, long offsetY) {
// (x,y) position relative to footprint or layout centroid
xLayoutOffsetNm = offsetX;
yLayoutOffsetNm = offsetY;
}
public FootprintText()
{
;
}
public String toString()
{
return kicadTextDescriptor;
}
public String generateGEDAelement(long offsetX, long offsetY, float magnificationRatio) {
xLayoutOffsetNm = offsetX + xCoordNm;
// x position relative to footprint or layout centroid + x position relative to footprint
yLayoutOffsetNm = offsetY + yCoordNm;
// y position relative to footprint or layout centroid + y position relative to footprint
HersheySansFontClass hershey = new HersheySansFontClass();
return hershey.renderKicadText(displayedTextField, xLayoutOffsetNm, yLayoutOffsetNm, kicadRotation, kicadTextWidthNm, magnificationRatio);
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyzz112234567890", 90000, 50000, Math.PI/8, 1.0) +
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyzz112234567890", 30000, 40000, 0, 1.0) +
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyzz112234567890", 30000, 50000, Math.PI, 1.0) +
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyz112234567890", 30000, 40000, 2*Math.PI/5, 1.0) +
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyzz112234567890", 30000, 40000, 5*Math.PI/8, 1.0) +
// hershey.renderString("abcdeffgghhiijklmmnnopqqrrstuvwxyzz112234567890", 30000, 40000, Math.PI/2, 1.0);
}
public void populateElement(String arg, boolean metric)
{
kicadTextDescriptor = arg;
String[] tokens = kicadTextDescriptor.split(" ");
float parsedValue = 0;
if (tokens[0].startsWith("T"))
{
parsedValue = Float.parseFloat(tokens[1]);
xCoordNm = convertToNanometres(parsedValue, metric);
parsedValue = Float.parseFloat(tokens[2]);
yCoordNm = convertToNanometres(parsedValue, metric);
parsedValue = Float.parseFloat(tokens[3]);
kicadMheightNm = convertToNanometres(parsedValue, metric);
parsedValue = Float.parseFloat(tokens[4]);
kicadMwidthNm = convertToNanometres(parsedValue, metric);
kicadRotation = Integer.parseInt(tokens[5]);
// tenths of degrees Angular
// rotation from horizontal, counterclockwise
parsedValue = Float.parseFloat(tokens[6]);
kicadLineThicknessNm = convertToNanometres(parsedValue, metric);
if (tokens[8].startsWith("I"))
{
textVisibility = false;
}
else if (tokens[8].startsWith("V"))
{
textVisibility = true;
}
textLayer = Integer.parseInt(tokens[9]);
String rawString = tokens[10];
// we only want what is inside double quotes
displayedTextField = rawString.substring(rawString.indexOf('"') + 1, rawString.lastIndexOf('"'));
}
else if (tokens[0].startsWith("fbtext"))
{ // s-files seem to have limited support for multiple text fields
for (int counter = 1; counter < tokens.length; counter++)
{
if (tokens[counter].startsWith("reference"))
{
displayedTextField = tokens[counter + 1];
counter++;
}
else if (tokens[counter].startsWith("value"))
{
displayedTextField = tokens[counter + 1];
counter++;
}
else if (tokens[counter].startsWith("at"))
{
counter++;
parsedValue = Float.parseFloat(tokens[counter]);
xCoordNm = convertToNanometres(parsedValue, metric);
counter++;
parsedValue = Float.parseFloat(tokens[counter]);
yCoordNm = convertToNanometres(parsedValue, metric);
}
else if (tokens[counter].startsWith("size"))
{
counter++;
parsedValue = Float.parseFloat(tokens[counter]);
kicadMheightNm = convertToNanometres(parsedValue, metric);
counter++;
parsedValue = Float.parseFloat(tokens[counter]);
kicadMwidthNm = convertToNanometres(parsedValue, metric);
}
else if (tokens[counter].startsWith("thickness"))
{
counter++;
parsedValue = Float.parseFloat(tokens[counter]);
kicadLineThicknessNm = convertToNanometres(parsedValue, metric);
}
else if (tokens[counter].startsWith("layer"))
{
counter++;
if (tokens[counter].startsWith("F.Silks"))
{
textLayer = kicadTopSilkLayer;
}
else if (tokens[counter].startsWith("F.Silks"))
{
textLayer = kicadBottomSilkLayer;
}
// we could add additional layer suport here if keen
}
else if (tokens[counter].startsWith("hide"))
{
textVisibility = false;
}
}
}
}
// public void populateElement(String moduleDefinition, boolean metric) {
// kicadTextDescriptor = moduleDefinition;
// for testing
// workingText = kicadTextDescriptor; //"Sample Text";
//}
private long convertToNanometres(float rawValue, boolean metricSystem)
{
if (metricSystem)// metric = the newer legacy format with mm instead of decimil = 0.1 mil units
{
return (long)(rawValue * 10000000);
}
else // this implies the input is in Kicad legacy decimil = 0.1mil units
{
return (long)(2540 * rawValue); // a 0.1 mil unit = 2540 nm
}
}
}