summarylogtreecommitdiffstats
path: root/0003-Apply-subpixel-rendering-in-Cairo-Backend.patch
blob: acb379526431b2f716d65378f7808496aec5649c (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
From c0c4a253390b391e7f7271969a48880faca070da Mon Sep 17 00:00:00 2001
From: Paul Gideon Dann <pdgiddie@gmail.com>
Date: Tue, 9 Aug 2016 15:02:53 +0100
Subject: [PATCH 3/4] Apply subpixel rendering in Cairo Backend

Source:
https://github.com/zhou13/poppler-subpixel/blob/master/poppler/poppler-subpixel.patch
---
 glib/demo/render.c         | 25 +++++++++++++++--
 glib/poppler-page.cc       | 10 +++++++
 glib/poppler-page.h        |  1 +
 poppler/CairoFontEngine.cc |  6 ++--
 poppler/CairoOutputDev.cc  | 24 ++++++++++++++++
 poppler/Gfx.cc             | 56 ++++++++++++++++++++++++++++++++++++++
 poppler/Gfx.h              |  3 ++
 poppler/Page.cc            | 14 ++++++++++
 poppler/Page.h             |  3 ++
 9 files changed, 136 insertions(+), 6 deletions(-)

diff --git a/glib/demo/render.c b/glib/demo/render.c
index 78d24bb5..d482ceb5 100644
--- a/glib/demo/render.c
+++ b/glib/demo/render.c
@@ -82,12 +82,14 @@ pgd_render_start (GtkButton     *button,
 		  PgdRenderDemo *demo)
 {
 	PopplerPage *page;
+	gboolean     subpixel_rendering;
 	gdouble      page_width, page_height;
 	gdouble      width, height;
 	gint         x, y;
 	gchar       *str;
 	GTimer      *timer;
         cairo_t     *cr;
+        cairo_font_options_t *fo;
 
 	page = poppler_document_get_page (demo->doc, demo->page);
 	if (!page)
@@ -116,6 +118,21 @@ pgd_render_start (GtkButton     *button,
                                                     width, height);
         cr = cairo_create (demo->surface);
 
+        fo = cairo_font_options_create ();
+        cairo_get_font_options (cr, fo);
+
+        subpixel_rendering = poppler_page_support_subpixel_rendering (page);
+        printf("subpixel_rendering %d\n", subpixel_rendering);
+        if (subpixel_rendering) {
+                cairo_set_source_rgb (cr, 1., 1., 1.);
+                cairo_paint (cr);
+                cairo_font_options_set_antialias (fo, CAIRO_ANTIALIAS_SUBPIXEL);
+                cairo_font_options_set_subpixel_order (fo, CAIRO_SUBPIXEL_ORDER_RGB);
+        }
+
+        cairo_set_font_options (cr, fo);
+        cairo_font_options_destroy (fo);
+
         cairo_save (cr);
         switch (demo->rotate) {
         case 90:
@@ -143,9 +160,11 @@ pgd_render_start (GtkButton     *button,
                 poppler_page_render (page, cr);
         cairo_restore (cr);
 
-        cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
-        cairo_set_source_rgb (cr, 1., 1., 1.);
-        cairo_paint (cr);
+        if (!subpixel_rendering) {
+                cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
+                cairo_set_source_rgb (cr, 1., 1., 1.);
+                cairo_paint (cr);
+        }
 
         g_timer_stop (timer);
 
diff --git a/glib/poppler-page.cc b/glib/poppler-page.cc
index 6b511692..8f08307d 100644
--- a/glib/poppler-page.cc
+++ b/glib/poppler-page.cc
@@ -2419,3 +2419,13 @@ poppler_page_get_text_attributes_for_area (PopplerPage      *page,
 
   return g_list_reverse(attributes);
 }
+
+gboolean
+poppler_page_support_subpixel_rendering (PopplerPage        *page)
+{
+  CairoOutputDev *output_dev;
+  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
+
+  output_dev = page->document->output_dev;
+  return page->page->supportSubpixelRendering(output_dev);
+}
diff --git a/glib/poppler-page.h b/glib/poppler-page.h
index fdbfd38e..0f02a3af 100644
--- a/glib/poppler-page.h
+++ b/glib/poppler-page.h
@@ -109,6 +109,7 @@ GList                 *poppler_page_get_text_attributes  (PopplerPage        *pa
 void                   poppler_page_free_text_attributes (GList              *list);
 GList *        poppler_page_get_text_attributes_for_area (PopplerPage        *page,
                                                           PopplerRectangle   *area);
+gboolean         poppler_page_support_subpixel_rendering (PopplerPage        *page);
 
 /* A rectangle on a page, with coordinates in PDF points. */
 #define POPPLER_TYPE_RECTANGLE             (poppler_rectangle_get_type ())
diff --git a/poppler/CairoFontEngine.cc b/poppler/CairoFontEngine.cc
index 91deac46..aad28564 100644
--- a/poppler/CairoFontEngine.cc
+++ b/poppler/CairoFontEngine.cc
@@ -132,7 +132,7 @@ CairoFont::getSubstitutionCorrection(GfxFont *gfxFont)
 	cairo_matrix_t m;
 	cairo_matrix_init_identity(&m);
 	cairo_font_options_t *options = cairo_font_options_create();
-	cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
+	cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_SLIGHT);
 	cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
 	cairo_scaled_font_t *scaled_font = cairo_scaled_font_create(cairo_font_face, &m, &m, options);
 
@@ -190,7 +190,7 @@ _ft_new_face_uncached (FT_Library lib,
   }
 
   font_face = cairo_ft_font_face_create_for_ft_face (face,
-							  FT_LOAD_NO_HINTING |
+							  FT_LOAD_TARGET_LIGHT |
 							  FT_LOAD_NO_BITMAP);
   if (cairo_font_face_set_user_data (font_face,
 				     &_ft_cairo_key,
@@ -359,7 +359,7 @@ _ft_new_face (FT_Library lib,
   _ft_open_faces = l;
 
   l->font_face = cairo_ft_font_face_create_for_ft_face (tmpl.face,
-							  FT_LOAD_NO_HINTING |
+							  FT_LOAD_TARGET_LIGHT |
 							  FT_LOAD_NO_BITMAP);
   if (cairo_font_face_set_user_data (l->font_face,
 				     &_ft_cairo_key,
diff --git a/poppler/CairoOutputDev.cc b/poppler/CairoOutputDev.cc
index 8e8102e9..a93a7075 100644
--- a/poppler/CairoOutputDev.cc
+++ b/poppler/CairoOutputDev.cc
@@ -214,6 +214,13 @@ void CairoOutputDev::setCairo(cairo_t *cairo)
   }
   if (cairo != nullptr) {
     this->cairo = cairo_reference (cairo);
+    {
+      cairo_font_options_t *options = cairo_font_options_create ();
+      cairo_get_font_options (cairo, options);
+      cairo_font_options_set_antialias (options, CAIRO_ANTIALIAS_SUBPIXEL);
+      cairo_set_font_options (cairo, options);
+      cairo_font_options_destroy (options);
+    }
 	/* save the initial matrix so that we can use it for type3 fonts. */
 	//XXX: is this sufficient? could we miss changes to the matrix somehow?
 	cairo_get_matrix(cairo, &orig_matrix);
@@ -1441,6 +1448,7 @@ void CairoOutputDev::drawChar(GfxState *state, double x, double y,
 void CairoOutputDev::endString(GfxState *state)
 {
   int render;
+  GfxFontType fontType;
 
   if (!currentFont)
     return;
@@ -1458,6 +1466,18 @@ void CairoOutputDev::endString(GfxState *state)
     goto finish;
   }
 
+  fontType = state->getFont()->getType();
+  // Do not enable subpixel rendering for type3 font
+  // For some reason it does not work
+  if (fontType == fontType3) {
+      cairo_save(cairo);
+      cairo_font_options_t *fo;
+      fo = cairo_font_options_create ();
+      cairo_get_font_options (cairo, fo);
+      cairo_font_options_set_antialias (fo, CAIRO_ANTIALIAS_DEFAULT);
+      cairo_set_font_options (cairo, fo);
+  }
+
   if (!(render & 1)) {
     LOG (printf ("fill string\n"));
     cairo_set_source (cairo, fill_pattern);
@@ -1508,6 +1528,10 @@ void CairoOutputDev::endString(GfxState *state)
   }
 
 finish:
+  // pair with the previous cairo_save to disable subpixel rendering for type3 fonts
+  if (fontType == fontType3) {
+      cairo_restore(cairo);
+  }
   gfree (glyphs);
   glyphs = nullptr;
   if (use_show_text_glyphs) {
diff --git a/poppler/Gfx.cc b/poppler/Gfx.cc
index 8f2c62d2..473cacb5 100644
--- a/poppler/Gfx.cc
+++ b/poppler/Gfx.cc
@@ -4611,6 +4611,62 @@ void Gfx::doImage(Object *ref, Stream *str, GBool inlineImg) {
   error(errSyntaxError, getPos(), "Bad image parameters");
 }
 
+GBool Gfx::checkNormalBlendModeOnly(Object *str) {
+  printf("check blender mode start\n");
+  char *cmd;
+  Object obj;
+  Object args[maxArgs];
+  int numArgs, i;
+  GBool onlyNormalBlendMode;
+  Parser myParser(xref, new Lexer(xref, str), gFalse);
+
+  numArgs = 0;
+  onlyNormalBlendMode = gTrue;
+
+  myParser.getObj(&obj);
+  while (!obj.isEOF()) {
+    if (obj.isCmd()) {
+      cmd = obj.getCmd();
+
+      if (strcmp(cmd, "gs") == 0) {
+        Object obj1, obj2;
+        GfxBlendMode mode;
+        if (res->lookupGState(args[0].getName(), &obj1)) {
+          if (!obj1.dictLookup("BM", &obj2)->isNull()) {
+            if (state->parseBlendMode(&obj2, &mode)) {
+              printf("check blend mode: %d\n", mode);
+              onlyNormalBlendMode &= (mode == gfxBlendNormal);
+            }
+          }
+          obj2.free();
+        }
+        obj1.free();
+      }
+      obj.free();
+
+      for (i = 0; i < numArgs; ++i)
+	args[i].free();
+      numArgs = 0;
+
+    } else if (numArgs < maxArgs) {
+      args[numArgs++] = obj;
+    } else {
+      obj.free();
+    }
+
+    myParser.getObj(&obj);
+  }
+  obj.free();
+
+  if (numArgs > 0) {
+    for (i = 0; i < numArgs; ++i)
+      args[i].free();
+  }
+
+  return onlyNormalBlendMode;
+}
+
+
 GBool Gfx::checkTransparencyGroup(Dict *resDict) {
   // check the effect of compositing objects as a group:
   // look for ExtGState entries with ca != 1 or CA != 1 or BM != normal
diff --git a/poppler/Gfx.h b/poppler/Gfx.h
index 3ac4c929..5cefa0d1 100644
--- a/poppler/Gfx.h
+++ b/poppler/Gfx.h
@@ -195,6 +195,9 @@ public:
   // Get the current graphics state object.
   GfxState *getState() { return state; }
 
+  // Check whether a stream only contains normal blend mode (to enable subpixel rendering)
+  GBool checkNormalBlendModeOnly(Object *str);
+
   GBool checkTransparencyGroup(Dict *resDict);
 
   void drawForm(Object *str, Dict *resDict, double *matrix, double *bbox,
diff --git a/poppler/Page.cc b/poppler/Page.cc
index d4799dd1..4a0e559e 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -348,6 +348,20 @@ Dict *Page::getResourceDictCopy(XRef *xrefA) {
   return dict ? dict->copy(xrefA) : nullptr;
 }
 
+GBool Page::supportSubpixelRendering(OutputDev *out) {
+  GBool supported = gFalse;
+  Object obj;
+  PDFRectangle box;
+
+  contents.fetch(xref, &obj);
+  if (!obj.isNull()) {
+    Gfx gfx(doc, out, attrs->getResourceDict(), &box, NULL);
+    supported = gfx.checkNormalBlendModeOnly(&obj);
+  }
+  obj.free();
+  return supported;
+}
+
 void Page::replaceXRef(XRef *xrefA) {
   Object obj1;
   Dict *pageDict = pageObj.getDict()->copy(xrefA);
diff --git a/poppler/Page.h b/poppler/Page.h
index 7151ee5b..41c1fd75 100644
--- a/poppler/Page.h
+++ b/poppler/Page.h
@@ -185,6 +185,9 @@ public:
   Object *getResourceDictObject();
   Dict *getResourceDictCopy(XRef *xrefA);
 
+  // Whether the content in this page supports subpixel rendering (lcdfilter)
+  GBool supportSubpixelRendering(OutputDev *out);
+
   // Get annotations array.
   Object getAnnotsObject(XRef *xrefA = nullptr) { return annotsObj.fetch(xrefA ? xrefA : xref); }
   // Add a new annotation to the page
-- 
2.18.0