summaryrefslogtreecommitdiff
path: root/source/fitz/document.c
blob: 3926255b6a670b81b4a01ba642418aa8de0b2e6d (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
#include "mupdf/fitz.h"

/* Yuck! Promiscuous we are. */
extern struct pdf_document *pdf_open_document(fz_context *ctx, const char *filename);
extern struct xps_document *xps_open_document(fz_context *ctx, const char *filename);
extern struct cbz_document *cbz_open_document(fz_context *ctx, const char *filename);
extern struct image_document *image_open_document(fz_context *ctx, const char *filename);

extern struct pdf_document *pdf_open_document_with_stream(fz_context *ctx, fz_stream *file);
extern struct xps_document *xps_open_document_with_stream(fz_context *ctx, fz_stream *file);
extern struct cbz_document *cbz_open_document_with_stream(fz_context *ctx, fz_stream *file);
extern struct image_document *image_open_document_with_stream(fz_context *ctx, fz_stream *file);

extern int pdf_js_supported(void);

static inline int fz_tolower(int c)
{
	if (c >= 'A' && c <= 'Z')
		return c + 32;
	return c;
}

static inline int fz_strcasecmp(const char *a, const char *b)
{
	while (fz_tolower(*a) == fz_tolower(*b))
	{
		if (*a++ == 0)
			return 0;
		b++;
	}
	return fz_tolower(*a) - fz_tolower(*b);
}

fz_document *
fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream)
{
	char *ext = strrchr(magic, '.');

	if (ext)
	{
		if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels") || !fz_strcasecmp(ext, ".oxps"))
			return (fz_document*) xps_open_document_with_stream(ctx, stream);
		if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
			return (fz_document*) cbz_open_document_with_stream(ctx, stream);
		if (!fz_strcasecmp(ext, ".pdf"))
			return (fz_document*) pdf_open_document_with_stream(ctx, stream);
		if (!fz_strcasecmp(ext, ".png") || !fz_strcasecmp(ext, ".jpg") ||
			!fz_strcasecmp(ext, ".jpeg") || !fz_strcasecmp(ext, ".jfif") ||
			!fz_strcasecmp(ext, ".jfif-tbnl") || !fz_strcasecmp(ext, ".jpe") ||
			!fz_strcasecmp(ext, ".tif") || !fz_strcasecmp(ext, ".tiff"))
			return (fz_document*) image_open_document_with_stream(ctx, stream);
	}

	if (!strcmp(magic, "cbz") || !strcmp(magic, "application/x-cbz"))
		return (fz_document*) cbz_open_document_with_stream(ctx, stream);
	if (!strcmp(magic, "xps") || !strcmp(magic, "oxps") ||
		!strcmp(magic, "application/vnd.ms-xpsdocument") ||
		!strcmp(magic, "application/oxps"))
		return (fz_document*) xps_open_document_with_stream(ctx, stream);
	if (!strcmp(magic, "pdf") || !strcmp(magic, "application/pdf"))
		return (fz_document*) pdf_open_document_with_stream(ctx, stream);
	if (!strcmp(magic, "png") || !strcmp(magic, "image/png") ||
		!strcmp(magic, "jpg") || !strcmp(magic, "image/jpeg") ||
		!strcmp(magic, "jpeg") || !strcmp(magic, "image/pjpeg") ||
		!strcmp(magic, "jpe") || !strcmp(magic, "jfif") ||
		!strcmp(magic, "tif") || !strcmp(magic, "image/tiff") ||
		!strcmp(magic, "tiff") || !strcmp(magic, "image/x-tiff"))
		return (fz_document*) image_open_document_with_stream(ctx, stream);

	/* last guess: pdf */
	return (fz_document*) pdf_open_document_with_stream(ctx, stream);
}

fz_document *
fz_open_document(fz_context *ctx, const char *filename)
{
	char *ext = strrchr(filename, '.');

	if (ext)
	{
		if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels") || !fz_strcasecmp(ext, ".oxps"))
			return (fz_document*) xps_open_document(ctx, filename);
		if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
			return (fz_document*) cbz_open_document(ctx, filename);
		if (!fz_strcasecmp(ext, ".pdf"))
			return (fz_document*) pdf_open_document(ctx, filename);
		if (!fz_strcasecmp(ext, ".png") || !fz_strcasecmp(ext, ".jpg") ||
			!fz_strcasecmp(ext, ".jpeg") || !fz_strcasecmp(ext, ".jpe") ||
			!fz_strcasecmp(ext, ".jfif") || !fz_strcasecmp(ext, ".jfif-tbnl") ||
			!fz_strcasecmp(ext, ".tif") || !fz_strcasecmp(ext, ".tiff"))
			return (fz_document*) image_open_document(ctx, filename);
	}

	/* last guess: pdf */
	return (fz_document*) pdf_open_document(ctx, filename);
}

void
fz_close_document(fz_document *doc)
{
	if (doc && doc->close)
		doc->close(doc);
}

int
fz_needs_password(fz_document *doc)
{
	if (doc && doc->needs_password)
		return doc->needs_password(doc);
	return 0;
}

int
fz_authenticate_password(fz_document *doc, const char *password)
{
	if (doc && doc->authenticate_password)
		return doc->authenticate_password(doc, password);
	return 1;
}

fz_outline *
fz_load_outline(fz_document *doc)
{
	if (doc && doc->load_outline)
		return doc->load_outline(doc);
	return NULL;
}

int
fz_count_pages(fz_document *doc)
{
	if (doc && doc->count_pages)
		return doc->count_pages(doc);
	return 0;
}

fz_page *
fz_load_page(fz_document *doc, int number)
{
	if (doc && doc->load_page)
		return doc->load_page(doc, number);
	return NULL;
}

fz_link *
fz_load_links(fz_document *doc, fz_page *page)
{
	if (doc && doc->load_links && page)
		return doc->load_links(doc, page);
	return NULL;
}

fz_rect *
fz_bound_page(fz_document *doc, fz_page *page, fz_rect *r)
{
	if (doc && doc->bound_page && page && r)
		return doc->bound_page(doc, page, r);
	if (r)
		*r = fz_empty_rect;
	return r;
}

fz_annot *
fz_first_annot(fz_document *doc, fz_page *page)
{
	if (doc && doc->first_annot && page)
		return doc->first_annot(doc, page);
	return NULL;
}

fz_annot *
fz_next_annot(fz_document *doc, fz_annot *annot)
{
	if (doc && doc->next_annot && annot)
		return doc->next_annot(doc, annot);
	return NULL;
}

fz_rect *
fz_bound_annot(fz_document *doc, fz_annot *annot, fz_rect *rect)
{
	if (doc && doc->bound_annot && annot && rect)
		return doc->bound_annot(doc, annot, rect);
	if (rect)
		*rect = fz_empty_rect;
	return rect;
}

void
fz_run_page_contents(fz_document *doc, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
{
	if (doc && doc->run_page_contents && page)
		doc->run_page_contents(doc, page, dev, transform, cookie);
}

void
fz_run_annot(fz_document *doc, fz_page *page, fz_annot *annot, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
{
	if (doc && doc->run_annot && page && annot)
		doc->run_annot(doc, page, annot, dev, transform, cookie);
}

void
fz_run_page(fz_document *doc, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
{
	fz_annot *annot;
	fz_rect mediabox;

	fz_bound_page(doc, page, &mediabox);
	fz_begin_page(dev, &mediabox, transform);

	fz_run_page_contents(doc, page, dev, transform, cookie);

	if (cookie && cookie->progress_max != -1)
	{
		int count = 1;
		for (annot = fz_first_annot(doc, page); annot; annot = fz_next_annot(doc, annot))
			count++;
		cookie->progress_max += count;
	}

	for (annot = fz_first_annot(doc, page); annot; annot = fz_next_annot(doc, annot))
	{
		/* Check the cookie for aborting */
		if (cookie)
		{
			if (cookie->abort)
				break;
			cookie->progress++;
		}

		fz_run_annot(doc, page, annot, dev, transform, cookie);
	}

	fz_end_page(dev);
}

void
fz_free_page(fz_document *doc, fz_page *page)
{
	if (doc && doc->free_page && page)
		doc->free_page(doc, page);
}

int
fz_meta(fz_document *doc, int key, void *ptr, int size)
{
	if (doc && doc->meta)
		return doc->meta(doc, key, ptr, size);
	return FZ_META_UNKNOWN_KEY;
}

fz_transition *
fz_page_presentation(fz_document *doc, fz_page *page, float *duration)
{
	float dummy;
	if (duration)
		*duration = 0;
	else
		duration = &dummy;
	if (doc && doc->page_presentation && page)
		return doc->page_presentation(doc, page, duration);
	return NULL;
}

int fz_javascript_supported(void)
{
	return pdf_js_supported();
}

void
fz_write_document(fz_document *doc, char *filename, fz_write_options *opts)
{
	if (doc && doc->write)
		doc->write(doc, filename, opts);
}