Промежуточный коммит, работа над изображениями, небольшой рефакторинг попутно

This commit is contained in:
Book Pauk
2019-02-20 15:26:54 +07:00
parent 12e0f9459b
commit ae3dc9b22c
3 changed files with 59 additions and 29 deletions

View File

@@ -216,6 +216,9 @@ class TextPage extends Vue {
while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ'; while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
this.parsed.maxWordLength = t.length - 1; this.parsed.maxWordLength = t.length - 1;
this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper); this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper);
this.parsed.lineHeight = this.lineHeight;
this.parsed.showImages = true;
this.parsed.imageHeightLines = 100;
} }
//statusBar //statusBar

View File

@@ -356,10 +356,6 @@ export default class BookParser {
let style = {}; let style = {};
let image = {}; let image = {};
/*let attrs = sax.getAttrsSync(tail);
if (attrs.href.value)
newParagraph(' '.repeat(maxImageLineCount) + `<image href="${attrs.href.value}" />`, maxImageLineCount);
*/
const onTextNode = async(text) => {// eslint-disable-line no-unused-vars const onTextNode = async(text) => {// eslint-disable-line no-unused-vars
result.push({ result.push({
style: Object.assign({}, style), style: Object.assign({}, style),
@@ -379,10 +375,16 @@ export default class BookParser {
case 'center': case 'center':
style.center = true; style.center = true;
break; break;
case 'image': case 'image': {
image = {}; let attrs = sax.getAttrsSync(tail);
let id = attrs.href.value;
if (id) {
id = id.substr(1);
image = {inline: false, id};
}
break; break;
} }
}
}; };
const onEndNode = async(elemName) => {// eslint-disable-line no-unused-vars const onEndNode = async(elemName) => {// eslint-disable-line no-unused-vars
@@ -412,6 +414,7 @@ export default class BookParser {
result = []; result = [];
for (const part of parts) { for (const part of parts) {
let p = part; let p = part;
if (!p.image.id) {
let i = 0; let i = 0;
let spaceIndex = -1; let spaceIndex = -1;
while (i < p.text.length) { while (i < p.text.length) {
@@ -427,6 +430,7 @@ export default class BookParser {
} }
i++; i++;
} }
}
result.push(p); result.push(p);
} }
@@ -494,7 +498,9 @@ export default class BookParser {
para.parsed.maxWordLength === this.maxWordLength && para.parsed.maxWordLength === this.maxWordLength &&
para.parsed.font === this.font && para.parsed.font === this.font &&
para.parsed.cutEmptyParagraphs === this.cutEmptyParagraphs && para.parsed.cutEmptyParagraphs === this.cutEmptyParagraphs &&
para.parsed.addEmptyParagraphs === this.addEmptyParagraphs para.parsed.addEmptyParagraphs === this.addEmptyParagraphs &&
para.parsed.showImages === this.showImages &&
para.parsed.imageHeightLines === this.imageHeightLines
) )
return para.parsed; return para.parsed;
@@ -506,6 +512,8 @@ export default class BookParser {
font: this.font, font: this.font,
cutEmptyParagraphs: this.cutEmptyParagraphs, cutEmptyParagraphs: this.cutEmptyParagraphs,
addEmptyParagraphs: this.addEmptyParagraphs, addEmptyParagraphs: this.addEmptyParagraphs,
showImages: this.showImages,
imageHeightLines: this.imageHeightLines,
visible: !( visible: !(
(this.cutEmptyParagraphs && para.cut) || (this.cutEmptyParagraphs && para.cut) ||
(para.addIndex > this.addEmptyParagraphs) (para.addIndex > this.addEmptyParagraphs)
@@ -521,6 +529,7 @@ export default class BookParser {
last: Boolean, last: Boolean,
parts: array of { parts: array of {
style: {bold: Boolean, italic: Boolean, center: Boolean}, style: {bold: Boolean, italic: Boolean, center: Boolean},
image: {inline: Boolean, id: String, imageLine: Number, lineCount: Number, resize: Boolean},
text: String, text: String,
} }
}*/ }*/
@@ -531,16 +540,40 @@ export default class BookParser {
let str = '';//измеряемая строка let str = '';//измеряемая строка
let prevStr = '';//строка без крайнего слова let prevStr = '';//строка без крайнего слова
let prevW = 0;
let j = 0;//номер строки let j = 0;//номер строки
let style = {}; let style = {};
let ofs = 0;//смещение от начала параграфа para.offset let ofs = 0;//смещение от начала параграфа para.offset
// тут начинается самый замес, перенос по слогам и стилизация // тут начинается самый замес, перенос по слогам и стилизация
for (const part of parts) { for (const part of parts) {
const words = part.text.split(' ');
style = part.style; style = part.style;
//изображения
if (part.image.id && !part.image.inline) {
const bin = this.binary[part.image.id];
let lineCount = this.imageHeightLines;
const c = Math.ceil(bin.h/this.lineHeight);
lineCount = (c < lineCount ? c : lineCount);
let i = 0;
for (; i < lineCount - 1; i++) {
line.end = para.offset + ofs;
line.first = (j == 0);
line.last = false;
line.parts.push({style, text: '!', image: {inline: false, id: part.image.id, imageLine: i, lineCount, resize: (c > lineCount)}});
lines.push(line);
line = {begin: line.end + 1, parts: []};
ofs++;
j++;
}
line.first = (j == 0);
line.last = true;
line.parts.push({style, text: '!', image: {inline: false, id: part.image.id, imageLine: i, lineCount, resize: (c > lineCount)}});
continue;
}
const words = part.text.split(' ');
let sp1 = ''; let sp1 = '';
let sp2 = ''; let sp2 = '';
for (let i = 0; i < words.length; i++) { for (let i = 0; i < words.length; i++) {
@@ -578,7 +611,6 @@ export default class BookParser {
} }
if (pw) { if (pw) {
prevW = pw;
partText += ss + (ss[ss.length - 1] == '-' ? '' : '-'); partText += ss + (ss[ss.length - 1] == '-' ? '' : '-');
wordTail = slogi.join(''); wordTail = slogi.join('');
} }
@@ -592,7 +624,6 @@ export default class BookParser {
let t = line.parts[line.parts.length - 1].text; let t = line.parts[line.parts.length - 1].text;
if (t[t.length - 1] == ' ') { if (t[t.length - 1] == ' ') {
line.parts[line.parts.length - 1].text = t.trimRight(); line.parts[line.parts.length - 1].text = t.trimRight();
prevW -= this.measureText(' ', style);
} }
} }
@@ -600,7 +631,6 @@ export default class BookParser {
if (line.end - line.begin < 0) if (line.end - line.begin < 0)
console.error(`Parse error, empty line in paragraph ${paraIndex}`); console.error(`Parse error, empty line in paragraph ${paraIndex}`);
line.width = prevW;
line.first = (j == 0); line.first = (j == 0);
line.last = false; line.last = false;
lines.push(line); lines.push(line);
@@ -616,7 +646,6 @@ export default class BookParser {
partText += sp2 + wordTail; partText += sp2 + wordTail;
sp1 = ' '; sp1 = ' ';
sp2 = ' '; sp2 = ' ';
prevW = w;
} }
if (partText != '') if (partText != '')
@@ -628,14 +657,12 @@ export default class BookParser {
let t = line.parts[line.parts.length - 1].text; let t = line.parts[line.parts.length - 1].text;
if (t[t.length - 1] == ' ') { if (t[t.length - 1] == ' ') {
line.parts[line.parts.length - 1].text = t.trimRight(); line.parts[line.parts.length - 1].text = t.trimRight();
prevW -= this.measureText(' ', style);
} }
line.end = para.offset + para.length - 1; line.end = para.offset + para.length - 1;
if (line.end - line.begin < 0) if (line.end - line.begin < 0)
console.error(`Parse error, empty line in paragraph ${paraIndex}`); console.error(`Parse error, empty line in paragraph ${paraIndex}`);
line.width = prevW;
line.first = (j == 0); line.first = (j == 0);
line.last = true; line.last = true;
lines.push(line); lines.push(line);