Coverage Report

Created: 2023-06-07 06:20

/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
#ifndef PATH_MAX
28
#define PATH_MAX 4096
29
#endif
30
31
typedef struct
32
{
33
  fz_document_writer super;
34
  char *path;
35
  int count;
36
  fz_output *out;
37
  int text_format;
38
  int reuse_images;
39
  int id;
40
} fz_svg_writer;
41
42
const char *fz_svg_write_options_usage =
43
  "SVG output options:\n"
44
  "\ttext=text: Emit text as <text> elements (inaccurate fonts).\n"
45
  "\ttext=path: Emit text as <path> elements (accurate fonts).\n"
46
  "\tno-reuse-images: Do not reuse images using <symbol> definitions.\n"
47
  "\n"
48
  ;
49
50
static fz_device *
51
svg_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
52
0
{
53
0
  fz_svg_writer *wri = (fz_svg_writer*)wri_;
54
0
  char path[PATH_MAX];
55
56
0
  float w = mediabox.x1 - mediabox.x0;
57
0
  float h = mediabox.y1 - mediabox.y0;
58
59
0
  wri->count += 1;
60
61
0
  fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
62
0
  wri->out = fz_new_output_with_path(ctx, path, 0);
63
0
  return fz_new_svg_device_with_id(ctx, wri->out, w, h, wri->text_format, wri->reuse_images, &wri->id);
64
0
}
65
66
static void
67
svg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
68
0
{
69
0
  fz_svg_writer *wri = (fz_svg_writer*)wri_;
70
71
0
  fz_try(ctx)
72
0
  {
73
0
    fz_close_device(ctx, dev);
74
0
    fz_close_output(ctx, wri->out);
75
0
  }
76
0
  fz_always(ctx)
77
0
  {
78
0
    fz_drop_device(ctx, dev);
79
0
    fz_drop_output(ctx, wri->out);
80
0
    wri->out = NULL;
81
0
  }
82
0
  fz_catch(ctx)
83
0
    fz_rethrow(ctx);
84
0
}
85
86
static void
87
svg_drop_writer(fz_context *ctx, fz_document_writer *wri_)
88
0
{
89
0
  fz_svg_writer *wri = (fz_svg_writer*)wri_;
90
0
  fz_drop_output(ctx, wri->out);
91
0
  fz_free(ctx, wri->path);
92
0
}
93
94
fz_document_writer *
95
fz_new_svg_writer(fz_context *ctx, const char *path, const char *args)
96
0
{
97
0
  const char *val;
98
0
  fz_svg_writer *wri = fz_new_derived_document_writer(ctx, fz_svg_writer, svg_begin_page, svg_end_page, NULL, svg_drop_writer);
99
100
0
  wri->text_format = FZ_SVG_TEXT_AS_PATH;
101
0
  wri->reuse_images = 1;
102
103
0
  fz_try(ctx)
104
0
  {
105
0
    if (fz_has_option(ctx, args, "text", &val))
106
0
    {
107
0
      if (fz_option_eq(val, "text"))
108
0
        wri->text_format = FZ_SVG_TEXT_AS_TEXT;
109
0
      else if (fz_option_eq(val, "path"))
110
0
        wri->text_format = FZ_SVG_TEXT_AS_PATH;
111
0
    }
112
0
    if (fz_has_option(ctx, args, "no-reuse-images", &val))
113
0
      if (fz_option_eq(val, "yes"))
114
0
        wri->reuse_images = 0;
115
0
    wri->path = fz_strdup(ctx, path ? path : "out-%04d.svg");
116
0
  }
117
0
  fz_catch(ctx)
118
0
  {
119
0
    fz_free(ctx, wri);
120
0
    fz_rethrow(ctx);
121
0
  }
122
123
0
  return (fz_document_writer*)wri;
124
0
}