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
|
#include "mupdf/fitz.h"
#include <string.h>
static inline int fz_tolower(int c)
{
/* TODO: proper unicode case folding */
/* TODO: character equivalence (a matches ä, etc) */
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
return c;
}
static inline int iswhite(int c)
{
return c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == 0xA0 || c == 0x2028 || c == 0x2029;
}
fz_char_and_box *fz_stext_char_at(fz_context *ctx, fz_char_and_box *cab, fz_stext_page *page, int idx)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
int ofs = 0;
for (block = page->first_block; block; block = block->next)
{
if (block->type != FZ_STEXT_BLOCK_TEXT)
continue;
for (line = block->u.t.first_line; line; line = line->next)
{
for (ch = line->first_char; ch; ch = ch->next)
{
if (ofs == idx)
{
cab->c = ch->c;
fz_stext_char_bbox(ctx, &cab->bbox, line, ch);
return cab;
}
++ofs;
}
/* pseudo-newline */
if (idx == ofs)
{
cab->bbox = fz_empty_rect;
cab->c = ' ';
return cab;
}
++ofs;
}
}
cab->bbox = fz_empty_rect;
cab->c = 0;
return cab;
}
static inline int charat(fz_context *ctx, fz_stext_page *page, int idx)
{
fz_char_and_box cab;
return fz_stext_char_at(ctx, &cab, page, idx)->c;
}
static fz_rect *bboxat(fz_context *ctx, fz_stext_page *page, int idx, fz_rect *bbox)
{
fz_char_and_box cab;
/* FIXME: Nasty extra copy */
*bbox = fz_stext_char_at(ctx, &cab, page, idx)->bbox;
return bbox;
}
static int textlen_stext(fz_context *ctx, fz_stext_page *page)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
int len = 0;
for (block = page->first_block; block; block = block->next)
{
if (block->type != FZ_STEXT_BLOCK_TEXT)
continue;
for (line = block->u.t.first_line; line; line = line->next)
{
for (ch = line->first_char; ch; ch = ch->next)
++len;
++len; /* pseudo-newline */
}
}
return len;
}
static int match_stext(fz_context *ctx, fz_stext_page *page, const char *s, int n)
{
int orig = n;
int c;
while (*s)
{
s += fz_chartorune(&c, (char *)s);
if (iswhite(c) && iswhite(charat(ctx, page, n)))
{
const char *s_next;
/* Skip over whitespace in the document */
do
n++;
while (iswhite(charat(ctx, page, n)));
/* Skip over multiple whitespace in the search string */
while (s_next = s + fz_chartorune(&c, (char *)s), iswhite(c))
s = s_next;
}
else
{
if (fz_tolower(c) != fz_tolower(charat(ctx, page, n)))
return 0;
n++;
}
}
return n - orig;
}
int
fz_search_stext_page(fz_context *ctx, fz_stext_page *text, const char *needle, fz_rect *hit_bbox, int hit_max)
{
int pos, len, i, n, hit_count;
if (strlen(needle) == 0)
return 0;
hit_count = 0;
len = textlen_stext(ctx, text);
pos = 0;
while (pos < len)
{
n = match_stext(ctx, text, needle, pos);
if (n)
{
fz_rect linebox = fz_empty_rect;
for (i = 0; i < n; i++)
{
fz_rect charbox;
bboxat(ctx, text, pos + i, &charbox);
if (!fz_is_empty_rect(&charbox))
{
if (charbox.y0 != linebox.y0 || fz_abs(charbox.x0 - linebox.x1) > 5)
{
if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
hit_bbox[hit_count++] = linebox;
linebox = charbox;
}
else
{
fz_union_rect(&linebox, &charbox);
}
}
}
if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
hit_bbox[hit_count++] = linebox;
pos += n;
}
else
{
pos += 1;
}
}
return hit_count;
}
int
fz_highlight_selection(fz_context *ctx, fz_stext_page *page, fz_rect rect, fz_rect *hit_bbox, int hit_max)
{
fz_rect linebox, charbox;
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
int hit_count;
float x0 = rect.x0;
float x1 = rect.x1;
float y0 = rect.y0;
float y1 = rect.y1;
hit_count = 0;
for (block = page->first_block; block; block = block->next)
{
if (block->type != FZ_STEXT_BLOCK_TEXT)
continue;
for (line = block->u.t.first_line; line; line = line->next)
{
linebox = fz_empty_rect;
for (ch = line->first_char; ch; ch = ch->next)
{
fz_stext_char_bbox(ctx, &charbox, line, ch);
if (charbox.x1 >= x0 && charbox.x0 <= x1 && charbox.y1 >= y0 && charbox.y0 <= y1)
{
if (charbox.y0 != linebox.y0 || fz_abs(charbox.x0 - linebox.x1) > 5)
{
if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
hit_bbox[hit_count++] = linebox;
linebox = charbox;
}
else
{
fz_union_rect(&linebox, &charbox);
}
}
}
if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
hit_bbox[hit_count++] = linebox;
}
}
return hit_count;
}
char *
fz_copy_selection(fz_context *ctx, fz_stext_page *page, fz_rect rect)
{
fz_buffer *buffer;
fz_rect hitbox;
int c, seen = 0;
unsigned char *s;
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
float x0 = rect.x0;
float x1 = rect.x1;
float y0 = rect.y0;
float y1 = rect.y1;
buffer = fz_new_buffer(ctx, 1024);
for (block = page->first_block; block; block = block->next)
{
if (block->type != FZ_STEXT_BLOCK_TEXT)
continue;
for (line = block->u.t.first_line; line; line = line->next)
{
if (seen)
{
fz_append_byte(ctx, buffer, '\n');
}
seen = 0;
for (ch = line->first_char; ch; ch = ch->next)
{
fz_stext_char_bbox(ctx, &hitbox, line, ch);
c = ch->c;
if (c < 32)
c = FZ_REPLACEMENT_CHARACTER;
if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1)
{
fz_append_rune(ctx, buffer, c);
seen = 1;
}
}
seen = (seen && line == block->u.t.last_line);
}
}
fz_terminate_buffer(ctx, buffer);
fz_buffer_extract(ctx, buffer, &s); /* take over the data */
fz_drop_buffer(ctx, buffer);
return (char*)s;
}
|