DrawHelper.js 8.4 KB

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