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
diff --git a/include/ezgl/canvas.hpp b/include/ezgl/canvas.hpp
index 2b03102..5bf4636 100644
--- a/include/ezgl/canvas.hpp
+++ b/include/ezgl/canvas.hpp
@@ -114,9 +114,9 @@
* @return returns true if the function has successfully generated the output file, otherwise
* failed due to errors such as out of memory occurs.
*/
- bool print_pdf(const char *file_name);
- bool print_svg(const char *file_name);
- bool print_png(const char *file_name);
+ bool print_pdf(const char *file_name, int width = 0, int height = 0);
+ bool print_svg(const char *file_name, int width = 0, int height = 0);
+ bool print_png(const char *file_name, int width = 0, int height = 0);
protected:
diff --git a/include/ezgl/rectangle.hpp b/include/ezgl/rectangle.hpp
index 60b4ec9..70138ea 100644
--- a/include/ezgl/rectangle.hpp
+++ b/include/ezgl/rectangle.hpp
@@ -40,17 +40,17 @@
/**
* Create a rectangle from two diagonally opposite points.
*/
- rectangle(point2d origin, point2d top_right) : m_first(origin), m_second(top_right)
+ rectangle(point2d origin_pt, point2d top_right_pt) : m_first(origin_pt), m_second(top_right_pt)
{
}
/**
* Create a rectangle with a given width and height.
*/
- rectangle(point2d origin, double width, double height) : m_first(origin), m_second(origin)
+ rectangle(point2d origin_pt, double rec_width, double rec_height) : m_first(origin_pt), m_second(origin_pt)
{
- m_second.x += width;
- m_second.y += height;
+ m_second.x += rec_width;
+ m_second.y += rec_height;
}
/**
diff --git a/src/canvas.cpp b/src/canvas.cpp
index 0b19732..fcf72f7 100644
--- a/src/canvas.cpp
+++ b/src/canvas.cpp
@@ -56,19 +56,26 @@
return context;
}
-bool canvas::print_pdf(const char *file_name)
+bool canvas::print_pdf(const char *file_name, int output_width, int output_height)
{
- cairo_surface_t *surface;
+ cairo_surface_t *pdf_surface;
cairo_t *context;
-
+ int surface_width = 0;
+ int surface_height = 0;
+
// create pdf surface based on canvas size
- int const width = gtk_widget_get_allocated_width(m_drawing_area);
- int const height = gtk_widget_get_allocated_height(m_drawing_area);
- surface = cairo_pdf_surface_create(file_name, width, height);
+ if(output_width == 0 && output_height == 0){
+ surface_width = gtk_widget_get_allocated_width(m_drawing_area);
+ surface_height = gtk_widget_get_allocated_height(m_drawing_area);
+ }else{
+ surface_width = output_width;
+ surface_height = output_height;
+ }
+ pdf_surface = cairo_pdf_surface_create(file_name, surface_width, surface_height);
- if(surface == NULL)
+ if(pdf_surface == NULL)
return false; // failed to create due to errors such as out of memory
- context = create_context(surface);
+ context = create_context(pdf_surface);
// draw on the newly created pdf surface & context
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
@@ -76,29 +83,38 @@
cairo_paint(context);
using namespace std::placeholders;
- renderer g(context, std::bind(&camera::world_to_screen, m_camera, _1), &m_camera, surface);
+ camera pdf_cam = m_camera;
+ pdf_cam.update_widget(surface_width, surface_height);
+ renderer g(context, std::bind(&camera::world_to_screen, pdf_cam, _1), &pdf_cam, pdf_surface);
m_draw_callback(g);
// free surface & context
- cairo_surface_destroy(surface);
+ cairo_surface_destroy(pdf_surface);
cairo_destroy(context);
return true;
}
-bool canvas::print_svg(const char *file_name)
+bool canvas::print_svg(const char *file_name, int output_width, int output_height)
{
- cairo_surface_t *surface;
+ cairo_surface_t *svg_surface;
cairo_t *context;
+ int surface_width = 0;
+ int surface_height = 0;
+
+ // create pdf surface based on canvas size
+ if(output_width == 0 && output_height == 0){
+ surface_width = gtk_widget_get_allocated_width(m_drawing_area);
+ surface_height = gtk_widget_get_allocated_height(m_drawing_area);
+ }else{
+ surface_width = output_width;
+ surface_height = output_height;
+ }
+ svg_surface = cairo_svg_surface_create(file_name, surface_width, surface_height);
- // create svg surface based on canvas size
- int const width = gtk_widget_get_allocated_width(m_drawing_area);
- int const height = gtk_widget_get_allocated_height(m_drawing_area);
- surface = cairo_svg_surface_create(file_name, width, height);
-
- if(surface == NULL)
+ if(svg_surface == NULL)
return false; // failed to create due to errors such as out of memory
- context = create_context(surface);
+ context = create_context(svg_surface);
// draw on the newly created svg surface & context
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
@@ -106,43 +122,55 @@
cairo_paint(context);
using namespace std::placeholders;
- renderer g(context, std::bind(&camera::world_to_screen, m_camera, _1), &m_camera, surface);
+ camera svg_cam = m_camera;
+ svg_cam.update_widget(surface_width, surface_height);
+ renderer g(context, std::bind(&camera::world_to_screen, svg_cam, _1), &svg_cam, svg_surface);
m_draw_callback(g);
// free surface & context
- cairo_surface_destroy(surface);
+ cairo_surface_destroy(svg_surface);
cairo_destroy(context);
return true;
}
-bool canvas::print_png(const char *file_name)
+bool canvas::print_png(const char *file_name, int output_width, int output_height)
{
- cairo_surface_t *surface;
+ cairo_surface_t *png_surface;
cairo_t *context;
+ int surface_width = 0;
+ int surface_height = 0;
+
+ // create pdf surface based on canvas size
+ if(output_width == 0 && output_height == 0){
+ surface_width = gtk_widget_get_allocated_width(m_drawing_area);
+ surface_height = gtk_widget_get_allocated_height(m_drawing_area);
+ }else{
+ surface_width = output_width;
+ surface_height = output_height;
+ }
+ png_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surface_width, surface_height);
- // create png surface based on canvas size
- int const width = gtk_widget_get_allocated_width(m_drawing_area);
- int const height = gtk_widget_get_allocated_height(m_drawing_area);
- surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
-
- if(surface == NULL)
+ if(png_surface == NULL)
return false; // failed to create due to errors such as out of memory
- context = create_context(surface);
+ context = create_context(png_surface);
// draw on the newly created png surface & context
cairo_set_source_rgb(context, m_background_color.red / 255.0, m_background_color.green / 255.0,
m_background_color.blue / 255.0);
cairo_paint(context);
+
using namespace std::placeholders;
- renderer g(context, std::bind(&camera::world_to_screen, m_camera, _1), &m_camera, surface);
+ camera png_cam = m_camera;
+ png_cam.update_widget(surface_width, surface_height);
+ renderer g(context, std::bind(&camera::world_to_screen, png_cam, _1), &png_cam, png_surface);
m_draw_callback(g);
// create png output file
- cairo_surface_write_to_png(surface, file_name);
+ cairo_surface_write_to_png(png_surface, file_name);
// free surface & context
- cairo_surface_destroy(surface);
+ cairo_surface_destroy(png_surface);
cairo_destroy(context);
return true;
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 1ef6aef..b7574c4 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -486,11 +486,11 @@
return;
// get the width and height of the drawn text
- cairo_text_extents_t text_extents{};
+ cairo_text_extents_t text_extents{0,0,0,0,0,0};
cairo_text_extents(m_cairo, text.c_str(), &text_extents);
// get more information about the font used
- cairo_font_extents_t font_extents{};
+ cairo_font_extents_t font_extents{0,0,0,0,0};
cairo_font_extents(m_cairo, &font_extents);
// get text width and height in world coordinates (text width and height are constant in widget coordinates)