Sahli/16col/imgtxtmode.js
2014-08-05 10:49:34 +03:00

143 lines
4.9 KiB
JavaScript

// Generated by CoffeeScript 1.7.1
(function() {
var __hasProp = {}.hasOwnProperty;
this.VERSION = '0.01';
({
constructor: function(options) {
var k, v, _results;
this.screen = [];
this.palette = new ImageTextModePaletteVGA();
this.font = new ImageTextModeFont8x16();
_results = [];
for (k in options) {
if (!__hasProp.call(options, k)) continue;
v = options[k];
_results.push(this[k] = v);
}
return _results;
},
parseUrl: function(url) {
var content, req;
req = new XMLHttpRequest();
req.open('GET', url, false);
req.overrideMimeType('text/plain; charset=x-user-defined');
req.send(null);
content = req.status === 200 || req.status === 0 ? req.responseText : '';
return this.parse(content);
},
unpackShort: function(data) {
var shrt;
shrt = (this.getByteAt(data, 1) << 8) + this.getByteAt(data, 0);
if (shrt < 0) {
shrt += 65536;
}
return shrt;
},
unpackLong: function(data) {
var lng;
lng = (((this.getByteAt(data, 0) << 8) + this.getByteAt(data, 1) << 8) + this.getByteAt(data, 2) << 8) + this.getByteAt(data, 3);
if (lng < 0) {
lng += 4294967296;
}
return lng;
},
getByteAt: function(data, offset) {
return data.charCodeAt(offset) & 0xFF;
},
getHeight: function() {
return this.screen.length;
},
parsePaletteData: function(data) {
var b, colors, g, i, r, _i;
colors = [];
for (i = _i = 0; _i <= 45; i = _i += 3) {
r = this.getByteAt(data, i);
r = r << 2 | r >> 4;
g = this.getByteAt(data, i + 1);
g = g << 2 | g >> 4;
b = this.getByteAt(data, i + 2);
b = b << 2 | b >> 4;
colors.push([r, g, b]);
}
return this.palette = new ImageTextModePalette({
colors: colors
});
},
parseFontData: function(data, height) {
var chars, chr, i, j, _i, _j, _ref;
if (height == null) {
height = 16;
}
chars = [];
for (i = _i = 0, _ref = data.length / height; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
chr = [];
for (j = _j = 0; 0 <= height ? _j < height : _j > height; j = 0 <= height ? ++_j : --_j) {
chr.push(this.getByteAt(data, i * height + j));
}
chars.push(chr);
}
return this.font = new ImageTextModeFont({
chars: chars,
height: height
});
},
renderCanvas: function(canvasElem) {
var bg, canvas, chr, ctx, curfillstyle, cx, cy, fg, h, i, j, line, pixel, px, py, w, _i, _j, _k, _l, _len, _ref, _ref1, _ref2;
w = this.getWidth() * this.font.width;
h = this.getHeight() * this.font.height;
canvas = document.createElement('canvas');
canvas.setAttribute('width', w);
canvas.setAttribute('height', h);
ctx = canvas.getContext('2d');
for (cy = _i = 0, _ref = this.screen.length; 0 <= _ref ? _i < _ref : _i > _ref; cy = 0 <= _ref ? ++_i : --_i) {
if (this.screen[cy] != null) {
for (cx = _j = 0, _ref1 = this.screen[cy].length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; cx = 0 <= _ref1 ? ++_j : --_j) {
pixel = this.screen[cy][cx];
curfillstyle = null;
if (pixel != null) {
if (pixel.attr != null) {
fg = pixel.attr & 15;
bg = (pixel.attr & 240) >> 4;
} else {
fg = pixel.fg;
bg = pixel.bg;
}
px = cx * this.font.width;
py = cy * this.font.height;
ctx.fillStyle = this.palette.toRgbaString(this.palette.colors[bg]);
ctx.fillRect(px, py, this.font.width, this.font.height);
ctx.fillStyle = this.palette.toRgbaString(this.palette.colors[fg]);
chr = this.font.chars[pixel.ch.charCodeAt(0) & 0xff];
i = 0;
for (_k = 0, _len = chr.length; _k < _len; _k++) {
line = chr[_k];
for (j = _l = 0, _ref2 = this.font.width; 0 <= _ref2 ? _l < _ref2 : _l > _ref2; j = 0 <= _ref2 ? ++_l : --_l) {
if (line & (1 << this.font.width - 1 - j)) {
ctx.fillRect(px + j, py + i, 1, 1);
}
}
i += 1;
}
}
}
}
}
canvasElem.setAttribute('width', w);
canvasElem.setAttribute('height', h);
ctx = canvasElem.getContext('2d');
return ctx.drawImage(canvas, 0, 0);
},
toBinaryArray: function(str) {
var buf, bufView, i, _i, _ref;
buf = new ArrayBuffer(str.length * 2);
bufView = new Uint8Array(buf);
for (i = _i = 0, _ref = str.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
});
}).call(this);