-
Notifications
You must be signed in to change notification settings - Fork 9
/
ic-label-creator.js
277 lines (231 loc) · 7.01 KB
/
ic-label-creator.js
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
/**
* (C) Klemens Ullmann-Marx / www.ull.at
* License: GPLv3
*/
/*
* Draw a chip
*
* Custom defaults can be set in your chip collection:
* var globals = {
* ...
* defaultChipLogicFamily : 'LS',
* defaultChipSeries : '74',
* };
*
* @param (string) chipName The chipname as defined in chips.js
* @param (string) family optional, the logic family, default=LS (e.g LS, HC, ...)
* @param (string) series optional, the series, default=74 (e.g. 74, 54)
*/
function drawChip(chipName, series, type) {
console.log('=== ', chipName, type, series);
var chip = chips[chipName];
if (chip === undefined) {
alert('Error: unknown chip "' + chipName + '". Please check spelling or add pinout to chips.js.');
return;
}
var numPins = Object.keys(chip.pins).length;
var chipWidth = numPins / 2 * globals.pinDistance + 1;
var chipHeightPins = ('heightPins' in chip) ? chip.heightPins : 3;
var chipHeight = chipHeightPins * globals.pinDistance;
var $page = $('#page');
// CREATE BASE SVG ELEMENT FOR CHIP
// jQuery won't render svg elements without namespace url
var svgChip = $(document.createElementNS("http://www.w3.org/2000/svg", 'svg')).attr({
width: chipWidth + 'mm',
height: chipHeight + 'mm',
x: globals.chipPositionX + 'mm',
y: globals.chipPositionY + 'mm',
});
// DRAW CHIP OUTLINE
svgChip.append($(document.createElementNS("http://www.w3.org/2000/svg", 'rect')).attr({
x : globals.svgStrokeOffset + 'mm',
y : globals.svgStrokeOffset + 'mm',
width: chipWidth - globals.svgStrokeOffset + 'mm',
height: chipHeight - globals.svgStrokeOffset + 'mm',
stroke: 'silver',
'stroke-width': globals.svgStrokeWidth + 'mm',
fill: 'white'
}));
// DRAW HALFCIRCLE MARKER
svgChip.append($(document.createElementNS("http://www.w3.org/2000/svg", 'circle')).attr({
cx : 0,
cy : '50%',
r : '1.2mm',
fill: 'grey'
}));
// DRAW CHIP MODEL + DESCRIPTION
tweakedChipName = tweakChipName(chipName, series, type);
svgChip.append($(document.createElementNS("http://www.w3.org/2000/svg", 'text'))
.html(' ' + tweakedChipName + ' ' + chip.description)
.attr({
x : '50%',
y : chipHeight / 2 + .2 + 'mm',
'dominant-baseline': 'middle',
'text-anchor': 'middle',
'font-family' : 'sans-serif',
'font-size' : '1.6mm',
'font-weight' : 'bold',
fill: chipColor(chip.type),
})
);
// DRAW PINS
var x = globals.pinDistance / 2 + .5;
jQuery.each(chip.pins, function (pinNum, pinName) {
// bottom side
if (pinNum <= numPins / 2) {
drawPin({
x: x,
chipHeight: chipHeight,
chipHeightPins: chipHeightPins,
svgChip: svgChip,
side: 'bottom',
pinName: pinName,
});
x += globals.pinDistance;
// top side
} else {
x = x - globals.pinDistance;
drawPin({
x: x,
chipHeight: chipHeight,
chipHeightPins: chipHeightPins,
svgChip: svgChip,
side: 'top',
pinName: pinName,
});
}
}); // end of for each pin
// DRAW CURRENT CHIP TO page
$('#page').append(svgChip);
// UPDATE POSITION FOR NEXT CHIP
globals.chipPositionY += chipHeight + 4;
// Begin new column
if (globals.chipPositionY + 10 > globals.pageHeight) {
globals.chipPositionY = 0;
globals.chipPositionX += 50;
}
}
/*
* Allow to tweak series (54, 74) and logic family (LS, HC, HCT, ...)
*
* Custom defaults can be set in your chip collection:
* var globals = {
* ...
* defaultChipLogicFamily : 'LS',
* defaultChipSeries : '74',
* };
*
* @param (string) chipName The chipname as defined in chips.js
* @param (string) family optional, the logic family, default=LS (e.g LS, HC, ...)
* @param (string) series optional, the series, default=74 (e.g. 74, 54)
*/
function tweakChipName(chipName, family, series) {
if (family === undefined) {
if (typeof globals.defaultChipLogicFamily === 'undefined') {
family = 'LS';
} else {
family = globals.defaultChipLogicFamily;
}
}
if (series === undefined) {
if (typeof globals.defaultChipSeries === 'undefined') {
series = '74';
} else {
series = globals.defaultChipSeries;
}
}
chipName = chipName.replace(/^74LS/, series + family);
return chipName;
}
/**
* Draw a single pin on an IC.
*/
function drawPin(pinData) {
if (pinData.pinName.startsWith('/')) {
pinData.activeLow = true;
pinData.pinName = pinData.pinName.substring(1)
}
if (pinData.side === 'bottom') {
pinData.svgChip.append($(document.createElementNS("http://www.w3.org/2000/svg", 'text'))
.html(pinData.pinName)
.attr({
x: 0,
y: 0,
'text-decoration': pinData.activeLow ? 'overline' : '',
'dominant-baseline': 'baseline',
'text-anchor': 'start',
'font-family': 'sans-serif',
'font-size': fontSize(pinData.pinName, pinData.chipHeightPins),
style: 'transform: rotate(270deg) translate(-' + (pinData.chipHeight - .2) + 'mm, ' + (pinData.x + .6) + 'mm);',
})
);
} else {
pinData.svgChip.append($(document.createElementNS("http://www.w3.org/2000/svg", 'text'))
.html(pinData.pinName)
.attr({
x: 0,
y: 0,
'text-decoration': pinData.activeLow ? 'overline' : '',
'dominant-baseline': 'baseline',
'text-anchor': 'end',
'font-family': 'sans-serif',
'font-size': fontSize(pinData.pinName, pinData.chipHeightPins),
style: 'transform: rotate(270deg) translate(-.3mm, ' + (pinData.x + .7) + 'mm);',
})
);
}
}
/**
* Calculate font size according to pinName string length
*/
function fontSize(pinName, chipHeightPins) {
// We don't need font scaling for larger chips
if (chipHeightPins > 3) {
return '1.6mm';
}
// Do some handstands for multibyte chars
var length = countPinNameChars(pinName);
if (length <=2) {
return '1.6mm';
}
if (length <=3) {
return '1.4mm';
}
return '1.1mm';
}
/**
* Count string length without overline multibytes
*/
function countPinNameChars(pinName) {
// Escape multibyte chars (and others)
var uriEncoded = encodeURIComponent(pinName)
// Remove overline multibyte chars
var overlineReplaced = uriEncoded.split('%CC%85').join('');
// Re-decode for other encoded chars like "+", "⏚"
var uriDecoded = decodeURIComponent(overlineReplaced);
return uriDecoded.length;
}
/**
* Color chip titel
*/
function chipColor(type) {
if (globals.gimmeColor == false) {
return 'black';
}
if (['ram', 'sram', 'eeprom', 'register', 'flipflop'].includes(type)) {
return 'red';
}
if (['gate'].includes(type)) {
return 'blue';
}
if (['mux', 'demux', 'via'].includes(type)) {
return 'green';
}
if (['counter'].includes(type)) {
return 'magenta';
}
if (['cpu'].includes(type)) {
return 'darkorange';
}
return 'black';
}