@@ -56,93 +56,121 @@ static cairo_t *create_context(cairo_surface_t *p_surface)
56
56
return context;
57
57
}
58
58
59
- bool canvas::print_pdf (const char *file_name)
59
+ bool canvas::print_pdf (const char *file_name, int output_width, int output_height )
60
60
{
61
- cairo_surface_t *surface ;
61
+ cairo_surface_t *pdf_surface ;
62
62
cairo_t *context;
63
-
63
+ int surface_width = 0 ;
64
+ int surface_height = 0 ;
65
+
64
66
// create pdf surface based on canvas size
65
- int const width = gtk_widget_get_allocated_width (m_drawing_area);
66
- int const height = gtk_widget_get_allocated_height (m_drawing_area);
67
- surface = cairo_pdf_surface_create (file_name, width, height);
67
+ if (output_width == 0 && output_height == 0 ){
68
+ surface_width = gtk_widget_get_allocated_width (m_drawing_area);
69
+ surface_height = gtk_widget_get_allocated_height (m_drawing_area);
70
+ }else {
71
+ surface_width = output_width;
72
+ surface_height = output_height;
73
+ }
74
+ pdf_surface = cairo_pdf_surface_create (file_name, surface_width, surface_height);
68
75
69
- if (surface == NULL )
76
+ if (pdf_surface == NULL )
70
77
return false ; // failed to create due to errors such as out of memory
71
- context = create_context (surface );
78
+ context = create_context (pdf_surface );
72
79
73
80
// draw on the newly created pdf surface & context
74
81
cairo_set_source_rgb (context, m_background_color.red / 255.0 , m_background_color.green / 255.0 ,
75
82
m_background_color.blue / 255.0 );
76
83
cairo_paint (context);
77
84
78
85
using namespace std ::placeholders;
79
- renderer g (context, std::bind (&camera::world_to_screen, m_camera, _1), &m_camera, surface);
86
+ camera pdf_cam = m_camera;
87
+ pdf_cam.update_widget (surface_width, surface_height);
88
+ renderer g (context, std::bind (&camera::world_to_screen, pdf_cam, _1), &pdf_cam, pdf_surface);
80
89
m_draw_callback (g);
81
90
82
91
// free surface & context
83
- cairo_surface_destroy (surface );
92
+ cairo_surface_destroy (pdf_surface );
84
93
cairo_destroy (context);
85
94
86
95
return true ;
87
96
}
88
97
89
- bool canvas::print_svg (const char *file_name)
98
+ bool canvas::print_svg (const char *file_name, int output_width, int output_height )
90
99
{
91
- cairo_surface_t *surface ;
100
+ cairo_surface_t *svg_surface ;
92
101
cairo_t *context;
102
+ int surface_width = 0 ;
103
+ int surface_height = 0 ;
104
+
105
+ // create pdf surface based on canvas size
106
+ if (output_width == 0 && output_height == 0 ){
107
+ surface_width = gtk_widget_get_allocated_width (m_drawing_area);
108
+ surface_height = gtk_widget_get_allocated_height (m_drawing_area);
109
+ }else {
110
+ surface_width = output_width;
111
+ surface_height = output_height;
112
+ }
113
+ svg_surface = cairo_svg_surface_create (file_name, surface_width, surface_height);
93
114
94
- // create svg surface based on canvas size
95
- int const width = gtk_widget_get_allocated_width (m_drawing_area);
96
- int const height = gtk_widget_get_allocated_height (m_drawing_area);
97
- surface = cairo_svg_surface_create (file_name, width, height);
98
-
99
- if (surface == NULL )
115
+ if (svg_surface == NULL )
100
116
return false ; // failed to create due to errors such as out of memory
101
- context = create_context (surface );
117
+ context = create_context (svg_surface );
102
118
103
119
// draw on the newly created svg surface & context
104
120
cairo_set_source_rgb (context, m_background_color.red / 255.0 , m_background_color.green / 255.0 ,
105
121
m_background_color.blue / 255.0 );
106
122
cairo_paint (context);
107
123
108
124
using namespace std ::placeholders;
109
- renderer g (context, std::bind (&camera::world_to_screen, m_camera, _1), &m_camera, surface);
125
+ camera svg_cam = m_camera;
126
+ svg_cam.update_widget (surface_width, surface_height);
127
+ renderer g (context, std::bind (&camera::world_to_screen, svg_cam, _1), &svg_cam, svg_surface);
110
128
m_draw_callback (g);
111
129
112
130
// free surface & context
113
- cairo_surface_destroy (surface );
131
+ cairo_surface_destroy (svg_surface );
114
132
cairo_destroy (context);
115
133
116
134
return true ;
117
135
}
118
136
119
- bool canvas::print_png (const char *file_name)
137
+ bool canvas::print_png (const char *file_name, int output_width, int output_height )
120
138
{
121
- cairo_surface_t *surface ;
139
+ cairo_surface_t *png_surface ;
122
140
cairo_t *context;
141
+ int surface_width = 0 ;
142
+ int surface_height = 0 ;
143
+
144
+ // create pdf surface based on canvas size
145
+ if (output_width == 0 && output_height == 0 ){
146
+ surface_width = gtk_widget_get_allocated_width (m_drawing_area);
147
+ surface_height = gtk_widget_get_allocated_height (m_drawing_area);
148
+ }else {
149
+ surface_width = output_width;
150
+ surface_height = output_height;
151
+ }
152
+ png_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, surface_width, surface_height);
123
153
124
- // create png surface based on canvas size
125
- int const width = gtk_widget_get_allocated_width (m_drawing_area);
126
- int const height = gtk_widget_get_allocated_height (m_drawing_area);
127
- surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
128
-
129
- if (surface == NULL )
154
+ if (png_surface == NULL )
130
155
return false ; // failed to create due to errors such as out of memory
131
- context = create_context (surface );
156
+ context = create_context (png_surface );
132
157
133
158
// draw on the newly created png surface & context
134
159
cairo_set_source_rgb (context, m_background_color.red / 255.0 , m_background_color.green / 255.0 ,
135
160
m_background_color.blue / 255.0 );
136
161
cairo_paint (context);
162
+
137
163
using namespace std ::placeholders;
138
- renderer g (context, std::bind (&camera::world_to_screen, m_camera, _1), &m_camera, surface);
164
+ camera png_cam = m_camera;
165
+ png_cam.update_widget (surface_width, surface_height);
166
+ renderer g (context, std::bind (&camera::world_to_screen, png_cam, _1), &png_cam, png_surface);
139
167
m_draw_callback (g);
140
168
141
169
// create png output file
142
- cairo_surface_write_to_png (surface , file_name);
170
+ cairo_surface_write_to_png (png_surface , file_name);
143
171
144
172
// free surface & context
145
- cairo_surface_destroy (surface );
173
+ cairo_surface_destroy (png_surface );
146
174
cairo_destroy (context);
147
175
148
176
return true ;
0 commit comments