comparison mupdf-source/source/fitz/output-svg.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-2024 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 <limits.h>
26
27 typedef struct
28 {
29 fz_document_writer super;
30 char *path;
31 int count;
32 fz_output *out;
33 int text_format;
34 int reuse_images;
35 int id;
36 } fz_svg_writer;
37
38 const char *fz_svg_write_options_usage =
39 "SVG output options:\n"
40 "\ttext=text: Emit text as <text> elements (inaccurate fonts).\n"
41 "\ttext=path: Emit text as <path> elements (accurate fonts).\n"
42 "\tno-reuse-images: Do not reuse images using <symbol> definitions.\n"
43 "\n"
44 ;
45
46 static fz_device *
47 svg_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
48 {
49 fz_svg_writer *wri = (fz_svg_writer*)wri_;
50 char path[PATH_MAX];
51
52 float w = mediabox.x1 - mediabox.x0;
53 float h = mediabox.y1 - mediabox.y0;
54
55 wri->count += 1;
56
57 if (wri->path)
58 {
59 fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
60 wri->out = fz_new_output_with_path(ctx, path, 0);
61 }
62 else
63 {
64 if (!wri->out)
65 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot write multiple pages to a single SVG output");
66 }
67
68 return fz_new_svg_device_with_id(ctx, wri->out, w, h, wri->text_format, wri->reuse_images, &wri->id);
69 }
70
71 static void
72 svg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
73 {
74 fz_svg_writer *wri = (fz_svg_writer*)wri_;
75
76 fz_try(ctx)
77 {
78 fz_close_device(ctx, dev);
79 fz_close_output(ctx, wri->out);
80 }
81 fz_always(ctx)
82 {
83 fz_drop_device(ctx, dev);
84 fz_drop_output(ctx, wri->out);
85 wri->out = NULL;
86 }
87 fz_catch(ctx)
88 fz_rethrow(ctx);
89 }
90
91 static void
92 svg_drop_writer(fz_context *ctx, fz_document_writer *wri_)
93 {
94 fz_svg_writer *wri = (fz_svg_writer*)wri_;
95 fz_drop_output(ctx, wri->out);
96 fz_free(ctx, wri->path);
97 }
98
99 fz_document_writer *
100 fz_new_svg_writer(fz_context *ctx, const char *path, const char *args)
101 {
102 const char *val;
103 fz_svg_writer *wri = fz_new_derived_document_writer(ctx, fz_svg_writer, svg_begin_page, svg_end_page, NULL, svg_drop_writer);
104
105 wri->text_format = FZ_SVG_TEXT_AS_PATH;
106 wri->reuse_images = 1;
107
108 fz_try(ctx)
109 {
110 if (fz_has_option(ctx, args, "text", &val))
111 {
112 if (fz_option_eq(val, "text"))
113 wri->text_format = FZ_SVG_TEXT_AS_TEXT;
114 else if (fz_option_eq(val, "path"))
115 wri->text_format = FZ_SVG_TEXT_AS_PATH;
116 }
117 if (fz_has_option(ctx, args, "no-reuse-images", &val))
118 if (fz_option_eq(val, "yes"))
119 wri->reuse_images = 0;
120 wri->path = fz_strdup(ctx, path ? path : "out-%04d.svg");
121 }
122 fz_catch(ctx)
123 {
124 fz_free(ctx, wri);
125 fz_rethrow(ctx);
126 }
127
128 return (fz_document_writer*)wri;
129 }
130
131 fz_document_writer *
132 fz_new_svg_writer_with_output(fz_context *ctx, fz_output *out, const char *args)
133 {
134 const char *val;
135 fz_svg_writer *wri = fz_new_derived_document_writer(ctx, fz_svg_writer, svg_begin_page, svg_end_page, NULL, svg_drop_writer);
136
137 wri->text_format = FZ_SVG_TEXT_AS_PATH;
138 wri->reuse_images = 1;
139
140 fz_try(ctx)
141 {
142 if (fz_has_option(ctx, args, "text", &val))
143 {
144 if (fz_option_eq(val, "text"))
145 wri->text_format = FZ_SVG_TEXT_AS_TEXT;
146 else if (fz_option_eq(val, "path"))
147 wri->text_format = FZ_SVG_TEXT_AS_PATH;
148 }
149 if (fz_has_option(ctx, args, "no-reuse-images", &val))
150 if (fz_option_eq(val, "yes"))
151 wri->reuse_images = 0;
152 wri->out = out;
153 }
154 fz_catch(ctx)
155 {
156 fz_free(ctx, wri);
157 fz_rethrow(ctx);
158 }
159
160 return (fz_document_writer*)wri;
161 }