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

This commit is contained in:
Book Pauk
2019-01-22 20:34:08 +07:00
parent ffae3e1fee
commit 3b2397dd2b
2 changed files with 58 additions and 26 deletions

View File

@@ -155,7 +155,7 @@ class TextPage extends Vue {
this.textColor = '#000000'; this.textColor = '#000000';
this.backgroundColor = '#478355'; this.backgroundColor = '#478355';
this.fontStyle = '';// 'bold','italic' this.fontStyle = '';// 'bold','italic'
this.fontSize = 33;// px this.fontSize = 35;// px
this.fontName = 'Arial'; this.fontName = 'Arial';
this.lineInterval = 7;// px, межстрочный интервал this.lineInterval = 7;// px, межстрочный интервал
this.textAlignJustify = true;// выравнивание по ширине this.textAlignJustify = true;// выравнивание по ширине
@@ -302,7 +302,7 @@ class TextPage extends Vue {
first: Boolean, first: Boolean,
last: Boolean, last: Boolean,
parts: array of { parts: array of {
style: {bold: Boolean, italic: Boolean} style: {bold: Boolean, italic: Boolean, center: Boolean}
text: String, text: String,
} }
}*/ }*/
@@ -310,13 +310,16 @@ class TextPage extends Vue {
let indent = this.indent + (line.first ? this.p : 0); let indent = this.indent + (line.first ? this.p : 0);
y += this.lineHeight; y += this.lineHeight;
let filled = false;
// если выравнивание по ширине включено
if (this.textAlignJustify && !line.last) {
let lineText = ''; let lineText = '';
let center = false;
for (const part of line.parts) { for (const part of line.parts) {
lineText += part.text; lineText += part.text;
center = center || part.style.center;
} }
let filled = false;
// если выравнивание по ширине включено
if (this.textAlignJustify && !line.last && !center) {
const words = lineText.split(' '); const words = lineText.split(' ');
if (words.length > 1) { if (words.length > 1) {
@@ -342,6 +345,7 @@ class TextPage extends Vue {
// просто выводим текст // просто выводим текст
if (!filled) { if (!filled) {
let x = indent; let x = indent;
x = (center ? this.indent + (this.w - context.measureText(lineText).width)/2 : x);
for (const part of line.parts) { for (const part of line.parts) {
let text = part.text; let text = part.text;
context.font = this.fontByStyle(part.style); context.font = this.fontByStyle(part.style);

View File

@@ -42,6 +42,7 @@ export default class BookParser {
let path = ''; let path = '';
let tag = ''; let tag = '';
let nextPerc = 0; let nextPerc = 0;
let center = false;
let paraIndex = -1; let paraIndex = -1;
let paraOffset = 0; let paraOffset = 0;
@@ -53,13 +54,14 @@ export default class BookParser {
text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами
} }
*/ */
const newParagraph = (text, len) => { const newParagraph = (text, len, single) => {
paraIndex++; paraIndex++;
let p = { let p = {
index: paraIndex, index: paraIndex,
offset: paraOffset, offset: paraOffset,
length: len, length: len,
text: text text: text,
single: single
}; };
para[paraIndex] = p; para[paraIndex] = p;
@@ -68,10 +70,14 @@ export default class BookParser {
const growParagraph = (text, len) => { const growParagraph = (text, len) => {
let p = para[paraIndex]; let p = para[paraIndex];
if (p) { if (p) {
if (p.single) {
newParagraph(text, len);
return;
}
paraOffset -= p.length; paraOffset -= p.length;
if (p.length == 1 && p.text[0] == ' ' && len > 0) { if (p.length == 1 && p.text[0] == ' ' && len > 0) {
p.length = 0; p.length = 0;
p.text = p.text.substr(1);; p.text = p.text.substr(1);
} }
p.length += len; p.length += len;
p.text += text; p.text += text;
@@ -104,6 +110,9 @@ export default class BookParser {
if (tag == 'emphasis' || tag == 'strong') { if (tag == 'emphasis' || tag == 'strong') {
growParagraph(`<${tag}>`, 0); growParagraph(`<${tag}>`, 0);
} }
if (tag == 'title')
center = true;
}); });
parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars
@@ -112,6 +121,9 @@ export default class BookParser {
growParagraph(`</${tag}>`, 0); growParagraph(`</${tag}>`, 0);
} }
if (tag == 'title')
center = false;
path = path.substr(0, path.length - tag.length - 1); path = path.substr(0, path.length - tag.length - 1);
let i = path.lastIndexOf('/'); let i = path.lastIndexOf('/');
if (i >= 0) { if (i >= 0) {
@@ -162,21 +174,24 @@ export default class BookParser {
fb2.annotation += text; fb2.annotation += text;
} }
let cOpen = (center ? '<center>' : '');
let cClose = (center ? '</center>' : '');
if (path.indexOf('/FictionBook/body/title') == 0) { 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) { if (path.indexOf('/FictionBook/body/section') == 0) {
switch (tag) { switch (tag) {
case 'p': case 'p':
growParagraph(text, text.length); growParagraph(`${cOpen}${text}${cClose}`, text.length);
break; break;
case 'section': //case 'section':
case 'title': case 'title':
newParagraph(text, text.length); newParagraph(`${cOpen}${text}${cClose}`, text.length, center);
break; break;
default: default:
growParagraph(text, text.length); growParagraph(`${cOpen}${text}${cClose}`, text.length);
} }
} }
}); });
@@ -231,10 +246,9 @@ export default class BookParser {
splitToStyle(s) { splitToStyle(s) {
let result = [];/*array of { let result = [];/*array of {
style: {bold: Boolean, italic: Boolean}, style: {bold: Boolean, italic: Boolean, center: Boolean},
text: String, text: String,
}*/ }*/
const parser = new EasySAXParser(); const parser = new EasySAXParser();
let style = {}; let style = {};
@@ -246,17 +260,31 @@ export default class BookParser {
}); });
parser.on('startNode', (elemName, getAttr, isTagEnd, getStrNode) => {// eslint-disable-line no-unused-vars parser.on('startNode', (elemName, getAttr, isTagEnd, getStrNode) => {// eslint-disable-line no-unused-vars
if (elemName == 'strong') switch (elemName) {
case 'strong':
style.bold = true; style.bold = true;
else if (elemName == 'emphasis') break;
case 'emphasis':
style.italic = true; style.italic = true;
break;
case 'center':
style.center = true;
break;
}
}); });
parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars parser.on('endNode', (elemName, isTagStart, getStrNode) => {// eslint-disable-line no-unused-vars
if (elemName == 'strong') switch (elemName) {
case 'strong':
style.bold = false; style.bold = false;
else if (elemName == 'emphasis') break;
case 'emphasis':
style.italic = false; style.italic = false;
break;
case 'center':
style.center = false;
break;
}
}); });
parser.parse(`<p>${s}</p>`); parser.parse(`<p>${s}</p>`);
@@ -340,7 +368,7 @@ export default class BookParser {
first: Boolean, first: Boolean,
last: Boolean, last: Boolean,
parts: array of { parts: array of {
style: {bold: Boolean, italic: Boolean}, style: {bold: Boolean, italic: Boolean, center: Boolean},
text: String, text: String,
} }
}*/ }*/