DrawHelper.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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; vertical-align: middle;">`;
  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; top: ${-this.fontSize*this.textShift}px; position: relative">` +
  158. `<div class="fit row justify-center items-center">` +
  159. `<div style="height: ${Math.round(boxH*this.dualDivHeight/100)}px; width: ${this.dualDivWidth}px;` +
  160. `background-image: url(&quot;data:image/svg+xml;utf8,<svg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'>` +
  161. //`<rect width='100%' height='100%' style='fill: none; stroke: white; stroke-width: 4; stroke-dasharray: 5 20'/>` +
  162. `<line x1='0' y1='0' x2='0' y2='100%' stroke='${this.dualDivColor}' stroke-width='100%' stroke-dasharray='${this.dualDivStrokeFill} ${this.dualDivStrokeGap}'/>` +
  163. `</svg>&quot;);">` +
  164. `</div>` +
  165. `</div>` +
  166. `</div>`;
  167. //правая страница
  168. out += `<div style="width: ${this.w}px; margin-right: ${this.dualIndentLR}px; position: relative;">`;
  169. for (let i = l2; i < len; i++) {
  170. out += this.drawLine(lines[i], i, l2, sel, imageDrawn2);
  171. }
  172. out += '</div>';
  173. }
  174. out += '</div>';
  175. return out;
  176. }
  177. drawPercentBar(x, y, w, h, font, fontSize, bookPos, textLength, imageNum, imageLength) {
  178. const pad = 3;
  179. const fh = h - 2*pad;
  180. const fh2 = fh/2;
  181. const tImg = (imageNum > 0 ? ` (${imageNum}/${imageLength})` : '');
  182. const t1 = `${Math.floor((bookPos + 1)/1000)}/${Math.floor(textLength/1000)}${tImg}`;
  183. const w1 = this.measureTextFont(t1, font) + fh2;
  184. const read = (bookPos + 1)/textLength;
  185. const t2 = `${(read*100).toFixed(2)}%`;
  186. const w2 = this.measureTextFont(t2, font);
  187. let w3 = w - w1 - w2;
  188. let out = '';
  189. if (w1 + w2 <= w)
  190. out += this.fillTextShift(t1, x, y, font, fontSize);
  191. if (w1 + w2 + w3 <= w && w3 > (10 + fh2)) {
  192. const barWidth = w - w1 - w2 - fh2;
  193. out += this.strokeRect(x + w1, y + pad, barWidth, fh - 2, this.statusBarColor);
  194. out += this.fillRect(x + w1 + 2, y + pad + 2, (barWidth - 4)*read, fh - 6, this.statusBarColor);
  195. }
  196. if (w1 <= w)
  197. out += this.fillTextShift(t2, x + w1 + w3, y, font, fontSize);
  198. return out;
  199. }
  200. drawStatusBar(statusBarTop, statusBarHeight, bookPos, textLength, title, imageNum, imageLength) {
  201. let out = `<div class="layout" style="` +
  202. `width: ${this.realWidth}px; height: ${statusBarHeight}px; ` +
  203. `color: ${this.statusBarColor}">`;
  204. const fontSize = statusBarHeight*0.75;
  205. const font = 'bold ' + this.fontBySize(fontSize);
  206. out += this.fillRect(0, (statusBarTop ? statusBarHeight : 0), this.realWidth, 1, this.statusBarColor);
  207. const date = new Date();
  208. const time = `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  209. const timeW = this.measureTextFont(time, font);
  210. out += this.fillTextShift(time, this.realWidth - timeW - fontSize, 2, font, fontSize);
  211. out += this.fillTextShift(this.fittingString(title, this.realWidth/2 - fontSize - 3, font), fontSize, 2, font, fontSize);
  212. out += this.drawPercentBar(this.realWidth/2, 2, this.realWidth/2 - timeW - 2*fontSize, statusBarHeight, font, fontSize, bookPos, textLength, imageNum, imageLength);
  213. out += '</div>';
  214. return out;
  215. }
  216. statusBarClickable(statusBarTop, statusBarHeight) {
  217. return `<div class="layout" style="position: absolute; ` +
  218. `left: 0px; top: ${statusBarTop ? 1 : this.realHeight - statusBarHeight + 1}px; ` +
  219. `width: ${this.realWidth/2}px; height: ${statusBarHeight}px; cursor: pointer"></div>`;
  220. }
  221. fittingString(str, maxWidth, font) {
  222. let w = this.measureTextFont(str, font);
  223. const ellipsis = '…';
  224. const ellipsisWidth = this.measureTextFont(ellipsis, font);
  225. if (w <= maxWidth || w <= ellipsisWidth) {
  226. return str;
  227. } else {
  228. let len = str.length;
  229. while (w >= maxWidth - ellipsisWidth && len-- > 0) {
  230. str = str.substring(0, len);
  231. w = this.measureTextFont(str, font);
  232. }
  233. return str + ellipsis;
  234. }
  235. }
  236. fillTextShift(text, x, y, font, size, css) {
  237. return this.fillText(text, x, y + size*this.fontShift, font, css);
  238. }
  239. fillText(text, x, y, font, css) {
  240. css = (css ? css : '');
  241. return `<div style="position: absolute; white-space: pre; left: ${x}px; top: ${y}px; font: ${font}; ${css}">${text}</div>`;
  242. }
  243. fillRect(x, y, w, h, color) {
  244. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  245. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  246. }
  247. strokeRect(x, y, w, h, color) {
  248. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  249. `width: ${w}px; height: ${h}px; box-sizing: border-box; border: 1px solid ${color}"></div>`;
  250. }
  251. async doPageAnimationThaw(page1, page2, duration, isDown, animation1Finish) {
  252. page1.style.animation = `page1-animation-thaw ${duration}ms ease-in 1`;
  253. page2.style.animation = `page2-animation-thaw ${duration}ms ease-in 1`;
  254. await animation1Finish(duration);
  255. }
  256. async doPageAnimationBlink(page1, page2, duration, isDown, animation1Finish, animation2Finish) {
  257. page1.style.opacity = '0';
  258. page2.style.opacity = '0';
  259. page2.style.animation = `page2-animation-thaw ${duration/2}ms ease-out 1`;
  260. await animation2Finish(duration/2);
  261. page1.style.opacity = '1';
  262. page1.style.animation = `page1-animation-thaw ${duration/2}ms ease-in 1`;
  263. await animation1Finish(duration/2);
  264. page2.style.opacity = '1';
  265. }
  266. async doPageAnimationRightShift(page1, page2, duration, isDown, animation1Finish) {
  267. const s = this.boxW + this.fontSize;
  268. if (isDown) {
  269. page1.style.transform = `translateX(${s}px)`;
  270. await sleep(30);
  271. page1.style.transition = `${duration}ms ease-in-out`;
  272. page1.style.transform = `translateX(0px)`;
  273. page2.style.transition = `${duration}ms ease-in-out`;
  274. page2.style.transform = `translateX(-${s}px)`;
  275. await animation1Finish(duration);
  276. } else {
  277. page1.style.transform = `translateX(-${s}px)`;
  278. await sleep(30);
  279. page1.style.transition = `${duration}ms ease-in-out`;
  280. page1.style.transform = `translateX(0px)`;
  281. page2.style.transition = `${duration}ms ease-in-out`;
  282. page2.style.transform = `translateX(${s}px)`;
  283. await animation1Finish(duration);
  284. }
  285. }
  286. async doPageAnimationDownShift(page1, page2, duration, isDown, animation1Finish) {
  287. const s = this.h + this.fontSize/2;
  288. if (isDown) {
  289. page1.style.transform = `translateY(${s}px)`;
  290. await sleep(30);
  291. page1.style.transition = `${duration}ms ease-in-out`;
  292. page1.style.transform = `translateY(0px)`;
  293. page2.style.transition = `${duration}ms ease-in-out`;
  294. page2.style.transform = `translateY(-${s}px)`;
  295. await animation1Finish(duration);
  296. } else {
  297. page1.style.transform = `translateY(-${s}px)`;
  298. await sleep(30);
  299. page1.style.transition = `${duration}ms ease-in-out`;
  300. page1.style.transform = `translateY(0px)`;
  301. page2.style.transition = `${duration}ms ease-in-out`;
  302. page2.style.transform = `translateY(${s}px)`;
  303. await animation1Finish(duration);
  304. }
  305. }
  306. async doPageAnimationRotate(page1, page2, duration, isDown, animation1Finish, animation2Finish) {
  307. if (isDown) {
  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. } else {
  317. page1.style.transform = `rotateY(-90deg)`;
  318. await sleep(30);
  319. page2.style.transition = `${duration/2}ms ease-in`;
  320. page2.style.transform = `rotateY(90deg)`;
  321. await animation2Finish(duration/2);
  322. page1.style.transition = `${duration/2}ms ease-out`;
  323. page1.style.transform = `rotateY(0deg)`;
  324. await animation1Finish(duration/2);
  325. }
  326. }
  327. async doPageAnimationFlip(page1, page2, duration, isDown, animation1Finish, animation2Finish, backgroundColor) {
  328. page2.style.background = backgroundColor;
  329. if (isDown) {
  330. page2.style.transformOrigin = '5%';
  331. await sleep(30);
  332. page2.style.transition = `${duration}ms ease-in-out`;
  333. page2.style.transform = `rotateY(-120deg) translateX(${this.w/4}px)`;
  334. await animation2Finish(duration);
  335. } else {
  336. page2.style.transformOrigin = '95%';
  337. await sleep(30);
  338. page2.style.transition = `${duration}ms ease-in-out`;
  339. page2.style.transform = `rotateY(120deg) translateX(-${this.w/4}px)`;
  340. await animation2Finish(duration);
  341. }
  342. page2.style.transformOrigin = 'center';
  343. page2.style.background = '';
  344. }
  345. }