DrawHelper.js 15 KB

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