summarylogtreecommitdiffstats
path: root/0001-Cairo-backend-added-to-Qt5-wrapper.patch
blob: b30e86d5d7619a190ce15ea51f5c497da6a871d2 (plain)
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
From caf6c676532d5767f7d52d589f2d1569f2554af9 Mon Sep 17 00:00:00 2001
From: Paul Gideon Dann <pdgiddie@gmail.com>
Date: Wed, 4 Apr 2018 16:36:56 +0100
Subject: [PATCH 1/4] Cairo backend added to Qt5 wrapper

---
 qt5/src/CMakeLists.txt      | 15 ++++++++
 qt5/src/poppler-document.cc |  3 ++
 qt5/src/poppler-page.cc     | 70 +++++++++++++++++++++++++++++++++++++
 qt5/src/poppler-qt5.h       |  3 +-
 qt5/tests/CMakeLists.txt    |  5 +++
 5 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/qt5/src/CMakeLists.txt b/qt5/src/CMakeLists.txt
index e3b60fcb..a32b6799 100644
--- a/qt5/src/CMakeLists.txt
+++ b/qt5/src/CMakeLists.txt
@@ -8,6 +8,11 @@ include_directories(
   ${CMAKE_CURRENT_BINARY_DIR}
 )
 
+if (HAVE_CAIRO)
+  include_directories(${CAIRO_INCLUDE_DIRS})
+  add_definitions(${CAIRO_CFLAGS})
+endif (HAVE_CAIRO)
+
 set(CMAKE_C_VISIBILITY_PRESET hidden)
 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
@@ -38,6 +43,13 @@ set(poppler_qt5_SRCS
   ArthurOutputDev.cc
   poppler-version.cpp
 )
+if (HAVE_CAIRO)
+  set(poppler_qt5_SRCS ${poppler_qt5_SRCS}
+    ${CMAKE_SOURCE_DIR}/poppler/CairoOutputDev.cc
+    ${CMAKE_SOURCE_DIR}/poppler/CairoRescaleBox.cc
+    ${CMAKE_SOURCE_DIR}/poppler/CairoFontEngine.cc
+  )
+endif(HAVE_CAIRO)
 add_library(poppler-qt5 ${poppler_qt5_SRCS})
 set_target_properties(poppler-qt5 PROPERTIES VERSION 1.20.0 SOVERSION 1)
 if(MINGW AND BUILD_SHARED_LIBS)
@@ -45,6 +57,9 @@ if(MINGW AND BUILD_SHARED_LIBS)
     set_target_properties(poppler-qt5 PROPERTIES SUFFIX "-${POPPLER_QT5_SOVERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}")
 endif()
 target_link_libraries(poppler-qt5 poppler ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Xml_LIBRARIES} ${FREETYPE_LIBRARIES})
+if (HAVE_CAIRO)
+  target_link_libraries(poppler-qt5 ${CAIRO_LIBRARIES})
+endif (HAVE_CAIRO)
 if(MSVC)
 target_link_libraries(poppler-qt5 poppler ${poppler_LIBS})
 endif()
diff --git a/qt5/src/poppler-document.cc b/qt5/src/poppler-document.cc
index 81db4eaf..635e44f9 100644
--- a/qt5/src/poppler-document.cc
+++ b/qt5/src/poppler-document.cc
@@ -695,6 +695,9 @@ namespace Poppler {
         ret << Document::SplashBackend;
 #endif
         ret << Document::ArthurBackend;
+#if defined(HAVE_CAIRO)
+        ret << Document::CairoBackend;
+#endif
         return ret;
     }
 
diff --git a/qt5/src/poppler-page.cc b/qt5/src/poppler-page.cc
index bf1f8889..592da057 100644
--- a/qt5/src/poppler-page.cc
+++ b/qt5/src/poppler-page.cc
@@ -48,6 +48,7 @@
 #include <QtGui/QPainter>
 
 #include <config.h>
+#include <math.h>
 #include <PDFDoc.h>
 #include <Catalog.h>
 #include <Form.h>
@@ -61,6 +62,9 @@
 #include <SplashOutputDev.h>
 #include <splash/SplashBitmap.h>
 #endif
+#if defined(HAVE_CAIRO)
+#include <CairoOutputDev.h>
+#endif
 
 #include "poppler-private.h"
 #include "poppler-page-transition-private.h"
@@ -622,6 +626,70 @@ QImage Page::renderToImage(double xres, double yres, int xPos, int yPos, int w,
       img = tmpimg;
       break;
     }
+    case Poppler::Document::CairoBackend:
+    {
+#if defined(HAVE_CAIRO)
+      CairoOutputDev *output_dev = new CairoOutputDev();
+      output_dev->startDoc(m_page->parentDoc->doc);
+      int buffer_width, buffer_height, rotate;
+      cairo_surface_t *surface;
+      cairo_t *cairo;
+
+      // If w or h are -1, that indicates the whole page, so we need to
+      // calculate how many pixels that corresponds to.  Otherwise, we can use w
+      // or h directly for our buffer size.
+      const QSize pageSize = this->pageSize();
+      if (w == -1) {
+        const double xscale = xres / 72.0;
+        const double width = pageSize.width();;
+        buffer_width = (int) ceil(width * xscale);
+      } else {
+        buffer_width = w;
+      }
+      if (h == -1) {
+        const double yscale = yres / 72.0;
+        const double height = pageSize.height();
+        buffer_height = (int) ceil(height * yscale);
+      } else {
+        buffer_height = h;
+      }
+
+      rotate = rotation + m_page->page->getRotate();
+
+      // FIXME: Okular never provides a rotation value, so I don't have any way
+      // of testing this right now.  The result is that subpixels are ordered
+      // incorrectly when the page is rotated.
+
+      //if (rotate == 90 || rotate == 270) {
+      //  const double temp = height;
+      //  height = width;
+      //  width = temp;
+      //}
+
+      img = QImage(buffer_width, buffer_height, QImage::Format_ARGB32);
+      img.fill(Qt::white);  // Never transparent
+
+      surface = cairo_image_surface_create_for_data(
+                  img.bits(),
+                  CAIRO_FORMAT_ARGB32,
+                  buffer_width, buffer_height,
+                  img.bytesPerLine());
+
+      cairo = cairo_create(surface);
+      output_dev->setCairo(cairo);
+
+      m_page->parentDoc->doc->displayPageSlice(
+        output_dev, m_page->index + 1, xres, yres, rotation, false, true,
+        false, xPos, yPos, w, h);
+
+      // Clean up
+      output_dev->setCairo(nullptr);
+      cairo_destroy(cairo);
+      cairo_surface_destroy(surface);
+      delete output_dev;
+#endif
+      break;
+    }
   }
 
   if (shouldAbortRenderCallback && shouldAbortRenderCallback(payload))
@@ -644,6 +712,8 @@ bool Page::renderToPainter(QPainter* painter, double xres, double yres, int x, i
         QImageDumpingArthurOutputDev arthur_output(painter, nullptr);
         return renderToArthur(&arthur_output, painter, m_page, xres, yres, x, y, w, h, rotate, flags);
     }
+    case Poppler::Document::CairoBackend:
+      return false;
   }
   return false;
 }
diff --git a/qt5/src/poppler-qt5.h b/qt5/src/poppler-qt5.h
index ebbc456a..37adf71a 100644
--- a/qt5/src/poppler-qt5.h
+++ b/qt5/src/poppler-qt5.h
@@ -1144,7 +1144,8 @@ delete it;
 	*/
 	enum RenderBackend {
 	    SplashBackend,   ///< Splash backend
-	    ArthurBackend   ///< Arthur (Qt) backend
+	    ArthurBackend,   ///< Arthur (Qt) backend
+	    CairoBackend     ///< Cairo backend
 	};
 
 	/**
diff --git a/qt5/tests/CMakeLists.txt b/qt5/tests/CMakeLists.txt
index 5abcbc45..99c12b9f 100644
--- a/qt5/tests/CMakeLists.txt
+++ b/qt5/tests/CMakeLists.txt
@@ -11,6 +11,11 @@ include_directories(
   ${Qt5Widgets_INCLUDE_DIRS}
 )
 
+if (HAVE_CAIRO)
+  include_directories(${CAIRO_INCLUDE_DIRS})
+  add_definitions(${CAIRO_CFLAGS})
+endif (HAVE_CAIRO)
+
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Gui_EXECUTABLE_COMPILE_FLAGS}")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Xml_EXECUTABLE_COMPILE_FLAGS}")
-- 
2.22.0