3 static void xps_init_document(xps_document *doc);
6 xps_new_part(xps_document *doc, char *name, unsigned char *data, int size)
10 part = fz_malloc_struct(doc->ctx, xps_part);
13 part->name = fz_strdup(doc->ctx, name);
19 fz_free(doc->ctx, part->name);
20 fz_free(doc->ctx, part->data);
21 fz_free(doc->ctx, part);
29 xps_free_part(xps_document *doc, xps_part *part)
31 fz_free(doc->ctx, part->name);
32 fz_free(doc->ctx, part->data);
33 fz_free(doc->ctx, part);
37 * Read and interleave split parts from a ZIP file.
40 xps_read_part(xps_document *doc, char *partname)
42 fz_context *ctx = doc->ctx;
43 fz_archive *zip = doc->zip;
56 /* All in one piece */
57 if (fz_has_archive_entry(ctx, zip, name))
59 buf = fz_read_archive_entry(ctx, zip, name);
62 /* Assemble all the pieces */
65 buf = fz_new_buffer(ctx, 512);
67 for (count = 0; !seen_last; ++count)
69 sprintf(path, "%s/[%d].piece", name, count);
70 if (fz_has_archive_entry(ctx, zip, path))
72 tmp = fz_read_archive_entry(ctx, zip, path);
73 fz_buffer_cat(ctx, buf, tmp);
74 fz_drop_buffer(ctx, tmp);
78 sprintf(path, "%s/[%d].last.piece", name, count);
79 if (fz_has_archive_entry(ctx, zip, path))
81 tmp = fz_read_archive_entry(ctx, zip, path);
82 fz_buffer_cat(ctx, buf, tmp);
83 fz_drop_buffer(ctx, tmp);
88 fz_drop_buffer(ctx, buf);
89 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find all pieces for part '%s'", partname);
95 fz_write_buffer_byte(ctx, buf, 0); /* zero-terminate */
97 /* take over the data */
99 /* size doesn't include the added zero-terminator */
103 return xps_new_part(doc, partname, data, size);
107 xps_has_part(xps_document *doc, char *name)
112 if (fz_has_archive_entry(doc->ctx, doc->zip, name))
114 sprintf(buf, "%s/[0].piece", name);
115 if (fz_has_archive_entry(doc->ctx, doc->zip, buf))
117 sprintf(buf, "%s/[0].last.piece", name);
118 if (fz_has_archive_entry(doc->ctx, doc->zip, buf))
123 static xps_document *
124 xps_open_document_with_directory(fz_context *ctx, const char *directory)
128 doc = fz_malloc_struct(ctx, xps_document);
129 xps_init_document(doc);
131 doc->zip = fz_open_directory(ctx, directory);
135 xps_read_page_list(doc);
139 xps_close_document(doc);
147 xps_open_document_with_stream(fz_context *ctx, fz_stream *file)
151 doc = fz_malloc_struct(ctx, xps_document);
152 xps_init_document(doc);
157 doc->zip = fz_open_archive_with_stream(ctx, file);
158 xps_read_page_list(doc);
162 xps_close_document(doc);
170 xps_open_document(fz_context *ctx, const char *filename)
177 if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels"))
179 fz_strlcpy(buf, filename, sizeof buf);
180 p = strstr(buf, "/_rels/.rels");
182 p = strstr(buf, "\\_rels\\.rels");
184 return xps_open_document_with_directory(ctx, buf);
187 file = fz_open_file(ctx, filename);
189 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
193 doc = xps_open_document_with_stream(ctx, file);
201 fz_rethrow_message(ctx, "cannot load document '%s'", filename);
207 xps_close_document(xps_document *doc)
209 xps_font_cache *font, *next;
215 fz_close_archive(doc->ctx, doc->zip);
217 font = doc->font_table;
221 fz_drop_font(doc->ctx, font->font);
222 fz_free(doc->ctx, font->name);
223 fz_free(doc->ctx, font);
227 xps_free_page_list(doc);
229 fz_free(doc->ctx, doc->start_part);
230 fz_free(doc->ctx, doc);
234 xps_meta(xps_document *doc, int key, void *ptr, int size)
238 case FZ_META_FORMAT_INFO:
239 sprintf((char *)ptr, "XPS");
242 return FZ_META_UNKNOWN_KEY;
247 xps_rebind(xps_document *doc, fz_context *ctx)
250 fz_rebind_archive(doc->zip, ctx);
251 fz_rebind_device(doc->dev, ctx);
255 xps_init_document(xps_document *doc)
257 doc->super.close = (fz_document_close_fn *)xps_close_document;
258 doc->super.load_outline = (fz_document_load_outline_fn *)xps_load_outline;
259 doc->super.count_pages = (fz_document_count_pages_fn *)xps_count_pages;
260 doc->super.load_page = (fz_document_load_page_fn *)xps_load_page;
261 doc->super.load_links = (fz_document_load_links_fn *)xps_load_links;
262 doc->super.bound_page = (fz_document_bound_page_fn *)xps_bound_page;
263 doc->super.run_page_contents = (fz_document_run_page_contents_fn *)xps_run_page;
264 doc->super.free_page = (fz_document_free_page_fn *)xps_free_page;
265 doc->super.meta = (fz_document_meta_fn *)xps_meta;
266 doc->super.rebind = (fz_document_rebind_fn *)xps_rebind;