File: PrinterWE.cpp

package info (click to toggle)
eln 1.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,876 kB
  • sloc: cpp: 26,606; perl: 796; python: 437; sh: 73; makefile: 32; xml: 8
file content (216 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// webgrab/PrinterWE.cpp - This file is part of eln

/* eln is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   eln is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with eln.  If not, see <http://www.gnu.org/licenses/>.
*/

// PrinterWE.C

#include "PrinterWE.h"
#include <QPrinter>
#include <QPainter>
#include <QTemporaryFile>
#include <QProcess>
#include <QPrintDialog>
#include <QDebug>
#include <QWebEngineView>
#include <QApplication>
#include <stdio.h>
#include <QSvgGenerator>
#include <QFile>
#include <QPdfDocument>

PrinterWE::PrinterWE(QWebEngineView *src, Options const &opt):
  src(src), opt(opt) {
    havesize = false;
}

PrinterWE::~PrinterWE() {
}

void PrinterWE::display() {
  QSize si = src->page()->contentsSize().toSize();
  if (!si.isEmpty() && !havesize) {
      havesize = true;
    si += QSize(4, 4);
    if (si.width()>1100) {
      si.setWidth(1100);
      si.setHeight(si.height() + 30); // make space for scroll bar
    }
    if (si.height()>1100) {
      si.setHeight(1100);
      si.setWidth(si.width() + 30); // make space for scroll bar
    }
    src->resize(si); // adjust window size
  }
  QSize si2 = src->page()->contentsSize().toSize();
}


void PrinterWE::complete(bool ok) {
  qDebug() << "complete";
  if (!ok) {
    fprintf(stderr, "webgrab: Failed to load web page");
    QApplication::exit(2);
    return;
  }
  if (opt.out.isEmpty()) {
    src->show();
  } else {
    foreach (QString fn, opt.out) {
      if (fn.endsWith(".pdf")) {
        toPdf(fn);
        return;
      }
    }
    // no pdf is requested, we'll make a temporary pdf
    toPdf("");
  }
}

void PrinterWE::sizeChange(QSizeF const &) {
  display();
}

void PrinterWE::toPdf(QString fn) {
  if (opt.paginate)
    toMultiPagePdf(fn);
  else
    toSinglePagePdf(fn);
}


static bool allequalx(QImage const &img, int y, int x0, int x1, QRgb bg) {
  for (int x=x0; x<x1; x++)
    if (img.pixel(x,y)!=bg)
      return false;
  return true;
}

static bool allequaly(QImage const &img, int x, int y0, int y1, QRgb bg) {
  for (int y=y0; y<y1; y++)
    if (img.pixel(x,y)!=bg)
      return false;
  return true;
}


static QImage autotrim(QImage const &img) {
  int X = img.width();
  int Y = img.height();
  if (X<=1 || Y<=1)
    return img;
  QRgb bg(img.pixel(X-1, Y-1));
  int y1 = Y;
  while (y1>1) 
    if (allequalx(img, y1-1, 0, X, bg))
      --y1;
    else
      break;
  int x1 = X;
  while (x1>1)
    if (allequaly(img, x1-1, 0, y1, bg))
      --x1;
    else
      break;
  int x0 = 0;
  while (x0<x1-1)
    if (allequaly(img, x0, 0, y1, bg))
      x0++;
    else
      break;
  int y0 = 0;
  while (y0<y1-1)
    if (allequalx(img, y0, x0, x1, bg))
      y0++;
    else
      break;
  qDebug() << X << Y << x0 << y0 << x1 << y1;
  if (x0==0 && y0==0 && x1==X && y1==Y)
    return img;
  else
    return img.copy(x0, y0, x1-x0, y1-y0);
}



class Receiver {
public:
  Receiver(QString fn, Options const &opt):
    fn(fn), opt(opt) {
  }
  void operator()(QByteArray const &ar) {
    QFile *f = fn.isEmpty() ? new QTemporaryFile : new QFile(fn);
    if (f->open(QFile::WriteOnly)) {
      f->write(ar);
      f->close();
      convert(f->fileName());
      QApplication::exit(0);
    } else {
      qDebug() << "Cannot open" << fn;
      QApplication::exit(2);
    }
  }
  void convert(QString pdffn) {
    bool mustdo = false;
    foreach (QString ofn, opt.out) 
      if (ofn != pdffn)
        mustdo = true;
    if (!mustdo)
      return;
    
    QPdfDocument pdf;
    pdf.load(pdffn);
    if (pdf.pageCount()<1) {
      qDebug() << "No pages in pdf" << pdffn;
      QApplication::exit(2);
    }

    /* Ugly section follows to deal with unannounced API change in Qt PDF */
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
    QSizeF ptsize = pdf.pagePointSize(0);
#else
    QSizeF ptsize = pdf.pageSize(0);
#endif
    /* End of ugly section */
    QSize outsize(opt.imSize,
                  int(opt.imSize*ptsize.height()/ptsize.width()));
    QImage img = autotrim(pdf.render(0, outsize));
    foreach (QString ofn, opt.out) 
      if (ofn != pdffn) 
        img.save(ofn);    
  }
private:
  QString fn;
  Options const &opt;
};

void PrinterWE::toMultiPagePdf(QString fn) {
  Receiver *recv = new Receiver(fn, opt);
  QPageLayout layout(QPageSize(QPageSize::Letter),
                     QPageLayout::Portrait,
                     QMarginsF());
  src->page()->printToPdf(*recv, layout);
}

void PrinterWE::toSinglePagePdf(QString fn) {
  toMultiPagePdf(fn);
  // single-page pdf not supported in WebEngine.
}


void PrinterWE::featureReq(QUrl const &url, QWebEnginePage::Feature f) {
    src->page()->setFeaturePermission(url, f,
                                      QWebEnginePage::PermissionDeniedByUser);
}