DrawHelper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 spaceWidth = this.measureText(' ', {});
  21. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  22. ` color: ${this.textColor}">`;
  23. let len = lines.length;
  24. const lineCount = this.pageLineCount + (isScrolling ? 1 : 0);
  25. len = (len > lineCount ? lineCount : len);
  26. let y = this.fontSize*this.textShift;
  27. for (let i = 0; i < len; i++) {
  28. const line = lines[i];
  29. /* line:
  30. {
  31. begin: Number,
  32. end: Number,
  33. first: Boolean,
  34. last: Boolean,
  35. parts: array of {
  36. style: {bold: Boolean, italic: Boolean, center: Boolean}
  37. text: String,
  38. }
  39. }*/
  40. let indent = line.first ? this.p : 0;
  41. let lineText = '';
  42. let center = false;
  43. let centerStyle = {};
  44. for (const part of line.parts) {
  45. lineText += part.text;
  46. center = center || part.style.center;
  47. if (part.style.center)
  48. centerStyle = part.style;
  49. }
  50. let filled = false;
  51. // если выравнивание по ширине включено
  52. if (this.textAlignJustify && !line.last && !center) {
  53. const words = lineText.split(' ');
  54. if (words.length > 1) {
  55. const spaceCount = words.length - 1;
  56. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  57. let x = indent;
  58. for (const part of line.parts) {
  59. const font = this.fontByStyle(part.style);
  60. let partWords = part.text.split(' ');
  61. for (let j = 0; j < partWords.length; j++) {
  62. let f = font;
  63. let style = part.style;
  64. let word = partWords[j];
  65. if (i == 0 && this.searching && word.toLowerCase().indexOf(this.needle) >= 0) {
  66. style = Object.assign({}, part.style, {bold: true});
  67. f = this.fontByStyle(style);
  68. }
  69. out += this.fillText(word, x, y, f);
  70. x += this.measureText(word, style) + (j < partWords.length - 1 ? space : 0);
  71. }
  72. }
  73. filled = true;
  74. }
  75. }
  76. // просто выводим текст
  77. if (!filled) {
  78. let x = indent;
  79. x = (center ? (this.w - this.measureText(lineText, centerStyle))/2 : x);
  80. for (const part of line.parts) {
  81. let font = this.fontByStyle(part.style);
  82. if (i == 0 && this.searching) {//для поиска, разбивка по словам
  83. let partWords = part.text.split(' ');
  84. for (let j = 0; j < partWords.length; j++) {
  85. let f = font;
  86. let style = part.style;
  87. let word = partWords[j];
  88. if (word.toLowerCase().indexOf(this.needle) >= 0) {
  89. style = Object.assign({}, part.style, {bold: true});
  90. f = this.fontByStyle(style);
  91. }
  92. out += this.fillText(word, x, y, f);
  93. x += this.measureText(word, style) + (j < partWords.length - 1 ? spaceWidth : 0);
  94. }
  95. } else {
  96. out += this.fillText(part.text, x, y, font);
  97. x += this.measureText(part.text, part.style);
  98. }
  99. }
  100. }
  101. y += this.lineHeight;
  102. }
  103. out += '</div>';
  104. return out;
  105. }
  106. drawPercentBar(x, y, w, h, font, fontSize, bookPos, textLength) {
  107. const pad = 3;
  108. const fh = h - 2*pad;
  109. const fh2 = fh/2;
  110. const t1 = `${Math.floor((bookPos + 1)/1000)}k/${Math.floor(textLength/1000)}k`;
  111. const w1 = this.measureTextFont(t1, font) + fh2;
  112. const read = (bookPos + 1)/textLength;
  113. const t2 = `${(read*100).toFixed(2)}%`;
  114. const w2 = this.measureTextFont(t2, font);
  115. let w3 = w - w1 - w2;
  116. let out = '';
  117. if (w1 + w2 <= w)
  118. out += this.fillTextShift(t1, x, y, font, fontSize);
  119. if (w1 + w2 + w3 <= w && w3 > (10 + fh2)) {
  120. const barWidth = w - w1 - w2 - fh2;
  121. out += this.strokeRect(x + w1, y + pad, barWidth, fh - 2, this.statusBarColor);
  122. out += this.fillRect(x + w1 + 2, y + pad + 2, (barWidth - 4)*read, fh - 6, this.statusBarColor);
  123. }
  124. if (w1 <= w)
  125. out += this.fillTextShift(t2, x + w1 + w3, y, font, fontSize);
  126. return out;
  127. }
  128. drawStatusBar(statusBarTop, statusBarHeight, bookPos, textLength, title) {
  129. let out = `<div class="layout" style="` +
  130. `width: ${this.realWidth}px; height: ${statusBarHeight}px; ` +
  131. `color: ${this.statusBarColor}">`;
  132. const fontSize = statusBarHeight*0.75;
  133. const font = 'bold ' + this.fontBySize(fontSize);
  134. out += this.fillRect(0, (statusBarTop ? statusBarHeight : 0), this.realWidth, 1, this.statusBarColor);
  135. const date = new Date();
  136. const time = `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  137. const timeW = this.measureTextFont(time, font);
  138. out += this.fillTextShift(time, this.realWidth - timeW - fontSize, 2, font, fontSize);
  139. out += this.fillTextShift(this.fittingString(title, this.realWidth/2 - fontSize - 3, font), fontSize, 2, font, fontSize);
  140. out += this.drawPercentBar(this.realWidth/2, 2, this.realWidth/2 - timeW - 2*fontSize, statusBarHeight, font, fontSize, bookPos, textLength);
  141. out += '</div>';
  142. return out;
  143. }
  144. statusBarClickable(statusBarTop, statusBarHeight) {
  145. return `<div class="layout" style="position: absolute; ` +
  146. `left: 0px; top: ${statusBarTop ? 1 : this.realHeight - statusBarHeight + 1}px; ` +
  147. `width: ${this.realWidth/2}px; height: ${statusBarHeight}px; cursor: pointer"></div>`;
  148. }
  149. fittingString(str, maxWidth, font) {
  150. let w = this.measureTextFont(str, font);
  151. const ellipsis = '…';
  152. const ellipsisWidth = this.measureTextFont(ellipsis, font);
  153. if (w <= maxWidth || w <= ellipsisWidth) {
  154. return str;
  155. } else {
  156. let len = str.length;
  157. while (w >= maxWidth - ellipsisWidth && len-- > 0) {
  158. str = str.substring(0, len);
  159. w = this.measureTextFont(str, font);
  160. }
  161. return str + ellipsis;
  162. }
  163. }
  164. fillTextShift(text, x, y, font, size, css) {
  165. return this.fillText(text, x, y + size*this.fontShift, font, css);
  166. }
  167. fillText(text, x, y, font, css) {
  168. css = (css ? css : '');
  169. return `<div style="position: absolute; white-space: pre; left: ${x}px; top: ${y}px; font: ${font}; ${css}">${text}</div>`;
  170. }
  171. fillRect(x, y, w, h, color) {
  172. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  173. `width: ${w}px; height: ${h}px; background-color: ${color}"></div>`;
  174. }
  175. strokeRect(x, y, w, h, color) {
  176. return `<div style="position: absolute; left: ${x}px; top: ${y}px; ` +
  177. `width: ${w}px; height: ${h}px; box-sizing: border-box; border: 1px solid ${color}"></div>`;
  178. }
  179. async doPageAnimationThaw(page1, page2, duration, isDown, animation1Finish) {
  180. page1.style.animation = `page1-animation-thaw ${duration}ms ease-in 1`;
  181. page2.style.animation = `page2-animation-thaw ${duration}ms ease-in 1`;
  182. await animation1Finish(duration);
  183. }
  184. async doPageAnimationBlink(page1, page2, duration, isDown, animation1Finish, animation2Finish) {
  185. page1.style.opacity = '0';
  186. page2.style.opacity = '0';
  187. page2.style.animation = `page2-animation-thaw ${duration/2}ms ease-out 1`;
  188. await animation2Finish(duration/2);
  189. page1.style.opacity = '1';
  190. page1.style.animation = `page1-animation-thaw ${duration/2}ms ease-in 1`;
  191. await animation1Finish(duration/2);
  192. page2.style.opacity = '1';
  193. }
  194. async doPageAnimationRightShift(page1, page2, duration, isDown, animation1Finish) {
  195. const s = this.w + this.fontSize;
  196. if (isDown) {
  197. page1.style.transform = `translateX(${s}px)`;
  198. await sleep(30);
  199. page1.style.transition = `${duration}ms ease-in-out`;
  200. page1.style.transform = `translateX(0px)`;
  201. page2.style.transition = `${duration}ms ease-in-out`;
  202. page2.style.transform = `translateX(-${s}px)`;
  203. await animation1Finish(duration);
  204. } else {
  205. page1.style.transform = `translateX(-${s}px)`;
  206. await sleep(30);
  207. page1.style.transition = `${duration}ms ease-in-out`;
  208. page1.style.transform = `translateX(0px)`;
  209. page2.style.transition = `${duration}ms ease-in-out`;
  210. page2.style.transform = `translateX(${s}px)`;
  211. await animation1Finish(duration);
  212. }
  213. }
  214. async doPageAnimationDownShift(page1, page2, duration, isDown, animation1Finish) {
  215. const s = this.h + this.fontSize/2;
  216. if (isDown) {
  217. page1.style.transform = `translateY(${s}px)`;
  218. await sleep(30);
  219. page1.style.transition = `${duration}ms ease-in-out`;
  220. page1.style.transform = `translateY(0px)`;
  221. page2.style.transition = `${duration}ms ease-in-out`;
  222. page2.style.transform = `translateY(-${s}px)`;
  223. await animation1Finish(duration);
  224. } else {
  225. page1.style.transform = `translateY(-${s}px)`;
  226. await sleep(30);
  227. page1.style.transition = `${duration}ms ease-in-out`;
  228. page1.style.transform = `translateY(0px)`;
  229. page2.style.transition = `${duration}ms ease-in-out`;
  230. page2.style.transform = `translateY(${s}px)`;
  231. await animation1Finish(duration);
  232. }
  233. }
  234. }