diff --git a/index.html b/index.html index 6a8efb6..b132d0d 100644 --- a/index.html +++ b/index.html @@ -126,17 +126,14 @@ // lex.opacity = 1 - - - diff --git a/js/clipboard.js b/js/clipboard.js index f636af9..ef89fb2 100644 --- a/js/clipboard.js +++ b/js/clipboard.js @@ -100,7 +100,70 @@ var clipboard = (function () { if (!data.match(/\x03/)) return exports.import_text(); - var json = colorcode.to_json(data, {fg:0, bg:1}) + var to_json = function(string, opts){ + var lines_in = string.split(/\r?\n/) + var lines_out = [] + var w = 0, h = 0 + for (var y = 0; y < lines_in.length; y++) { + var bg = 1, fg = 15 + var cells = [], line = lines_in[y] + if (line.length === 0) { + continue + } else { + for (var x = 0; x < line.length; x++) { + switch (line[x]) { + case "\x02": // ^B (unimplemented) + break + case "\x03": // ^C + var parseColour = function(line, x) { + if (/1[0-5]/.test(line.substr(x, 2))) { + colour = parseInt(line.substr(x, 2)) + return [colour, x + 2] + } else if (/0[0-9]/.test(line.substr(x, 2))) { + colour = parseInt(line.substr(x, 2)) + return [colour, x + 2] + } else if (/[0-9]/.test(line.substr(x, 1))) { + colour = parseInt(line.substr(x, 1)) + return [colour, x + 1] + } else { + return [undefined, x] + } + } + var bg_ = undefined, fg_ = undefined, x_ = x + 1; + [fg_, x_] = parseColour(line, x_) + if (line[x_] === ",") { + [bg_, x_] = parseColour(line, x_ + 1) + } + if ((bg_ == undefined) && (fg_ == undefined)) { + [bg, fg] = [1, 15] + } else { + bg = (bg_ != undefined) ? bg_ : bg; + fg = (fg_ != undefined) ? fg_ : fg; + }; + if (x_ != x) {x = x_ - 1}; break; + case "\x06": // ^F (unimplemented) + break + case "\x0f": // ^O + [bg, fg] = [1, 15]; break; + case "\x16": // ^V + [bg, fg] = [fg, bg]; break; + case "\x1f": // ^_ (unimplemented) + break + default: + cells.push({bg: bg, fg: bg, value: line[x]}) + } + } + if (cells.length > 0) { + if (w < cells.length) { + w = cells.length + } + lines_out.push(cells); h++; + } + } + } + return {h: h, lines: lines_out, w: w} + } + var json = to_json(data, {fg:0, bg:1}) if (!no_undo) undo.new() if (!no_undo) undo.save_rect(0,0, canvas.w, canvas.h) @@ -114,7 +177,7 @@ var clipboard = (function () { var row = canvas.aa[y] for (var x = 0, char; char = line[x]; x++){ var lex = row[x] - lex.char = String.fromCharCode(char.value) + lex.char = char.value lex.fg = char.fg lex.bg = char.bg lex.opacity = 1 @@ -211,9 +274,11 @@ var clipboard = (function () { var c = canvas.rotated ? clipboard.rotate_canvas() : clipboard.canvas if (done_fn) done_fn(c) } + var to_canvas = function(fn, opts){ + } var start = Date.now(); - colorcode.to_canvas(canvas.mirc(), opts) + to_canvas(canvas.mirc(), opts) var total = Date.now() - start; console.log("took " + total) }, diff --git a/js/ext/colorcode.js b/js/ext/colorcode.js deleted file mode 100644 index 1d035d3..0000000 --- a/js/ext/colorcode.js +++ /dev/null @@ -1,551 +0,0 @@ -!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.colorcode=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o cp437 function by sheetjs -// edited from https://github.com/SheetJS/js-codepage/blob/master/bits/437.js -var cp437 = (function(){ var d = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ", D = [], e = {}; for(var i=0;i!=d.length;++i) { if(d.charCodeAt(i) !== 0xFFFD) e[d[i]] = i; D[i] = d.charAt(i); } return {"enc": e, "dec": D }; })(); - -var render_char = function(font, char_value, ctx, ctx_x, ctx_y){ - char_value = cp437.enc[String.fromCharCode(char_value)] | 0; - var sheet_x = (char_value % font.sheet_w_in_chars) * font.char_w - var sheet_y = ((char_value / font.sheet_w_in_chars) |0) * font.char_h - ctx.drawImage(font.sheet, - sheet_x|0, sheet_y|0, font.char_w, font.char_h, - ctx_x|0, ctx_y|0, font.char_w, font.char_h) - -} - -for (var i=0, wh; wh=cp437s[i]; i++){ - var font = {}; - font.is_char_blank = require('../fontutil').is_char_blank; - font.render_char = render_char; - font.name = 'cp437_' + wh[0] + 'x' + wh[1]; - font.sheet_url = './img/' + font.name + '.png' - font.sheet_w_in_chars = 16; - font.char_w = wh[0] - font.char_h = wh[1] - fonts[font.name] = font; -} - - - -// window.cp437 = cp437; - -},{"../fontutil":7}],6:[function(require,module,exports){ -var fsexps = [[8,16,0],[8,15,1],[8,8,5]] -var fonts = {}; -module.exports = fonts; - -var render_char = function(font, char_value, ctx, ctx_x, ctx_y, char){ - var sheet_x = 0, sheet_y = 3; - if (char_value >= 0x20 && char_value <= 0x7e){ // ascii - sheet_x = (char_value - 0x20) * font.char_w_sheet - if (char.i){ // italic - sheet_y = 1 * font.char_h_sheet + 3 - } - } else if (char_value >= 0x80 && char_value <= 0xff){ // latin-1 - sheet_x = (char_value - 0x80) * font.char_w_sheet; - sheet_y = 2 * font.char_h_sheet + 3 - } else if (char_value >= 0x0100 && char_value <= 0x017f){ // latin a - sheet_x = (char_value - 0x0100) * font.char_w_sheet; - sheet_y = 3 * font.char_h_sheet + 3 - } else if (char_value >= 0x0180 && char_value <= 0x024f){ // latin b - sheet_x = (char_value - 0x0180) * font.char_w_sheet; - sheet_y = 4 * font.char_h_sheet + 3 - } else if (char_value >= 0x2500 && char_value <= 0x25ff){ // geom - sheet_x = (char_value - 0x2500) * font.char_w_sheet; - sheet_y = 5 * font.char_h_sheet + 3 - } else if (char_value >= 0x2600 && char_value <= 0x26ff){ // emoji - sheet_x = (char_value - 0x2600) * font.char_w_sheet; - sheet_y = 6 * font.char_h_sheet + 3 - } - - // var sheet_x = (char_value % font.sheet_w_in_chars) * font.char_w - // var sheet_y = ((char_value / font.sheet_w_in_chars) |0) * font.char_h + 3 - ctx.drawImage(font.sheet, - sheet_x|0, (sheet_y|0) + font.y_adj, font.char_w, font.char_h, - ctx_x|0, ctx_y|0, font.char_w, font.char_h) - -} - -for (var i=0, wh; wh=fsexps[i]; i++){ - var font = { - name: 'fixedsys_' + wh[0] + 'x' + wh[1], - sheet_url: './img/fsex-simple.png', - sheet_w_in_chars: 128, - char_w_sheet: 8, - char_h_sheet: 16, - char_w: wh[0], - char_h: wh[1], - y_adj: wh[2], - is_char_blank: require('../fontutil').is_char_blank, - render_char: render_char - } - fonts[font.name] = font -} -},{"../fontutil":7}],7:[function(require,module,exports){ -var util = {}; -module.exports = util; - -util.is_char_blank = function(char_value){ - if (char_value === 32) return true; -} - -util.render_char = function(font, char_value, ctx, ctx_x, ctx_y){ - var sheet_x = (char_value % font.sheet_w_in_chars) * font.char_w - var sheet_y = ((char_value / font.sheet_w_in_chars) |0) * font.char_h - ctx.drawImage(font.sheet, - sheet_x|0, sheet_y|0, font.char_w, font.char_h, - ctx_x|0, ctx_y|0, font.char_w, font.char_h) - -} - - -},{}],8:[function(require,module,exports){ -var char_color = '\x03'; - -var make_colorcode_fgbg = function(fg, bg){ - // pad numbers: this prevents irc parsing confusion - // when the character after the colorcode is a number - if (fg < 10) fg = "0" + fg; - if (bg < 10) bg = "0" + bg; - return char_color + fg + "," + bg -} - -var colorcode_from_json = function(json, opts){ - var out = ""; - for (var li=0, line; line=json.lines[li]; li++){ - for (var ci=0, char; char=line[ci]; ci++){ - out += make_colorcode_fgbg(char.fg, char.bg) - out += String.fromCharCode(char.value) - } - out += "\n"; - } - return out; -} - - -module.exports = colorcode_from_json; - -},{}],9:[function(require,module,exports){ -// default settings for fonts, colors, etc -var style = {}; -module.exports = style; - -},{}],10:[function(require,module,exports){ -var char_color = '\x03'; -var regexp_color = /(^[\d]{1,2})?(?:,([\d]{1,2}))?/; - -var style_chars = { - '\x02': 'bold', - '\x1d': 'italic', - '\x1f': 'underline', - '\x0f': 'reset', - '\x16': 'inverse' -}; - -var Style = function(style){ - this.b = style.b; - this.i = style.i; - this.u = style.u; - this.fg = style.fg; - this.bg = style.bg; -}; - -var style_fns = {}; - -style_fns.bold = function(style){ style.b = !style.b }; - -style_fns.italic = function(style){ style.i = !style.i }; - -style_fns.underline = function(style){ style.u = !style.u }; - -style_fns.inverse = function(style){ - var tmp = style.fg; - style.fg = style.bg; - style.bg = tmp; -}; - -style_fns.reset = function(style, base_style){ - style.b = base_style.b; - style.i = base_style.i; - style.u = base_style.u; - style.fg = base_style.fg; - style.bg = base_style.bg; -}; - -var colorcode_to_json = function(string, opts){ - // looks like its already converted - if (typeof string === 'object' && - 'lines' in string && - 'w' in string && - 'h' in string) - return string; - - - opts = opts || {}; - var d = colorcode_to_json.defaults; - - var base_style = { - b: "b" in opts ? opts.b : d.b, - i: "i" in opts ? opts.i : d.i, - u: "u" in opts ? opts.u : d.u, - fg: "fg" in opts ? opts.fg : d.fg, - bg: "bg" in opts ? opts.bg : d.bg - }; - - var lines_in = string.split(/\r?\n/); - var lines_out = []; - var w = 0, h = 0; - - for (var i=0; i>> 8) ^ table[(crc ^ u8[i]) & 0xFF] - } - - //return (crc ^ (-1)) // signed - return (crc ^ (-1)) >>> 0 -} - -var signature = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]) -var te, td - -// decodes chunks in png -// see http://www.w3.org/TR/PNG/#5Chunk-layout -// returns something like -// [{length: Number, -// type: String[4], -// crc: Number, -// data: Uint8Array[] // optional -// }, ...] -var decode = function(buf, err){ - var u8a = new Uint8Array(buf) - var dv = new DataView(buf) - td = td || new TextDecoder('utf-8') - err = err || function(msg){ throw new Error(msg) } - - var out = [] - var pos = 0 - - if (u8a.length < signature.length) return err("not a valid png") - for (var i=0; i u8a.length) return err("unexpected end of file") - chunk.length = dv.getInt32(pos, false) - pos += 4 - - if (pos + 4 > u8a.length) return err("unexpected end of file") - chunk.type = td.decode(new DataView(buf, pos, 4)) - pos += 4 - - if (chunk.length){ - if (pos + chunk.length > u8a.length) return err('unexpected end of file') - chunk.data = new Uint8Array(buf, pos, chunk.length) - pos += chunk.length - } - - if (pos + 4 > u8a.length) return err("unexpected end of file") - //chunk.crc = new Uint8Array(buf, pos, 4) - chunk.crc = dv.getUint32(pos, false) - pos += 4 - - - out.push(chunk) - //done = true - //console.log(pos.length, u8a.length) - if (pos === u8a.length) done = true - } - - - return out -} - -var encode = function(chunks){ - te = te || new TextEncoder('utf-8') - - var size = 8 // inital png signature - for (var i=0, c; c=chunks[i]; i++){ - size += 4 // length - size += 4 // type - size += c.length // data - size += 4 // crc32 - } - - var buf = new ArrayBuffer(size) - var u8 = new Uint8Array(buf) - var dv = new DataView(buf) - var pos = 0 - - u8.set(signature, 0) - pos += 8 - - for (var i=0, c; c=chunks[i]; i++){ - dv.setInt32(pos, c.length, false) // length - pos += 4 - var chunk_type_u8 = te.encode(c.type) // type - u8.set(chunk_type_u8, pos) - pos += 4 - if (c.length){ - u8.set(c.data, pos) // data - pos += c.length - } - //u8.set(c.crc, pos) // crc32 - dv.setUint32(pos, c.crc, false) // crc32 - pos += 4 - } - - return u8 -} - - -var make_itxt_chunk = function(keyword, txt){ - te = te || new TextEncoder('utf-8') - var keyword_u8 = te.encode(keyword) - var txt_u8 = te.encode(txt) - var header_u8 = new Uint8Array(keyword_u8.length + 5) - header_u8.set(keyword_u8, 0) - // header has keyword, then a null byte and some additional fields - // see http://www.w3.org/TR/PNG/#11iTXt - var chunk = {type: 'iTXt'} - chunk.length = header_u8.length + txt_u8.length - var u8 = new Uint8Array(4 + chunk.length) - // put type and data on the same u8 array so we can calculate crc - u8.set(te.encode(chunk.type), 0) - u8.set(header_u8, 4) - u8.set(txt_u8, header_u8.length + 4) - chunk.crc = crc32(u8) - chunk.data = new Uint8Array(u8.buffer, 4) - return chunk -} - -var read_cstring = function(u8, pos){ - var str = "" - while (pos < u8.length){ - if (u8[pos] === 0) return str - str += String.fromCharCode(u8[pos]) - pos++ - } - return str -} - -var decode_itxt_chunk = function(chunk){ - td = td || new TextDecoder('utf-8') - var data = {} - var pos = 0 - data.keyword = read_cstring(chunk.data, 0) - pos += data.keyword.length + 1 - data.compression = chunk.data[pos] - pos += 1 - data.compression_method = chunk.data[pos] - pos += 1 - data.language = read_cstring(chunk.data, pos) - pos += data.language.length + 1 - data.translated_keyword = read_cstring(chunk.data, pos) - pos += data.translated_keyword.length + 1 - var data_u8 = chunk.data.subarray(pos) - data.data = td.decode(data_u8) - return data -} - -var canvas_to_blob_with_colorcode = function(canvas, cc){ - var u8 = dataUriToUint8Array(canvas.toDataURL()) - var chunks = decode(u8.buffer) - var itxt_chunk = make_itxt_chunk('colorcode', cc) - // assume we wanna insert the chunk very last, just in front of the end - chunks.splice(chunks.length - 1, 0, itxt_chunk) - var blob = new Blob([encode(chunks)], {type: 'image/png'}) - return blob -} - -var exports = {} -exports.crc32 = crc32 -exports.decode = decode -exports.encode = encode -exports.make_itxt_chunk = make_itxt_chunk -exports.decode_itxt_chunk = decode_itxt_chunk -exports.canvas_to_blob_with_colorcode = canvas_to_blob_with_colorcode -return exports - -})()