DrawHelper.js 9.5 KB

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