Работа над стилизацией текста

This commit is contained in:
Book Pauk
2019-01-17 09:16:29 +07:00
parent 4608182a52
commit 46f56a5a84
2 changed files with 34 additions and 25 deletions

View File

@@ -63,6 +63,7 @@ class TextPage extends Vue {
this.parsed.font = this.font;
this.parsed.wordWrap = this.wordWrap;
this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
this.context.font = this.fontByStyle(style);
return this.context.measureText(text).width;
};
this.parsed.measureText = this.measureText;
@@ -130,6 +131,10 @@ class TextPage extends Vue {
return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
}
fontByStyle(style) {
return `${style.italic ? 'italic' : ''} ${style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
}
drawPage() {
if (!this.lastBook)
return;
@@ -155,7 +160,6 @@ class TextPage extends Vue {
let y = 0;
for (let i = 0; i < len; i++) {
const line = lines[i];
console.log(line.parts);
/* line:
{
begin: Number,
@@ -171,37 +175,42 @@ console.log(line.parts);
let indent = this.indent + (line.first ? this.p : 0);
y += this.lineHeight;
/*let filled = false;
let filled = false;
if (this.textAlignJustify && !line.last) {
const words = text.split(' ');
let lineText = '';
for (const part of line.parts) {
lineText += part.text;
}
const words = lineText.split(' ');
if (words.length > 1) {
const spaceCount = words.length - 1;
const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
let x = indent;
for (const word of words) {
for (const part of line.parts) {
context.font = this.fontByStyle(part.style);
let partWords = part.text.split(' ');
for (let i = 0; i < partWords.length; i++) {
let word = partWords[i];
context.fillText(word, x, y);
x += this.measureText(word) + space;
x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
}
}
filled = true;
}
}
if (!filled)
context.fillText(text, indent, y);
*/
/*let text = '';
for (const part of line.parts) {
text += part.text;
}
context.fillText(text, indent, y);
*/
if (!filled) {
let x = indent;
for (const part of line.parts) {
let text = part.text;
context.font = `${part.style.italic ? 'italic' : ''} ${part.style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
context.font = this.fontByStyle(part.style);
context.fillText(text, x, y);
x += this.measureText(text);
x += this.measureText(text, part.style);
}
}
}

View File

@@ -341,7 +341,6 @@ export default class BookParser {
let j = 0;//номер строки
let ofs = -1;
let word = '';
let sp1 = '';
let text = '';
let style = {};
@@ -351,6 +350,7 @@ export default class BookParser {
text = part.text + ' ';
style = part.style;
let sp1 = '';
let sp2 = '';
for (let i = 0; i < text.length; i++) {
if (i < text.length - 1)
@@ -364,10 +364,10 @@ export default class BookParser {
sp1 = ' ';
let p = (j == 0 ? parsed.p : 0);
let w = this.measureText(str) + p;
let w = this.measureText(str, style) + p;
let wordTail = word;
if (w > parsed.w) {
if (parsed.wordWrap) {
if (parsed.wordWrap) {//по слогам
let slogi = this.splitToSlogi(word);
if (slogi.length > 1) {
@@ -378,7 +378,7 @@ export default class BookParser {
const slogiLen = slogi.length;
for (let k = 0; k < slogiLen - 1; k++) {
let slog = slogi[0];
let ww = this.measureText(s + slog + (slog[slog.length - 1] == '-' ? '' : '-')) + p;
let ww = this.measureText(s + slog + (slog[slog.length - 1] == '-' ? '' : '-'), style) + p;
if (ww <= parsed.w) {
s += slog;
ss += slog;