canteen.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /**
  2. * Canteen v1.0.4
  3. * August 19th, 2015
  4. *
  5. * Copyright 2015 Platfora, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. ;(function() {
  20. // ================================ Constants ================================
  21. var CONTEXT_2D_ATTRIBUTES = [
  22. 'fillStyle',
  23. 'font',
  24. 'globalAlpha',
  25. 'globalCompositeOperation',
  26. 'lineCap',
  27. 'lineDashOffset',
  28. 'lineJoin',
  29. 'lineWidth',
  30. 'miterLimit',
  31. 'shadowBlur',
  32. 'shadowColor',
  33. 'shadowOffsetX',
  34. 'shadowOffsetY',
  35. 'strokeStyle',
  36. 'textAlign',
  37. 'textBaseline'
  38. ];
  39. // ================================ Utils ================================
  40. function each(arr, func) {
  41. var len = arr.length,
  42. n;
  43. for (n=0; n<len; n++) {
  44. func(arr[n], n);
  45. }
  46. }
  47. function round(val, decimalPoints) {
  48. var power = Math.pow(10, decimalPoints);
  49. return Math.round(val * power) / power;
  50. }
  51. function roundArr(arr, decimalPoints) {
  52. var len = arr.length,
  53. ret = [],
  54. n;
  55. for (n=0; n<len; n++) {
  56. if (isNumber(arr[n])) {
  57. ret.push(round(arr[n], decimalPoints));
  58. }
  59. else {
  60. ret.push(arr[n]);
  61. }
  62. }
  63. return ret;
  64. }
  65. function isFunction(func) {
  66. return func && {}.toString.call(func) === '[object Function]';
  67. }
  68. function isNumber(val) {
  69. return typeof val === 'number';
  70. }
  71. // ================================ Canteen Class ================================
  72. /**
  73. * Canteen Constructor
  74. * @constructor
  75. */
  76. var Canteen = function(context) {
  77. var that = this;
  78. this._stack = [];
  79. this.context = context;
  80. // add observable attributes
  81. each(CONTEXT_2D_ATTRIBUTES, function(key, n) {
  82. Object.defineProperty(that, key, {
  83. get: function() {
  84. return that.context[key];
  85. },
  86. set: function(val) {
  87. that._pushAttr(key, val);
  88. that.context[key] = val;
  89. }
  90. });
  91. });
  92. };
  93. // Canteen methods
  94. Canteen.prototype = {
  95. /**
  96. * get a stack of operations
  97. * @method stack
  98. * @param {Object} config
  99. * @param {String} [config.loose=false] - strict mode returns method calls with arguments and property names
  100. * with values. loose mode only returns method calls and property names
  101. * @param {Number} [config.decimalPoints=3] - number of decimal points to round numeric values to. The default is
  102. * 3, i.e. 1.23456 will round to 1.234
  103. * @returns {Array}
  104. * @public
  105. */
  106. stack: function(config) {
  107. var config = config || {},
  108. loose = config.loose,
  109. decimalPoints = config.decimalPoints === undefined ? 3 : config.decimalPoints,
  110. ret = [];
  111. if (loose) {
  112. each(this._stack, function(el, n) {
  113. ret.push(el.method || el.attr);
  114. });
  115. }
  116. else {
  117. each(this._stack, function(el, n) {
  118. // if method instruction
  119. if (el.method) {
  120. ret.push({
  121. method: el.method,
  122. arguments: roundArr(el.arguments, decimalPoints)
  123. });
  124. }
  125. // if attr
  126. else if (el.attr) {
  127. ret.push({
  128. attr: el.attr,
  129. val: isNumber(el.val) ? round(el.val, decimalPoints) : el.val
  130. });
  131. }
  132. });
  133. }
  134. return ret;
  135. },
  136. /**
  137. * serialize a stack into a string
  138. * @method json
  139. * @param {Object} config
  140. * @param {String} [config.loose=false] - strict mode returns method calls with arguments and property names
  141. * with values. loose mode only returns method calls and property names
  142. * @param {Number} [config.decimalPoints=3] - number of decimal points to round numeric values to. The default is
  143. * 3, i.e. 1.23456 will round to 1.234
  144. * @returns {String}
  145. * @public
  146. */
  147. json: function(config) {
  148. return JSON.stringify(this.stack(config));
  149. },
  150. /**
  151. * convert a stack into a small hash string for easy comparisons
  152. * @method hash
  153. * @param {Object} config
  154. * @param {String} [config.loose=false] - strict mode returns method calls with arguments and property names
  155. * with values. loose mode only returns method calls and property names
  156. * @param {Number} [config.decimalPoints=3] - number of decimal points to round numeric values to. The default is
  157. * 3, i.e. 1.23456 will round to 1.234
  158. * @public
  159. * @returns {String}
  160. */
  161. hash: function(config) {
  162. return Canteen.md5(this.json(config));
  163. },
  164. /**
  165. * clear the stack
  166. * @method clear
  167. * @public
  168. */
  169. clear: function() {
  170. this._stack = [];
  171. },
  172. /**
  173. * push instruction method onto the stack
  174. * @method _pushMethod
  175. * @param {String} method
  176. * @param {arguments} args
  177. * @private
  178. */
  179. _pushMethod: function(method, args) {
  180. this._stack.push({
  181. method: method,
  182. arguments: Array.prototype.slice.call(args, 0)
  183. });
  184. this._slice();
  185. },
  186. /**
  187. * push instruction attribute onto the stack
  188. * @method _pushAttr
  189. * @param {String} attr
  190. * @param {*} val
  191. * @private
  192. */
  193. _pushAttr: function(attr, val) {
  194. this._stack.push({
  195. attr: attr,
  196. val: val
  197. });
  198. this._slice();
  199. },
  200. /**
  201. * slice the stack if needed. This means making sure that it doesn't exceed
  202. * the STACK_SIZE. if it does, then shorten the stack starting from the beginning
  203. * @method _slice
  204. * @private
  205. */
  206. _slice: function() {
  207. var stack = this._stack,
  208. len = stack.length,
  209. exceded = len - Canteen.globals.STACK_SIZE;
  210. if (exceded > 0) {
  211. this._stack = stack.slice(exceded);
  212. }
  213. }
  214. };
  215. // generate observable methods and add them to the Canteen prototype
  216. (function(){
  217. var proto = CanvasRenderingContext2D.prototype,
  218. key, val, desc;
  219. function addMethod(key, val) {
  220. Canteen.prototype[key] = function() {
  221. this._pushMethod(key, arguments);
  222. return this.context[key].apply(this.context, arguments);
  223. };
  224. }
  225. for (key in proto) {
  226. desc = Object.getOwnPropertyDescriptor(CanvasRenderingContext2D.prototype, key);
  227. val = (desc && desc.value ? proto[key] : null);
  228. if (isFunction(val)) {
  229. addMethod(key, val);
  230. }
  231. }
  232. })();
  233. // ================================ Global Config ================================
  234. /**
  235. * global config. You can directly change these values in order to configure Canteen
  236. * @static
  237. * @example
  238. * // change stack size to 3000
  239. * Canteen.globals.STACK_SIZE = 3000;
  240. */
  241. Canteen.globals = {
  242. STACK_SIZE: 10000
  243. };
  244. // ================================ Initialization ================================
  245. // override the canvas context getContext method in order to automatically instantiate
  246. // a Canteen instance and wrap the native context object
  247. (function(){
  248. var origGetContext = HTMLCanvasElement.prototype.getContext;
  249. HTMLCanvasElement.prototype.getContext = function() {
  250. var context = origGetContext.apply(this, arguments);
  251. // if the context already has a canteen instance, then return it
  252. if (context.canteen) {
  253. return context.canteen
  254. }
  255. // if the context does not have a canteen instance, then instantiate one
  256. // and return it
  257. else {
  258. context.canteen = new Canteen(context);
  259. return context.canteen;
  260. }
  261. }
  262. })();
  263. // make the Canteen namespace global so that developers can configure
  264. // it via Canteen.globals, or override methods if desired
  265. window.Canteen = Canteen;
  266. })();
  267. ;/*
  268. * JavaScript MD5 1.0.1
  269. * https://github.com/blueimp/JavaScript-MD5
  270. *
  271. * Copyright 2011, Sebastian Tschan
  272. * https://blueimp.net
  273. *
  274. * Licensed under the MIT license:
  275. * http://www.opensource.org/licenses/MIT
  276. *
  277. * Based on
  278. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  279. * Digest Algorithm, as defined in RFC 1321.
  280. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  281. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  282. * Distributed under the BSD License
  283. * See http://pajhome.org.uk/crypt/md5 for more info.
  284. */
  285. /*jslint bitwise: true */
  286. /*global unescape, define */
  287. (function ($) {
  288. 'use strict';
  289. /*
  290. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  291. * to work around bugs in some JS interpreters.
  292. */
  293. function safe_add(x, y) {
  294. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  295. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  296. return (msw << 16) | (lsw & 0xFFFF);
  297. }
  298. /*
  299. * Bitwise rotate a 32-bit number to the left.
  300. */
  301. function bit_rol(num, cnt) {
  302. return (num << cnt) | (num >>> (32 - cnt));
  303. }
  304. /*
  305. * These functions implement the four basic operations the algorithm uses.
  306. */
  307. function md5_cmn(q, a, b, x, s, t) {
  308. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  309. }
  310. function md5_ff(a, b, c, d, x, s, t) {
  311. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  312. }
  313. function md5_gg(a, b, c, d, x, s, t) {
  314. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  315. }
  316. function md5_hh(a, b, c, d, x, s, t) {
  317. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  318. }
  319. function md5_ii(a, b, c, d, x, s, t) {
  320. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  321. }
  322. /*
  323. * Calculate the MD5 of an array of little-endian words, and a bit length.
  324. */
  325. function binl_md5(x, len) {
  326. /* append padding */
  327. x[len >> 5] |= 0x80 << (len % 32);
  328. x[(((len + 64) >>> 9) << 4) + 14] = len;
  329. var i, olda, oldb, oldc, oldd,
  330. a = 1732584193,
  331. b = -271733879,
  332. c = -1732584194,
  333. d = 271733878;
  334. for (i = 0; i < x.length; i += 16) {
  335. olda = a;
  336. oldb = b;
  337. oldc = c;
  338. oldd = d;
  339. a = md5_ff(a, b, c, d, x[i], 7, -680876936);
  340. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  341. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  342. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  343. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  344. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  345. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  346. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  347. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  348. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  349. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  350. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  351. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  352. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  353. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  354. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  355. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  356. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  357. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  358. b = md5_gg(b, c, d, a, x[i], 20, -373897302);
  359. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  360. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  361. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  362. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  363. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  364. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  365. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  366. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  367. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  368. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  369. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  370. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  371. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  372. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  373. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  374. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  375. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  376. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  377. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  378. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  379. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  380. d = md5_hh(d, a, b, c, x[i], 11, -358537222);
  381. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  382. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  383. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  384. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  385. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  386. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  387. a = md5_ii(a, b, c, d, x[i], 6, -198630844);
  388. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  389. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  390. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  391. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  392. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  393. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  394. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  395. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  396. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  397. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  398. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  399. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  400. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  401. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  402. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  403. a = safe_add(a, olda);
  404. b = safe_add(b, oldb);
  405. c = safe_add(c, oldc);
  406. d = safe_add(d, oldd);
  407. }
  408. return [a, b, c, d];
  409. }
  410. /*
  411. * Convert an array of little-endian words to a string
  412. */
  413. function binl2rstr(input) {
  414. var i,
  415. output = '';
  416. for (i = 0; i < input.length * 32; i += 8) {
  417. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  418. }
  419. return output;
  420. }
  421. /*
  422. * Convert a raw string to an array of little-endian words
  423. * Characters >255 have their high-byte silently ignored.
  424. */
  425. function rstr2binl(input) {
  426. var i,
  427. output = [];
  428. output[(input.length >> 2) - 1] = undefined;
  429. for (i = 0; i < output.length; i += 1) {
  430. output[i] = 0;
  431. }
  432. for (i = 0; i < input.length * 8; i += 8) {
  433. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  434. }
  435. return output;
  436. }
  437. /*
  438. * Calculate the MD5 of a raw string
  439. */
  440. function rstr_md5(s) {
  441. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  442. }
  443. /*
  444. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  445. */
  446. function rstr_hmac_md5(key, data) {
  447. var i,
  448. bkey = rstr2binl(key),
  449. ipad = [],
  450. opad = [],
  451. hash;
  452. ipad[15] = opad[15] = undefined;
  453. if (bkey.length > 16) {
  454. bkey = binl_md5(bkey, key.length * 8);
  455. }
  456. for (i = 0; i < 16; i += 1) {
  457. ipad[i] = bkey[i] ^ 0x36363636;
  458. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  459. }
  460. hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  461. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  462. }
  463. /*
  464. * Convert a raw string to a hex string
  465. */
  466. function rstr2hex(input) {
  467. var hex_tab = '0123456789abcdef',
  468. output = '',
  469. x,
  470. i;
  471. for (i = 0; i < input.length; i += 1) {
  472. x = input.charCodeAt(i);
  473. output += hex_tab.charAt((x >>> 4) & 0x0F) +
  474. hex_tab.charAt(x & 0x0F);
  475. }
  476. return output;
  477. }
  478. /*
  479. * Encode a string as utf-8
  480. */
  481. function str2rstr_utf8(input) {
  482. return unescape(encodeURIComponent(input));
  483. }
  484. /*
  485. * Take string arguments and return either raw or hex encoded strings
  486. */
  487. function raw_md5(s) {
  488. return rstr_md5(str2rstr_utf8(s));
  489. }
  490. function hex_md5(s) {
  491. return rstr2hex(raw_md5(s));
  492. }
  493. function raw_hmac_md5(k, d) {
  494. return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d));
  495. }
  496. function hex_hmac_md5(k, d) {
  497. return rstr2hex(raw_hmac_md5(k, d));
  498. }
  499. function md5(string, key, raw) {
  500. if (!key) {
  501. if (!raw) {
  502. return hex_md5(string);
  503. }
  504. return raw_md5(string);
  505. }
  506. if (!raw) {
  507. return hex_hmac_md5(key, string);
  508. }
  509. return raw_hmac_md5(key, string);
  510. }
  511. if (typeof define === 'function' && define.amd) {
  512. define(function () {
  513. return md5;
  514. });
  515. } else {
  516. $.md5 = md5;
  517. }
  518. }(Canteen));