Дмитрий
ДмитрийSept. 22, 2018, 4:16 a.m.

Reader fb2-files on Qt Creator

Some time ago I wrote an article in which I showed how to open a fb2 file with Qt tools. After some time, I noticed a number of shortcomings in it, which I decided to eliminate. Moreover, I found that some fb2 readers also have disadvantages (namely, incorrect display of tables), which prompted me to write this article. For starters, you can read the last article . We will act on the same principle: we form the book string in html format and place it in the QTextBrowser object.

Let me remind you that in order to create an html document, you need to perform 3 actions: open the tag, fill it with content and close it. Therefore, there are 4 options for us: we rewrite from the source file, rewrite with corrections, do nothing (ignore), and conduct special processing.


Introduction

All fb2 format tags can be divided into 3 groups identical to html, similar to html, and others.

Identical tags are shown in table 1.

Table 1 - fb2 and html tags

description tag
Paragraph p
Link a
Table table
Table row tr
Table cell td
Table header cell th
Superscript sup
Subscript sub
Monospace font code
Bold font strong
Quote cite

Similar tags are identical to the html element by purpose, but have different names. All correspondences are given in table 2.

Table 2 - Correspondence between fb2 and html tags

description fb2 html
Empty line empty-line br
Italics emphasis i
Strikethrough strikethrough strike

Algorithm

The file opening algorithm is implemented as the openFB2File function. Other tags do not have clear correspondence with html elements or are similar, but they need special processing (<a>, <body>, <image>). Some of them can simply be ignored (<poem>). To convert the main part (<v>, <date>, <text-author>, <title>, <subtitle>), I suggest that they correspond <p> with some additional attributes. The algorithm is implemented as follows:

    QFile f(file);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "файл не открыт";
        return false;
    }
    bool ok = true;
    QString special;
    QString description; // описание видео
    //  настройки отображения
    int fontSize = 20;
    if( QSysInfo::productType() == "android" )
        fontSize *= 1.8;

    QXmlStreamReader sr(&f);
    QString rId;
    QString rType;

    QString opt;

    QStringList thisToken;

    while( !sr.atEnd() )
    {
        switch( sr.readNext() )
        {
        case QXmlStreamReader::NoToken:
            qDebug() << "QXmlStreamReader::NoToken";
            break;
        case QXmlStreamReader::StartDocument:
            *book = "<!DOCTYPE HTML><html><body style=\"font-size:%1px; font-family:Sans, Times New Roman;\">";
            *book = book->arg(fontSize);
            break;
        case QXmlStreamReader::EndDocument:
            book->append("</body></html>");
            break;
        case QXmlStreamReader::StartElement:
            thisToken.append( sr.name().toString() );

            if(thisToken.contains("description")) // ОПИСАНИЕ КНИГИ
            {
                if( thisToken.back() != "image" ) //пропускаем всё кроме обложки
                    break; // не выводим
            }

            if(sr.name().toString() == "title")
            {
                content->append(""); // добавляем пункт содержания
                break;
            }

            if( sr.name().toString() == "body" )
                if( !sr.attributes().isEmpty()
                    && sr.attributes().first().value().toString() == "notes")
                    special = "notes";  // режим примечаний

            if(special == "notes")
            {
                if( sr.name().toString() == "section" )
                {
                    if( sr.attributes().count() > 0 )
                    {
                        rId = sr.attributes().at(0).value().toString(); // ссылка на текст
                        rType = "";
                    }
                }
            }

            opt = " align=\"justify\"";
            if(thisToken.contains("title") )
            {
                opt = " align=\"center\" style=\"font-size:" +QString::number(int(fontSize * 1.5)) + "px\" ";
                if(special == "notes")
                {
                    opt += (" id=\"" + rId + "\"");
                }
            }
            if(thisToken.contains("subtitle") )
            {
                opt = " align=\"center\" style=\"font-size:" +QString::number(int(fontSize * 1.2)) + "px\" ";
            }
            if(thisToken.contains("annotation") )
            {
                opt = " align=\"left\" ";
            }

            if(sr.name().toString() == "p"
                    || sr.name().toString() == "subtitle")
            {
                book->append("<p"+opt +" >");
                break;
            }

            if( sr.name().toString() == "table" )
            {
                QString text;
                for(int i = 0; i < sr.attributes().count(); i++)
                {
                    if(sr.attributes().at(i).name() == "id")
                        qDebug() << sr.attributes().at(i).value().toString();
                    if(sr.attributes().at(i).name() == "style")
                        text.append( "style=\"" +sr.attributes().at(i).value().toString()+ ";\"" );
                }
                book->append("<table border=1 align=\"center\" style=\"border:solid;\" " + text + ">");
                break;
            }
            if( sr.name().toString() == "tr" )
            {
                QString text;
                if(!thisToken.contains("table"))
                    qDebug() << "ошибка в таблице";
                for(int i = 0; i < sr.attributes().count(); i++)
                {
                    if(sr.attributes().at(i).name() == "aling")
                        text.append( "aling=\"" +sr.attributes().at(i).value().toString()+ "\"" );
                    else
                        qDebug() << "<tr>" << sr.attributes().at(i).name() << sr.attributes().at(i).value().toString();
                }
                book->append("<tr " + text + ">");
                break;
            }            //
            if( sr.name().toString() == "td"
                 || sr.name().toString() == "th" )
            {
                if(!thisToken.contains("table"))
                    qDebug() << "ошибка в таблице";
                QString text;
                for(int i = 0; i < sr.attributes().count(); i++)
                {
                    if(sr.attributes().at(i).name() == "aling")
                        text.append( "aling=\"" +sr.attributes().at(i).value().toString()+ "\" " );
                    else if(sr.attributes().at(i).name() == "valing")
                        text.append( "valing=\"" +sr.attributes().at(i).value().toString()+ "\" " );
                    else if(sr.attributes().at(i).name() == "colspan")
                        text.append( "colspan=" +sr.attributes().at(i).value().toString()+ " " );
                    else if(sr.attributes().at(i).name() == "rowspan")
                        text.append( "rowspan=" +sr.attributes().at(i).value().toString()+ " " );
                    else
                        qDebug() << "<td th>" << sr.attributes().at(i).name() << sr.attributes().at(i).value().toString();
                }
                book->append( "<"+sr.name().toString()+ " " + text +">" );
                break;
            }
            if( sr.name().toString() == "empty-line" )
            {
                book->append("<br/>");
                break;
            }
            if(sr.name().toString() == "strong"
                    || sr.name().toString() == "sup"
                    || sr.name().toString() == "sub"
                    || sr.name().toString() == "code"
                    || sr.name().toString() == "cite")
            {
                book->append( "<" + sr.name().toString() + ">");
                break;
            }
            if(sr.name().toString() == "emphasis")
            {
                book->append( "<i>" );
                break;
            }
            if( sr.name().toString() == "v" )
            {
                book->append("<p align=\"left\" style=\"margin-left:25px;\">");
                break;
            }
            if(sr.name().toString() == "strikethrough")
            {
                book->append( "<strike>" );
                break;
            }

            if( sr.name().toString() == "a" ) // метка примечания
            {
                rId = "";
                for(int i = 0; i < sr.attributes().count(); i++)
                {
                    if(sr.attributes().at(i).name() == "type" )
                    {
                        //rType = sr.attributes().at(i).value().toString();
                    }
                    if(sr.attributes().at(i).name() == "href")
                    {
                        rId = sr.attributes().at(i).value().toString();
                    }
                }
                book->append("<a href=\"" + rId + "\"> ");
                //qDebug() << "a" << rId;
            }

            if(sr.name().toString() == "poem"
                    || sr.name().toString() == "stanza"
                    || sr.name().toString() == "epigraph")
            {
                break;
            }

            if(sr.name().toString() == "text-author" ) // автор текстта
            {
                book->append( "<p align=\"justify\" style=\"margin-left:45px;\">" );
                break;
            }
            if(sr.name().toString() == "date" ) // автор текстта
            {
                book->append( "<p align=\"justify\" style=\"margin-left:45px;\">" );
                break;
            }

            if( sr.name().toString() == "image" ) // расположение рисунков
            {
                if(sr.attributes().count() > 0)
                    book->append("<p align=\"center\">"+sr.attributes().at(0).value().toString() + "#" + "</p>");
            }
            if(sr.name() == "binary") // хранилище рисунков
            {
                if(sr.attributes().at(0).name() == "id")
                {
                    rId = sr.attributes().at(0).value().toString();
                    rType = sr.attributes().at(1).value().toString();
                }
                if(sr.attributes().at(1).name() == "id")
                {
                    rId = sr.attributes().at(1).value().toString();
                    rType = sr.attributes().at(0).value().toString();
                }
            }
            break;
        case QXmlStreamReader::EndElement:
            if( thisToken.last() == sr.name().toString() )
            {
                thisToken.removeLast();
            }
            else
                qDebug() << "error token";

            if(thisToken.contains("description")) // ОПИСАНИЕ КНИГИ
            {
                break; // не выводим
            }

            if( sr.name().toString() == "p"
                    || sr.name().toString() == "subtitle"
                    || sr.name().toString() == "v"
                    || sr.name().toString() == "date"
                    || sr.name().toString() == "text-author")
            {
                book->append("</p>");
                break;
            }

            if(sr.name().toString() == "td"
                    || sr.name().toString() == "th"
                    || sr.name().toString() == "tr"
                    || sr.name().toString() == "table"
                    || sr.name().toString() == "sup"
                    || sr.name().toString() == "sub"
                    || sr.name().toString() == "strong"
                    || sr.name().toString() == "code"
                    || sr.name().toString() == "cite")
            {
                book->append( "</"+sr.name().toString()+">" );
                break;
            }

            if( sr.name().toString() == "a" )
            {
                rId.remove("#");
                book->append( "</a><span id=\"" + rId + "___" + "\"></span>" );
                qDebug() << "id" << rId + "___";
                break;
            }

            if(sr.name().toString() == "emphasis")
            {
                book->append( "</i>" );
                break;
            }
            if(sr.name().toString() == "strikethrough")
            {
                book->append( "</strike>" );
                break;
            }

            if(sr.name().toString() == "stanza") // конец строфы
            {
                //book->append("<br/>");
                break;
            }
            if(sr.name().toString() == "epigraph"
                    || sr.name().toString() == "poem")
            {
                break;
            }

            if(special == "notes") // режим извлечения примечаний
            {
                if( sr.name().toString() == "body" )
                {
                    special = "";
                }
                if( sr.name().toString() == "section" )
                {
                    book->insert(book->lastIndexOf("<"), "<a href=\"#" + rId + "___" + "\"> назад</a>");
                }
            }
            break;
        case QXmlStreamReader::Characters:
            if( sr.text().toString() == "" )
            {
                //qDebug() << "isEmpty";
                break;
            }
            if( sr.text().toString() == "\n" )
            {
                //qDebug() << "isEmpty";
                break;
            }

            if(thisToken.contains("description")) // ОПИСАНИЕ КНИГИ
            {
                description.append(sr.text().toString() + " "); // не выводим
                break;
            }

            if(thisToken.contains( "binary" ) ) // для рисунков
            {
                QString image = "<img src=\"data:"
                        + rType +";base64,"
                        + sr.text().toString()
                        + "\"/>";
                book->replace("#"+rId +"#", image);
                rId = "";
                rType = "";

                break;
            }
            if(thisToken.contains("div"))
            {
                qDebug() << "div" << sr.text().toString();
                break;
            }
            if(thisToken.back() == "FictionBook")
            {
                qDebug() << "FictionBook" << sr.text().toString();
                break;
            }

            if( thisToken.contains("title") ) // формируем содержание
            {
                content->back() += " " + sr.text().toString();//content->back()=="" ? "" : " " +
                //  qDebug() << "title" << sr.text().toString();
            }

            if(special == "notes" && !thisToken.contains("title") )  // добавление текста примечания
            {
                rType += " ";
                rType += sr.text().toString();
                //break;
            }

            if(thisToken.back() == "p"
                    || thisToken.back() == "subtitle"
                    || thisToken.back() == "v"
                    || thisToken.back() == "emphasis"
                    || thisToken.back() == "strong"
                    || thisToken.back() == "strikethrough"
                    || thisToken.back() == "sup"
                    || thisToken.back() == "sub"
                    || thisToken.back() == "td"
                    || thisToken.back() == "th"
                    || thisToken.back() == "code"
                    || thisToken.back() == "cite"
                    || thisToken.back() == "text-author"  // ??
                    || thisToken.back() == "date"
                    )
            {
                book->append( sr.text().toString() );
                break;
            }

            if(thisToken.back() == "section")
            {
                break;
            }
            if(thisToken.back() == "body")
            {
                break;
            }
            if(thisToken.back() == "table"
                    || thisToken.back() == "tr"
                    || thisToken.back() == "title"
                    || thisToken.back() == "poem"
                    || thisToken.back() == "stanza")
            {
                //book->append( sr.text().toString() );
                break;
            }
            if(thisToken.back() == "annotation")
            {
                qDebug() << "annotation" << sr.text().toString();
                break;
            }

            if(thisToken.back() == "a")
            {
                book->append( sr.text().toString() );
                break;
            }
            //все прочие тэги
            if( !sr.text().toString().isEmpty() )
            {
                qDebug() << thisToken.back() <<  "исключение" ;
                book->append("<span> " + sr.text().toString() + "</span>");
            }
            break;
        }
    }
    f.close();

The file open function is launched by pressing the button:

    ui->textBrowser->clear();
    QString text;
    QStringList content;

    if(name.endsWith(".fb2"))
    {
        openerTextFiles otf;
        otf.openFB2File(name, &text, &content);
        ui->textBrowser->setText(text);
        ui->textBrowser->verticalScrollBar()->setValue(0);
    }

Now the generated html page can be easily saved

    QFile file(name);
    if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) )
        return;

    QTextStream out(&file);
    out << ui->textBrowser->toHtml();
    file.close();

Our reader is ready.

The source code can be downloaded [here] (https://cloud.mail.ru/public/2aN1/5GQW1s6Rb).

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

ВТ
  • Feb. 6, 2019, 10:58 a.m.
  • (edited)

добавил функцию чтения из fb2.zip чеоез QZipReader

    if (name.endsWith(".zip")) {
        QZipReader unzip(name);
        QByteArray ba;
            QVector<QZipReader::FileInfo> files = unzip.fileInfoList();
            QZipReader::FileInfo fi = files.at(0);
                if (fi.isFile) {
                    ba = unzip.fileData(fi.filePath);
                    if (fi.size != ba.size()) qDebug() << "unzip error";
            }
            unzip.close();
            openerTextFiles otf;
            otf.openFB2File(ba, &text, &content);
            ui->textBrowser->document()->clear();
            ui->textBrowser->setHtml(text);
            ui->textBrowser->verticalScrollBar()->setValue(0);
 }

в.pro добавить: QT += gui-private,
в .h :

#include <QtGui/private/qzipwriter_p.h>
#include <QtGui/private/qzipreader_p.h>

ридер через QByteArray сделал

    bool openFB2File(QByteArray ba, QString *book, QStringList *content);
ВТ
  • Feb. 9, 2019, 3:24 a.m.

ну у меня несколько компактней получилось


tags = {'poem':'i','emphasis':'i', 'stanza':'i','title':'p','title':'p','empty-line':'br',
'subtitle':'p','section' : 'p','strong':'b','v':'p','coverpage':'br',
}

attrs = {
'title':{'align':'center','style':'font-size:24px;'},
'subtitle':{'align':'center','style':'font-size:22px;'},
'table':{'width':'100%','align':'center','border':'1'},
'td':{'align':'center'},
'p':{'align':'justify','style':'font-size:20px;'},
}

class Fb2Reader(QObject):
    def __init__(self,fbdata = None):
        super().__init__()
        self.fbdata = fbdata

    def read(self):
        sr = QXmlStreamReader(self.fbdata)
        ba = QByteArray()
        wr = QXmlStreamWriter(ba)
        wr.setAutoFormatting(True)
        wr.setAutoFormattingIndent(1)
        tokens = []
        name = ''
        d = {}
        images = []
        while not sr.atEnd():
            elem = sr.readNext()
            if sr.hasError() : 
                err = sr.errorString()+':'+str(sr.lineNumber())
                return err
            if elem == QXmlStreamReader.StartDocument:
                wr.writeStartDocument()
            elif elem == QXmlStreamReader.EndDocument:
                wr.writeEndDocument()
            elif elem == QXmlStreamReader.StartElement:
                name = sr.name()
                tokens.append(name)
                if 'description' in tokens and name != 'image':
                    continue
                d = {i.name():i.value() for i in sr.attributes()}
                if name == 'image':
                    link = d.get('href')
                    wr.writeStartElement('p')
                    wr.writeAttribute('align','center')
                    wr.writeCharacters(link)
                    wr.writeEndElement()
                    continue
                tag = tags.get(name,name)
                wr.writeStartElement('',tag)
                attr = attrs.get(name)
                if  attr:
                    for i in attr: 
                        wr.writeAttribute('',i,attr[i])
                else:
                    for i in d: 
                        wr.writeAttribute('',i,d[i])            
            elif elem == QXmlStreamReader.EndElement:
                tokens.pop()
                wr.writeEndElement()
            elif elem == QXmlStreamReader.Characters:
                if 'description' in tokens: continue
                text = sr.text()
                if name == 'binary':
                    link,typ = d.get('id'),d.get('content-type')
                    content = '<img src=\"data:' + typ +';base64,' + text +'\"/>'
                    images.append(('#'+link,content,)) 
                else: wr.writeCharacters(text)
        s = bytes(ba).decode()
        for i in images: 
            s = s.replace(i[0],i[1])
        del images
        return s

```

ВТ
  • Feb. 9, 2019, 3:27 a.m.

и с навигацией вы круто заморочилить - QTextBrowser отслеживает нажатие на линки

        self.edit.anchorClicked.connect(self.on_anchor)
        self.btnBack.setEnabled(False)
        self.btnBack.clicked.connect(self.back_ward)

    def on_anchor(self,args):
        self.cursor_pos = self.edit.textCursor().position()
        self.btnBack.setEnabled(True)

    def back_ward(self):
        self.btnBack.setEnabled(False)
        self.edit.moveCursor(self.cursor_pos)
        self.edit.viewport().repaint()

```

Дмитрий
  • Feb. 26, 2019, 10:37 a.m.

Спасибо за комментарии, а zip-архивация пригодится для открытия книг fb3 и epub. Это работает на Qt без дополнительных библиотек? Я сделал архиватор, но qzipwriter_p.h и qzipreader_p.h, а ещё zlib качал отдельно.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
d
  • dsfs
  • April 26, 2024, 4:56 a.m.

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
d
  • dsfs
  • April 26, 2024, 4:45 a.m.

C++ - Test 002. Constants

  • Result:50points,
  • Rating points-4
d
  • dsfs
  • April 26, 2024, 4:35 a.m.

C++ - Test 001. The first program and data types

  • Result:73points,
  • Rating points1
Last comments
k
kmssrFeb. 8, 2024, 6:43 p.m.
Qt Linux - Lesson 001. Autorun Qt application under Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
Qt WinAPI - Lesson 007. Working with ICMP Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
EVA
EVADec. 25, 2023, 10:30 a.m.
Boost - static linking in CMake project under Windows Ошибка LNK1104 часто возникает, когда компоновщик не может найти или открыть файл библиотеки. В вашем случае, это файл libboost_locale-vc142-mt-gd-x64-1_74.lib из библиотеки Boost для C+…
J
JonnyJoDec. 25, 2023, 8:38 a.m.
Boost - static linking in CMake project under Windows Сделал всё по-как у вас, но выдаёт ошибку [build] LINK : fatal error LNK1104: не удается открыть файл "libboost_locale-vc142-mt-gd-x64-1_74.lib" Хоть убей, не могу понять в чём дел…
G
GvozdikDec. 18, 2023, 9:01 p.m.
Qt/C++ - Lesson 056. Connecting the Boost library in Qt for MinGW and MSVC compilers Для решения твой проблемы добавь в файл .pro строчку "LIBS += -lws2_32" она решит проблему , лично мне помогло.
Now discuss on the forum
G
GarApril 22, 2024, 5:46 a.m.
Clipboard Как скопировать окно целиком в clipb?
DA
Dr Gangil AcademicsApril 20, 2024, 7:45 a.m.
Unlock Your Aesthetic Potential: Explore MSC in Facial Aesthetics and Cosmetology in India Embark on a transformative journey with an msc in facial aesthetics and cosmetology in india . Delve into the intricate world of beauty and rejuvenation, guided by expert faculty and …
a
a_vlasovApril 14, 2024, 6:41 a.m.
Мобильное приложение на C++Qt и бэкенд к нему на Django Rest Framework Евгений, добрый день! Такой вопрос. Верно ли следующее утверждение: Любое Android-приложение, написанное на Java/Kotlin чисто теоретически (пусть и с большими трудностями) можно написать и на C+…
Павел Дорофеев
Павел ДорофеевApril 14, 2024, 2:35 a.m.
QTableWidget с 2 заголовками Вот тут есть кастомный QTableView с многорядностью проект поддерживается, обращайтесь
f
fastrexApril 4, 2024, 4:47 a.m.
Вернуть старое поведение QComboBox, не менять индекс при resetModel Добрый день! У нас много проектов в которых используется QComboBox, в версии 5.5.1, когда модель испускает сигнал resetModel, currentIndex не менялся. В версии 5.15 при resetModel происходит try…

Follow us in social networks