Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/xps/xps-outline.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-2021 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 #include "xps-imp.h" | |
| 25 | |
| 26 #include <stdlib.h> | |
| 27 #include <math.h> | |
| 28 | |
| 29 /* | |
| 30 * Parse the document structure / outline parts referenced from fixdoc relationships. | |
| 31 */ | |
| 32 | |
| 33 static fz_outline * | |
| 34 xps_lookup_last_outline_at_level(fz_context *ctx, xps_document *doc, fz_outline *node, int level, int target_level) | |
| 35 { | |
| 36 while (node->next) | |
| 37 node = node->next; | |
| 38 if (level == target_level || !node->down) | |
| 39 return node; | |
| 40 return xps_lookup_last_outline_at_level(ctx, doc, node->down, level + 1, target_level); | |
| 41 } | |
| 42 | |
| 43 static fz_outline * | |
| 44 xps_parse_document_outline(fz_context *ctx, xps_document *doc, fz_xml *root) | |
| 45 { | |
| 46 fz_xml *node; | |
| 47 fz_outline *head = NULL, *entry, *tail; | |
| 48 int last_level = 1, this_level; | |
| 49 for (node = fz_xml_down(root); node; node = fz_xml_next(node)) | |
| 50 { | |
| 51 if (fz_xml_is_tag(node, "OutlineEntry")) | |
| 52 { | |
| 53 char *level = fz_xml_att(node, "OutlineLevel"); | |
| 54 char *target = fz_xml_att(node, "OutlineTarget"); | |
| 55 char *description = fz_xml_att(node, "Description"); | |
| 56 if (!target || !description) | |
| 57 continue; | |
| 58 | |
| 59 entry = fz_new_outline(ctx); | |
| 60 entry->title = Memento_label(fz_strdup(ctx, description), "outline_title"); | |
| 61 entry->uri = Memento_label(fz_strdup(ctx, target), "outline_uri"); | |
| 62 entry->page = xps_lookup_link_target(ctx, (fz_document*)doc, target).loc; | |
| 63 entry->down = NULL; | |
| 64 entry->next = NULL; | |
| 65 | |
| 66 this_level = level ? atoi(level) : 1; | |
| 67 | |
| 68 if (!head) | |
| 69 { | |
| 70 head = entry; | |
| 71 } | |
| 72 else | |
| 73 { | |
| 74 tail = xps_lookup_last_outline_at_level(ctx, doc, head, 1, this_level); | |
| 75 if (this_level > last_level) | |
| 76 tail->down = entry; | |
| 77 else | |
| 78 tail->next = entry; | |
| 79 } | |
| 80 | |
| 81 last_level = this_level; | |
| 82 } | |
| 83 } | |
| 84 return head; | |
| 85 } | |
| 86 | |
| 87 static fz_outline * | |
| 88 xps_parse_document_structure(fz_context *ctx, xps_document *doc, fz_xml *root) | |
| 89 { | |
| 90 fz_xml *node; | |
| 91 if (fz_xml_is_tag(root, "DocumentStructure")) | |
| 92 { | |
| 93 node = fz_xml_down(root); | |
| 94 if (node && fz_xml_is_tag(node, "DocumentStructure.Outline")) | |
| 95 { | |
| 96 node = fz_xml_down(node); | |
| 97 if (node && fz_xml_is_tag(node, "DocumentOutline")) | |
| 98 return xps_parse_document_outline(ctx, doc, node); | |
| 99 } | |
| 100 } | |
| 101 return NULL; | |
| 102 } | |
| 103 | |
| 104 static fz_outline * | |
| 105 xps_load_document_structure(fz_context *ctx, xps_document *doc, xps_fixdoc *fixdoc) | |
| 106 { | |
| 107 xps_part *part; | |
| 108 fz_xml_doc *xml = NULL; | |
| 109 fz_outline *outline = NULL; | |
| 110 | |
| 111 fz_var(xml); | |
| 112 | |
| 113 part = xps_read_part(ctx, doc, fixdoc->outline); | |
| 114 fz_try(ctx) | |
| 115 { | |
| 116 xml = fz_parse_xml(ctx, part->data, 0); | |
| 117 outline = xps_parse_document_structure(ctx, doc, fz_xml_root(xml)); | |
| 118 } | |
| 119 fz_always(ctx) | |
| 120 { | |
| 121 fz_drop_xml(ctx, xml); | |
| 122 xps_drop_part(ctx, doc, part); | |
| 123 } | |
| 124 fz_catch(ctx) | |
| 125 { | |
| 126 fz_rethrow(ctx); | |
| 127 } | |
| 128 | |
| 129 return outline; | |
| 130 } | |
| 131 | |
| 132 fz_outline * | |
| 133 xps_load_outline(fz_context *ctx, fz_document *doc_) | |
| 134 { | |
| 135 xps_document *doc = (xps_document*)doc_; | |
| 136 xps_fixdoc *fixdoc; | |
| 137 fz_outline *head = NULL, *tail, *outline = NULL; | |
| 138 | |
| 139 for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next) | |
| 140 { | |
| 141 if (fixdoc->outline) | |
| 142 { | |
| 143 fz_try(ctx) | |
| 144 { | |
| 145 outline = xps_load_document_structure(ctx, doc, fixdoc); | |
| 146 } | |
| 147 fz_catch(ctx) | |
| 148 { | |
| 149 fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); | |
| 150 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 151 fz_report_error(ctx); | |
| 152 outline = NULL; | |
| 153 } | |
| 154 if (!outline) | |
| 155 continue; | |
| 156 | |
| 157 if (!head) | |
| 158 head = outline; | |
| 159 else | |
| 160 { | |
| 161 while (tail->next) | |
| 162 tail = tail->next; | |
| 163 tail->next = outline; | |
| 164 } | |
| 165 tail = outline; | |
| 166 } | |
| 167 } | |
| 168 return head; | |
| 169 } |
