Работа над парсером и канвасом

This commit is contained in:
Book Pauk
2019-01-16 17:48:30 +07:00
parent 159dbd6860
commit 66278be356
2 changed files with 24 additions and 17 deletions

View File

@@ -66,13 +66,13 @@ class TextPage extends Vue {
//canvas props //canvas 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.updateCanvasSize(); this.updateCanvasSize();
this.drawPage();//пока не загрузили, очистим канвас this.drawPage();// пока не загрузили, очистим канвас
if (this.lastBook) { if (this.lastBook) {
(async() => { (async() => {
@@ -92,9 +92,10 @@ class TextPage extends Vue {
this.fb2.bookTitle this.fb2.bookTitle
]).join(' ')); ]).join(' '));
this.updateCanvasSize();
const parsed = this.book.parsed; const parsed = this.book.parsed;
parsed.p = 30;//px, отступ параграфа parsed.p = 30;// px, отступ параграфа
parsed.w = 300;//px, ширина страницы parsed.w = this.canvas.width;// px, ширина страницы
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;
}; };
@@ -134,6 +135,7 @@ class TextPage extends Vue {
{ {
begin: Number, begin: Number,
end: Number, end: Number,
para: Boolean,
parts: array of { parts: array of {
style: 'bold'|'italic', style: 'bold'|'italic',
text: String, text: String,

View File

@@ -252,6 +252,7 @@ export default class BookParser {
{ {
begin: Number, begin: Number,
end: Number, end: Number,
para: Boolean,
parts: array of { parts: array of {
style: 'bold'|'italic', style: 'bold'|'italic',
text: String, text: String,
@@ -262,28 +263,32 @@ export default class BookParser {
const words = text.split(' '); const words = text.split(' ');
let line = {begin: para.offset, parts: []}; let line = {begin: para.offset, parts: []};
let prevPart = '';
let part = ''; let part = '';
let j = 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];
if (j > 0)
part += ' ';
j++;
part += word; part += word;
if (this.measureText(part) >= parsed.w || i == words.length - 1) { if (this.measureText(part) > parsed.w) {
line.parts.push({style: '', text: (k == 0 ? ' ' : '') + part}); line.parts.push({style: '', text: prevPart});
line.end = line.begin + part.length - 1; line.end = line.begin + prevPart.length;//нет -1 !!!
line.para = (k == 0);
lines.push(line); lines.push(line);
line = {begin: line.end + 1, parts: []}; line = {begin: line.end + 1, parts: []};
part = ''; part = word;
j = 0;
k++; k++;
} }
} prevPart = part;
part += ' ';
}
line.parts.push({style: '', text: prevPart});
line.end = line.begin + prevPart.length - 1;
line.para = (k == 0);
lines.push(line);
parsed.lines = lines; parsed.lines = lines;
para.parsed = parsed; para.parsed = parsed;