Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/fitz/writer.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "mupdf/fitz.h" | |
| 24 | |
| 25 #include <string.h> | |
| 26 | |
| 27 /* Return non-null terminated pointers to key/value entries in comma separated | |
| 28 * option string. A plain key has the default value 'yes'. Use strncmp to compare | |
| 29 * key/value strings. */ | |
| 30 static const char * | |
| 31 fz_get_option(fz_context *ctx, const char **key, const char **val, const char *opts) | |
| 32 { | |
| 33 if (!opts || *opts == 0) | |
| 34 return NULL; | |
| 35 | |
| 36 if (*opts == ',') | |
| 37 ++opts; | |
| 38 | |
| 39 *key = opts; | |
| 40 while (*opts != 0 && *opts != ',' && *opts != '=') | |
| 41 ++opts; | |
| 42 | |
| 43 if (*opts == '=') | |
| 44 { | |
| 45 *val = ++opts; | |
| 46 while (*opts != 0 && *opts != ',') | |
| 47 ++opts; | |
| 48 } | |
| 49 else | |
| 50 { | |
| 51 *val = "yes"; | |
| 52 } | |
| 53 | |
| 54 return opts; | |
| 55 } | |
| 56 | |
| 57 int | |
| 58 fz_has_option(fz_context *ctx, const char *opts, const char *key, const char **val) | |
| 59 { | |
| 60 const char *straw; | |
| 61 size_t n = strlen(key); | |
| 62 while ((opts = fz_get_option(ctx, &straw, val, opts))) | |
| 63 if (!strncmp(straw, key, n) && (straw[n] == '=' || straw[n] == ',' || straw[n] == 0)) | |
| 64 return 1; | |
| 65 return 0; | |
| 66 } | |
| 67 | |
| 68 int | |
| 69 fz_option_eq(const char *a, const char *b) | |
| 70 { | |
| 71 size_t n = strlen(b); | |
| 72 return !strncmp(a, b, n) && (a[n] == ',' || a[n] == 0); | |
| 73 } | |
| 74 | |
| 75 size_t | |
| 76 fz_copy_option(fz_context *ctx, const char *val, char *dest, size_t maxlen) | |
| 77 { | |
| 78 const char *e = val; | |
| 79 size_t len, len2; | |
| 80 | |
| 81 if (val == NULL) { | |
| 82 if (maxlen) | |
| 83 *dest = 0; | |
| 84 return 0; | |
| 85 } | |
| 86 | |
| 87 while (*e != ',' && *e != 0) | |
| 88 e++; | |
| 89 | |
| 90 len = e-val; | |
| 91 len2 = len+1; /* Allow for terminator */ | |
| 92 if (len > maxlen) | |
| 93 len = maxlen; | |
| 94 memcpy(dest, val, len); | |
| 95 if (len < maxlen) | |
| 96 memset(dest+len, 0, maxlen-len); | |
| 97 | |
| 98 return len2 >= maxlen ? len2 - maxlen : 0; | |
| 99 } | |
| 100 | |
| 101 fz_document_writer *fz_new_document_writer_of_size(fz_context *ctx, size_t size, fz_document_writer_begin_page_fn *begin_page, | |
| 102 fz_document_writer_end_page_fn *end_page, fz_document_writer_close_writer_fn *close, fz_document_writer_drop_writer_fn *drop) | |
| 103 { | |
| 104 fz_document_writer *wri = Memento_label(fz_calloc(ctx, 1, size), "fz_document_writer"); | |
| 105 | |
| 106 wri->begin_page = begin_page; | |
| 107 wri->end_page = end_page; | |
| 108 wri->close_writer = close; | |
| 109 wri->drop_writer = drop; | |
| 110 | |
| 111 return wri; | |
| 112 } | |
| 113 | |
| 114 static void fz_save_pixmap_as_jpeg_default(fz_context *ctx, fz_pixmap *pixmap, const char *filename) | |
| 115 { | |
| 116 fz_save_pixmap_as_jpeg(ctx, pixmap, filename, 90); | |
| 117 } | |
| 118 | |
| 119 fz_document_writer *fz_new_jpeg_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 120 { | |
| 121 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.jpeg", 0, fz_save_pixmap_as_jpeg_default); | |
| 122 } | |
| 123 | |
| 124 fz_document_writer *fz_new_png_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 125 { | |
| 126 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.png", 0, fz_save_pixmap_as_png); | |
| 127 } | |
| 128 | |
| 129 fz_document_writer *fz_new_pam_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 130 { | |
| 131 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.pam", 0, fz_save_pixmap_as_pam); | |
| 132 } | |
| 133 | |
| 134 fz_document_writer *fz_new_pnm_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 135 { | |
| 136 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.pnm", 0, fz_save_pixmap_as_pnm); | |
| 137 } | |
| 138 | |
| 139 fz_document_writer *fz_new_pgm_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 140 { | |
| 141 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.pgm", 1, fz_save_pixmap_as_pnm); | |
| 142 } | |
| 143 | |
| 144 fz_document_writer *fz_new_ppm_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 145 { | |
| 146 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.ppm", 3, fz_save_pixmap_as_pnm); | |
| 147 } | |
| 148 | |
| 149 fz_document_writer *fz_new_pbm_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 150 { | |
| 151 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.pbm", 1, fz_save_pixmap_as_pbm); | |
| 152 } | |
| 153 | |
| 154 fz_document_writer *fz_new_pkm_pixmap_writer(fz_context *ctx, const char *path, const char *options) | |
| 155 { | |
| 156 return fz_new_pixmap_writer(ctx, path, options, "out-%04d.pkm", 4, fz_save_pixmap_as_pkm); | |
| 157 } | |
| 158 | |
| 159 static int is_extension(const char *a, const char *ext) | |
| 160 { | |
| 161 if (!a) | |
| 162 return 0; | |
| 163 if (a[0] == '.') | |
| 164 ++a; | |
| 165 return !fz_strcasecmp(a, ext); | |
| 166 } | |
| 167 | |
| 168 static const char *prev_period(const char *start, const char *p) | |
| 169 { | |
| 170 while (--p > start) | |
| 171 if (*p == '.') | |
| 172 return p; | |
| 173 return NULL; | |
| 174 } | |
| 175 | |
| 176 fz_document_writer * | |
| 177 fz_new_document_writer(fz_context *ctx, const char *path, const char *explicit_format, const char *options) | |
| 178 { | |
| 179 const char *format = explicit_format; | |
| 180 if (!format) | |
| 181 format = strrchr(path, '.'); | |
| 182 while (format) | |
| 183 { | |
| 184 #if FZ_ENABLE_OCR_OUTPUT | |
| 185 if (is_extension(format, "ocr")) | |
| 186 return fz_new_pdfocr_writer(ctx, path, options); | |
| 187 #endif | |
| 188 #if FZ_ENABLE_PDF | |
| 189 if (is_extension(format, "pdf")) | |
| 190 return fz_new_pdf_writer(ctx, path, options); | |
| 191 #endif | |
| 192 | |
| 193 if (is_extension(format, "cbz")) | |
| 194 return fz_new_cbz_writer(ctx, path, options); | |
| 195 if (is_extension(format, "csv")) | |
| 196 return fz_new_csv_writer(ctx, path, options); | |
| 197 | |
| 198 if (is_extension(format, "svg")) | |
| 199 return fz_new_svg_writer(ctx, path, options); | |
| 200 | |
| 201 if (is_extension(format, "png")) | |
| 202 return fz_new_png_pixmap_writer(ctx, path, options); | |
| 203 if (is_extension(format, "pam")) | |
| 204 return fz_new_pam_pixmap_writer(ctx, path, options); | |
| 205 if (is_extension(format, "pnm")) | |
| 206 return fz_new_pnm_pixmap_writer(ctx, path, options); | |
| 207 if (is_extension(format, "pgm")) | |
| 208 return fz_new_pgm_pixmap_writer(ctx, path, options); | |
| 209 if (is_extension(format, "ppm")) | |
| 210 return fz_new_ppm_pixmap_writer(ctx, path, options); | |
| 211 if (is_extension(format, "pbm")) | |
| 212 return fz_new_pbm_pixmap_writer(ctx, path, options); | |
| 213 if (is_extension(format, "pkm")) | |
| 214 return fz_new_pkm_pixmap_writer(ctx, path, options); | |
| 215 if (is_extension(format, "jpeg") || is_extension(format, "jpg")) | |
| 216 return fz_new_jpeg_pixmap_writer(ctx, path, options); | |
| 217 | |
| 218 if (is_extension(format, "pcl")) | |
| 219 return fz_new_pcl_writer(ctx, path, options); | |
| 220 if (is_extension(format, "pclm")) | |
| 221 return fz_new_pclm_writer(ctx, path, options); | |
| 222 if (is_extension(format, "ps")) | |
| 223 return fz_new_ps_writer(ctx, path, options); | |
| 224 if (is_extension(format, "pwg")) | |
| 225 return fz_new_pwg_writer(ctx, path, options); | |
| 226 | |
| 227 if (is_extension(format, "txt") || is_extension(format, "text")) | |
| 228 return fz_new_text_writer(ctx, "text", path, options); | |
| 229 if (is_extension(format, "html")) | |
| 230 return fz_new_text_writer(ctx, "html", path, options); | |
| 231 if (is_extension(format, "xhtml")) | |
| 232 return fz_new_text_writer(ctx, "xhtml", path, options); | |
| 233 if (is_extension(format, "stext") || is_extension(format, "stext.xml")) | |
| 234 return fz_new_text_writer(ctx, "stext.xml", path, options); | |
| 235 if (is_extension(format, "stext.json")) | |
| 236 return fz_new_text_writer(ctx, "stext.json", path, options); | |
| 237 | |
| 238 #if FZ_ENABLE_ODT_OUTPUT | |
| 239 if (is_extension(format, "odt")) | |
| 240 return fz_new_odt_writer(ctx, path, options); | |
| 241 #endif | |
| 242 #if FZ_ENABLE_DOCX_OUTPUT | |
| 243 if (is_extension(format, "docx")) | |
| 244 return fz_new_docx_writer(ctx, path, options); | |
| 245 #endif | |
| 246 if (format != explicit_format) | |
| 247 format = prev_period(path, format); | |
| 248 else | |
| 249 format = NULL; | |
| 250 } | |
| 251 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot detect document format"); | |
| 252 } | |
| 253 | |
| 254 fz_document_writer * | |
| 255 fz_new_document_writer_with_output(fz_context *ctx, fz_output *out, const char *format, const char *options) | |
| 256 { | |
| 257 #if FZ_ENABLE_OCR_OUTPUT | |
| 258 if (is_extension(format, "ocr")) | |
| 259 return fz_new_pdfocr_writer_with_output(ctx, out, options); | |
| 260 #endif | |
| 261 #if FZ_ENABLE_PDF | |
| 262 if (is_extension(format, "pdf")) | |
| 263 return fz_new_pdf_writer_with_output(ctx, out, options); | |
| 264 #endif | |
| 265 | |
| 266 if (is_extension(format, "cbz")) | |
| 267 return fz_new_cbz_writer_with_output(ctx, out, options); | |
| 268 if (is_extension(format, "csv")) | |
| 269 return fz_new_csv_writer_with_output(ctx, out, options); | |
| 270 | |
| 271 if (is_extension(format, "svg")) | |
| 272 return fz_new_svg_writer_with_output(ctx, out, options); | |
| 273 | |
| 274 if (is_extension(format, "pcl")) | |
| 275 return fz_new_pcl_writer_with_output(ctx, out, options); | |
| 276 if (is_extension(format, "pclm")) | |
| 277 return fz_new_pclm_writer_with_output(ctx, out, options); | |
| 278 if (is_extension(format, "ps")) | |
| 279 return fz_new_ps_writer_with_output(ctx, out, options); | |
| 280 if (is_extension(format, "pwg")) | |
| 281 return fz_new_pwg_writer_with_output(ctx, out, options); | |
| 282 | |
| 283 if (is_extension(format, "txt") || is_extension(format, "text")) | |
| 284 return fz_new_text_writer_with_output(ctx, "text", out, options); | |
| 285 if (is_extension(format, "html")) | |
| 286 return fz_new_text_writer_with_output(ctx, "html", out, options); | |
| 287 if (is_extension(format, "xhtml")) | |
| 288 return fz_new_text_writer_with_output(ctx, "xhtml", out, options); | |
| 289 if (is_extension(format, "stext") || is_extension(format, "stext.xml")) | |
| 290 return fz_new_text_writer_with_output(ctx, "stext.xml", out, options); | |
| 291 if (is_extension(format, "stext.json")) | |
| 292 return fz_new_text_writer_with_output(ctx, "stext.json", out, options); | |
| 293 | |
| 294 #if FZ_ENABLE_ODT_OUTPUT | |
| 295 if (is_extension(format, "odt")) | |
| 296 return fz_new_odt_writer_with_output(ctx, out, options); | |
| 297 #endif | |
| 298 #if FZ_ENABLE_DOCX_OUTPUT | |
| 299 if (is_extension(format, "docx")) | |
| 300 return fz_new_docx_writer_with_output(ctx, out, options); | |
| 301 #endif | |
| 302 | |
| 303 fz_throw(ctx, FZ_ERROR_ARGUMENT, "unknown output document format: %s", format); | |
| 304 } | |
| 305 | |
| 306 fz_document_writer * | |
| 307 fz_new_document_writer_with_buffer(fz_context *ctx, fz_buffer *buffer, const char *format, const char *options) | |
| 308 { | |
| 309 fz_document_writer *wri; | |
| 310 fz_output *out = fz_new_output_with_buffer(ctx, buffer); | |
| 311 fz_try(ctx) { | |
| 312 wri = fz_new_document_writer_with_output(ctx, out, format, options); | |
| 313 } | |
| 314 fz_catch(ctx) { | |
| 315 fz_drop_output(ctx, out); | |
| 316 fz_rethrow(ctx); | |
| 317 } | |
| 318 return wri; | |
| 319 } | |
| 320 | |
| 321 void | |
| 322 fz_close_document_writer(fz_context *ctx, fz_document_writer *wri) | |
| 323 { | |
| 324 if (wri->close_writer) | |
| 325 wri->close_writer(ctx, wri); | |
| 326 wri->close_writer = NULL; | |
| 327 } | |
| 328 | |
| 329 void | |
| 330 fz_drop_document_writer(fz_context *ctx, fz_document_writer *wri) | |
| 331 { | |
| 332 if (!wri) | |
| 333 return; | |
| 334 | |
| 335 if (wri->close_writer) | |
| 336 fz_warn(ctx, "dropping unclosed document writer"); | |
| 337 if (wri->dev) | |
| 338 fz_drop_device(ctx, wri->dev); | |
| 339 if (wri->drop_writer) | |
| 340 wri->drop_writer(ctx, wri); | |
| 341 fz_free(ctx, wri); | |
| 342 } | |
| 343 | |
| 344 fz_device * | |
| 345 fz_begin_page(fz_context *ctx, fz_document_writer *wri, fz_rect mediabox) | |
| 346 { | |
| 347 if (!wri) | |
| 348 return NULL; | |
| 349 if (wri->dev) | |
| 350 fz_throw(ctx, FZ_ERROR_ARGUMENT, "called begin page without ending the previous page"); | |
| 351 wri->dev = wri->begin_page(ctx, wri, mediabox); | |
| 352 return wri->dev; | |
| 353 } | |
| 354 | |
| 355 void | |
| 356 fz_end_page(fz_context *ctx, fz_document_writer *wri) | |
| 357 { | |
| 358 fz_device *dev; | |
| 359 | |
| 360 if (!wri) | |
| 361 return; | |
| 362 dev = wri->dev; | |
| 363 wri->dev = NULL; | |
| 364 wri->end_page(ctx, wri, dev); | |
| 365 } | |
| 366 | |
| 367 void | |
| 368 fz_write_document(fz_context *ctx, fz_document_writer *wri, fz_document *doc) | |
| 369 { | |
| 370 int i, n; | |
| 371 fz_page *page = NULL; | |
| 372 fz_device *dev; | |
| 373 | |
| 374 fz_var(page); | |
| 375 | |
| 376 n = fz_count_pages(ctx, doc); | |
| 377 fz_try(ctx) | |
| 378 { | |
| 379 for (i = 0; i < n; i++) | |
| 380 { | |
| 381 page = fz_load_page(ctx, doc, i); | |
| 382 dev = fz_begin_page(ctx, wri, fz_bound_page(ctx, page)); | |
| 383 fz_run_page(ctx, page, dev, fz_identity, NULL); | |
| 384 fz_drop_page(ctx, page); | |
| 385 page = NULL; | |
| 386 fz_end_page(ctx, wri); | |
| 387 } | |
| 388 } | |
| 389 fz_catch(ctx) | |
| 390 { | |
| 391 fz_drop_page(ctx, page); | |
| 392 fz_rethrow(ctx); | |
| 393 } | |
| 394 } |
