TextPage.vue 7.0 KB

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