TextPage.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <template>
  2. <div ref="main" class="main">
  3. <canvas ref="canvas" class="canvas"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. //-----------------------------------------------------------------------------
  8. import Vue from 'vue';
  9. import Component from 'vue-class-component';
  10. import _ from 'lodash';
  11. import bookManager from '../share/bookManager';
  12. export default @Component({
  13. watch: {
  14. bookPos: function(newValue) {
  15. this.debouncedEmitPosChange(newValue);
  16. this.drawPage();
  17. },
  18. },
  19. })
  20. class TextPage extends Vue {
  21. lastBook = null;
  22. bookPos = 0;
  23. //убрать
  24. meta = null;
  25. items = null;
  26. created() {
  27. this.commit = this.$store.commit;
  28. this.dispatch = this.$store.dispatch;
  29. this.config = this.$store.state.config;
  30. this.reader = this.$store.state.reader;
  31. this.debouncedEmitPosChange = _.debounce((newValue) => {
  32. this.$emit('book-pos-changed', {bookPos: newValue});
  33. }, 100);
  34. window.addEventListener('resize', () => {
  35. this.onResize();
  36. });
  37. }
  38. mounted() {
  39. this.canvas = this.$refs.canvas;
  40. this.context = this.canvas.getContext('2d');
  41. this.context.textAlign = 'left';
  42. this.context.textBaseline = 'bottom';
  43. }
  44. async calcDrawProps() {
  45. this.canvas.width = this.$refs.main.clientWidth;
  46. this.canvas.height = this.$refs.main.clientHeight;
  47. this.lineHeight = this.fontSize + this.lineInterval;
  48. this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
  49. this.w = this.canvas.width - 2*this.indent;
  50. if (this.parsed) {
  51. this.parsed.p = this.p;
  52. this.parsed.w = this.w;// px, ширина текста
  53. this.parsed.font = this.font;
  54. this.parsed.wordWrap = this.wordWrap;
  55. this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  56. this.context.font = this.fontByStyle(style);
  57. return this.context.measureText(text).width;
  58. };
  59. this.parsed.measureText = this.measureText;
  60. }
  61. }
  62. async loadFonts() {
  63. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  64. if (loaded.some(r => !r)) {
  65. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  66. if (loaded.some(r => !r.length))
  67. throw new Error('some font not loaded');
  68. }
  69. }
  70. showBook() {
  71. this.$refs.main.focus();
  72. this.book = null;
  73. this.meta = null;
  74. this.fb2 = null;
  75. this.parsed = null;
  76. this.linesUp = null;
  77. this.linesDown = null;
  78. //preloaded fonts
  79. this.fontList = ['10px ReaderDefaultFont'];
  80. //draw props
  81. this.textColor = 'black';
  82. this.backgroundColor = '#478355';
  83. this.fontStyle = '';// 'bold','italic'
  84. this.fontSize = 40;// px
  85. this.fontName = 'ReaderDefaultFont';
  86. this.lineInterval = 15;// px, межстрочный интервал
  87. this.textAlignJustify = true;// выравнивание по ширине
  88. this.p = 60;// px, отступ параграфа
  89. this.indent = 20;// px, отступ всего текста слева и справа
  90. this.wordWrap = true;
  91. this.calcDrawProps();
  92. this.drawPage();// пока не загрузили, очистим канвас
  93. if (this.lastBook) {
  94. (async() => {
  95. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  96. if (!isParsed) {
  97. return;
  98. }
  99. this.book = await bookManager.getBook(this.lastBook);
  100. this.meta = bookManager.metaOnly(this.book);
  101. this.fb2 = this.meta.fb2;
  102. this.$root.$emit('set-app-title', _.compact([
  103. this.fb2.lastName,
  104. this.fb2.middleName,
  105. this.fb2.firstName,
  106. '-',
  107. this.fb2.bookTitle
  108. ]).join(' '));
  109. const parsed = this.book.parsed;
  110. this.parsed = parsed;
  111. this.calcDrawProps();
  112. await this.loadFonts();
  113. this.drawPage();
  114. })();
  115. }
  116. }
  117. onResize() {
  118. this.calcDrawProps();
  119. this.drawPage();
  120. }
  121. get font() {
  122. return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  123. }
  124. fontByStyle(style) {
  125. return `${style.italic ? 'italic' : ''} ${style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
  126. }
  127. drawPage() {
  128. if (!this.lastBook)
  129. return;
  130. //пустой канвас
  131. const canvas = this.canvas;
  132. const context = this.context;
  133. context.fillStyle = this.backgroundColor;
  134. context.fillRect(0, 0, canvas.width, canvas.height);
  135. if (!this.book)
  136. return;
  137. context.font = this.font;
  138. context.fillStyle = this.textColor;
  139. const spaceWidth = this.context.measureText(' ').width;
  140. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  141. let len = lines.length;
  142. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  143. let y = 0;
  144. for (let i = 0; i < len; i++) {
  145. const line = lines[i];
  146. /* line:
  147. {
  148. begin: Number,
  149. end: Number,
  150. first: Boolean,
  151. last: Boolean,
  152. parts: array of {
  153. style: {bold: Boolean, italic: Boolean}
  154. text: String,
  155. }
  156. }*/
  157. let indent = this.indent + (line.first ? this.p : 0);
  158. y += this.lineHeight;
  159. let filled = false;
  160. if (this.textAlignJustify && !line.last) {
  161. let lineText = '';
  162. for (const part of line.parts) {
  163. lineText += part.text;
  164. }
  165. const words = lineText.split(' ');
  166. if (words.length > 1) {
  167. const spaceCount = words.length - 1;
  168. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  169. let x = indent;
  170. for (const part of line.parts) {
  171. context.font = this.fontByStyle(part.style);
  172. let partWords = part.text.split(' ');
  173. for (let i = 0; i < partWords.length; i++) {
  174. let word = partWords[i];
  175. context.fillText(word, x, y);
  176. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  177. }
  178. }
  179. filled = true;
  180. }
  181. }
  182. if (!filled) {
  183. let x = indent;
  184. for (const part of line.parts) {
  185. let text = part.text;
  186. context.font = this.fontByStyle(part.style);
  187. context.fillText(text, x, y);
  188. x += this.measureText(text, part.style);
  189. }
  190. }
  191. }
  192. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  193. this.linesDown = lines;
  194. }
  195. doDown() {
  196. if (this.linesDown && this.linesDown.length > 1) {
  197. this.bookPos = this.linesDown[1].begin;
  198. }
  199. }
  200. doUp() {
  201. if (this.linesUp && this.linesUp.length > 1) {
  202. this.bookPos = this.linesUp[1].begin;
  203. }
  204. }
  205. doPageDown() {
  206. if (this.linesDown) {
  207. let i = this.pageLineCount;
  208. i--;
  209. if (i >= 0 && this.linesDown.length > i) {
  210. this.bookPos = this.linesDown[i].begin;
  211. }
  212. }
  213. }
  214. doPageUp() {
  215. if (this.linesUp) {
  216. let i = this.pageLineCount;
  217. i--;
  218. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  219. if (i >= 0 && this.linesUp.length > i) {
  220. this.bookPos = this.linesUp[i].begin;
  221. }
  222. }
  223. }
  224. doHome() {
  225. this.bookPos = 0;
  226. }
  227. doEnd() {
  228. if (this.parsed.para.length) {
  229. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  230. this.bookPos = lastPara.offset + lastPara.length - 1;
  231. }
  232. }
  233. keyHook(event) {
  234. if (event.type == 'keydown') {
  235. switch (event.code) {
  236. case 'ArrowDown':
  237. this.doDown();
  238. break;
  239. case 'ArrowUp':
  240. this.doUp();
  241. break;
  242. case 'PageDown':
  243. case 'ArrowRight':
  244. case 'Enter':
  245. case 'Space':
  246. this.doPageDown();
  247. break;
  248. case 'PageUp':
  249. case 'ArrowLeft':
  250. case 'Backspace':
  251. this.doPageUp();
  252. break;
  253. case 'Home':
  254. this.doHome();
  255. break;
  256. case 'End':
  257. this.doEnd();
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. //-----------------------------------------------------------------------------
  264. </script>
  265. <style scoped>
  266. .main {
  267. flex: 1;
  268. margin: 0;
  269. padding: 0;
  270. overflow: hidden;
  271. }
  272. .canvas {
  273. margin: 0;
  274. padding: 0;
  275. }
  276. </style>