Добавил загрузку внешних изображений
This commit is contained in:
@@ -103,9 +103,8 @@ export default class DrawHelper {
|
||||
//image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, paraIndex: Number},
|
||||
const img = part.image;
|
||||
if (img && img.id && !img.inline && !imageDrawn.has(img.paraIndex)) {
|
||||
if (img.local) {
|
||||
const bin = this.parsed.binary[img.id];
|
||||
|
||||
const bin = this.parsed.binary[img.id];
|
||||
if (bin) {
|
||||
let imgH = img.lineCount*this.lineHeight;
|
||||
imgH = (imgH <= bin.h ? imgH : bin.h);
|
||||
let imgW = bin.w;
|
||||
@@ -118,9 +117,11 @@ export default class DrawHelper {
|
||||
|
||||
const left = (this.w - imgW)/2;
|
||||
const top = ((img.lineCount*this.lineHeight - imgH)/2) + (i - img.imageLine)*this.lineHeight;
|
||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
||||
} else {
|
||||
//
|
||||
if (img.local) {
|
||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
||||
} else {
|
||||
lineText += `<img src="${img.id}" style="position: absolute; left: ${left}px; top: ${top}px; ${resize}"/>`;
|
||||
}
|
||||
}
|
||||
imageDrawn.add(img.paraIndex);
|
||||
}
|
||||
@@ -128,11 +129,13 @@ export default class DrawHelper {
|
||||
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`;
|
||||
if (bin) {
|
||||
let resize = '';
|
||||
if (bin.h > this.fontSize) {
|
||||
resize = `height: ${this.fontSize - 3}px`;
|
||||
}
|
||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
|
||||
}
|
||||
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="${resize}"/>`;
|
||||
} else {
|
||||
//
|
||||
}
|
||||
|
||||
@@ -85,6 +85,26 @@ export default class BookParser {
|
||||
});
|
||||
};
|
||||
|
||||
const getExternalImageDimensions = (src) => {
|
||||
return new Promise (async(resolve, reject) => {
|
||||
const i = new Image();
|
||||
let resolved = false;
|
||||
i.onload = () => {
|
||||
resolved = true;
|
||||
this.binary[src] = {
|
||||
w: i.width,
|
||||
h: i.height,
|
||||
};
|
||||
resolve();
|
||||
};
|
||||
|
||||
i.src = src;
|
||||
await sleep(30*1000);
|
||||
if (!resolved)
|
||||
reject('Не удалось получить размер изображения');
|
||||
});
|
||||
};
|
||||
|
||||
const newParagraph = (text, len, addIndex) => {
|
||||
paraIndex++;
|
||||
let p = {
|
||||
@@ -147,21 +167,26 @@ export default class BookParser {
|
||||
|
||||
if (tag == 'binary') {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
binaryType = (attrs['content-type'].value ? attrs['content-type'].value : '');
|
||||
binaryType = (attrs['content-type'] && attrs['content-type'].value ? attrs['content-type'].value : '');
|
||||
if (binaryType == 'image/jpeg' || binaryType == 'image/png')
|
||||
binaryId = (attrs.id.value ? attrs.id.value : '');
|
||||
}
|
||||
|
||||
if (tag == 'image') {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.href.value) {
|
||||
if (inPara && !this.showInlineImagesInCenter)
|
||||
growParagraph(`<image-inline href="${attrs.href.value}"></image-inline>`, 0);
|
||||
else
|
||||
newParagraph(`<image href="${attrs.href.value}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||
|
||||
if (inPara && this.showInlineImagesInCenter)
|
||||
newParagraph(' ', 1);
|
||||
if (attrs.href && attrs.href.value) {
|
||||
const href = attrs.href.value;
|
||||
if (href[0] == '#') {//local
|
||||
if (inPara && !this.showInlineImagesInCenter)
|
||||
growParagraph(`<image-inline href="${href}"></image-inline>`, 0);
|
||||
else
|
||||
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||
if (inPara && this.showInlineImagesInCenter)
|
||||
newParagraph(' ', 1);
|
||||
} else {//external
|
||||
dimPromises.push(getExternalImageDimensions(href));
|
||||
newParagraph(`<image href="${href}">${' '.repeat(maxImageLineCount)}</image>`, maxImageLineCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,14 +434,14 @@ export default class BookParser {
|
||||
break;
|
||||
case 'space': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
if (attrs.w.value)
|
||||
if (attrs.w && attrs.w.value)
|
||||
style.space = attrs.w.value;
|
||||
break;
|
||||
}
|
||||
case 'image': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
let id = attrs.href.value;
|
||||
if (id) {
|
||||
if (attrs.href && attrs.href.value) {
|
||||
let id = attrs.href.value;
|
||||
let local = false;
|
||||
if (id[0] == '#') {
|
||||
id = id.substr(1);
|
||||
@@ -428,8 +453,8 @@ export default class BookParser {
|
||||
}
|
||||
case 'image-inline': {
|
||||
let attrs = sax.getAttrsSync(tail);
|
||||
let id = attrs.href.value;
|
||||
if (id) {
|
||||
if (attrs.href && attrs.href.value) {
|
||||
let id = attrs.href.value;
|
||||
let local = false;
|
||||
if (id[0] == '#') {
|
||||
id = id.substr(1);
|
||||
@@ -617,6 +642,8 @@ export default class BookParser {
|
||||
if (part.image.id && !part.image.inline) {
|
||||
parsed.visible = this.showImages;
|
||||
const bin = this.binary[part.image.id];
|
||||
if (!bin)
|
||||
continue;
|
||||
|
||||
let lineCount = this.imageHeightLines;
|
||||
const c = Math.ceil(bin.h/this.lineHeight);
|
||||
@@ -648,10 +675,12 @@ export default class BookParser {
|
||||
|
||||
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}});
|
||||
if (bin) {
|
||||
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(' ');
|
||||
|
||||
Reference in New Issue
Block a user