TextPage.vue 9.0 KB

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