Работа над изображениями

This commit is contained in:
Book Pauk
2019-02-20 16:49:03 +07:00
parent ae3dc9b22c
commit 83fc586e03
2 changed files with 40 additions and 10 deletions

View File

@@ -26,10 +26,11 @@ export default class DrawHelper {
const font = this.fontByStyle({}); const font = this.fontByStyle({});
const justify = (this.textAlignJustify ? 'text-align: justify; text-align-last: justify;' : ''); const justify = (this.textAlignJustify ? 'text-align: justify; text-align-last: justify;' : '');
let out = `<div class="layout" style="width: ${this.w}px; height: ${this.h}px;` + let out = `<div style="width: ${this.w}px; height: ${this.h}px;` +
` position: absolute; top: ${this.fontSize*this.textShift}px; color: ${this.textColor}; font: ${font}; ${justify}` + ` position: absolute; top: ${this.fontSize*this.textShift}px; color: ${this.textColor}; font: ${font}; ${justify}` +
` line-height: ${this.lineHeight}px;">`; ` line-height: ${this.lineHeight}px;">`;
let imageDrawn = new Set();
let len = lines.length; let len = lines.length;
const lineCount = this.pageLineCount + (isScrolling ? 1 : 0); const lineCount = this.pageLineCount + (isScrolling ? 1 : 0);
len = (len > lineCount ? lineCount : len); len = (len > lineCount ? lineCount : len);
@@ -43,7 +44,8 @@ export default class DrawHelper {
first: Boolean, first: Boolean,
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: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, resize: Boolean, paraIndex: Number},
text: String, text: String,
} }
}*/ }*/
@@ -89,10 +91,26 @@ export default class DrawHelper {
lineText += `${tOpen}${text}${tClose}`; lineText += `${tOpen}${text}${tClose}`;
center = center || part.style.center; center = center || part.style.center;
//избражения
//image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, resize: Boolean, 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 left = (this.w - bin.w)/2;
const s = (img.lineCount*this.lineHeight - bin.h)/2;
const top = s + (i - img.imageLine)*this.lineHeight;
lineText += `<img src="data:${bin.type};base64,${bin.data}" style="position: absolute; left: ${left}px; top: ${top}px"/>`;
} else {
//
}
imageDrawn.add(img.paraIndex);
}
} }
const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '') const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')
if (line.first) if (line.first && !center)
lineText = `<span style="display: inline-block; margin-left: ${this.p}px"></span>${lineText}`; lineText = `<span style="display: inline-block; margin-left: ${this.p}px"></span>${lineText}`;
if (line.last || center) if (line.last || center)
lineText = `<span style="display: inline-block; ${centerStyle}">${lineText}</span>`; lineText = `<span style="display: inline-block; ${centerStyle}">${lineText}</span>`;

View File

@@ -349,8 +349,7 @@ export default class BookParser {
splitToStyle(s) { splitToStyle(s) {
let result = [];/*array of { let result = [];/*array of {
style: {bold: Boolean, italic: Boolean, center: Boolean}, style: {bold: Boolean, italic: Boolean, center: Boolean},
image: Boolean, image: {local: Boolean, inline: Boolean, id: String},
imageId: String,
text: String, text: String,
}*/ }*/
let style = {}; let style = {};
@@ -379,8 +378,12 @@ export default class BookParser {
let attrs = sax.getAttrsSync(tail); let attrs = sax.getAttrsSync(tail);
let id = attrs.href.value; let id = attrs.href.value;
if (id) { if (id) {
let local = false;
if (id[0] == '#') {
id = id.substr(1); id = id.substr(1);
image = {inline: false, id}; local = true;
}
image = {local, inline: false, id};
} }
break; break;
} }
@@ -529,7 +532,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}, image: {local: Boolean, inline: Boolean, id: String, imageLine: Number, lineCount: Number, resize: Boolean, paraIndex: Number},
text: String, text: String,
} }
}*/ }*/
@@ -560,7 +563,15 @@ export default class BookParser {
line.end = para.offset + ofs; line.end = para.offset + ofs;
line.first = (j == 0); line.first = (j == 0);
line.last = false; line.last = false;
line.parts.push({style, text: '!', image: {inline: false, id: part.image.id, imageLine: i, lineCount, resize: (c > lineCount)}}); line.parts.push({style, text: ' ', image: {
local: part.image.local,
inline: false,
id: part.image.id,
imageLine: i,
lineCount,
resize: (c > lineCount),
paraIndex
}});
lines.push(line); lines.push(line);
line = {begin: line.end + 1, parts: []}; line = {begin: line.end + 1, parts: []};
ofs++; ofs++;
@@ -568,7 +579,8 @@ export default class BookParser {
} }
line.first = (j == 0); line.first = (j == 0);
line.last = true; line.last = true;
line.parts.push({style, text: '!', image: {inline: false, id: part.image.id, imageLine: i, lineCount, resize: (c > lineCount)}}); line.parts.push({style, text: ' ',
image: {local: part.image.local, inline: false, id: part.image.id, imageLine: i, lineCount, resize: (c > lineCount), paraIndex}});
continue; continue;
} }