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