DrawHelper.js 17 KB

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