Skip to content

Commit

Permalink
Add SVG and PDF output.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregersn committed Jun 9, 2024
1 parent 9ff43e7 commit c090eb5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 18 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@
"variant": "cpp",
"vector": "cpp",
"nonlinearoptimization": "cpp",
"ios": "cpp"
"ios": "cpp",
"charconv": "cpp",
"numbers": "cpp",
"__locale": "cpp",
"__verbose_abort": "cpp",
"execution": "cpp"
},
"mesonbuild.buildFolder": "build",
"terminal.integrated.env.linux": {
Expand Down
15 changes: 12 additions & 3 deletions include/pyro/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
#include "font.h"
#include "transformer.h"

#include <filesystem>

namespace Pyro
{
enum GraphicsMode
{
CAIRO
CAIRO,
PDF,
SVG,
};

class Graphics : public Image
{
protected:
GraphicsMode mode;
std::filesystem::path filename{""};
bool stroke_enable{true};
bool fill_enable{true};

Expand All @@ -35,7 +41,7 @@ namespace Pyro
Transformer2D transformer;

public:
Graphics(unsigned int width, unsigned int height, unsigned int channels, unsigned int dpi, Unit unit = Unit::PX);
Graphics(unsigned int width, unsigned int height, std::filesystem::path filename = "");
virtual ~Graphics() override;

virtual void init() override;
Expand Down Expand Up @@ -194,7 +200,10 @@ namespace Pyro
* @param width The width of the canvas
* @param height The height of the canvas
*/
Graphics *creategraphics(unsigned int width, unsigned int height, GraphicsMode mode = GraphicsMode::CAIRO);
Graphics *creategraphics(unsigned int width,
unsigned int height,
GraphicsMode mode = GraphicsMode::CAIRO,
std::filesystem::path filename = "");

};

Expand Down
4 changes: 3 additions & 1 deletion include/pyro/graphics_cairo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace Pyro
cairo_font_face_t *font{nullptr};

public:
GraphicsCairo(unsigned int width, unsigned int height, unsigned int channels, unsigned int dpi, Unit unit);
GraphicsCairo(unsigned int width, unsigned int height,
GraphicsMode mode = GraphicsMode::CAIRO,
std::filesystem::path filename = "");
~GraphicsCairo() override;
void init() override;

Expand Down
4 changes: 2 additions & 2 deletions include/pyro/pyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Pyro
// Drawing functions

// **** COLOR ****
inline void background(Color c) { pg->background(c); };
inline void background(const Color &c) { pg->background(c); };

inline void background(int c, int a = 255) { pg->background(c / 255.0f, c / 255.0f, c / 255.0f, a / 255.0); };
inline void background(int r, int g, int b, int a = 255) { pg->background(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); };
Expand All @@ -44,7 +44,7 @@ namespace Pyro
// TODO: clear
// TODO: colormode

inline void fill(Color c) { pg->fill(c); };
inline void fill(const Color &c) { pg->fill(c); };
inline void fill(float c, float a = 1.0) { pg->fill(c, c, c, a); };
inline void fill(float r, float g, float b, float a = 1.0) { pg->fill(r, g, b, a); };
inline void fill(int c, int a = 255) { pg->fill(c, a); };
Expand Down
18 changes: 13 additions & 5 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

namespace Pyro
{
Graphics::Graphics(unsigned int width, unsigned int height, unsigned int format, unsigned int dpi, Unit unit) : Image(width, height, format, 1, dpi, unit)
Graphics::Graphics(unsigned int width, unsigned int height, std::filesystem::path filename) : Image(width, height, ARGB)
{
this->filename = filename;
}

Graphics::~Graphics()
Expand All @@ -29,17 +30,24 @@ namespace Pyro
this->background(192);
}

Graphics *creategraphics(unsigned int width, unsigned int height, GraphicsMode mode)
Graphics *creategraphics(unsigned int width, unsigned int height, GraphicsMode mode, std::filesystem::path filename)
{

Graphics *g = nullptr;
switch (mode)
{
case GraphicsMode::SVG:
case GraphicsMode::PDF:
assert(filename != "");
g = new GraphicsCairo(width, height, mode, filename);
break;
case GraphicsMode::CAIRO:
default:
Graphics *g = new GraphicsCairo(width, height, ARGB, 72, Unit::PX);
g->init();
return g;
g = new GraphicsCairo(width, height);
}
assert(g != nullptr);
g->init();
return g;
}

void Graphics::point(float x, float y, Unit unit)
Expand Down
25 changes: 20 additions & 5 deletions src/graphics_cairo.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#include "pyro/graphics_cairo.h"
#include "pyro/font_impl.h"
#include "pyro/utils.h"
#include <cairo-svg.h>
#include <cairo-pdf.h>

namespace Pyro
{
GraphicsCairo::GraphicsCairo(unsigned int width, unsigned int height, unsigned int format, unsigned int dpi, Unit unit) : Graphics(width, height, format, dpi, unit)
GraphicsCairo::GraphicsCairo(unsigned int width, unsigned int height, GraphicsMode mode, std::filesystem::path filename) : Graphics(width, height, filename)
{
this->mode = mode;
}

void GraphicsCairo::init()
{
Image::init();
this->surface = cairo_image_surface_create_for_data(this->load_bytes(),
CAIRO_FORMAT_ARGB32,
this->_pixelwidth, this->_pixelheight,
this->_pixelwidth * 4);
switch (this->mode)
{
case SVG:
this->surface = cairo_svg_surface_create(filename.c_str(), _width, _height);
break;
case PDF:
this->surface = cairo_pdf_surface_create(filename.c_str(), _width, _height);
break;
default:
this->surface = cairo_image_surface_create_for_data(this->load_bytes(),
CAIRO_FORMAT_ARGB32,
this->_pixelwidth, this->_pixelheight,
this->_pixelwidth * 4);
break;
}
this->cr = cairo_create(this->surface);
Graphics::init();
}
Expand Down
2 changes: 1 addition & 1 deletion src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ namespace Pyro
// Utility functions
Image *createimage(unsigned int width, unsigned int height, int format)
{
Image *img = new Image(width, height, format, 1, 72, Unit::PX);
Image *img = new Image(width, height, format);
img->init();
return img;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/test-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,25 @@ TEST_CASE("Save and load images", "[graphics]")
remove(filename);
}
}

TEST_CASE("Use different modes", "[graphics]")
{
SECTION("SVG")
{
Pyro::Graphics *svg = Pyro::creategraphics(640, 480, Pyro::GraphicsMode::SVG, "test-file.svg");
svg->rect(10, 10, 20, 20);
REQUIRE(svg);
delete svg;
std::remove("test-file.svg");
}

SECTION("PDF")
{

Pyro::Graphics *pdf = Pyro::creategraphics(640, 480, Pyro::GraphicsMode::PDF, "test-file.pdf");
pdf->rect(10, 10, 20, 20);
REQUIRE(pdf);
delete pdf;
std::remove("test-file.pdf");
}
}

0 comments on commit c090eb5

Please sign in to comment.