fb2read.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include <QtDebug>
  4. #include "fb2read.h"
  5. #define FB2_BEGIN_KEYHASH(x) \
  6. Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
  7. { \
  8. static const KeywordHash map; \
  9. KeywordHash::const_iterator i = map.find(name); \
  10. return i == map.end() ? None : i.value(); \
  11. } \
  12. Fb2Handler::x::KeywordHash::KeywordHash() {
  13. #define FB2_END_KEYHASH }
  14. static QString Value(const QXmlAttributes &attributes, const QString &name)
  15. {
  16. int count = attributes.count();
  17. for (int i = 0; i < count; i++ ) {
  18. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  19. return attributes.value(i);
  20. }
  21. }
  22. return QString();
  23. }
  24. //---------------------------------------------------------------------------
  25. // Fb2Handler::BaseHandler
  26. //---------------------------------------------------------------------------
  27. Fb2Handler::BaseHandler::~BaseHandler()
  28. {
  29. if (m_handler) delete m_handler;
  30. }
  31. bool Fb2Handler::BaseHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  32. {
  33. if (m_handler) return m_handler->doStart(name, attributes);
  34. m_handler = new BaseHandler(name);
  35. return true;
  36. }
  37. bool Fb2Handler::BaseHandler::doText(const QString &text)
  38. {
  39. if (m_handler) return m_handler->doText(text);
  40. return true;
  41. }
  42. bool Fb2Handler::BaseHandler::doEnd(const QString &name, bool & exit)
  43. {
  44. if (m_handler) {
  45. bool ok = m_handler->doEnd(name, exit);
  46. if (exit) {
  47. delete m_handler;
  48. m_handler = NULL;
  49. exit = false;
  50. }
  51. return ok;
  52. } else {
  53. if (name != m_name) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
  54. exit = true;
  55. return true;
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. // Fb2Handler::RootHandler
  60. //---------------------------------------------------------------------------
  61. FB2_BEGIN_KEYHASH(RootHandler)
  62. insert("stylesheet", Style);
  63. insert("description", Descr);
  64. insert("body", Body);
  65. insert("binary", Binary);
  66. FB2_END_KEYHASH
  67. Fb2Handler::RootHandler::RootHandler(Fb2MainDocument &document, const QString &name)
  68. : BaseHandler(name)
  69. , m_document(document)
  70. , m_cursor1(&document)
  71. , m_cursor2(&document.child())
  72. , m_empty(true)
  73. {
  74. m_cursor1.beginEditBlock();
  75. m_cursor2.beginEditBlock();
  76. }
  77. Fb2Handler::RootHandler::~RootHandler()
  78. {
  79. m_cursor1.endEditBlock();
  80. m_cursor2.endEditBlock();
  81. }
  82. bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  83. {
  84. if (m_handler) return m_handler->doStart(name, attributes);
  85. switch (toKeyword(name)) {
  86. case Descr : m_handler = new DescrHandler(name); break;
  87. case Body : m_handler = new BodyHandler(m_empty ? m_cursor1 : m_cursor2, name); m_empty = false; break;
  88. case Binary : m_handler = new BinaryHandler(m_document, name, attributes); break;
  89. default:
  90. qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
  91. m_handler = new BaseHandler(name);
  92. }
  93. return true;
  94. }
  95. //---------------------------------------------------------------------------
  96. // Fb2Handler::DescrHandler
  97. //---------------------------------------------------------------------------
  98. bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  99. {
  100. Q_UNUSED(name);
  101. Q_UNUSED(attributes);
  102. return true;
  103. }
  104. bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
  105. {
  106. Q_UNUSED(name);
  107. if (name == "description") exit = true;
  108. return true;
  109. }
  110. //---------------------------------------------------------------------------
  111. // Fb2Handler::TextHandler
  112. //---------------------------------------------------------------------------
  113. Fb2Handler::TextHandler::TextHandler(QTextCursor &cursor, const QString &name)
  114. : BaseHandler(name)
  115. , m_cursor(cursor)
  116. , m_blockFormat(m_cursor.blockFormat())
  117. , m_charFormat(m_cursor.charFormat())
  118. {
  119. }
  120. Fb2Handler::TextHandler::TextHandler(TextHandler &parent, const QString &name)
  121. : BaseHandler(name)
  122. , m_cursor(parent.m_cursor)
  123. , m_blockFormat(m_cursor.blockFormat())
  124. , m_charFormat(m_cursor.charFormat())
  125. {
  126. }
  127. bool Fb2Handler::TextHandler::doEnd(const QString &name, bool & exit)
  128. {
  129. bool ok = BaseHandler::doEnd(name, exit);
  130. if (!m_handler) cursor().setCharFormat(m_charFormat);
  131. return ok;
  132. }
  133. //---------------------------------------------------------------------------
  134. // Fb2Handler::BodyHandler
  135. //---------------------------------------------------------------------------
  136. FB2_BEGIN_KEYHASH(BodyHandler)
  137. insert("image", Image);
  138. insert("title", Title);
  139. insert("epigraph", Epigraph);
  140. insert("section", Section);
  141. insert("p", Paragraph);
  142. insert("poem", Poem);
  143. insert("stanza", Stanza);
  144. insert("v", Verse);
  145. FB2_END_KEYHASH
  146. Fb2Handler::BodyHandler::BodyHandler(QTextCursor &cursor, const QString &name)
  147. : TextHandler(cursor, name)
  148. , m_feed(false)
  149. {
  150. QTextBlockFormat blockFormat;
  151. blockFormat.setTopMargin(4);
  152. blockFormat.setBottomMargin(4);
  153. cursor.setBlockFormat(blockFormat);
  154. }
  155. bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  156. {
  157. if (m_handler) return m_handler->doStart(name, attributes);
  158. switch (toKeyword(name)) {
  159. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  160. case Image : m_handler = new ImageHandler(*this, name, attributes); break;
  161. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  162. case Title : m_handler = new TitleHandler(*this, name, attributes); break;
  163. case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
  164. case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
  165. default : m_handler = new BaseHandler(name); break;
  166. }
  167. return true;
  168. }
  169. //---------------------------------------------------------------------------
  170. // Fb2Handler::SectionHandler
  171. //---------------------------------------------------------------------------
  172. FB2_BEGIN_KEYHASH(SectionHandler)
  173. insert("title", Title);
  174. insert("epigraph", Epigraph);
  175. insert("image", Image);
  176. insert("annotation", Annotation);
  177. insert("section", Section);
  178. insert("p", Paragraph);
  179. insert("poem", Poem);
  180. insert("subtitle", Subtitle);
  181. insert("cite", Cite);
  182. insert("empty-line", Emptyline);
  183. insert("table", Table);
  184. FB2_END_KEYHASH
  185. Fb2Handler::SectionHandler::SectionHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  186. : TextHandler(parent, name)
  187. , m_frame(cursor().currentFrame())
  188. , m_feed(false)
  189. {
  190. Q_UNUSED(attributes);
  191. QTextFrameFormat frameFormat;
  192. frameFormat.setBorder(1);
  193. frameFormat.setPadding(8);
  194. frameFormat.setTopMargin(4);
  195. frameFormat.setBottomMargin(4);
  196. if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
  197. cursor().insertFrame(frameFormat);
  198. QTextBlockFormat blockFormat;
  199. blockFormat.setTopMargin(4);
  200. blockFormat.setBottomMargin(4);
  201. cursor().setBlockFormat(blockFormat);
  202. }
  203. bool Fb2Handler::SectionHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  204. {
  205. if (m_handler) return m_handler->doStart(name, attributes);
  206. Keyword keyword = toKeyword(name);
  207. switch (keyword) {
  208. case Paragraph:
  209. case Emptyline:
  210. case Image:
  211. if (m_feed) cursor().insertBlock();
  212. m_feed = true;
  213. break;
  214. default:
  215. m_feed = false;
  216. }
  217. switch (keyword) {
  218. case Emptyline : m_handler = new BaseHandler(name); break;
  219. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  220. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  221. case Title : m_handler = new TitleHandler(*this, name, attributes); break;
  222. case Poem : m_handler = new PoemHandler(*this, name, attributes); break;
  223. case Image : m_handler = new ImageHandler(*this, name, attributes); break;
  224. default : m_handler = new BaseHandler(name); break;
  225. }
  226. return true;
  227. }
  228. bool Fb2Handler::SectionHandler::doEnd(const QString &name, bool & exit)
  229. {
  230. bool ok = BaseHandler::doEnd(name, exit);
  231. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  232. return ok;
  233. }
  234. //---------------------------------------------------------------------------
  235. // Fb2Handler::TitleHandler
  236. //---------------------------------------------------------------------------
  237. FB2_BEGIN_KEYHASH(TitleHandler)
  238. insert("p", Paragraph);
  239. insert("empty-line", Emptyline);
  240. FB2_END_KEYHASH
  241. Fb2Handler::TitleHandler::TitleHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  242. : TextHandler(parent, name)
  243. , m_frame(cursor().currentFrame())
  244. , m_table(NULL)
  245. , m_feed(false)
  246. {
  247. Q_UNUSED(attributes);
  248. QTextTableFormat format1;
  249. format1.setBorder(0);
  250. format1.setCellPadding(4);
  251. format1.setCellSpacing(4);
  252. format1.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  253. m_table = cursor().insertTable(1, 1, format1);
  254. QTextTableCellFormat format2;
  255. format2.setBackground(Qt::darkGreen);
  256. m_table->cellAt(cursor()).setFormat(format2);
  257. QTextCharFormat format3 = cursor().charFormat();
  258. format3.setForeground(Qt::white);
  259. m_table->cellAt(cursor()).setFormat(format3);
  260. }
  261. bool Fb2Handler::TitleHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  262. {
  263. if (m_handler) return m_handler->doStart(name, attributes);
  264. if (m_feed) cursor().insertBlock(); else m_feed = true;
  265. switch (toKeyword(name)) {
  266. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  267. case Emptyline : m_handler = new BlockHandler(*this, name, attributes); break;
  268. default: m_handler = new BaseHandler(name); break;
  269. }
  270. return true;
  271. }
  272. bool Fb2Handler::TitleHandler::doEnd(const QString &name, bool & exit)
  273. {
  274. bool ok = BaseHandler::doEnd(name, exit);
  275. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  276. return ok;
  277. }
  278. //---------------------------------------------------------------------------
  279. // Fb2Handler::PoemHandler
  280. //---------------------------------------------------------------------------
  281. FB2_BEGIN_KEYHASH(PoemHandler)
  282. insert("title", Title);
  283. insert("epigraph", Epigraph);
  284. insert("stanza", Stanza);
  285. insert("author", Author);
  286. insert("date", Date);
  287. FB2_END_KEYHASH
  288. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  289. : TextHandler(parent, name)
  290. , m_frame(cursor().currentFrame())
  291. , m_table(NULL)
  292. , m_feed(false)
  293. {
  294. Q_UNUSED(attributes);
  295. QTextTableFormat format;
  296. format.setBorder(1);
  297. format.setCellPadding(4);
  298. format.setCellSpacing(4);
  299. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  300. m_table = cursor().insertTable(1, 1, format);
  301. }
  302. bool Fb2Handler::PoemHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  303. {
  304. if (m_handler) return m_handler->doStart(name, attributes);
  305. Keyword keyword = toKeyword(name);
  306. switch (keyword) {
  307. case Title:
  308. case Epigraph:
  309. case Stanza:
  310. case Author:
  311. if (m_feed) m_table->appendRows(1);
  312. m_feed = true;
  313. m_handler = new StanzaHandler(*this, name, attributes);
  314. break;
  315. default:
  316. m_handler = new BaseHandler(name);
  317. }
  318. return true;
  319. }
  320. bool Fb2Handler::PoemHandler::doEnd(const QString &name, bool & exit)
  321. {
  322. bool ok = BaseHandler::doEnd(name, exit);
  323. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  324. return ok;
  325. }
  326. //---------------------------------------------------------------------------
  327. // Fb2Handler::StanzaHandler
  328. //---------------------------------------------------------------------------
  329. FB2_BEGIN_KEYHASH(StanzaHandler)
  330. insert("title", Title);
  331. insert("subtitle", Subtitle);
  332. insert("v", Verse);
  333. FB2_END_KEYHASH
  334. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  335. : TextHandler(parent, name)
  336. , m_feed(false)
  337. {
  338. Q_UNUSED(attributes);
  339. }
  340. bool Fb2Handler::StanzaHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  341. {
  342. if (m_handler) return m_handler->doStart(name, attributes);
  343. Keyword keyword = toKeyword(name);
  344. switch (keyword) {
  345. case Title:
  346. case Subtitle:
  347. case Verse:
  348. if (m_feed) cursor().insertBlock();
  349. m_feed = true;
  350. default: ;
  351. }
  352. switch (keyword) {
  353. case Title:
  354. case Subtitle:
  355. m_handler = new TitleHandler(*this, name, attributes); break;
  356. case Verse:
  357. m_handler = new BlockHandler(*this, name, attributes); break;
  358. default:
  359. m_handler = new BaseHandler(name); break;
  360. }
  361. return true;
  362. }
  363. //---------------------------------------------------------------------------
  364. // Fb2Handler::BlockHandler
  365. //---------------------------------------------------------------------------
  366. FB2_BEGIN_KEYHASH(BlockHandler)
  367. insert("strong" , Strong);
  368. insert("emphasis" , Emphasis);
  369. insert("style" , Style);
  370. insert("a" , Anchor);
  371. insert("strikethrough" , Strikethrough);
  372. insert("sub" , Sub);
  373. insert("sup" , Sup);
  374. insert("code" , Code);
  375. insert("image" , Image);
  376. FB2_END_KEYHASH
  377. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  378. : TextHandler(parent, name)
  379. {
  380. Q_UNUSED(attributes);
  381. QTextBlockFormat blockFormat;
  382. blockFormat.setTopMargin(4);
  383. blockFormat.setBottomMargin(4);
  384. cursor().setBlockFormat(blockFormat);
  385. }
  386. bool Fb2Handler::BlockHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  387. {
  388. Q_UNUSED(attributes);
  389. if (m_handler) return m_handler->doStart(name, attributes);
  390. switch (toKeyword(name)) {
  391. default: m_handler = new BaseHandler(name); break;
  392. }
  393. return true;
  394. }
  395. bool Fb2Handler::BlockHandler::doText(const QString &text)
  396. {
  397. if (m_handler) return m_handler->doText(text);
  398. cursor().insertText(text);
  399. return true;
  400. }
  401. //---------------------------------------------------------------------------
  402. // Fb2Handler::ImageHandler
  403. //---------------------------------------------------------------------------
  404. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  405. : TextHandler(parent, name)
  406. {
  407. QString image = Value(attributes, "href");
  408. while (image.left(1) == "#") image.remove(0, 1);
  409. if (!image.isEmpty()) cursor().insertImage(image);
  410. }
  411. bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  412. {
  413. Q_UNUSED(name);
  414. Q_UNUSED(attributes);
  415. return false;
  416. }
  417. //---------------------------------------------------------------------------
  418. // Fb2Handler::BinaryHandler
  419. //---------------------------------------------------------------------------
  420. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  421. : BaseHandler(name)
  422. , m_document(document)
  423. , m_file(Value(attributes, "id"))
  424. {
  425. }
  426. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  427. {
  428. Q_UNUSED(name);
  429. Q_UNUSED(attributes);
  430. return false;
  431. }
  432. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  433. {
  434. m_text += text;
  435. return true;
  436. }
  437. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  438. {
  439. Q_UNUSED(name);
  440. QByteArray in; in.append(m_text);
  441. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  442. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  443. exit = true;
  444. return true;
  445. }
  446. //---------------------------------------------------------------------------
  447. // Fb2Handler
  448. //---------------------------------------------------------------------------
  449. Fb2Handler::Fb2Handler(Fb2MainDocument & document)
  450. : m_document(document)
  451. , m_handler(NULL)
  452. {
  453. document.clear();
  454. m_document.child().clear();
  455. }
  456. Fb2Handler::~Fb2Handler()
  457. {
  458. m_document.clearUndoRedoStacks();
  459. m_document.child().clearUndoRedoStacks();
  460. m_document.setModified(false);
  461. m_document.child().setModified(false);
  462. if (m_handler) delete m_handler;
  463. }
  464. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  465. {
  466. Q_UNUSED(namespaceURI);
  467. Q_UNUSED(localName);
  468. const QString name = qName.toLower();
  469. if (m_handler) return m_handler->doStart(name, attributes);
  470. if (name == "fictionbook") {
  471. m_handler = new RootHandler(m_document, name);
  472. return true;
  473. } else {
  474. m_error = QObject::tr("The file is not an FB2 file.");
  475. return false;
  476. }
  477. }
  478. static bool isWhiteSpace(const QString &str)
  479. {
  480. return str.simplified().isEmpty();
  481. }
  482. bool Fb2Handler::characters(const QString &str)
  483. {
  484. QString s = str.simplified();
  485. if (s.isEmpty()) return true;
  486. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  487. if (isWhiteSpace(str.right(1))) s.append(" ");
  488. return m_handler && m_handler->doText(s);
  489. }
  490. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  491. {
  492. Q_UNUSED(namespaceURI);
  493. Q_UNUSED(localName);
  494. bool exit = false;
  495. return m_handler && m_handler->doEnd(qName.toLower(), exit);
  496. }
  497. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  498. {
  499. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  500. .arg(exception.lineNumber())
  501. .arg(exception.columnNumber())
  502. .arg(exception.message());
  503. return false;
  504. }
  505. QString Fb2Handler::errorString() const
  506. {
  507. return m_error;
  508. }
  509. #undef FB2_BEGIN_KEYHASH
  510. #undef FB2_END_KEYHASH