DrawHelper.js 17 KB

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