display.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2011 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. /*jslint browser: true, white: false, bitwise: false */
  9. /*global Util, Base64, changeCursor */
  10. function Display(defaults) {
  11. "use strict";
  12. var that = {}, // Public API methods
  13. conf = {}, // Configuration attributes
  14. // Private Display namespace variables
  15. c_ctx = null,
  16. c_forceCanvas = false,
  17. c_imageData, c_rgbxImage, c_cmapImage,
  18. // Predefine function variables (jslint)
  19. imageDataCreate, imageDataGet, rgbxImageData, cmapImageData,
  20. rgbxImageFill, cmapImageFill, setFillColor, rescale, flush,
  21. // The full frame buffer (logical canvas) size
  22. fb_width = 0,
  23. fb_height = 0,
  24. // The visible "physical canvas" viewport
  25. viewport = {'x': 0, 'y': 0, 'w' : 0, 'h' : 0 },
  26. cleanRect = {'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1},
  27. c_prevStyle = "",
  28. c_webkit_bug = false,
  29. c_flush_timer = null;
  30. // Configuration attributes
  31. Util.conf_defaults(conf, that, defaults, [
  32. ['target', 'wo', 'dom', null, 'Canvas element for rendering'],
  33. ['context', 'ro', 'raw', null, 'Canvas 2D context for rendering (read-only)'],
  34. ['logo', 'rw', 'raw', null, 'Logo to display when cleared: {"width": width, "height": height, "data": data}'],
  35. ['true_color', 'rw', 'bool', true, 'Use true-color pixel data'],
  36. ['colourMap', 'rw', 'arr', [], 'Colour map array (when not true-color)'],
  37. ['scale', 'rw', 'float', 1.0, 'Display area scale factor 0.0 - 1.0'],
  38. ['viewport', 'rw', 'bool', false, 'Use a viewport set with viewportChange()'],
  39. ['width', 'rw', 'int', null, 'Display area width'],
  40. ['height', 'rw', 'int', null, 'Display area height'],
  41. ['render_mode', 'ro', 'str', '', 'Canvas rendering mode (read-only)'],
  42. ['prefer_js', 'rw', 'str', null, 'Prefer Javascript over canvas methods'],
  43. ['cursor_uri', 'rw', 'raw', null, 'Can we render cursor using data URI']
  44. ]);
  45. // Override some specific getters/setters
  46. that.get_context = function () { return c_ctx; };
  47. that.set_scale = function(scale) { rescale(scale); };
  48. that.set_width = function (val) { that.resize(val, fb_height); };
  49. that.get_width = function() { return fb_width; };
  50. that.set_height = function (val) { that.resize(fb_width, val); };
  51. that.get_height = function() { return fb_height; };
  52. that.set_prefer_js = function(val) {
  53. if (val && c_forceCanvas) {
  54. Util.Warn("Preferring Javascript to Canvas ops is not supported");
  55. return false;
  56. }
  57. conf.prefer_js = val;
  58. return true;
  59. };
  60. //
  61. // Private functions
  62. //
  63. // Create the public API interface
  64. function constructor() {
  65. Util.Debug(">> Display.constructor");
  66. var c, func, imgTest, tval, i, curDat, curSave,
  67. has_imageData = false, UE = Util.Engine;
  68. if (! conf.target) { throw("target must be set"); }
  69. if (typeof conf.target === 'string') {
  70. throw("target must be a DOM element");
  71. }
  72. c = conf.target;
  73. if (! c.getContext) { throw("no getContext method"); }
  74. if (! c_ctx) { c_ctx = c.getContext('2d'); }
  75. Util.Debug("User Agent: " + navigator.userAgent);
  76. if (UE.gecko) { Util.Debug("Browser: gecko " + UE.gecko); }
  77. if (UE.webkit) { Util.Debug("Browser: webkit " + UE.webkit); }
  78. if (UE.trident) { Util.Debug("Browser: trident " + UE.trident); }
  79. if (UE.presto) { Util.Debug("Browser: presto " + UE.presto); }
  80. that.clear();
  81. /*
  82. * Determine browser Canvas feature support
  83. * and select fastest rendering methods
  84. */
  85. tval = 0;
  86. try {
  87. imgTest = c_ctx.getImageData(0, 0, 1,1);
  88. imgTest.data[0] = 123;
  89. imgTest.data[3] = 255;
  90. c_ctx.putImageData(imgTest, 0, 0);
  91. tval = c_ctx.getImageData(0, 0, 1, 1).data[0];
  92. if (tval === 123) {
  93. has_imageData = true;
  94. }
  95. } catch (exc1) {}
  96. if (has_imageData) {
  97. Util.Info("Canvas supports imageData");
  98. c_forceCanvas = false;
  99. if (c_ctx.createImageData) {
  100. // If it's there, it's faster
  101. Util.Info("Using Canvas createImageData");
  102. conf.render_mode = "createImageData rendering";
  103. c_imageData = imageDataCreate;
  104. } else if (c_ctx.getImageData) {
  105. // I think this is mostly just Opera
  106. Util.Info("Using Canvas getImageData");
  107. conf.render_mode = "getImageData rendering";
  108. c_imageData = imageDataGet;
  109. }
  110. Util.Info("Prefering javascript operations");
  111. if (conf.prefer_js === null) {
  112. conf.prefer_js = true;
  113. }
  114. c_rgbxImage = rgbxImageData;
  115. c_cmapImage = cmapImageData;
  116. } else {
  117. Util.Warn("Canvas lacks imageData, using fillRect (slow)");
  118. conf.render_mode = "fillRect rendering (slow)";
  119. c_forceCanvas = true;
  120. conf.prefer_js = false;
  121. c_rgbxImage = rgbxImageFill;
  122. c_cmapImage = cmapImageFill;
  123. }
  124. if (UE.webkit && UE.webkit >= 534.7 && UE.webkit <= 534.9) {
  125. // Workaround WebKit canvas rendering bug #46319
  126. conf.render_mode += ", webkit bug workaround";
  127. Util.Debug("Working around WebKit bug #46319");
  128. c_webkit_bug = true;
  129. for (func in {"fillRect":1, "copyImage":1, "rgbxImage":1,
  130. "cmapImage":1, "blitStringImage":1}) {
  131. that[func] = (function() {
  132. var myfunc = that[func]; // Save original function
  133. //Util.Debug("Wrapping " + func);
  134. return function() {
  135. myfunc.apply(this, arguments);
  136. if (!c_flush_timer) {
  137. c_flush_timer = setTimeout(flush, 100);
  138. }
  139. };
  140. }());
  141. }
  142. }
  143. /*
  144. * Determine browser support for setting the cursor via data URI
  145. * scheme
  146. */
  147. curDat = [];
  148. for (i=0; i < 8 * 8 * 4; i += 1) {
  149. curDat.push(255);
  150. }
  151. try {
  152. curSave = c.style.cursor;
  153. changeCursor(conf.target, curDat, curDat, 2, 2, 8, 8);
  154. if (c.style.cursor) {
  155. if (conf.cursor_uri === null) {
  156. conf.cursor_uri = true;
  157. }
  158. Util.Info("Data URI scheme cursor supported");
  159. } else {
  160. if (conf.cursor_uri === null) {
  161. conf.cursor_uri = false;
  162. }
  163. Util.Warn("Data URI scheme cursor not supported");
  164. }
  165. c.style.cursor = curSave;
  166. } catch (exc2) {
  167. Util.Error("Data URI scheme cursor test exception: " + exc2);
  168. conf.cursor_uri = false;
  169. }
  170. Util.Debug("<< Display.constructor");
  171. return that ;
  172. }
  173. rescale = function(factor) {
  174. var c, tp, x, y,
  175. properties = ['transform', 'WebkitTransform', 'MozTransform', null];
  176. c = conf.target;
  177. tp = properties.shift();
  178. while (tp) {
  179. if (typeof c.style[tp] !== 'undefined') {
  180. break;
  181. }
  182. tp = properties.shift();
  183. }
  184. if (tp === null) {
  185. Util.Debug("No scaling support");
  186. return;
  187. }
  188. if (typeof(factor) === "undefined") {
  189. factor = conf.scale;
  190. } else if (factor > 1.0) {
  191. factor = 1.0;
  192. } else if (factor < 0.1) {
  193. factor = 0.1;
  194. }
  195. if (conf.scale === factor) {
  196. //Util.Debug("Display already scaled to '" + factor + "'");
  197. return;
  198. }
  199. conf.scale = factor;
  200. x = c.width - c.width * factor;
  201. y = c.height - c.height * factor;
  202. c.style[tp] = "scale(" + conf.scale + ") translate(-" + x + "px, -" + y + "px)";
  203. };
  204. that.viewportChange = function(deltaX, deltaY, width, height) {
  205. var c = conf.target, v = viewport, cr = cleanRect,
  206. saveImg = null, saveStyle, x1, y1, vx2, vy2, w, h;
  207. if (!conf.viewport) {
  208. Util.Debug("Setting viewport to full display region");
  209. deltaX = -v.w; // Clamped later if out of bounds
  210. deltaY = -v.h; // Clamped later if out of bounds
  211. width = fb_width;
  212. height = fb_height;
  213. }
  214. if (typeof(deltaX) === "undefined") { deltaX = 0; }
  215. if (typeof(deltaY) === "undefined") { deltaY = 0; }
  216. if (typeof(width) === "undefined") { width = v.w; }
  217. if (typeof(height) === "undefined") { height = v.h; }
  218. // Size change
  219. if (width > fb_width) { width = fb_width; }
  220. if (height > fb_height) { height = fb_height; }
  221. if ((v.w !== width) || (v.h !== height)) {
  222. // Change width
  223. if ((width < v.w) && (cr.x2 > v.x + width -1)) {
  224. cr.x2 = v.x + width - 1;
  225. }
  226. v.w = width;
  227. // Change height
  228. if ((height < v.h) && (cr.y2 > v.y + height -1)) {
  229. cr.y2 = v.y + height - 1;
  230. }
  231. v.h = height;
  232. if (v.w > 0 && v.h > 0 && c.width > 0 && c.height > 0) {
  233. console.log("here1:",
  234. ((c.width < v.w) ? c.width : v.w),
  235. ((c.height < v.h) ? c.height : v.h));
  236. saveImg = c_ctx.getImageData(0, 0,
  237. (c.width < v.w) ? c.width : v.w,
  238. (c.height < v.h) ? c.height : v.h);
  239. }
  240. c.width = v.w;
  241. c.height = v.h;
  242. if (saveImg) {
  243. c_ctx.putImageData(saveImg, 0, 0);
  244. }
  245. }
  246. vx2 = v.x + v.w - 1;
  247. vy2 = v.y + v.h - 1;
  248. // Position change
  249. if ((deltaX < 0) && ((v.x + deltaX) < 0)) {
  250. deltaX = - v.x;
  251. }
  252. if ((vx2 + deltaX) >= fb_width) {
  253. deltaX -= ((vx2 + deltaX) - fb_width + 1);
  254. }
  255. if ((v.y + deltaY) < 0) {
  256. deltaY = - v.y;
  257. }
  258. if ((vy2 + deltaY) >= fb_height) {
  259. deltaY -= ((vy2 + deltaY) - fb_height + 1);
  260. }
  261. if ((deltaX === 0) && (deltaY === 0)) {
  262. //Util.Debug("skipping viewport change");
  263. return;
  264. }
  265. Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  266. v.x += deltaX;
  267. vx2 += deltaX;
  268. v.y += deltaY;
  269. vy2 += deltaY;
  270. // Update the clean rectangle
  271. if (v.x > cr.x1) {
  272. cr.x1 = v.x;
  273. }
  274. if (vx2 < cr.x2) {
  275. cr.x2 = vx2;
  276. }
  277. if (v.y > cr.y1) {
  278. cr.y1 = v.y;
  279. }
  280. if (vy2 < cr.y2) {
  281. cr.y2 = vy2;
  282. }
  283. if (deltaX < 0) {
  284. // Shift viewport left, redraw left section
  285. x1 = 0;
  286. w = - deltaX;
  287. } else {
  288. // Shift viewport right, redraw right section
  289. x1 = v.w - deltaX;
  290. w = deltaX;
  291. }
  292. if (deltaY < 0) {
  293. // Shift viewport up, redraw top section
  294. y1 = 0;
  295. h = - deltaY;
  296. } else {
  297. // Shift viewport down, redraw bottom section
  298. y1 = v.h - deltaY;
  299. h = deltaY;
  300. }
  301. // Copy the valid part of the viewport to the shifted location
  302. saveStyle = c_ctx.fillStyle;
  303. c_ctx.fillStyle = "rgb(255,255,255)";
  304. if (deltaX !== 0) {
  305. //that.copyImage(0, 0, -deltaX, 0, v.w, v.h);
  306. //that.fillRect(x1, 0, w, v.h, [255,255,255]);
  307. c_ctx.drawImage(c, 0, 0, v.w, v.h, -deltaX, 0, v.w, v.h);
  308. c_ctx.fillRect(x1, 0, w, v.h);
  309. }
  310. if (deltaY !== 0) {
  311. //that.copyImage(0, 0, 0, -deltaY, v.w, v.h);
  312. //that.fillRect(0, y1, v.w, h, [255,255,255]);
  313. c_ctx.drawImage(c, 0, 0, v.w, v.h, 0, -deltaY, v.w, v.h);
  314. c_ctx.fillRect(0, y1, v.w, h);
  315. }
  316. c_ctx.fillStyle = saveStyle;
  317. };
  318. that.getCleanDirtyReset = function() {
  319. var v = viewport, c = cleanRect, cleanBox, dirtyBoxes = [],
  320. vx2 = v.x + v.w - 1, vy2 = v.y + v.h - 1;
  321. // Copy the cleanRect
  322. cleanBox = {'x': c.x1, 'y': c.y1,
  323. 'w': c.x2 - c.x1 + 1, 'h': c.y2 - c.y1 + 1};
  324. if ((c.x1 >= c.x2) || (c.y1 >= c.y2)) {
  325. // Whole viewport is dirty
  326. dirtyBoxes.push({'x': v.x, 'y': v.y, 'w': v.w, 'h': v.h});
  327. } else {
  328. // Redraw dirty regions
  329. if (v.x < c.x1) {
  330. // left side dirty region
  331. dirtyBoxes.push({'x': v.x, 'y': v.y,
  332. 'w': c.x1 - v.x + 1, 'h': v.h});
  333. }
  334. if (vx2 > c.x2) {
  335. // right side dirty region
  336. dirtyBoxes.push({'x': c.x2 + 1, 'y': v.y,
  337. 'w': vx2 - c.x2, 'h': v.h});
  338. }
  339. if (v.y < c.y1) {
  340. // top/middle dirty region
  341. dirtyBoxes.push({'x': c.x1, 'y': v.y,
  342. 'w': c.x2 - c.x1 + 1, 'h': c.y1 - v.y});
  343. }
  344. if (vy2 > c.y2) {
  345. // bottom/middle dirty region
  346. dirtyBoxes.push({'x': c.x1, 'y': c.y2 + 1,
  347. 'w': c.x2 - c.x1 + 1, 'h': vy2 - c.y2});
  348. }
  349. }
  350. // Reset the cleanRect to the whole viewport
  351. cleanRect = {'x1': v.x, 'y1': v.y,
  352. 'x2': v.x + v.w - 1, 'y2': v.y + v.h - 1};
  353. return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
  354. };
  355. that.absX = function(x) {
  356. return x + viewport.x;
  357. }
  358. that.absY = function(y) {
  359. return y + viewport.y;
  360. }
  361. // Force canvas redraw (for webkit bug #46319 workaround)
  362. flush = function() {
  363. var old_val;
  364. //Util.Debug(">> flush");
  365. old_val = conf.target.style.marginRight;
  366. conf.target.style.marginRight = "1px";
  367. c_flush_timer = null;
  368. setTimeout(function () {
  369. conf.target.style.marginRight = old_val;
  370. }, 1);
  371. };
  372. setFillColor = function(color) {
  373. var rgb, newStyle;
  374. if (conf.true_color) {
  375. rgb = color;
  376. } else {
  377. rgb = conf.colourMap[color[0]];
  378. }
  379. newStyle = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";
  380. if (newStyle !== c_prevStyle) {
  381. c_ctx.fillStyle = newStyle;
  382. c_prevStyle = newStyle;
  383. }
  384. };
  385. //
  386. // Public API interface functions
  387. //
  388. that.resize = function(width, height) {
  389. c_prevStyle = "";
  390. fb_width = width;
  391. fb_height = height;
  392. rescale(conf.scale);
  393. that.viewportChange();
  394. };
  395. that.clear = function() {
  396. if (conf.logo) {
  397. that.resize(conf.logo.width, conf.logo.height);
  398. that.blitStringImage(conf.logo.data, 0, 0);
  399. } else {
  400. that.resize(640, 20);
  401. c_ctx.clearRect(0, 0, viewport.w, viewport.h);
  402. }
  403. // No benefit over default ("source-over") in Chrome and firefox
  404. //c_ctx.globalCompositeOperation = "copy";
  405. };
  406. that.fillRect = function(x, y, width, height, color) {
  407. setFillColor(color);
  408. c_ctx.fillRect(x - viewport.x, y - viewport.y, width, height);
  409. };
  410. that.copyImage = function(old_x, old_y, new_x, new_y, w, h) {
  411. var x1 = old_x - viewport.x, y1 = old_y - viewport.y,
  412. x2 = new_x - viewport.x, y2 = new_y - viewport.y;
  413. c_ctx.drawImage(conf.target, x1, y1, w, h, x2, y2, w, h);
  414. };
  415. /*
  416. * Tile rendering functions optimized for rendering engines.
  417. *
  418. * - In Chrome/webkit, Javascript image data array manipulations are
  419. * faster than direct Canvas fillStyle, fillRect rendering. In
  420. * gecko, Javascript array handling is much slower.
  421. */
  422. that.getTile = function(x, y, width, height, color) {
  423. var img, data = [], rgb, red, green, blue, i;
  424. img = {'x': x, 'y': y, 'width': width, 'height': height,
  425. 'data': data};
  426. if (conf.prefer_js) {
  427. if (conf.true_color) {
  428. rgb = color;
  429. } else {
  430. rgb = conf.colourMap[color[0]];
  431. }
  432. red = rgb[0];
  433. green = rgb[1];
  434. blue = rgb[2];
  435. for (i = 0; i < (width * height * 4); i+=4) {
  436. data[i ] = red;
  437. data[i + 1] = green;
  438. data[i + 2] = blue;
  439. }
  440. } else {
  441. that.fillRect(x, y, width, height, color);
  442. }
  443. return img;
  444. };
  445. that.setSubTile = function(img, x, y, w, h, color) {
  446. var data, p, rgb, red, green, blue, width, j, i, xend, yend;
  447. if (conf.prefer_js) {
  448. data = img.data;
  449. width = img.width;
  450. if (conf.true_color) {
  451. rgb = color;
  452. } else {
  453. rgb = conf.colourMap[color[0]];
  454. }
  455. red = rgb[0];
  456. green = rgb[1];
  457. blue = rgb[2];
  458. xend = x + w;
  459. yend = y + h;
  460. for (j = y; j < yend; j += 1) {
  461. for (i = x; i < xend; i += 1) {
  462. p = (i + (j * width) ) * 4;
  463. data[p ] = red;
  464. data[p + 1] = green;
  465. data[p + 2] = blue;
  466. }
  467. }
  468. } else {
  469. that.fillRect(img.x + x, img.y + y, w, h, color);
  470. }
  471. };
  472. that.putTile = function(img) {
  473. if (conf.prefer_js) {
  474. c_rgbxImage(img.x, img.y, img.width, img.height, img.data, 0);
  475. }
  476. // else: No-op, under gecko already done by setSubTile
  477. };
  478. imageDataGet = function(width, height) {
  479. return c_ctx.getImageData(0, 0, width, height);
  480. };
  481. imageDataCreate = function(width, height) {
  482. return c_ctx.createImageData(width, height);
  483. };
  484. rgbxImageData = function(x, y, width, height, arr, offset) {
  485. var img, i, j, data, v = viewport;
  486. /*
  487. if ((x - v.x >= v.w) || (y - v.y >= v.h) ||
  488. (x - v.x + width < 0) || (y - v.y + height < 0)) {
  489. //console.log("skipping, out of bounds: ", x, y);
  490. // Skipping because outside of viewport
  491. return;
  492. }
  493. */
  494. img = c_imageData(width, height);
  495. data = img.data;
  496. for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+4) {
  497. data[i ] = arr[j ];
  498. data[i + 1] = arr[j + 1];
  499. data[i + 2] = arr[j + 2];
  500. data[i + 3] = 255; // Set Alpha
  501. }
  502. c_ctx.putImageData(img, x - v.x, y - v.y);
  503. };
  504. // really slow fallback if we don't have imageData
  505. rgbxImageFill = function(x, y, width, height, arr, offset) {
  506. var i, j, sx = 0, sy = 0;
  507. for (i=0, j=offset; i < (width * height); i+=1, j+=4) {
  508. that.fillRect(x+sx, y+sy, 1, 1, [arr[j], arr[j+1], arr[j+2]]);
  509. sx += 1;
  510. if ((sx % width) === 0) {
  511. sx = 0;
  512. sy += 1;
  513. }
  514. }
  515. };
  516. cmapImageData = function(x, y, width, height, arr, offset) {
  517. var img, i, j, data, rgb, cmap;
  518. img = c_imageData(width, height);
  519. data = img.data;
  520. cmap = conf.colourMap;
  521. for (i=0, j=offset; i < (width * height * 4); i+=4, j+=1) {
  522. rgb = cmap[arr[j]];
  523. data[i ] = rgb[0];
  524. data[i + 1] = rgb[1];
  525. data[i + 2] = rgb[2];
  526. data[i + 3] = 255; // Set Alpha
  527. }
  528. c_ctx.putImageData(img, x - viewport.x, y - viewport.y);
  529. };
  530. cmapImageFill = function(x, y, width, height, arr, offset) {
  531. var i, j, sx = 0, sy = 0, cmap;
  532. cmap = conf.colourMap;
  533. for (i=0, j=offset; i < (width * height); i+=1, j+=1) {
  534. that.fillRect(x+sx, y+sy, 1, 1, [arr[j]]);
  535. sx += 1;
  536. if ((sx % width) === 0) {
  537. sx = 0;
  538. sy += 1;
  539. }
  540. }
  541. };
  542. that.blitImage = function(x, y, width, height, arr, offset) {
  543. if (conf.true_color) {
  544. c_rgbxImage(x, y, width, height, arr, offset);
  545. } else {
  546. c_cmapImage(x, y, width, height, arr, offset);
  547. }
  548. };
  549. that.blitStringImage = function(str, x, y) {
  550. var img = new Image();
  551. img.onload = function () {
  552. c_ctx.drawImage(img, x - viewport.x, y - viewport.y);
  553. };
  554. img.src = str;
  555. };
  556. that.changeCursor = function(pixels, mask, hotx, hoty, w, h) {
  557. if (conf.cursor_uri === false) {
  558. Util.Warn("changeCursor called but no cursor data URI support");
  559. return;
  560. }
  561. if (conf.true_color) {
  562. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h);
  563. } else {
  564. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h, conf.colourMap);
  565. }
  566. };
  567. that.defaultCursor = function() {
  568. conf.target.style.cursor = "default";
  569. };
  570. return constructor(); // Return the public API interface
  571. } // End of Display()
  572. /* Set CSS cursor property using data URI encoded cursor file */
  573. function changeCursor(target, pixels, mask, hotx, hoty, w, h, cmap) {
  574. "use strict";
  575. var cur = [], rgb, IHDRsz, RGBsz, ANDsz, XORsz, url, idx, alpha, x, y;
  576. //Util.Debug(">> changeCursor, x: " + hotx + ", y: " + hoty + ", w: " + w + ", h: " + h);
  577. // Push multi-byte little-endian values
  578. cur.push16le = function (num) {
  579. this.push((num ) & 0xFF,
  580. (num >> 8) & 0xFF );
  581. };
  582. cur.push32le = function (num) {
  583. this.push((num ) & 0xFF,
  584. (num >> 8) & 0xFF,
  585. (num >> 16) & 0xFF,
  586. (num >> 24) & 0xFF );
  587. };
  588. IHDRsz = 40;
  589. RGBsz = w * h * 4;
  590. XORsz = Math.ceil( (w * h) / 8.0 );
  591. ANDsz = Math.ceil( (w * h) / 8.0 );
  592. // Main header
  593. cur.push16le(0); // 0: Reserved
  594. cur.push16le(2); // 2: .CUR type
  595. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  596. // Cursor #1 header (ICONDIRENTRY)
  597. cur.push(w); // 6: width
  598. cur.push(h); // 7: height
  599. cur.push(0); // 8: colors, 0 -> true-color
  600. cur.push(0); // 9: reserved
  601. cur.push16le(hotx); // 10: hotspot x coordinate
  602. cur.push16le(hoty); // 12: hotspot y coordinate
  603. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  604. // 14: cursor data byte size
  605. cur.push32le(22); // 18: offset of cursor data in the file
  606. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  607. cur.push32le(IHDRsz); // 22: Infoheader size
  608. cur.push32le(w); // 26: Cursor width
  609. cur.push32le(h*2); // 30: XOR+AND height
  610. cur.push16le(1); // 34: number of planes
  611. cur.push16le(32); // 36: bits per pixel
  612. cur.push32le(0); // 38: Type of compression
  613. cur.push32le(XORsz + ANDsz); // 43: Size of Image
  614. // Gimp leaves this as 0
  615. cur.push32le(0); // 46: reserved
  616. cur.push32le(0); // 50: reserved
  617. cur.push32le(0); // 54: reserved
  618. cur.push32le(0); // 58: reserved
  619. // 62: color data (RGBQUAD icColors[])
  620. for (y = h-1; y >= 0; y -= 1) {
  621. for (x = 0; x < w; x += 1) {
  622. idx = y * Math.ceil(w / 8) + Math.floor(x/8);
  623. alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  624. if (cmap) {
  625. idx = (w * y) + x;
  626. rgb = cmap[pixels[idx]];
  627. cur.push(rgb[2]); // blue
  628. cur.push(rgb[1]); // green
  629. cur.push(rgb[0]); // red
  630. cur.push(alpha); // alpha
  631. } else {
  632. idx = ((w * y) + x) * 4;
  633. cur.push(pixels[idx + 2]); // blue
  634. cur.push(pixels[idx + 1]); // green
  635. cur.push(pixels[idx ]); // red
  636. cur.push(alpha); // alpha
  637. }
  638. }
  639. }
  640. // XOR/bitmask data (BYTE icXOR[])
  641. // (ignored, just needs to be right size)
  642. for (y = 0; y < h; y += 1) {
  643. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  644. cur.push(0x00);
  645. }
  646. }
  647. // AND/bitmask data (BYTE icAND[])
  648. // (ignored, just needs to be right size)
  649. for (y = 0; y < h; y += 1) {
  650. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  651. cur.push(0x00);
  652. }
  653. }
  654. url = "data:image/x-icon;base64," + Base64.encode(cur);
  655. target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
  656. //Util.Debug("<< changeCursor, cur.length: " + cur.length);
  657. }