From 3b2397dd2b4f5dd210c63d9b8af6db050631773b Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Tue, 22 Jan 2019 20:34:08 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=86=D0=B5=D0=BD=D1=82=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/Reader/TextPage/TextPage.vue | 18 +++-- client/components/Reader/share/BookParser.js | 66 +++++++++++++------ 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/client/components/Reader/TextPage/TextPage.vue b/client/components/Reader/TextPage/TextPage.vue index 57181690..4b5de376 100644 --- a/client/components/Reader/TextPage/TextPage.vue +++ b/client/components/Reader/TextPage/TextPage.vue @@ -155,7 +155,7 @@ class TextPage extends Vue { this.textColor = '#000000'; this.backgroundColor = '#478355'; this.fontStyle = '';// 'bold','italic' - this.fontSize = 33;// px + this.fontSize = 35;// px this.fontName = 'Arial'; this.lineInterval = 7;// px, межстрочный интервал this.textAlignJustify = true;// выравнивание по ширине @@ -302,7 +302,7 @@ class TextPage extends Vue { first: Boolean, last: Boolean, parts: array of { - style: {bold: Boolean, italic: Boolean} + style: {bold: Boolean, italic: Boolean, center: Boolean} text: String, } }*/ @@ -310,13 +310,16 @@ class TextPage extends Vue { let indent = this.indent + (line.first ? this.p : 0); y += this.lineHeight; + let lineText = ''; + let center = false; + for (const part of line.parts) { + lineText += part.text; + center = center || part.style.center; + } + let filled = false; // если выравнивание по ширине включено - if (this.textAlignJustify && !line.last) { - let lineText = ''; - for (const part of line.parts) { - lineText += part.text; - } + if (this.textAlignJustify && !line.last && !center) { const words = lineText.split(' '); if (words.length > 1) { @@ -342,6 +345,7 @@ class TextPage extends Vue { // просто выводим текст if (!filled) { let x = indent; + x = (center ? this.indent + (this.w - context.measureText(lineText).width)/2 : x); for (const part of line.parts) { let text = part.text; context.font = this.fontByStyle(part.style); diff --git a/client/components/Reader/share/BookParser.js b/client/components/Reader/share/BookParser.js index 88a69676..45de62d9 100644 --- a/client/components/Reader/share/BookParser.js +++ b/client/components/Reader/share/BookParser.js @@ -42,6 +42,7 @@ export default class BookParser { let path = ''; let tag = ''; let nextPerc = 0; + let center = false; let paraIndex = -1; let paraOffset = 0; @@ -53,13 +54,14 @@ export default class BookParser { text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами } */ - const newParagraph = (text, len) => { + const newParagraph = (text, len, single) => { paraIndex++; let p = { index: paraIndex, offset: paraOffset, length: len, - text: text + text: text, + single: single }; para[paraIndex] = p; @@ -68,10 +70,14 @@ export default class BookParser { const growParagraph = (text, len) => { let p = para[paraIndex]; if (p) { + if (p.single) { + newParagraph(text, len); + return; + } paraOffset -= p.length; if (p.length == 1 && p.text[0] == ' ' && len > 0) { p.length = 0; - p.text = p.text.substr(1);; + p.text = p.text.substr(1); } p.length += len; p.text += text; @@ -104,6 +110,9 @@ export default class BookParser { if (tag == 'emphasis' || tag == 'strong') { growParagraph(`<${tag}>`, 0); } + + if (tag == 'title') + center = true; }); parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars @@ -112,6 +121,9 @@ export default class BookParser { growParagraph(``, 0); } + if (tag == 'title') + center = false; + path = path.substr(0, path.length - tag.length - 1); let i = path.lastIndexOf('/'); if (i >= 0) { @@ -162,21 +174,24 @@ export default class BookParser { fb2.annotation += text; } + let cOpen = (center ? '
' : ''); + let cClose = (center ? '
' : ''); + if (path.indexOf('/FictionBook/body/title') == 0) { - newParagraph(text, text.length); + newParagraph(`${cOpen}${text}${cClose}`, text.length, true); } if (path.indexOf('/FictionBook/body/section') == 0) { switch (tag) { case 'p': - growParagraph(text, text.length); + growParagraph(`${cOpen}${text}${cClose}`, text.length); break; - case 'section': + //case 'section': case 'title': - newParagraph(text, text.length); + newParagraph(`${cOpen}${text}${cClose}`, text.length, center); break; default: - growParagraph(text, text.length); + growParagraph(`${cOpen}${text}${cClose}`, text.length); } } }); @@ -231,10 +246,9 @@ export default class BookParser { splitToStyle(s) { let result = [];/*array of { - style: {bold: Boolean, italic: Boolean}, + style: {bold: Boolean, italic: Boolean, center: Boolean}, text: String, }*/ - const parser = new EasySAXParser(); let style = {}; @@ -246,17 +260,31 @@ export default class BookParser { }); parser.on('startNode', (elemName, getAttr, isTagEnd, getStrNode) => {// eslint-disable-line no-unused-vars - if (elemName == 'strong') - style.bold = true; - else if (elemName == 'emphasis') - style.italic = true; + switch (elemName) { + case 'strong': + style.bold = true; + break; + case 'emphasis': + style.italic = true; + break; + case 'center': + style.center = true; + break; + } }); parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars - if (elemName == 'strong') - style.bold = false; - else if (elemName == 'emphasis') - style.italic = false; + switch (elemName) { + case 'strong': + style.bold = false; + break; + case 'emphasis': + style.italic = false; + break; + case 'center': + style.center = false; + break; + } }); parser.parse(`

${s}

`); @@ -340,7 +368,7 @@ export default class BookParser { first: Boolean, last: Boolean, parts: array of { - style: {bold: Boolean, italic: Boolean}, + style: {bold: Boolean, italic: Boolean, center: Boolean}, text: String, } }*/