Добавлена обработка inline-изображений

This commit is contained in:
Book Pauk
2019-02-20 20:57:34 +07:00
parent d3a30b87f4
commit 1a7ceb333d
3 changed files with 58 additions and 4 deletions

View File

@@ -124,6 +124,19 @@ export default class DrawHelper {
}
imageDrawn.add(img.paraIndex);
}
if (img && img.id && img.inline) {
if (img.local) {
const bin = this.parsed.binary[img.id];
let resize = '';
if (bin.h > this.fontSize) {
resize = `height: ${this.fontSize - 3}px`;
}
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
} else {
//
}
}
}
const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')

View File

@@ -209,6 +209,7 @@ class TextPage extends Vue {
this.parsed.p = this.p;
this.parsed.w = this.w;// px, ширина текста
this.parsed.font = this.font;
this.parsed.fontSize = this.fontSize;
this.parsed.wordWrap = this.wordWrap;
this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;

View File

@@ -40,6 +40,7 @@ export default class BookParser {
let bold = false;
let italic = false;
let space = 0;
let inPara = false;
this.binary = {};
let binaryId = '';
@@ -149,8 +150,12 @@ export default class BookParser {
if (tag == 'image') {
let attrs = sax.getAttrsSync(tail);
if (attrs.href.value)
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
if (attrs.href.value) {
if (inPara)
growParagraph(`<image-inline href="${attrs.href.value}"></image-inline>`, 0);
else
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
}
}
if (path.indexOf('/fictionbook/body') == 0) {
@@ -170,6 +175,8 @@ export default class BookParser {
if ((tag == 'p' || tag == 'empty-line' || tag == 'v')) {
newParagraph(' ', 1);
if (tag == 'p')
inPara = true;
}
if (tag == 'subtitle') {
@@ -209,6 +216,10 @@ export default class BookParser {
growParagraph(`</${tag}>`, 0);
}
if (tag == 'p') {
inPara = false;
}
if (tag == 'subtitle') {
bold = false;
}
@@ -408,6 +419,23 @@ export default class BookParser {
}
break;
}
case 'image-inline': {
let attrs = sax.getAttrsSync(tail);
let id = attrs.href.value;
if (id) {
let local = false;
if (id[0] == '#') {
id = id.substr(1);
local = true;
}
result.push({
style: Object.assign({}, style),
image: {local, inline: true, id},
text: ''
});
}
break;
}
}
};
@@ -428,6 +456,8 @@ export default class BookParser {
case 'image':
image = {};
break;
case 'image-inline':
break;
}
};
@@ -570,8 +600,9 @@ export default class BookParser {
let j = 0;//номер строки
let style = {};
let ofs = 0;//смещение от начала параграфа para.offset
let imgW = 0;
// тут начинается самый замес, перенос по слогам и стилизация
// тут начинается самый замес, перенос по слогам и стилизация, а также изображения
for (const part of parts) {
style = part.style;
@@ -608,6 +639,14 @@ export default class BookParser {
continue;
}
if (part.image.id && part.image.inline && this.showImages) {
const bin = this.binary[part.image.id];
let imgH = (bin.h > this.fontSize ? this.fontSize : bin.h);
imgW += bin.w*imgH/bin.h;
line.parts.push({style, text: '',
image: {local: part.image.local, inline: true, id: part.image.id}});
}
let words = part.text.split(' ');
let sp1 = '';
@@ -621,7 +660,7 @@ export default class BookParser {
str += sp1 + word;
let p = (j == 0 ? parsed.p : 0);
let p = (j == 0 ? parsed.p : 0) + imgW;
p = (style.space ? p + parsed.p*style.space : p);
let w = this.measureText(str, style) + p;
let wordTail = word;
@@ -676,6 +715,7 @@ export default class BookParser {
partText = '';
sp2 = '';
str = wordTail;
imgW = 0;
j++;
}