gerbv  2.6A
exportimage.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * This file is a part of gerbv.
4  *
5  * Copyright (C) 2000-2002 Stefan Petersen (spe@stacken.kth.se)
6  *
7  * Contributed by Dino Ghilardi <dino.ghilardi@ieee.org>
8  *
9  * $Id$
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
24  */
25 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 #include <math.h>
40 #include <gdk-pixbuf/gdk-pixbuf.h>
41 #include <png.h>
42 
43 #include "common.h"
44 #include "gerbv.h"
45 
46 #include "draw.h"
47 #include <cairo.h>
48 #include <cairo-pdf.h>
49 #include <cairo-ps.h>
50 #include <cairo-svg.h>
51 
52 extern gerbv_render_info_t screenRenderInfo;
53 
54 void exportimage_render_to_surface_and_destroy (gerbv_project_t *gerbvProject,
55  cairo_surface_t *cSurface, gerbv_render_info_t *renderInfo, gchar const* filename) {
56  cairo_t *cairoTarget = cairo_create (cSurface);
57 
58  gerbv_render_all_layers_to_cairo_target_for_vector_output (gerbvProject, cairoTarget, renderInfo);
59  cairo_destroy (cairoTarget);
60  cairo_surface_destroy (cSurface);
61 }
62 
63 gerbv_render_info_t gerbv_export_autoscale_project (gerbv_project_t *gerbvProject) {
65  gerbv_render_get_boundingbox(gerbvProject, &bb);
66  // add a border around the bounding box
67  gfloat tempWidth = bb.right - bb.left;
68  gfloat tempHeight = bb.bottom - bb.top;
69  bb.right += (tempWidth*0.05);
70  bb.left -= (tempWidth*0.05);
71  bb.bottom += (tempHeight*0.05);
72  bb.top -= (tempHeight*0.05);
73  float width = bb.right - bb.left + 0.001; // Plus a little extra to prevent from
74  float height = bb.bottom - bb.top + 0.001; // missing items due to round-off errors
75 
76  gerbv_render_info_t renderInfo = {72, 72, bb.left, bb.top,
77  GERBV_RENDER_TYPE_CAIRO_HIGH_QUALITY, width*72, height*72};
78  return renderInfo;
79 }
80 
81 void gerbv_export_png_file_from_project_autoscaled (gerbv_project_t *gerbvProject, int widthInPixels,
82  int heightInPixels, gchar const* filename) {
83  gerbv_render_info_t renderInfo = {1, 1, 0, 0,
85  widthInPixels, heightInPixels};
86 
87  gerbv_render_zoom_to_fit_display (gerbvProject, &renderInfo);
88  gerbv_export_png_file_from_project (gerbvProject, &renderInfo, filename);
89 }
90 
91 void gerbv_export_png_file_from_project (gerbv_project_t *gerbvProject, gerbv_render_info_t *renderInfo, gchar const* filename) {
92  cairo_surface_t *cSurface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
93  renderInfo->displayWidth, renderInfo->displayHeight);
94  cairo_t *cairoTarget = cairo_create (cSurface);
95  gerbv_render_all_layers_to_cairo_target (gerbvProject, cairoTarget, renderInfo);
96  if (CAIRO_STATUS_SUCCESS != cairo_surface_write_to_png (cSurface, filename)) {
97  GERB_COMPILE_ERROR (_("Exporting error to file \"%s\""), filename);
98  }
99  cairo_destroy (cairoTarget);
100  cairo_surface_destroy (cSurface);
101 }
102 
103 void gerbv_export_pdf_file_from_project_autoscaled (gerbv_project_t *gerbvProject, gchar const* filename) {
104  gerbv_render_info_t renderInfo = gerbv_export_autoscale_project(gerbvProject);
105  gerbv_export_pdf_file_from_project (gerbvProject, &renderInfo, filename);
106 }
107 
109  gchar const* filename) {
110  cairo_surface_t *cSurface = cairo_pdf_surface_create (filename, renderInfo->displayWidth,
111  renderInfo->displayHeight);
112 
113  exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
114 }
115 
116 void gerbv_export_postscript_file_from_project_autoscaled (gerbv_project_t *gerbvProject, gchar const* filename) {
117  gerbv_render_info_t renderInfo = gerbv_export_autoscale_project(gerbvProject);
118  gerbv_export_postscript_file_from_project (gerbvProject, &renderInfo, filename);
119 }
120 
122  gchar const* filename) {
123  cairo_surface_t *cSurface = cairo_ps_surface_create (filename, renderInfo->displayWidth,
124  renderInfo->displayHeight);
125  exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
126 }
127 
128 void gerbv_export_svg_file_from_project_autoscaled (gerbv_project_t *gerbvProject, gchar const* filename) {
129  gerbv_render_info_t renderInfo = gerbv_export_autoscale_project(gerbvProject);
130  gerbv_export_svg_file_from_project (gerbvProject, &renderInfo, filename);
131 }
132 
134  gchar const* filename) {
135  cairo_surface_t *cSurface = cairo_svg_surface_create (filename, renderInfo->displayWidth,
136  renderInfo->displayHeight);
137  exportimage_render_to_surface_and_destroy (gerbvProject, cSurface, renderInfo, filename);
138 }
139