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
|
#include "mupdf/html.h"
#define DEFW (450)
#define DEFH (600)
#define DEFEM (12)
typedef struct html_document_s html_document;
struct html_document_s
{
fz_document super;
fz_context *ctx;
fz_archive *zip;
fz_html_font_set *set;
float page_w, page_h, em;
struct box *box;
};
static void
htdoc_close_document(html_document *doc)
{
fz_close_archive(doc->ctx, doc->zip);
fz_free_html_font_set(doc->ctx, doc->set);
fz_free(doc->ctx, doc);
}
static int
htdoc_count_pages(html_document *doc)
{
int count;
// TODO: reflow
count = ceilf(doc->box->h / doc->page_h);
printf("count pages! %g / %g = %d\n", doc->box->h, doc->page_h, count);
return count;
}
static void *
htdoc_load_page(html_document *doc, int number)
{
printf("load page %d\n", number);
// TODO: reflow
return (void*)((intptr_t)number + 1);
}
static void
htdoc_free_page(html_document *doc, void *page)
{
}
static void
htdoc_layout(html_document *doc, float w, float h, float em)
{
doc->page_w = w;
doc->page_h = h;
doc->em = em;
fz_layout_html(doc->ctx, doc->box, w, h, em);
}
static fz_rect *
htdoc_bound_page(html_document *doc, void *page, fz_rect *bbox)
{
// TODO: reflow
printf("html: bound page\n");
bbox->x0 = bbox->y0 = 0;
bbox->x1 = doc->page_w;
bbox->y1 = doc->page_h;
return bbox;
}
static void
htdoc_run_page(html_document *doc, void *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
{
int n = ((intptr_t)page) - 1;
printf("html: run page %d\n", n);
fz_draw_html(doc->ctx, doc->box, n * doc->page_h, (n+1) * doc->page_h, dev, ctm);
}
static html_document *
htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
html_document *doc;
fz_buffer *buf;
doc = fz_malloc_struct(ctx, html_document);
doc->ctx = ctx;
doc->zip = fz_open_directory(ctx, ".");
doc->set = fz_new_html_font_set(ctx);
doc->super.close = (void*)htdoc_close_document;
doc->super.layout = (void*)htdoc_layout;
doc->super.count_pages = (void*)htdoc_count_pages;
doc->super.load_page = (void*)htdoc_load_page;
doc->super.bound_page = (void*)htdoc_bound_page;
doc->super.run_page_contents = (void*)htdoc_run_page;
doc->super.free_page = (void*)htdoc_free_page;
buf = fz_read_all(file, 0);
fz_write_buffer_byte(ctx, buf, 0);
doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf);
fz_drop_buffer(ctx, buf);
htdoc_layout(doc, DEFW, DEFH, DEFEM);
return doc;
}
static html_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_malloc_struct(ctx, html_document);
doc->ctx = ctx;
doc->zip = fz_open_directory(ctx, dirname);
doc->set = fz_new_html_font_set(ctx);
doc->super.close = (void*)htdoc_close_document;
doc->super.layout = (void*)htdoc_layout;
doc->super.count_pages = (void*)htdoc_count_pages;
doc->super.load_page = (void*)htdoc_load_page;
doc->super.bound_page = (void*)htdoc_bound_page;
doc->super.run_page_contents = (void*)htdoc_run_page;
doc->super.free_page = (void*)htdoc_free_page;
buf = fz_read_file(ctx, filename);
fz_write_buffer_byte(ctx, buf, 0);
doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf);
fz_drop_buffer(ctx, buf);
htdoc_layout(doc, DEFW, DEFH, DEFEM);
return 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"))
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 =
{
(fz_document_recognize_fn *)&htdoc_recognize,
(fz_document_open_fn *)&htdoc_open_document,
(fz_document_open_with_stream_fn *)&htdoc_open_document_with_stream
};
|