TextPage.vue 12 KB

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