Работа над переносом по слогам

This commit is contained in:
Book Pauk
2019-01-17 00:09:12 +07:00
parent 4829902176
commit f622dbca7d
2 changed files with 58 additions and 4 deletions

View File

@@ -54,12 +54,13 @@ class TextPage extends Vue {
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;
this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight); this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
this.w = this.canvas.width - 2*this.indent; this.w = this.canvas.width - 2*this.indent - 200;
if (this.parsed) { if (this.parsed) {
this.parsed.p = this.p; this.parsed.p = this.p;
this.parsed.w = this.w;// px, ширина текста this.parsed.w = this.w;// px, ширина текста
this.parsed.font = this.font; this.parsed.font = this.font;
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
return this.context.measureText(text).width; return this.context.measureText(text).width;
}; };
@@ -87,6 +88,7 @@ class TextPage extends Vue {
this.textAlignJustify = true;// выравнивание по ширине this.textAlignJustify = true;// выравнивание по ширине
this.p = 30;// px, отступ параграфа this.p = 30;// px, отступ параграфа
this.indent = 20;// px, отступ всего текста слева и справа this.indent = 20;// px, отступ всего текста слева и справа
this.wordWrap = true;
this.calcDrawProps(); this.calcDrawProps();
this.drawPage();// пока не загрузили, очистим канвас this.drawPage();// пока не загрузили, очистим канвас

View File

@@ -228,6 +228,42 @@ export default class BookParser {
return result; return result;
} }
splitToSlogi(word) {
let result = [];
const glas = new Set(['а', 'А', 'о', 'О', 'и', 'И', 'е', 'Е', 'ё', 'Ё', 'э', 'Э', 'ы', 'Ы', 'у', 'У', 'ю', 'Ю', 'я', 'Я']);
const soglas = new Set([
'б', 'в', 'г', 'д', 'ж', 'з', 'й', 'к', 'л', 'м', 'н', 'п', 'р', 'с', 'т', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'Б', 'В', 'Г', 'Д', 'Ж', 'З', 'Й', 'К', 'Л', 'М', 'Н', 'П', 'Р', 'С', 'Т', 'Ф', 'Х', 'Ч', 'Ц', 'Ш', 'Щ'
]);
const znak = new Set(['ь', 'Ь', 'ъ', 'Ъ', 'й', 'Й']);
const alpha = new Set(...glas, ...soglas, ...znak);
let slog = '';
let slogLen = 0;
const len = word.length;
word += ' ';
for (let i = 0; i < len; i++) {
slog += word[i];
if (alpha.has(word[i]))
slogLen++;
if (slogLen > 1 && i < len - 2 && (
(glas.has(word[i]) && !(soglas.has(word[i + 1]) && soglas.has(word[i + 2]))) ||
(soglas.has(word[i]) && soglas.has(word[i + 1]) && (glas.has(word[i + 2]) || soglas.has(word[i + 2]))) ||
(znak.has(word[i]))
)) {
result.push(slog);
slog = '';
slogLen = 0;
}
}
if (slog)
result.push(slog);
return result;
}
parsePara(paraIndex) { parsePara(paraIndex) {
const para = this.para[paraIndex]; const para = this.para[paraIndex];
@@ -246,8 +282,8 @@ export default class BookParser {
font: this.font, font: this.font,
}; };
const lines = [];
/* array of const lines = []; /* array of
{ {
begin: Number, begin: Number,
end: Number, end: Number,
@@ -274,6 +310,22 @@ export default class BookParser {
let w = this.measureText(part) + (j == 0 ? parsed.p : 0); let w = this.measureText(part) + (j == 0 ? parsed.p : 0);
if (w > parsed.w) { if (w > parsed.w) {
let wordTail;
if (parsed.wordWrap) {
let slogi = this.splitToSlogi(word);
/*for (let k = 0; k < slogi.length - 1; k++) {
}*/
if (slogi.length > 1) {
prevPart += ' ' + slogi[0] + '-';
slogi.shift();
wordTail = slogi.join('');
} else {
wordTail = word;
}
} else {
wordTail = word;
}
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;
@@ -282,7 +334,7 @@ export default class BookParser {
lines.push(line); lines.push(line);
line = {begin: line.end + 1, parts: []}; line = {begin: line.end + 1, parts: []};
part = word; part = wordTail;
j++; j++;
} }
prevW = w; prevW = w;