-
Notifications
You must be signed in to change notification settings - Fork 0
/
borland.js
411 lines (351 loc) · 10.4 KB
/
borland.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
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
function Font() {
this.chars = [];
this.index = 0;
this.header_size = 0;
this.font_name = '';
this.font_size = 0;
this.font_major = 0;
this.font_minor = 0;
this.bgi_major = 0;
this.bgi_minor = 0;
this.sig = 0;
this.nchrs = 0;
this.unused1 = '';
this.firstch = 0;
this.cdefs = 0;
this.scan_flag = 0;
this.org_to_cap = 0;
this.org_to_base = 0;
this.org_to_dec = 0;
this.unused2 = '';
this.x_max = 0;
this.x_min = 0;
this.y_max = 0;
this.y_min = 0;
}
Font.prototype = {
getLength: function() {
return this.chars.length;
},
getIndex: function() {
return this.index;
},
getChar: function() {
return this.chars[ this.index ];
},
getCharByIndex: function(c) {
return this.chars[ c ];
},
getCharByName: function(name) {
var cl = this.getLength();
for (var c=0; c<cl; c++) {
var theChar = this.getCharByIndex(c);
if ( theChar.getName() == name ) {
return theChar;
}
}
},
getCharByAscii: function() {
},
increment: function(amt) {
this.index += amt;
}
}
function Char() {
this.char_name = null;
this.ascii_value = null;
this.cmds = [];
this.index = 0;
this.defined_width = 0;
this.x_max = 0;
this.x_min = 0;
this.y_max = 0;
this.y_min = 0;
}
Char.prototype = {
getLength: function() {
return this.cmds.length;
},
getWidth: function() {
return this.defined_width;
},
getIndex: function() {
return this.index;
},
getAscii: function() {
return this.ascii_value;
},
getName: function() {
return this.char_name;
},
getCmdByIndex: function(c) {
return this.cmds[ c ];
},
getCmd: function() {
return this.cmds[ this.index ];
},
increment: function(amt) {
this.index += amt;
}
}
function Cmd() {
this.x = null;
this.y = null;
this.cmd = null;
}
Cmd.prototype = {
getX: function() {
return this.x;
},
getY: function() {
return this.y;
},
getCmd: function() {
return this.cmd;
}
}
function createContext(width, height, id ) {
var canvas = document.createElement('canvas');
canvas.setAttribute('id', id);
document.body.appendChild(canvas);
canvas.width = width;
canvas.height = height;
canvas.style.margin = '5px';
var context = canvas.getContext('2d');
// This transform accounts for negative Y-coordinates.
// But I can also recalculate the coordinates myself in the preview function
//context.transform(1, 0, 0, -1, 0, canvas.height)
//context = canvas.getContext('2d');
context.fillStyle = 'rgb(200,200,200)';
context.rect( 0, 0, width, height );
context.fill();
return context;
}
function assert(condition, message) {
message = message || "Assertion failed";
if (!condition) {
alert(message);
throw message;
}
}
function decToBinStr( byte1 ) {
var bits1 = Number(byte1).toString(2);
bits1 = '00000000'.substr(bits1.length) + bits1;
return bits1;
}
// I'm sure there's a better way to convert one's complement binary strings
// into decimal numbers than what I'm doing below. But it's working for now.
// Adapted from: Code Golf, http://codegolf.stackexchange.com/a/30371
function signedBinToDec(bits1) {
// First bit signifies positive or negative
var sign = bits1[0];
// Remaining bits are the number
var val = bits1.slice(1);
// If negative, then need to flip all the bits to get one's complement
if (sign == '1') { val = val.replace(/./g,x=>x^1); }
// Convert binary string to decimal
var num = parseInt(val,2);
// If it should be negative, make negative
if (sign == '1') { num *= -1 }
return num;
}
// JoshReader obj allows us to seek over the arrayBuffer
function JoshReader(arrayBuffer)
{
assert(arrayBuffer instanceof ArrayBuffer);
this.pos = 0;
this.data = new DataView(arrayBuffer);
}
JoshReader.prototype = {
seek: function(pos) {
assert(pos >=0 && pos <= this.data.byteLength);
var oldPos = this.pos;
this.pos = pos;
return oldPos;
},
tell: function() {
return this.pos;
},
getUint8: function() {
assert(this.pos < this.data.byteLength);
return this.data.getUint8(this.pos++);
},
getUint16: function() {
assert(this.pos < this.data.byteLength);
var ret = this.data.getUint16(this.pos,true);
this.pos += 2;
return ret;
},
getUint32: function() {
assert(this.pos < this.data.byteLength);
var ret = this.data.getUint32(this.pos,true);
this.pos += 4;
return ret;
},
getInt16: function() {
assert(this.pos < this.data.byteLength);
var ret = this.data.getInt16(this.pos,true);
this.pos += 2;
return ret;
},
getInt32: function() {
assert(this.pos < this.data.byteLength);
var ret = this.data.getInt32(this.pos,true);
this.pos += 4;
return ret;
},
getFword: function() {
return this.getInt16();
},
get2Dot14: function() {
return this.getInt16() / (1 << 14);
},
getFixed: function() {
return this.getInt32() / (1 << 16);
},
getString: function(length) {
var result = "";
for(var i = 0; i < length; i++) {
result += String.fromCharCode(this.getUint8());
}
return result;
}
};
function convertFont(arrayBuffer) {
// INFORMATION ON PARSING BORLAND CHR FONTS CAME FROM:
// * The file `fdv_bgi.h` in the GRX graphics library
// * The file `main.c` in the chrcvt package in the cc65 C compiler
// - https://github.com/SvenMichaelKlose/cc65g/blob/master/src/chrcvt/main.c
// * FileFormat.info:
// - http://www.fileformat.info/format/borland-chr/corion.htm
// * Post in Delphi Groups:
// - http://www.delphigroups.info/2/03/13720.html
var f = new JoshReader(arrayBuffer);
var theFont = new Font();
// This is the ASCII copyright message
var ch, msg;
while ( ch != 26 ) {
ch = f.getUint8();
msg += String.fromCharCode(ch);
}
theFont.header_size = f.getUint16();
theFont.font_name = f.getString(4);
theFont.font_size = f.getUint16();
theFont.font_major = f.getUint8();
theFont.font_minor = f.getUint8();
theFont.bgi_major = f.getUint8();
theFont.bgi_minor = f.getUint8();
// Seek to the theFont header (usually at position 128) and
// make sure it contains the signature byte: ascii 43 or '+'
f.seek( theFont.header_size );
theFont.sig = f.getUint8();
theFont.nchrs = f.getUint16();
theFont.unused1 = f.getString(1);
theFont.firstch = f.getUint8();
theFont.cdefs = f.getUint16();
theFont.scan_flag = f.getUint8(); // All these theFonts have scan_flag set to 0
theFont.org_to_cap = f.getUint8();
theFont.org_to_base = f.getUint8();
theFont.org_to_dec = signedBinToDec( decToBinStr( f.getUint8() ) );
theFont.unused2 = f.getString(5);
// TABLES
// The Delphi Groups post indicates these tables are tied to 96h.
// But that is definitely wrong. The Borland doc and FileFormat info say 90h.
// 90h Table of offsets to individual character definitions (one word each)
// 90h+2n Table of character widths (one byte each)
// 90h+3n Start of character definitions
// NOTES: h means hex. n = numchar. 90h = 144. 96h = 150.
// Also, (90h + 3*numchars) usually equals (cdefs + header_size).
// Compile list of offsets to individual character definitions
var offsets = []
for (var n=0; n<theFont.nchrs; n++) {
offsets.push( f.getUint16() );
}
// Compile list of individual character widths
var widths = []
for (var n=0; n<theFont.nchrs; n++) {
widths.push( f.getUint8() );
}
// CHARACTER DEFINITIONS
// Character definitions consist of a series of words. Each word contains
// a two-bit opcode and an (x,y) coordinate:
// Byte 1 7 6 5 4 3 2 1 0 bit #
// op1 <seven bit signed X coord>
//
// Byte 2 7 6 5 4 3 2 1 0 bit #
// op2 <seven bit signed Y coord>
//
// Opcodes:
// op1=0 op2=0 End of character definition.
// op1=1 op2=0 Move the pointer to (x,y)
// op1=1 op2=1 Draw from current pointer to (x,y)
// op1=0 op2=1 Scan. (I haven't been able to figure out what this is for)
// After experimenting, I found the seven-bit signed integer is signed
// using ONE'S COMPLEMENT form -- NOT two's complement.
// As noted above, character definitions begin at 90h + 3*n. 90h = 144.
var chardefs = 3*theFont.nchrs + 144;
var fontXVals = [];
var fontYVals = [];
for (var n=0; n<theFont.nchrs; n++) {
var theChar = new Char();
theChar.defined_width = widths[n];
theChar.ascii_value = n + theFont.firstch;
theChar.char_name = String.fromCharCode( n + theFont.firstch );
var charXVals = [];
var charYVals = [];
// Seek to the position where this character begins
f.seek( chardefs + offsets[n] );
// Create a variable to keep track of number of bytes in this definition
var b=0;
// Keep iterating over bytes until we reach the next character definition.
// Each iteration will grab TWO bytes to make one word.
while ( (b*2)+offsets[n] < offsets[n+1] ) {
var theCmd = new Cmd();
// These values are SIGNED 7-bit integers, so need to convert them.
// Convert the bytes into binary strings
bits1 = decToBinStr( f.getUint8() );
bits2 = decToBinStr( f.getUint8() );
// Last 7 bits are the signed int. Convert back to decimal.
var x = signedBinToDec( bits1.slice(1) );
var y = signedBinToDec( bits2.slice(1) );
// PARSE OPCODE BITS
if ( bits1[0] == 0 && bits2[0] == 0 ) {
theCmd.cmd = 'END';
}
else if ( bits1[0] == 1 && bits2[0] == 0 ) {
theCmd.cmd = 'MOVE';
theCmd.x = x;
theCmd.y = y;
}
else if ( bits1[0] == 0 && bits2[0] == 1 ) {
theCmd.cmd = 'SCAN';
theCmd.x = x;
theCmd.y = y;
}
else if ( bits1[0] == 1 && bits2[0] == 1 ) {
theCmd.cmd = 'DRAW';
theCmd.x = x;
theCmd.y = y;
}
// Track X and Y values for font
if (fontXVals.indexOf(x) === -1) { fontXVals.push(x); }
if (fontYVals.indexOf(y) === -1) { fontYVals.push(y); }
if (charXVals.indexOf(x) === -1) { charXVals.push(x); }
if (charYVals.indexOf(y) === -1) { charYVals.push(y); }
// Add command to Char obj
theChar.cmds.push( theCmd );
// Increment the byte counter
b++;
} // end while
theChar.x_max = Math.max.apply(null, charXVals);
theChar.x_min = Math.min.apply(null, charXVals);
theChar.y_max = Math.max.apply(null, charYVals);
theChar.y_min = Math.min.apply(null, charYVals);
theFont.chars.push( theChar );
} // end for
theFont.x_max = Math.max.apply(null, fontXVals);
theFont.x_min = Math.min.apply(null, fontXVals);
theFont.y_max = Math.max.apply(null, fontYVals);
theFont.y_min = Math.min.apply(null, fontYVals);
return theFont;
}