TextPage.vue 9.7 KB

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