35 #include <gdk-pixbuf/gdk-pixbuf.h>
43 #include <cairo-pdf.h>
45 #include <cairo-svg.h>
46 #include <glib/gstdio.h>
49 exportimage_extract_svg_inner_content (
const gchar *svgText) {
51 const gchar *svgOpenEnd;
52 const gchar *svgClose;
54 if (svgText == NULL) {
58 svgOpen = strstr (svgText,
"<svg");
59 if (svgOpen == NULL) {
63 svgOpenEnd = strchr (svgOpen,
'>');
64 if (svgOpenEnd == NULL) {
68 svgClose = g_strrstr (svgOpenEnd,
"</svg>");
69 if (svgClose == NULL || svgClose <= svgOpenEnd) {
73 return g_strndup (svgOpenEnd + 1, svgClose - (svgOpenEnd + 1));
77 exportimage_append_svg_background (GString *svgOut,
gerbv_project_t *gerbvProject) {
85 if ((bg->red == 0xffff && bg->green == 0xffff && bg->blue == 0xffff)
86 || (bg->red == 0x0000 && bg->green == 0x0000 && bg->blue == 0x0000)) {
90 g_string_append_printf (svgOut,
91 " <rect x=\"0\" y=\"0\" width=\"100%%\" height=\"100%%\" fill=\"rgb(%u,%u,%u)\" />\n",
92 bg->red / 257, bg->green / 257, bg->blue / 257);
98 cairo_surface_t *cSurface = cairo_svg_surface_create (filename,
100 cairo_t *cairoTarget = cairo_create (cSurface);
102 gerbv_render_cairo_set_scale_and_translation (cairoTarget, renderInfo);
103 gerbv_render_layer_to_cairo_target_without_transforming (cairoTarget,
104 fileInfo, renderInfo, FALSE);
106 cairo_destroy (cairoTarget);
107 cairo_surface_destroy (cSurface);
111 exportimage_render_svg_layers_from_project (
gerbv_project_t *gerbvProject,
113 GString *svgOut = g_string_new (NULL);
114 gboolean hadError = FALSE;
117 g_string_append (svgOut,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
118 g_string_append_printf (svgOut,
119 "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" version=\"1.1\" width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\">\n",
123 exportimage_append_svg_background (svgOut, gerbvProject);
127 gchar *tmpSvgName = NULL;
128 gchar *tmpSvgText = NULL;
129 gchar *innerSvg = NULL;
130 gchar *layerLabelEscaped = NULL;
133 if (fileInfo == NULL || !fileInfo->
isVisible) {
137 tmpFd = g_file_open_tmp (
"gerbv-svg-layer-XXXXXX.svg", &tmpSvgName, NULL);
138 if (tmpFd < 0 || tmpSvgName == NULL) {
139 GERB_COMPILE_ERROR (_(
"Exporting error to file \"%s\""), filename);
146 exportimage_render_layer_to_svg_file (fileInfo, renderInfo, tmpSvgName);
148 if (!g_file_get_contents (tmpSvgName, &tmpSvgText, NULL, NULL)) {
149 GERB_COMPILE_ERROR (_(
"Exporting error to file \"%s\""), filename);
151 goto cleanup_layer_export;
154 innerSvg = exportimage_extract_svg_inner_content (tmpSvgText);
155 if (innerSvg == NULL) {
156 GERB_COMPILE_ERROR (_(
"Exporting error to file \"%s\""), filename);
158 goto cleanup_layer_export;
161 layerLabelEscaped = g_markup_escape_text (
162 fileInfo->
name ? fileInfo->
name : _(
"Unnamed layer"), -1);
164 g_string_append_printf (svgOut,
165 " <g inkscape:groupmode=\"layer\" inkscape:label=\"%s\">\n",
167 g_string_append (svgOut, innerSvg);
168 g_string_append (svgOut,
"\n");
169 g_string_append (svgOut,
" </g>\n");
171 cleanup_layer_export:
172 g_free (layerLabelEscaped);
175 g_unlink (tmpSvgName);
183 g_string_append (svgOut,
"</svg>\n");
185 if (!hadError && !g_file_set_contents (filename, svgOut->str, svgOut->len, NULL)) {
186 GERB_COMPILE_ERROR (_(
"Exporting error to file \"%s\""), filename);
189 g_string_free (svgOut, TRUE);
192 void exportimage_render_to_surface_and_destroy (
gerbv_project_t *gerbvProject,
194 cairo_t *cairoTarget = cairo_create (cSurface);
196 gerbv_render_all_layers_to_cairo_target_for_vector_output (gerbvProject, cairoTarget, renderInfo);
197 cairo_destroy (cairoTarget);
198 cairo_surface_destroy (cSurface);
203 gerbv_render_get_boundingbox(gerbvProject, &bb);
207 bb.
right += (tempWidth*0.05);
208 bb.
left -= (tempWidth*0.05);
209 bb.
bottom += (tempHeight*0.05);
210 bb.
top -= (tempHeight*0.05);
211 float width = bb.
right - bb.
left + 0.001;
212 float height = bb.
bottom - bb.
top + 0.001;
220 int heightInPixels, gchar
const* filename) {
223 widthInPixels, heightInPixels};
230 cairo_surface_t *cSurface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
232 cairo_t *cairoTarget = cairo_create (cSurface);
233 gerbv_render_all_layers_to_cairo_target (gerbvProject, cairoTarget, renderInfo);
234 if (CAIRO_STATUS_SUCCESS != cairo_surface_write_to_png (cSurface, filename)) {
235 GERB_COMPILE_ERROR (_(
"Exporting error to file \"%s\""), filename);
237 cairo_destroy (cairoTarget);
238 cairo_surface_destroy (cSurface);
247 gchar
const* filename) {
248 cairo_surface_t *cSurface = cairo_pdf_surface_create (filename, renderInfo->
displayWidth,
251 exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
260 gchar
const* filename) {
261 cairo_surface_t *cSurface = cairo_ps_surface_create (filename, renderInfo->
displayWidth,
263 exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
272 gchar
const* filename) {
277 gchar
const* filename, gboolean exportLayersAsSvgLayers) {
280 exportLayersAsSvgLayers);
285 gboolean exportLayersAsSvgLayers) {
289 exportLayersAsSvgLayers);
294 if (exportLayersAsSvgLayers) {
295 exportimage_render_svg_layers_from_project (gerbvProject, renderInfo, filename);
299 cairo_surface_t *cSurface = cairo_svg_surface_create (filename, renderInfo->
displayWidth,
301 exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
Header info for the cairo rendering functions and the related selection calculating functions.
void gerbv_export_png_file_from_project(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const *filename)
Render a project to a PNG file using user-specified render info.
void gerbv_export_postscript_file_from_project_autoscaled(gerbv_project_t *gerbvProject, gchar const *filename)
Render a project to a Postscript file, autoscaling the layers to fit inside the specified image dimen...
void gerbv_export_svg_file_from_project_autoscaled_with_options(gerbv_project_t *gerbvProject, gchar const *filename, gboolean exportLayersAsSvgLayers)
Render a project to a SVG file, optionally exporting visible layers as Inkscape layers.
void gerbv_export_svg_file_from_project(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const *filename)
Render a project to a file using user-specified render info.
void gerbv_export_postscript_file_from_project(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const *filename)
Render a project to a Postscript file using user-specified render info.
void gerbv_export_svg_file_from_project_with_options(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const *filename, gboolean exportLayersAsSvgLayers)
Render a project to a SVG file using user-specified render info and export options.
void gerbv_export_png_file_from_project_autoscaled(gerbv_project_t *gerbvProject, int widthInPixels, int heightInPixels, gchar const *filename)
Render a project to a PNG file, autoscaling the layers to fit inside the specified image dimensions.
void gerbv_export_pdf_file_from_project_autoscaled(gerbv_project_t *gerbvProject, gchar const *filename)
Render a project to a PDF file, autoscaling the layers to fit inside the specified image dimensions.
void gerbv_export_pdf_file_from_project(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const *filename)
Render a project to a PDF file using user-specified render info.
void gerbv_export_svg_file_from_project_autoscaled(gerbv_project_t *gerbvProject, gchar const *filename)
Render a project to a SVG file, autoscaling the layers to fit inside the specified image dimensions.
void export_svg_render_project(gerbv_project_t *project, gerbv_render_info_t *renderInfo, const gchar *filename, gboolean exportLayersAsSvgLayers)
Render the entire project to an optimized SVG file.
Header for optimized SVG export that bypasses Cairo's SVG surface.
void gerbv_render_zoom_to_fit_display(gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo)
Calculate the zoom and translations to fit the rendered scene inside the given scene size.
The main header file for the libgerbv library.
@ GERBV_RENDER_TYPE_CAIRO_HIGH_QUALITY
Header info for the rendering support functions for gerbv.