Добавил выравнивание текста по ширине
This commit is contained in:
@@ -45,7 +45,7 @@ class TextPage extends Vue {
|
|||||||
this.context.textAlign = 'left';
|
this.context.textAlign = 'left';
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCanvasSize() {
|
calcDrawProps() {
|
||||||
this.canvas.width = this.$refs.main.clientWidth;
|
this.canvas.width = this.$refs.main.clientWidth;
|
||||||
this.canvas.height = this.$refs.main.clientHeight;
|
this.canvas.height = this.$refs.main.clientHeight;
|
||||||
this.lineHeight = this.fontSize + this.lineInterval;
|
this.lineHeight = this.fontSize + this.lineInterval;
|
||||||
@@ -63,15 +63,16 @@ class TextPage extends Vue {
|
|||||||
this.linesDown = null;
|
this.linesDown = null;
|
||||||
this.pageLineCount = 0;
|
this.pageLineCount = 0;
|
||||||
|
|
||||||
//canvas props
|
//draw props
|
||||||
this.textColor = 'black';
|
this.textColor = 'black';
|
||||||
this.backgroundColor = '#478355';
|
this.backgroundColor = '#478355';
|
||||||
this.fontStyle = '';// 'bold','italic'
|
this.fontStyle = '';// 'bold','italic'
|
||||||
this.fontSize = 20;// px
|
this.fontSize = 20;// px
|
||||||
this.fontName = 'arial';
|
this.fontName = 'arial';
|
||||||
this.lineInterval = 5;// px
|
this.lineInterval = 5;// px
|
||||||
|
this.textAlignJustify = true;
|
||||||
|
|
||||||
this.updateCanvasSize();
|
this.calcDrawProps();
|
||||||
this.drawPage();// пока не загрузили, очистим канвас
|
this.drawPage();// пока не загрузили, очистим канвас
|
||||||
|
|
||||||
if (this.lastBook) {
|
if (this.lastBook) {
|
||||||
@@ -92,13 +93,15 @@ class TextPage extends Vue {
|
|||||||
this.fb2.bookTitle
|
this.fb2.bookTitle
|
||||||
]).join(' '));
|
]).join(' '));
|
||||||
|
|
||||||
this.updateCanvasSize();
|
this.calcDrawProps();
|
||||||
const parsed = this.book.parsed;
|
const parsed = this.book.parsed;
|
||||||
parsed.p = 30;// px, отступ параграфа
|
parsed.p = 30;// px, отступ параграфа
|
||||||
parsed.w = this.canvas.width;// px, ширина страницы
|
parsed.w = this.canvas.width;// px, ширина страницы
|
||||||
|
parsed.font = this.font;
|
||||||
parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
||||||
return this.context.measureText(text).width;
|
return this.context.measureText(text).width;
|
||||||
};
|
};
|
||||||
|
this.measureText = parsed.measureText;
|
||||||
|
|
||||||
this.parsed = parsed;
|
this.parsed = parsed;
|
||||||
this.drawPage();
|
this.drawPage();
|
||||||
@@ -106,6 +109,10 @@ class TextPage extends Vue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get font() {
|
||||||
|
return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
|
||||||
|
}
|
||||||
|
|
||||||
drawPage() {
|
drawPage() {
|
||||||
if (!this.lastBook)
|
if (!this.lastBook)
|
||||||
return;
|
return;
|
||||||
@@ -114,15 +121,15 @@ class TextPage extends Vue {
|
|||||||
const canvas = this.canvas;
|
const canvas = this.canvas;
|
||||||
const context = this.context;
|
const context = this.context;
|
||||||
|
|
||||||
context.font = `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
|
|
||||||
|
|
||||||
context.fillStyle = this.backgroundColor;
|
context.fillStyle = this.backgroundColor;
|
||||||
context.fillRect(0, 0, canvas.width, canvas.height);
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
if (!this.book)
|
if (!this.book)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
context.font = this.font;
|
||||||
context.fillStyle = this.textColor;
|
context.fillStyle = this.textColor;
|
||||||
|
const spaceWidth = this.context.measureText(' ').width;
|
||||||
|
|
||||||
const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
|
const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
|
||||||
|
|
||||||
@@ -135,7 +142,8 @@ class TextPage extends Vue {
|
|||||||
{
|
{
|
||||||
begin: Number,
|
begin: Number,
|
||||||
end: Number,
|
end: Number,
|
||||||
para: Boolean,
|
paraBegin: Boolean,
|
||||||
|
paraEnd: Boolean,
|
||||||
parts: array of {
|
parts: array of {
|
||||||
style: 'bold'|'italic',
|
style: 'bold'|'italic',
|
||||||
text: String,
|
text: String,
|
||||||
@@ -148,7 +156,25 @@ class TextPage extends Vue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
y += this.lineHeight;
|
y += this.lineHeight;
|
||||||
context.fillText(text, 0, y);
|
let filled = false;
|
||||||
|
if (this.textAlignJustify && !line.paraEnd) {
|
||||||
|
const words = text.split(' ');
|
||||||
|
if (words.length > 1) {
|
||||||
|
let space = canvas.width - line.width + spaceWidth*(words.length - 1);
|
||||||
|
space = space/(words.length - 1);
|
||||||
|
|
||||||
|
let x = 0;
|
||||||
|
for (const word of words) {
|
||||||
|
context.fillText(word, x, y);
|
||||||
|
x += this.measureText(word) + space;
|
||||||
|
}
|
||||||
|
filled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filled)
|
||||||
|
context.fillText(text, 0, y);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
|
this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
|
||||||
|
|||||||
@@ -236,15 +236,16 @@ export default class BookParser {
|
|||||||
if (para.parsed &&
|
if (para.parsed &&
|
||||||
para.parsed.w === this.w &&
|
para.parsed.w === this.w &&
|
||||||
para.parsed.p === this.p &&
|
para.parsed.p === this.p &&
|
||||||
para.parsed.textAlignJustify === this.textAlignJustify &&
|
para.parsed.wordWrap === this.wordWrap &&
|
||||||
para.parsed.wordWrap === this.wordWrap)
|
para.parsed.font === this.font
|
||||||
|
)
|
||||||
return para.parsed;
|
return para.parsed;
|
||||||
|
|
||||||
const parsed = {
|
const parsed = {
|
||||||
w: this.w,
|
w: this.w,
|
||||||
p: this.p,
|
p: this.p,
|
||||||
textAlignJustify: this.textAlignJustify,
|
wordWrap: this.wordWrap,
|
||||||
wordWrap: this.wordWrap
|
font: this.font,
|
||||||
};
|
};
|
||||||
|
|
||||||
const lines = [];
|
const lines = [];
|
||||||
@@ -252,7 +253,8 @@ export default class BookParser {
|
|||||||
{
|
{
|
||||||
begin: Number,
|
begin: Number,
|
||||||
end: Number,
|
end: Number,
|
||||||
para: Boolean,
|
paraBegin: Boolean,
|
||||||
|
paraEnd: Boolean,
|
||||||
parts: array of {
|
parts: array of {
|
||||||
style: 'bold'|'italic',
|
style: 'bold'|'italic',
|
||||||
text: String,
|
text: String,
|
||||||
@@ -265,29 +267,36 @@ export default class BookParser {
|
|||||||
let line = {begin: para.offset, parts: []};
|
let line = {begin: para.offset, parts: []};
|
||||||
let prevPart = '';
|
let prevPart = '';
|
||||||
let part = '';
|
let part = '';
|
||||||
|
let prevW = 0;
|
||||||
let k = 0;
|
let k = 0;
|
||||||
// тут начинается самый замес, перенос и стилизация
|
// тут начинается самый замес, перенос и стилизация
|
||||||
for (let i = 0; i < words.length; i++) {
|
for (let i = 0; i < words.length; i++) {
|
||||||
const word = words[i];
|
const word = words[i];
|
||||||
part += word;
|
part += word;
|
||||||
|
|
||||||
if (this.measureText(part) > parsed.w) {
|
let w = this.measureText(part);
|
||||||
|
if (w > parsed.w) {
|
||||||
line.parts.push({style: '', text: prevPart});
|
line.parts.push({style: '', text: prevPart});
|
||||||
line.end = line.begin + prevPart.length;//нет -1 !!!
|
line.end = line.begin + prevPart.length;//нет -1 !!!
|
||||||
line.para = (k == 0);
|
line.width = prevW;
|
||||||
|
line.paraBegin = (k == 0);
|
||||||
|
line.paraEnd = false;
|
||||||
lines.push(line);
|
lines.push(line);
|
||||||
|
|
||||||
line = {begin: line.end + 1, parts: []};
|
line = {begin: line.end + 1, parts: []};
|
||||||
part = word;
|
part = word;
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
|
prevW = w;
|
||||||
prevPart = part;
|
prevPart = part;
|
||||||
part += ' ';
|
part += ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
line.parts.push({style: '', text: prevPart});
|
line.parts.push({style: '', text: prevPart});
|
||||||
line.end = line.begin + prevPart.length - 1;
|
line.end = line.begin + prevPart.length - 1;
|
||||||
line.para = (k == 0);
|
line.width = prevW;
|
||||||
|
line.paraBegin = (k == 0);
|
||||||
|
line.paraEnd = true;
|
||||||
lines.push(line);
|
lines.push(line);
|
||||||
|
|
||||||
parsed.lines = lines;
|
parsed.lines = lines;
|
||||||
|
|||||||
Reference in New Issue
Block a user