File indexing completed on 2024-05-12 16:06:35

0001 /*
0002     SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef _KDJVU_
0008 #define _KDJVU_
0009 
0010 #include <QColor>
0011 #include <QImage>
0012 #include <QList>
0013 #include <QPolygon>
0014 #include <QRect>
0015 #include <QVariant>
0016 #include <QVector>
0017 
0018 class QDomDocument;
0019 class QFile;
0020 
0021 #ifndef MINIEXP_H
0022 typedef struct miniexp_s *miniexp_t;
0023 #endif
0024 
0025 /**
0026  * @brief Qt (KDE) encapsulation of the DjVuLibre
0027  */
0028 class KDjVu
0029 {
0030 public:
0031     KDjVu();
0032     ~KDjVu();
0033 
0034     KDjVu(const KDjVu &) = delete;
0035     KDjVu &operator=(const KDjVu &) = delete;
0036 
0037     /**
0038      * A DjVu page.
0039      */
0040     class Page
0041     {
0042         friend class KDjVu;
0043 
0044     public:
0045         ~Page();
0046 
0047         int width() const;
0048         int height() const;
0049         int dpi() const;
0050         int orientation() const;
0051 
0052     private:
0053         Page();
0054 
0055         int m_width;
0056         int m_height;
0057         int m_dpi;
0058         int m_orientation;
0059     };
0060 
0061     /**
0062      * The base implementation for a DjVu link.
0063      */
0064     class Link
0065     {
0066         friend class KDjVu;
0067 
0068     public:
0069         Link() = default;
0070         virtual ~Link();
0071 
0072         Link(const Link &) = delete;
0073         Link &operator=(const Link &) = delete;
0074 
0075         enum LinkType { PageLink, UrlLink };
0076         enum LinkArea { UnknownArea, RectArea, EllipseArea, PolygonArea };
0077         virtual int type() const = 0;
0078         LinkArea areaType() const;
0079         QPoint point() const;
0080         QSize size() const;
0081         QPolygon polygon() const;
0082 
0083     private:
0084         LinkArea m_area;
0085         QPoint m_point;
0086         QSize m_size;
0087         QPolygon m_poly;
0088     };
0089 
0090     /**
0091      * A link to reach a page of a DjVu document.
0092      */
0093     class PageLink : public Link
0094     {
0095         friend class KDjVu;
0096 
0097     public:
0098         int type() const override;
0099         QString page() const;
0100 
0101     private:
0102         PageLink();
0103         QString m_page;
0104     };
0105 
0106     /**
0107      * A DjVu link to open an external Url.
0108      */
0109     class UrlLink : public Link
0110     {
0111         friend class KDjVu;
0112 
0113     public:
0114         int type() const override;
0115         QString url() const;
0116 
0117     private:
0118         UrlLink();
0119         QString m_url;
0120     };
0121 
0122     /**
0123      * The base implementation for a DjVu annotation.
0124      */
0125     class Annotation
0126     {
0127         friend class KDjVu;
0128 
0129     public:
0130         virtual ~Annotation();
0131 
0132         Annotation(const Annotation &) = delete;
0133         Annotation &operator=(const Annotation &) = delete;
0134 
0135         enum AnnotationType { TextAnnotation, LineAnnotation };
0136         virtual int type() const = 0;
0137         QPoint point() const;
0138         QString comment() const;
0139         void setComment(const QString &comment);
0140         virtual QColor color() const;
0141         virtual void setColor(const QColor &color);
0142 
0143     protected:
0144         explicit Annotation(miniexp_t anno);
0145 
0146         miniexp_t m_anno;
0147         QPoint m_point;
0148     };
0149 
0150     /**
0151      * A DjVu text annotation.
0152      */
0153     class TextAnnotation : public Annotation
0154     {
0155         friend class KDjVu;
0156 
0157     public:
0158         int type() const override;
0159         QColor color() const override;
0160         void setColor(const QColor &color) override;
0161         QSize size() const;
0162         bool inlineText() const;
0163 
0164     private:
0165         explicit TextAnnotation(miniexp_t anno);
0166         QSize m_size;
0167         bool m_inlineText;
0168     };
0169 
0170     /**
0171      * A DjVu line annotation.
0172      */
0173     class LineAnnotation : public Annotation
0174     {
0175         friend class KDjVu;
0176 
0177     public:
0178         int type() const override;
0179         QColor color() const override;
0180         void setColor(const QColor &color) override;
0181         QPoint point2() const;
0182         bool isArrow() const;
0183         int width() const;
0184         void setWidth(int width);
0185 
0186     private:
0187         explicit LineAnnotation(miniexp_t anno);
0188         QPoint m_point2;
0189         bool m_isArrow;
0190         miniexp_t m_width;
0191     };
0192 
0193     /**
0194      * A DjVu text entity.
0195      */
0196     class TextEntity
0197     {
0198         friend class KDjVu;
0199 
0200     public:
0201         ~TextEntity();
0202 
0203         QString text() const;
0204         QRect rect() const;
0205 
0206     private:
0207         TextEntity();
0208 
0209         QString m_text;
0210         QRect m_rect;
0211     };
0212 
0213     /**
0214      * Opens the file \p fileName, closing the old one if necessary.
0215      */
0216     bool openFile(const QString &fileName);
0217     /**
0218      * Close the file currently opened, if any.
0219      */
0220     void closeFile();
0221 
0222     /**
0223      * The pages of the current document, or an empty vector otherwise.
0224      * \note KDjVu handles the pages, so you don't need to delete them manually
0225      * \return a vector with the pages of the current document
0226      */
0227     const QVector<KDjVu::Page *> &pages() const;
0228 
0229     /**
0230      * Get the metadata for the specified \p key, or a null variant otherwise.
0231      */
0232     QVariant metaData(const QString &key) const;
0233 
0234     /**
0235      * Get an XML document with the bookmarks of the current document (if any).
0236      * The XML will look like this:
0237      * \verbatim
0238      * <!DOCTYPE KDjVuBookmarks>
0239      * <item title="Title 1" destination="dest1">
0240      *   <item title="Title 1.1" destination="dest1.1" />
0241      *   ...
0242      * </item>
0243      * <item title="Title 2" destination="dest2">
0244      * \endverbatim
0245      */
0246     const QDomDocument *documentBookmarks() const;
0247 
0248     /**
0249      * Reads the links and the annotations for the page \p pageNum
0250      *
0251      * For both \p links and \p annotations , you can pass either a valid pointer
0252      * (in case you want to extract that kind of information), or a null pointer
0253      * (if you don't want that information).
0254      */
0255     void linksAndAnnotationsForPage(int pageNum, QList<KDjVu::Link *> *links, QList<KDjVu::Annotation *> *annotations) const;
0256 
0257     /**
0258      * Check if the image for the specified \p page with the specified
0259      * \p width, \p height and \p rotation is already in cache, and returns
0260      * it. If not, a null image is returned.
0261      */
0262     QImage image(int page, int width, int height, int rotation);
0263 
0264     /**
0265      * Export the currently open document as PostScript file \p fileName.
0266      * \returns whether the exporting was successful
0267      */
0268     bool exportAsPostScript(const QString &fileName, const QList<int> &pageList) const;
0269 
0270     /**
0271      * Export the currently open document as PostScript file.
0272      * \returns whether the exporting was successful
0273      */
0274     bool exportAsPostScript(QFile *file, const QList<int> &pageList) const;
0275 
0276     /**
0277      * Return the list of the text entities for the specified \p page, that matches the
0278      * specified \p granularity.
0279      */
0280     QList<KDjVu::TextEntity> textEntities(int page, const QString &granularity) const;
0281 
0282     /**
0283      * Enable or disable the internal rendered pages cache.
0284      */
0285     void setCacheEnabled(bool enable);
0286     /**
0287      * \returns whether the internal rendered pages cache is enabled
0288      */
0289     bool isCacheEnabled() const;
0290 
0291     /**
0292      * Return the page number of the page whose title is \p name.
0293      */
0294     int pageNumber(const QString &name) const;
0295 
0296 private:
0297     class Private;
0298     Private *const d;
0299 };
0300 
0301 #endif