소스 검색

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

Book Pauk 6 년 전
부모
커밋
1a7ceb333d
3개의 변경된 파일58개의 추가작업 그리고 4개의 파일을 삭제
  1. 13 0
      client/components/Reader/TextPage/DrawHelper.js
  2. 1 0
      client/components/Reader/TextPage/TextPage.vue
  3. 44 4
      client/components/Reader/share/BookParser.js

+ 13 - 0
client/components/Reader/TextPage/DrawHelper.js

@@ -124,6 +124,19 @@ export default class DrawHelper {
                     }
                     }
                     imageDrawn.add(img.paraIndex);
                     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` : '')
             const centerStyle = (center ? `text-align: center; text-align-last: center; width: ${this.w}px` : '')

+ 1 - 0
client/components/Reader/TextPage/TextPage.vue

@@ -209,6 +209,7 @@ class TextPage extends Vue {
             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.fontSize = this.fontSize;
             this.parsed.wordWrap = this.wordWrap;
             this.parsed.wordWrap = this.wordWrap;
             this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
             this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
             this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
             this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;

+ 44 - 4
client/components/Reader/share/BookParser.js

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