Добавил отступ параграфа

This commit is contained in:
Book Pauk
2019-01-16 21:40:08 +07:00
parent 7fe41d6f77
commit 51135df58a
2 changed files with 12 additions and 10 deletions

View File

@@ -70,7 +70,8 @@ class TextPage extends Vue {
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.textAlignJustify = true;// выравнивание по ширине
this.p = 30;// px, отступ параграфа
this.calcDrawProps(); this.calcDrawProps();
this.drawPage();// пока не загрузили, очистим канвас this.drawPage();// пока не загрузили, очистим канвас
@@ -95,7 +96,7 @@ class TextPage extends Vue {
this.calcDrawProps(); this.calcDrawProps();
const parsed = this.book.parsed; const parsed = this.book.parsed;
parsed.p = 30;// px, отступ параграфа parsed.p = this.p;
parsed.w = this.canvas.width;// px, ширина страницы parsed.w = this.canvas.width;// px, ширина страницы
parsed.font = this.font; parsed.font = this.font;
parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
@@ -155,7 +156,9 @@ class TextPage extends Vue {
text += part.text; text += part.text;
} }
let 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(' '); const words = text.split(' ');
@@ -163,7 +166,7 @@ class TextPage extends Vue {
const spaceCount = words.length - 1; const spaceCount = words.length - 1;
const space = (canvas.width - line.width + spaceWidth*spaceCount)/spaceCount; const space = (canvas.width - line.width + spaceWidth*spaceCount)/spaceCount;
let x = 0; let x = indent;
for (const word of words) { for (const word of words) {
context.fillText(word, x, y); context.fillText(word, x, y);
x += this.measureText(word) + space; x += this.measureText(word) + space;
@@ -173,8 +176,7 @@ class TextPage extends Vue {
} }
if (!filled) if (!filled)
context.fillText(text, 0, y); context.fillText(text, indent, y);
} }
this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1)); this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));

View File

@@ -266,24 +266,24 @@ export default class BookParser {
let prevPart = ''; let prevPart = '';
let part = ''; let part = '';
let prevW = 0; let prevW = 0;
let k = 0; let j = 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;
let w = this.measureText(part); let w = this.measureText(part) + (j == 0 ? parsed.p : 0);
if (w > parsed.w) { 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.width = prevW; line.width = prevW;
line.first = (k == 0); line.first = (j == 0);
line.last = false; line.last = false;
lines.push(line); lines.push(line);
line = {begin: line.end + 1, parts: []}; line = {begin: line.end + 1, parts: []};
part = word; part = word;
k++; j++;
} }
prevW = w; prevW = w;
prevPart = part; prevPart = part;
@@ -293,7 +293,7 @@ export default class BookParser {
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.width = prevW; line.width = prevW;
line.first = (k == 0); line.first = (j == 0);
line.last = true; line.last = true;
lines.push(line); lines.push(line);