浏览代码

FB2: process <image> elements

Kandrashin Denis 13 年之前
父节点
当前提交
9912ce4664
共有 2 个文件被更改,包括 41 次插入18 次删除
  1. 35 14
      source/fb2read.cpp
  2. 6 4
      source/fb2read.h

+ 35 - 14
source/fb2read.cpp

@@ -3,11 +3,22 @@
 
 #include "fb2read.h"
 
+static QString Value(const QXmlAttributes &attributes, const QString &name)
+{
+    int count = attributes.count();
+    for (int i = 0; i < count; i++ ) {
+        if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
+            return attributes.value(i);
+        }
+    }
+    return QString();
+}
+
 Fb2Handler::SectionHash::SectionHash()
 {
     insert("body", Body);
     insert("descriptin", Descr);
-    insert("bynary", Bynary);
+    insert("binary", Binary);
 }
 
 Fb2Handler::KeywordHash::KeywordHash()
@@ -21,13 +32,15 @@ Fb2Handler::KeywordHash::KeywordHash()
 Fb2Handler::DocSection Fb2Handler::GetSection(const QString &name)
 {
     static SectionHash map;
-    return map[name];
+    SectionHash::const_iterator i = map.find(name);
+    return i == map.end() ? None : i.value();
 }
 
 Fb2Handler::DocKeyword Fb2Handler::GetKeyword(const QString &name)
 {
     static KeywordHash map;
-    return map[name];
+    KeywordHash::const_iterator i = map.find(name);
+    return i == map.end() ? Empty : i.value();
 }
 
 Fb2Handler::Fb2Handler(QTextEdit * editor)
@@ -50,6 +63,9 @@ bool Fb2Handler::startElement(const QString & /* namespaceURI */,
                                const QString &qName,
                                const QXmlAttributes &attributes)
 {
+    m_text.clear();
+    m_name.clear();
+
     const QString name = qName.toLower();
 
     switch (m_tags.count()) {
@@ -61,6 +77,11 @@ bool Fb2Handler::startElement(const QString & /* namespaceURI */,
         } break;
         case 1: {
                 m_section = GetSection(name);
+                switch (m_section) {
+                    case Binary: {
+                        m_name = Value(attributes, "id");
+                    } break;
+                };
         } break;
         default: {
             switch (m_section) {
@@ -69,8 +90,6 @@ bool Fb2Handler::startElement(const QString & /* namespaceURI */,
         } break;
     }
 
-    m_text.clear();
-
     m_tags << name;
 
     return true;
@@ -82,6 +101,14 @@ bool Fb2Handler::endElement(const QString & /* namespaceURI */,
 {
     const QString name = qName.toLower();
 
+    switch (m_section) {
+        case Binary: {
+            QByteArray in; in.append(m_text);
+            QImage img = QImage::fromData(QByteArray::fromBase64(in));
+            if (!m_name.isEmpty()) m_editor->document()->addResource(QTextDocument::ImageResource, QUrl(m_name), img);
+        } break;
+    }
+
     if (name == "section") {
         if (!m_frames.isEmpty()) {
             m_cursor.setPosition(m_frames.last()->lastPosition());
@@ -142,15 +169,9 @@ bool Fb2Handler::BodyNew(const QString &name, const QXmlAttributes &attributes)
             m_cursor.insertFrame(format);
         } break;
         case Image: {
-            int count = attributes.count();
-            for (int i = 0; i < count; i++ ) {
-                if (attributes.localName(i).compare("href", Qt::CaseInsensitive) == 0) {
-                    QString image = attributes.value(i);
-                    while (image.left(1) == "#") image.remove(0, 1);
-                    m_cursor.insertImage(image);
-                    break;
-                }
-            }
+            QString image = Value(attributes, "href");
+            while (image.left(1) == "#") image.remove(0, 1);
+            if (!image.isEmpty()) m_cursor.insertImage(image);
         } break;
     }
     return true;

+ 6 - 4
source/fb2read.h

@@ -22,13 +22,14 @@ public:
 
 private:
     enum DocSection {
-        None   = 0x0000,
-        Body   = 0x0001,
-        Descr  = 0x0002,
-        Bynary = 0x0003,
+        None = 0,
+        Body,
+        Descr,
+        Binary,
     };
 
     enum DocKeyword {
+        Empty = 0,
         Image,
         Paragraph,
         Section,
@@ -50,6 +51,7 @@ private:
 private:
     QTextEdit * m_editor;
     QTextCursor m_cursor;
+    QString m_name;
     QString m_text;
     QString m_error;
     QStringList m_tags;