DrawHelper.js 14 KB

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