display.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. /*jslint browser: true, white: false */
  9. /*global Util, Base64, changeCursor */
  10. var Display;
  11. (function () {
  12. "use strict";
  13. Display = function (defaults) {
  14. this._drawCtx = null;
  15. this._c_forceCanvas = false;
  16. this._renderQ = []; // queue drawing actions for in-oder rendering
  17. // the full frame buffer (logical canvas) size
  18. this._fb_width = 0;
  19. this._fb_height = 0;
  20. // the visible "physical canvas" viewport
  21. this._viewportLoc = { 'x': 0, 'y': 0, 'w': 0, 'h': 0 };
  22. this._cleanRect = { 'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1 };
  23. this._prevDrawStyle = "";
  24. this._tile = null;
  25. this._tile16x16 = null;
  26. this._tile_x = 0;
  27. this._tile_y = 0;
  28. Util.set_defaults(this, defaults, {
  29. 'true_color': true,
  30. 'colourMap': [],
  31. 'scale': 1.0,
  32. 'viewport': false,
  33. 'render_mode': ''
  34. });
  35. Util.Debug(">> Display.constructor");
  36. if (!this._target) {
  37. throw new Error("Target must be set");
  38. }
  39. if (typeof this._target === 'string') {
  40. throw new Error('target must be a DOM element');
  41. }
  42. if (!this._target.getContext) {
  43. throw new Error("no getContext method");
  44. }
  45. if (!this._drawCtx) {
  46. this._drawCtx = this._target.getContext('2d');
  47. }
  48. Util.Debug("User Agent: " + navigator.userAgent);
  49. if (Util.Engine.gecko) { Util.Debug("Browser: gecko " + Util.Engine.gecko); }
  50. if (Util.Engine.webkit) { Util.Debug("Browser: webkit " + Util.Engine.webkit); }
  51. if (Util.Engine.trident) { Util.Debug("Browser: trident " + Util.Engine.trident); }
  52. if (Util.Engine.presto) { Util.Debug("Browser: presto " + Util.Engine.presto); }
  53. this.clear();
  54. // Check canvas features
  55. if ('createImageData' in this._drawCtx) {
  56. this._render_mode = 'canvas rendering';
  57. } else {
  58. throw new Error("Canvas does not support createImageData");
  59. }
  60. if (this._prefer_js === null) {
  61. Util.Info("Prefering javascript operations");
  62. this._prefer_js = true;
  63. }
  64. // Determine browser support for setting the cursor via data URI scheme
  65. var curDat = [];
  66. for (var i = 0; i < 8 * 8 * 4; i++) {
  67. curDat.push(255);
  68. }
  69. try {
  70. var curSave = this._target.style.cursor;
  71. Display.changeCursor(this._target, curDat, curDat, 2, 2, 8, 8);
  72. if (this._target.style.cursor) {
  73. if (this._cursor_uri === null || this._cursor_uri === undefined) {
  74. this._cursor_uri = true;
  75. }
  76. Util.Info("Data URI scheme cursor supported");
  77. } else {
  78. if (this._cursor_uri === null || this._cursor_uri === undefined) {
  79. this._cursor_uri = false;
  80. }
  81. Util.Warn("Data URI scheme cursor not supported");
  82. }
  83. this._target.style.cursor = curSave;
  84. } catch (exc) {
  85. Util.Error("Data URI scheme cursor test exception: " + exc);
  86. this._cursor_uri = false;
  87. }
  88. Util.Debug("<< Display.constructor");
  89. };
  90. Display.prototype = {
  91. // Public methods
  92. viewportChange: function (deltaX, deltaY, width, height) {
  93. var vp = this._viewportLoc;
  94. var cr = this._cleanRect;
  95. var canvas = this._target;
  96. if (!this._viewport) {
  97. Util.Debug("Setting viewport to full display region");
  98. deltaX = -vp.w; // clamped later of out of bounds
  99. deltaY = -vp.h;
  100. width = this._fb_width;
  101. height = this._fb_height;
  102. }
  103. if (typeof(deltaX) === "undefined") { deltaX = 0; }
  104. if (typeof(deltaY) === "undefined") { deltaY = 0; }
  105. if (typeof(width) === "undefined") { width = vp.w; }
  106. if (typeof(height) === "undefined") { height = vp.h; }
  107. // Size change
  108. if (width > this._fb_width) { width = this._fb_width; }
  109. if (height > this._fb_height) { height = this._fb_height; }
  110. if (vp.w !== width || vp.h !== height) {
  111. // Change width
  112. if (width < vp.w && cr.x2 > vp.x + width - 1) {
  113. cr.x2 = vp.x + width - 1;
  114. }
  115. vp.w = width;
  116. // Change height
  117. if (height < vp.h && cr.y2 > vp.y + height - 1) {
  118. cr.y2 = vp.y + height - 1;
  119. }
  120. vp.h = height;
  121. var saveImg = null;
  122. if (vp.w > 0 && vp.h > 0 && canvas.width > 0 && canvas.height > 0) {
  123. var img_width = canvas.width < vp.w ? canvas.width : vp.w;
  124. var img_height = canvas.height < vp.h ? canvas.height : vp.h;
  125. saveImg = this._drawCtx.getImageData(0, 0, img_width, img_height);
  126. }
  127. canvas.width = vp.w;
  128. canvas.height = vp.h;
  129. if (saveImg) {
  130. this._drawCtx.putImageData(saveImg, 0, 0);
  131. }
  132. }
  133. var vx2 = vp.x + vp.w - 1;
  134. var vy2 = vp.y + vp.h - 1;
  135. // Position change
  136. if (deltaX < 0 && vp.x + deltaX < 0) {
  137. deltaX = -vp.x;
  138. }
  139. if (vx2 + deltaX >= this._fb_width) {
  140. deltaX -= vx2 + deltaX - this._fb_width + 1;
  141. }
  142. if (vp.y + deltaY < 0) {
  143. deltaY = -vp.y;
  144. }
  145. if (vy2 + deltaY >= this._fb_height) {
  146. deltaY -= (vy2 + deltaY - this._fb_height + 1);
  147. }
  148. if (deltaX === 0 && deltaY === 0) {
  149. return;
  150. }
  151. Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  152. vp.x += deltaX;
  153. vx2 += deltaX;
  154. vp.y += deltaY;
  155. vy2 += deltaY;
  156. // Update the clean rectangle
  157. if (vp.x > cr.x1) {
  158. cr.x1 = vp.x;
  159. }
  160. if (vx2 < cr.x2) {
  161. cr.x2 = vx2;
  162. }
  163. if (vp.y > cr.y1) {
  164. cr.y1 = vp.y;
  165. }
  166. if (vy2 < cr.y2) {
  167. cr.y2 = vy2;
  168. }
  169. var x1, w;
  170. if (deltaX < 0) {
  171. // Shift viewport left, redraw left section
  172. x1 = 0;
  173. w = -deltaX;
  174. } else {
  175. // Shift viewport right, redraw right section
  176. x1 = vp.w - deltaX;
  177. w = deltaX;
  178. }
  179. var y1, h;
  180. if (deltaY < 0) {
  181. // Shift viewport up, redraw top section
  182. y1 = 0;
  183. h = -deltaY;
  184. } else {
  185. // Shift viewport down, redraw bottom section
  186. y1 = vp.h - deltaY;
  187. h = deltaY;
  188. }
  189. // Copy the valid part of the viewport to the shifted location
  190. var saveStyle = this._drawCtx.fillStyle;
  191. this._drawCtx.fillStyle = "rgb(255,255,255)";
  192. if (deltaX !== 0) {
  193. this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, -deltaX, 0, vp.w, vp.h);
  194. this._drawCtx.fillRect(x1, 0, w, vp.h);
  195. }
  196. if (deltaY !== 0) {
  197. this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, 0, -deltaY, vp.w, vp.h);
  198. this._drawCtx.fillRect(0, y1, vp.w, h);
  199. }
  200. this._drawCtx.fillStyle = saveStyle;
  201. },
  202. // Return a map of clean and dirty areas of the viewport and reset the
  203. // tracking of clean and dirty areas
  204. //
  205. // Returns: { 'cleanBox': { 'x': x, 'y': y, 'w': w, 'h': h},
  206. // 'dirtyBoxes': [{ 'x': x, 'y': y, 'w': w, 'h': h }, ...] }
  207. getCleanDirtyReset: function () {
  208. var vp = this._viewportLoc;
  209. var cr = this._cleanRect;
  210. var cleanBox = { 'x': cr.x1, 'y': cr.y1,
  211. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y2 - cr.y1 + 1 };
  212. var dirtyBoxes = [];
  213. if (cr.x1 >= cr.x2 || cr.y1 >= cr.y2) {
  214. // Whole viewport is dirty
  215. dirtyBoxes.push({ 'x': vp.x, 'y': vp.y, 'w': vp.w, 'h': vp.h });
  216. } else {
  217. // Redraw dirty regions
  218. var vx2 = vp.x + vp.w - 1;
  219. var vy2 = vp.y + vp.h - 1;
  220. if (vp.x < cr.x1) {
  221. // left side dirty region
  222. dirtyBoxes.push({'x': vp.x, 'y': vp.y,
  223. 'w': cr.x1 - vp.x + 1, 'h': vp.h});
  224. }
  225. if (vx2 > cr.x2) {
  226. // right side dirty region
  227. dirtyBoxes.push({'x': cr.x2 + 1, 'y': vp.y,
  228. 'w': vx2 - cr.x2, 'h': vp.h});
  229. }
  230. if(vp.y < cr.y1) {
  231. // top/middle dirty region
  232. dirtyBoxes.push({'x': cr.x1, 'y': vp.y,
  233. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y1 - vp.y});
  234. }
  235. if (vy2 > cr.y2) {
  236. // bottom/middle dirty region
  237. dirtyBoxes.push({'x': cr.x1, 'y': cr.y2 + 1,
  238. 'w': cr.x2 - cr.x1 + 1, 'h': vy2 - cr.y2});
  239. }
  240. }
  241. this._cleanRect = {'x1': vp.x, 'y1': vp.y,
  242. 'x2': vp.x + vp.w - 1, 'y2': vp.y + vp.h - 1};
  243. return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
  244. },
  245. absX: function (x) {
  246. return x + this._viewportLoc.x;
  247. },
  248. absY: function (y) {
  249. return y + this._viewportLoc.y;
  250. },
  251. resize: function (width, height) {
  252. this._prevDrawStyle = "";
  253. this._fb_width = width;
  254. this._fb_height = height;
  255. this._rescale(this._scale);
  256. this.viewportChange();
  257. },
  258. clear: function () {
  259. if (this._logo) {
  260. this.resize(this._logo.width, this._logo.height);
  261. this.blitStringImage(this._logo.data, 0, 0);
  262. } else {
  263. if (Util.Engine.trident === 6) {
  264. // NB(directxman12): there's a bug in IE10 where we can fail to actually
  265. // clear the canvas here because of the resize.
  266. // Clearing the current viewport first fixes the issue
  267. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  268. }
  269. this.resize(640, 20);
  270. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  271. }
  272. this._renderQ = [];
  273. },
  274. fillRect: function (x, y, width, height, color) {
  275. this._setFillColor(color);
  276. this._drawCtx.fillRect(x - this._viewportLoc.x, y - this._viewportLoc.y, width, height);
  277. },
  278. copyImage: function (old_x, old_y, new_x, new_y, w, h) {
  279. var x1 = old_x - this._viewportLoc.x;
  280. var y1 = old_y - this._viewportLoc.y;
  281. var x2 = new_x - this._viewportLoc.x;
  282. var y2 = new_y - this._viewportLoc.y;
  283. this._drawCtx.drawImage(this._target, x1, y1, w, h, x2, y2, w, h);
  284. },
  285. // start updating a tile
  286. startTile: function (x, y, width, height, color) {
  287. this._tile_x = x;
  288. this._tile_y = y;
  289. if (width === 16 && height === 16) {
  290. this._tile = this._tile16x16;
  291. } else {
  292. this._tile = this._drawCtx.createImageData(width, height);
  293. }
  294. if (this._prefer_js) {
  295. var bgr;
  296. if (this._true_color) {
  297. bgr = color;
  298. } else {
  299. bgr = this._colourMap[color[0]];
  300. }
  301. var red = bgr[2];
  302. var green = bgr[1];
  303. var blue = bgr[0];
  304. var data = this._tile.data;
  305. for (var i = 0; i < width * height * 4; i += 4) {
  306. data[i] = red;
  307. data[i + 1] = green;
  308. data[i + 2] = blue;
  309. data[i + 3] = 255;
  310. }
  311. } else {
  312. this.fillRect(x, y, width, height, color);
  313. }
  314. },
  315. // update sub-rectangle of the current tile
  316. subTile: function (x, y, w, h, color) {
  317. if (this._prefer_js) {
  318. var bgr;
  319. if (this._true_color) {
  320. bgr = color;
  321. } else {
  322. bgr = this._colourMap[color[0]];
  323. }
  324. var red = bgr[2];
  325. var green = bgr[1];
  326. var blue = bgr[0];
  327. var xend = x + w;
  328. var yend = y + h;
  329. var data = this._tile.data;
  330. var width = this._tile.width;
  331. for (var j = y; j < yend; j++) {
  332. for (var i = x; i < xend; i++) {
  333. var p = (i + (j * width)) * 4;
  334. data[p] = red;
  335. data[p + 1] = green;
  336. data[p + 2] = blue;
  337. data[p + 3] = 255;
  338. }
  339. }
  340. } else {
  341. this.fillRect(this._tile_x + x, this._tile_y + y, w, h, color);
  342. }
  343. },
  344. // draw the current tile to the screen
  345. finishTile: function () {
  346. if (this._prefer_js) {
  347. this._drawCtx.putImageData(this._tile, this._tile_x - this._viewportLoc.x,
  348. this._tile_y - this._viewportLoc.y);
  349. }
  350. // else: No-op -- already done by setSubTile
  351. },
  352. blitImage: function (x, y, width, height, arr, offset) {
  353. if (this._true_color) {
  354. this._bgrxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  355. } else {
  356. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  357. }
  358. },
  359. blitRgbImage: function (x, y , width, height, arr, offset) {
  360. if (this._true_color) {
  361. this._rgbImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  362. } else {
  363. // probably wrong?
  364. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  365. }
  366. },
  367. blitStringImage: function (str, x, y) {
  368. var img = new Image();
  369. img.onload = function () {
  370. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  371. }.bind(this);
  372. img.src = str;
  373. return img; // for debugging purposes
  374. },
  375. // wrap ctx.drawImage but relative to viewport
  376. drawImage: function (img, x, y) {
  377. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  378. },
  379. renderQ_push: function (action) {
  380. this._renderQ.push(action);
  381. if (this._renderQ.length === 1) {
  382. // If this can be rendered immediately it will be, otherwise
  383. // the scanner will start polling the queue (every
  384. // requestAnimationFrame interval)
  385. this._scan_renderQ();
  386. }
  387. },
  388. changeCursor: function (pixels, mask, hotx, hoty, w, h) {
  389. if (this._cursor_uri === false) {
  390. Util.Warn("changeCursor called but no cursor data URI support");
  391. return;
  392. }
  393. if (this._true_color) {
  394. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h);
  395. } else {
  396. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h, this._colourMap);
  397. }
  398. },
  399. defaultCursor: function () {
  400. this._target.style.cursor = "default";
  401. },
  402. // Overridden getters/setters
  403. get_context: function () {
  404. return this._drawCtx;
  405. },
  406. set_scale: function (scale) {
  407. this._rescale(scale);
  408. },
  409. set_width: function (w) {
  410. this.resize(w, this._fb_height);
  411. },
  412. get_width: function () {
  413. return this._fb_width;
  414. },
  415. set_height: function (h) {
  416. this.resize(this._fb_width, h);
  417. },
  418. get_height: function () {
  419. return this._fb_height;
  420. },
  421. // Private Methods
  422. _rescale: function (factor) {
  423. var canvas = this._target;
  424. var properties = ['transform', 'WebkitTransform', 'MozTransform'];
  425. var transform_prop;
  426. while ((transform_prop = properties.shift())) {
  427. if (typeof canvas.style[transform_prop] !== 'undefined') {
  428. break;
  429. }
  430. }
  431. if (transform_prop === null) {
  432. Util.Debug("No scaling support");
  433. return;
  434. }
  435. if (typeof(factor) === "undefined") {
  436. factor = this._scale;
  437. } else if (factor > 1.0) {
  438. factor = 1.0;
  439. } else if (factor < 0.1) {
  440. factor = 0.1;
  441. }
  442. if (this._scale === factor) {
  443. return;
  444. }
  445. this._scale = factor;
  446. var x = canvas.width - (canvas.width * factor);
  447. var y = canvas.height - (canvas.height * factor);
  448. canvas.style[transform_prop] = 'scale(' + this._scale + ') translate(-' + x + 'px, -' + y + 'px)';
  449. },
  450. _setFillColor: function (color) {
  451. var bgr;
  452. if (this._true_color) {
  453. bgr = color;
  454. } else {
  455. bgr = this._colourMap[color[0]];
  456. }
  457. var newStyle = 'rgb(' + bgr[2] + ',' + bgr[1] + ',' + bgr[0] + ')';
  458. if (newStyle !== this._prevDrawStyle) {
  459. this._drawCtx.fillStyle = newStyle;
  460. this._prevDrawStyle = newStyle;
  461. }
  462. },
  463. _rgbImageData: function (x, y, vx, vy, width, height, arr, offset) {
  464. var img = this._drawCtx.createImageData(width, height);
  465. var data = img.data;
  466. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 3) {
  467. data[i] = arr[j];
  468. data[i + 1] = arr[j + 1];
  469. data[i + 2] = arr[j + 2];
  470. data[i + 3] = 255; // Alpha
  471. }
  472. this._drawCtx.putImageData(img, x - vx, y - vy);
  473. },
  474. _bgrxImageData: function (x, y, vx, vy, width, height, arr, offset) {
  475. var img = this._drawCtx.createImageData(width, height);
  476. var data = img.data;
  477. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 4) {
  478. data[i] = arr[j + 2];
  479. data[i + 1] = arr[j + 1];
  480. data[i + 2] = arr[j];
  481. data[i + 3] = 255; // Alpha
  482. }
  483. this._drawCtx.putImageData(img, x - vx, y - vy);
  484. },
  485. _cmapImageData: function (x, y, vx, vy, width, height, arr, offset) {
  486. var img = this._drawCtx.createImageData(width, height);
  487. var data = img.data;
  488. var cmap = this._colourMap;
  489. for (var i = 0, j = offset; i < width * height * 4; i += 4, j++) {
  490. var bgr = cmap[arr[j]];
  491. data[i] = bgr[2];
  492. data[i + 1] = bgr[1];
  493. data[i + 2] = bgr[0];
  494. data[i + 3] = 255; // Alpha
  495. }
  496. this._drawCtx.putImageData(img, x - vx, y - vy);
  497. },
  498. _scan_renderQ: function () {
  499. var ready = true;
  500. while (ready && this._renderQ.length > 0) {
  501. var a = this._renderQ[0];
  502. switch (a.type) {
  503. case 'copy':
  504. this.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height);
  505. break;
  506. case 'fill':
  507. this.fillRect(a.x, a.y, a.width, a.height, a.color);
  508. break;
  509. case 'blit':
  510. this.blitImage(a.x, a.y, a.width, a.height, a.data, 0);
  511. break;
  512. case 'blitRgb':
  513. this.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0);
  514. break;
  515. case 'img':
  516. if (a.img.complete) {
  517. this.drawImage(a.img, a.x, a.y);
  518. } else {
  519. // We need to wait for this image to 'load'
  520. // to keep things in-order
  521. ready = false;
  522. }
  523. break;
  524. }
  525. if (ready) {
  526. this._renderQ.shift();
  527. }
  528. }
  529. if (this._renderQ.length > 0) {
  530. requestAnimFrame(this._scan_renderQ.bind(this));
  531. }
  532. },
  533. };
  534. Util.make_properties(Display, [
  535. ['target', 'wo', 'dom'], // Canvas element for rendering
  536. ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
  537. ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "data": data}
  538. ['true_color', 'rw', 'bool'], // Use true-color pixel data
  539. ['colourMap', 'rw', 'arr'], // Colour map array (when not true-color)
  540. ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
  541. ['viewport', 'rw', 'bool'], // Use a viewport set with viewportChange()
  542. ['width', 'rw', 'int'], // Display area width
  543. ['height', 'rw', 'int'], // Display area height
  544. ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
  545. ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
  546. ['cursor_uri', 'rw', 'raw'] // Can we render cursor using data URI
  547. ]);
  548. // Class Methods
  549. Display.changeCursor = function (target, pixels, mask, hotx, hoty, w0, h0, cmap) {
  550. var w = w0;
  551. var h = h0;
  552. if (h < w) {
  553. h = w; // increase h to make it square
  554. } else {
  555. w = h; // increase w to make it square
  556. }
  557. var cur = [];
  558. // Push multi-byte little-endian values
  559. cur.push16le = function (num) {
  560. this.push(num & 0xFF, (num >> 8) & 0xFF);
  561. };
  562. cur.push32le = function (num) {
  563. this.push(num & 0xFF,
  564. (num >> 8) & 0xFF,
  565. (num >> 16) & 0xFF,
  566. (num >> 24) & 0xFF);
  567. };
  568. var IHDRsz = 40;
  569. var RGBsz = w * h * 4;
  570. var XORsz = Math.ceil((w * h) / 8.0);
  571. var ANDsz = Math.ceil((w * h) / 8.0);
  572. cur.push16le(0); // 0: Reserved
  573. cur.push16le(2); // 2: .CUR type
  574. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  575. // Cursor #1 header (ICONDIRENTRY)
  576. cur.push(w); // 6: width
  577. cur.push(h); // 7: height
  578. cur.push(0); // 8: colors, 0 -> true-color
  579. cur.push(0); // 9: reserved
  580. cur.push16le(hotx); // 10: hotspot x coordinate
  581. cur.push16le(hoty); // 12: hotspot y coordinate
  582. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  583. // 14: cursor data byte size
  584. cur.push32le(22); // 18: offset of cursor data in the file
  585. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  586. cur.push32le(IHDRsz); // 22: InfoHeader size
  587. cur.push32le(w); // 26: Cursor width
  588. cur.push32le(h * 2); // 30: XOR+AND height
  589. cur.push16le(1); // 34: number of planes
  590. cur.push16le(32); // 36: bits per pixel
  591. cur.push32le(0); // 38: Type of compression
  592. cur.push32le(XORsz + ANDsz);
  593. // 42: Size of Image
  594. cur.push32le(0); // 46: reserved
  595. cur.push32le(0); // 50: reserved
  596. cur.push32le(0); // 54: reserved
  597. cur.push32le(0); // 58: reserved
  598. // 62: color data (RGBQUAD icColors[])
  599. var y, x;
  600. for (y = h - 1; y >= 0; y--) {
  601. for (x = 0; x < w; x++) {
  602. if (x >= w0 || y >= h0) {
  603. cur.push(0); // blue
  604. cur.push(0); // green
  605. cur.push(0); // red
  606. cur.push(0); // alpha
  607. } else {
  608. var idx = y * Math.ceil(w0 / 8) + Math.floor(x / 8);
  609. var alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  610. if (cmap) {
  611. idx = (w0 * y) + x;
  612. var rgb = cmap[pixels[idx]];
  613. cur.push(rgb[2]); // blue
  614. cur.push(rgb[1]); // green
  615. cur.push(rgb[0]); // red
  616. cur.push(alpha); // alpha
  617. } else {
  618. idx = ((w0 * y) + x) * 4;
  619. cur.push(pixels[idx + 2]); // blue
  620. cur.push(pixels[idx + 1]); // green
  621. cur.push(pixels[idx]); // red
  622. cur.push(alpha); // alpha
  623. }
  624. }
  625. }
  626. }
  627. // XOR/bitmask data (BYTE icXOR[])
  628. // (ignored, just needs to be the right size)
  629. for (y = 0; y < h; y++) {
  630. for (x = 0; x < Math.ceil(w / 8); x++) {
  631. cur.push(0);
  632. }
  633. }
  634. // AND/bitmask data (BYTE icAND[])
  635. // (ignored, just needs to be the right size)
  636. for (y = 0; y < h; y++) {
  637. for (x = 0; x < Math.ceil(w / 8); x++) {
  638. cur.push(0);
  639. }
  640. }
  641. var url = 'data:image/x-icon;base64,' + Base64.encode(cur);
  642. target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
  643. };
  644. })();