1#include "mupdf/fitz.h"
2
3#include <string.h>
4#include <stdlib.h>
5#include <stdio.h>
6
7static void usage(void)
8{
9 fprintf(stderr,
10 "Usage: mutool trace [options] file [pages]\n"
11 "\t-p -\tpassword\n"
12 "\n"
13 "\t-W -\tpage width for EPUB layout\n"
14 "\t-H -\tpage height for EPUB layout\n"
15 "\t-S -\tfont size for EPUB layout\n"
16 "\t-U -\tfile name of user stylesheet for EPUB layout\n"
17 "\t-X\tdisable document styles for EPUB layout\n"
18 "\n"
19 "\t-d\tuse display list\n"
20 "\n"
21 "\tpages\tcomma separated list of page numbers and ranges\n"
22 );
23 exit(1);
24}
25
26static float layout_w = FZ_DEFAULT_LAYOUT_W;
27static float layout_h = FZ_DEFAULT_LAYOUT_H;
28static float layout_em = FZ_DEFAULT_LAYOUT_EM;
29static char *layout_css = NULL;
30static int layout_use_doc_css = 1;
31
32static int use_display_list = 0;
33
34static void runpage(fz_context *ctx, fz_document *doc, int number)
35{
36 fz_page *page = NULL;
37 fz_display_list *list = NULL;
38 fz_device *dev = NULL;
39 fz_rect mediabox;
40
41 fz_var(page);
42 fz_var(list);
43 fz_var(dev);
44 fz_try(ctx)
45 {
46 page = fz_load_page(ctx, doc, number - 1);
47 mediabox = fz_bound_page(ctx, page);
48 printf("<page number=\"%d\" mediabox=\"%g %g %g %g\">\n",
49 number, mediabox.x0, mediabox.y0, mediabox.x1, mediabox.y1);
50 dev = fz_new_trace_device(ctx, fz_stdout(ctx));
51 if (use_display_list)
52 {
53 list = fz_new_display_list_from_page(ctx, page);
54 fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
55 }
56 else
57 {
58 fz_run_page(ctx, page, dev, fz_identity, NULL);
59 }
60 printf("</page>\n");
61 }
62 fz_always(ctx)
63 {
64 fz_drop_display_list(ctx, list);
65 fz_drop_page(ctx, page);
66 fz_drop_device(ctx, dev);
67 }
68 fz_catch(ctx)
69 fz_rethrow(ctx);
70}
71
72static void runrange(fz_context *ctx, fz_document *doc, int count, const char *range)
73{
74 int start, end, i;
75
76 while ((range = fz_parse_page_range(ctx, range, &start, &end, count)))
77 {
78 if (start < end)
79 for (i = start; i <= end; ++i)
80 runpage(ctx, doc, i);
81 else
82 for (i = start; i >= end; --i)
83 runpage(ctx, doc, i);
84 }
85}
86
87int mutrace_main(int argc, char **argv)
88{
89 fz_context *ctx;
90 fz_document *doc = NULL;
91 char *password = "";
92 int i, c, count;
93
94 while ((c = fz_getopt(argc, argv, "p:W:H:S:U:Xd")) != -1)
95 {
96 switch (c)
97 {
98 default: usage(); break;
99 case 'p': password = fz_optarg; break;
100
101 case 'W': layout_w = fz_atof(fz_optarg); break;
102 case 'H': layout_h = fz_atof(fz_optarg); break;
103 case 'S': layout_em = fz_atof(fz_optarg); break;
104 case 'U': layout_css = fz_optarg; break;
105 case 'X': layout_use_doc_css = 0; break;
106
107 case 'd': use_display_list = 1; break;
108 }
109 }
110
111 if (fz_optind == argc)
112 usage();
113
114 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
115 if (!ctx)
116 {
117 fprintf(stderr, "cannot create mupdf context\n");
118 return EXIT_FAILURE;
119 }
120
121 fz_try(ctx)
122 {
123 fz_register_document_handlers(ctx);
124 if (layout_css)
125 {
126 fz_buffer *buf = fz_read_file(ctx, layout_css);
127 fz_set_user_css(ctx, fz_string_from_buffer(ctx, buf));
128 fz_drop_buffer(ctx, buf);
129 }
130 fz_set_use_document_css(ctx, layout_use_doc_css);
131 }
132 fz_catch(ctx)
133 {
134 fprintf(stderr, "cannot initialize mupdf: %s\n", fz_caught_message(ctx));
135 fz_drop_context(ctx);
136 return EXIT_FAILURE;
137 }
138
139 fz_var(doc);
140 fz_try(ctx)
141 {
142 for (i = fz_optind; i < argc; ++i)
143 {
144 doc = fz_open_document(ctx, argv[i]);
145 if (fz_needs_password(ctx, doc))
146 if (!fz_authenticate_password(ctx, doc, password))
147 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", argv[i]);
148 fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
149 printf("<document filename=\"%s\">\n", argv[i]);
150 count = fz_count_pages(ctx, doc);
151 if (i+1 < argc && fz_is_page_range(ctx, argv[i+1]))
152 runrange(ctx, doc, count, argv[++i]);
153 else
154 runrange(ctx, doc, count, "1-N");
155 printf("</document>\n");
156 fz_drop_document(ctx, doc);
157 doc = NULL;
158 }
159 }
160 fz_catch(ctx)
161 {
162 fprintf(stderr, "cannot run document: %s\n", fz_caught_message(ctx));
163 fz_drop_document(ctx, doc);
164 fz_drop_context(ctx);
165 return EXIT_FAILURE;
166 }
167
168 fz_drop_context(ctx);
169 return EXIT_SUCCESS;
170}
171