TextPage.vue 7.5 KB

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