DrawHelper.js 9.1 KB

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