File indexing completed on 2024-05-12 04:33:58

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
0002 //
0003 // ghostscript_interface
0004 //
0005 // Part of KDVI - A framework for multipage text/gfx viewers
0006 //
0007 // SPDX-FileCopyrightText: 2004 Stefan Kebekus
0008 // SPDX-License-Identifier: GPL-2.0-or-later
0009 
0010 #ifndef _PSGS_H_
0011 #define _PSGS_H_
0012 
0013 #include <QApplication>
0014 #include <QColor>
0015 #include <QEvent>
0016 #include <QHash>
0017 #include <QObject>
0018 
0019 class QUrl;
0020 class PageNumber;
0021 class QPainter;
0022 
0023 class pageInfo
0024 {
0025 public:
0026     explicit pageInfo(const QString &_PostScriptString);
0027     ~pageInfo();
0028 
0029     pageInfo(const pageInfo &) = delete;
0030     pageInfo &operator=(const pageInfo &) = delete;
0031 
0032     QColor background;
0033     QColor permanentBackground;
0034     QString *PostScriptString;
0035 };
0036 
0037 class ghostscript_interface : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     ghostscript_interface();
0043     ~ghostscript_interface() override;
0044 
0045     void clear();
0046 
0047     // sets the PostScript which is used on a certain page
0048     void setPostScript(const quint16 page, const QString &PostScript);
0049 
0050     // sets path from additional postscript files may be read
0051     void setIncludePath(const QString &_includePath);
0052 
0053     // Sets the background color for a certain page. If permanent is false then the original
0054     // background color can be restored by calling restoreBackground(page).
0055     // The Option permanent = false is used when we want to display a different paper
0056     // color as the one specified in the dvi file.
0057     void setBackgroundColor(const quint16 page, const QColor &background_color, bool permanent = true);
0058 
0059     // Restore the background to the color which was specified by the last call to setBackgroundColor()
0060     // With option permanent = true.
0061     void restoreBackgroundColor(const quint16 page);
0062 
0063     // Draws the graphics of the page into the painter, if possible. If
0064     // the page does not contain any graphics, nothing happens
0065     void graphics(const quint16 page, double dpi, long magnification, QPainter *paint);
0066 
0067     // Returns the background color for a certain page. If no color was
0068     // set, Qt::white is returned.
0069     QColor getBackgroundColor(const quint16 page) const;
0070 
0071     QString *PostScriptHeaderString;
0072 
0073     /** This method tries to find the PostScript file 'filename' in the
0074         DVI file's directory (if the base-URL indicates that the DVI file
0075         is local), and, if that fails, uses kpsewhich to find the file. If
0076         the file is found, the full path (including file name) is
0077         returned. Otherwise, the method returns the first argument. TODO:
0078         use the DVI file's baseURL, once this is implemented.
0079     */
0080     static QString locateEPSfile(const QString &filename, const QUrl &base);
0081 
0082 private:
0083     void gs_generate_graphics_file(const quint16 page, const QString &filename, long magnification);
0084     QHash<quint16, pageInfo *> pageList;
0085 
0086     double resolution; // in dots per inch
0087     int pixel_page_w;  // in pixels
0088     int pixel_page_h;  // in pixels
0089 
0090     QString includePath;
0091 
0092     // Output device that ghostscript is supposed tp use. Default is
0093     // "png256". If that does not work, gs_generate_graphics_file will
0094     // automatically try other known device drivers. If no known output
0095     // device can be found, something is badly wrong. In that case,
0096     // "gsDevice" is set to an empty string, and
0097     // gs_generate_graphics_file will return immediately.
0098     QList<QString>::iterator gsDevice;
0099 
0100     // A list of known devices, set by the constructor. This includes
0101     // "png256", "pnm". If a device is found to not work, its name is
0102     // removed from the list, and another device name is tried.
0103     QStringList knownDevices;
0104 
0105 Q_SIGNALS:
0106     /** Passed through to the top-level kpart. */
0107     void error(const QString &message, int duration);
0108 };
0109 
0110 #endif