TextPage.vue 7.1 KB

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