output-svg.c 4.1 KB

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