forked from lkesteloot/turbopascal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
606 lines (544 loc) · 24.7 KB
/
graph.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// The graphics sub-system.
if (typeof define !== 'function') { var define = require('amdefine')(module) };
define(["./Node"], function (Node) {
var $canvas;
var gCtx;
var gGraphResult = 0;
var grOk = 0;
var grNoInitGraph = -1;
var gPenX = 0;
var gPenY = 0;
var gTextFont = 0;
var gTextDirection = 0;
var gTextSize = 0;
var gTextHorizontalAlign = 0;
var gTextVerticalAlign = 0;
// Default EGA color palette.
// http://en.wikipedia.org/wiki/Enhanced_Graphics_Adapter#Color_palette
var COLORS = [
"#000000",
"#0000AA",
"#00AA00",
"#00AAAA",
"#AA0000",
"#AA00AA",
"#AA5500",
"#AAAAAA",
"#555555",
"#5555FF",
"#55FF55",
"#55FFFF",
"#FF5555",
"#FF55FF",
"#FFFF55",
"#FFFFFF"
];
// Like COLORS array but [R,G,B].
var RGB_COLORS = [];
// 24-bit int color to color map index.
var COLOR_TO_INDEX = {};
for (var i = 0; i < COLORS.length; i++) {
COLOR_TO_INDEX[parseInt(COLORS[i].slice(1), 16)] = i;
RGB_COLORS.push([
parseInt(COLORS[i].slice(1, 3), 16),
parseInt(COLORS[i].slice(3, 5), 16),
parseInt(COLORS[i].slice(5, 7), 16)
]);
}
var WIDTH = 640;
var HEIGHT = 350;
var gCurrentColor = 0;
var gBackgroundColor = 0;
// Flag to remember whether the next graphics command should clear the device.
var gMustClearDevice = false;
// Check and clear the device if necessary.
var checkClearDevice = function () {
if (gMustClearDevice) {
// Don't use clearRect(), since that sets the alpha to 0 and later
// anti-aliased operations set the alpha to something gray, which
// our Pascal code doesn't know about and can't handle.
gCtx.fillStyle = COLORS[gBackgroundColor];
gCtx.fillRect(0, 0, WIDTH, HEIGHT);
gMustClearDevice = false;
}
};
// Converts RGB from an RGB array to a color index.
var rgbToIndex = function (data, offset) {
// Convert to 24-bit integer.
var color = (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
// Map back to color map. Treat unknown as black.
return COLOR_TO_INDEX[color] || 0;
};
// Returns the color at pixel x, y.
var getPixel = function (x, y) {
// Get raw pixel value.
var data = gCtx.getImageData(x, y, 1, 1).data;
// Convert to color index.
return rgbToIndex(data, 0);
};
var importSymbols = function (symbolTable) {
var symbol;
// These types aren't documented anywhere I think.
var node = new Node(Node.RECORD_TYPE, null, {
fields: [
new Node(Node.FIELD, null, {
name: Node.makeIdentifierNode("X"),
type: Node.integerType,
offset: 0
}),
new Node(Node.FIELD, null, {
name: Node.makeIdentifierNode("Y"),
type: Node.integerType,
offset: 1
})
]
});
symbolTable.addType("PointType", node);
// Error codes.
symbolTable.addNativeConstant("grOk", grOk, Node.integerType);
symbolTable.addNativeConstant("grNoInitGraph", grNoInitGraph, Node.integerType);
// There are lots more of these errors. We can add them as we need them.
// Graphics drivers.
symbolTable.addNativeConstant("Detect", 0, Node.integerType);
// Colors.
symbolTable.addNativeConstant("Black", 0, Node.integerType);
symbolTable.addNativeConstant("Blue", 1, Node.integerType);
symbolTable.addNativeConstant("Green", 2, Node.integerType);
symbolTable.addNativeConstant("Cyan", 3, Node.integerType);
symbolTable.addNativeConstant("Red", 4, Node.integerType);
symbolTable.addNativeConstant("Magenta", 5, Node.integerType);
symbolTable.addNativeConstant("Brown", 6, Node.integerType);
symbolTable.addNativeConstant("LightGray", 7, Node.integerType);
symbolTable.addNativeConstant("DarkGray", 8, Node.integerType);
symbolTable.addNativeConstant("LightBlue", 9, Node.integerType);
symbolTable.addNativeConstant("LightGreen", 10, Node.integerType);
symbolTable.addNativeConstant("LightCyan", 11, Node.integerType);
symbolTable.addNativeConstant("LightRed", 12, Node.integerType);
symbolTable.addNativeConstant("LightMagenta", 13, Node.integerType);
symbolTable.addNativeConstant("Yellow", 14, Node.integerType);
symbolTable.addNativeConstant("White", 15, Node.integerType);
// Line styles.
symbolTable.addNativeConstant("SolidLn", 0, Node.integerType);
symbolTable.addNativeConstant("DottedLn", 1, Node.integerType);
symbolTable.addNativeConstant("CenterLn", 2, Node.integerType);
symbolTable.addNativeConstant("DashedLn", 3, Node.integerType);
symbolTable.addNativeConstant("UserBitLn", 4, Node.integerType);
// Line width.
symbolTable.addNativeConstant("NormWidth", 1, Node.integerType);
symbolTable.addNativeConstant("ThickWidth", 3, Node.integerType);
// Text style.
symbolTable.addNativeConstant("DefaultFont", 0, Node.integerType);
symbolTable.addNativeConstant("TriplexFont", 1, Node.integerType);
symbolTable.addNativeConstant("SmallFont", 2, Node.integerType);
symbolTable.addNativeConstant("SansSerifFont", 3, Node.integerType);
symbolTable.addNativeConstant("GothicFont", 4, Node.integerType);
// Direction
symbolTable.addNativeConstant("HorizDir", 0, Node.integerType);
symbolTable.addNativeConstant("VertDir", 1, Node.integerType);
// Character size.
symbolTable.addNativeConstant("UserCharSize", 0, Node.integerType);
// Clipping.
symbolTable.addNativeConstant("ClipOn", true, Node.booleanType);
symbolTable.addNativeConstant("ClipOff", false, Node.booleanType);
// Bar3D.
symbolTable.addNativeConstant("TopOn", true, Node.booleanType);
symbolTable.addNativeConstant("TopOff", false, Node.booleanType);
// Fill patterns.
symbolTable.addNativeConstant("EmptyFill", 0, Node.integerType);
symbolTable.addNativeConstant("SolidFill", 1, Node.integerType);
symbolTable.addNativeConstant("LineFill", 2, Node.integerType);
symbolTable.addNativeConstant("LtSlashFill", 3, Node.integerType);
symbolTable.addNativeConstant("SlashFill", 4, Node.integerType);
symbolTable.addNativeConstant("BkSlashFill", 5, Node.integerType);
symbolTable.addNativeConstant("LtBkSlashFill", 6, Node.integerType);
symbolTable.addNativeConstant("HatchFill", 7, Node.integerType);
symbolTable.addNativeConstant("XHatchFill", 8, Node.integerType);
symbolTable.addNativeConstant("InterleaveFill", 9, Node.integerType);
symbolTable.addNativeConstant("WideDotFill", 10, Node.integerType);
symbolTable.addNativeConstant("CloseDotFill", 11, Node.integerType);
symbolTable.addNativeConstant("UserFill", 12, Node.integerType);
// BitBlt.
var cNormalPut = 0;
var cXORPut = 1;
var cOrPut = 2;
var cAndPut = 3;
var cNotPut = 4;
symbolTable.addNativeConstant("NormalPut", cNormalPut, Node.integerType);
symbolTable.addNativeConstant("XORPut", cXORPut, Node.integerType);
symbolTable.addNativeConstant("OrPut", cOrPut, Node.integerType);
symbolTable.addNativeConstant("AndPut", cAndPut, Node.integerType);
symbolTable.addNativeConstant("NotPut", cNotPut, Node.integerType);
// Text alignment.
symbolTable.addNativeConstant("LeftText", 0, Node.integerType);
symbolTable.addNativeConstant("CenterText", 1, Node.integerType);
symbolTable.addNativeConstant("RightText", 2, Node.integerType);
symbolTable.addNativeConstant("BottomText", 0, Node.integerType);
symbolTable.addNativeConstant("TopText", 2, Node.integerType);
symbolTable.addNativeFunction("InitGraph", Node.voidType,
[Node.integerType, Node.integerType, Node.stringType],
function (ctl) {
// Initialize the graphics system.
$("#screen").hide();
$canvas = $("#canvas").show();
if ($canvas.length > 0 && $canvas[0].getContext) {
gCtx = $canvas[0].getContext("2d");
gGraphResult = grOk;
} else {
gGraphResult = grNoInitGraph;
}
gMustClearDevice = true;
// TODO Should write mode into the second parameter.
});
symbolTable.addNativeFunction("GraphResult", Node.integerType, [], function (ctl) {
// The result of a graphics call.
return gGraphResult;
});
symbolTable.addNativeFunction("SetColor", Node.voidType, [Node.integerType],
function (ctl, color) {
// Set the current drawing color.
gCurrentColor = color;
});
symbolTable.addNativeFunction("SetBkColor", Node.voidType, [Node.integerType],
function (ctl, color) {
// Set the background color.
gBackgroundColor = color;
});
symbolTable.addNativeFunction("SetFillStyle", Node.voidType,
[Node.integerType, Node.integerType],
function (ctl, fillStyle, color) {
// Set the current fill style.
if (fillStyle !== 1) { // SolidFill
console.log("SetFillStyle: Only SolidFill is supported");
}
gCurrentColor = color;
});
symbolTable.addNativeFunction("SetLineStyle", Node.voidType,
[Node.integerType, Node.integerType, Node.integerType],
function (ctl, lineStyle, pattern, thickness) {
// Not implemented.
});
symbolTable.addNativeFunction("GetMaxColor", Node.integerType, [], function (ctl) {
// Return the maximum color number.
return COLORS.length - 1;
});
symbolTable.addNativeFunction("GraphErrorMsg", Node.stringType, [Node.integerType],
function (ctl, errorCode) {
// Look this up in a table.
return "error message here!";
});
symbolTable.addNativeFunction("GetMaxX", Node.integerType, [], function (ctl) {
// Return the maximum X column.
return WIDTH - 1;
});
symbolTable.addNativeFunction("GetMaxY", Node.integerType, [], function (ctl) {
// Return the maximum Y row.
return HEIGHT - 1;
});
symbolTable.addNativeFunction("GetPixel", Node.integerType,
[Node.integerType, Node.integerType], function (ctl, x, y) {
checkClearDevice();
return getPixel(x, y);
});
symbolTable.addNativeFunction("PutPixel", Node.voidType,
[Node.integerType, Node.integerType, Node.integerType],
function (ctl, x, y, c) {
checkClearDevice();
if (x >= 0 && y >= 0 && x < WIDTH && y < HEIGHT) {
c = Math.floor(c);
if (c < 0) {
c = 0;
}
if (c > COLORS.length - 1) {
c = COLORS.length - 1;
}
gCtx.fillStyle = COLORS[c];
gCtx.fillRect(x, y, 1, 1);
}
});
symbolTable.addNativeFunction("FloodFill", Node.voidType,
[Node.integerType, Node.integerType, Node.integerType],
function (ctl, x, y, edgeColor) {
checkClearDevice();
// Flood fill with the current fill pattern and color.
var queue = [[x, y]];
var fillColor = gCurrentColor;
var fillColorRgb = RGB_COLORS[fillColor];
// Getting each individual pixel is too slow, so we get the whole screen,
// do the flood fill, and replace the whole thing.
var imageData = gCtx.getImageData(0, 0, WIDTH, HEIGHT);
var data = imageData.data;
while (queue.length > 0) {
var p = queue.pop();
var x = p[0];
var y = p[1];
if (x >= 0 && y >= 0 && x < WIDTH && y < HEIGHT) {
var offset = (x + y*WIDTH)*4;
var pixelColor = rgbToIndex(data, offset);
if (pixelColor != edgeColor && pixelColor != fillColor) {
// Draw the fill pixel.
data[offset + 0] = fillColorRgb[0];
data[offset + 1] = fillColorRgb[1];
data[offset + 2] = fillColorRgb[2];
// Push neighbors.
queue.push([x + 1, y]);
queue.push([x - 1, y]);
queue.push([x, y + 1]);
queue.push([x, y - 1]);
}
}
}
gCtx.putImageData(imageData, 0, 0);
});
symbolTable.addNativeFunction("Circle", Node.voidType,
[Node.integerType, Node.integerType, Node.integerType],
function (ctl, cx, cy, r) {
checkClearDevice();
// Draw a circle with the current line style and color.
if (false) {
// This circle is anti-aliased, which messed up flood fills.
gCtx.strokeStyle = COLORS[gCurrentColor];
gCtx.beginPath();
gCtx.arc(cx + 0.5, cy + 0.5, r, 0, Math.PI*2);
gCtx.stroke();
} else {
gCtx.fillStyle = COLORS[gCurrentColor];
// https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
var x = r;
var y = 0;
var radiusError = 1 - x;
while (x >= y) {
// Eight octants.
gCtx.fillRect(cx + x, cy + y, 1, 1);
gCtx.fillRect(cx + y, cy + x, 1, 1);
gCtx.fillRect(cx - x, cy + y, 1, 1);
gCtx.fillRect(cx - y, cy + x, 1, 1);
gCtx.fillRect(cx - x, cy - y, 1, 1);
gCtx.fillRect(cx - y, cy - x, 1, 1);
gCtx.fillRect(cx + x, cy - y, 1, 1);
gCtx.fillRect(cx + y, cy - x, 1, 1);
y++;
if (radiusError < 0) {
radiusError += 2*y + 1;
} else {
x--;
radiusError += 2*(y - x + 1);
}
}
}
});
symbolTable.addNativeFunction("Rectangle", Node.voidType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType],
function (ctl, x1, y1, x2, y2) {
checkClearDevice();
// Draw a 2D rectangle with the current line style and color.
gCtx.strokeStyle = COLORS[gCurrentColor];
gCtx.rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
});
symbolTable.addNativeFunction("Bar", Node.voidType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType],
function (ctl, x1, y1, x2, y2) {
checkClearDevice();
// Draw a 2D bar with the current fill style and color.
gCtx.fillStyle = COLORS[gCurrentColor];
if (x2 < x1) {
var t = x1;
x1 = x2;
x2 = t;
}
if (y2 < y1) {
var t = y1;
y1 = y2;
y2 = t;
}
gCtx.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
});
symbolTable.addNativeFunction("Line", Node.voidType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType],
function (ctl, x1, y1, x2, y2) {
// Draw a line.
checkClearDevice();
gCtx.strokeStyle = COLORS[gCurrentColor];
gCtx.beginPath();
gCtx.moveTo(x1, y1);
gCtx.lineTo(x2, y2);
gCtx.stroke();
});
symbolTable.addNativeFunction("MoveTo", Node.voidType,
[Node.integerType, Node.integerType],
function (ctl, x, y) {
// Move pen to location.
gPenX = x;
gPenY = y;
});
symbolTable.addNativeFunction("LineTo", Node.voidType,
[Node.integerType, Node.integerType],
function (ctl, x, y) {
// Draw a line.
checkClearDevice();
gCtx.strokeStyle = COLORS[gCurrentColor];
gCtx.beginPath();
gCtx.moveTo(gPenX, gPenY);
gPenX = x;
gPenY = y;
gCtx.lineTo(gPenX, gPenY);
gCtx.stroke();
});
symbolTable.addNativeFunction("SetBorderLimits", Node.voidType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType],
function (ctl, x1, y1, x2, y2) {
// This is not documented in the book. How did I find out about these?
// It's probably a clipping rectangle.
});
symbol = symbolTable.addNativeFunction("FillPoly", Node.voidType,
[Node.integerType, Node.voidType],
function (ctl, count, p) {
checkClearDevice();
// Fill a polygon. The second parameter is a generic pointer to
// an array of "count" records of type PointType.
gCtx.fillStyle = COLORS[gCurrentColor];
gCtx.beginPath();
for (var i = 0; i < count; i++) {
var x = ctl.readDstore(p + i*2);
var y = ctl.readDstore(p + i*2 + 1);
if (i === 0) {
gCtx.moveTo(x, y);
} else {
gCtx.lineTo(x, y);
}
}
gCtx.closePath();
gCtx.fill();
});
symbol.type.parameters[1].byReference = true;
symbolTable.addNativeFunction("ClearDevice", Node.voidType, [], function (ctl) {
// Here we want to let the screen have a chance to draw itself, so we don't
// want to clear it right away. Instead we remember that we should clear it,
// and tell the system to wait a bit for the screen to show.
ctl.delay(20);
gMustClearDevice = true;
});
symbolTable.addNativeFunction("CloseGraph", Node.voidType, [], function (ctl) {
$canvas.hide();
});
symbolTable.addNativeFunction("SetWriteMode", Node.voidType, [Node.integerType],
function (ctl) {
// This is not documented in the reference books.
});
// Image read/write functions.
symbolTable.addNativeFunction("ImageSize", Node.integerType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType],
function (ctl, x1, y1, x2, y2) {
var width = x2 - x1 + 1;
var height = y2 - y1 + 1;
// Two words for width/height.
return width*height + 2;
});
symbol = symbolTable.addNativeFunction("GetImage", Node.voidType,
[Node.integerType, Node.integerType,
Node.integerType, Node.integerType, Node.voidType],
function (ctl, x1, y1, x2, y2, bitmap) {
var width = x2 - x1 + 1;
var height = y2 - y1 + 1;
var data = gCtx.getImageData(x1, y1, width, height).data;
// Store width and height.
ctl.writeDstore(bitmap++, width);
ctl.writeDstore(bitmap++, height);
// Store all pixels.
for (var i = 0; i < width*height; i++) {
// The data we get is RGBA.
ctl.writeDstore(bitmap++, rgbToIndex(data, i*4));
}
});
symbol.type.parameters[4].byReference = true;
symbol = symbolTable.addNativeFunction("PutImage", Node.voidType,
[Node.integerType, Node.integerType,
Node.voidType, Node.integerType],
function (ctl, x, y, bitmap, bitblt) {
// Read width and height.
var width = ctl.readDstore(bitmap++);
var height = ctl.readDstore(bitmap++);
var imageData;
var data;
if (bitblt === cNormalPut || bitblt === cNotPut) {
// Create blank image.
imageData = gCtx.createImageData(width, height);
} else {
// Get existing data from screen.
imageData = gCtx.getImageData(x, y, width, height);
}
data = imageData.data;
// Load all pixels.
for (var i = 0; i < width*height; i++) {
var color = ctl.readDstore(bitmap++);
switch (bitblt) {
case cNormalPut:
// Nothing.
break;
case cXORPut:
// XOR with existing data.
color = (color ^ rgbToIndex(data, i*4)) & 0x0F;
break;
case cOrPut:
// Or with existing data.
color = (color | rgbToIndex(data, i*4)) & 0x0F;
break;
case cAndPut:
// And with existing data.
color = (color & rgbToIndex(data, i*4)) & 0x0F;
break;
case cNotPut:
// Invert source data.
color = ~color & 0x0F;
break;
}
var rgb = RGB_COLORS[color];
data[i*4 + 0] = rgb[0];
data[i*4 + 1] = rgb[1];
data[i*4 + 2] = rgb[2];
data[i*4 + 3] = 255;
}
gCtx.putImageData(imageData, x, y);
});
symbol.type.parameters[2].byReference = true;
// Text functions.
symbolTable.addNativeFunction("SetTextJustify", Node.voidType,
[Node.integerType, Node.integerType],
function (ctl, horizontal, vertical) {
gTextHorizontalAlign = horizontal;
gTextVerticalAlign = vertical;
});
symbolTable.addNativeFunction("SetTextStyle", Node.voidType,
[Node.integerType, Node.integerType, Node.integerType],
function (ctl, font, direction, charSize) {
gTextFont = font;
gTextDirection = direction;
gTextSize = charSize;
});
symbolTable.addNativeFunction("OutTextXY", Node.voidType,
[Node.integerType, Node.integerType, Node.stringType],
function (ctl, x, y, text) {
gCtx.fillStyle = COLORS[gCurrentColor];
var metrics = gCtx.measureText(text);
gCtx.font = (gTextSize*8) + "px sans-serif";
switch (gTextHorizontalAlign) {
case 0: gCtx.textAlign = "left"; break;
case 1: gCtx.textAlign = "center"; break;
case 2: gCtx.textAlign = "right"; break;
}
switch (gTextVerticalAlign) {
case 0: gCtx.textBaseline = "bottom"; break;
case 1: gCtx.textBaseline = "middle"; break;
case 2: gCtx.textBaseline = "top"; break;
}
gCtx.fillText(text, x, y);
});
};
return {
importSymbols: importSymbols
};
});