summaryrefslogtreecommitdiff
path: root/ios/document.c
blob: 9e46644c9cd7998cbdb31d375045fb4b0170f3eb (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
#include "fitz/fitz.h"
#include "pdf/mupdf.h"
#include "xps/muxps.h"
#include "document.h"

#include <ctype.h> // for tolower()

struct document *
open_document(char *filename)
{
	fz_error error;

	if (strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
		struct document *doc = fz_malloc(sizeof *doc);
		memset(doc, 0, sizeof *doc);
		doc->number = -1;
		error = pdf_open_xref(&doc->pdf, filename, "");
		if (error) {
			fz_free(doc);
			fz_rethrow(error, "cannot open pdf document");
			return NULL;
		}
		error = pdf_load_page_tree(doc->pdf);
		if (error) {
			pdf_free_xref(doc->pdf);
			fz_free(doc);
			fz_rethrow(error, "cannot open pdf document");
			return NULL;
		}
		return doc;
	} else if (strstr(filename, ".xps") || strstr(filename, ".XPS")) {
		struct document *doc = fz_malloc(sizeof *doc);
		memset(doc, 0, sizeof *doc);
		doc->number = -1;
		error = xps_open_file(&doc->xps, filename);
		if (error) {
			fz_free(doc);
			fz_rethrow(error, "cannot open xps document");
			return NULL;
		}
		return doc;
	} else {
		fz_throw("unknown document format");
		return NULL;
	}
}

fz_outline *
load_outline(struct document *doc)
{
	if (doc->pdf)
		return pdf_load_outline(doc->pdf);
	else if (doc->xps)
		return xps_load_outline(doc->xps);
	else
		return NULL;
}

int
count_pages(struct document *doc)
{
	if (doc->pdf)
		return pdf_count_pages(doc->pdf);
	else if (doc->xps)
		return xps_count_pages(doc->xps);
	else
		return 1;
}

static void
load_page(struct document *doc, int number)
{
	fz_error error;
	if (doc->number == number)
		return;
	doc->number = number;
	if (doc->pdf) {
		if (doc->pdf_page) {
			pdf_age_store(doc->pdf->store, 1);
			pdf_free_page(doc->pdf_page);
		}
		doc->pdf_page = NULL;
printf("load pdf page %d\n", number);
		error = pdf_load_page(&doc->pdf_page, doc->pdf, number);
		if (error)
			fz_catch(error, "cannot load page %d", number);
	}
	if (doc->xps) {
		if (doc->xps_page)
			xps_free_page(doc->xps, doc->xps_page);
		doc->xps_page = NULL;
printf("load xps page %d\n", number);
		error = xps_load_page(&doc->xps_page, doc->xps, number);
		if (error)
			fz_catch(error, "cannot load page %d", number);
	}
}

void
measure_page(struct document *doc, int number, float *w, float *h)
{
	load_page(doc, number);
	if (doc->pdf_page) {
		pdf_page *page = doc->pdf_page;
		fz_rect mediabox = fz_transform_rect(fz_rotate(page->rotate), page->mediabox);
		*w = mediabox.x1 - mediabox.x0;
		*h = mediabox.y1 - mediabox.y0;
	}
	else if (doc->xps_page) {
		xps_page *page = doc->xps_page;
		*w = page->width * 72.0f / 96.0f;
		*h = page->height * 72.0f / 96.0f;
	}
	else {
		*w = *h = 72;
	}
	fz_flush_warnings();
}

void
draw_page(struct document *doc, int number, fz_device *dev, fz_matrix ctm)
{
	load_page(doc, number);
	if (doc->pdf_page) {
		pdf_page *page = doc->pdf_page;
		fz_matrix page_ctm = fz_concat(fz_rotate(-page->rotate), fz_scale(1, -1));
		fz_rect mediabox = fz_transform_rect(page_ctm, page->mediabox);
		page_ctm = fz_concat(page_ctm, fz_translate(-mediabox.x0, -mediabox.y0));
		ctm = fz_concat(page_ctm, ctm);
		pdf_run_page(doc->pdf, page, dev, ctm);
	} else if (doc->xps_page) {
		xps_page *page = doc->xps_page;
		fz_matrix page_ctm = fz_scale(72.0f / 96.0f, 72.0f / 96.0f);
		ctm = fz_concat(page_ctm, ctm);
		doc->xps->dev = dev;
		xps_parse_fixed_page(doc->xps, ctm, page);
		doc->xps->dev = NULL;
	}
	fz_flush_warnings();
}

static int
charat(fz_text_span *span, int idx)
{
	int ofs = 0;
	while (span) {
		if (idx < ofs + span->len)
			return span->text[idx - ofs].c;
		if (span->eol) {
			if (idx == ofs + span->len)
				return ' ';
			ofs ++;
		}
		ofs += span->len;
		span = span->next;
	}
	return 0;
}

static fz_bbox
bboxat(fz_text_span *span, int idx)
{
	int ofs = 0;
	while (span) {
		if (idx < ofs + span->len)
			return span->text[idx - ofs].bbox;
		if (span->eol) {
			if (idx == ofs + span->len)
				return fz_empty_bbox;
			ofs ++;
		}
		ofs += span->len;
		span = span->next;
	}
	return fz_empty_bbox;
}

static int
textlen(fz_text_span *span)
{
	int len = 0;
	while (span) {
		len += span->len;
		if (span->eol)
			len ++;
		span = span->next;
	}
	return len;
}

static int
match(fz_text_span *span, char *s, int n)
{
	int start = n, c;
	while (*s) {
		s += chartorune(&c, s);
		if (c == ' ' && charat(span, n) == ' ') {
			while (charat(span, n) == ' ')
				n++;
		} else {
			if (tolower(c) != tolower(charat(span, n)))
				return 0;
			n++;
		}
	}
	return n - start;
}

int
search_page(struct document *doc, int number, char *needle)
{
	int pos, len, i, n;

	if (strlen(needle) == 0)
		return 0;

	fz_text_span *text = fz_new_text_span();
	fz_device *dev = fz_new_text_device(text);
	draw_page(doc, number, dev, fz_identity);
	fz_free_device(dev);

	doc->hit_count = 0;

	len = textlen(text);
	for (pos = 0; pos < len; pos++) {
		n = match(text, needle, pos);
		if (n) {
			printf("found a match at page %d, pos %d!\n", number, pos);
			for (i = 0; i < n; i++) {
				fz_bbox r = bboxat(text, pos + i);
				if (!fz_is_empty_bbox(r) && doc->hit_count < nelem(doc->hit_bbox))
					doc->hit_bbox[doc->hit_count++] = r;
			}
		}
	}

	fz_free_text_span(text);
	return doc->hit_count;
}

fz_bbox
search_result_bbox(struct document *doc, int i)
{
	return doc->hit_bbox[i];
}

void
close_document(struct document *doc)
{
	if (doc->pdf) {
		if (doc->pdf_page)
			pdf_free_page(doc->pdf_page);
		if (doc->pdf->store)
			pdf_free_store(doc->pdf->store);
		doc->pdf->store = NULL;
		pdf_free_xref(doc->pdf);
	}
	if (doc->xps) {
		if (doc->xps_page)
			xps_free_page(doc->xps, doc->xps_page);
		xps_free_context(doc->xps);
	}
	fz_flush_warnings();
}