TextPage.vue 7.4 KB

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