/src/mupdf/source/fitz/output-svg.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | 0 | { |
49 | 0 | fz_svg_writer *wri = (fz_svg_writer*)wri_; |
50 | 0 | char path[PATH_MAX]; |
51 | |
|
52 | 0 | float w = mediabox.x1 - mediabox.x0; |
53 | 0 | float h = mediabox.y1 - mediabox.y0; |
54 | |
|
55 | 0 | wri->count += 1; |
56 | |
|
57 | 0 | if (wri->path) |
58 | 0 | { |
59 | 0 | fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count); |
60 | 0 | wri->out = fz_new_output_with_path(ctx, path, 0); |
61 | 0 | } |
62 | 0 | else |
63 | 0 | { |
64 | 0 | if (!wri->out) |
65 | 0 | fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot write multiple pages to a single SVG output"); |
66 | 0 | } |
67 | | |
68 | 0 | return fz_new_svg_device_with_id(ctx, wri->out, w, h, wri->text_format, wri->reuse_images, &wri->id); |
69 | 0 | } |
70 | | |
71 | | static void |
72 | | svg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) |
73 | 0 | { |
74 | 0 | fz_svg_writer *wri = (fz_svg_writer*)wri_; |
75 | |
|
76 | 0 | fz_try(ctx) |
77 | 0 | { |
78 | 0 | fz_close_device(ctx, dev); |
79 | 0 | fz_close_output(ctx, wri->out); |
80 | 0 | } |
81 | 0 | fz_always(ctx) |
82 | 0 | { |
83 | 0 | fz_drop_device(ctx, dev); |
84 | 0 | fz_drop_output(ctx, wri->out); |
85 | 0 | wri->out = NULL; |
86 | 0 | } |
87 | 0 | fz_catch(ctx) |
88 | 0 | fz_rethrow(ctx); |
89 | 0 | } |
90 | | |
91 | | static void |
92 | | svg_drop_writer(fz_context *ctx, fz_document_writer *wri_) |
93 | 0 | { |
94 | 0 | fz_svg_writer *wri = (fz_svg_writer*)wri_; |
95 | 0 | fz_drop_output(ctx, wri->out); |
96 | 0 | fz_free(ctx, wri->path); |
97 | 0 | } |
98 | | |
99 | | fz_document_writer * |
100 | | fz_new_svg_writer(fz_context *ctx, const char *path, const char *args) |
101 | 0 | { |
102 | 0 | const char *val; |
103 | 0 | 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 | 0 | wri->text_format = FZ_SVG_TEXT_AS_PATH; |
106 | 0 | wri->reuse_images = 1; |
107 | |
|
108 | 0 | fz_try(ctx) |
109 | 0 | { |
110 | 0 | if (fz_has_option(ctx, args, "text", &val)) |
111 | 0 | { |
112 | 0 | if (fz_option_eq(val, "text")) |
113 | 0 | wri->text_format = FZ_SVG_TEXT_AS_TEXT; |
114 | 0 | else if (fz_option_eq(val, "path")) |
115 | 0 | wri->text_format = FZ_SVG_TEXT_AS_PATH; |
116 | 0 | } |
117 | 0 | if (fz_has_option(ctx, args, "no-reuse-images", &val)) |
118 | 0 | if (fz_option_eq(val, "yes")) |
119 | 0 | wri->reuse_images = 0; |
120 | 0 | wri->path = fz_strdup(ctx, path ? path : "out-%04d.svg"); |
121 | 0 | } |
122 | 0 | fz_catch(ctx) |
123 | 0 | { |
124 | 0 | fz_free(ctx, wri); |
125 | 0 | fz_rethrow(ctx); |
126 | 0 | } |
127 | | |
128 | 0 | return (fz_document_writer*)wri; |
129 | 0 | } |
130 | | |
131 | | fz_document_writer * |
132 | | fz_new_svg_writer_with_output(fz_context *ctx, fz_output *out, const char *args) |
133 | 0 | { |
134 | 0 | const char *val; |
135 | 0 | 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 | 0 | wri->text_format = FZ_SVG_TEXT_AS_PATH; |
138 | 0 | wri->reuse_images = 1; |
139 | |
|
140 | 0 | fz_try(ctx) |
141 | 0 | { |
142 | 0 | if (fz_has_option(ctx, args, "text", &val)) |
143 | 0 | { |
144 | 0 | if (fz_option_eq(val, "text")) |
145 | 0 | wri->text_format = FZ_SVG_TEXT_AS_TEXT; |
146 | 0 | else if (fz_option_eq(val, "path")) |
147 | 0 | wri->text_format = FZ_SVG_TEXT_AS_PATH; |
148 | 0 | } |
149 | 0 | if (fz_has_option(ctx, args, "no-reuse-images", &val)) |
150 | 0 | if (fz_option_eq(val, "yes")) |
151 | 0 | wri->reuse_images = 0; |
152 | 0 | wri->out = out; |
153 | 0 | } |
154 | 0 | fz_catch(ctx) |
155 | 0 | { |
156 | 0 | fz_free(ctx, wri); |
157 | 0 | fz_rethrow(ctx); |
158 | 0 | } |
159 | | |
160 | 0 | return (fz_document_writer*)wri; |
161 | 0 | } |