DrawHelper.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import {sleep} from '../../../share/utils';
  2. export default class DrawHelper {
  3. fontBySize(size) {
  4. return `${size}px '${this.fontName}'`;
  5. }
  6. fontByStyle(style) {
  7. return `${style.italic ? 'italic' : this.fontStyle} ${style.bold ? 'bold' : this.fontWeight} ${this.fontSize}px '${this.fontName}'`;
  8. }
  9. measureText(text, style) {// eslint-disable-line no-unused-vars
  10. this.context.font = this.fontByStyle(style);
  11. return this.context.measureText(text).width;
  12. }
  13. measureTextFont(text, font) {// eslint-disable-line no-unused-vars
  14. this.context.font = font;
  15. return this.context.measureText(text).width;
  16. }
  17. drawLine(line, lineIndex, baseLineIndex, sel, imageDrawn) {
  18. /* line:
  19. {
  20. begin: Number,
  21. end: Number,
  22. first: Boolean,
  23. last: Boolean,
  24. parts: array of {
  25. style: {bold: Boolean, italic: Boolean, center: Boolean},
  26. image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number},
  27. text: String,
  28. }
  29. }*/
  30. let out = '<div>';
  31. let lineText = '';
  32. let center = false;
  33. let space = 0;
  34. let j = 0;
  35. //формируем строку
  36. for (const part of line.parts) {
  37. let tOpen = '';
  38. tOpen += (part.style.bold ? '<b>' : '');
  39. tOpen += (part.style.italic ? '<i>' : '');
  40. tOpen += (part.style.sup ? '<span style="vertical-align: baseline; position: relative; line-height: 0; top: -0.3em">' : '');
  41. tOpen += (part.style.sub ? '<span style="vertical-align: baseline; position: relative; line-height: 0; top: 0.3em">' : '');
  42. let tClose = '';
  43. tClose += (part.style.sub ? '</span>' : '');
  44. tClose += (part.style.sup ? '</span>' : '');
  45. tClose += (part.style.italic ? '</i>' : '');
  46. tClose += (part.style.bold ? '</b>' : '');
  47. let text = '';
  48. if (lineIndex == 0 && this.searching) {
  49. for (let k = 0; k < part.text.length; k++) {
  50. text += (sel.has(j) ? `<ins>${part.text[k]}</ins>` : part.text[k]);
  51. j++;
  52. }
  53. } else
  54. text = part.text;
  55. if (text && text.trim() == '')
  56. text = `<span style="white-space: pre">${text}</span>`;
  57. lineText += `${tOpen}${text}${tClose}`;
  58. center = center || part.style.center;
  59. space = (part.style.space > space ? part.style.space : space);
  60. //избражения
  61. //image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number, w: Number, h: Number},
  62. const img = part.image;
  63. if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
  64. const bin = this.parsed.binary[img.id];
  65. if (bin) {
  66. let resize = '';
  67. if (bin.h > img.h) {
  68. resize = `height: ${img.h}px`;
  69. }
  70. const left = (this.w - img.w)/2;
  71. const top = ((img.lineCount*this.lineHeight - img.h)/2) + (lineIndex - baseLineIndex - img.imageLine)*this.lineHeight;
  72. if (img.local) {
  73. lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
  74. } else {
  75. lineText += `<img src="${img.id}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
  76. }
  77. }
  78. imageDrawn.add(img.paraIndex);
  79. }
  80. if (img && img.id && img.inline) {
  81. if (img.local) {
  82. const bin = this.parsed.binary[img.id];
  83. if (bin) {
  84. let resize = '';
  85. if (bin.h > this.fontSize) {
  86. resize = `height: ${this.fontSize - 3}px`;
  87. }
  88. lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
  89. }
  90. } else {
  91. //
  92. }
  93. }
  94. }
  95. const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')
  96. if ((line.first || space) && !center) {
  97. let p = (line.first ? this.p : 0);
  98. p = (space ? p + this.p*space : p);
  99. lineText = `<span style="display: inline-block; margin-left: ${p}px"></span>${lineText}`;
  100. }
  101. if (line.last || center)
  102. lineText = `<span style="display: inline-block; ${centerStyle}">${lineText}</span>`;
  103. out += lineText + '</div>';
  104. return out;
  105. }
  106. drawPage(lines, isScrolling) {
  107. if (!this.lastBook || this.pageLineCount < 1 || !this.book || !lines || !this.parsed.textLength)
  108. return '';
  109. const font = this.fontByStyle({});
  110. const justify = (this.textAlignJustify ? 'text-align: justify; text-align-last: justify;' : '');
  111. const boxH = this.h + (isScrolling ? this.lineHeight : 0);
  112. let out = `<div class="row no-wrap" style="width: ${this.boxW}px; height: ${boxH}px;` +
  113. ` position: absolute; top: ${this.fontSize*this.textShift}px; color: ${this.textColor}; font: ${font}; ${justify}` +
  114. ` line-height: ${this.lineHeight}px; white-space: nowrap;">`;
  115. let imageDrawn1 = new Set();
  116. let imageDrawn2 = new Set();
  117. let len = lines.length;
  118. const lineCount = this.pageLineCount + (isScrolling ? 1 : 0);
  119. len = (len > lineCount ? lineCount : len);
  120. //поиск
  121. let sel = new Set();
  122. if (len > 0 && this.searching) {
  123. const line = lines[0];
  124. let pureText = '';
  125. for (const part of line.parts) {
  126. pureText += part.text;
  127. }
  128. pureText = pureText.toLowerCase();
  129. let j = 0;
  130. while (1) {// eslint-disable-line no-constant-condition
  131. j = pureText.indexOf(this.needle, j);
  132. if (j >= 0) {
  133. for (let k = 0; k < this.needle.length; k++) {
  134. sel.add(j + k);
  135. }
  136. } else
  137. break;
  138. j++;
  139. }
  140. }
  141. //отрисовка строк
  142. if (!this.dualPageMode) {
  143. out += `<div class="fit">`;
  144. for (let i = 0; i < len; i++) {
  145. out += this.drawLine(lines[i], i, 0, sel, imageDrawn1);
  146. }
  147. out += `</div>`;
  148. } else {
  149. //левая страница
  150. out += `<div style="width: ${this.w}px; margin-left: ${this.dualIndentLR}px; position: relative;">`;
  151. const l2 = (this.pageRowsCount > len ? len : this.pageRowsCount);
  152. for (let i = 0; i < l2; i++) {
  153. out += this.drawLine(lines[i], i, 0, sel, imageDrawn1);
  154. }
  155. out += '</div>';
  156. //разделитель
  157. out += `<div style="width: ${this.dualIndentLR*2}px;"></div>`;
  158. //правая страница
  159. out += `<div style="width: ${this.w}px; margin-right: ${this.dualIndentLR}px; position: relative;">`;
  160. for (let i = l2; i < len; i++) {
  161. out += this.drawLine(lines[i], i, l2, sel, imageDrawn2);
  162. }
  163. out += '</div>';
  164. }
  165. out += '</div>';
  166. return out;
  167. }
  168. drawPercentBar(x, y, w, h, font, fontSize, bookPos, textLength, imageNum, imageLength) {
  169. const pad = 3;
  170. const fh = h - 2*pad;
  171. const fh2 = fh/2;
  172. const tImg = (imageNum > 0 ? ` (${imageNum}/${imageLength})` : '');
  173. const t1 = `${Math.floor((bookPos + 1)/1000)}/${Math.floor(textLength/1000)}${tImg}`;
  174. const w1 = this.measureTextFont(t1, font) + fh2;
  175. const read = (bookPos + 1)/textLength;
  176. const t2 = `${(read*100).toFixed(2)}%`;
  177. const w2 = this.measureTextFont(t2, font);
  178. let w3 = w - w1 - w2;
  179. let out = '';
  180. if (w1 + w2 <= w)
  181. out += this.fillTextShift(t1, x, y, font, fontSize);
  182. if (w1 + w2 + w3 <= w && w3 > (10 + fh2)) {
  183. const barWidth = w - w1 - w2 - fh2;
  184. out += this.strokeRect(x + w1, y + pad, barWidth, fh - 2, this.statusBarRgbaColor);
  185. out += this.fillRect(x + w1 + 2, y + pad + 2, (barWidth - 4)*read, fh - 6, this.statusBarRgbaColor);
  186. }
  187. if (w1 <= w)
  188. out += this.fillTextShift(t2, x + w1 + w3, y, font, fontSize);
  189. return out;
  190. }
  191. drawStatusBar(statusBarTop, statusBarHeight, bookPos, textLength, title, imageNum, imageLength) {
  192. let out = `<div class="layout" style="` +
  193. `width: ${this.realWidth}px; height: ${statusBarHeight}px; ` +
  194. `color: ${this.statusBarRgbaColor}">`;
  195. const fontSize = statusBarHeight*0.75;
  196. const font = 'bold ' + this.fontBySize(fontSize);
  197. out += this.fillRect(0, (statusBarTop ? statusBarHeight : 0), this.realWidth, 1, this.statusBarRgbaColor);
  198. const date = new Date();
  199. const time = `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  200. const timeW = this.measureTextFont(time, font);
  201. out += this.fillTextShift(time, this.realWidth - timeW - fontSize, 2, font, fontSize);
  202. out += this.fillTextShift(this.fittingString(title, this.realWidth/2 - fontSize - 3, font), fontSize, 2, font, fontSize);
  203. out += this.drawPercentBar(this.realWidth/2 + fontSize, 2, this.realWidth/2 - timeW - 3*fontSize, statusBarHeight, font, fontSize, bookPos, textLength, imageNum, imageLength);
  204. out += '</div>';
  205. return out;
  206. }
  207. statusBarClickable(statusBarTop, statusBarHeight) {
  208. return `<div class="layout" style="position: absolute; ` +
  209. `left: 0px; top: ${statusBarTop ? 1 : this.realHeight - statusBarHeight + 1}px; ` +
  210. `width: ${this.realWidth/2}px; height: ${statusBarHeight}px; cursor: pointer"></div>`;
  211. }
  212. fittingString(str, maxWidth, font) {
  213. let w = this.measureTextFont(str, font);
  214. const ellipsis = '…';
  215. const ellipsisWidth = this.measureTextFont(ellipsis, font);
  216. if (w <= maxWidth || w <= ellipsisWidth) {
  217. return str;
  218. } else {
  219. let len = str.length;
  220. while (w >= maxWidth - ellipsisWidth && len-- > 0) {
  221. str = str.substring(0, len);
  222. w = this.measureTextFont(str, font);
  223. }
  224. return str + ellipsis;
  225. }
  226. }
  227. fillTextShift(text, x, y, font, size, css) {
  228. return this.fillText(text, x, y + size*this.fontShift, font, css);
  229. }
  230. fillText(text, x, y, font, css) {
  231. css = (css ? css : '');
  232. return `<div style="position: absolute; white-space: pre; left: ${x}px; top: ${y}px; font: ${font}; ${css}">${text}</div>`;
  233. }
  234. fillRect(x, y, w, h, color) {
  235. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  236. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  237. }
  238. strokeRect(x, y, w, h, color) {
  239. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  240. `width: ${w}px; height: ${h}px; box-sizing: border-box; border: 1px solid ${color}"></div>`;
  241. }
  242. async doPageAnimationThaw(page1, page2, duration, isDown, animation1Finish) {
  243. page1.style.animation = `page1-animation-thaw ${duration}ms ease-in 1`;
  244. page2.style.animation = `page2-animation-thaw ${duration}ms ease-in 1`;
  245. await animation1Finish(duration);
  246. }
  247. async doPageAnimationBlink(page1, page2, duration, isDown, animation1Finish, animation2Finish) {
  248. page1.style.opacity = '0';
  249. page2.style.opacity = '0';
  250. page2.style.animation = `page2-animation-thaw ${duration/2}ms ease-out 1`;
  251. await animation2Finish(duration/2);
  252. page1.style.opacity = '1';
  253. page1.style.animation = `page1-animation-thaw ${duration/2}ms ease-in 1`;
  254. await animation1Finish(duration/2);
  255. page2.style.opacity = '1';
  256. }
  257. async doPageAnimationRightShift(page1, page2, duration, isDown, animation1Finish) {
  258. const s = this.boxW + this.fontSize;
  259. if (isDown) {
  260. page1.style.transform = `translateX(${s}px)`;
  261. await sleep(30);
  262. page1.style.transition = `${duration}ms ease-in-out`;
  263. page1.style.transform = `translateX(0px)`;
  264. page2.style.transition = `${duration}ms ease-in-out`;
  265. page2.style.transform = `translateX(-${s}px)`;
  266. await animation1Finish(duration);
  267. } else {
  268. page1.style.transform = `translateX(-${s}px)`;
  269. await sleep(30);
  270. page1.style.transition = `${duration}ms ease-in-out`;
  271. page1.style.transform = `translateX(0px)`;
  272. page2.style.transition = `${duration}ms ease-in-out`;
  273. page2.style.transform = `translateX(${s}px)`;
  274. await animation1Finish(duration);
  275. }
  276. }
  277. async doPageAnimationDownShift(page1, page2, duration, isDown, animation1Finish) {
  278. const s = this.h + this.fontSize/2;
  279. if (isDown) {
  280. page1.style.transform = `translateY(${s}px)`;
  281. await sleep(30);
  282. page1.style.transition = `${duration}ms ease-in-out`;
  283. page1.style.transform = `translateY(0px)`;
  284. page2.style.transition = `${duration}ms ease-in-out`;
  285. page2.style.transform = `translateY(-${s}px)`;
  286. await animation1Finish(duration);
  287. } else {
  288. page1.style.transform = `translateY(-${s}px)`;
  289. await sleep(30);
  290. page1.style.transition = `${duration}ms ease-in-out`;
  291. page1.style.transform = `translateY(0px)`;
  292. page2.style.transition = `${duration}ms ease-in-out`;
  293. page2.style.transform = `translateY(${s}px)`;
  294. await animation1Finish(duration);
  295. }
  296. }
  297. async doPageAnimationRotate(page1, page2, duration, isDown, animation1Finish, animation2Finish) {
  298. if (isDown) {
  299. page1.style.transform = `rotateY(90deg)`;
  300. await sleep(30);
  301. page2.style.transition = `${duration/2}ms ease-in`;
  302. page2.style.transform = `rotateY(-90deg)`;
  303. await animation2Finish(duration/2);
  304. page1.style.transition = `${duration/2}ms ease-out`;
  305. page1.style.transform = `rotateY(0deg)`;
  306. await animation1Finish(duration/2);
  307. } else {
  308. page1.style.transform = `rotateY(-90deg)`;
  309. await sleep(30);
  310. page2.style.transition = `${duration/2}ms ease-in`;
  311. page2.style.transform = `rotateY(90deg)`;
  312. await animation2Finish(duration/2);
  313. page1.style.transition = `${duration/2}ms ease-out`;
  314. page1.style.transform = `rotateY(0deg)`;
  315. await animation1Finish(duration/2);
  316. }
  317. }
  318. async doPageAnimationFlip(page1, page2, duration, isDown, animation1Finish, animation2Finish, backgroundColor) {
  319. page2.style.background = backgroundColor;
  320. if (isDown) {
  321. page2.style.transformOrigin = '5%';
  322. await sleep(30);
  323. page2.style.transition = `${duration}ms ease-in-out`;
  324. page2.style.transform = `rotateY(-120deg) translateX(${this.w/4}px)`;
  325. await animation2Finish(duration);
  326. } else {
  327. page2.style.transformOrigin = '95%';
  328. await sleep(30);
  329. page2.style.transition = `${duration}ms ease-in-out`;
  330. page2.style.transform = `rotateY(120deg) translateX(-${this.w/4}px)`;
  331. await animation2Finish(duration);
  332. }
  333. page2.style.transformOrigin = 'center';
  334. page2.style.background = '';
  335. }
  336. }