poppler-0.30.0-rotated-words-selection.patch 7.75 KB
Newer Older
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
From 0ab1f29d4ce315b0fca260c0e0f3007024d00342 Mon Sep 17 00:00:00 2001
From: Marek Kasik <mkasik@redhat.com>
Date: Tue, 28 Jan 2014 15:13:24 +0100
Subject: [PATCH] TextOutputDev: Respect orientation when selecting words

Take rotation into account when visiting selection.
This doesn't fix all problems (there are still problems
on line and block levels).

https://bugs.freedesktop.org/show_bug.cgi?id=16619
---
 poppler/TextOutputDev.cc | 193 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 150 insertions(+), 43 deletions(-)

diff --git a/poppler/TextOutputDev.cc b/poppler/TextOutputDev.cc
index 7c2ca78..e93908c 100644
--- a/poppler/TextOutputDev.cc
+++ b/poppler/TextOutputDev.cc
@@ -178,6 +178,12 @@
 #define combMaxMidDelta 0.3
 #define combMaxBaseDelta 0.4
 
+// Returns whether x is between a and b or equal to a or b.
+// a and b don't need to be sorted.
+#define XBetweenAB(x,a,b) (!(((x) > (a) && (x) > (b)) || \
+                             ((x) < (a) && (x) < (b))) ? \
+                           gTrue : gFalse)
+
 static int reorderText(Unicode *text, int len, UnicodeMap *uMap, GBool primaryLR, GooString *s, Unicode* u) {
   char lre[8], rle[8], popdf[8], buf[8];
   int lreLen = 0, rleLen = 0, popdfLen = 0, n;
@@ -4411,11 +4417,37 @@ void TextSelectionSizer::visitLine (TextLine *line,
   PDFRectangle *rect;
   double x1, y1, x2, y2, margin;
 
-  margin = (line->yMax - line->yMin) / 8;
-  x1 = line->edge[edge_begin];
-  y1 = line->yMin - margin;
-  x2 = line->edge[edge_end];
-  y2 = line->yMax + margin;
+  switch (line->rot) {
+  default:
+  case 0:
+    margin = (line->yMax - line->yMin) / 8;
+    x1 = line->edge[edge_begin];
+    x2 = line->edge[edge_end];
+    y1 = line->yMin - margin;
+    y2 = line->yMax + margin;
+    break;
+  case 1:
+    margin = (line->xMax - line->xMin) / 8;
+    x1 = line->xMin - margin;
+    x2 = line->xMax + margin;
+    y1 = line->edge[edge_begin];
+    y2 = line->edge[edge_end];
+    break;
+  case 2:
+    margin = (line->yMax - line->yMin) / 8;
+    x1 = line->edge[edge_end];
+    x2 = line->edge[edge_begin];
+    y1 = line->yMin - margin;
+    y2 = line->yMax + margin;
+    break;
+  case 3:
+    margin = (line->xMax - line->xMin) / 8;
+    x1 = line->xMin - margin;
+    x2 = line->xMax + margin;
+    y1 = line->edge[edge_end];
+    y2 = line->edge[edge_begin];
+    break;
+  }
 
   rect = new PDFRectangle (floor (x1 * scale), 
 			   floor (y1 * scale),
@@ -4499,19 +4531,56 @@ void TextSelectionPainter::visitLine (TextLine *line,
 {
   double x1, y1, x2, y2, margin;
 
-  margin = (line->yMax - line->yMin) / 8;
-  x1 = floor (line->edge[edge_begin]);
-  y1 = floor (line->yMin - margin);
-  x2 = ceil (line->edge[edge_end]);
-  y2 = ceil (line->yMax + margin);
+  switch (line->rot) {
+  default:
+  case 0:
+    margin = (line->yMax - line->yMin) / 8;
+    x1 = line->edge[edge_begin];
+    x2 = line->edge[edge_end];
+    y1 = line->yMin - margin;
+    y2 = line->yMax + margin;
+    break;
+  case 1:
+    margin = (line->xMax - line->xMin) / 8;
+    x1 = line->xMin - margin;
+    x2 = line->xMax + margin;
+    y1 = line->edge[edge_begin];
+    y2 = line->edge[edge_end];
+    break;
+  case 2:
+    margin = (line->yMax - line->yMin) / 8;
+    x1 = line->edge[edge_end];
+    x2 = line->edge[edge_begin];
+    y1 = line->yMin - margin;
+    y2 = line->yMax + margin;
+    break;
+  case 3:
+    margin = (line->xMax - line->xMin) / 8;
+    x1 = line->xMin - margin;
+    x2 = line->xMax + margin;
+    y1 = line->edge[edge_end];
+    y2 = line->edge[edge_begin];
+    break;
+  }
+
+  ctm.transform(x1, y1, &x1, &y1);
+  ctm.transform(x2, y2, &x2, &y2);
 
-  ctm.transform(line->edge[edge_begin], line->yMin - margin, &x1, &y1);
-  ctm.transform(line->edge[edge_end], line->yMax + margin, &x2, &y2);
+  if (x1 < x2) {
+    x1 = floor (x1);
+    x2 = ceil (x2);
+  } else {
+    x1 = ceil (x1);
+    x2 = floor (x2);
+  }
 
-  x1 = floor (x1);
-  y1 = floor (y1);
-  x2 = ceil (x2);
-  y2 = ceil (y2);
+  if (y1 < y2) {
+    y1 = floor (y1);
+    y2 = ceil (y2);
+  } else {
+    y1 = ceil (y1);
+    y2 = floor (y2);
+  }
 
   ictm.transform(x1, y1, &x1, &y1);
   ictm.transform(x2, y2, &x2, &y2);
@@ -4589,17 +4658,27 @@ void TextWord::visitSelection(TextSelectionVisitor *visitor,
 			      SelectionStyle style)
 {
   int i, begin, end;
-  double mid;
+  double mid, s1, s2;
+
+  if (rot == 0 || rot == 2) {
+    s1 = selection->x1;
+    s2 = selection->x2;
+  } else {
+    s1 = selection->y1;
+    s2 = selection->y2;
+  }
 
   begin = len;
   end = 0;
   for (i = 0; i < len; i++) {
     mid = (edge[i] + edge[i + 1]) / 2;
-    if (selection->x1 < mid || selection->x2 < mid)
-      if (i < begin)
-	begin = i;
-    if (mid < selection->x1 || mid < selection->x2)
-      end = i + 1;
+    if (XBetweenAB (mid, s1, s2))
+      {
+        if (i < begin)
+          begin = i;
+
+        end = i + 1;
+      }
   }
 
   /* Skip empty selection. */
@@ -4615,30 +4694,41 @@ void TextLine::visitSelection(TextSelectionVisitor *visitor,
   TextWord *p, *begin, *end, *current;
   int i, edge_begin, edge_end;
   PDFRectangle child_selection;
+  double s1, s2, p_min, p_max;
+
+  if (rot == 0 || rot == 2) {
+    s1 = selection->x1;
+    s2 = selection->x2;
+  } else {
+    s1 = selection->y1;
+    s2 = selection->y2;
+  }
 
   begin = nullptr;
   end = nullptr;
   current = nullptr;
   for (p = words; p != nullptr; p = p->next) {
+    if (rot == 0 || rot == 2) {
+      p_min = p->xMin;
+      p_max = p->xMax;
+    } else {
+      p_min = p->yMin;
+      p_max = p->yMax;
+    }
+
     if (blk->page->primaryLR) {
-      if ((selection->x1 < p->xMax) ||
-	  (selection->x2 < p->xMax))
-        if (begin == nullptr) 
-	  begin = p;
+      if (((s1 < p_max) || (s2 < p_max)) && begin == nullptr)
+        begin = p;
 
-      if (((selection->x1 > p->xMin) ||
-	   (selection->x2 > p->xMin)) && (begin != nullptr)) {
+      if (((s1 > p_min) || (s2 > p_min)) && begin != nullptr) {
         end = p->next;
         current = p;
       }
     } else {
-      if ((selection->x1 > p->xMin) ||
-	  (selection->x2 > p->xMin))
-        if (begin == nullptr) 
-	  begin = p;
+      if (((s1 > p_min) || (s2 > p_min)) && begin == nullptr)
+        begin = p;
 
-      if (((selection->x1 < p->xMax) ||
-	   (selection->x2 < p->xMax)) && (begin != nullptr)) {
+      if (((s1 < p_max) || (s2 < p_max)) && begin != nullptr) {
         end = p->next;
         current = p;
       }
@@ -4650,23 +4740,42 @@ void TextLine::visitSelection(TextSelectionVisitor *visitor,
   
   child_selection = *selection;
   if (style == selectionStyleWord) {
-    child_selection.x1 = begin ? begin->xMin : xMin;
-    if (end && end->xMax != -1) {
-      child_selection.x2 = current->xMax;
+    if (rot == 0 || rot == 2) {
+      child_selection.x1 = begin ? begin->xMin : xMin;
+      if (end && end->xMax != -1) {
+        child_selection.x2 = current->xMax;
+      } else {
+        child_selection.x2 = xMax;
+      }
     } else {
-      child_selection.x2 = xMax;
+      child_selection.y1 = begin ? begin->yMin : yMin;
+      if (end && end->yMax != -1) {
+        child_selection.y2 = current->yMax;
+      } else {
+        child_selection.y2 = yMax;
+      }
     }
   }
 
+  if (rot == 0 || rot == 2) {
+    s1 = child_selection.x1;
+    s2 = child_selection.x2;
+  } else {
+    s1 = child_selection.y1;
+    s2 = child_selection.y2;
+  }
+
   edge_begin = len;
   edge_end = 0;
   for (i = 0; i < len; i++) {
     double mid = (edge[i] + edge[i + 1]) /  2;
-    if (child_selection.x1 < mid || child_selection.x2 < mid)
-      if (i < edge_begin)
-	edge_begin = i;
-    if (mid < child_selection.x2 || mid < child_selection.x1)
-      edge_end = i + 1;
+    if (XBetweenAB (mid, s1, s2))
+      {
+        if (i < edge_begin)
+          edge_begin = i;
+
+        edge_end = i + 1;
+      }
   }
 
   /* Skip empty selection. */
-- 
1.8.4.2