TextPage.vue 7.3 KB

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