From 5afb147034e393c80eb7fdaa024272de1aa99ffb Mon Sep 17 00:00:00 2001 From: Maxim Kadushkin Date: Wed, 25 May 2022 14:58:36 +0300 Subject: [PATCH] [mobile] refactoring --- apps/common/mobile/utils/utils.js | 98 ++++++++++++++ .../mobile/src/store/textSettings.js | 121 +----------------- .../mobile/src/store/textSettings.js | 121 +----------------- .../mobile/src/store/textSettings.js | 121 +----------------- 4 files changed, 101 insertions(+), 360 deletions(-) diff --git a/apps/common/mobile/utils/utils.js b/apps/common/mobile/utils/utils.js index 5a3481c5b6..af586a73bd 100644 --- a/apps/common/mobile/utils/utils.js +++ b/apps/common/mobile/utils/utils.js @@ -127,4 +127,102 @@ define([ }, 500); } }; + + Common.Utils.CThumbnailLoader = function () { + this.image = null; + this.binaryFormat = null; + this.data = null; + this.width = 0; + this.height = 0; + this.heightOne = 0; + this.count = 0; + + this.load = function(url, callback) { + if (!callback) + return; + + var me = this; + var xhr = new XMLHttpRequest(); + xhr.open('GET', url + ".bin", true); + xhr.responseType = 'arraybuffer'; + + if (xhr.overrideMimeType) + xhr.overrideMimeType('text/plain; charset=x-user-defined'); + else xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); + + xhr.onload = function() { + // TODO: check errors + me.binaryFormat = this.response; + callback(); + }; + + xhr.send(null); + }; + + this.openBinary = function(arrayBuffer) { + //var t1 = performance.now(); + + const binaryAlpha = new Uint8Array(arrayBuffer); + this.width = (binaryAlpha[0] << 24) | (binaryAlpha[1] << 16) | (binaryAlpha[2] << 8) | (binaryAlpha[3] << 0); + this.heightOne = (binaryAlpha[4] << 24) | (binaryAlpha[5] << 16) | (binaryAlpha[6] << 8) | (binaryAlpha[7] << 0); + this.count = (binaryAlpha[8] << 24) | (binaryAlpha[9] << 16) | (binaryAlpha[10] << 8) | (binaryAlpha[11] << 0); + this.height = this.count * this.heightOne; + + this.data = new Uint8ClampedArray(4 * this.width * this.height); + + var binaryIndex = 12; + var imagePixels = this.data; + var index = 0; + + var len0 = 0; + var tmpValue = 0; + while (binaryIndex < binaryAlpha.length) { + tmpValue = binaryAlpha[binaryIndex++]; + if (0 == tmpValue) { + len0 = binaryAlpha[binaryIndex++]; + while (len0 > 0) { + len0--; + imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255; + imagePixels[index + 3] = 0; // this value is already 0. + index += 4; + } + } else { + imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255 - tmpValue; + imagePixels[index + 3] = tmpValue; + index += 4; + } + } + + //var t2 = performance.now(); + //console.log(t2 - t1); + }; + + this.getImage = function(index, canvas, ctx) { + //var t1 = performance.now(); + if (!canvas) { + canvas = document.createElement("canvas"); + canvas.width = this.width; + canvas.height = this.heightOne; + canvas.style.width = iconWidth + "px"; + canvas.style.height = iconHeight + "px"; + + ctx = canvas.getContext("2d"); + } + + if (!this.data) { + this.openBinary(this.binaryFormat); + delete this.binaryFormat; + } + + var dataTmp = ctx.createImageData(this.width, this.heightOne); + const sizeImage = 4 * this.width * this.heightOne; + dataTmp.data.set(new Uint8ClampedArray(this.data.buffer, index * sizeImage, sizeImage)); + ctx.putImageData(dataTmp, 0, 0); + + //var t2 = performance.now(); + //console.log(t2 - t1); + + return canvas; + }; + }; }); diff --git a/apps/documenteditor/mobile/src/store/textSettings.js b/apps/documenteditor/mobile/src/store/textSettings.js index 156f407028..6240b3baf5 100644 --- a/apps/documenteditor/mobile/src/store/textSettings.js +++ b/apps/documenteditor/mobile/src/store/textSettings.js @@ -129,126 +129,7 @@ export class storeTextSettings { } loadSprite() { - function CThumbnailLoader() { - this.supportBinaryFormat = (window['AscDesktopEditor'] && !window['AscDesktopEditor']['isSupportBinaryFontsSprite']) ? false : true; - - this.image = null; - this.binaryFormat = null; - this.data = null; - this.width = 0; - this.height = 0; - this.heightOne = 0; - this.count = 0; - - this.load = function(url, callback) { - if (!callback) - return; - - if (!this.supportBinaryFormat) { - this.width = thumbs[thumbIdx].width; - this.heightOne = thumbs[thumbIdx].height; - - this.image = new Image(); - this.image.onload = callback; - this.image.src = thumbs[thumbIdx].path; - } else { - var me = this; - var xhr = new XMLHttpRequest(); - xhr.open('GET', url + ".bin", true); - xhr.responseType = 'arraybuffer'; - - if (xhr.overrideMimeType) - xhr.overrideMimeType('text/plain; charset=x-user-defined'); - else - xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); - - xhr.onload = function() { - // TODO: check errors - me.binaryFormat = this.response; - callback(); - }; - - xhr.send(null); - } - }; - - this.openBinary = function(arrayBuffer) { - - //var t1 = performance.now(); - - var binaryAlpha = new Uint8Array(arrayBuffer); - this.width = (binaryAlpha[0] << 24) | (binaryAlpha[1] << 16) | (binaryAlpha[2] << 8) | (binaryAlpha[3] << 0); - this.heightOne = (binaryAlpha[4] << 24) | (binaryAlpha[5] << 16) | (binaryAlpha[6] << 8) | (binaryAlpha[7] << 0); - this.count = (binaryAlpha[8] << 24) | (binaryAlpha[9] << 16) | (binaryAlpha[10] << 8) | (binaryAlpha[11] << 0); - this.height = this.count * this.heightOne; - - this.data = new Uint8ClampedArray(4 * this.width * this.height); - - var binaryIndex = 12; - var binaryLen = binaryAlpha.length; - var imagePixels = this.data; - var index = 0; - - var len0 = 0; - var tmpValue = 0; - while (binaryIndex < binaryLen) { - tmpValue = binaryAlpha[binaryIndex++]; - if (0 == tmpValue) { - len0 = binaryAlpha[binaryIndex++]; - while (len0 > 0) { - len0--; - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255; - imagePixels[index + 3] = 0; // this value is already 0. - index += 4; - } - } else { - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255 - tmpValue; - imagePixels[index + 3] = tmpValue; - index += 4; - } - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - }; - - this.getImage = function(index, canvas, ctx) { - - //var t1 = performance.now(); - if (!canvas) - { - canvas = document.createElement("canvas"); - canvas.width = this.width; - canvas.height = this.heightOne; - canvas.style.width = iconWidth + "px"; - canvas.style.height = iconHeight + "px"; - - ctx = canvas.getContext("2d"); - } - - if (this.supportBinaryFormat) { - if (!this.data) { - this.openBinary(this.binaryFormat); - delete this.binaryFormat; - } - - var dataTmp = ctx.createImageData(this.width, this.heightOne); - var sizeImage = 4 * this.width * this.heightOne; - dataTmp.data.set(new Uint8ClampedArray(this.data.buffer, index * sizeImage, sizeImage)); - ctx.putImageData(dataTmp, 0, 0); - } else { - ctx.clearRect(0, 0, this.width, this.heightOne); - ctx.drawImage(this.image, 0, -this.heightOne * index); - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - - return canvas; - }; - } - - this.spriteThumbs = new CThumbnailLoader(); + this.spriteThumbs = new Common.Utils.CThumbnailLoader(); this.spriteThumbs.load(this.thumbs[this.thumbIdx].path, () => { this.spriteCols = Math.floor(this.spriteThumbs.width / (this.thumbs[this.thumbIdx].width)) || 1; }); diff --git a/apps/presentationeditor/mobile/src/store/textSettings.js b/apps/presentationeditor/mobile/src/store/textSettings.js index ccf03a07ba..16c811a14e 100644 --- a/apps/presentationeditor/mobile/src/store/textSettings.js +++ b/apps/presentationeditor/mobile/src/store/textSettings.js @@ -133,126 +133,7 @@ export class storeTextSettings { } loadSprite() { - function CThumbnailLoader() { - this.supportBinaryFormat = (window['AscDesktopEditor'] && !window['AscDesktopEditor']['isSupportBinaryFontsSprite']) ? false : true; - - this.image = null; - this.binaryFormat = null; - this.data = null; - this.width = 0; - this.height = 0; - this.heightOne = 0; - this.count = 0; - - this.load = function(url, callback) { - if (!callback) - return; - - if (!this.supportBinaryFormat) { - this.width = thumbs[thumbIdx].width; - this.heightOne = thumbs[thumbIdx].height; - - this.image = new Image(); - this.image.onload = callback; - this.image.src = thumbs[thumbIdx].path; - } else { - var me = this; - var xhr = new XMLHttpRequest(); - xhr.open('GET', url + ".bin", true); - xhr.responseType = 'arraybuffer'; - - if (xhr.overrideMimeType) - xhr.overrideMimeType('text/plain; charset=x-user-defined'); - else - xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); - - xhr.onload = function() { - // TODO: check errors - me.binaryFormat = this.response; - callback(); - }; - - xhr.send(null); - } - }; - - this.openBinary = function(arrayBuffer) { - - //var t1 = performance.now(); - - var binaryAlpha = new Uint8Array(arrayBuffer); - this.width = (binaryAlpha[0] << 24) | (binaryAlpha[1] << 16) | (binaryAlpha[2] << 8) | (binaryAlpha[3] << 0); - this.heightOne = (binaryAlpha[4] << 24) | (binaryAlpha[5] << 16) | (binaryAlpha[6] << 8) | (binaryAlpha[7] << 0); - this.count = (binaryAlpha[8] << 24) | (binaryAlpha[9] << 16) | (binaryAlpha[10] << 8) | (binaryAlpha[11] << 0); - this.height = this.count * this.heightOne; - - this.data = new Uint8ClampedArray(4 * this.width * this.height); - - var binaryIndex = 12; - var binaryLen = binaryAlpha.length; - var imagePixels = this.data; - var index = 0; - - var len0 = 0; - var tmpValue = 0; - while (binaryIndex < binaryLen) { - tmpValue = binaryAlpha[binaryIndex++]; - if (0 == tmpValue) { - len0 = binaryAlpha[binaryIndex++]; - while (len0 > 0) { - len0--; - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255; - imagePixels[index + 3] = 0; // this value is already 0. - index += 4; - } - } else { - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255 - tmpValue; - imagePixels[index + 3] = tmpValue; - index += 4; - } - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - }; - - this.getImage = function(index, canvas, ctx) { - - //var t1 = performance.now(); - if (!canvas) - { - canvas = document.createElement("canvas"); - canvas.width = this.width; - canvas.height = this.heightOne; - canvas.style.width = iconWidth + "px"; - canvas.style.height = iconHeight + "px"; - - ctx = canvas.getContext("2d"); - } - - if (this.supportBinaryFormat) { - if (!this.data) { - this.openBinary(this.binaryFormat); - delete this.binaryFormat; - } - - var dataTmp = ctx.createImageData(this.width, this.heightOne); - var sizeImage = 4 * this.width * this.heightOne; - dataTmp.data.set(new Uint8ClampedArray(this.data.buffer, index * sizeImage, sizeImage)); - ctx.putImageData(dataTmp, 0, 0); - } else { - ctx.clearRect(0, 0, this.width, this.heightOne); - ctx.drawImage(this.image, 0, -this.heightOne * index); - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - - return canvas; - }; - } - - this.spriteThumbs = new CThumbnailLoader(); + this.spriteThumbs = new Common.Utils.CThumbnailLoader(); this.spriteThumbs.load(this.thumbs[this.thumbIdx].path, () => { this.spriteCols = Math.floor(this.spriteThumbs.width / (this.thumbs[this.thumbIdx].width)) || 1; }); diff --git a/apps/spreadsheeteditor/mobile/src/store/textSettings.js b/apps/spreadsheeteditor/mobile/src/store/textSettings.js index 25d7c1d460..21e54e9027 100644 --- a/apps/spreadsheeteditor/mobile/src/store/textSettings.js +++ b/apps/spreadsheeteditor/mobile/src/store/textSettings.js @@ -133,126 +133,7 @@ export class storeTextSettings { } loadSprite() { - function CThumbnailLoader() { - this.supportBinaryFormat = (window['AscDesktopEditor'] && !window['AscDesktopEditor']['isSupportBinaryFontsSprite']) ? false : true; - - this.image = null; - this.binaryFormat = null; - this.data = null; - this.width = 0; - this.height = 0; - this.heightOne = 0; - this.count = 0; - - this.load = function(url, callback) { - if (!callback) - return; - - if (!this.supportBinaryFormat) { - this.width = thumbs[thumbIdx].width; - this.heightOne = thumbs[thumbIdx].height; - - this.image = new Image(); - this.image.onload = callback; - this.image.src = thumbs[thumbIdx].path; - } else { - var me = this; - var xhr = new XMLHttpRequest(); - xhr.open('GET', url + ".bin", true); - xhr.responseType = 'arraybuffer'; - - if (xhr.overrideMimeType) - xhr.overrideMimeType('text/plain; charset=x-user-defined'); - else - xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); - - xhr.onload = function() { - // TODO: check errors - me.binaryFormat = this.response; - callback(); - }; - - xhr.send(null); - } - }; - - this.openBinary = function(arrayBuffer) { - - //var t1 = performance.now(); - - var binaryAlpha = new Uint8Array(arrayBuffer); - this.width = (binaryAlpha[0] << 24) | (binaryAlpha[1] << 16) | (binaryAlpha[2] << 8) | (binaryAlpha[3] << 0); - this.heightOne = (binaryAlpha[4] << 24) | (binaryAlpha[5] << 16) | (binaryAlpha[6] << 8) | (binaryAlpha[7] << 0); - this.count = (binaryAlpha[8] << 24) | (binaryAlpha[9] << 16) | (binaryAlpha[10] << 8) | (binaryAlpha[11] << 0); - this.height = this.count * this.heightOne; - - this.data = new Uint8ClampedArray(4 * this.width * this.height); - - var binaryIndex = 12; - var binaryLen = binaryAlpha.length; - var imagePixels = this.data; - var index = 0; - - var len0 = 0; - var tmpValue = 0; - while (binaryIndex < binaryLen) { - tmpValue = binaryAlpha[binaryIndex++]; - if (0 == tmpValue) { - len0 = binaryAlpha[binaryIndex++]; - while (len0 > 0) { - len0--; - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255; - imagePixels[index + 3] = 0; // this value is already 0. - index += 4; - } - } else { - imagePixels[index] = imagePixels[index + 1] = imagePixels[index + 2] = 255 - tmpValue; - imagePixels[index + 3] = tmpValue; - index += 4; - } - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - }; - - this.getImage = function(index, canvas, ctx) { - - //var t1 = performance.now(); - if (!canvas) - { - canvas = document.createElement("canvas"); - canvas.width = this.width; - canvas.height = this.heightOne; - canvas.style.width = iconWidth + "px"; - canvas.style.height = iconHeight + "px"; - - ctx = canvas.getContext("2d"); - } - - if (this.supportBinaryFormat) { - if (!this.data) { - this.openBinary(this.binaryFormat); - delete this.binaryFormat; - } - - var dataTmp = ctx.createImageData(this.width, this.heightOne); - var sizeImage = 4 * this.width * this.heightOne; - dataTmp.data.set(new Uint8ClampedArray(this.data.buffer, index * sizeImage, sizeImage)); - ctx.putImageData(dataTmp, 0, 0); - } else { - ctx.clearRect(0, 0, this.width, this.heightOne); - ctx.drawImage(this.image, 0, -this.heightOne * index); - } - - //var t2 = performance.now(); - //console.log(t2 - t1); - - return canvas; - }; - } - - this.spriteThumbs = new CThumbnailLoader(); + this.spriteThumbs = new Common.Utils.CThumbnailLoader(); this.spriteThumbs.load(this.thumbs[this.thumbIdx].path, () => { this.spriteCols = Math.floor(this.spriteThumbs.width / (this.thumbs[this.thumbIdx].width)) || 1; });