Работа над стилизацией текста
This commit is contained in:
@@ -63,6 +63,7 @@ class TextPage extends Vue {
|
|||||||
this.parsed.font = this.font;
|
this.parsed.font = this.font;
|
||||||
this.parsed.wordWrap = this.wordWrap;
|
this.parsed.wordWrap = this.wordWrap;
|
||||||
this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
||||||
|
this.context.font = this.fontByStyle(style);
|
||||||
return this.context.measureText(text).width;
|
return this.context.measureText(text).width;
|
||||||
};
|
};
|
||||||
this.parsed.measureText = this.measureText;
|
this.parsed.measureText = this.measureText;
|
||||||
@@ -130,6 +131,10 @@ class TextPage extends Vue {
|
|||||||
return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
|
return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fontByStyle(style) {
|
||||||
|
return `${style.italic ? 'italic' : ''} ${style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
|
||||||
|
}
|
||||||
|
|
||||||
drawPage() {
|
drawPage() {
|
||||||
if (!this.lastBook)
|
if (!this.lastBook)
|
||||||
return;
|
return;
|
||||||
@@ -155,7 +160,6 @@ class TextPage extends Vue {
|
|||||||
let y = 0;
|
let y = 0;
|
||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
const line = lines[i];
|
const line = lines[i];
|
||||||
console.log(line.parts);
|
|
||||||
/* line:
|
/* line:
|
||||||
{
|
{
|
||||||
begin: Number,
|
begin: Number,
|
||||||
@@ -171,37 +175,42 @@ console.log(line.parts);
|
|||||||
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;
|
let filled = false;
|
||||||
if (this.textAlignJustify && !line.last) {
|
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) {
|
if (words.length > 1) {
|
||||||
const spaceCount = words.length - 1;
|
const spaceCount = words.length - 1;
|
||||||
|
|
||||||
const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
|
const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
|
||||||
|
|
||||||
let x = indent;
|
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);
|
context.fillText(word, x, y);
|
||||||
x += this.measureText(word) + space;
|
x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
filled = true;
|
filled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filled)
|
if (!filled) {
|
||||||
context.fillText(text, indent, y);
|
|
||||||
*/
|
|
||||||
/*let text = '';
|
|
||||||
for (const part of line.parts) {
|
|
||||||
text += part.text;
|
|
||||||
}
|
|
||||||
context.fillText(text, indent, y);
|
|
||||||
*/
|
|
||||||
let x = indent;
|
let x = indent;
|
||||||
for (const part of line.parts) {
|
for (const part of line.parts) {
|
||||||
let text = part.text;
|
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);
|
context.fillText(text, x, y);
|
||||||
x += this.measureText(text);
|
x += this.measureText(text, part.style);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -341,7 +341,6 @@ export default class BookParser {
|
|||||||
let j = 0;//номер строки
|
let j = 0;//номер строки
|
||||||
let ofs = -1;
|
let ofs = -1;
|
||||||
let word = '';
|
let word = '';
|
||||||
let sp1 = '';
|
|
||||||
|
|
||||||
let text = '';
|
let text = '';
|
||||||
let style = {};
|
let style = {};
|
||||||
@@ -351,6 +350,7 @@ export default class BookParser {
|
|||||||
text = part.text + ' ';
|
text = part.text + ' ';
|
||||||
style = part.style;
|
style = part.style;
|
||||||
|
|
||||||
|
let sp1 = '';
|
||||||
let sp2 = '';
|
let sp2 = '';
|
||||||
for (let i = 0; i < text.length; i++) {
|
for (let i = 0; i < text.length; i++) {
|
||||||
if (i < text.length - 1)
|
if (i < text.length - 1)
|
||||||
@@ -364,10 +364,10 @@ export default class BookParser {
|
|||||||
sp1 = ' ';
|
sp1 = ' ';
|
||||||
|
|
||||||
let p = (j == 0 ? parsed.p : 0);
|
let p = (j == 0 ? parsed.p : 0);
|
||||||
let w = this.measureText(str) + p;
|
let w = this.measureText(str, style) + p;
|
||||||
let wordTail = word;
|
let wordTail = word;
|
||||||
if (w > parsed.w) {
|
if (w > parsed.w) {
|
||||||
if (parsed.wordWrap) {
|
if (parsed.wordWrap) {//по слогам
|
||||||
let slogi = this.splitToSlogi(word);
|
let slogi = this.splitToSlogi(word);
|
||||||
|
|
||||||
if (slogi.length > 1) {
|
if (slogi.length > 1) {
|
||||||
@@ -378,7 +378,7 @@ export default class BookParser {
|
|||||||
const slogiLen = slogi.length;
|
const slogiLen = slogi.length;
|
||||||
for (let k = 0; k < slogiLen - 1; k++) {
|
for (let k = 0; k < slogiLen - 1; k++) {
|
||||||
let slog = slogi[0];
|
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) {
|
if (ww <= parsed.w) {
|
||||||
s += slog;
|
s += slog;
|
||||||
ss += slog;
|
ss += slog;
|
||||||
|
|||||||
Reference in New Issue
Block a user