浏览代码

Добавил центрирование строк

Book Pauk 6 年之前
父节点
当前提交
3b2397dd2b
共有 2 个文件被更改,包括 58 次插入26 次删除
  1. 11 7
      client/components/Reader/TextPage/TextPage.vue
  2. 47 19
      client/components/Reader/share/BookParser.js

+ 11 - 7
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);

+ 47 - 19
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(`</${tag}>`, 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 ? '<center>' : '');
+            let cClose = (center ? '</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(`<p>${s}</p>`);
@@ -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,
             }
         }*/