TextPage.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <div ref="main" class="main">
  3. <canvas ref="canvas" class="canvas" @mousedown.prevent="onMouseDown" @touchstart.prevent="onTouchStart"></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. }, 1000);
  34. this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
  35. }
  36. mounted() {
  37. this.canvas = this.$refs.canvas;
  38. this.context = this.canvas.getContext('2d');
  39. }
  40. async calcDrawProps() {
  41. this.realWidth = this.$refs.main.clientWidth;
  42. this.realHeight = this.$refs.main.clientHeight;
  43. if (window.devicePixelRatio) {
  44. this.canvas.width = this.realWidth*window.devicePixelRatio;
  45. this.canvas.height = this.realHeight*window.devicePixelRatio;
  46. this.canvas.style.width = this.$refs.main.clientWidth + 'px';
  47. this.canvas.style.height = this.$refs.main.clientHeight + 'px';
  48. this.context.scale(window.devicePixelRatio, window.devicePixelRatio);
  49. } else {
  50. this.canvas.width = this.realWidth;
  51. this.canvas.height = this.realHeight;
  52. }
  53. this.lineHeight = this.fontSize + this.lineInterval;
  54. this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
  55. this.w = this.realWidth - 2*this.indent;
  56. this.h = this.realHeight;
  57. this.context.textAlign = 'left';
  58. this.context.textBaseline = 'bottom';
  59. if (this.parsed) {
  60. this.parsed.p = this.p;
  61. this.parsed.w = this.w;// px, ширина текста
  62. this.parsed.font = this.font;
  63. this.parsed.wordWrap = this.wordWrap;
  64. this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  65. if (style)
  66. this.context.font = this.fontByStyle(style);
  67. return this.context.measureText(text).width;
  68. };
  69. this.parsed.measureText = this.measureText;
  70. }
  71. }
  72. async loadFonts() {
  73. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  74. if (loaded.some(r => !r)) {
  75. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  76. if (loaded.some(r => !r.length))
  77. throw new Error('some font not loaded');
  78. }
  79. }
  80. showBook() {
  81. this.$refs.main.focus();
  82. this.book = null;
  83. this.meta = null;
  84. this.fb2 = null;
  85. this.parsed = null;
  86. this.linesUp = null;
  87. this.linesDown = null;
  88. //preloaded fonts
  89. this.fontList = ['12px ReaderDefault', '12px Arial', '12px ComicSansMS', '12px OpenSans', '12px Roboto', '12px ArialNarrow',
  90. '12px Georgia', '12px Tahoma', '12px Helvetica', '12px CenturySchoolbook'];
  91. //draw props
  92. this.textColor = 'black';
  93. this.backgroundColor = '#478355';
  94. this.fontStyle = '';// 'bold','italic'
  95. this.fontSize = 33;// px
  96. this.fontName = 'Arial';
  97. this.lineInterval = 10;// px, межстрочный интервал
  98. this.textAlignJustify = true;// выравнивание по ширине
  99. this.p = 50;// px, отступ параграфа
  100. this.indent = 10;// px, отступ всего текста слева и справа
  101. this.wordWrap = true;
  102. this.calcDrawProps();
  103. this.drawPage();// пока не загрузили, очистим канвас
  104. if (this.lastBook) {
  105. (async() => {
  106. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  107. if (!isParsed) {
  108. return;
  109. }
  110. this.book = await bookManager.getBook(this.lastBook);
  111. this.meta = bookManager.metaOnly(this.book);
  112. this.fb2 = this.meta.fb2;
  113. this.$root.$emit('set-app-title', _.compact([
  114. this.fb2.lastName,
  115. this.fb2.middleName,
  116. this.fb2.firstName,
  117. '-',
  118. this.fb2.bookTitle
  119. ]).join(' '));
  120. const parsed = this.book.parsed;
  121. this.parsed = parsed;
  122. this.calcDrawProps();
  123. await this.loadFonts();
  124. this.drawPage();
  125. })();
  126. }
  127. }
  128. onResize() {
  129. this.calcDrawProps();
  130. this.drawPage();
  131. }
  132. get font() {
  133. return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  134. }
  135. fontByStyle(style) {
  136. return `${style.italic ? 'italic' : ''} ${style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
  137. }
  138. drawPage() {
  139. if (!this.lastBook)
  140. return;
  141. //пустой канвас
  142. const canvas = this.canvas;
  143. const context = this.context;
  144. context.fillStyle = this.backgroundColor;
  145. context.fillRect(0, 0, canvas.width, canvas.height);
  146. if (!this.book)
  147. return;
  148. context.font = this.font;
  149. context.fillStyle = this.textColor;
  150. const spaceWidth = this.context.measureText(' ').width;
  151. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  152. let len = lines.length;
  153. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  154. let y = -this.lineInterval/2 + (this.h - this.pageLineCount*this.lineHeight)/2;
  155. for (let i = 0; i < len; i++) {
  156. const line = lines[i];
  157. /* line:
  158. {
  159. begin: Number,
  160. end: Number,
  161. first: Boolean,
  162. last: Boolean,
  163. parts: array of {
  164. style: {bold: Boolean, italic: Boolean}
  165. text: String,
  166. }
  167. }*/
  168. let indent = this.indent + (line.first ? this.p : 0);
  169. y += this.lineHeight;
  170. let filled = false;
  171. if (this.textAlignJustify && !line.last) {
  172. let lineText = '';
  173. for (const part of line.parts) {
  174. lineText += part.text;
  175. }
  176. const words = lineText.split(' ');
  177. if (words.length > 1) {
  178. const spaceCount = words.length - 1;
  179. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  180. let x = indent;
  181. for (const part of line.parts) {
  182. context.font = this.fontByStyle(part.style);
  183. let partWords = part.text.split(' ');
  184. for (let i = 0; i < partWords.length; i++) {
  185. let word = partWords[i];
  186. context.fillText(word, x, y);
  187. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  188. }
  189. }
  190. filled = true;
  191. }
  192. }
  193. if (!filled) {
  194. let x = indent;
  195. for (const part of line.parts) {
  196. let text = part.text;
  197. context.font = this.fontByStyle(part.style);
  198. context.fillText(text, x, y);
  199. x += this.measureText(text, part.style);
  200. }
  201. }
  202. }
  203. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  204. this.linesDown = lines;
  205. }
  206. doDown() {
  207. if (this.linesDown && this.linesDown.length > 1) {
  208. this.bookPos = this.linesDown[1].begin;
  209. }
  210. }
  211. doUp() {
  212. if (this.linesUp && this.linesUp.length > 1) {
  213. this.bookPos = this.linesUp[1].begin;
  214. }
  215. }
  216. doPageDown() {
  217. if (this.linesDown) {
  218. let i = this.pageLineCount;
  219. i--;
  220. if (i >= 0 && this.linesDown.length > i) {
  221. this.bookPos = this.linesDown[i].begin;
  222. }
  223. }
  224. }
  225. doPageUp() {
  226. if (this.linesUp) {
  227. let i = this.pageLineCount;
  228. i--;
  229. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  230. if (i >= 0 && this.linesUp.length > i) {
  231. this.bookPos = this.linesUp[i].begin;
  232. }
  233. }
  234. }
  235. doHome() {
  236. this.bookPos = 0;
  237. }
  238. doEnd() {
  239. if (this.parsed.para.length) {
  240. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  241. this.bookPos = lastPara.offset + lastPara.length - 1;
  242. }
  243. }
  244. doToolBarToggle() {
  245. this.$emit('tool-bar-toggle');
  246. }
  247. keyHook(event) {
  248. if (event.type == 'keydown') {
  249. switch (event.code) {
  250. case 'ArrowDown':
  251. this.doDown();
  252. break;
  253. case 'ArrowUp':
  254. this.doUp();
  255. break;
  256. case 'PageDown':
  257. case 'ArrowRight':
  258. case 'Enter':
  259. case 'Space':
  260. this.doPageDown();
  261. break;
  262. case 'PageUp':
  263. case 'ArrowLeft':
  264. case 'Backspace':
  265. this.doPageUp();
  266. break;
  267. case 'Home':
  268. this.doHome();
  269. break;
  270. case 'End':
  271. this.doEnd();
  272. break;
  273. }
  274. }
  275. }
  276. onTouchStart(event) {
  277. if (event.touches.length == 1) {
  278. this.onMouseDown(event.touches[0]);
  279. }
  280. }
  281. onMouseDown(event) {
  282. const mouseLegend = {
  283. 40: {30: 'PgUp', 100: 'PgDown'},
  284. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  285. 100: {30: 'PgUp', 100: 'PgDown'}
  286. };
  287. const w = event.clientX/this.realWidth*100;
  288. const h = event.clientY/this.realHeight*100;
  289. let action = '';
  290. loops: {
  291. for (const x in mouseLegend) {
  292. for (const y in mouseLegend[x]) {
  293. if (w < x && h < y) {
  294. action = mouseLegend[x][y];
  295. break loops;
  296. }
  297. }
  298. }
  299. }
  300. switch (action) {
  301. case 'Down' ://Down
  302. this.doDown();
  303. break;
  304. case 'Up' ://Up
  305. this.doUp();
  306. break;
  307. case 'PgDown' ://PgDown
  308. this.doPageDown();
  309. break;
  310. case 'PgUp' ://PgUp
  311. this.doPageUp();
  312. break;
  313. case 'Menu' :
  314. this.doToolBarToggle();
  315. break;
  316. default :
  317. // Nothing
  318. }
  319. }
  320. }
  321. //-----------------------------------------------------------------------------
  322. </script>
  323. <style scoped>
  324. .main {
  325. flex: 1;
  326. margin: 0;
  327. padding: 0;
  328. overflow: hidden;
  329. }
  330. .canvas {
  331. margin: 0;
  332. padding: 0;
  333. }
  334. </style>