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