Skip to content

Commit 469a8cb

Browse files
committed
Squashed 'libs/EXTERNAL/libezgl/' changes from 172bcb4..e6f62b3
e6f62b3 enable saving graphics without getting into the event loop (#11) 5243fa0 Merge pull request #12 from mariobadr/fix-warnings 45e4bba fix more warning messages in graphics.cpp and canvas.cpp 17557e4 change variable names in rectangle.hpp to avoid shallowing warnings git-subtree-dir: libs/EXTERNAL/libezgl git-subtree-split: e6f62b3a4f88bd523a8b77af1494839f83f62bc7
1 parent c9da385 commit 469a8cb

File tree

4 files changed

+70
-42
lines changed

4 files changed

+70
-42
lines changed

include/ezgl/canvas.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ class canvas {
114114
* @return returns true if the function has successfully generated the output file, otherwise
115115
* failed due to errors such as out of memory occurs.
116116
*/
117-
bool print_pdf(const char *file_name);
118-
bool print_svg(const char *file_name);
119-
bool print_png(const char *file_name);
117+
bool print_pdf(const char *file_name, int width = 0, int height = 0);
118+
bool print_svg(const char *file_name, int width = 0, int height = 0);
119+
bool print_png(const char *file_name, int width = 0, int height = 0);
120120

121121

122122
protected:

include/ezgl/rectangle.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ class rectangle {
4040
/**
4141
* Create a rectangle from two diagonally opposite points.
4242
*/
43-
rectangle(point2d origin, point2d top_right) : m_first(origin), m_second(top_right)
43+
rectangle(point2d origin_pt, point2d top_right_pt) : m_first(origin_pt), m_second(top_right_pt)
4444
{
4545
}
4646

4747
/**
4848
* Create a rectangle with a given width and height.
4949
*/
50-
rectangle(point2d origin, double width, double height) : m_first(origin), m_second(origin)
50+
rectangle(point2d origin_pt, double rec_width, double rec_height) : m_first(origin_pt), m_second(origin_pt)
5151
{
52-
m_second.x += width;
53-
m_second.y += height;
52+
m_second.x += rec_width;
53+
m_second.y += rec_height;
5454
}
5555

5656
/**

src/canvas.cpp

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,93 +56,121 @@ static cairo_t *create_context(cairo_surface_t *p_surface)
5656
return context;
5757
}
5858

59-
bool canvas::print_pdf(const char *file_name)
59+
bool canvas::print_pdf(const char *file_name, int output_width, int output_height)
6060
{
61-
cairo_surface_t *surface;
61+
cairo_surface_t *pdf_surface;
6262
cairo_t *context;
63-
63+
int surface_width = 0;
64+
int surface_height = 0;
65+
6466
// 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);
6875

69-
if(surface == NULL)
76+
if(pdf_surface == NULL)
7077
return false; // failed to create due to errors such as out of memory
71-
context = create_context(surface);
78+
context = create_context(pdf_surface);
7279

7380
// draw on the newly created pdf surface & context
7481
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
7582
m_background_color.blue / 255.0);
7683
cairo_paint(context);
7784

7885
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);
8089
m_draw_callback(g);
8190

8291
// free surface & context
83-
cairo_surface_destroy(surface);
92+
cairo_surface_destroy(pdf_surface);
8493
cairo_destroy(context);
8594

8695
return true;
8796
}
8897

89-
bool canvas::print_svg(const char *file_name)
98+
bool canvas::print_svg(const char *file_name, int output_width, int output_height)
9099
{
91-
cairo_surface_t *surface;
100+
cairo_surface_t *svg_surface;
92101
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);
93114

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)
100116
return false; // failed to create due to errors such as out of memory
101-
context = create_context(surface);
117+
context = create_context(svg_surface);
102118

103119
// draw on the newly created svg surface & context
104120
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
105121
m_background_color.blue / 255.0);
106122
cairo_paint(context);
107123

108124
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);
110128
m_draw_callback(g);
111129

112130
// free surface & context
113-
cairo_surface_destroy(surface);
131+
cairo_surface_destroy(svg_surface);
114132
cairo_destroy(context);
115133

116134
return true;
117135
}
118136

119-
bool canvas::print_png(const char *file_name)
137+
bool canvas::print_png(const char *file_name, int output_width, int output_height)
120138
{
121-
cairo_surface_t *surface;
139+
cairo_surface_t *png_surface;
122140
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);
123153

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)
130155
return false; // failed to create due to errors such as out of memory
131-
context = create_context(surface);
156+
context = create_context(png_surface);
132157

133158
// draw on the newly created png surface & context
134159
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
135160
m_background_color.blue / 255.0);
136161
cairo_paint(context);
162+
137163
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);
139167
m_draw_callback(g);
140168

141169
// create png output file
142-
cairo_surface_write_to_png(surface, file_name);
170+
cairo_surface_write_to_png(png_surface, file_name);
143171

144172
// free surface & context
145-
cairo_surface_destroy(surface);
173+
cairo_surface_destroy(png_surface);
146174
cairo_destroy(context);
147175

148176
return true;

src/graphics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ void renderer::draw_text(point2d point, std::string const &text, double bound_x,
486486
return;
487487

488488
// get the width and height of the drawn text
489-
cairo_text_extents_t text_extents{};
489+
cairo_text_extents_t text_extents{0,0,0,0,0,0};
490490
cairo_text_extents(m_cairo, text.c_str(), &text_extents);
491491

492492
// get more information about the font used
493-
cairo_font_extents_t font_extents{};
493+
cairo_font_extents_t font_extents{0,0,0,0,0};
494494
cairo_font_extents(m_cairo, &font_extents);
495495

496496
// get text width and height in world coordinates (text width and height are constant in widget coordinates)

0 commit comments

Comments
 (0)