TextPage.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  43. calcDrawProps() {
  44. this.canvas.width = this.$refs.main.clientWidth;
  45. this.canvas.height = this.$refs.main.clientHeight;
  46. this.lineHeight = this.fontSize + this.lineInterval;
  47. this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
  48. this.w = this.canvas.width - 2*this.indent;
  49. if (this.parsed) {
  50. this.parsed.p = this.p;
  51. this.parsed.w = this.w;// px, ширина текста
  52. this.parsed.font = this.font;
  53. this.parsed.wordWrap = this.wordWrap;
  54. this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  55. return this.context.measureText(text).width;
  56. };
  57. this.parsed.measureText = this.measureText;
  58. }
  59. }
  60. showBook() {
  61. this.$refs.main.focus();
  62. this.book = null;
  63. this.meta = null;
  64. this.fb2 = null;
  65. this.parsed = null;
  66. this.linesUp = null;
  67. this.linesDown = null;
  68. //draw props
  69. this.textColor = 'black';
  70. this.backgroundColor = '#478355';
  71. this.fontStyle = '';// 'bold','italic'
  72. this.fontSize = 40;// px
  73. this.fontName = 'arial';
  74. this.lineInterval = 5;// px, межстрочный интервал
  75. this.textAlignJustify = true;// выравнивание по ширине
  76. this.p = 30;// px, отступ параграфа
  77. this.indent = 20;// px, отступ всего текста слева и справа
  78. this.wordWrap = true;
  79. this.calcDrawProps();
  80. this.drawPage();// пока не загрузили, очистим канвас
  81. if (this.lastBook) {
  82. (async() => {
  83. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  84. if (!isParsed) {
  85. return;
  86. }
  87. this.book = await bookManager.getBook(this.lastBook);
  88. this.meta = bookManager.metaOnly(this.book);
  89. this.fb2 = this.meta.fb2;
  90. this.$root.$emit('set-app-title', _.compact([
  91. this.fb2.lastName,
  92. this.fb2.middleName,
  93. this.fb2.firstName,
  94. '-',
  95. this.fb2.bookTitle
  96. ]).join(' '));
  97. const parsed = this.book.parsed;
  98. this.parsed = parsed;
  99. this.calcDrawProps();
  100. this.drawPage();
  101. })();
  102. }
  103. }
  104. onResize() {
  105. this.calcDrawProps();
  106. this.drawPage();
  107. }
  108. get font() {
  109. return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  110. }
  111. drawPage() {
  112. if (!this.lastBook)
  113. return;
  114. //пустой канвас
  115. const canvas = this.canvas;
  116. const context = this.context;
  117. context.fillStyle = this.backgroundColor;
  118. context.fillRect(0, 0, canvas.width, canvas.height);
  119. if (!this.book)
  120. return;
  121. context.font = this.font;
  122. context.fillStyle = this.textColor;
  123. const spaceWidth = this.context.measureText(' ').width;
  124. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  125. let len = lines.length;
  126. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  127. let y = 0;
  128. for (let i = 0; i < len; i++) {
  129. const line = lines[i];
  130. /* line:
  131. {
  132. begin: Number,
  133. end: Number,
  134. first: Boolean,
  135. last: Boolean,
  136. parts: array of {
  137. style: 'bold'|'italic',
  138. text: String,
  139. }
  140. }*/
  141. let text = '';
  142. for (const part of line.parts) {
  143. text += part.text;
  144. }
  145. let indent = this.indent + (line.first ? this.p : 0);
  146. y += this.lineHeight;
  147. let filled = false;
  148. if (this.textAlignJustify && !line.last) {
  149. const words = text.split(' ');
  150. if (words.length > 1) {
  151. const spaceCount = words.length - 1;
  152. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  153. let x = indent;
  154. for (const word of words) {
  155. context.fillText(word, x, y);
  156. x += this.measureText(word) + space;
  157. }
  158. filled = true;
  159. }
  160. }
  161. if (!filled)
  162. context.fillText(text, indent, y);
  163. }
  164. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  165. this.linesDown = lines;
  166. }
  167. doPageDown() {
  168. if (this.linesDown) {
  169. let i = this.pageLineCount;
  170. i--;
  171. if (i >= 0 && this.linesDown.length > i) {
  172. this.bookPos = this.linesDown[i].begin;
  173. }
  174. }
  175. }
  176. doPageUp() {
  177. if (this.linesUp) {
  178. let i = this.pageLineCount;
  179. i--;
  180. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  181. if (i >= 0 && this.linesUp.length > i) {
  182. this.bookPos = this.linesUp[i].begin;
  183. }
  184. }
  185. }
  186. doHome() {
  187. this.bookPos = 0;
  188. }
  189. doEnd() {
  190. if (this.parsed.para.length) {
  191. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  192. this.bookPos = lastPara.offset + lastPara.length - 1;
  193. }
  194. }
  195. keyHook(event) {
  196. if (event.type == 'keydown') {
  197. switch (event.key) {
  198. case 'PageDown':
  199. this.doPageDown();
  200. break;
  201. case 'PageUp':
  202. this.doPageUp();
  203. break;
  204. case 'Home':
  205. this.doHome();
  206. break;
  207. case 'End':
  208. this.doEnd();
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. //-----------------------------------------------------------------------------
  215. </script>
  216. <style scoped>
  217. .main {
  218. flex: 1;
  219. margin: 0;
  220. padding: 0;
  221. overflow: hidden;
  222. }
  223. .canvas {
  224. margin: 0;
  225. padding: 0;
  226. }
  227. </style>