DrawHelper.js 15 KB

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