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
|
#include "mupdf/html.h"
enum { T, R, B, L };
typedef struct html_document_s html_document;
typedef struct html_page_s html_page;
struct html_document_s
{
fz_document super;
fz_archive *zip;
fz_html_font_set *set;
fz_html *html;
};
struct html_page_s
{
fz_page super;
html_document *doc;
int number;
};
static void
htdoc_drop_document(fz_context *ctx, fz_document *doc_)
{
html_document *doc = (html_document*)doc_;
fz_drop_archive(ctx, doc->zip);
fz_drop_html(ctx, doc->html);
fz_drop_html_font_set(ctx, doc->set);
}
static int
htdoc_resolve_link(fz_context *ctx, fz_document *doc_, const char *dest, float *xp, float *yp)
{
html_document *doc = (html_document*)doc_;
const char *s = strchr(dest, '#');
if (s && s[1] != 0)
{
float y = fz_find_html_target(ctx, doc->html, s+1);
if (y >= 0)
{
int page = y / doc->html->page_h;
if (yp) *yp = y - page * doc->html->page_h;
return page;
}
}
return -1;
}
static int
htdoc_count_pages(fz_context *ctx, fz_document *doc_)
{
html_document *doc = (html_document*)doc_;
int count = ceilf(doc->html->root->h / doc->html->page_h);
return count;
}
static void
htdoc_layout(fz_context *ctx, fz_document *doc_, float w, float h, float em)
{
html_document *doc = (html_document*)doc_;
fz_layout_html(ctx, doc->html, w, h, em);
}
static void
htdoc_drop_page(fz_context *ctx, fz_page *page_)
{
}
static fz_rect *
htdoc_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox)
{
html_page *page = (html_page*)page_;
html_document *doc = page->doc;
bbox->x0 = 0;
bbox->y0 = 0;
bbox->x1 = doc->html->page_w + doc->html->page_margin[L] + doc->html->page_margin[R];
bbox->y1 = doc->html->page_h + doc->html->page_margin[T] + doc->html->page_margin[B];
return bbox;
}
static void
htdoc_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
{
html_page *page = (html_page*)page_;
html_document *doc = page->doc;
fz_draw_html(ctx, dev, ctm, doc->html, page->number);
}
static fz_link *
htdoc_load_links(fz_context *ctx, fz_page *page_)
{
html_page *page = (html_page*)page_;
html_document *doc = page->doc;
return fz_load_html_links(ctx, doc->html, page->number, "");
}
static fz_page *
htdoc_load_page(fz_context *ctx, fz_document *doc_, int number)
{
html_document *doc = (html_document*)doc_;
html_page *page = fz_new_page(ctx, sizeof *page);
page->super.bound_page = htdoc_bound_page;
page->super.run_page_contents = htdoc_run_page;
page->super.load_links = htdoc_load_links;
page->super.drop_page = htdoc_drop_page;
page->doc = doc;
page->number = number;
return (fz_page*)page;
}
int
htdoc_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, int size)
{
if (!strcmp(key, "format"))
return (int)fz_strlcpy(buf, "XHTML", size);
return -1;
}
static fz_document *
htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
html_document *doc;
fz_buffer *buf;
doc = fz_new_document(ctx, html_document);
doc->super.drop_document = htdoc_drop_document;
doc->super.layout = htdoc_layout;
doc->super.resolve_link = htdoc_resolve_link;
doc->super.count_pages = htdoc_count_pages;
doc->super.load_page = htdoc_load_page;
doc->super.lookup_metadata = htdoc_lookup_metadata;
doc->super.is_reflowable = 1;
doc->zip = fz_open_directory(ctx, ".");
doc->set = fz_new_html_font_set(ctx);
buf = fz_read_all(ctx, file, 0);
fz_try(ctx)
doc->html = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, fz_user_css(ctx));
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
fz_rethrow(ctx);
return (fz_document*)doc;
}
static fz_document *
htdoc_open_document(fz_context *ctx, const char *filename)
{
char dirname[2048];
fz_buffer *buf;
html_document *doc;
fz_dirname(dirname, filename, sizeof dirname);
doc = fz_new_document(ctx, html_document);
doc->super.drop_document = htdoc_drop_document;
doc->super.layout = htdoc_layout;
doc->super.resolve_link = htdoc_resolve_link;
doc->super.count_pages = htdoc_count_pages;
doc->super.load_page = htdoc_load_page;
doc->super.lookup_metadata = htdoc_lookup_metadata;
doc->super.is_reflowable = 1;
doc->zip = fz_open_directory(ctx, dirname);
doc->set = fz_new_html_font_set(ctx);
buf = fz_read_file(ctx, filename);
fz_try(ctx)
doc->html = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, fz_user_css(ctx));
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
fz_rethrow(ctx);
return (fz_document*)doc;
}
static int
htdoc_recognize(fz_context *doc, const char *magic)
{
char *ext = strrchr(magic, '.');
if (ext)
{
if (!fz_strcasecmp(ext, ".xml") || !fz_strcasecmp(ext, ".xhtml") ||
!fz_strcasecmp(ext, ".html") || !fz_strcasecmp(ext, ".htm") ||
!fz_strcasecmp(ext, ".fb2"))
return 100;
}
if (!strcmp(magic, "application/html+xml") || !strcmp(magic, "application/xml") || !strcmp(magic, "text/xml"))
return 100;
return 0;
}
fz_document_handler html_document_handler =
{
&htdoc_recognize,
&htdoc_open_document,
&htdoc_open_document_with_stream
};
|